Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

Explosion.cpp

Go to the documentation of this file.
00001 
00007 #include "Explosion.h"
00008 
00009 Explosion::Explosion() {
00010 }
00011 
00012 Explosion::Explosion(Vec3 pos, float power, bool sphere) {
00013         explode(pos, power, sphere);
00014 }
00015 
00016 Explosion::~Explosion() {
00017 }
00018 
00019 void Explosion::explode(Vec3 pos, float power, bool sphere) {
00020         unsigned int i;
00021 
00022         for (i=0; i<NUM_EXPLOSION_PARTICLES; i++) {
00023                 particles[i].position = pos;
00024                 particles[i].color = Vec4(1.0f, 1.0f, 1.0f, 1.0f);
00025                 particles[i].velocity = newVelocity(sphere)*0.7f;
00026         }
00027 
00028         for (i=0; i<NUM_EXPLOSION_DEBRIS; i++) {
00029                 debris[i].position = pos;
00030                 debris[i].rotation = Vec3(0.0f, 0.0f, 0.0f);
00031                 debris[i].color = Vec4(0.7f, 0.7f, 0.7f, 1.0f);
00032                 debris[i].scale = Vec3(EXPLOSION_RAND, EXPLOSION_RAND, EXPLOSION_RAND)*0.3f;
00033                 debris[i].velocity = newVelocity(sphere);
00034                 debris[i].rotationSpeed = newVelocity(sphere);
00035         }
00036 
00037         this->power = power;
00038         life = 1.5f*power;
00039         totLife = life;
00040 }
00041 
00042 Vec3 Explosion::newVelocity(bool sphere) {
00043         Vec3 vel;
00044 
00045         vel.x = EXPLOSION_RAND;
00046         vel.y = EXPLOSION_RAND;
00047         vel.z = EXPLOSION_RAND;
00048 
00049         if (sphere)
00050                 vel = vel.normalize();
00051 
00052         return vel;
00053 }
00054 
00055 void Explosion::drawAndUpdate(float dTime) {
00056         unsigned int i;
00057 
00058         if (life > 0.0f) {
00059 
00060                 //glDisable(GL_LIGHTING);
00061                 //app->renderer->setDepthFunc(DEPTH_NONE);
00062                 //app->renderer->setBlending(ONE, ONE);
00063                 //app->renderer->apply();
00064 
00065                 if (life > totLife*0.33f) {
00066                         float mat[16];
00067                         glGetFloatv(GL_MODELVIEW_MATRIX, mat);
00068 
00069                         Vec3 vRight(mat[0], mat[4], mat[8]);
00070                         Vec3 vUp(mat[1], mat[5], mat[9]);
00071                         Vec3 vPoint0, vPoint1, vPoint2, vPoint3;
00072                         Vec3 vCenter;
00073 
00074                         float x = ((life < totLife*0.7f) ? dTime*2.5f : 0.0f);
00075                         float y = ((life < totLife*0.9f) ? dTime*2.0f : 0.0f);
00076                         float z = ((life < totLife*0.9f) ? dTime*3.0f : 0.0f);
00077                         //float w = 1.0f*(life);
00078                         //if (w < 0.0f) w = 0.0f;
00079 
00080                         glBegin(GL_QUADS);
00081                         for (i=0; i<NUM_EXPLOSION_PARTICLES; i++) {
00082                                 glColor4fv(particles[i].color);
00083 
00084                                 vCenter = particles[i].position;
00085 
00086                                 vPoint0 = vCenter + ((-vRight - vUp) * 0.6f)*power;
00087                                 vPoint1 = vCenter + (( vRight - vUp) * 0.6f)*power;
00088                                 vPoint2 = vCenter + (( vRight + vUp) * 0.6f)*power;
00089                                 vPoint3 = vCenter + ((-vRight + vUp) * 0.6f)*power;
00090 
00091                                 glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0, 0);
00092                                 glVertex3fv(vPoint0);
00093 
00094                                 glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0, 1);
00095                                 glVertex3fv(vPoint1);
00096 
00097                                 glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 1, 1);
00098                                 glVertex3fv(vPoint2);
00099 
00100                                 glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 1, 0);
00101                                 glVertex3fv(vPoint3);
00102 
00103                                 //glVertex3fv(particles[i].position);
00104 
00105                                 particles[i].position += particles[i].velocity * dTime;// * (1.0f/(life*1.5f));
00106 
00107                                 //particles[i].color.x -= 1.0f * dTime * 0.1f;
00108                                 //particles[i].color.y -= 1.0f * dTime * 1.75f;
00109                                 //particles[i].color.z -= 1.0f * dTime * 1.25f;
00110                                 //particles[i].color.w -= 1.0f * dTime * 1.0f;
00111                                 particles[i].color.x -= x;
00112                                 particles[i].color.y -= y;
00113                                 particles[i].color.z -= z;
00114                                 //particles[i].color.w -= w;
00115 
00116                                 if (particles[i].color.x < 0.0f) particles[i].color.x = 0.0f;
00117                                 if (particles[i].color.y < 0.0f) particles[i].color.y = 0.0f;
00118                                 if (particles[i].color.z < 0.0f) particles[i].color.z = 0.0f;
00119                                 //if (particles[i].color.w < 0.0f) particles[i].color.w = 0.0f;
00120                         }
00121                         glEnd();
00122                 }
00123 
00124 
00125                 //glEnable(GL_LIGHTING);
00126                 app->renderer->setDepthFunc(LEQUAL);
00127                 app->renderer->setBlending(SRC_ALPHA, ONE);
00128                 app->renderer->apply();
00129 
00130                 glNormal3f(0.0f, 0.0f, 1.0f);
00131 
00132                 float alpha = debris[1].color.w - dTime / (life*6.0f);
00133                 if (alpha < 0.0f) alpha = 0.0f;
00134                 //LOG_MISC(("alpha:%f", alpha));
00135 
00136                 for (i=0; i<NUM_EXPLOSION_DEBRIS; i++) {
00137                         glColor4fv(debris[i].color);
00138 
00139                         glPushMatrix();
00140                         {
00141                                 glTranslatef(debris[i].position.x, debris[i].position.y, debris[i].position.z);
00142                                 
00143                                 glRotatef(debris[i].rotation.x, 1.0f, 0.0f, 0.0f);
00144                                 glRotatef(debris[i].rotation.y, 0.0f, 1.0f, 0.0f);
00145                                 glRotatef(debris[i].rotation.z, 0.0f, 0.0f, 1.0f);
00146 
00147                                 glScalef(debris[i].scale.x, debris[i].scale.y, debris[i].scale.z);
00148 
00149                                 glBegin(GL_TRIANGLES);
00150                                         glVertex3f(0.0f, 0.5f, 0.0f);
00151                                         glVertex3f(-0.25f, 0.0f, 0.0f);
00152                                         glVertex3f(0.25f, 0.0f, 0.0f);
00153                                 glEnd();
00154                         }
00155                         glPopMatrix();
00156 
00157                         debris[i].color.w = alpha;
00158                         //debris[i].color.w -= dAlpha;
00159                         //if (debris[i].color.w < 0.0f) {
00160                         //      LOG_MISC(("zero.."));
00161                         //      debris[i].color.w = 0.0f;
00162                         //}
00163 
00164                         debris[i].position += debris[i].velocity * dTime;
00165 
00166                         debris[i].rotation += debris[i].rotationSpeed * dTime * 300.0f;
00167                 }
00168 
00169         }
00170 
00171         life -= dTime;
00172 }

Generated on Sun Jun 5 15:47:03 2005 for Defacto by  doxygen 1.4.3