0
\$\begingroup\$

OK, so I successfully learned how to bind textures to quads and display this cool looking crate. However, when I rotate the cube something goes wrong. During rotation the back-face of the cube overlays the front-face and appears in front of the front-face. This is also the case with the right and left faces. They overlay each other giving a weird perception. I'm not sure why this happens. Here is the code where I draw only four face of the quad (front, back right, left).

gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);      // clear the screen and depth buffers
gl.glLoadIdentity();                            // reset the current modelview matrix

    // draw quad
    gl.glTranslatef(0.0f, 0.0f, -6.0f);                 // translate to the left and into the screen
    gl.glRotatef(rotateAngle, 0.0f, 1.0f, 0.0f);        // rotate triangle around the y-axis
    gl.glBindTexture(GL2.GL_TEXTURE_2D, textures[2]);   // bind the texture you want to use to gl_texture_2d
    gl.glBegin(GL2.GL_QUADS);

        // front face
        gl.glTexCoord2f(0.0f, 1.0f);                // bottom left
        gl.glVertex3f(-1.0f, -1.0f, 1.0f);
        gl.glTexCoord2f(1.0f, 1.0f);                // bottom right
        gl.glVertex3f(1.0f, -1.0f, 1.0f);
        gl.glTexCoord2f(1.0f, 0.0f);                // top right
        gl.glVertex3f(1.0f, 1.0f, 1.0f);
        gl.glTexCoord2f(0.0f, 0.0f);                // top left
        gl.glVertex3f(-1.0f, 1.0f, 1.0f);

        // right face
        gl.glTexCoord2f(0.0f, 1.0f);                // bottom left
        gl.glVertex3f(1.0f, -1.0f, 1.0f);
        gl.glTexCoord2f(1.0f, 1.0f);                // bottom right
        gl.glVertex3f(1.0f, -1.0f, -1.0f);
        gl.glTexCoord2f(1.0f, 0.0f);                // top right
        gl.glVertex3f(1.0f, 1.0f, -1.0f);
        gl.glTexCoord2f(0.0f, 0.0f);                // top left
        gl.glVertex3f(1.0f, 1.0f, 1.0f);

        // left face
        gl.glTexCoord2f(0.0f, 1.0f);                // bottom left
        gl.glVertex3f(-1.0f, -1.0f, -1.0f);
        gl.glTexCoord2f(1.0f, 1.0f);                // bottom right
        gl.glVertex3f(-1.0f, -1.0f, 1.0f);
        gl.glTexCoord2f(1.0f, 0.0f);                // top right
        gl.glVertex3f(-1.0f, 1.0f, 1.0f);
        gl.glTexCoord2f(0.0f, 0.0f);                // top left
        gl.glVertex3f(-1.0f, 1.0f, -1.0f);

        // back face
        gl.glTexCoord2f(0.0f, 1.0f);                // bottom left
        gl.glVertex3f(-1.0f, -1.0f, -1.0f);
        gl.glTexCoord2f(1.0f, 1.0f);                // bottom right
        gl.glVertex3f(1.0f, -1.0f, -1.0f);
        gl.glTexCoord2f(1.0f, 0.0f);                // top right
        gl.glVertex3f(1.0f, 1.0f, -1.0f);
        gl.glTexCoord2f(0.0f, 0.0f);                // top left
        gl.glVertex3f(-1.0f, 1.0f, -1.0f);

    gl.glEnd();

Moreover, the way I draw textures and then vertices is not yet clear to me. According to what I've read the bottom-left corner (0, 0) is where the textures start drawing, so I should also start drawing my quad from bottom-left accordingly. I tried that but it didn't work. The texture was drawn, but up side down. So I had to go through some trial and error till I got the texture drawn correctly. I wish I could attach a picture to this post so I can show you how the back face of the cube overlays the front one during rotation, but I think this facility isn't provided in stack exchange. Thanks.

\$\endgroup\$
2
  • \$\begingroup\$ Another observation: almost all the library functions you are using (glVertex3f, glTexCoord2f...) are deprecated in modern OpenGL. Check this question for a few pointers if you want to use modern OpenGL with Java. \$\endgroup\$ Commented Apr 23, 2012 at 9:07
  • 1
    \$\begingroup\$ unfortunately i can't upload a screen-shot. stack exchange requires me to have more reputation :( but thanks for pointing that out \$\endgroup\$
    – moeabdol
    Commented Apr 23, 2012 at 9:36

2 Answers 2

3
\$\begingroup\$

I figured the problem out! at first I thought it was a driver problem because i'm using mac os x like this http://forum.jogamp.org/Depth-buffer-not-working-on-Win7-64b-td1737435.html but then i tried it on 64-bit windows and the same problem occurred. Finally, I found out that it was a code problem due to gluProspective(). I didn't set zNear correctly I should have glu.gluPerspective(45.0f, (float) width / height, 1.0f, 100.0f) instead of glu.gluPerspective(45.0f, (float) width / height, 0.0f, 100.0f). Because opengl expects zNear to always be positive. Hope this will help other beginners who face the same problem.

\$\endgroup\$
1
  • \$\begingroup\$ Don't hesitate to accept your own answer, so that people see it at the top of the list. \$\endgroup\$ Commented Apr 25, 2012 at 7:54
0
\$\begingroup\$

Is depth test enabled and preserved? You need the following code somewhere at initialisation:

gl.glClearDepth(1.0f);
gl.glEnable(GL.GL_DEPTH_TEST);
gl.glDepthFunc(GL.GL_LEQUAL);
\$\endgroup\$
1
  • \$\begingroup\$ yes, i have added them at init(). here are the content of my init gl = drawable.getGL().getGL2(); glu = new GLUgl2(); gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); gl.glEnable(GL2.GL_TEXTURE_2D); gl.glTexEnvi(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, GL2.GL_DECAL); gl.glShadeModel(GL2.GL_SMOOTH); gl.glClearDepth(1.0f); gl.glEnable(GL2.GL_DEPTH_TEST); gl.glDepthFunc(GL2.GL_LEQUAL); gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST); \$\endgroup\$
    – moeabdol
    Commented Apr 23, 2012 at 9:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .