Rain.cpp File Reference

Rain project source code. More...

#include <particle/papi.h>
#include <GL/glut.h>
#include <im.h>
#include <im_image.h>
#include <im_convert.h>

Functions

void MakeRainStreaksTexture ()
 makes the rain streaks texture for testing
void LoadBackgroundImage (char *file_name)
 loads the image for the background
void ComputeParticles ()
 calculates the particle dynamics
void DrawGroupAsPoints ()
 renders the points
void drawParticles ()
 renders the particles
void drawBackground ()
void Draw ()
 OpenGL registrered rendering method.
void Reshape (int w, int h)
void initGL ()
int main (int argc, char **argv)

Variables

imImage * bg_image = NULL
imImage * tex_image = NULL
unsigned char * gl_bg_data = NULL
unsigned char * gl_tex_data_R = NULL
unsigned char * gl_tex_data_G = NULL
unsigned char * gl_tex_data_B = NULL
unsigned char * gl_tex_data_RGBA = NULL
GLuint tex_id = -1
GLuint SpotTexID = -1


Detailed Description

Rain project source code.


Function Documentation

ComputeParticles  ) 
 

calculates the particle dynamics

The control over the Particles API is done here

00104 {
00105         // Set up the state.
00106         pVelocityD(PDLine(pVec(0.0,0.0,0.1),pVec(0.0,0.0,0.0)));
00107         pColorD(PDLine(pVec(0.8, 0.9, 1.0), pVec(0.1, 1.0, 1.0)));
00108         pSize(1.5);
00109         pStartingAge(0);
00110 
00111         // Generate particles along a very small line in the nozzle.
00112         //pSource(2, PDLine(pVec(0.0, 0.0, 0.0), pVec(0.0, 0.0, 0.405)));
00113         pSource(200,PDBox(pVec(-3.5,-3.5,0.0),pVec(3.5,3.5,0.5)));
00114 
00115         // Gravity.
00116         pGravity(pVec(0.0, 0.0, -0.01));
00117         
00118         // Kill particles below Z=-3.
00119         pSink(false, PDPlane(pVec(0,0,0), pVec(0,0,1)));
00120 
00121         // Move particles to their new positions.
00122         pMove();
00123 }

Draw  ) 
 

OpenGL registrered rendering method.

The rendering consists of : 1- clean up the buffers 2- drawing the background on the back color buffer 3- drawing the particles on the back color buffer 4- blitting

00201 {
00202         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00203         glMatrixMode(GL_PROJECTION);
00204 
00205         // Draws the background
00206         drawBackground();
00207         
00208         // Do what the particles do.
00209         ComputeParticles();
00210         
00211         // Draw the particles.
00212         drawParticles();
00213         
00214         // shows everything...
00215         glutSwapBuffers();
00216 }

DrawGroupAsPoints  ) 
 

renders the points

We have used the Array implementation for performance to draw as textured point sprites just call glEnable(GL_POINT_SPRITE_ARB) before calling this function and after binding the texture.

00134 {
00135         int cnt = (int)pGetGroupCount();
00136         if(cnt < 1) return;
00137 
00138         float *ptr;
00139         size_t flstride, pos3Ofs, posB3Ofs, size3Ofs, vel3Ofs, velB3Ofs, color3Ofs, alpha1Ofs, age1Ofs;
00140 
00141         cnt = (int)pGetParticlePointer(ptr, flstride, pos3Ofs, posB3Ofs,
00142      
00143         size3Ofs, vel3Ofs, velB3Ofs, color3Ofs, alpha1Ofs, age1Ofs);
00144         if(cnt < 1) return;
00145         
00146 
00147         glEnableClientState(GL_COLOR_ARRAY);
00148         glColorPointer(4, GL_FLOAT, int(flstride) * sizeof(float), ptr + color3Ofs);
00149 
00150         glEnableClientState(GL_VERTEX_ARRAY);
00151         glVertexPointer(3, GL_FLOAT, int(flstride) * sizeof(float), ptr + pos3Ofs);
00152 
00153         glDrawArrays(GL_POINTS, 0, cnt);
00154         glDisableClientState(GL_VERTEX_ARRAY);
00155         glDisableClientState(GL_COLOR_ARRAY);
00156                 
00157 
00158 }

