Click to See Complete Forum and Search --> : opengl code debuging help


meckle
January 9th, 2007, 02:43 PM
hello,

i'm currently trying to write a simple particle generator. while everything looks alright just looking at the code but there is a problem with it displaying the particles. The screen doesnt do anything funky and it will exit properly but the particles just wont display at all. If you would please scan over this and tell me any obvious mistakes that you see.
this is not all of the code but while trying to keep it short i didnt want to include all 650 lines.
I'm sorry but i dont know what the tags are for submiting 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

#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

bool setlife = false;

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

glLoadIdentity(); // Reset The View
glBindTexture(GL_TEXTURE_2D, texture[filter]);
for (int a = 0; a <= PARTICLE_NUM; a++)
{
particlea[a].death = (rand()%10)/1000;

particlea[a].r = (rand()%100)/255.0f;
particlea[a].g = (rand()%100)/255.0f;
particlea[a].b = (rand()%100)/255.0f;
particlea[a].xfor = (rand()%10)/100.0f;
particlea[a].yfor = (rand()%10)/100.0f;
particlea[a].zfor = (rand()%10)/100.0f;
}
if ( setlife == false )
{
setlife = true;
for (int c =0; c<= PARTICLE_NUM; c++)
{
particlea[c].life = 4.0f;
}
}
for (int b = 0; b <= PARTICLE_NUM; b++)
{
glLoadIdentity();
particlea[b].ygrav = particlea[b].ygrav+(rand()%10/1000);
glTranslatef (particlea[b].xpos,particlea[b].ypos,particlea[b].zpos);
glColor4f(particlea[b].r,particlea[b].g,particlea[b].b,particlea[b].life);
glBegin(GL_QUADS);
glNormal3f( 0.0f, 0.0f, 1.0f);
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].ygrav;
particlea[b].xpos+= particlea[b].xfor;
particlea[b].life -= particlea[b].death;
if (particlea[b].life<= 0.0f)
{
particlea[b].life = 4.0f;
}
}
return TRUE;
}

JohnyDog
January 10th, 2007, 01:49 AM
particlea[b].xpos+= particlea[b].xfor;
should probably be
particlea[b].zpos+= particlea[b].zfor;
but anyway, the opengl coordination system has positive Z from 0,0 towards you - default camera is located at 0,0 and looks towards negative Z, so the way you generate Z particle coordinate (positive), all are drawn behind the camera. You should either make them negative, or move the camera little back. You should also set viewing frustum near/far clipping planes using glFrustum (if you don't do that already).