I use JOGL (Java OpenGL). I have a parallelepiped spinning with glRotatef . I draw it like this:

  private void drawCenteredCube(GL2 gl) { int height = (int) SizeUtil.getHeight(); int width = (int) SizeUtil.getWidth(); int depth = (int) SizeUtil.getDepth(); gl.glColor4f(0.4f, 0.3f, 0.3f, 0); drawSquareFace(gl, height, depth, width); // бок gl.glColor4f(0.09f, 0.095f, 0.095f, 0); gl.glRotatef(90, 1, 0, 0); drawSquareFace(gl, height, width, depth); // зад gl.glColor4f(0.61f, 0.05f, 0.1f, 0); gl.glRotatef(90, 0, 1, 0); drawSquareFace(gl, depth, width, height); // низ gl.glRotatef(90, 0, 1, 0); ForegroundUtil.paint(gl, 0, 0, depth / 2); // лицо gl.glColor4f(0.4f, 0.3f, 0.3f, 0); gl.glRotatef(90, 1, 0, 0); drawSquareFace(gl, height, depth, width); // бок gl.glColor4f(0.61f, 0.05f, 0.1f, 0); gl.glRotatef(90, 0, 1, 0); drawSquareFace(gl, width, depth, height); // верх gl.glColor3f(0.0f, 0.0f, 0.0f); } 

The problem is that I constantly have some parts of the figure drawn on top of others, although in fact they should not be visible at a specific point in time. How to understand when some face to draw?

    2 answers 2

    Emnip to include:

     glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); //функция сравнения глубины - на ваше усмотрение 

    for the cleaning

     glClear(GL_DEPTH_BUFFER_BIT); 
    • I changed the display method. It was like this: gl.glClear (GL.GL_COLOR_BUFFER_BIT); It was like this: gl.glEnable (GL.GL_DEPTH_TEST); gl.glClear (GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); Indeed, now the unnecessary does not draw. But wildly flickering on the textures (on the faces without textures everything is fine). - angry
    • gl.glEnable(GL.GL_DEPTH_TEST); By the way, it is enough to call once. if you do not turn it on / off in the frame drawing process. - Nofate
    • Thank. Indeed, you can once. I already used the glEnable function (in order to put a texture). And what to do with the flicker? - angry
    • and you show that you can find the problem in ForegroundUtil.paint and drawSquareFace . - Nofate
    • @Nofate, I opened a new question (about flicker). - angry

    this you need a depth buffer to enable

    • But how to do it? Is there an example? - angry