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
gl_sphere_list_server.cpp
Go to the documentation of this file.
1 #include "gfxlib.h"
2 #include "gfxlib_struct.h"
3 #include "gfx/matrix.h"
4 #include <assert.h>
5 
7 
8 void GFXSphereVertexList::Draw( enum POLYTYPE *poly, const INDEX index, const int numLists, const int *offsets ) {}
10 void GFXSphereVertexList::GetPolys( GFXVertex **vert, int *numPolys, int *numTris )
11 {
12  sphere->GetPolys( vert, numPolys, numTris );
13  int numt = *numTris;
14  int numq = *numPolys-numt;
15  int verts = numt*3+numq*4;
16  for (int i = 0; i < verts; ++i) {
17  (*vert)[i].x *= radius;
18  (*vert)[i].y *= radius;
19  (*vert)[i].z *= radius;
20  }
21 }
24 {
25  return NULL;
26 }
27 void GFXSphereVertexList::EndMutate( int newsize )
28 {
29  assert( 0 );
30 }
31 GFXSphereVertexList::GFXSphereVertexList( float radius, int detail, bool Insideout, bool reverse_normals )
32 {
33  this->radius = radius;
34  radius = 100000.0f;
35  static vector< GFXVertexList* >vlists[4];
36  int which = (Insideout ? 1 : 0)+(reverse_normals ? 2 : 0);
37  while ( (unsigned int) detail >= vlists[which].size() )
38  vlists[which].insert( vlists[which].end(), 1+detail-vlists[which].size(), NULL );
39  if (vlists[which][detail] == 0) {
40  int slices;
41  int stacks = slices = detail;
42  if (stacks > 12) {
43  stacks -= 4;
44  slices -= 4;
45  } else {
46  stacks -= 2;
47  slices -= 2;
48  }
49  float rho_max = 3.1415926536;
50  float rho_min = 0;
51  float theta_min = 0.0;
52  float theta_max = 2*3.1415926536;
53  float rho, drho, theta, dtheta;
54  float x, y, z;
55  float s, t, ds, dt;
56  int i, j, imin, imax;
57  float nsign = Insideout ? -1.0 : 1.0;
58  float normalscale = reverse_normals ? -1.0 : 1.0;
59  int fir = 0; //Insideout?1:0;
60  int sec = 1; //Insideout?0:1;
61  /* Code below adapted from gluSphere */
62  drho = (rho_max-rho_min)/(float) stacks;
63  dtheta = (theta_max-theta_min)/(float) slices;
64 
65  ds = 1.0/slices;
66  dt = 1.0/stacks;
67  t = 1.0; /* because loop now runs from 0 */
68 
69  imin = 0;
70  imax = stacks;
71 
72  int numQuadstrips = stacks;
73  //numQuadstrips = 0;
74  int *QSOffsets = new int[numQuadstrips];
75 
76  //draw intermediate stacks as quad strips
77  int numvertex = stacks*(slices+1)*2;
78  GFXVertex *vertexlist = new GFXVertex[numvertex];
79 
80  GFXVertex *vl = vertexlist;
81  enum POLYTYPE *modes = new enum POLYTYPE[numQuadstrips];
82  /* SetOrientation(Vector(1,0,0),
83  * Vector(0,0,-1),
84  * Vector(0,1,0));//that's the way prop*/ //taken care of in loading
85  for (i = imin; i < imax; i++) {
86  GFXVertex *vertexlist = vl+(i*(slices+1)*2);
87  rho = i*drho+rho_min;
88 
89  s = 0.0;
90  for (j = 0; j <= slices; j++) {
91  theta = j*dtheta; //(j == slices) ? theta_min * 2 * M_PI : j * dtheta;
92  x = -sin( theta )*sin( rho );
93  y = cos( theta )*sin( rho );
94  z = nsign*cos( rho );
95 
96  vertexlist[j*2+fir].i = x*normalscale;
97  vertexlist[j*2+fir].k = -y*normalscale;
98  vertexlist[j*2+fir].j = z*normalscale;
99 #define GetS( theta, theta_min, theta_max ) ( 1-(theta-theta_min)/(theta_max-theta_min) )
100 #define GetT( rho, rho_min, rho_max ) ( 1-(rho-rho_min)/(rho_max-rho_min) )
101 
102  vertexlist[j*2+fir].s = GetS( theta, theta_min, theta_max ); //1-s;//insideout?1-s:s;
103  vertexlist[j*2+fir].t = GetT( rho, rho_min, rho_max ); //t;
104  vertexlist[j*2+fir].x = x*radius;
105  vertexlist[j*2+fir].z = -y*radius;
106  vertexlist[j*2+fir].y = z*radius;
107 
108  x = -sin( theta )*sin( rho+drho );
109  y = cos( theta )*sin( rho+drho );
110  z = nsign*cos( rho+drho );
111 
112  vertexlist[j*2+sec].i = x*normalscale;
113  vertexlist[j*2+sec].k = -y*normalscale;
114  vertexlist[j*2+sec].j = z*normalscale; //double negative
115  vertexlist[j*2+sec].s = GetS( theta, theta_min, theta_max ); //1-s;//insideout?1-s:s;
116  vertexlist[j*2+sec].t = GetT( rho+drho, rho_min, rho_max ); //t - dt;
117  vertexlist[j*2+sec].x = x*radius;
118  vertexlist[j*2+sec].z = -y*radius;
119  vertexlist[j*2+sec].y = z*radius;
120 
121  s += ds;
122  }
123 
124  t -= dt;
125  QSOffsets[i] = (slices+1)*2;
126  modes[i] = GFXQUADSTRIP;
127  }
128  vlists[which][detail] = new GFXVertexList( modes, numvertex, vertexlist, numQuadstrips, QSOffsets );
129  }
130  sphere = vlists[which][detail];
134  index.i = sphere->index.i;
136  mode = sphere->mode;
138 }
139 
141 {
142  numVertices = 0;
143  data.vertices = 0;
144  data.colors = 0;
145  index.i = 0;
146  mode = 0;
147  numlists = 0;
148  display_list = 0;
149  offsets = 0;
150 }
151