Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
•All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
env_map_gent.cpp
Go to the documentation of this file.
1 #include "endianness.h"
2 #include <float.h>
3 #include <stdio.h>
4 #include <math.h>
5 #include <stdlib.h>
6 #include <assert.h>
7 #include <string.h>
8 #include "vsfilesystem.h"
9 #include "vsimage.h"
10 #include "aux_texture.h"
11 #include "gldrv/sdds.h"
12 #ifndef WIN32
13 #else
14 #ifndef NOMINMAX
15 #define NOMINMAX
16 #endif //tells VCC not to generate min/max macros
17 #include <windows.h>
18 #include <wingdi.h>
19 #endif
20 
21 #if _MSC_VER >= 1300
22 #define snprintf _snprintf
23 #endif
24 
25 static char *InputName = NULL;
26 static char *OutputName = NULL;
27 static bool pushdown = false;
28 static float affine = 0;
29 static float multiplicitive = 1;
30 static float power = 1;
31 
32 using namespace VSFileSystem;
33 
34 /*
35 struct Vector {
36  float i;
37  float j;
38  float k;
39 };
40 
41 #define NumLights 1
42 
43 struct RGBColor
44 {
45  float r, b, g;
46 };
47 
48 struct Light
49 {
50  Vector Dir;
51  RGBColor Ambient;
52  RGBColor Intensity;
53 };
54 
55 struct Material
56 {
57  RGBColor Ka;
58  RGBColor Kd;
59  RGBColor Ks;
60  int exp;
61 };
62 
63 static Light L[NumLights];
64 static Material M;
65 
66 static float Power( float A, int B )
67 {
68  float res = 1.;
69  for (int i = 0; i < B; i++)
70  res *= A;
71  return res;
72 }
73 
74 static void Lighting( RGBColor &Col, const Vector &Norm )
75 {
76  static float OONL = 1./NumLights;
77  Col.r = Col.g = Col.b = 0;
78  for (int i = 0; i < NumLights; i++) {
79  float dot = L[i].Dir.i*Norm.i+L[i].Dir.j*Norm.j+L[i].Dir.k*Norm.k;
80  if (dot < 0) dot = 0;
81  Col.r += OONL*L[i].Ambient.r*M.Ka.r+L[i].Intensity.r*( M.Kd.r*dot+M.Ks.r*Power( dot, M.exp ) );
82  Col.g += OONL*L[i].Ambient.g*M.Ka.g+L[i].Intensity.g*( M.Kd.g*dot+M.Ks.g*Power( dot, M.exp ) );
83  Col.b += OONL*L[i].Ambient.b*M.Ka.b+L[i].Intensity.b*( M.Kd.b*dot+M.Ks.b*Power( dot, M.exp ) );
84  if (Col.r > 1) Col.r = 1;
85  if (Col.b > 1) Col.b = 1;
86  if (Col.g > 1) Col.g = 1;
87  if (Col.r < 0) Col.r = 0;
88  if (Col.b < 0) Col.b = 0;
89  if (Col.g < 0) Col.g = 0;
90  }
91 }
92 */
93 
94 const int lmwid = 512;
95 const int lmwido2 = lmwid/2;
96 const float ooWIDTHo2 = 2./lmwid;
97 const float PIoWIDTHo2 = 2*3.1415926535/lmwid;
98 const char bytepp = 3;
99 
100 struct CubeCoord
101 {
102  float s;
103  float t;
104  unsigned int TexMap; //0 = front, 1=back,2=right,3=left,4=up,5=down
105  unsigned int padding; //added by chuck_starchaser
106 };
107 
108 static void gluSphereMap( CubeCoord &Tex, Vector Normal, float Theta )
109 {
110  Tex.TexMap = 0;
111  float vert = Normal.j;
112  if (!pushdown) {
113  Tex.t = vert*lmwido2+lmwido2;
114  Tex.s = Theta;
115  } else {
116  Tex.t = ( (int) (vert*lmwido2) )%lmwid;
117  Tex.s = Theta;
118  }
119 }
120 
121 static void TexMap( CubeCoord &Tex, Vector Normal )
122 {
123  float r[6];
124  Normal.i = Normal.i;
125  Normal.j = -Normal.j;
126  Normal.k = -Normal.k;
127  const float CubeSize = lmwido2; //half of the length of any of the cube's sides
128  if (Normal.k)
129  r[0] = CubeSize/Normal.k; //find what you need to multiply to get to the cube
130  if (Normal.i)
131  r[2] = CubeSize/Normal.i; //find what you need to multiply to get to the cube
132  if (Normal.j)
133  r[4] = CubeSize/Normal.j; //find what you need to multiply to get to the cube
134  if (!Normal.k)
135  r[0] = r[1] = CubeSize*1000;
136  if (!Normal.i)
137  r[2] = r[3] = CubeSize*1000;
138  if (!Normal.j)
139  r[4] = r[5] = CubeSize*1000;
140  r[1] = -r[0];
141  r[3] = -r[2];
142  r[5] = -r[4];
143 
144  float rf;
145  rf = CubeSize*1000;
146  for (int i = 0; i < 6; i++)
147  if (r[i] >= CubeSize) {
148  if (rf > r[i]) {
149  rf = r[i];
150  Tex.TexMap = i;
151  }
152  }
154  switch (Tex.TexMap)
155  {
156  case 0:
157  Tex.s = rf*Normal.i+lmwido2; //btw 0 and 256
158  Tex.t = lmwido2-rf*Normal.j; //top left is 0,0
159  break;
160  case 1:
161  Tex.s = lmwido2-rf*Normal.i; //btw 0 and 256
162  Tex.t = lmwido2-rf*Normal.j; //top left is 0,0
163  break;
164  case 2:
165  Tex.s = lmwido2-rf*Normal.k;
166  Tex.t = lmwido2-rf*Normal.j;
167  break;
168  case 3:
169  Tex.s = lmwido2+rf*Normal.k;
170  Tex.t = lmwido2-rf*Normal.j;
171  break;
172  case 4:
173  Tex.t = lmwido2-rf*Normal.i;
174  Tex.s = lmwido2+rf*Normal.k;
175  break;
176  case 5:
177  Tex.s = lmwido2-rf*Normal.i;
178  Tex.t = lmwido2+rf*Normal.k;
179  break;
180  }
181 }
182 
183 static bool LoadTex( char *FileName, unsigned char scdata[lmwid][lmwid][3] )
184 {
185  using namespace VSFileSystem;
186 
187  unsigned char ctemp;
188  VSFile f;
189  VSError err = f.OpenReadOnly( FileName, TextureFile );
190  if (err > Ok)
191  return false;
192  Texture tex;
193  unsigned char *data = tex.ReadImage( &f, texTransform, true );
194  int bpp = tex.Depth();
195  int format = tex.Format();
196 
197  unsigned char *buffer = NULL;
198  bpp /= 8;
199  //999 is the code for DDS file, we must decompress them.
200  if (format == 999) {
201  unsigned char *tmpbuffer = data+2;
202  TEXTUREFORMAT internformat;
203  bpp = 1;
204  //Make sure we are reading a DXT1 file. All backgrounds
205  //should be DXT1
206  switch (tex.mode)
207  {
208  case::VSImage::_DXT1:
209  internformat = DXT1;
210  break;
211  default:
212  return false;
213  }
214  //we could hardware decompress, but that involves more
215  //pollution of gl in gfx.
216  ddsDecompress( tmpbuffer, buffer, internformat, tex.sizeY, tex.sizeX );
217  //We are done with the DDS file data. Remove it.
218  free( data );
219  data = buffer;
220 
221  //stride and row_pointers are used for the texTransform
222  unsigned long stride = 4*sizeof (unsigned char);
223  unsigned char **row_pointers = (unsigned char**) malloc( sizeof (unsigned char*)*tex.sizeY );
224  for (unsigned int i = 0; i < tex.sizeY; ++i)
225  row_pointers[i] = &data[i*stride*tex.sizeX];
226  //texTransform demands that the first argument (bpp) be 8. So we abide
227  int tmp = 8;
228  int tmp2 = PNG_HAS_COLOR+PNG_HAS_ALPHA;
229  buffer = texTransform( tmp, tmp2, tex.sizeX, tex.sizeY, row_pointers );
230  //We're done with row_pointers, free it
231  free( row_pointers );
232  row_pointers = NULL;
233  //We're done with the decompressed dds data, free it
234  free( data );
235  //We set data to the transformed image data
236  data = buffer;
237  buffer = NULL;
238  //it's 3 because 24/8
239  bpp = 4;
240  } else if (format&PNG_HAS_ALPHA) {
241  bpp *= 4;
242  } else {
243  bpp *= 3;
244  }
245  if (data) {
246  int ii;
247  int jj;
248  for (int i = 0; i < lmwid; i++) {
249  ii = (i*tex.sizeY)/lmwid;
250  for (int j = 0; j < lmwid; j++) {
251  jj = (j*tex.sizeX)/lmwid;
252  scdata[i][j][0] = data[(ii*tex.sizeX+jj)*bpp];
253  scdata[i][j][1] = data[(ii*tex.sizeX+jj)*bpp+1];
254  scdata[i][j][2] = data[(ii*tex.sizeX+jj)*bpp+2];
255  }
256  }
257  free( data );
258  } else {
259  return false;
260  }
261  f.Close();
262  return true;
263 }
264 
265 struct Texmp
266 {
267  unsigned char D[lmwid][lmwid][3];
268 };
269 
270 static char * makebgname( char *tmp, size_t size, const char *InputName, const char *add, const char *suffix )
271 {
272  size_t len=strlen(InputName)+strlen(add)+strlen(suffix)+1;
273  if (size>len) {
274  strcpy(tmp,InputName);
275  strcat(tmp,add);
276  strcat(tmp,suffix);
277  }else {
278  std::string temp = InputName;
279  temp+=add;
280  temp+=suffix;
281  strncpy(tmp,temp.c_str(),size);
282  tmp[size-1]='\0';
283  }
284  return tmp;
285 }
286 
287 static void Spherize( CubeCoord Tex[lmwid][lmwid], CubeCoord gluSph[lmwid][lmwid], unsigned char Col[] )
288 {
289  Texmp *Data = NULL;
290  bool sphere = false;
291  Data = new Texmp[6];
292  if (!Data)
293  return; //borken down and down Data[5], right Data[3]
294 
295  size_t tmpsize = strlen( InputName )+60;
296  char *tmp = (char*) malloc( tmpsize );
297  const char *suffix = ".image";
298  {
299  std::string temp( InputName );
300  if (VSFileSystem::LookForFile( temp+"_up.image", TextureFile ) > VSFileSystem::Ok) {
301  //greater than Ok means failed to load.
302  if (VSFileSystem::LookForFile( temp+"_sphere.image", TextureFile ) > VSFileSystem::Ok)
304  suffix = ".bmp";
305  }
306  //backwards compatibility
307  }
308  if ( !( LoadTex( makebgname( tmp, tmpsize, InputName, "_front",
309  suffix ),
310  Data[0].D )
311  && LoadTex( makebgname( tmp, tmpsize, InputName, "_back",
312  suffix ),
313  Data[1].D )
314  && LoadTex( makebgname( tmp, tmpsize, InputName, "_left",
315  suffix ),
316  Data[2].D )
317  && LoadTex( makebgname( tmp, tmpsize, InputName, "_right",
318  suffix ),
319  Data[3].D )
320  && LoadTex( makebgname( tmp, tmpsize, InputName, "_up",
321  suffix ),
322  Data[4].D )
323  && LoadTex( makebgname( tmp, tmpsize, InputName, "_down",
324  suffix ), Data[5].D ) ) )
325  {
326  if ( !LoadTex( makebgname( tmp, tmpsize, InputName, "_sphere", suffix ), Data[0].D ) )
327  LoadTex( makebgname( tmp, tmpsize, InputName, "", suffix ), Data[0].D );
328  sphere = true;
329  Tex = gluSph;
330  }
331  free( tmp );
332  tmp = NULL;
333  //int NumPix;
334  float sleft, sright, tdown, tup;
335  for (int t = 0; t < lmwid; t++)
336  for (int s = 0; s < lmwid; s++) {
337  float r = 0;
338  float g = 0;
339  float b = 0;
340  {
341  float avg = 1;
342  if ( (int) floor( Tex[t][s].s ) > lmwid-1 )
343  Tex[t][s].s = lmwid-1;
344  if ( (int) floor( Tex[t][s].t ) > lmwid-1 )
345  Tex[t][s].t = lmwid-1;
346  if ( (int) floor( Tex[t][s].t ) < 0 )
347  Tex[t][s].t = 0;
348  if ( (int) floor( Tex[t][s].s ) < 0 )
349  Tex[t][s].s = 0;
350  r = Data[Tex[t][s].TexMap].D[(int) floor( Tex[t][s].t )][(int) floor( Tex[t][s].s )][0];
351  g = Data[Tex[t][s].TexMap].D[(int) floor( Tex[t][s].t )][(int) floor( Tex[t][s].s )][1];
352  b = Data[Tex[t][s].TexMap].D[(int) floor( Tex[t][s].t )][(int) floor( Tex[t][s].s )][2];
353  if ( (int) floor( Tex[t][s].s ) < lmwid-1 ) {
354  avg++;
355  r += Data[Tex[t][s].TexMap].D[(int) floor( Tex[t][s].t )][(int) floor( Tex[t][s].s+1 )][0];
356  g += Data[Tex[t][s].TexMap].D[(int) floor( Tex[t][s].t )][(int) floor( Tex[t][s].s+1 )][1];
357  b += Data[Tex[t][s].TexMap].D[(int) floor( Tex[t][s].t )][(int) floor( Tex[t][s].s+1 )][2];
358  }
359  if ( (int) floor( Tex[t][s].t ) < lmwid-1 ) {
360  avg++;
361  r += Data[Tex[t][s].TexMap].D[(int) floor( Tex[t][s].t+1 )][(int) floor( Tex[t][s].s )][0];
362  g += Data[Tex[t][s].TexMap].D[(int) floor( Tex[t][s].t+1 )][(int) floor( Tex[t][s].s )][1];
363  b += Data[Tex[t][s].TexMap].D[(int) floor( Tex[t][s].t+1 )][(int) floor( Tex[t][s].s )][2];
364  }
365  if ( (int) floor( Tex[t][s].t ) > 0 ) {
366  avg++;
367  r += Data[Tex[t][s].TexMap].D[(int) floor( Tex[t][s].t-1 )][(int) floor( Tex[t][s].s )][0];
368  g += Data[Tex[t][s].TexMap].D[(int) floor( Tex[t][s].t-1 )][(int) floor( Tex[t][s].s )][1];
369  b += Data[Tex[t][s].TexMap].D[(int) floor( Tex[t][s].t-1 )][(int) floor( Tex[t][s].s )][2];
370  }
371  if ( (int) floor( Tex[t][s].s ) > 0 ) {
372  avg++;
373  r += Data[Tex[t][s].TexMap].D[(int) floor( Tex[t][s].t )][(int) floor( Tex[t][s].s-1 )][0];
374  g += Data[Tex[t][s].TexMap].D[(int) floor( Tex[t][s].t )][(int) floor( Tex[t][s].s-1 )][1];
375  b += Data[Tex[t][s].TexMap].D[(int) floor( Tex[t][s].t )][(int) floor( Tex[t][s].s-1 )][2];
376  }
377  r /= avg;
378  g /= avg;
379  b /= avg;
380  }
381  unsigned int rr = (unsigned int) r;
382  unsigned int gg = (unsigned int) g;
383  unsigned int bb = (unsigned int) b;
384  if (affine != 0 || multiplicitive != 1 || power != 1) {
385  rr = (unsigned int) ( affine+( ( pow( (float) r, power ) )*multiplicitive ) );
386  gg = (unsigned int) ( affine+( ( pow( (float) g, power ) )*multiplicitive ) );
387  bb = (unsigned int) ( affine+( ( pow( (float) b, power ) )*multiplicitive ) );
388  }
389  if (rr > 255) rr = 255;
390  if (gg > 255) gg = 255;
391  if (bb > 255) bb = 255;
392  Col[bytepp*( lmwid*(lmwid-1-t)+(lmwid-1-s) )] = rr;
393  Col[bytepp*( lmwid*(lmwid-1-t)+(lmwid-1-s) )+1] = gg;
394  Col[bytepp*( lmwid*(lmwid-1-t)+(lmwid-1-s) )+2] = bb;
395  //Col[4*(256*(255-t)+(255-s))+3] = 255;
396  }
397  delete[] Data;
398 }
399 
400 static void GenerateSphereMap()
401 {
402  //float SinPhi;
403  //float CosPhi;
404  //float Theta;
405  Vector Normal;
406  //RGBColor Col;
407  static CubeCoord TexCoord[lmwid][lmwid];
408  static CubeCoord gluSphereCoord[lmwid][lmwid];
409  unsigned char *LightMap = (unsigned char*) malloc( lmwid*lmwid*4 );
410  int t;
411  for (t = 0; t < lmwid; t++) {
412  //keep in mind that t = lmwido2 (sin phi) +lmwido2
413  float to256 = t/(104.*lmwid/256)-1.23;
414  for (int s = 0; s < lmwid; s++) {
415  //is is none other than Theta * lmwido2/PI
416  float so256 = s/(104.*lmwid/256)-1.23;
417  Normal.k = 2*(1-so256*so256-to256*to256);
418  float double_one_more_normal = 2*(Normal.k+1);
419  if (double_one_more_normal >= 0) {
420  Normal.i = so256*sqrt( 2*(Normal.k+1) );
421  Normal.j = to256*sqrt( 2*(Normal.k+1) );
422  } else {
423  Normal.i = Normal.j = 0.0;
424  }
425  float sz = sqrt( Normal.k*Normal.k+Normal.j*Normal.j+Normal.i*Normal.i );
426  Normal.k /= -sz;
427  Normal.i /= sz;
428  Normal.j /= sz;
429  TexMap( TexCoord[t][s], Normal ); //find what the lighting is
430  gluSphereMap( gluSphereCoord[t][s], Normal, s );
431  }
432  }
433  Spherize( TexCoord, gluSphereCoord, LightMap );
434 /* We dont use this crap anymore. png backgrounds, not bitmap
435  * BITMAPFILEHEADER bmfh;
436  * BITMAPINFOHEADER info;
437  * bmfh.bfType=19778;
438  * bmfh.bfSize=3;
439  * bmfh.bfReserved1=0;
440  * bmfh.bfReserved2=54;
441  * bmfh.bfOffBits=0;
442  * info.biSize=40;
443  * info.biWidth=lmwid;
444  * info.biHeight=lmwid;
445  * info.biPlanes=1;
446  * info.biBitCount=24;
447  * info.biCompression=0;
448  * info.biSizeImage=196608;
449  * info.biXPelsPerMeter=2834;
450  * info.biYPelsPerMeter=2834;
451  * info.biClrUsed=0;
452  * info.biClrImportant=0;
453  */
454  ::VSImage image;
455  image.WriteImage( (char*) OutputName, LightMap, PngImage, lmwid, lmwid, false, 8, TextureFile );
456 }
457 
458 void EnvironmentMapGeneratorMain( const char *inpt, const char *outpt, float a, float m, float p, bool w )
459 {
460  affine = a;
461  multiplicitive = m;
462  power = p;
463  pushdown = w;
464  int size = sizeof (char)*strlen( inpt )+40;
465  char *tmp = (char*) malloc( size );
466  strcpy( tmp, inpt );
467  VSFile f;
468  VSError err = f.OpenReadOnly( strcat( tmp, "_sphere.image" ), TextureFile );
469  if (err > Ok) {
470  err = f.OpenReadOnly( strcat( tmp, "_sphere.bmp" ), TextureFile );
471  if (err > Ok) {
472  memset( tmp, 0, size );
473  strcpy( tmp, inpt );
474  err = f.OpenReadOnly( strcat( tmp, "_up.image" ), TextureFile );
475  if (err > Ok)
476  err = f.OpenReadOnly( strcat( tmp, "_up.bmp" ), TextureFile );
477  }
478  }
479  //bool share = false;
480  std::string s;
481  if (err > Ok) {
482  //s = VSFileSystem::GetSharedTexturePath (std::string (inpt));
483  s = VSFileSystem::sharedtextures+"/"+string( inpt );
484  InputName = (char*) malloc( sizeof (char)*(s.length()+2) );
485  strcpy( InputName, s.c_str() );
486  } else {
487  f.Close();
488  InputName = (char*) malloc( sizeof (char)*(strlen( inpt )+2) );
489  strcpy( InputName, inpt );
490  }
491  OutputName = strdup( outpt );
492  free( tmp );
493  tmp = NULL;
494  VSFileSystem::vs_fprintf( stderr,
495  "input name %s, output name %s\nAffine %f Mult %f Pow %f\n",
496  InputName,
497  OutputName,
498  affine,
500  power );
502  free( InputName );
503  free( OutputName );
504 }
505 
506 #if USEDEPRECATEDENVMAPGENT
507 /* SO PEOPLE KNOW ITS DEPRECATED
508  * static void GenerateLightMap ()
509  * {
510  * L[0].Dir.i = 0;//.403705173615;
511  * L[0].Dir.j = 1;//-.897122608033;
512  * L[0].Dir.k = 0;//.179424521607;
513  * L[0].Ambient.r = 0;
514  * L[0].Ambient.g = 0;
515  * L[0].Ambient.b = 0;
516  * L[0].Intensity.r = 1;
517  * L[0].Intensity.g = 1;
518  * L[0].Intensity.b = 1;
519  * M.Ka.r = 0;
520  * M.Ka.g = 0;
521  * M.Ka.b = 0;
522  * M.Kd.r = 0;
523  * M.Kd.g = 0;
524  * M.Kd.b = 0;
525  * M.Ks.r = 1;
526  * M.Ks.g = 1;
527  * M.Ks.b = 1;
528  * M.exp = 60;
529  * float SinPhi;
530  * float CosPhi;
531  * float Theta;
532  * Vector Normal;
533  * RGBColor Col;
534  * unsigned char *LightMap= new unsigned char [lmwid*lmwid*4];
535  * for (int t=0; t<lmwid; t++) //keep in mind that t = lmwido2 (sin phi) +lmwido2
536  * {
537  * SinPhi = ((float)t)*ooWIDTHo2 -1;
538  * CosPhi = sqrt (1-SinPhi*SinPhi);//yes I know (-) ... but -PI/2<Phi<PI/2 so cos >0 like I said
539  * for (int s = 0; s < lmwid; s++) // is is none other than Theta * 128/PI
540  * {
541  * Theta = s*PIoWIDTHo2;// 128oPI = 128/3.1415926535
542  * //now that we have all this wonderful stuff, we must calculate lighting on this one point.
543  * // first calc the normal
544  * Normal.i = CosPhi * cos (Theta);
545  * Normal.j = CosPhi * sin (Theta);
546  * Normal.k = SinPhi;
547  * Lighting (Col, Normal);//find what the lighting is
548  * LightMap[lmwid*bytepp*t+bytepp*s] = (unsigned char) 255*Col.r;
549  * LightMap[lmwid*bytepp*t+bytepp*s+1] = (unsigned char) 255*Col.g;
550  * LightMap[lmwid*bytepp*t+bytepp*s+2] = (unsigned char) 255*Col.b;
551  * }
552  * }
553  *
554  *
555  *
556  *
557  * char tmp [lmwid];
558  * assert (0);
559  * strcpy (tmp,OutputName);
560  * png_write (strcat (tmp,"1.image"),LightMap, lmwid,lmwid,false,8);
561  * }
562  *
563  * static void GenerateTexMap ()//DEPRECATED
564  * {
565  * float SinPhi;
566  * float CosPhi;
567  * float Theta;
568  * Vector Normal;
569  * //RGBColor Col;
570  * static CubeCoord TexCoord [256][256];
571  * static CubeCoord gluSphereCoord[256][256];
572  * unsigned char LightMap [65536*3];
573  * unsigned char LightMap1 [65536*3];
574  * unsigned char * Disc = new unsigned char [65536*3];
575  * int t;
576  * for (t=0; t<256; t++) //keep in mind that t = 128 (sin phi) +128
577  * {
578  * SinPhi = ((float)t)*ooWIDTHo2 -1;
579  * CosPhi = sqrt (1-SinPhi*SinPhi);//yes I know (-) ... but -PI/2<Phi<PI/2 so cos >0 like I said
580  * for (int s = 0; s < 256; s++) // is is none other than Theta * 128/PI
581  * {
582  * Theta = s*PIoWIDTHo2;// 128oPI = 128/3.1415926535
583  * //now that we have all this wonderful stuff, we must calculate lighting on this one point.
584  * // first calc the normal
585  * Normal.i = CosPhi * cos (Theta);
586  * Normal.k = CosPhi * sin (Theta);
587  * Normal.j = SinPhi;
588  *
589  * TexMap (TexCoord[t][s], Normal);//find what the lighting is
590  * gluSphereMap (gluSphereCoord[t][s],Normal,s);
591  * }
592  * }
593  * Spherize (TexCoord,gluSphereCoord,LightMap);
594  * for (t=0; t<lmwid; t++) //keep in mind that t = 128 (sin phi) +128
595  * {
596  * SinPhi = ((float)t)*ooWIDTHo2 -1;
597  * CosPhi = sqrt (1-SinPhi*SinPhi);//yes I know (-) ... but -PI/2<Phi<PI/2 so cos >0 like I said
598  * for (int s = 0; s < lmwid; s++) // is is none other than Theta * 128/PI
599  * {
600  * Theta = s*PIoWIDTHo2;// 128oPI = 128/3.1415926535
601  * //now that we have all this wonderful stuff, we must calculate lighting on this one point.
602  * // first calc the normal
603  * Normal.i = CosPhi * cos (Theta);
604  * Normal.j = CosPhi * sin (Theta);
605  * Normal.k = SinPhi;
606  *
607  * TexMap (TexCoord[t][s], Normal);//find what the lighting is
608  * gluSphereMap (gluSphereCoord[t][s],Normal,s);
609  * }
610  * }
611  * Spherize (TexCoord,gluSphereCoord,LightMap1);
612  * for (t=0; t<lmwido2; t++)
613  * {
614  * for (int s=0; s<256; s++)
615  * {
616  * Disc [768*t+3*s] = (LightMap[768*2*t+3*s]+ LightMap[768*(2*t+1)+3*s])/2; //int divis
617  * Disc [768*t+3*s+1] = (LightMap[768*2*t+3*s+1]+ LightMap[768*(2*t+1)+3*s+1])/2; //int divis
618  * Disc [768*t+3*s+2] = (LightMap[768*2*t+3*s+2]+ LightMap[768*(2*t+1)+3*s+2])/2; //int divis
619  *
620  * }
621  * }
622  * for (t=0; t<lmwido2; t++)
623  * {
624  * for (int s=0; s<256; s++)
625  * {
626  * Disc [98304+768*t+3*s] = (LightMap1[768*(2*t+1)+3*s]+ LightMap1[768*((2*t)+1)+3*s])/2; //int divis
627  * Disc [98304+768*t+3*s+1] = (LightMap1[768*(2*t+1)+3*s+1]+ LightMap1[768*(2*t+1)+3*s+1])/2; //int divis
628  * Disc [98304+768*t+3*s+2] = (LightMap1[768*(2*t+1)+3*s+2]+ LightMap1[768*(2*t+1)+3*s+2])/2; //int divis
629  *
630  * }
631  * }
632  * char tmp [256]="";
633  * strcpy (tmp,OutputName);
634  * VSFile f;
635  * VSError err = f.OpenCreateWrite( strcat (tmp,".image"), TextureFile);
636  * f.Write (Disc,256*256*3);
637  * f.Close();
638  * delete [] Disc;
639  * }
640  */
641 #endif
642