Sign in to follow this  
gameplayprogammer

Fading reflection in my coverflow demo....

Recommended Posts

I have created a simple coverflow demo using nehe tutorials. I want to now implement a reflection that fades as it ends, just like all the coverflow examples on apple. Currently the reflection of my object is a full reflection, meaning the whole object is reflected. Again i wish a partial faded reflection. How is this done? The code i have is below...

int DrawGLScene(GLvoid)									// Draw Everything
{
#define STENCIL_CODE
#ifdef STENCIL_CODE

	// Clear Screen, Depth Buffer & Stencil Buffer
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

	// Clip Plane Equations
	double eqr[] = {0.0f,-1.0f, 0.0f, 0.0f};			// Plane Equation To Use For The Reflected Objects

	glLoadIdentity();									// Reset The Modelview Matrix
//	glTranslatef(0.0f, -1.6f, zoom);					// Zoom And Raise Camera Above The Floor (Up 0.6 Units)
	Move(-tx, -ty, -tz, -rx, -ry, -rz); //0.0f,0.0f,0.0f); //-rx, -ry, -rz);

	glColorMask(0,0,0,0);								// Set Color Mask
	glEnable(GL_STENCIL_TEST);							// Enable Stencil Buffer For "marking" The Floor
	glStencilFunc(GL_ALWAYS, 1, 1);						// Always Passes, 1 Bit Plane, 1 As Mask
	glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);			// We Set The Stencil Buffer To 1 Where We Draw Any Polygon
														// Keep If Test Fails, Keep If Test Passes But Buffer Test Fails
														// Replace If Test Passes
	glDisable(GL_DEPTH_TEST);							// Disable Depth Testing
	DrawFloor();										// Draw The Floor (Draws To The Stencil Buffer)
														// We Only Want To Mark It In The Stencil Buffer
	glEnable(GL_DEPTH_TEST);							// Enable Depth Testing
	glColorMask(1,1,1,1);								// Set Color Mask to TRUE, TRUE, TRUE, TRUE
	glStencilFunc(GL_EQUAL, 1, 1);						// We Draw Only Where The Stencil Is 1
														// (I.E. Where The Floor Was Drawn)
	glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);				// Don't Change The Stencil Buffer
	glEnable(GL_CLIP_PLANE0);							// Enable Clip Plane For Removing Artifacts
														// (When The Object Crosses The Floor)
	glClipPlane(GL_CLIP_PLANE0, eqr);					// Equation For Reflected Objects
	glPushMatrix();										// Push The Matrix Onto The Stack
		glScalef(1.0f, -1.0f, 1.0f);					// Mirror Y Axis
		glLightfv(GL_LIGHT0, GL_POSITION, LightPos);	// Set Up Light0
		glTranslatef(0.0f, height, 0.0f);				// Position The Object
		glRotatef(xrot, 1.0f, 0.0f, 0.0f);				// Rotate Local Coordinate System On X Axis
		glRotatef(yrot, 0.0f, 1.0f, 0.0f);				// Rotate Local Coordinate System On Y Axis
		DrawCube();									// Draw The Sphere (Reflection)	
	glPopMatrix();										// Pop The Matrix Off The Stack

	glPushMatrix();										// Push The Matrix Onto The Stack
		glScalef(1.0f, -1.0f, 1.0f);					// Mirror Y Axis
		glLightfv(GL_LIGHT0, GL_POSITION, LightPos);	// Set Up Light0		
		glTranslatef(4.0f, height, 0.0f);				// Position The Object
		glRotatef(xrot, 1.0f, 0.0f, 0.0f);				// Rotate Local Coordinate System On X Axis
		glRotatef(yrot, 0.0f, 1.0f, 0.0f);				// Rotate Local Coordinate System On Y Axis
		DrawCube();									// Draw The Sphere (Reflection)
	glPopMatrix();										// Pop The Matrix Off The Stack

	glDisable(GL_CLIP_PLANE0);							// Disable Clip Plane For Drawing The Floor
	glDisable(GL_STENCIL_TEST);							// We Don't Need The Stencil Buffer Any More (Disable)
	glLightfv(GL_LIGHT0, GL_POSITION, LightPos);		// Set Up Light0 Position
	glEnable(GL_BLEND);									// Enable Blending (Otherwise The Reflected Object Wont Show)
	glDisable(GL_LIGHTING);								// Since We Use Blending, We Disable Lighting
	glColor4f(1.0f, 1.0f, 1.0f, 0.8f);					// Set Color To White With 80% Alpha
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);	// Blending Based On Source Alpha And 1 Minus Dest Alpha
	DrawFloor();										// Draw The Floor To The Screen
	glEnable(GL_LIGHTING);								// Enable Lighting
	glDisable(GL_BLEND);								// Disable Blending
	
	glPushMatrix();	
		glTranslatef(0.0f, height, 0.0f);			// Position The Ball At Proper Height
		glRotatef(xrot, 1.0f, 0.0f, 0.0f);					// Rotate On The X Axis
		glRotatef(yrot, 0.0f, 1.0f, 0.0f);					// Rotate On The Y Axis
		DrawCube();											// Draw The Ball
	glPopMatrix();
	
	glPushMatrix();
		glTranslatef(4.0f, height, 0.0f);			// Position The Ball At Proper Height
		glRotatef(xrot, 1.0f, 0.0f, 0.0f);					// Rotate On The X Axis
		glRotatef(yrot, 0.0f, 1.0f, 0.0f);					// Rotate On The Y Axis
		DrawCube();											// Draw The Ball
	glPopMatrix();

