Thread: debuging help with opengl coding

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    6

    debuging help with opengl coding

    Hello,
    I have been working on writing my own particle generator and i have writin it, and every thing seems like it should work just looking at the code, but only one particle will display at a time.

    Here is the code:
    (note: this is not all the code if you want to look at the rest of it, just request for it)
    Code:
    /*this is a particle generator made from basic opengl window tut code*/
    
    #include <stdlib.h>
    #include <windows.h>		// Header File For Windows
    #include <stdio.h>			// Header File For Standard Input/Output
    #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
    #include <time.h>
    
    #define PARTICLE_NUM 100
    
    
    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
    
    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
    bool	light;				// Lighting ON/OFF ( NEW )
    bool	lp;					// L Pressed? ( NEW )
    bool	fp;					// F Pressed? ( NEW )	
    
    GLfloat LightAmbient[]=		{ 0.5f, 0.5f, 0.5f, 1.0f };
    GLfloat LightDiffuse[]=		{ 1.0f, 1.0f, 1.0f, 1.0f };
    GLfloat LightPosition[]=	{ 0.0f, 0.0f, 2.0f, 1.0f };
    
    GLuint	filter;				// Which Filter To Use
    GLuint	texture[3];			// Storage For 3 Textures
    
    
    
    typedef struct tagPARTICLE
    {
            GLfloat  xpos;
            GLfloat  ypos;
            GLfloat  zpos;
            GLfloat  xfor;
            GLfloat  yfor;
            GLfloat  zfor;
            GLfloat ygrav;
            GLfloat  life;
            GLfloat  live;
            GLfloat death;
            GLfloat     r;
            GLfloat     g;
            GLfloat     b;
            bool    alive;
            bool    run;
            bool setlife;
    }
    PARTICLE;
    
    PARTICLE particlea[PARTICLE_NUM];
    LRESULT	CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);	// Declaration For WndProc
    
    AUX_RGBImageRec *LoadBMP(char *Filename)				// Loads A Bitmap Image
    {
    	FILE *File=NULL;									// File Handle
    
    	if (!Filename)										// Make Sure A Filename Was Given
    	{
    		return NULL;									// If Not Return NULL
    	}
    
    	File=fopen(Filename,"r");							// Check To See If The File Exists
    
    	if (File)											// Does The File Exist?
    	{
    		fclose(File);									// Close The Handle
    		return auxDIBImageLoad(Filename);				// Load The Bitmap And Return A Pointer
    	}
    
    	return NULL;										// If Load Failed Return NULL
    }
    
    int LoadGLTextures()									// Load Bitmaps And Convert To Textures
    {
    	int Status=FALSE;									// Status Indicator
    
    	AUX_RGBImageRec *TextureImage[1];					// Create Storage Space For The Texture
    
    	memset(TextureImage,0,sizeof(void *)*1);           	// Set The Pointer To NULL
    
    	// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
    	if (TextureImage[0]=LoadBMP("Data/smile.bmp"))
    	{
    		Status=TRUE;									// Set The Status To TRUE
    
    		glGenTextures(3, &texture[0]);					// Create Three Textures
    
    		// Create Nearest Filtered Texture
    		glBindTexture(GL_TEXTURE_2D, texture[0]);
    		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
    		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
    		glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
    
    		// Create Linear Filtered Texture
    		glBindTexture(GL_TEXTURE_2D, texture[1]);
    		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    		glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
    
    		// Create MipMapped Texture
    		glBindTexture(GL_TEXTURE_2D, texture[2]);
    		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
    		gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
    	}
    
    	if (TextureImage[0])								// If Texture Exists
    	{
    		if (TextureImage[0]->data)						// If Texture Image Exists
    		{
    			free(TextureImage[0]->data);// Free The Texture Image Memory will not compile in dev
    		}
    
    		free(TextureImage[0]);							// Free The Image Structure
    	}
    
    	return Status;										// Return The Status
    }
    
    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
    {
    	if (!LoadGLTextures())								// Jump To Texture Loading Routine
    	{
    		return FALSE;									// If Texture Didn't Load Return FALSE
    	}
    
    	glEnable(GL_TEXTURE_2D);							// Enable Texture Mapping
    	glShadeModel(GL_SMOOTH);							// Enable Smooth Shading
    	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);				// Black Background
    	glClearDepth(1.0f);									// Depth Buffer Setup
    	glEnable(GL_DEPTH_TEST);							// Enables Depth Testing
    	glDepthFunc(GL_LEQUAL);								// The Type Of Depth Testing To Do
    	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// Really Nice Perspective Calculations
    
    	glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);		// Setup The Ambient Light
    	glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);		// Setup The Diffuse Light
    	glLightfv(GL_LIGHT1, GL_POSITION,LightPosition);	// Position The Light
    	glEnable(GL_LIGHT1);								// Enable Light One
    	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 The Screen And The Depth Buffer
        glBindTexture(GL_TEXTURE_2D, texture[0]);
    
        for (int a = 0; a <= PARTICLE_NUM; a++)
        {
            if ( particlea[a].run == false)
            {
            /* initialize random seed: */
            srand ( time(NULL) );
            particlea[a].death = ((rand()%100+1)/(rand()%1000+1))+0.01f;
            particlea[a].ygrav =(rand()%1000/1000);
            particlea[a].r = (rand()%999+1)/3.917647f;
            particlea[a].g = (rand()%999+1)/3.917647f;
            particlea[a].b = (rand()%999+1)/3.917647f;
            particlea[a].xfor = (((rand()%10000)/100000.0f)) -0.05f;
            particlea[a].yfor = (((rand()%10000)/90000.0f)) + 0.01f;
            particlea[a].zfor = (((rand()%10000)/100000.0f)) -0.05f;
            particlea[a].run = true;
            particlea[a].life = 1.0f;
            }
        }
    
        for (int b = 0; b <= PARTICLE_NUM; b++)
        {
        /* initialize random seed: */
            srand ( time(NULL) );
            glLoadIdentity();									// Reset The View
            particlea[b].life -= 0.01f;
            glTranslatef (particlea[b].xpos,particlea[b].ypos,particlea[b].zpos-25.0f);
           	glColor4f( particlea[b].r, particlea[b].g, particlea[b].b, particlea[b].life);
            glBegin(GL_QUADS);    
                 glTexCoord2f( 0.0f, 0.0f); glVertex3f (-0.5f,-0.5f, 0.0f);
                 glTexCoord2f( 0.0f, 1.0f); glVertex3f (-0.5f, 0.5f, 0.0f);
                 glTexCoord2f( 1.0f, 1.0f); glVertex3f ( 0.5f, 0.5f, 0.0f);
                 glTexCoord2f( 1.0f, 0.0f); glVertex3f ( 0.5f,-0.5f, 0.0f);
            glEnd();
            particlea[b].xpos += particlea[b].xfor;
            particlea[b].ypos += particlea[b].yfor;
            particlea[b].zpos += particlea[b].zfor;
            if (particlea[b].life >= 0.00f && particlea[b].life <= 0.02f)
            {
               particlea[b].xpos = 0.0f;
               particlea[b].ypos = 0.0f;
               particlea[b].zpos = 0.0f;
               particlea[b].run = false;
            }
        }
    	return TRUE;										
    }
    thanks in advance for your help

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    #include <stdlib.h>
    ->
    Code:
    #include <cstdlib>
    Ditto for <stdio.h> and <time.h>.

    You're only supposed to call srand() once during the execution of a program. Put a call to it at the beginning of main or an initialization function.

    I think you meant to cast one of these numbers to a float.
    Code:
    ((rand()%100+1)/(rand()%1000+1))+0.01f;
    ->
    Code:
    ( (float) (rand()%100+1)/(rand()%1000+1))+0.01f;
    I don't program in OpenGL, but
    Code:
            glLoadIdentity();									// Reset The View
    that sounds like you clear the screen before each particle

    There's an off-by-one error here (and elsewhere); you're going too far. Use <, not <=.
    Code:
    for (int b = 0; b <= PARTICLE_NUM; b++)
    I don't program in OpenGL, but this sounds like you might be clearing the screen before each particle.
    Code:
            glLoadIdentity();									// Reset The View
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    6
    thanks for your help it solved some problems

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  2. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  3. OpenGL .dll vs video card dll
    By Silvercord in forum Game Programming
    Replies: 14
    Last Post: 02-12-2003, 07:57 PM
  4. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM