// particle_fountain.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include #include #include #include #include #define MAX_PARTICLES 100 //max number of particles #define MAX_BOUNCE_COUNT 3 //number of times a particle should bounce #define MAX_PARTICLE_AGE 90 //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; GLuint txPlane; 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 timer(int extra); void Load_Plane(); void DrawGLscene(); void Reshape(GLsizei w, GLsizei h); int main(int argc, char** argv){ glutInit(&argc,argv); glutInitDisplayMode( GLUT_RGBA| GLUT_DOUBLE ); glutInitWindowSize( 640, 480 ); glutCreateWindow("Particle fountain"); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0, 10.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0, -0.9, -3.0); Load_Plane(); Init_Particles(); glutDisplayFunc(DrawGLscene); glutTimerFunc(0, timer, 0); glutMainLoop(); } void timer(int extra) { glutPostRedisplay(); glutTimerFunc(16, timer, 0); } void Load_Plane(){ glEnable(GL_BLEND); glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA); glColor4f(0.0f, 0.2f, 0.2f, 0.5f); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, txPlane); glBegin(GL_QUADS); glNormal3f(0.0f, 1.0f, 0.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f(-10.0f, 0.0f, 10.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f( 10.0f, 0.0f, 10.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f( 10.0f, 0.0f, -10.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(-10.0f, 0.0f, -10.0f); glEnd(); } void DrawGLscene(){ glPushMatrix(); glScalef(1.0f, -1.0f, 1.0f); Render_Particles(); glPopMatrix(); Render_Particles(); } void Init_Particles(){ LoadBitmapTexture("./Particle.bmp", txParticle); LoadBitmapTexture("./Plain.bmp",txPlane); int p; 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(){ Activate_Particles(); Adjust_Particles(); glClear( GL_COLOR_BUFFER_BIT ); 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