Shapes not showing depth

Hi,

I have a problem with shapes showing depth.

Basically the screen that I want to render uses Ortho for the status panel and ModelView Matrix for the other 3D stuff. However my shapes, drawn under the modelview matrix does not display properly. The depth is not represented correctly.

In the screenshot below, the yellow quad is supposed to be rendered behind the red quad, but yet, it is showing it is showing in front. It does not matter whether I put the code for the yellow quad before or after the red quad, it still displays the same thing.

I have enabled depth test and this part of the code switches fron ortho to render the 3D scene. Any advice on my problem?


	glPushMatrix();
	gluPerspective(45.0f,800/600, 0.1f, 100.0f);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	// Red quad
	glColor3f(1.0f, 0.0f, 0.0f);
	glBegin(GL_QUADS);
	glVertex3f(0, -380, -3.6);
	glVertex3f(0, -100, -3.6 );
	glVertex3f(-180, -100, -3.6 );
	glVertex3f(-180, -380, -3.6);
	glEnd();

	// Yellow quad
	glColor3f(1.0f, 1.0f, 0.0f);
	glBegin(GL_QUADS);
	glVertex3f(0, -380, -5.6);
	glVertex3f(0, -100, -5.6 );
	glVertex3f(-100, -100, -5.6 );
	glVertex3f(-100, -380, -5.6);
	glEnd();

	glMatrixMode(GL_PROJECTION);
	glPopMatrix();

Edit: it seems like that the forum does not support images, so please click on the link for the screen dump.

from the code u posted , i dont see anything wrong expect that u should reset ur projection matrix to identity before setting perspective projection :
glMatrixMode(GL_PROJECTION);
// setup parallel projection
glortho(…)
//2d drawing

glMatrixMode(GL_PROJECTION)
glPushMatrix();
glLoadIdentity();//reset projection to identity
gluPerspective(45.0f,800/600, 0.1f, 100.0f);
//3d drawing

I pressed the wrong button.

I would try using seperate matrices.

@ Abdallah DIB

I tried implementing your suggestion

glMatrixMode(GL_PROJECTION)
glPushMatrix();
glLoadIdentity();//reset projection to identity

but now none of the 3D stuff is appearing on screen. Unless I need to do something to setup parallel projection as you mentioned on the first part of the code, it’s not working. Also, what do I need to do to setup parallel projection? Sorry, if this is something simple, but I only have beginner’s knowledge of OpenGL. I currently have first part of the code drawing 2D stuff in Ortho mode and the next part drwing in 3D.

@ mikeynovemberoscar

Do you mean using a set of glPushMatrix() and glPopMatrix() enclosing the ortho and 3D modes? I’ve had it like that from the start.

glOrtho setup a parallel projection while GLFrustum or gluperspective setup a perspective one where the parallel lines meet at infinity(the way in which objects appear to the eye ).
now the matrices generated by this two functions will be multiplied by the previous matrix , that is why u should reset to identity before setting ortho or frustum .( it does not make sense to multiply an ortho with frustum projection ).
now to know why ur 3d objects do not appear can u post some code ?
anyway verify that u intialize ur modelview matrices after the 2D drawing !
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// setup parallel projection
glortho(…)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
//position ur 2d objects using glrotate …
//2d drawing

glMatrixMode(GL_PROJECTION)
glLoadIdentity();//reset projection to identity
gluPerspective(45.0f,800/600, 0.1f, 100.0f);
//3d drawing
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//position ur 3d models glrotate …
draw3d …

Thanks for the explanation. I have modelview matrices set up. I have posted my code so that you can see what might be the problem. Thanks in advanced.

This is the code from the init function


