#include "Explosion.h" #include #include void InitExplosion(VExplosion * e, VObject * o, float duration) { int i; int iplusone; int NumVertices; switch(o->Model.PrimitiveType) { case GL_POINTS: NumVertices = 1; break; case GL_LINES: NumVertices = 2; break; case GL_TRIANGLES: NumVertices = 3; break; case GL_QUADS: NumVertices = 4; break; } duration *= 400; e->markedForRemoval = false; e->next = NULL; e->NumLines = o->Model.NumberOfVertices; e->NumQuads = 10; e->TimeLeft = duration; e->Duration = duration; e->lines = (VObject *) NewPtr(sizeof(VObject) * e->NumLines); e->quads = (VObject *) NewPtr(sizeof(VObject) * e->NumQuads); e->rotates = (Vector *) NewPtr(sizeof(Vector) * e->NumLines); for(i = 0; i < e->NumLines; i++) { /* Every NumVerices'th vertex, I have to create a line connecting the first vertex to the last */ iplusone = i + 1; if(iplusone % NumVertices == 0) iplusone = i - 3; InitObject(&e->lines[i]); e->lines[i].Model.NumberOfVertices = 2; AllocateVertices(&e->lines[i].Model, 2); e->lines[i].Position = o->Position; e->lines[i].Front = o->Front; e->lines[i].Right = o->Right; e->lines[i].Up = o->Up; e->lines[i].Model.PrimitiveType = GL_LINES; e->lines[i].Model.WireFrame = true; e->lines[i].Model.Vertices[0] = o->Model.Vertices[i]; e->lines[i].Model.Vertices[1] = o->Model.Vertices[iplusone]; memcpy(e->lines[i].Model.Color, o->Model.Color, sizeof(o->Model.Color)); e->rotates[i].x = ((float)(Random() % 100) / 50000); e->rotates[i].y = ((float)(Random() % 100) / 50000); e->rotates[i].z = ((float)(Random() % 100) / 50000); } for(i = 0; i < e->NumQuads; i++) { InitObject(&e->quads[i]); BuildExplosionModel(&e->quads[i].Model); e->quads[i].Position = o->Position; switch(Random() % 3) { case 0: e->quads[i].Model.Color[0] = 1; e->quads[i].Model.Color[1] = 0.5; e->quads[i].Model.Color[2] = 0; break; case 1: e->quads[i].Model.Color[0] = 1; e->quads[i].Model.Color[1] = 0; e->quads[i].Model.Color[2] = 0; break; case 2: e->quads[i].Model.Color[0] = 1; e->quads[i].Model.Color[1] = 1; e->quads[i].Model.Color[2] = 0; break; } } } static void ProcessExplosionObject(VExplosion * e, VObject * o, float interval, int i) { Quaternion QuatView; Vector NewRot; o->Model.Color[3] = (e->TimeLeft / e->Duration); NewRot = e->rotates[i]; NormalizeVector(&NewRot); QuaternionRotateVector(VectorMagnitude(&e->rotates[i]), interval, &NewRot, &o->Front, &QuatView); o->Front.x = QuatView.v.x; o->Front.y = QuatView.v.y; o->Front.z = QuatView.v.z; CrossProduct(&o->Front, &o->Right, &o->Up); CrossProduct(&o->Up, &o->Front, &o->Right); NormalizeVector(&o->Right); NormalizeVector(&o->Up); NewRot = e->rotates[i]; NewRot.x *= EXPLOSIONSPEED; NewRot.y *= EXPLOSIONSPEED; NewRot.z *= EXPLOSIONSPEED; AddVector(&o->Position, &NewRot, &o->Position); } void RunExplosion(VExplosion * e, float interval) { int i; e->TimeLeft -= (1.0 / interval); if(e->TimeLeft <= 0) e->markedForRemoval = true; for(i = 0; i < e->NumLines; i++) { ProcessExplosionObject(e, &e->lines[i], interval, i); } for(i = 0; i < e->NumQuads; i++) { ProcessExplosionObject(e, &e->quads[i], interval, i); } } void DrawExplosion(VExplosion * e) { int i; for(i = 0; i < e->NumLines; i++) { DrawObject(&e->lines[i]); } for(i = 0; i < e->NumQuads; i++) { DrawObject(&e->quads[i]); } } void CleanUpExplosion(VExplosion * e) { int i; for(i = 0; i < e->NumLines; i++) { DisposePtr((Ptr)e->lines[i].Model.Vertices); CleanUpObject(&e->lines[i]); } for(i = 0; i < e->NumQuads; i++) { CleanUpObject(&e->quads[i]); } DisposePtr((Ptr)e->lines); DisposePtr((Ptr)e->quads); DisposePtr((Ptr)e->rotates); }