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
particle.cpp
Go to the documentation of this file.
1 #include "particle.h"
2 #include "gfxlib.h"
3 #include "lin_time.h"
4 #include "vs_globals.h"
5 #include "cmd/unit_generic.h"
6 #include "config_xml.h"
7 #include "camera.h"
8 #include "aux_texture.h"
9 #include "gldrv/gl_globals.h"
10 
12 void ParticleTrail::ChangeMax( unsigned int max )
13 {
14  this->maxparticles = max;
15 }
16 
17 bool ParticlePoint::Draw( const Vector &vel, const double time, Vector p, Vector q )
18 {
19  static float pgrow = XMLSupport::parse_float( vs_config->getVariable( "graphics", "sparkegrowrate", "200.0" ) ); //200x size when disappearing
20  static float adj = XMLSupport::parse_float( vs_config->getVariable( "graphics", "sparklefade", "0.1" ) );
21  static float trans = XMLSupport::parse_float( vs_config->getVariable( "graphics", "sparklealpha", "2.5" ) ); //NOTE: It's the base transparency, before surface attenuation, so it needn't be within the [0-1] range.
22  static bool use_points = XMLSupport::parse_bool( vs_config->getVariable( "graphics", "point_sparkles", "false" ) );
23 
24  float size = this->size*(pgrow*(1-col.a)+col.a);
25  float maxsize = (this->size > size) ? this->size : size;
26  float minsize = (this->size <= size) ? this->size : size;
27  //Squared, surface-linked decay - looks nicer, more real for emmisive gasses
28  //NOTE: maxsize/minsize allows for inverted growth (shrinkage) while still fading correctly. Cheers!
29  GFXColorf( col*( col.a*trans*( minsize/( (maxsize > 0) ? maxsize : 1.f ) ) ) );
30 
31  {
32  QVector loc = this->loc-_Universe->AccessCamera()->GetPosition();
33  if (use_points) {
34  GFXVertexf( loc );
35  } else {
36  #if 0
37  q *= size;
38  p *= size;
39  GFXTexCoord2f( 0, 0 );
40  GFXVertex3d( loc.i+p.i+q.i, loc.j+p.j+q.j, loc.k+p.k+q.k );
41  GFXTexCoord2f( 0, 1 );
42  GFXVertex3d( loc.i+p.i-q.i, loc.j+p.j-q.j, loc.k+p.k-q.k );
43  GFXTexCoord2f( 1, 1 );
44  GFXVertex3d( loc.i-p.i-q.i, loc.j-p.j-q.j, loc.k-p.k-q.k );
45  GFXTexCoord2f( 1, 0 );
46  GFXVertex3d( loc.i-p.i+q.i, loc.j-p.j+q.j, loc.k-p.k+q.k );
47 #else
48  GFXTexCoord2f( 0, 0 );
49  GFXVertex3d( loc.i+size, loc.j+size, loc.k );
50  GFXTexCoord2f( 0, 1 );
51  GFXVertex3d( loc.i+size, loc.j-size, loc.k );
52  GFXTexCoord2f( 1, 1 );
53  GFXVertex3d( loc.i-size, loc.j-size, loc.k );
54  GFXTexCoord2f( 1, 0 );
55  GFXVertex3d( loc.i-size, loc.j+size, loc.k );
56 
57  GFXTexCoord2f( 0, 0 );
58  GFXVertex3d( loc.i, loc.j+size, loc.k+size );
59  GFXTexCoord2f( 0, 1 );
60  GFXVertex3d( loc.i, loc.j-size, loc.k+size );
61  GFXTexCoord2f( 1, 1 );
62  GFXVertex3d( loc.i, loc.j-size, loc.k-size );
63  GFXTexCoord2f( 1, 0 );
64  GFXVertex3d( loc.i, loc.j+size, loc.k-size );
65 
66  GFXTexCoord2f( 0, 0 );
67  GFXVertex3d( loc.i+size, loc.j, loc.k+size );
68  GFXTexCoord2f( 0, 1 );
69  GFXVertex3d( loc.i+size, loc.j, loc.k-size );
70  GFXTexCoord2f( 1, 1 );
71  GFXVertex3d( loc.i-size, loc.j, loc.k-size );
72  GFXTexCoord2f( 1, 0 );
73  GFXVertex3d( loc.i-size, loc.j, loc.k+size );
74 #endif
75 
76 #if 0
77  GFXEnd();
78  GFXBegin( GFXPOINT );
79  GFXVertexf( loc );
80  GFXEnd();
81  GFXBegin( GFXQUAD );
82 #endif
83  }
84  }
85 
86  loc += (vel*time).Cast();
87  col = ( col-GFXColor( adj*time, adj*time, adj*time, adj*time ) ).clamp();
88  return col.a != 0;
89 }
91 {
92  static bool use_points = XMLSupport::parse_bool( vs_config->getVariable( "graphics", "point_sparkles", "false" ) );
93  Vector P, Q;
94  {
95  Vector R;
96  _Universe->AccessCamera()->GetPQR( P, Q, R );
97  static float particlesize = XMLSupport::parse_float( vs_config->getVariable( "graphics", "sparksize", "1" ) );
98  P *= particlesize;
99  Q *= particlesize;
100  }
101 
102  vector< Vector >::iterator v = particleVel.begin();
103  vector< ParticlePoint >::iterator p = particle.begin();
104  if (use_points) {
105  GFXDisable( TEXTURE0 );
106  GFXDisable( CULLFACE );
107  static float psiz = XMLSupport::parse_float( vs_config->getVariable( "graphics", "sparkesize", "1.5" ) );
108 
109  GFXPointSize( psiz );
110 
111  static bool psmooth = XMLSupport::parse_bool( vs_config->getVariable( "graphics", "sparkesmooth", "false" ) );
112  if (psmooth && gl_options.smooth_points)
113  glEnable( GL_POINT_SMOOTH );
114  } else {
115  GFXEnable( TEXTURE0 );
116  GFXDisable( TEXTURE1 );
118  GFXDisable( CULLFACE );
119  static string s = vs_config->getVariable( "graphics", "sparkletexture", "supernova.bmp" );
120  static Texture *t = new Texture( s.c_str() );
121 
122  t->MakeActive();
123  }
124  GFXDisable( LIGHTING );
127  static bool pblend = XMLSupport::parse_bool( vs_config->getVariable( "graphics", "sparkeblend", "false" ) );
128  //GFXBlendMode(ONE,ZERO);
129  if (use_points) {
130  if (pblend)
132  else
133  GFXBlendMode( ONE, ZERO );
134  GFXBegin( GFXPOINT );
135  } else {
136  GFXBlendMode( ONE, ONE );
137  GFXBegin( GFXQUAD );
138  }
139  double mytime = GetElapsedTime();
140  while ( p != particle.end() ) {
141  if ( !(*p).Draw( *v, mytime, P, Q ) ) {
142  vector< Vector >::iterator vlast = particleVel.end();
143  vector< ParticlePoint >::iterator plast = particle.end();
144  --vlast;
145  --plast;
146  if (p != plast) {
147  *v = *vlast;
148  *p = *plast;
149  }
150  size_t index = p-particle.begin();
151  size_t indexv = v-particleVel.begin();
152  particle.pop_back();
153  particleVel.pop_back();
154  p = particle.begin()+index;
155  v = particleVel.begin()+indexv; //continue where we left off
156  } else {
157  ++p;
158  ++v;
159  }
160  }
161  GFXEnd();
162  if (use_points) {
163  glDisable( GL_POINT_SMOOTH );
164  GFXPointSize( 1 );
165  } else {
167  GFXDisable( CULLFACE );
168  }
170 }
171 
172 void ParticleTrail::AddParticle( const ParticlePoint &P, const Vector &V, float size )
173 {
174  if (particle.size() > maxparticles) {
175  vector< Vector >::iterator vel = particleVel.begin();
176  vector< ParticlePoint >::iterator p = particle.begin();
177  size_t off = ( (size_t) rand() )%particle.size();
178  vel += off;
179  p += off;
180  *p = P;
181  (*p).size = size;
182  *vel = V;
183  } else {
184  particle.push_back( P );
185  particle.back().size = size;
186  particleVel.push_back( V );
187  }
188 }
189