Lesson 05: Unwanted Transperency (Opaquify!)

Started by
2 comments, last by plusnoir 12 years, 5 months ago
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
	glBegin(GL_QUADS);			               // Draw A Quad
	
		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);
    glLoadIdentity();
    glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
    
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity() ;
}



--------------------------------------Amaze your friends! Astound your family! Kennify your text!
Advertisement
Question
Do you have these lines somewhere in your code

glShadeModel(GL_SMOOTH);
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.
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!
--------------------------------------Amaze your friends! Astound your family! Kennify your text!

Question
Do you have these lines somewhere in your code

glShadeModel(GL_SMOOTH);
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. :)

This topic is closed to new replies.

Advertisement