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
ring.cpp
Go to the documentation of this file.
1 #include "ring.h"
2 #include "vegastrike.h"
3 #include "vs_globals.h"
4 #include "config_xml.h"
5 #include "vsfilesystem.h"
6 #include "xml_support.h"
7 #include "ani_texture.h"
8 
9 extern int pixelscalesize; //from sphere.cpp
10 
11 void RingMesh::InitRing( float iradius,
12  float oradius,
13  int slices,
14  const char *texture,
15  const QVector &R,
16  const QVector &S,
17  int wrapx,
18  int wrapy,
19  const BLENDFUNC a,
20  const BLENDFUNC b,
21  bool envMapping,
22  float theta_min,
23  float theta_max,
24  FILTER mipmap )
25 {
26  int numspheres = slices/4;
27  if (numspheres < 1)
28  numspheres = 1;
29  Mesh *oldmesh;
30  char ab[3];
31  ab[2] = '\0';
32  ab[1] = b+'0';
33  ab[0] = a+'0';
34  hash_name = string( "@@Ring" )+"#"+texture+"#"+XMLSupport::tostring( slices )+ab+"#"+XMLSupport::tostring( theta_min )+"#"
35  +XMLSupport::tostring( theta_max );
36  if ( LoadExistant( hash_name, Vector( iradius, iradius, iradius ), 0 ) )
37  return;
38  oldmesh = AllocNewMeshesEachInSizeofMeshSpace( numspheres ); //FIXME::RISKY::MIGHT HAVE DIFFERENT SIZES!! DON"T YOU DARE ADD XTRA VARS TO SphereMesh calsshave to!
39  numlods = numspheres;
41  iradius,
42  iradius ), 0 ), oldmesh );
43  this->orig = oldmesh;
44  radialSize = oradius; //MAKE SURE FRUSTUM CLIPPING IS DONE CORRECTLY!!!!!
45  //mn = Vector (radialSize,radialSize,radialSize);
46  //mx = Vector (-radialSize,-radialSize,-radialSize);
47  mn = Vector( 0, 0, 0 );
48  mx = Vector( 0, 0, 0 );
49  vector< MeshDrawContext > *odq = NULL;
50  for (int l = 0; l < numspheres; l++) {
51  draw_queue = new vector< MeshDrawContext >[NUM_ZBUF_SEQ+1];
52  if (!odq)
53  odq = draw_queue;
54  if (slices > 12)
55  slices -= 4;
56  else
57  slices -= 2;
58  float theta, dtheta;
59  float ds;
60  int i, j, imin, imax;
61  vlist = NULL;
62  /* Code below adapted from gluSphere */
63  dtheta = (theta_max-theta_min)/(GLfloat) slices;
64  ds = 1.0/slices;
65  int numQuadstrips = 2;
66  imin = 0;
67  imax = numQuadstrips;
68  int numvertex = (slices+1)*4;
69  GFXVertex *vertexlist = new GFXVertex[numvertex];
70  GFXVertex *vl = vertexlist;
71  enum POLYTYPE *modes = new enum POLYTYPE[numQuadstrips];
72  int *QSOffsets = new int[numQuadstrips];
73  int fir = 0;
74  int sec = 1;
75  for ( i = imin; i < imax; i++, (fir = 1-fir), (sec = 1-sec) ) {
76  GFXVertex *vertexlist = vl+(i*(slices+1)*2);
77  for (j = 0; j <= slices; j++) {
78  theta = j*dtheta+theta_min;
79  QVector unitpos( R.Scale( -sin( theta ) )+S.Scale( -cos( theta ) ) );
80  QVector up = R.Cross( S )*( 1-(2*fir) );
81  vertexlist[j*2+fir].i = up.i;
82  vertexlist[j*2+fir].k = up.j;
83  vertexlist[j*2+fir].j = up.k;
84  vertexlist[j*2+fir].s = wrapx*theta/(2*M_PI);
85  vertexlist[j*2+fir].t = 0;
86  vertexlist[j*2+fir].x = unitpos.i*iradius;
87  vertexlist[j*2+fir].z = unitpos.j*iradius;
88  vertexlist[j*2+fir].y = unitpos.k*iradius;
89  mn = vertexlist[j*2+fir].GetVertex().Min( mn );
90  mx = vertexlist[j*2+fir].GetVertex().Max( mx );
91  vertexlist[j*2+sec].i = unitpos.i;
92  vertexlist[j*2+sec].k = unitpos.j;
93  vertexlist[j*2+sec].j = unitpos.k;
94  vertexlist[j*2+sec].s = wrapx*theta/(2*M_PI);
95  vertexlist[j*2+sec].t = wrapy;
96  vertexlist[j*2+sec].x = unitpos.i*oradius;
97  vertexlist[j*2+sec].z = unitpos.j*oradius;
98  vertexlist[j*2+sec].y = unitpos.k*oradius;
99  mn = vertexlist[j*2+sec].GetVertex().Min( mn );
100  mx = vertexlist[j*2+sec].GetVertex().Max( mx );
101  }
102  modes[i] = GFXQUADSTRIP;
103  QSOffsets[i] = (slices+1)*2;
104  }
105  //radialSize = .5*(mx-mn).Magnitude();//+.5*oradius;
106  local_pos = (mx+mn)*.5;
107  //local_pos.Set(0,0,0);
108  vlist = new GFXVertexList( modes, numvertex, vertexlist, numQuadstrips, QSOffsets );
109  delete[] vertexlist;
110  delete[] modes;
111  delete[] QSOffsets;
112  SetBlendMode( a, b );
113  int texlen = strlen( texture );
114  bool found_texture = false;
115  if (texlen > 3) {
116  if (texture[texlen-1] == 'i' && texture[texlen-2] == 'n'
117  && texture[texlen-3] == 'a' && texture[texlen-4] == '.') {
118  found_texture = true;
119  if ( Decal.empty() )
120  Decal.push_back( NULL );
121  Decal[0] = new AnimatedTexture( texture, 0, mipmap );
122  }
123  }
124  if (!found_texture) {
125  if ( Decal.empty() )
126  Decal.push_back( NULL );
127  Decal[0] = new Texture( texture, 0, mipmap, TEXTURE2D, TEXTURE_2D, g_game.use_planet_textures ? GFXTRUE : GFXFALSE );
128  }
129  setEnvMap( envMapping );
130  Mesh *oldorig = orig;
131  refcount = 1;
132  orig = NULL;
133  if (l >= 1) {
134  lodsize = (numspheres+1-l)*pixelscalesize;
135  if (l == 1)
136  lodsize *= 2;
137  else if (l == 2)
138  lodsize *= 1.75;
139  else if (l == 3)
140  lodsize *= 1.5;
141  }
142  oldmesh[l] = *this;
143  refcount = 0;
144  orig = oldorig;
145  lodsize = FLT_MAX;
146  }
147  draw_queue = odq;
148 }
149 
151 {
152  return /*mx.Magnitude()*.33+*/ rSize();
153 }
154