Advertisement

Lesson 19:Question regarding coloring particles

Started by August 16, 2009 10:28 PM
1 comment, last by dgma9271 15 years, 3 months ago
In chapter 19 Nehe Tutorial creates particles by constructing quads and applying colored texture on them. First he initializes 1000 particles like this: int InitGL(GLvoid) // All Setup For OpenGL Goes Here { if (!LoadGLTextures()) // Jump To Texture Loading Routine { return 0; // If Texture Didn't Load Return FALSE } glShadeModel(GL_SMOOTH); // Enable Smooth Shading glClearColor(0.0f,0.0f,0.0f,0.0f); // Black Background glClearDepth(1.0f); // Depth Buffer Setup glDisable(GL_DEPTH_TEST); // Disable Depth Testing glEnable(GL_BLEND); // Enable Blending glBlendFunc(GL_SRC_ALPHA,GL_ONE); // Type Of Blending To Perform glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST); // Really Nice Perspective Calculations glHint(GL_POINT_SMOOTH_HINT,GL_NICEST); // Really Nice Point Smoothing glEnable(GL_TEXTURE_2D); // Enable Texture Mapping glBindTexture(GL_TEXTURE_2D,texture[0]); // Select Our Texture for (loop=0;loop<MAX_PARTICLES;loop++) // Initials All The Textures { particle[loop].active=true; // Make All The Particles Active particle[loop].life=1.0f; // Give All The Particles Full Life particle[loop].fade=float(rand()%100)/1000.0f+0.003f; // Random Fade Speed particle[loop].r=colors[(loop+1)/(MAX_PARTICLES/12)][0]; // Select Red Rainbow Color particle[loop].g=colors[(loop+1)/(MAX_PARTICLES/12)][1]; // Select Green Rainbow Color particle[loop].b=colors[(loop+1)/(MAX_PARTICLES/12)][2]; // Select Blue Rainbow Color particle[loop].xi=float((rand()%50)-26.0f)*10.0f; // Random Speed On X Axis particle[loop].yi=float((rand()%50)-25.0f)*10.0f; // Random Speed On Y Axis particle[loop].zi=float((rand()%50)-25.0f)*10.0f; // Random Speed On Z Axis particle[loop].xg=0.0f; // Set Horizontal Pull To Zero particle[loop].yg=-0.8f; // Set Vertical Pull Downward particle[loop].zg=0.0f; // Set Pull On Z Axis To Zero } return 1; // Initialization Went OK } Then those particles are drawn with the color from the given color array. the code is below. If you view the exe file you will see the color of the center fire is always white. I don't understand where in the code makes it white? There is no white color in colors array. There is no (1,1,1) in colors array. So what makes that center always white? static GLfloat colors[12][3]= // Rainbow Of Colors { {1.0f,0.5f,0.5f},{1.0f,0.75f,0.5f},{1.0f,1.0f,0.5f},{0.75f,1.0f,0.5f}, {0.5f,1.0f,0.5f},{0.5f,1.0f,0.75f},{0.5f,1.0f,1.0f},{0.5f,0.75f,1.0f}, {0.5f,0.5f,1.0f},{0.75f,0.5f,1.0f},{1.0f,0.5f,1.0f},{1.0f,0.5f,0.75f} }; 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 ModelView Matrix for (loop=0;loop<MAX_PARTICLES;loop++) // Loop Through All The Particles { if (particle[loop].active) // If The Particle Is Active { float x=particle[loop].x; // Grab Our Particle X Position float y=particle[loop].y; // Grab Our Particle Y Position float z=particle[loop].z+zoom; // Particle Z Pos + Zoom // Draw The Particle Using Our RGB Values, Fade The Particle Based On It's Life glColor4f(particle[loop].r,particle[loop].g,particle[loop].b,particle[loop].life); glBegin(GL_TRIANGLE_STRIP); // Build Quad From A Triangle Strip glTexCoord2d(1,1); glVertex3f(x+0.5f,y+0.5f,z); // Top Right glTexCoord2d(0,1); glVertex3f(x-0.5f,y+0.5f,z); // Top Left glTexCoord2d(1,0); glVertex3f(x+0.5f,y-0.5f,z); // Bottom Right glTexCoord2d(0,0); glVertex3f(x-0.5f,y-0.5f,z); // Bottom Left glEnd(); // Done Building Triangle Strip particle[loop].x+=particle[loop].xi/(slowdown*1000);// Move On The X Axis By X Speed particle[loop].y+=particle[loop].yi/(slowdown*1000);// Move On The Y Axis By Y Speed particle[loop].z+=particle[loop].zi/(slowdown*1000);// Move On The Z Axis By Z Speed particle[loop].xi+=particle[loop].xg; // Take Pull On X Axis Into Account particle[loop].yi+=particle[loop].yg; // Take Pull On Y Axis Into Account particle[loop].zi+=particle[loop].zg; // Take Pull On Z Axis Into Account particle[loop].life-=particle[loop].fade; // Reduce Particles Life By 'Fade' if (particle[loop].life<0.0f) // If Particle Is Burned Out { particle[loop].life=1.0f; // Give It New Life particle[loop].fade=float(rand()%100)/1000.0f+0.003f; // Random Fade Value particle[loop].x=0.0f; // Center On X Axis particle[loop].y=0.0f; // Center On Y Axis particle[loop].z=0.0f; // Center On Z Axis particle[loop].xi=xspeed+float((rand()%60)-32.0f); // X Axis Speed And Direction particle[loop].yi=yspeed+float((rand()%60)-30.0f); // Y Axis Speed And Direction particle[loop].zi=float((rand()%60)-30.0f); // Z Axis Speed And Direction particle[loop].r=colors[col][0]; // Select Red From Color Table particle[loop].g=colors[col][1]; // Select Green From Color Table particle[loop].b=colors[col][2]; // Select Blue From Color Table } // If Number Pad 8 And Y Gravity Is Less Than 1.5 Increase Pull Upwards if (keys[SDLK_KP8] && (particle[loop].yg<1.5f)) particle[loop].yg+=0.01f; // If Number Pad 2 And Y Gravity Is Greater Than -1.5 Increase Pull Downwards if (keys[SDLK_KP2] && (particle[loop].yg>-1.5f)) particle[loop].yg-=0.01f; // If Number Pad 6 And X Gravity Is Less Than 1.5 Increase Pull Right if (keys[SDLK_KP6] && (particle[loop].xg<1.5f)) particle[loop].xg+=0.01f; // If Number Pad 4 And X Gravity Is Greater Than -1.5 Increase Pull Left if (keys[SDLK_KP4] && (particle[loop].xg>-1.5f)) particle[loop].xg-=0.01f; if (keys[SDLK_TAB]) // Tab Key Causes A Burst { particle[loop].x=0.0f; // Center On X Axis particle[loop].y=0.0f; // Center On Y Axis particle[loop].z=0.0f; // Center On Z Axis particle[loop].xi=float((rand()%50)-26.0f)*10.0f; // Random Speed On X Axis particle[loop].yi=float((rand()%50)-25.0f)*10.0f; // Random Speed On Y Axis particle[loop].zi=float((rand()%50)-25.0f)*10.0f; // Random Speed On Z Axis } } } SDL_GL_SwapBuffers (); return 1; // Everything Went OK }
Just taking a quick look at the example picture of the app, I would say the white color is just the accumulation of all the particles blending together at that center point. Basically, the colors accumulate until they are clamped at full red, green, and blue, and what you get is white.
---------------------------Visit my Blog at http://robwalkerdme.blogspot.com
Advertisement
I never thought of that. ..very interesting. Thanks dudeman21.

This topic is closed to new replies.

Advertisement