PDA

View Full Version : modify particles engine code to dust



oneofakind
07-17-2011, 09:14 PM
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( &amp;textures[ 0 ], "images/particle1.tga",false );
}
int main( int argc, char *argv[] )
{
glutInit( &amp;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;
}

mobeen
07-17-2011, 09:38 PM
my teacher specifically said modify this code.

So it is your assignment. The teacher would have taught u the things needed in the lectures. Have a look through them and then come over when u have a specific issue.

oneofakind
07-17-2011, 09:43 PM
my teacher specifically said modify this code.

So it is your assignment. The teacher would have taught u the things needed in the lectures. Have a look through them and then come over when u have a specific issue.

There wasnt much in the lectures. Can u gimme some tips? im really bad at these stuff.

BionicBytes
07-18-2011, 12:52 AM
...then you ,ust be either bad at taking notes or listening.
You must have been given the information or specific books to read. I suggest you follow the advise given and actually attempt to learn something rather than manupulate others to do it for you.

oneofakind
07-18-2011, 01:32 AM
...then you ,ust be either bad at taking notes or listening.
You must have been given the information or specific books to read. I suggest you follow the advise given and actually attempt to learn something rather than manupulate others to do it for you.

thats a bad accusation.

There were no books or information given. juz this code and an assignment. im in serious dilemia here, almost all my classmates are having difficulty too.

BionicBytes
07-18-2011, 02:56 AM
Well... you know I have been to univeristy and do know how it works. Looking back at my self and reflecting I can honestly say I did not make the most of the lectures and references either.

Anyway, there is always the option of actually talking to the lecturer and putting the question back to him.

oneofakind
07-18-2011, 03:56 AM
Well... you know I have been to univeristy and do know how it works. Looking back at my self and reflecting I can honestly say I did not make the most of the lectures and references either.

Anyway, there is always the option of actually talking to the lecturer and putting the question back to him.

I did actually. He told me to add velocity or something liddat. no idea what to do still.