Nehe Lesson 26

Started by
3 comments, last by quophyie 15 years, 8 months ago
Hi I have been reading Nehe lesson 26 and I am wondering why the DrawObject (below)function draws 2 balls i.e. the 'real' ball and the reflected ball on every call. Would it not have been easier to have the DrawBall() function draw just one ball each time its called i.e. draw the ball once above the floor and once on the stencil buffer. It seems to me that the second ball does nothing as and it just doubles the ball drawing. Is there a good reason for drawing the ball twice. An Explanation will be very much appreciated.

void DrawObject()								// Draw Our Ball
{
	glColor3f(1.0f, 1.0f, 1.0f);						// Set Color To White
	glBindTexture(GL_TEXTURE_2D, texture[1]);				// Select Texture 2 (1)
	gluSphere(q, 0.35f, 32, 16);						// Draw First Sphere

   
/*After drawing the first sphere, we select a new texture (EnvRoll), set the alpha value to 40% and enable blending based on the source alpha value. glEnable(GL_TEXTURE_GEN_S) and glEnable(GL_TEXTURE_GEN_T) enables sphere mapping. 

After doing all that, we redraw the sphere, disable sphere mapping and disable blending. 

The final result is a reflection that almost looks like bright points of light mapped to the beach ball. Because we enable sphere mapping, the texture is always facing the viewer, even as the ball spins. We blend so that the new texture doesn't cancel out the old texture (a form of multitexturing).   */
   

	glBindTexture(GL_TEXTURE_2D, texture[2]);				// Select Texture 3 (2)
	glColor4f(1.0f, 1.0f, 1.0f, 0.4f);					// Set Color To White With 40% Alpha
	glEnable(GL_BLEND);							// Enable Blending
	glBlendFunc(GL_SRC_ALPHA, GL_ONE);					// Set Blending Mode To Mix Based On SRC Alpha
	glEnable(GL_TEXTURE_GEN_S);						// Enable Sphere Mapping
	glEnable(GL_TEXTURE_GEN_T);						// Enable Sphere Mapping

	gluSphere(q, 0.35f, 32, 16);						// Draw Another Sphere Using New Texture
										// Textures Will Mix Creating A MultiTexture Effect (Reflection)
	glDisable(GL_TEXTURE_GEN_S);						// Disable Sphere Mapping
	glDisable(GL_TEXTURE_GEN_T);						// Disable Sphere Mapping
	glDisable(GL_BLEND);							// Disable Blending
}



Advertisement
This DrawObject method just draws a ball on one position. It draws it twice to apply Environment Sphere mapping. This is achieved by drawing the ball a second time with blending enabled, so the color values get modulated where the new ball is rendered. Notice the use of a different texture and the texture coordinate generation enabled when drawing the second sphere.

Within the Draw() method this must be called twice, once for the real sphere, then render the reflecting plane to the stencil buffer, after that render the sphere again, this time with glScalef(1,-1,1) so its y-axis gets inverted.
Thanks man!. I get it now. I should have realised that he was drawing the ball twice in the same position and adding different textures. Cheers Caste!. Anyway, what is an environment sphere?
The first sentence of Lesson 23 says it all: Sphere Environment Mapping is a quick way to add a reflection to a metallic or reflective object in your scene.
Thanks again man! Very much appreciated

This topic is closed to new replies.

Advertisement