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
bolt.cpp
Go to the documentation of this file.
1 #include "bolt.h"
2 #include "gfxlib.h"
3 #include "gfx/mesh.h"
4 #include "gfxlib_struct.h"
5 #include "gfx/aux_texture.h"
6 #include "gfx/animation.h"
7 #include "gfx/decalqueue.h"
8 #include <vector>
9 
10 #include <string>
11 #include <algorithm>
12 #include "unit.h"
13 #include "audiolib.h"
14 #include "config_xml.h"
15 #include "gfx/camera.h"
16 
17 using std::vector;
18 using std::string;
21 {
22  unsigned int i;
23  for (i = 0; i < cachedecals.size(); i++)
25  cachedecals.clear();
26  for (i = 0; i < animations.size(); i++)
27  delete animations[i];
28  for (i = 0; i < balls.size(); i++)
29  for (int j = balls[i].size()-1; j >= 0; j--)
30  balls[i][j].Destroy( j );
31  for (i = 0; i < bolts.size(); i++)
32  for (int j = bolts[i].size()-1; j >= 0; j--)
33  bolts[i][j].Destroy( j );
34  delete boltdecals;
35 }
37 {
38  boltdecals = new DecalQueue;
39  if (!boltmesh) {
40  static float beam_offset = XMLSupport::parse_float( vs_config->getVariable( "graphics", "bolt_offset", "-.2" ) );
41  GFXVertex vtx[12];
42 #define V( ii, xx, yy, zz, ss, \
43  tt ) vtx[ii].x = xx; vtx[ii].y = yy; vtx[ii].z = zz+beam_offset+.875; vtx[ii].i = 0; vtx[ii].j = 0; vtx[ii].k = 1; \
44  vtx[ii].s = ss; vtx[ii].t = tt;
45  V( 0, 0, 0, -.875, 0, .5 );
46  V( 1, 0, -1, 0, .875, 1 );
47  V( 2, 0, 0, .125, 1, .5 );
48  V( 3, 0, 1, 0, .875, 0 );
49  V( 4, 0, 0, -.875, 0, .5 );
50  V( 5, -1, 0, 0, .875, 1 );
51  V( 6, 0, 0, .125, 1, .5 );
52  V( 7, 1, 0, 0, .875, 0 );
53  V( 8, 1, 0, 0, .1875, 0 );
54  V( 9, 0, 1, 0, .375, .1875 );
55  V( 10, -1, 0, 0, .1875, .375 );
56  V( 11, 0, -1, 0, 0, .1875 );
57  boltmesh = new GFXVertexList( GFXQUAD, 12, vtx, 12, false ); //not mutable;
58  }
59 }
60 
61 extern double interpolation_blend_factor;
62 
63 inline void BlendTrans( Matrix &drawmat, const QVector &cur_position, const QVector &prev_position )
64 {
65  drawmat.p = prev_position.Scale( 1-interpolation_blend_factor )+cur_position.Scale( interpolation_blend_factor );
66 }
67 int Bolt::AddTexture( bolt_draw *q, std::string file )
68 {
69  int decal = q->boltdecals->AddTexture( file.c_str(), MIPMAP );
70  if ( decal >= (int) q->bolts.size() ) {
71  q->bolts.push_back( vector< Bolt > () );
72  int blargh = q->boltdecals->AddTexture( file.c_str(), MIPMAP );
73  if ( blargh >= (int) q->bolts.size() )
74  q->bolts.push_back( vector< Bolt > () );
75  q->cachedecals.push_back( blargh );
76  }
77  return decal;
78 }
79 int Bolt::AddAnimation( bolt_draw *q, std::string file, QVector cur_position )
80 {
81  int decal = -1;
82  for (unsigned int i = 0; i < q->animationname.size(); i++)
83  if (file == q->animationname[i])
84  decal = i;
85  if (decal == -1) {
86  decal = q->animations.size();
87  q->animationname.push_back( file );
88  q->animations.push_back( new Animation( file.c_str(), true, .1, MIPMAP, false ) ); //balls have their own orientation
89  q->animations.back()->SetPosition( cur_position );
90  q->balls.push_back( vector< Bolt > () );
91  }
92  return decal;
93 }
94 
95 void Bolt::Draw()
96 {
100 
101  static bool blendbeams = XMLSupport::parse_bool( vs_config->getVariable( "graphics", "BlendGuns", "true" ) );
102  static float stretchbolts = XMLSupport::parse_float( vs_config->getVariable( "graphics", "StretchBolts", "0" ) ); //set DEFAULT TO BE ZERO--it was wrecking all the mods.
103 
104  GFXBlendMode( ONE, blendbeams ? ONE : ZERO );
105 
107  GFXDisable( TEXTURE1 );
108  GFXEnable( TEXTURE0 );
109  GFXAlphaTest( GREATER, .1 );
110  vector< vector< Bolt > >::iterator i;
111  vector< Bolt >::iterator j;
112  vector< Animation* >::iterator k = qq->animations.begin();
113  float etime = GetElapsedTime();
114  static float bolt_constant = XMLSupport::parse_float( vs_config->getVariable( "graphics", "bolt_pixel_size", ".5" ) );
115  float pixel_angle = 2
116  *sin( g_game.fov*M_PI/180.0
118  > g_game.x_resolution ? g_game.y_resolution : g_game.x_resolution) )*bolt_constant;
119  pixel_angle *= pixel_angle;
120  Vector p, q, r;
121  _Universe->AccessCamera()->GetOrientation( p, q, r );
122  QVector campos = _Universe->AccessCamera()->GetPosition();
123  for (i = qq->balls.begin(); i != qq->balls.end(); i++, k++) {
124  Animation *cur = *k;
125  if ( i->begin() != i->end() ) {
126  float bolt_size = 2*i->begin()->type->Radius*2;
127  bolt_size *= bolt_size;
128  //Matrix result;
129  //FIXME::MuST USE DRAWNO TRANSFORMNOW cur->CalculateOrientation (result);
130  for (j = i->begin(); j != i->end(); j++) {
131  //don't update time more than once
132  Bolt *bolt = &*j;
133  float distance = (bolt->cur_position-campos).MagnitudeSquared();
134  if (distance*pixel_angle < bolt_size) {
135  const weapon_info *type = bolt->type;
136  BlendTrans( bolt->drawmat, bolt->cur_position, bolt->prev_position );
137  Matrix tmp;
138  VectorAndPositionToMatrix( tmp, p, q, r, bolt->drawmat.p );
139  cur->SetDimensions( bolt->type->Radius, bolt->type->Radius );
140  GFXLoadMatrixModel( tmp );
141  GFXColor4f( type->r, type->g, type->b, type->a );
142  cur->DrawNoTransform( false, true );
143  }
144  }
145  }
146  }
147  GFXAlphaTest( ALWAYS, 0 );
149  GFXDisable( TEXTURE1 );
150  if (blendbeams == true)
151  GFXBlendMode( ONE, ONE );
152  else
153  GFXBlendMode( ONE, ZERO );
154  GFXVertexList *qmesh = qq->boltmesh;
155  if (qmesh) {
156  qmesh->LoadDrawState();
157  qmesh->BeginDrawState();
158  int decal = 0;
159  for (i = qq->bolts.begin(); i != qq->bolts.end(); decal++, i++) {
160  Texture *dec = qq->boltdecals->GetTexture( decal );
161  if ( dec && i->begin() != i->end() ) {
162  float bolt_size = 2*i->begin()->type->Radius+i->begin()->type->Length;
163  bolt_size *= bolt_size;
164  dec->MakeActive();
165  GFXToggleTexture( true, 0 );
166  for (j = i->begin(); j != i->end(); j++) {
167  Bolt &bolt = *j;
168  float distance = (bolt.cur_position-campos).MagnitudeSquared();
169  if (distance*pixel_angle < bolt_size) {
170  const weapon_info *wt = bolt.type;
171 
172  BlendTrans( bolt.drawmat, bolt.cur_position, bolt.prev_position );
173  Matrix drawmat( bolt.drawmat );
174  if (stretchbolts > 0)
175  ScaleMatrix( drawmat, Vector( 1, 1, bolt.type->Speed*etime*stretchbolts/bolt.type->Length ) );
176  GFXLoadMatrixModel( drawmat );
177  GFXColor4f( wt->r, wt->g, wt->b, wt->a );
178  qmesh->Draw();
179  }
180  }
181  }
182  }
183  qmesh->EndDrawState();
184  }
185  GFXEnable( LIGHTING );
186  GFXEnable( CULLFACE );
187  GFXBlendMode( ONE, ZERO );
188  GFXEnable( DEPTHTEST );
190  GFXEnable( TEXTURE0 );
191  GFXColor4f( 1, 1, 1, 1 );
192 }
193 
194 extern void BoltDestroyGeneric( Bolt *whichbolt, unsigned int index, int decal, bool isBall );
195 void Bolt::Destroy( unsigned int index )
196 {
199  bool isBall = true;
200  if (type->type == weapon_info::BOLT) {
201  q->boltdecals->DelTexture( decal );
202  isBall = false;
203  } else {}
204  BoltDestroyGeneric( this, index, decal, isBall );
205 }
206