templatevk
templatevk

Reputation: 3

OpenGL glOrtho and GL_DEPTH_TEST

I'm trying to draw a simple triangle and I've faced a problem combining glEnable(GL_DEPTH_TEST) and glOrtho. The triangle is displayed without depth test not depending on the nearVal argument but if the depth test is enabled and the nearVal is different from 0 then nothing is displayed. Here is the code.

@Override
public void init(GLAutoDrawable drawable) {
    GL2 gl = drawable.getGL().getGL2();

    gl.glShadeModel(GL2.GL_SMOOTH);
    gl.glEnable(GL2.GL_DEPTH_TEST);
    gl.glDepthFunc(GL2.GL_LEQUAL);
    gl.glClearColor(0, 0, 0, 1);
    gl.glClearDepth(100.0);

    gl.glMatrixMode(GL2.GL_PROJECTION);
    gl.glLoadIdentity();

    // the nearVal = 1 and the triangle is not displayed
    // if we set the nearVal = 1 then the triangle is displayed
    gl.glOrtho(-1, 1, -1, 1, 1, 10); 
}

@Override
public void display(GLAutoDrawable drawable) {      
    GL2 gl = drawable.getGL().getGL2();

    gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);

    gl.glBegin(GL.GL_TRIANGLES);        
    gl.glColor3f(1, 0, 0);
    gl.glVertex3f(0.0f, 0.0f, 0);       
    gl.glColor3f(0, 1, 0);
    gl.glVertex3f(1, 0.0f, 0);      
    gl.glColor3f(0, 0, 1);
    gl.glVertex3f(0f, 1f, 0);       
    gl.glEnd();

    gl.glFlush();
}

Upvotes: 0

Views: 1612

Answers (1)

tbalazs
tbalazs

Reputation: 599

Your glVertex3f calls define a triangle on the z=0 plane. If you set the nearPlane parameter of the glOrtho call to one, then your triangle will be clipped out.

Upvotes: 3

Related Questions