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_poly.cpp
Go to the documentation of this file.
1 #include "mesh.h"
2 #include "aux_texture.h"
3 #if !defined (_WIN32) && !defined (__CYGWIN__) && !(defined (__APPLE__) || defined (MACOSX ) ) && !defined (BSD) && !defined(__HAIKU__)
4 #include <values.h>
5 #endif
6 #include <float.h>
7 #include <assert.h>
8 #define PBEHIND (-1)
9 #define PFRONT (1)
10 #define PUNK (0)
11 
12 #define WHICHSID( v ) ( ( (v.x*a+v.y*b+v.z*c+d) > 0 )*2-1 )
13 
14 static int whichside( GFXVertex *t, int numvertex, float a, float b, float c, float d )
15 {
16  int count = PUNK;
17  for (int i = 0; i < numvertex; i++)
18  count += WHICHSID( t[i] );
19  if (count == PUNK)
20  count = (rand()%2)*2-1;
21  return ( (count > 0)*2 )-1;
22 }
23 void updateMax( Vector &mn, Vector &mx, const GFXVertex &ver );
24 
25 void Mesh::Fork( Mesh* &x, Mesh* &y, float a, float b, float c, float d )
26 {
27  if (orig && orig != this) {
28  orig->Fork( x, y, a, b, c, d );
29  return;
30  }
31  int numtris, numquads;
32  GFXVertex *Orig;
33  vlist->GetPolys( &Orig, &numquads, &numtris );
34  numquads -= numtris;
35  int numtqx[2] = {0, 0};
36  int numtqy[2] = {0, 0};
37 
38  GFXVertex *X = new GFXVertex[numquads*4+numtris*3];
39  GFXVertex *xnow = X;
40  GFXVertex *Y = new GFXVertex[numquads*4+numtris*3];
41  GFXVertex *ynow = Y;
42  Vector xmax, xmin, ymax, ymin;
43  xmax = ymax = Vector( -FLT_MAX, -FLT_MAX, -FLT_MAX );
44  xmin = ymin = Vector( FLT_MAX, FLT_MAX, FLT_MAX );
45 
46  int i, j;
47  int inc = 3;
48  int offset = 0;
49  int last = numtris;
50  mesh_polygon tmppolygon;
51  for (int l = 0; l < 2; l++) {
52  for (i = 0; i < last; i++) {
53  if ( ( !(numtqy[l] < last/3
54  && numtqx[l] > 2*last/3) )
55  && ( (numtqx[l] < last/3
56  && numtqy[l] > 2*last/3) || whichside( &Orig[offset+i*inc], inc, a, b, c, d ) == PFRONT ) ) {
57  numtqx[l]++;
58  for (j = 0; j < inc; j++) {
59  memcpy( xnow, &Orig[offset+i*inc+j], sizeof (GFXVertex) );
60  updateMax( xmin, xmax, *xnow );
61  xnow++;
62  }
63  } else {
64  numtqy[l]++;
65  for (j = 0; j < inc; j++) {
66  memcpy( ynow, &Orig[offset+i*inc+j], sizeof (GFXVertex) );
67  updateMax( ymin, ymax, *ynow );
68  ynow++;
69  }
70  }
71  }
72  numtqx[l] *= inc;
73  numtqy[l] *= inc;
74  inc = 4;
75  offset = numtris*3;
76  last = numquads;
77  }
78  free( Orig );
79  enum POLYTYPE polytypes[2] = {GFXTRI, GFXQUAD};
80  if ( ( !(numtqx[0] || numtqx[1]) ) || ( !(numtqy[0] || numtqy[1]) ) ) {
81  x = y = NULL;
82  delete[] X;
83  delete[] Y;
84  return;
85  }
86  x = new Mesh;
87  x->setLighting( getLighting() );
88  x->setEnvMap( getEnvMap() );
89  x->forceCullFace( GFXFALSE );
90 
91  y = new Mesh;
92  y->setLighting( getLighting() );
93  y->setEnvMap( getEnvMap() );
94 
95  y->forceCullFace( GFXFALSE );
96  x->forcelogos = x->squadlogos = NULL;
97  x->numforcelogo = x->numsquadlogo = 0;
98  x->setLighting( getLighting() );
99  x->setEnvMap( getEnvMap() );
100  x->blendSrc = y->blendSrc = blendSrc;
101  x->blendDst = y->blendDst = blendDst;
102  while ( x->Decal.size() < Decal.size() )
103  x->Decal.push_back( NULL );
104  {
105  for (unsigned int i = 0; i < Decal.size(); i++)
106  if (Decal[i])
107  x->Decal[i] = Decal[i]->Clone();
108  }
109 
110  y->squadlogos = y->forcelogos = NULL;
111  y->numforcelogo = y->numsquadlogo = 0;
112  y->setLighting( getLighting() );
113  y->setEnvMap( getEnvMap() );
114  while ( y->Decal.size() < Decal.size() )
115  y->Decal.push_back( NULL );
116  {
117  for (unsigned int i = 0; i < Decal.size(); i++)
118  if (Decal[i])
119  y->Decal[i] = Decal[i]->Clone();
120  }
121  if (numtqx[0] && numtqx[1]) {
122  x->vlist = new GFXVertexList( polytypes, numtqx[0]+numtqx[1], X, 2, numtqx, true );
123  } else {
124  int exist = 0;
125  if (numtqx[1])
126  exist = 1;
127  assert( numtqx[0] || numtqx[1] );
128  x->vlist = new GFXVertexList( &polytypes[exist], numtqx[exist], X, 1, &numtqx[exist], true, 0 );
129  }
130  if (numtqy[0] || numtqy[1]) {
131  y->vlist = new GFXVertexList( polytypes, numtqy[0]+numtqy[1], Y, 2, numtqy, true );
132  } else {
133  int exis = 0;
134  if (numtqy[1])
135  exis = 1;
136  assert( numtqx[0] || numtqx[1] );
137  y->vlist = new GFXVertexList( &polytypes[exis], numtqy[exis], Y, 1, &numtqy[exis], true, 0 );
138  }
139  x->local_pos = Vector( .5*(xmin+xmax) );
140  y->local_pos = Vector( .5*(ymin+ymax) );
141  x->radialSize = .5*(xmax-xmin).Magnitude();
142  y->radialSize = .5*(ymax-ymin).Magnitude();
143  x->mn = xmin;
144  x->mx = xmax;
145  y->mn = ymin;
146  y->mx = ymax;
147  x->orig = new Mesh[1];
148  x->forceCullFace( GFXFALSE );
149 
150  y->orig = new Mesh[1];
151  y->forceCullFace( GFXFALSE );
152  x->draw_queue = new vector< MeshDrawContext >[NUM_ZBUF_SEQ+1];
153  y->draw_queue = new vector< MeshDrawContext >[NUM_ZBUF_SEQ+1];
154  *y->orig = *y;
155  *x->orig = *x;
156  x->orig->refcount = 1;
157  y->orig->refcount = 1;
158  x->numforcelogo = 0;
159  x->forcelogos = NULL;
160  x->numsquadlogo = 0;
161  x->squadlogos = NULL;
162  x->numforcelogo = 0;
163  x->forcelogos = NULL;
164  x->numsquadlogo = 0;
165  x->squadlogos = NULL;
166 
167  delete[] X;
168  delete[] Y;
169 }
170 
171 void Mesh::GetPolys( vector< mesh_polygon > &polys )
172 {
173  int numtris;
174  int numquads;
175  if (orig && orig != this) {
176  orig->GetPolys( polys );
177  return;
178  }
179  GFXVertex *tmpres;
180  Vector vv;
181  vlist->GetPolys( &tmpres, &numquads, &numtris );
182  numquads -= numtris;
183  int i;
184  int inc = 3;
185  int offset = 0;
186  int last = numtris;
187  mesh_polygon tmppolygon;
188  for (int l = 0; l < 2; l++) {
189  for (i = 0; i < last; i++) {
190  polys.push_back( tmppolygon );
191  for (int j = 0; j < 3; j++) {
192  vv.i = tmpres[offset+i*inc+j].x; //+local_pos.i;
193  vv.j = tmpres[offset+i*inc+j].y; //+local_pos.j;
194  vv.k = tmpres[offset+i*inc+j].z; //+local_pos.k;
195  polys[polys.size()-1].v.push_back( vv );
196  }
197  if (inc == 4) {
198  for (int j = 1; j < 4; j++) {
199  vv.i = tmpres[offset+i*inc+j].x; //+local_pos.i;
200  vv.j = tmpres[offset+i*inc+j].y; //+local_pos.j;
201  vv.k = tmpres[offset+i*inc+j].z; //+local_pos.k;
202  polys[polys.size()-1].v.push_back( vv );
203  }
204  }
205  }
206  inc = 4;
207  offset = numtris*3;
208  last = numquads;
209  }
210  free( tmpres );
211 }
212