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
mesh_bin.cpp
Go to the documentation of this file.
1 #include "mesh.h"
2 //#include "aux_logo.h"
3 //#include "aux_texture.h"
4 #include <stdio.h>
5 #include "vegastrike.h"
6 #include "vs_globals.h"
7 #include "file_main.h"
8 #include "vsfilesystem.h"
9 
10 using namespace VSFileSystem;
11 
12 extern Texture * createTexture( char const *ccc,
13  char const *cc,
14  int k = 0,
15  enum FILTER f1 = MIPMAP,
18  float f = 1,
19  int j = 0,
20  unsigned char c = GFXFALSE,
21  int i = 65536 );
22 
23 extern Logo * createLogo( int numberlogos,
24  Vector *center,
25  Vector *normal,
26  float *sizes,
27  float *rotations,
28  float offset,
29  Texture *Dec,
30  Vector *Ref );
31 
32 #ifdef __cplusplus
33 extern "C"
34 {
35 void winsys_exit( int code );
36 }
37 #endif
38 
39 void Mesh::LoadBinary( const char *filename, int faction )
40 {
41  GFXBOOL objtex;
42  int TexNameLength;
43  char *TexName = NULL;
44  int NumPoints;
45  float *vb;
46  int NumTris;
47  int *Tris;
48  int NumQuads;
49  int *Quads;
50  GFXBOOL AlphaMap = GFXFALSE;
51 
52  VSFile fp;
53  VSError err = fp.OpenReadOnly( filename, MeshFile );
54  if (err > Ok) {
55  VSFileSystem::vs_fprintf( stderr, "Failed to load file %s", filename );
56  VSExit( 1 );
57  }
58  TexNameLength = readi( fp );
59  if (TexNameLength) {
60  if (TexNameLength < 0) {
61  AlphaMap = GFXTRUE;
62  TexNameLength = -TexNameLength;
63  }
64  objtex = GFXTRUE;
65  TexName = new char[TexNameLength+5];
66  fp.Read( TexName, TexNameLength );
67  TexName[TexNameLength+4] = '\0';
68  TexName[TexNameLength+0] = '.';
69  TexName[TexNameLength+1] = 'b';
70  TexName[TexNameLength+2] = 'm';
71  TexName[TexNameLength+3] = 'p';
72  } else {
73  objtex = GFXFALSE;
74  }
75  NumPoints = readi( fp );
76  vb = new float[NumPoints*6];
77  int ii, jj, kk;
78 
79 #define x( i ) (vb[i*6])
80 #define y( i ) (vb[i*6+1])
81 #define z( i ) (vb[i*6+2])
82 #define i( i ) (vb[i*6+3])
83 #define j( i ) (vb[i*6+4])
84 #define k( i ) (vb[i*6+5])
85 
86  readf( fp, vb, NumPoints*6 );
87  for (ii = jj = 0; jj < NumPoints; ii += 6, jj++) {
88  //x,y,z,i,j,k
89  if (vb[ii] > mx.i) mx.i = vb[ii];
90  if (vb[ii] < mn.i) mn.i = vb[ii];
91  if (vb[ii+1] > mx.j) mx.j = vb[ii+1];
92  if (vb[ii+1] < mn.j) mn.j = vb[ii+1];
93  if (vb[ii+2] > mx.k) mx.k = vb[ii+2];
94  if (vb[ii+2] < mn.k) mn.k = vb[ii+2];
95  vb[ii+3] = -vb[ii+3];
96  vb[ii+4] = -vb[ii+4];
97  vb[ii+5] = -vb[ii+5];
98  }
99  /*for (ii=0; ii<NumPoints; ii++)
100  * {
101  *
102  * x[ii] = readf (fp);
103  * if (x[ii]>mx.i)
104  * mx.i = x[ii];
105  * if (x[ii]<mn.i)
106  * mn.i=x[ii];
107  * y[ii] = readf (fp);
108  *
109  * if (y[ii]>mx.j)
110  * mx.j = y[ii];
111  * if (y[ii]<mn.j)
112  * mn.j=y[ii];
113  * z[ii] = readf (fp);
114  *
115  * if (z[ii]>mx.k)
116  * mx.k = z[ii];
117  * if (z[ii]<mn.k)
118  * mn.k=z[ii];
119  * i[ii] = -readf (fp);
120  * j[ii] = -readf (fp);
121  * k[ii] = -readf (fp);
122  *
123  * }*/
124  //below, the square fo teh radial size, because sqrtf will be useless l8r
125  radialSize = .5*(mx-mn).Magnitude();
126  NumTris = readi( fp );
127  Tris = new int[NumTris*3];
128 
129  readi( fp, Tris, NumTris*3 );
130  /*for (ii=0; ii< NumTris;ii++)
131  * for (int jj=0; jj<3; jj++)
132  * Tris[ii*3+jj] = readi(fp);*/
133  NumQuads = readi( fp );
134  Quads = new int[NumQuads*4];
135  readi( fp, Quads, NumQuads*4 );
136  /*for (ii=0; ii< NumQuads;ii++)
137  * for (int jj=0; jj<4; jj++)
138  * Quads[ii*4+jj] = readi(fp);*/
139 
140  //int numtrivertex = NumTris*3;
141  //int numquadvertex = NumQuads*4;
142  int numvertex = NumTris*3+NumQuads*4;
143  GFXVertex *vertexlist;
144  //GFXVertex *alphalist;
145 
146  vertexlist = new GFXVertex[numvertex];
147 
148  jj = 0;
149  for (ii = kk = 0; ii < NumTris; ii++, kk += 3) {
150  vertexlist[jj].x = x( Tris[kk+0] );
151  vertexlist[jj].y = y( Tris[kk+0] );
152  vertexlist[jj].z = z( Tris[kk+0] );
153  vertexlist[jj].i = i( Tris[kk+0] );
154  vertexlist[jj].j = j( Tris[kk+0] );
155  vertexlist[jj].k = k( Tris[kk+0] );
156  jj++;
157  vertexlist[jj].x = x( Tris[kk+1] );
158  vertexlist[jj].y = y( Tris[kk+1] );
159  vertexlist[jj].z = z( Tris[kk+1] );
160  vertexlist[jj].i = i( Tris[kk+1] );
161  vertexlist[jj].j = j( Tris[kk+1] );
162  vertexlist[jj].k = k( Tris[kk+1] );
163  jj++;
164  vertexlist[jj].x = x( Tris[kk+2] );
165  vertexlist[jj].y = y( Tris[kk+2] );
166  vertexlist[jj].z = z( Tris[kk+2] );
167  vertexlist[jj].i = i( Tris[kk+2] );
168  vertexlist[jj].j = j( Tris[kk+2] );
169  vertexlist[jj].k = k( Tris[kk+2] );
170  jj++;
171  }
172  for (ii = kk = 0; ii < NumQuads; ii++, kk += 4) {
173  vertexlist[jj].x = x( Quads[kk+0] );
174  vertexlist[jj].y = y( Quads[kk+0] );
175  vertexlist[jj].z = z( Quads[kk+0] );
176  vertexlist[jj].i = i( Quads[kk+0] );
177  vertexlist[jj].j = j( Quads[kk+0] );
178  vertexlist[jj].k = k( Quads[kk+0] );
179  jj++;
180  vertexlist[jj].x = x( Quads[kk+1] );
181  vertexlist[jj].y = y( Quads[kk+1] );
182  vertexlist[jj].z = z( Quads[kk+1] );
183  vertexlist[jj].i = i( Quads[kk+1] );
184  vertexlist[jj].j = j( Quads[kk+1] );
185  vertexlist[jj].k = k( Quads[kk+1] );
186  jj++;
187  vertexlist[jj].x = x( Quads[kk+2] );
188  vertexlist[jj].y = y( Quads[kk+2] );
189  vertexlist[jj].z = z( Quads[kk+2] );
190  vertexlist[jj].i = i( Quads[kk+2] );
191  vertexlist[jj].j = j( Quads[kk+2] );
192  vertexlist[jj].k = k( Quads[kk+2] );
193  jj++;
194  vertexlist[jj].x = x( Quads[kk+3] );
195  vertexlist[jj].y = y( Quads[kk+3] );
196  vertexlist[jj].z = z( Quads[kk+3] );
197  vertexlist[jj].i = i( Quads[kk+3] );
198  vertexlist[jj].j = j( Quads[kk+3] );
199  vertexlist[jj].k = k( Quads[kk+3] );
200  jj++;
201  }
202 #undef x
203 #undef y
204 #undef z
205 #undef i
206 #undef j
207 #undef k
208  delete[] vb;
209  if (objtex) {
210  //int temp = NumTris*3;
211  //float oo256 = .00390625;
212  /*long pos =*/ fp.GetPosition();
213  {
214  int temp = (NumTris*3+NumTris*3+NumQuads*4)*2;
215  float *b = new float[temp];
216  readf( fp, b, temp );
217  for (ii = jj = 0; ii < temp; ii++, jj += 2)
218  vertexlist[ii].s = b[jj], vertexlist[ii].t = b[jj+1];
219  delete[] b;
220  }
221  /*for (ii=0; ii< temp; ii++)
222  * {
223  * vertexlist[ii].s = readf(fp);// *oo256;
224  * vertexlist[ii].t = readf (fp);// *oo256;
225  * }
226  *
227  * temp = NumTris*3+NumQuads*4;
228  * for (; ii<temp; ii++)
229  * {
230  * vertexlist[ii].s = readf(fp);// *oo256;
231  * vertexlist[ii].t = readf(fp);// *oo256;
232  * }*/
233  if (AlphaMap) {
234  if ( Decal.empty() ) Decal.push_back( NULL );
235  Decal[0] = createTexture( TexName, 0 );
236  } else {
237  if ( Decal.empty() ) Decal.push_back( NULL );
238  Decal[0] = createTexture( TexName, 0 );
239  }
240  if (!Decal[0])
241  objtex = GFXFALSE;
242  }
243  numforcelogo = readi( fp );
244  Vector *PolyNormal = new Vector[numforcelogo];
245  Vector *center = new Vector[numforcelogo];
246  float *sizes = new float[numforcelogo];
247  float *rotations = new float[numforcelogo];
248  float *offset = new float[numforcelogo];
249  char polytype;
250  int offst; //FIXME
251  Vector *Ref;
252  Ref = new Vector[numforcelogo];
253  for (ii = 0; ii < numforcelogo; ii++) {
254  Ref[ii] = Vector( 0, 0, 0 );
255  center[ii].i = readf( fp );
256  center[ii].j = readf( fp );
257  center[ii].k = readf( fp );
258  polytype = readc( fp );
259  switch (polytype)
260  {
261  case 'T':
262  case 't':
263  offst = 3*readi( fp );
264  break;
265  case 'D':
266  case 'A':
267  offst = 3*readi( fp );
268  Ref[ii].i = vertexlist[offst+1].x-vertexlist[offst].x;
269  Ref[ii].j = vertexlist[offst+1].y-vertexlist[offst].y;
270  Ref[ii].k = vertexlist[offst+1].z-vertexlist[offst].z;
271  break;
272  case 'E':
273  case 'B':
274  offst = 3*readi( fp );
275  Ref[ii].i = vertexlist[offst+2].x-vertexlist[offst+1].x;
276  Ref[ii].j = vertexlist[offst+2].y-vertexlist[offst+1].y;
277  Ref[ii].k = vertexlist[offst+2].z-vertexlist[offst+1].z;
278  break;
279  case 'F':
280  case 'C':
281  offst = 3*readi( fp );
282  Ref[ii].i = vertexlist[offst].x-vertexlist[offst+2].x;
283  Ref[ii].j = vertexlist[offst].y-vertexlist[offst+2].y;
284  Ref[ii].k = vertexlist[offst].z-vertexlist[offst+2].z;
285  break;
286  case 'Q':
287  case 'q':
288  offst = 3*NumTris+4*readi( fp );
289  break;
290  case '0':
291  case '4':
292  offst = 3*NumTris+4*readi( fp );
293  Ref[ii].i = vertexlist[offst+1].x-vertexlist[offst].x;
294  Ref[ii].j = vertexlist[offst+1].y-vertexlist[offst].y;
295  Ref[ii].k = vertexlist[offst+1].z-vertexlist[offst].z;
296  break;
297  case '5':
298  case '1':
299  offst = 3*NumTris+4*readi( fp );
300  Ref[ii].i = vertexlist[offst+2].x-vertexlist[offst+1].x;
301  Ref[ii].j = vertexlist[offst+2].y-vertexlist[offst+1].y;
302  Ref[ii].k = vertexlist[offst+2].z-vertexlist[offst+1].z;
303  break;
304  case '6':
305  case '2':
306  offst = 3*NumTris+4*readi( fp );
307  Ref[ii].i = vertexlist[offst+3].x-vertexlist[offst+2].x;
308  Ref[ii].j = vertexlist[offst+3].y-vertexlist[offst+2].y;
309  Ref[ii].k = vertexlist[offst+3].z-vertexlist[offst+2].z;
310  break;
311  case '7':
312  case '3':
313  offst = 3*NumTris+4*readi( fp ); //total number of triangles incl pents
314  Ref[ii].i = vertexlist[offst].x-vertexlist[offst+3].x;
315  Ref[ii].j = vertexlist[offst].y-vertexlist[offst+3].y;
316  Ref[ii].k = vertexlist[offst].z-vertexlist[offst+3].z;
317  break;
318  default:
319  offst = 0.0f; //FIXME added by chuck_starchaser to shut off warnings; please verify correctness
320  break;
321  }
322  switch (polytype)
323  {
324  case '4':
325  case '5':
326  case '6':
327  case '7':
328  case 'D':
329  case 'E':
330  case 'F':
331  Ref[ii].i = -Ref[ii].i;
332  Ref[ii].j = -Ref[ii].j;
333  Ref[ii].k = -Ref[ii].k;
334  break;
335  default:
336  break; //FIXME added by chuck_starchaser to shut off warnings; please verify correctness
337  }
338  PolyNormal[ii] = PolygonNormal(
339  Vector( vertexlist[offst].x, vertexlist[offst].y, vertexlist[offst].z ),
340  Vector( vertexlist[offst+1].x, vertexlist[offst+1].y, vertexlist[offst+1].z ),
341  Vector( vertexlist[offst+2].x, vertexlist[offst+2].y, vertexlist[offst+2].z )
342  );
343 
344  sizes[ii] = readf( fp );
345  rotations[ii] = readf( fp );
346  offset[ii] = readf( fp );
347  }
348  forcelogos = createLogo( numforcelogo, center, PolyNormal, sizes, rotations, 0.01F, FactionUtil::getForceLogo(
349  faction ), Ref );
350  delete[] Ref;
351  delete[] PolyNormal;
352  delete[] center;
353  delete[] sizes;
354  delete[] rotations;
355  delete[] offset;
356  for (ii = 0; ii < NumTris*3; ii += 3) {
357  Vector Norm1( vertexlist[ii+1].x-vertexlist[ii].x,
358  vertexlist[ii+1].y-vertexlist[ii].y,
359  vertexlist[ii+1].z-vertexlist[ii].z );
360  Vector Norm2( vertexlist[ii+2].x-vertexlist[ii].x,
361  vertexlist[ii+2].y-vertexlist[ii].y,
362  vertexlist[ii+2].z-vertexlist[ii].z );
363  Vector Normal;
364  CrossProduct( Norm2, Norm1, Normal );
365  //CrossProduct (Norm1,Norm2,Normal);
366  Normalize( Normal );
367  vertexlist[ii].i = vertexlist[ii+1].i = vertexlist[ii+2].i = Normal.i;
368  vertexlist[ii].j = vertexlist[ii+1].j = vertexlist[ii+2].j = Normal.j;
369  vertexlist[ii].k = vertexlist[ii+1].k = vertexlist[ii+2].k = Normal.k;
370  }
371  for (ii = NumTris*3; ii < NumTris*3+NumQuads*4; ii += 4) {
372  Vector Norm1( vertexlist[ii+1].x-vertexlist[ii].x,
373  vertexlist[ii+1].y-vertexlist[ii].y,
374  vertexlist[ii+1].z-vertexlist[ii].z );
375  Vector Norm2( vertexlist[ii+3].x-vertexlist[ii].x,
376  vertexlist[ii+3].y-vertexlist[ii].y,
377  vertexlist[ii+3].z-vertexlist[ii].z );
378  Vector Normal;
379  CrossProduct( Norm2, Norm1, Normal );
380  //CrossProduct (Norm1,Norm2,Normal);
381  Normalize( Normal );
382  vertexlist[ii].i = vertexlist[ii+1].i = vertexlist[ii+2].i = vertexlist[ii+3].i = Normal.i;
383  vertexlist[ii].j = vertexlist[ii+1].j = vertexlist[ii+2].j = vertexlist[ii+3].j = Normal.j;
384  vertexlist[ii].k = vertexlist[ii+1].k = vertexlist[ii+2].k = vertexlist[ii+3].k = Normal.k;
385  }
386  numsquadlogo = readi( fp );
387  PolyNormal = new Vector[numsquadlogo];
388  center = new Vector[numsquadlogo];
389  sizes = new float[numsquadlogo];
390  rotations = new float[numsquadlogo];
391  offset = new float[numsquadlogo];
392  //char polytype;
393  //int offset;
394  Ref = new Vector[numsquadlogo];
395  for (ii = 0; ii < numsquadlogo; ii++) {
396  Ref[ii] = Vector( 0, 0, 0 );
397  center[ii].i = readf( fp );
398  center[ii].j = readf( fp );
399  center[ii].k = readf( fp );
400  polytype = readc( fp );
401  switch (polytype)
402  {
403  case 'T':
404  case 't':
405  offst = 3*readi( fp );
406  break;
407  case 'D':
408  case 'A':
409  offst = 3*readi( fp );
410  Ref[ii].i = vertexlist[offst+1].x-vertexlist[offst].x;
411  Ref[ii].j = vertexlist[offst+1].y-vertexlist[offst].y;
412  Ref[ii].k = vertexlist[offst+1].z-vertexlist[offst].z;
413  break;
414  case 'E':
415  case 'B':
416  offst = 3*readi( fp );
417  Ref[ii].i = vertexlist[offst+2].x-vertexlist[offst+1].x;
418  Ref[ii].j = vertexlist[offst+2].y-vertexlist[offst+1].y;
419  Ref[ii].k = vertexlist[offst+2].z-vertexlist[offst+1].z;
420  break;
421  case 'F':
422  case 'C':
423  offst = 3*readi( fp );
424  Ref[ii].i = vertexlist[offst].x-vertexlist[offst+2].x;
425  Ref[ii].j = vertexlist[offst].y-vertexlist[offst+2].y;
426  Ref[ii].k = vertexlist[offst].z-vertexlist[offst+2].z;
427  break;
428  case 'Q':
429  case 'q':
430  offst = 3*NumTris+4*readi( fp );
431  break;
432  case '0':
433  case '4':
434  offst = 3*NumTris+4*readi( fp );
435  Ref[ii].i = vertexlist[offst+1].x-vertexlist[offst].x;
436  Ref[ii].j = vertexlist[offst+1].y-vertexlist[offst].y;
437  Ref[ii].k = vertexlist[offst+1].z-vertexlist[offst].z;
438  break;
439  case '5':
440  case '1':
441  offst = 3*NumTris+4*readi( fp );
442  Ref[ii].i = vertexlist[offst+2].x-vertexlist[offst+1].x;
443  Ref[ii].j = vertexlist[offst+2].y-vertexlist[offst+1].y;
444  Ref[ii].k = vertexlist[offst+2].z-vertexlist[offst+1].z;
445  break;
446  case '6':
447  case '2':
448  offst = 3*NumTris+4*readi( fp );
449  Ref[ii].i = vertexlist[offst+3].x-vertexlist[offst+2].x;
450  Ref[ii].j = vertexlist[offst+3].y-vertexlist[offst+2].y;
451  Ref[ii].k = vertexlist[offst+3].z-vertexlist[offst+2].z;
452  break;
453  case '7':
454  case '3':
455  offst = 3*NumTris+4*readi( fp ); //total number of triangles incl pents
456  Ref[ii].i = vertexlist[offst].x-vertexlist[offst+3].x;
457  Ref[ii].j = vertexlist[offst].y-vertexlist[offst+3].y;
458  Ref[ii].k = vertexlist[offst].z-vertexlist[offst+3].z;
459  break;
460  default:
461  offst = 0.0f; //FIXME added by chuck_starchaser to shut off warnings; please verify correctness
462  break;
463  }
464  switch (polytype)
465  {
466  case '4':
467  case '5':
468  case '6':
469  case '7':
470  case 'D':
471  case 'E':
472  case 'F':
473  Ref[ii].i = -Ref[ii].i;
474  Ref[ii].j = -Ref[ii].j;
475  Ref[ii].k = -Ref[ii].k;
476  break;
477  default:
478  break; //FIXME added by chuck_starchaser to shut off warnings; please verify correctness
479  }
480  PolyNormal[ii] = PolygonNormal(
481  Vector( vertexlist[offst].x, vertexlist[offst].y, vertexlist[offst].z ),
482  Vector( vertexlist[offst+1].x, vertexlist[offst+1].y, vertexlist[offst+1].z ),
483  Vector( vertexlist[offst+2].x, vertexlist[offst+2].y, vertexlist[offst+2].z )
484  );
485  sizes[ii] = readf( fp );
486  rotations[ii] = readf( fp );
487  offset[ii] = readf( fp );
488  }
489  squadlogos =
490  createLogo( numsquadlogo, center, PolyNormal, sizes, rotations, (float) 0.01, FactionUtil::getSquadLogo( faction ), Ref );
491  delete[] Ref;
492  //VSFileSystem::Fprintf (stderr, "Ri:%f Rj: %f Rk %f",vertexlist[0].i,vertexlist[0].j,vertexlist[0].k);
493  int vert_offset[2];
494  vert_offset[0] = NumTris*3;
495  vert_offset[1] = NumQuads*4;
496  enum POLYTYPE modes[2];
497  modes[0] = GFXTRI;
498  modes[1] = GFXQUAD;
499  vlist = new GFXVertexList( modes, NumTris*3+NumQuads*4, vertexlist, 2, vert_offset );
500  //vlist = new GFXVertexList(numtris*4,0,numquads*4, vertexlist+numtris*3);
501  /*long pos =*/ fp.GetPosition();
502  myMatNum = readi( fp );
503  fp.Close();
504 
505  delete[] vertexlist;
506  delete[] PolyNormal;
507  delete[] center;
508  delete[] sizes;
509  delete[] rotations;
510  delete[] offset;
511 }
512