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
00061
00062
00063
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
00078
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
00104
00105 particles[i].position += particles[i].velocity * dTime;
00106
00107
00108
00109
00110
00111 particles[i].color.x -= x;
00112 particles[i].color.y -= y;
00113 particles[i].color.z -= z;
00114
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
00120 }
00121 glEnd();
00122 }
00123
00124
00125
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
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
00159
00160
00161
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 }