#include #include #include #include #include #include #define MAX_PARTICLES 100 #define MAX_BOUNCE_COUNT 10 #define MAX_PARTICLE_AGE 20 //Colours float R = 0.8f; float G = 0.2f; float B = 0.0f; float cR = 0.001f; float cG = 0.002f; float cB = 0.003f; float Size = 0.01f; //size for points GLuint txParticle; struct PARTICLE { float X,Y,Z; // Current position float sX,sY,sZ; // Current Speed/Movement float tX,tY,tZ; // Target Speed/Movement float R,B,G; // Particle Colour bool Active; // Is particle Active int Age; // Age of the particle int MaxAge; // Maximum Age before particle dies int BounceCount; } Particles[MAX_PARTICLES]; void Init_Particles(); void Activate_Particles(); void Adjust_Particles(); void Render_Particles(); bool LoadBitmapTexture(char * FileName, GLuint &texid); void idle(); void DrawGLscene(); void Reshape(GLsizei w, GLsizei h); int main(int argc, char** argv){ glutInit(&argc,argv); glutCreateWindow("Particle fountain"); Init_Particles(); glutDisplayFunc(DrawGLscene); glutIdleFunc(idle); glutReshapeFunc(Reshape); glutMainLoop(); } void DrawGLscene(){ glTranslatef(0.0f, -0.05f, 0.0f); Render_Particles(); glFlush(); } void Reshape(GLsizei w,GLsizei h){ // resize viewport to new canvas size glViewport(0, 0, w, h); // change clipping region to have same w:h ratio as canvas glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (w <= h) gluOrtho2D(-1.0, 1.0, -1.0 * h/w, 1.0 * h/w); else gluOrtho2D(-1.0 * w/h, 1.0 * w/h, -1.0, 1.0); } void idle(){ Activate_Particles(); Adjust_Particles(); glutPostRedisplay(); } void Init_Particles(){ int p; LoadBitmapTexture("./Particle.bmp", txParticle); srand((int)time(NULL)); for(p=0; p1.0f){R=1.0f; cR=-cR;} if(R<0.0f){R=0.0f; cR=-cR;} if(G>1.0f){G=1.0f; cG=-cG;} if(G<0.0f){G=0.0f; cG=-cG;} if(B>1.0f){B=1.0f; cB=-cB;} if(B<0.0f){B=0.0f; cB=-cB;} return; } } } void Adjust_Particles(){ int p; for(p=0; p MAX_BOUNCE_COUNT){ Particles[p].Active = false; } } // And finally the age check Particles[p].Age++; if(Particles[p].Age > Particles[p].MaxAge){ Particles[p].Active = false; } } } void Render_Particles(){ int p; // Enable textures and bind our particle texture glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, txParticle); // Disable Depth testing. glDisable(GL_DEPTH_TEST); // Enable blending glEnable(GL_BLEND); glBlendFunc(GL_SRC_COLOR,GL_ONE); for(p=0; p