17 | | == Explosions, Particle Rendering - from NeHe == |
18 | | source = http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=30 [[br]] |
19 | | Every time a collision takes place an explosion is triggered at the collision point. A nice way to model explosions is to alpha blend two polygons which are perpendicular to each other and have as the center the point of interest (here intersection point). The polygons are scaled and disappear over time. The disappearing is done by changing the alpha values of the vertices from 1 to 0, over time. Because a lot of alpha blended polygons can cause problems and overlap each other (as it is stated in the Red Book in the chapter about transparency and blending) because of the Z buffer, we borrow a technique used in particle rendering. To be correct we had to sort the polygons from back to front according to their eye point distance, but disabling the Depth buffer writes (not reads) also does the trick (this is also documented in the red book). Notice that we limit our number of explosions to maximum 20 per frame, if additional explosions occur and the buffer is full, the explosion is discarded. The source which updates and renders the explosions is: |
20 | | {{{ |
21 | | |
22 | | / Render / Blend Explosions |
23 | | glEnable(GL_BLEND); // Enable Blending |
24 | | glDepthMask(GL_FALSE); // Disable Depth Buffer Writes |
25 | | glBindTexture(GL_TEXTURE_2D, texture[1]); // Upload Texture |
26 | | for(i=0; i<20; i++) // Update And Render Explosions |
27 | | { |
28 | | if(ExplosionArray[i]._Alpha>=0) |
29 | | { |
30 | | glPushMatrix(); |
31 | | ExplosionArray[i]._Alpha-=0.01f; // Update Alpha |
32 | | ExplosionArray[i]._Scale+=0.03f; // Update Scale |
33 | | // Assign Vertices Colour Yellow With Alpha |
34 | | // Colour Tracks Ambient And Diffuse |
35 | | glColor4f(1,1,0,ExplosionArray[i]._Alpha); // Scale |
36 | | glScalef(ExplosionArray[i]._Scale,ExplosionArray[i]._Scale,ExplosionArray[i]._Scale); |
37 | | // Translate Into Position Taking Into Account The Offset Caused By The Scale |
38 | | glTranslatef((float)ExplosionArray[i]._Position.X()/ExplosionArray[i]._Scale, |
39 | | (float)ExplosionArray[i]._Position.Y()/ExplosionArray[i]._Scale, |
40 | | (float)ExplosionArray[i]._Position.Z()/ExplosionArray[i]._Scale); |
41 | | glCallList(dlist); // Call Display List |
42 | | glPopMatrix(); |
43 | | } |
44 | | } |
45 | | |
46 | | }}} |
47 | | |