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_fx.cpp
Go to the documentation of this file.
1 #include "mesh.h"
2 #include "lin_time.h"
3 #define MAXLOCALFX (6)
4 static float startpotency = 20;
5 static float endpotency = 4;
6 static float flickertime = 3.5;
7 const float mindamage = .1;
8 static void AvLights( float target[4], const float other[4] )
9 {
10  target[0] = .5*(target[0]+other[0]);
11  target[1] = .5*(target[1]+other[1]);
12  target[2] = .5*(target[2]+other[2]);
13  target[3] = .5*(target[3]+other[3]);
14 }
15 MeshFX::MeshFX( const float TTL,
16  const float delta,
17  const bool enabled,
18  const GFXColor &vect,
19  const GFXColor &diffuse,
20  const GFXColor &specular,
21  const GFXColor &ambient,
22  const GFXColor &attenuate ) :
23  GFXLight( enabled, vect, diffuse, specular, ambient, attenuate )
24 {
25  this->TTL = TTL;
26  this->TTD = this->TTL;
27  this->delta = delta;
28 }
29 void MeshFX::MergeLights( const MeshFX &other )
30 {
31  //if (TTL>0) {
32  delta = .5*(other.delta+this->delta);
33  /* TTL = (TTL>other.TTL)
34  * ?
35  * (.666667*TTL+.33333*other.TTL)
36  * :
37  * (.666667*other.TTL+.333333*TTL);*/
38  TTL = .5*(TTL+other.TTL);
39  TTD = .5*(TTD+other.TTD);
40  Vector vec( vect[0], vect[1], vect[2] );
41  vec *= .5;
42  Vector othervec( other.vect[0], other.vect[1], other.vect[2] );
43  othervec *= .5;
44  //float distsqr = ((vec-othervec)).Dot ((vec-othervec));
45  options |= other.options;
46  vec = vec+othervec;
47  vect[0] = vec.i;
48  vect[1] = vec.j;
49  vect[2] = vec.k;
50  AvLights( diffuse, other.diffuse );
51  AvLights( specular, other.specular );
52  AvLights( ambient, other.ambient );
53  /* attenuate[2]=1./attenuate[2];
54  * attenuate[2]+=1./other.attenuate[2]+distsqr;
55  * attenuate[2]= 1./attenuate[2];
56  *
57  * attenuate[1]=1./attenuate[1];
58  * attenuate[1]+=1./other.attenuate[1]+sqrtf(distsqr);
59  * attenuate[1]= 1./attenuate[1];*/
60  //} else {
61  //memcpy(this, &other, sizeof (MeshFX));
62  //}
63 }
64 bool MeshFX::Update( float howmuchtime )
65 {
66  TTL -= howmuchtime;
67  if (TTL < 0) {
68  TTL = 0;
69  TTD -= howmuchtime;
70  attenuate[2] += 1.5*delta*howmuchtime;
71  //attenuate[1]+=2*delta*GetElapsedTime();
72 
73  //attenuate[2]*=1+2*delta*GetElapsedTime();
74  //attenuate[1]*=1+2*delta*GetElapsedTime();
75  } else {
76  attenuate[2] -= delta*howmuchtime;
77  if (attenuate[2] < delta) attenuate[2] = delta/4;
78  //attenuate[1]-=1.25*delta*GetElapsedTime();
79  //attenuate[2]*=1- .5*delta*GetElapsedTime();
80  //attenuate[1]*=1- .5*delta*GetElapsedTime();
81  }
82  return TTD > 0;
83 }
84 
85 void Mesh::AddDamageFX( const Vector &pnt, const Vector &norm, const float damage, const GFXColor &col )
86 {
87  Vector loc( pnt+norm );
88  /*if (!(norm.i||norm.j||norm.k)) */ {
89  loc = pnt;
90  loc.Normalize();
91  loc *= ( 1+rSize() );
92  }
93 
94  GFXColor tmp( col.r, col.g, col.b, col.a );
95  float numsec = flickertime*(damage < mindamage) ? mindamage : damage;
96  MeshFX newFX( numsec, (startpotency-endpotency)/( numsec*rSize()*rSize() ), true,
97  GFXColor( loc.i, loc.j, loc.k, 1 ),
98  tmp,
99  GFXColor( 0, 0, 0, 1 ),
100  tmp,
101  GFXColor( 1, 0, startpotency/( rSize()*rSize() ) ) );
102  newFX.setSize(rSize());
103  if (LocalFX.size() >= MAXLOCALFX)
104  LocalFX[( rand()%( LocalFX.size() ) )].MergeLights( newFX );
105  else
106  LocalFX.push_back( newFX );
107 }
108 void Mesh::UpdateFX( float howmuchtime )
109 {
110  //adjusts lights by TTL, eventually removing them
111  for (unsigned int i = 0; i < LocalFX.size(); i++)
112  if ( !LocalFX[i].Update( howmuchtime ) ) {
113  LocalFX.erase( LocalFX.begin()+i );
114  i--;
115  }
116 }
117 
119 {
121  setEnvMap( GFXFALSE );
122  setLighting( GFXTRUE );
123  if (orig) {
125  orig->blendSrc = orig->blendDst = ONE;
126  orig->setEnvMap( GFXFALSE );
128  }
129  blendSrc = ONE;
130  blendDst = ONE;
131 }
132