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 File Reference
#include "endianness.h"
#include <float.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "vsfilesystem.h"
#include "vsimage.h"
#include "aux_texture.h"
#include "gldrv/sdds.h"

Go to the source code of this file.

Classes

struct  CubeCoord
 
struct  Texmp
 

Functions

static void gluSphereMap (CubeCoord &Tex, Vector Normal, float Theta)
 
static void TexMap (CubeCoord &Tex, Vector Normal)
 
static bool LoadTex (char *FileName, unsigned char scdata[lmwid][lmwid][3])
 
static char * makebgname (char *tmp, size_t size, const char *InputName, const char *add, const char *suffix)
 
static void Spherize (CubeCoord Tex[lmwid][lmwid], CubeCoord gluSph[lmwid][lmwid], unsigned char Col[])
 
static void GenerateSphereMap ()
 
void EnvironmentMapGeneratorMain (const char *inpt, const char *outpt, float a, float m, float p, bool w)
 

Variables

static char * InputName = NULL
 
static char * OutputName = NULL
 
static bool pushdown = false
 
static float affine = 0
 
static float multiplicitive = 1
 
static float power = 1
 
const int lmwid = 512
 
const int lmwido2 = lmwid/2
 
const float ooWIDTHo2 = 2./lmwid
 
const float PIoWIDTHo2 = 2*3.1415926535/lmwid
 
const char bytepp = 3
 

Function Documentation

void EnvironmentMapGeneratorMain ( const char *  inpt,
const char *  outpt,
float  a,
float  m,
float  p,
bool  w 
)

Definition at line 458 of file env_map_gent.cpp.

References a, affine, VSFileSystem::VSFile::Close(), f, GenerateSphereMap(), InputName, multiplicitive, VSFileSystem::Ok, VSFileSystem::VSFile::OpenReadOnly(), OutputName, power, pushdown, VSFileSystem::sharedtextures, size, VSFileSystem::TextureFile, and VSFileSystem::vs_fprintf().

Referenced by GameStarSystem::createBackground().

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 }
static void GenerateSphereMap ( )
static

Definition at line 400 of file env_map_gent.cpp.

References gluSphereMap(), lmwid, OutputName, PngImage, Spherize(), UniverseUtil::sqrt(), TexMap(), VSFileSystem::TextureFile, and Vector.

Referenced by EnvironmentMapGeneratorMain().

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 }
static void gluSphereMap ( CubeCoord Tex,
Vector  Normal,
float  Theta 
)
static

Definition at line 108 of file env_map_gent.cpp.

References int, lmwid, lmwido2, pushdown, CubeCoord::s, CubeCoord::t, CubeCoord::TexMap, and vert.

Referenced by GenerateSphereMap().

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 }
static bool LoadTex ( char *  FileName,
unsigned char  scdata[lmwid][lmwid][3] 
)
static

Definition at line 183 of file env_map_gent.cpp.

References buffer, ddsDecompress(), DXT1, f, i, j, lmwid, VSFileSystem::Ok, PNG_HAS_ALPHA, PNG_HAS_COLOR, texTransform, and VSFileSystem::TextureFile.

Referenced by Spherize().

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 }
static char* makebgname ( char *  tmp,
size_t  size,
const char *  InputName,
const char *  add,
const char *  suffix 
)
static

Definition at line 270 of file env_map_gent.cpp.

References InputName, and size.

Referenced by Spherize().

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 }
static void Spherize ( CubeCoord  Tex[lmwid][lmwid],
CubeCoord  gluSph[lmwid][lmwid],
unsigned char  Col[] 
)
static

Definition at line 287 of file env_map_gent.cpp.

References affine, b, bytepp, Texmp::D, g, InputName, int, lmwid, LoadTex(), VSFileSystem::LookForFile(), makebgname(), multiplicitive, VSFileSystem::Ok, power, and VSFileSystem::TextureFile.

Referenced by GenerateSphereMap().

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 }
static void TexMap ( CubeCoord Tex,
Vector  Normal 
)
static

find coordinates on this tex map 'box in space'

Definition at line 121 of file env_map_gent.cpp.

References i, lmwido2, CubeCoord::s, CubeCoord::t, and CubeCoord::TexMap.

Referenced by GenerateSphereMap().

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 }

Variable Documentation

float affine = 0
static

Definition at line 28 of file env_map_gent.cpp.

Referenced by EnvironmentMapGeneratorMain(), and Spherize().

const char bytepp = 3

Definition at line 98 of file env_map_gent.cpp.

Referenced by Spherize().

char* InputName = NULL
static

Definition at line 25 of file env_map_gent.cpp.

Referenced by EnvironmentMapGeneratorMain(), makebgname(), and Spherize().

const int lmwid = 512

Definition at line 94 of file env_map_gent.cpp.

Referenced by GenerateSphereMap(), gluSphereMap(), LoadTex(), and Spherize().

const int lmwido2 = lmwid/2

Definition at line 95 of file env_map_gent.cpp.

Referenced by gluSphereMap(), and TexMap().

float multiplicitive = 1
static

Definition at line 29 of file env_map_gent.cpp.

Referenced by EnvironmentMapGeneratorMain(), and Spherize().

const float ooWIDTHo2 = 2./lmwid

Definition at line 96 of file env_map_gent.cpp.

char* OutputName = NULL
static

Definition at line 26 of file env_map_gent.cpp.

Referenced by EnvironmentMapGeneratorMain(), and GenerateSphereMap().

const float PIoWIDTHo2 = 2*3.1415926535/lmwid

Definition at line 97 of file env_map_gent.cpp.

bool pushdown = false
static

Definition at line 27 of file env_map_gent.cpp.

Referenced by EnvironmentMapGeneratorMain(), and gluSphereMap().