Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Lesson 05: Unwanted Transperency (Opaquify!)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Verminox   Members   -  Reputation: 166

Like
1Likes
Like

Posted 06 October 2006 - 05:14 AM

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() ;
}





Sponsor:

#2 lc_overlord   Members   -  Reputation: 435

Like
1Likes
Like

Posted 06 October 2006 - 05:48 AM

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.

#3 Verminox   Members   -  Reputation: 166

Like
0Likes
Like

Posted 06 October 2006 - 06:09 AM

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!

#4 plusnoir   Members   -  Reputation: 106

Like
0Likes
Like

Posted 24 November 2011 - 05:15 AM

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. :)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS