Sign in to follow this  
mysterio123

need help changing particle engine code to disperse dust

Recommended Posts

mysterio123    102
ok i am having some trouble modifying this code to make the particles engine make dust.

[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; }

[/code]

myengine.h is just my image loader.

Share this post


Link to post
Share on other sites
RDragon1    1205
Can't read that - all of the code is on one line.

Also can you better describe the specific problem you're having, what results you're getting vs expected results, and what you've tried to do to fix it?

Share this post


Link to post
Share on other sites
mysterio123    102
[quote]<br><pre><font face="Arial">#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 &lt; MAX_PARTICLE; i++ )
{
ps[i].pos += ps[i].vel;
ps[i].life -= 0.005f;
if ( ps[i].life &lt; 0.0f) ps[i].life = 0.0f;
if ( ps[i].life &gt; 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 &lt; 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 &lt; 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;</font>
</pre>[/quote]<br><br>i havent tried anything as i do not know how to change the gravity, etc.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this