Thread: having an error with my partice engine

  1. #1
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790

    having an error with my partice engine

    heres the code:

    Code:
    #include <windows.h>		// Header File For Windows
    #include <gl\gl.h>			// Header File For The OpenGL32 Library
    #include <gl\glu.h>			// Header File For The GLu32 Library
    #include <gl\glaux.h>		// Header File For The Glaux Library
    #define MAX_PARTICLES 1000
    HDC			hDC=NULL;		// Private GDI Device Context
    HGLRC		hRC=NULL;		// Permanent Rendering Context
    HWND		hWnd=NULL;		// Holds Our Window Handle
    HINSTANCE	hInstance;		// Holds The Instance Of The Application
    float slowdown = 2.0f;
    bool	keys[256];			// Array Used For The Keyboard Routine
    bool	active=TRUE;		// Window Active Flag Set To TRUE By Default
    bool	fullscreen=TRUE;	// Fullscreen Flag Set To Fullscreen Mode By Default
    
    struct PARTICLE
    {
    
    	bool active; // is particle alive?
    	float life;  // how much life does particle have
    	float fade;  // how fast does it die out
    	float x;     // its x pos
    	float y;     // its y pos
     	float z;     // its z pos
    	float xvec;  // its x vector
    	float yvec;  // its y vector
    	float zvec;  // its z vector
    	float xgrav; // its gravity on x plane
    	float ygrav; // its gravity on y plane
    	float zgrav; // its gravity on z plane
    	float r;     // its red color
    	float g;     // its green color
    	float b;     // its blue color
    };
    
    PARTICLE particle[MAX_PARTICLES];
    
    
    LRESULT	CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);	// Declaration For WndProc
    
    GLvoid ReSizeGLScene(GLsizei width, GLsizei height)		// Resize And Initialize The GL Window
    {
    	if (height==0)										// Prevent A Divide By Zero By
    	{
    		height=1;										// Making Height Equal One
    	}
    
    	glViewport(0,0,width,height);						// Reset The Current Viewport
    
    	glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix
    	glLoadIdentity();									// Reset The Projection Matrix
    
    	// Calculate The Aspect Ratio Of The Window
    	gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
    
    	glMatrixMode(GL_MODELVIEW);							// Select The Modelview Matrix
    	glLoadIdentity();									// Reset The Modelview Matrix
    }
    
    int InitGL(GLvoid)										// All Setup For OpenGL Goes Here
    {
    	glShadeModel(GL_SMOOTH);							// Enable Smooth Shading
    	glClearColor(0.0f, 0.0f, 0.0f, 0.5f);				// Black Background
    	glClearDepth(1.0f);									// Depth Buffer Setup
    		glEnable(GL_BLEND);									// Enable Blending
    	glBlendFunc(GL_SRC_ALPHA,GL_ONE);					// Type Of Blending To Perform
    	glDisable(GL_DEPTH_TEST);							// Disables Depth Testing
    	glDepthFunc(GL_LEQUAL);								// The Type Of Depth Testing To Do
    	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// Really Nice Perspective Calculations
    	
    	
    	for (int loop = 0; loop < MAX_PARTICLES;loop++)
    	{
    
    		particle[loop].active = TRUE; // initiating particles life;
    		particle[loop].life   = 1.0f; // initiating particle's life time
    		particle[loop].fade   = float(((rand() % loop)/1000) + .003f); // set a random fading time
            particle[loop].xvec      = float((rand() % 100) - 50.0f) * 10.0f; // randome velocity on x vector
    		particle[loop].yvec      = float((rand() % 100) - 50.0f) * 10.0f; // randome velocity on y vector
    		particle[loop].zvec      = float((rand() % 100) - 50.0f) * 10.0f; // randome velocity on z vector
    		particle[loop].xgrav     = 0.0f;
    		particle[loop].ygrav     = -0.8f;
    		particle[loop].zgrav     = 0.0f;
    		particle[loop].r    = float(((rand() % 100)/1000) * slowdown);
    		particle[loop].g    = float(((rand() % 100)/1000) * slowdown);
    		particle[loop].b    = float(((rand() % 100)/1000) * slowdown);
    	}
    
    	return TRUE;										// Initialization Went OK
    }
    
    int DrawGLScene(GLvoid)									// Here's Where We Do All The Drawing
    {
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear Screen And Depth Buffer
    	glLoadIdentity(); // Reset The Current Modelview Matrix
    	
    
    	for (int loop = 0; loop<MAX_PARTICLES;loop++)
    	{
    
    		if (particle[loop].active)
    		{
    
    			float x = particle[loop].x;
    			float y = particle[loop].y;
    			float z = particle[loop].z;
    
    			glColor4f(particle[loop].r,particle[loop].g,particle[loop].b,particle[loop].life);
    			glBegin(GL_TRIANGLES);
    			
    			glVertex3f(x+0.5f,y+0.5f,z);
    			glVertex3f(x-0.5f,y+0.5f,z);
    			glVertex3f(x+0.5f,y-0.5f,z);
    			glEnd();
    
    			particle[loop].x += particle[loop].xvec/(slowdown*1000);
    			particle[loop].y += particle[loop].yvec/(slowdown*1000);
    			particle[loop].z += particle[loop].zvec/(slowdown*1000);
    
    			particle[loop].xvec += particle[loop].xgrav;
    			particle[loop].yvec += particle[loop].ygrav;
    			particle[loop].xvec += particle[loop].zgrav;
    
    			particle[loop].life -= particle[loop].fade;
    
    			if (particle[loop].life < 0.0f)
    			{
    				particle[loop].life = 1.0f;
    				particle[loop].fade = float(rand()%100)/1000.0f+0.003f;
    				particle[loop].x    = 0.0f;
    				particle[loop].y    = 0.0f;
    				particle[loop].z    = 0.0f;
    				particle[loop].xvec = float(((rand() % 100)/100) + .05f);
    				particle[loop].yvec = float(((rand() % 100)/100) + .05f);
    				particle[loop].zvec = float(((rand() % 100)/100) + .05f);
    				particle[loop].r    = float(((rand() % 100)/1000) * slowdown);
    				particle[loop].g    = float(((rand() % 100)/1000) * slowdown);
    				particle[loop].b    = float(((rand() % 100)/1000) * slowdown);
    			}
    		}
    	}
    
    	
    	return TRUE;
    }
    
    ................everything the same as NeHe's base code
    my problem is, it compiles just fine, but when I run it, it says that it has commited an error, and I dont know what it is.

  2. #2
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790
    Nevermind, I fixed it, i acidentally put too many parenthesis in the "fade" definitions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In a game Engine...
    By Shamino in forum Game Programming
    Replies: 28
    Last Post: 02-19-2006, 11:30 AM
  2. Game Engine Link Prob
    By swgh in forum Game Programming
    Replies: 2
    Last Post: 01-26-2006, 12:14 AM
  3. Ultra chess engine contest
    By yodacpp in forum Projects and Job Recruitment
    Replies: 8
    Last Post: 11-21-2004, 07:58 AM
  4. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  5. Game structure, any thoughts?
    By Vorok in forum Game Programming
    Replies: 2
    Last Post: 06-07-2003, 01:47 PM