PDA

View Full Version : Depth testing problem using glOrtho



Tranqui
09-04-2012, 02:57 PM
Hi there,

I'm trying to render some cubes using orthographic projection to create a retro 'perfectly' isometric appearance.

Below is my code below for rendering 3 unit cubes next to each other with one face touching. However, when the cubes are rendered the depth seems to be ignored. Here is a screenshot of what happens:

http://i45.tinypic.com/34o6hqa.png

All of the red cube's faces are rendered underneath the others. How can I ensure these faces render on top? I've googled around and searched the forum but no other solutions I've seen seem to work for me.

Any suggestions would be much appreciated.

Thanks.



#include <GL/glfw.h>

int main()
{
int width, height;
int frame = 0;
bool running = true;

glfwInit();

if( !glfwOpenWindow( 512, 512, 0, 0, 0, 8, 24, 0, GLFW_WINDOW ) )
{
glfwTerminate();
return 0;
}

glfwSetWindowTitle("GLFW Application");

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);

while(running)
{
frame++;

glfwGetWindowSize(&width, &height);
height = height > 0 ? height : 1;

glViewport(0, 0, width, height);

glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-5.0, 5.0, -5.0, 5.0, -5.0, 5.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0);

glColor3d(1.0, 0.0, 0.0);
draw_cube();

glTranslated(1.0, 0.0, 0.0);
glColor3d(0.0, 1.0, 0.0);
draw_cube();

glTranslated(-1.0, 1.0, 0.0);
glColor3d(0.0, 0.0, 1.0);
draw_cube();

glfwSwapBuffers();

// exit if ESC was pressed or window was closed
running = !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam( GLFW_OPENED);
}

glfwTerminate();

return 0;
}

void draw_cube()
{
glBegin(GL_QUADS);

glVertex3f(0.5, 0.5,-0.5);
glVertex3f(-0.5, 0.5,-0.5);
glVertex3f(-0.5, 0.5, 0.5);
glVertex3f( 0.5, 0.5, 0.5);

glVertex3f( 0.5,-0.5, 0.5);
glVertex3f(-0.5,-0.5, 0.5);
glVertex3f(-0.5,-0.5,-0.5);
glVertex3f( 0.5,-0.5,-0.5);

glVertex3f( 0.5, 0.5, 0.5);
glVertex3f(-0.5, 0.5, 0.5);
glVertex3f(-0.5,-0.5, 0.5);
glVertex3f( 0.5,-0.5, 0.5);

glVertex3f( 0.5,-0.5,-0.5);
glVertex3f(-0.5,-0.5,-0.5);
glVertex3f(-0.5, 0.5,-0.5);
glVertex3f( 0.5, 0.5,-0.5);

glVertex3f(-0.5, 0.5, 0.5);
glVertex3f(-0.5, 0.5,-0.5);
glVertex3f(-0.5,-0.5,-0.5);
glVertex3f(-0.5,-0.5, 0.5);

glVertex3f( 0.5, 0.5,-0.5);
glVertex3f( 0.5, 0.5, 0.5);
glVertex3f( 0.5,-0.5, 0.5);
glVertex3f( 0.5,-0.5,-0.5);

glEnd();
}

Joshhua5
09-05-2012, 09:07 AM
It actually appears to be working normally if you think about how it would look. It's easier to understand if you draw in the wireframe.

Tranqui
09-05-2012, 10:07 AM
Ah, you're right. After I added a directional light it became clearer what was going on - I had set up my camera on the wrong side so was expecting to see something else. Thank you!