glFlush();
}





void DrawCube(float fOffset = 0.0f)	
{
	glColor3f(1.0f, 1.0f, 1.0f);
    glBindTexture( GL_TEXTURE_2D, texture[0] );

    /* NOTE:
     *   The x coordinates of the glTexCoord2f function need to inverted
     * for SDL because of the way SDL_LoadBmp loads the data. So where
     * in the tutorial it has glTexCoord2f( 1.0f, 0.0f ); it should
     * now read glTexCoord2f( 0.0f, 0.0f );
     */
    glBegin(GL_QUADS);
      /* Front Face */
      /* Bottom Left Of The Texture and Quad */
      glTexCoord2f( 0.0f, 1.0f ); glVertex3f( -fRadius, -fRadius, fRadius );
      /* Bottom Right Of The Texture and Quad */
      glTexCoord2f( 1.0f, 1.0f ); glVertex3f(  fRadius, -fRadius, fRadius );
      /* Top Right Of The Texture and Quad */
      glTexCoord2f( 1.0f, 0.0f ); glVertex3f(  fRadius,  fRadius, fRadius );
      /* Top Left Of The Texture and Quad */
      glTexCoord2f( 0.0f, 0.0f ); glVertex3f( -fRadius,  fRadius, fRadius );

      /* Back Face */
      /* Bottom Right Of The Texture and Quad */
      glTexCoord2f( 0.0f, 0.0f ); glVertex3f( -fRadius, -fRadius, -fRadius );
      /* Top Right Of The Texture and Quad */
      glTexCoord2f( 0.0f, 1.0f ); glVertex3f( -fRadius,  fRadius, -fRadius );
      /* Top Left Of The Texture and Quad */
      glTexCoord2f( 1.0f, 1.0f ); glVertex3f(  fRadius,  fRadius, -fRadius );
      /* Bottom Left Of The Texture and Quad */
      glTexCoord2f( 1.0f, 0.0f ); glVertex3f(  fRadius, -fRadius, -fRadius );

      /* Top Face */
      /* Top Left Of The Texture and Quad */
      glTexCoord2f( 1.0f, 1.0f ); glVertex3f( -fRadius,  fRadius, -fRadius );
      /* Bottom Left Of The Texture and Quad */
      glTexCoord2f( 1.0f, 0.0f ); glVertex3f( -fRadius,  fRadius,  fRadius );
      /* Bottom Right Of The Texture and Quad */
      glTexCoord2f( 0.0f, 0.0f ); glVertex3f(  fRadius,  fRadius,  fRadius );
      /* Top Right Of The Texture and Quad */
      glTexCoord2f( 0.0f, 1.0f ); glVertex3f(  fRadius,  fRadius, -fRadius );

      /* Bottom Face */
      /* Top Right Of The Texture and Quad */
      glTexCoord2f( 0.0f, 1.0f ); glVertex3f( -fRadius, -fRadius, -fRadius );
      /* Top Left Of The Texture and Quad */
      glTexCoord2f( 1.0f, 1.0f ); glVertex3f(  fRadius, -fRadius, -fRadius );
      /* Bottom Left Of The Texture and Quad */
      glTexCoord2f( 1.0f, 0.0f ); glVertex3f(  fRadius, -fRadius,  fRadius );
      /* Bottom Right Of The Texture and Quad */
      glTexCoord2f( 0.0f, 0.0f ); glVertex3f( -fRadius, -fRadius,  fRadius );

      /* Right face */
      /* Bottom Right Of The Texture and Quad */
      glTexCoord2f( 0.0f, 0.0f ); glVertex3f( fRadius, -fRadius, -fRadius );
      /* Top Right Of The Texture and Quad */
      glTexCoord2f( 0.0f, 1.0f ); glVertex3f( fRadius,  fRadius, -fRadius );
      /* Top Left Of The Texture and Quad */
      glTexCoord2f( 1.0f, 1.0f ); glVertex3f( fRadius,  fRadius,  fRadius );
      /* Bottom Left Of The Texture and Quad */
      glTexCoord2f( 1.0f, 0.0f ); glVertex3f( fRadius, -fRadius,  fRadius );

      /* Left Face */
      /* Bottom Left Of The Texture and Quad */
      glTexCoord2f( 1.0f, 0.0f ); glVertex3f( -fRadius, -fRadius, -fRadius );
      /* Bottom Right Of The Texture and Quad */
      glTexCoord2f( 0.0f, 0.0f ); glVertex3f( -fRadius, -fRadius,  fRadius );
      /* Top Right Of The Texture and Quad */
      glTexCoord2f( 0.0f, 1.0f ); glVertex3f( -fRadius,  fRadius,  fRadius );
      /* Top Left Of The Texture and Quad */
      glTexCoord2f( 1.0f, 1.0f ); glVertex3f( -fRadius,  fRadius, -fRadius );
    glEnd( );
}

