ok i am having some trouble modifying this code to make the particles engine make dust. I cant use other codes, my teacher specifically said modify this code.
The code:
#include "freeglut.h"
#include "myengine.h"
bool full_screen;
struct CVector
{
public :
CVector() : x( 0.0f ), y( 0.0f ), z( 0.0f ) {}
CVector( float x, float y, float z ) : x( x ), y( y ), z( z ) {}
float Magnitude() { return sqrt( x*x + y*y + z*z ); }
CVector Normalize()
{
float m = Magnitude();
return CVector( x/m, y/m, z/m);
}
CVector operator +(CVector v)
{
return CVector ( x + v.x, y + v.y, z + v.z );
}
CVector operator -( CVector v)
{
return CVector ( x -v.x, y -v.y, z -v.z );
}
void operator += ( CVector v)
{
x=v.x+x;
y=v.y+y;
z=v.z+z;
}
CVector operator * ( float scale ) { return CVector( x * scale, y * scale, z * scale );}
CVector operator ^ ( CVector v )
{
return CVector( y * v.z -z * v.y, -x * v.z -z * v.x, x * v.y -y * v.x );
}
float x, y, z;
};
const int MAX_PARTICLE = 150;
struct Particle{
Particle(){
life = 1.0f;
}
CVector pos,vel;
float r,g,b;
float life;
};
Particle ps[MAX_PARTICLE];
void RenderScene()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glClearDepth( 1.0 );
glDisable( GL_DEPTH_TEST );
glDepthFunc( GL_LESS );
glMatrixMode ( GL_MODELVIEW );
glLoadIdentity();
glEnable( GL_TEXTURE_2D );
glBlendFunc( GL_SRC_ALPHA, GL_ONE );
glEnable( GL_BLEND );
for ( int i = 0; i < MAX_PARTICLE; i++ )
{
ps[i].pos += ps[i].vel;
ps[i].life -= 0.005f;
if ( ps[i].life < 0.0f) ps[i].life = 0.0f;
if ( ps[i].life > 0.0f )
{
glPushMatrix();
glTranslatef( ps[i].pos.x, ps[i].pos.y-20, -80.0f);
glColor4f( ps[i].r, ps[i].g, ps[i].b, ps[i].life);
glBegin(GL_QUADS);
glTexCoord2f( 0.0f, 0.0f ); glVertex3f( -1.0f, -1.0f, 1.0f );
glTexCoord2f( 1.0f, 0.0f ); glVertex3f( 1.0f, -1.0f, 1.0f );
glTexCoord2f( 1.0f, 1.0f ); glVertex3f( 1.0f, 1.0f, 1.0f );
glTexCoord2f( 0.0f, 1.0f ); glVertex3f( -1.0f, 1.0f, 1.0f);
glEnd();
glPopMatrix();
}
}
glDisable(GL_BLEND);
glDisable( GL_TEXTURE_2D );
glutSwapBuffers();
}
void Resize( int width, int height )
{
glViewport ( 0, 0, width, height );
glMatrixMode ( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 45, (float)(width)/(float)(height), 0.1f, 1000 );
glMatrixMode ( GL_MODELVIEW );
glLoadIdentity ();
}
void SpecialKeys( int key, int x, int y )
{
switch ( key ) {
case GLUT_KEY_F1 : for ( int i = 0;i < MAX_PARTICLE; i++ ) {
float x = (((float) rand()/RAND_MAX)-0.5f)/15;
float y = ((float) rand()/RAND_MAX)/2;
float z = ((float) rand()/RAND_MAX)/15;
ps[i].vel = CVector(x,y,z);
x = (((float) rand()/RAND_MAX)-0.5f)/2;
y = (((float) rand()/RAND_MAX)-0.5f)/2;
z = (((float) rand()/RAND_MAX)-0.5f)/2;
ps[i].pos = CVector(x,y,z);
ps[i].life = 1.0f;
}
break ;
}
glutPostRedisplay();
}
void InitApp(){
full_screen = false;
srand((unsigned)time( NULL ) );
for ( int i = 0; i < MAX_PARTICLE; i++ )
{
float x = (((float) rand()/RAND_MAX)-0.5f)/15;
float y = ((float) rand()/RAND_MAX)/2;
float z = ((float) rand()/RAND_MAX)/15;
ps[i].vel = CVector(x,y,z);
x = ((float) rand()/RAND_MAX -0.5f)/2;
y = ((float) rand()/RAND_MAX -0.5f)/2;
z = ((float) rand()/RAND_MAX -0.5f)/2;
ps[i].pos = CVector(x,y,z);
ps[i].life = 1.0f;
x = ((float) rand()/RAND_MAX);
y = ((float) rand()/RAND_MAX);
z = ((float) rand()/RAND_MAX);
ps[i].r = x; ps[i].g = y; ps[i].b = z;
}
}
void InitGL()
{
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glShadeModel( GL_SMOOTH );
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
LoadTGA( &textures[ 0 ], "images/particle1.tga",false );
}
int main( int argc, char *argv[] )
{
glutInit( &argc, argv );
InitApp();
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );
glutInitWindowSize( 800, 600 );
if (full_screen) {
glutGameModeString("800x600:16@60");
glutEnterGameMode();
} else
glutCreateWindow( "Framework" );
InitGL();
glutIdleFunc( RenderScene );
glutReshapeFunc( Resize );
glutDisplayFunc( RenderScene );
glutSpecialFunc( SpecialKeys );
glutMainLoop();
return 0;
}