#include #include #include #include using namespace std; int rot; int max_parts; class part { public: float lifetime; // total lifetime of the particle float decay; // decay speed of the particle float r,g,b; // color values of the particle float xpos,ypos,zpos; // position of the particle float xspeed,yspeed,zspeed; // speed of the particle int direction; bool active; // is particle active or not? }; part particle[100]; void CreateParticle(int i){ int random_number = rand(); particle[i].lifetime= (float)random_number; particle[i].decay=0.0002; particle[i].r = (rand()%1000)/1000.0; particle[i].g = (rand()%1000)/1000.0; particle[i].b = (rand()%1000)/1000.0; particle[i].xpos= 0.0; particle[i].ypos= 0.0; particle[i].zpos= 0.0; particle[i].xspeed = (float)( (random_number % 5 ) + 1 )/1000; particle[i].yspeed = (float)( (random_number % 12 ) + 1 )/1000; particle[i].zspeed = (float)( (random_number % 7 ) + 1 )/1000; particle[i].direction = ((rand()%4) + 1)*90; particle[i].active = true; return; } void EvolveParticle(){ for(int i=0;i -2.0 ) ){ glPushMatrix(); glRotatef( particle[i].direction, 0, 1, 0 ); glTranslatef( particle[i].xpos, particle[i].ypos, particle[i].zpos ); //glRotatef( rot, 0, 1, 0 ); //glColor3f( particle[i].r, particle[i].g, particle[i].b ); glColor3f( 1, 1, 1 ); drawCube( particle[i].r, particle[i].g, particle[i].b, 0, 0, 0 ); //glutWireIcosahedron(); //glutWireSphere( 1, 16, 16 ); glPopMatrix(); } else CreateParticle(i); } EvolveParticle(); return; } //The Window Resizing function. void resize(int w, int h){ float ratio; //Prevents a divide by 0. if (h == 0) h = 1; ratio = 1.00 * w / h; glMatrixMode(GL_PROJECTION); glLoadIdentity(); glViewport(0, 0, w, h); gluPerspective(45, ratio, 1, 1000); gluLookAt(0.0,1.2,4.0, 0.0,0.0,0.0, 0.0f,1.0f,0.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); return; } void init(){ glClearColor(0.0, 0.0, 0.30, 0.0); glShadeModel(GL_FLAT); /* Enable depth buffering for 3D graphics */ glEnable(GL_DEPTH_TEST); return; } void render() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glLoadIdentity(); glClearColor(0.0, 0.0, 0.0, 0.0); // Yellow screen of life rot += 1; drawParticles(); glutSwapBuffers(); return; } //The Main Function, using GLUT. int main( int argc, char *argv[] ){ //The window width and height, followed by the window title. const int WindowWidth = 800, WindowHeight = 800; char title[] = "Particle Rendering System"; //Initialisation glutInit( &argc, argv ); glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GL_DOUBLE ); glutInitWindowSize( WindowWidth, WindowHeight ); glutCreateWindow( title ); init(); //Maximum particle number max_parts = 100; //Callback functions glutDisplayFunc( render ); glutReshapeFunc( resize ); //glutKeyboardFunc( input ); glutIdleFunc( glutPostRedisplay ); //glutMouseFunc( mouse ); //glutSpecialFunc( input_keys ); //The main loop. glutMainLoop(); return 0; }