# Lesson 05: Unwanted Transperency (Opaquify!)

This topic is 2554 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Somehow I've managed to screw up on lesson 05. I was able to play around with lessons 1 to 4 properly with no peculiar effect but with lesson 5.... **Read Edit at the end first** Part of my Render() function:
        glLoadIdentity();                                      // Reset The View

static float rcube = 0.0f;                             // Rotation angle for cube
glTranslatef(1.5f,0.0f,-10.0f);			       // Move Right 1.5 Units And Into The Screen 6.0
glRotatef(rcube, 0.0f, 0.0f, 1.0f);                    // Rotate the cube about the axes

glColor3f(0.0f,1.0f,0.0f);	       		// Set The Color To Green
glVertex3f( 1.0f, 1.0f,-1.0f);			// Top Right Of The Quad (Top)
glVertex3f(-1.0f, 1.0f,-1.0f);			// Top Left Of The Quad (Top)
glVertex3f(-1.0f, 1.0f, 1.0f);			// Bottom Left Of The Quad (Top)
glVertex3f( 1.0f, 1.0f, 1.0f);			// Bottom Right Of The Quad (Top)

glColor3f(1.0f,0.5f,0.0f);		      	// Set The Color To Orange
glVertex3f( 1.0f,-1.0f, 1.0f);			// Top Right Of The Quad (Bottom)
glVertex3f(-1.0f,-1.0f, 1.0f);			// Top Left Of The Quad (Bottom)
glVertex3f(-1.0f,-1.0f,-1.0f);			// Bottom Left Of The Quad (Bottom)
glVertex3f( 1.0f,-1.0f,-1.0f);			// Bottom Right Of The Quad (Bottom)

glColor3f(1.0f,0.0f,0.0f);			// Set The Color To Red
glVertex3f( 1.0f, 1.0f, 0.0f);			// Top Right Of The Quad (Front)
glVertex3f(-1.0f, 1.0f, 0.0f);			// Top Left Of The Quad (Front)
glVertex3f(-1.0f,-1.0f, 0.0f);			// Bottom Left Of The Quad (Front)
glVertex3f( 1.0f,-1.0f, 0.0f);			// Bottom Right Of The Quad (Front)

glColor3f(1.0f,1.0f,0.0f);		      	// Set The Color To Yellow
glVertex3f( 1.0f,-1.0f,-1.0f);			// Bottom Left Of The Quad (Back)
glVertex3f(-1.0f,-1.0f,-1.0f);			// Bottom Right Of The Quad (Back)
glVertex3f(-1.0f, 1.0f,-1.0f);			// Top Right Of The Quad (Back)
glVertex3f( 1.0f, 1.0f,-1.0f);			// Top Left Of The Quad (Back)

glColor3f(0.0f,0.0f,1.0f);		      	// Set The Color To Blue
glVertex3f(-1.0f, 1.0f, 1.0f);			// Top Right Of The Quad (Left)
glVertex3f(-1.0f, 1.0f,-1.0f);			// Top Left Of The Quad (Left)
glVertex3f(-1.0f,-1.0f,-1.0f);			// Bottom Left Of The Quad (Left)
glVertex3f(-1.0f,-1.0f, 1.0f);			// Bottom Right Of The Quad (Left)

glColor3f(1.0f,0.0f,1.0f);		      	// Set The Color To Violet
glVertex3f( 1.0f, 1.0f,-1.0f);			// Top Right Of The Quad (Right)
glVertex3f( 1.0f, 1.0f, 1.0f);			// Top Left Of The Quad (Right)
glVertex3f( 1.0f,-1.0f, 1.0f);			// Bottom Left Of The Quad (Right)
glVertex3f( 1.0f,-1.0f,-1.0f);			// Bottom Right Of The Quad (Right)

glEnd();						            // Done Drawing The Cube


Screenshot 1 Ok, so I had no clue why the top and bottom quads turned red with no front quad at all (similar effect for the pyramid). In order to experiment, I tried with putting the Z vertext = 0.0f instead of 1.0f for the FRONT quad (which should show 5 sides of the cube right and the front side halfway inside the box), now look at what happens: Screenshot 2 So basically the front quad is appearing superimposed on the top and bottom quads instead of showing upright... now what maybe causing this effect? Note: When making 2D images in previous lessons all worked fine, so there shouldnt be any problem with the OpenGL initializations (I'm using GLUT btw) ** EDIT ** : Ok after a bit of playing around I found the problem. The problem is that because the quad for the BACK is after the one for the FRONT, the FRONT one gets rendered first, and then the BACK one gets rendered on top of it... even if it's Z co-ords are furthur away, the rendering overlaps the FRONT quad... same with other sides... so I think I'm getting somekind of unwanted transperency... any idea how to opaquify this? :) Thanks. ** EDIT 2 ** It is indeed, a problem with transperency... if I put the code for the FRONT pane after all but the sides, here is what I get: Screenshot 3 Maybe I initialized OpenGL wrongly? Here is how I initialized:

/** MAIN **/

int main(int argc, char *argv[]){
// Initialize
glutInit(&argc, argv);
glutInitWindowSize(800,600);
glutInitWindowPosition(0,0);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("OpenGL Test");
// Register handlers
glutReshapeFunc(ResizeWindow);
glutDisplayFunc(Render);
glutKeyboardFunc(KeyboardEvent);
glutIdleFunc(Idle);
// Begin Loop
glutMainLoop();
//Quit
return EXIT_SUCCESS;
}

/** RESIZE **/

void ResizeWindow(int width, int height){
const float ar = (float) width / (float) height;

glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);

glMatrixMode(GL_MODELVIEW);
}



##### Share on other sites
Question
Do you have these lines somewhere in your code

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

If not then add them just before glutMainLoop(); or where ever the other similar lines are.
The problem here is that the z-buffer is not used, and the polygons just overdraw each other.

##### Share on other sites
Sweet.

I did not go through lesson 1 because I was using glut instead of the Windows API, and therefore missed out the initialization part (and did not want to try and spot it in between all that COM code). Thanks a lot dude!

##### Share on other sites

Question
Do you have these lines somewhere in your code

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

If not then add them just before glutMainLoop(); or where ever the other similar lines are.
The problem here is that the z-buffer is not used, and the polygons just overdraw each other.

I put it into DrawGLScene() just after "glLoadIdentity();"
Helped me a lot too, thank you sir.

1. 1
Rutin
71
2. 2
3. 3
4. 4
5. 5

• 21
• 10
• 33
• 20
• 9
• ### Forum Statistics

• Total Topics
633428
• Total Posts
3011822
• ### Who's Online (See full list)

There are no registered users currently online

×