rendered paste body#include <GL/glut.h>
#include <iostream>
#include <ctime>
#include <cstdlib>
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<max_parts;i++){ // evolve the particle parameters
particle[i].lifetime-= particle[ i ].decay;
particle[i].xpos+= particle[ i ].xspeed;
particle[i].ypos+= particle[ i ].yspeed;
particle[i].zpos+= particle[ i ].zspeed;
particle[i].yspeed-=0.00005;
}
return;
}
void drawCube( float r, float g, float b, float x, float y, float z ){
glBegin(GL_QUADS);
//Back
glColor3f ( r - 0.2, g - 0.2, b - 0.2 );
glVertex3f( x-0.1, y-0.1, z-0.1);
glVertex3f(x+0.1, y-0.1, z-0.1);
glVertex3f(x+0.1, y+0.1, z-0.1);
glVertex3f(x-0.1, y+0.1, z-0.1);
//left
glColor3f ( r, g, b );
glVertex3f(x-0.1, y-0.1, z-0.1);
glVertex3f(x-0.1, y+0.1, z-0.1);
glVertex3f(x-0.1, y+0.1, z+0.1);
glVertex3f(x-0.1, y-0.1, z+0.1);
//Front
//glColor3f ( r - 0.2, g - 0.2, b - 0.2 );
glVertex3f(x-0.1, y-0.1, z+0.1);
glVertex3f(x+0.1, y-0.1, z+0.1);
glVertex3f(x+0.1, y+0.1, z+0.1);
glVertex3f(x-0.1, y+0.1, z+0.1);
//right
glColor3f ( r - 0.1, g - 0.1, b - 0.1 );
glVertex3f(x+0.1, y-0.1, z-0.1);
glVertex3f(x+0.1, y+0.1, z-0.1);
glVertex3f(x+0.1, y+0.1, z+0.1);
glVertex3f(x+0.1, y-0.1, z+0.1);
//Top
glColor3f ( r - 0.3, g - 0.3, b - 0.3 );
glVertex3f(x-0.1, y+0.1, z-0.1);
glVertex3f(x+0.1, y+0.1, z-0.1);
glVertex3f(x+0.1, y+0.1, z+0.1);
glVertex3f(x-0.1, y+0.1, z+0.1);
//Bottom
glColor3f ( r - 0.3, g - 0.3, b - 0.3 );
glVertex3f(x-0.1,y-0.1, z-0.1);
glVertex3f(x+0.1, y-0.1, z-0.1);
glVertex3f(x+0.1, y-0.1, z+0.1);
glVertex3f(x-0.1, y-0.1, z+0.1);
glEnd();
return;
}
void drawParticles(){
// rendering functions
//glRotatef(50.0,1.0,0.0,0.0); // show scene from top front
//Loop 10 times ( ten particles in a frame )
for (int i = 0; i < max_parts; i++ ){
//NULL out negatives
if( particle[i].ypos < 0.0 )
particle[i].lifetime=-2.0;
//Test and render
if( (particle[i].active==true ) && ( particle[i].lifetime > -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;
}