drawParticles  ) 
 

renders the particles

We have used Perspective projection for this... TODO

00165                      {
00166         
00167         glLoadIdentity();
00168         gluPerspective(10, bg_image->width / double(bg_image->height), 1, 100);
00169         gluLookAt(0, -8, 3, 0, 0, 0, 0, 0, 1);
00170 
00171         DrawGroupAsPoints();
00172 }

LoadBackgroundImage char *  file_name  ) 
 

loads the image for the background

The IM package will take are of most common formats...

00088 {
00089         int error;
00090 
00091         bg_image = imFileImageLoadBitmap(file_name, 0, &error);
00092         gl_bg_data = (unsigned char*) malloc(bg_image->size); 
00093 
00094         imConvertPacking(bg_image->data[0], gl_bg_data, bg_image->width, bg_image->height, bg_image->depth, bg_image->data_type, 0);
00095 }

MakeRainStreaksTexture  ) 
 

makes the rain streaks texture for testing

For OpenGL the texture image must be of the form 2^x

00030 {
00031 
00032         int error;
00033 
00034         tex_image = imFileImageLoadBitmap("rain_32x32.png", 0, &error);
00035         int width = tex_image->width;
00036         int height = tex_image->height;
00037         gl_tex_data_RGBA = (unsigned char*) malloc(4*width*height); 
00038 
00039         gl_tex_data_R = (unsigned char*) malloc(tex_image->size); 
00040         gl_tex_data_G = (unsigned char*) malloc(tex_image->size); 
00041         gl_tex_data_B = (unsigned char*) malloc(tex_image->size); 
00042 
00043         imConvertPacking(tex_image->data[0], gl_tex_data_R, tex_image->width, tex_image->height, 
00044         tex_image->depth,tex_image->data_type, 0);
00045         imConvertPacking(tex_image->data[1], gl_tex_data_G, tex_image->width, tex_image->height, 
00046         tex_image->depth,tex_image->data_type, 0);
00047         imConvertPacking(tex_image->data[2], gl_tex_data_B, tex_image->width, tex_image->height, 
00048         tex_image->depth,tex_image->data_type, 0);
00049 
00050         int rgba_count = 0;
00051         for(int i=0; i<width; i++)
00052                 for(int j=0; j<height; j++)
00053                 {
00054                         int ch_count = i*width + j;
00055 
00056                         gl_tex_data_RGBA[rgba_count++] = gl_tex_data_R[ch_count];
00057                         gl_tex_data_RGBA[rgba_count++] = gl_tex_data_G[ch_count];
00058                         gl_tex_data_RGBA[rgba_count++] = gl_tex_data_B[ch_count];
00059 
00060 
00061                         if ((gl_tex_data_R[ch_count] == 0) &&
00062                             (gl_tex_data_G[ch_count] == 0) &&
00063                             (gl_tex_data_B[ch_count] == 0))
00064                                 gl_tex_data_RGBA[rgba_count++] = 0;
00065                         else
00066                                 gl_tex_data_RGBA[rgba_count++] = 255;
00067 
00068                 }
00069 
00070         glGenTextures(1, &tex_id);
00071         glBindTexture(GL_TEXTURE_2D, tex_id);
00072 
00073         glTexImage2D(GL_TEXTURE_2D, 0, 4, tex_image->width, tex_image->height, 0, GL_RGBA,
00074         GL_UNSIGNED_BYTE, gl_tex_data_RGBA);
00075         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
00076         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
00077 
00078 
00079 }


Generated on Tue Dec 5 18:09:10 2006 for Rain by  doxygen 1.4.6