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.
glVertex3f
,glTexCoord2f
...) are deprecated in modern OpenGL. Check this question for a few pointers if you want to use modern OpenGL with Java. \$\endgroup\$