void DrawFloor()										// Draws The Floor
{
	float fFloorRadius = 220.0f;

	glColor4f(0.0f, 0.0f, 0.0f,0.5f);
//	glBindTexture(GL_TEXTURE_2D, texture[0]);			// Select Texture 1 (0)
	glBegin(GL_QUADS);									// Begin Drawing A Quad
		glNormal3f(0.0f, 1.0f, 0.0f);						// Normal Pointing Up
//			glTexCoord2f(0.0f, 1.0f);					// Bottom Left Of Texture
			glVertex3f(-fFloorRadius, 0.0f, fFloorRadius);					// Bottom Left Corner Of Floor
			
//			glTexCoord2f(0.0f, 0.0f);					// Top Left Of Texture
			glVertex3f(-20.0f, 0.0f,-fFloorRadius);					// Top Left Corner Of Floor
			
//			glTexCoord2f(1.0f, 0.0f);					// Top Right Of Texture
			glVertex3f( fFloorRadius, 0.0f,-fFloorRadius);					// Top Right Corner Of Floor
			
//			glTexCoord2f(1.0f, 1.0f);					// Bottom Right Of Texture
			glVertex3f( fFloorRadius, 0.0f, fFloorRadius);					// Bottom Right Corner Of Floor
	glEnd();											// Done Drawing The Quad
}


Share this post


Link to post
Share on other sites
glTexCoord2f( 0.0f, 1.0f ); glVertex3f( -fRadius, -fRadius, fRadius );

You don't have normals.
If you only learn GL (and you have GF8 or sth better), try to learn 3.2 version.

Share this post


Link to post
Share on other sites
I have added normals to six faces of my cube, there was a difference, some of the cube faces became brighter while others became darker. But this does not solve my problem... I am looking for the reflection of my cube on the floor of my scene to gradually fade out, at the moment i see a full copy of my cube on the floor, i only want to see about half of it reflected on the floor. ANy help on this....

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this