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