void init_OpenGL()
{
	glfwEnable( GLFW_MOUSE_CURSOR );		// Enable mouse cursor
	glEnable(GL_TEXTURE_2D);			// Enable texturing
	glEnable(GL_CULL_FACE);				// Enable back face culling
	glEnable(GL_DEPTH_TEST);			// Enable depth test (for 3D drawing)
	glfwSetKeyCallback(keyhandler);			// Register keyboard controls
	glfwEnable(GLFW_KEY_REPEAT);			// Allow keys to repeat whilst pressed down
	glClearColor(0.0, 0.0, 0.0, 0.0);		// Set background colour to black
	glMatrixMode(GL_PROJECTION);			// Switch to projection matrix mode	
	glLoadIdentity();				// Reloads the matrix
	glOrtho(-400.0, 400.0, -300.0, 300.0, -100.0, 100.0);	// We are using Ortho view and one unit is 1 pixel.
}


Here is the code for render the screen in question.


void atGameScreen()
{
	// When the level is completed, run this statement
	if (percentCompleted == 100){		// if the level is completed
		gameBrightness -= 0.005;		// slowly fade out the screen
		if (gameBrightness <= 0.0f){	// once completely faded
			BACKSPACEpressed();			// simulate a backspace key press to reset variables
		}
	// When the level starts, run this statement					
	}else if (gameBrightness <= 2.0){	// if the game screen is not at 100% brightness
		gameBrightness += 0.005;		// slowly fade in the screen
	}else if (areYouReadyPos >=800.0f){	// If the "ARE YOU READY" sign is gone freom view
		a += 0.05;						// expand the background stars to simulate slying through space
		b -= 0.05;
		c += 0.05;
	}
	glPushMatrix();
	glColor3f(gameBrightness, gameBrightness, gameBrightness);
	glEnable(GL_TEXTURE_2D);
	// Draw the arrow bar underlay at the top of the screen
	glBindTexture(GL_TEXTURE_2D, g_Texture[0]);
	glBegin(GL_QUADS);
		glTexCoord2f(1.0f, 0.0f); glVertex3f(400.0f, 200.0f, -0.2f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f(400.0f, 300.0f, -0.2f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-400.0f, 300.0f, -0.2f);
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-400.0f, 200.0f, -0.2f);
	glEnd();

	// Draw the status panel
	glBindTexture(GL_TEXTURE_2D, g_Texture[1]);
	glBegin(GL_QUADS);
		glTexCoord2f(1.0f, 0.0f); glVertex3f(a, b, -90.2f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f(a, c, -90.2f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-a, c, -90.2f);
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-a, b, -90.2f);
	glEnd();

	// Draw the song thumbnail image 
	glBindTexture(GL_TEXTURE_2D, thumb_Texture[index]);
	glBegin(GL_QUADS);
		glTexCoord2f(1.0f, 0.0f); glVertex3f(390, 206, -0.1f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f(390, 279, -0.1f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(317, 279, -0.1f);
		glTexCoord2f(0.0f, 0.0f); glVertex3f(317, 206, -0.1f);
	glEnd();


	// This deals the the flashing of the target line
	glDisable(GL_TEXTURE_2D);	
	if (time >= 0.08){
		glColor3f(gameBrightness - 1.65f, gameBrightness - 1.65f, gameBrightness - 1.65f);
	}else if (time >= songs[index].getBeatTime() - timeSkip - 0.08){
		glColor3f(gameBrightness, gameBrightness, gameBrightness);
	}else{
		glColor3f(gameBrightness, gameBrightness, gameBrightness);
	}

	// Draw the left arrow of the target line
	glBegin(GL_TRIANGLES);
		glVertex3f(-400, -248, 0.0f);
		glVertex3f(-370, -239, 0.0f);
		glVertex3f(-400, -230, 0.0f);
	glEnd();

	// Draw the target line across the screen
	glBegin(GL_TRIANGLES);
		glVertex3f(370, -239, 0.0f);
		glVertex3f(400, -248, 0.0f);
		glVertex3f(400, -230, 0.0f);
	glEnd();

	// Draw the right arrow of the target line
	glBegin(GL_QUADS);
		glVertex3f(400, -240, 0.0f);
		glVertex3f(400, -238, 0.0f);
		glVertex3f(-400, -238, 0.0f);
		glVertex3f(-400, -240, 0.0f);
	glEnd();

	// The following draws the beat on the arrow bar on top of the screen
	if (beat == 1){									// If beat count is 1 then
		glColor3f(0.0,gameBrightness, 0.0);			// Draw green beat bar (Beat 1)
		glBegin(GL_QUADS);
		glVertex3f(-201.0f, 291.0f, 0.0f);
		glVertex3f(-201.0f, 300.0f, 0.0f);
		glVertex3f(-399.0f, 300.0f, 0.0f);
		glVertex3f(-399.0f, 291.0f, 0.0f);
		glEnd();

	}else if (beat == 2){							// If beat count is 2 then
		glColor3f(gameBrightness,gameBrightness, 0.0);// Draw green yellow bar (Beat 2)
		glBegin(GL_QUADS);
		glVertex3f(-1.0f, 291.0f, 0.0f);
		glVertex3f(-1.0f, 300.0f, 0.0f);
		glVertex3f(-199.0f, 300.0f, 0.0f);
		glVertex3f(-199.0f, 291.0f, 0.0f);
		glEnd();
	}else if (beat == 3){							// If beat count is 3 then
		glColor3f(gameBrightness,gameBrightness, 0.0);// Draw green yellow bar (Beat 3)
		glBegin(GL_QUADS);
		glVertex3f(199.0f, 291.0f, 0.0f);
		glVertex3f(199.0f, 300.0f, 0.0f);
		glVertex3f(1.0f, 300.0f, 0.0f);
		glVertex3f(1.0f, 291.0f, 0.0f);
		glEnd();
	}else if (beat ==4){							// If beat count is 4 then
		glColor3f(gameBrightness,gameBrightness, 0.0);// Draw green yellow bar (Beat 4)
		glBegin(GL_QUADS);
		glVertex3f(399.0f, 291.0f, 0.0f);
		glVertex3f(399.0f, 300.0f, 0.0f);
		glVertex3f(201.0f, 300.0f, 0.0f);
		glVertex3f(201.0f, 291.0f, 0.0f);
		glEnd();
	}
	drawGameText();
	glPopMatrix();


	// FINISHED ORTHO DRAWING AND BEGIN USING 3D VIEW

	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	gluPerspective(45.0f,800/600, 0.1f, 100.0f);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();


	glColor3f(1.0f, 0.0f, 0.0f);               // Draw red quad
	glBegin(GL_QUADS);
	glVertex3f(0, -380, -3.6);
	glVertex3f(0, -100, -3.6 );
	glVertex3f(-180, -100, -3.6 );
	glVertex3f(-180, -380, -3.6);
	glEnd();

	glColor3f(1.0f, 1.0f, 0.0f);               // Draw yellow quad

	glBegin(GL_QUADS);
	glVertex3f(0, -380, -5.6);
	glVertex3f(0, -100, -5.6 );
	glVertex3f(-100, -100, -5.6 );
	glVertex3f(-100, -380, -5.6);
	glEnd();

	glMatrixMode(GL_PROJECTION);
	glPopMatrix();
}

disable Face culling glDisable(GL_CULL_FACE); before the 3D drawing part or if u want to keep face culling enabled then send ur quad vertices in CCW( conter clock wise ) order.

Thanks for the help, but the problem is not with face culling. If you look at the code carefully, all quad faces are drawn counter clockwise.

The problem remains that if I add these extra lines of code,

glMatrixMode(GL_PROJECTION); // Added code
glPushMatrix();
glLoadIdentity(); // Added code

My 3D scene disappears. If I do not add these lines, my 3D scene appears but shapes drawn behind of another are shown in front.

I am still unable to solve the problem.

sorry it was my mistake i didnt read carefully the , ur vertices are drawn in the correct order .
i dont see anything wrong , but can u try to specify a znear value to 1 .
something like this,
gluPerspective(45.0f,800/600, 1.0f, 100.0f);
because maybe for values near zero can cause depth buffering to not work correctly .