Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the draw order affects objects position in depth? (images included)

I have a few objects on the scene and even if I specify that the object A have y= 10 (the highest object), from the TOP camera I can see the bottom objects through the object A. Here is an image from my scene.

enter image description here

And only today I found an interesting property that draw order of models matters, I may be wrong. Here is another image where I change the draw order of "ship1", attention: "ship1" is way bellow my scene, if I do ship1.draw(); first, the ship disappears (correct), but if I do ship1.draw(); in last, he appears on top (incorrect).

enter image description here

Video: Opengl Depth Problem video

  • Q1) Does the draw order always matter?
  • Q2) How do I fix this, should I change draw order every time I change camera position?

Edit: I also compared my class of Perspective Projection with the glm library, just to be sure that it´s not the problem with my projection Matrix. Everything is correct.

Edit1: I have my project on git: Arkanoid git repository (windows, project is ready to run at any computer with VS installed)

Edit2: I don´t use normals or texture. Just vertices and indices.

Edit3: Is there a problem, if every object on the scene uses(share) vertices from the same file ?

Edit4: I also changed my Perspective Projection values. I had near plane at 0.0f, now I have near=20.0f and far=500.0f, angle=60º. But nothing changes, view does but the depth not. =/

Edit5: Here is my Vertex and Fragment shaders.

Edit6: contact me any time, I am here all day, so ask me anything. At the moment I am rewriting all project from zero. I have two cubes which renders well, one in front of another. Already added mine class for: camera, projections, handler for shaders. Moving to Class which creates and draws objects.

// Vertex shader

in   vec4 in_Position;
    out vec4 color;
    uniform mat4 Model;
    uniform mat4 View;
    uniform mat4 Projection;

    void main(void)
    {
        color = in_Position;
        gl_Position =   Projection * View    * Model * in_Position;
    }

// Fragment shader

       #version 330 core
    in   vec4 color;
    out vec4 out_Color;
    void main(void)
    {
        out_Color = color;
    }

Some code:

 void setupOpenGL() {
    std::cerr << "CONTEXT: OpenGL v" << glGetString(GL_VERSION) << std::endl;
    glClearColor(0.1f, 0.1f, 0.1f, 1.0f);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glDepthMask(GL_TRUE);
    glDepthRange(0.0, 1.0);
    glClearDepth(1.0);
    glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);
    glFrontFace(GL_CCW);
}

    void display()
{
    ++FrameCount;
    glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    renderScene();
    glutSwapBuffers();
}
void renderScene()
{
    wallNorth.draw(shader);
    obstacle1.draw(shader);
    wallEast.draw(shader);
    wallWest.draw(shader);
    ship1.draw(shader);
    plane.draw(shader);
}
like image 337
Volodymyr Balytskyy Avatar asked Jun 27 '15 17:06

Volodymyr Balytskyy


1 Answers

I have cloned the repository you have linked to see if the issue was located somewhere else. In your most recent version the Object3D::draw function looks like this:

    glBindVertexArray(this->vaoID);

    glUseProgram(shader.getProgramID());
    glUniformMatrix4fv(this->currentshader.getUniformID_Model(), 1, GL_TRUE, this->currentMatrix.getMatrix());  // PPmat é matriz identidade
    glDrawElements(GL_TRIANGLES, 40, GL_UNSIGNED_INT, (GLvoid*)0);
    glBindVertexArray(0);
    glUseProgram(0);

    glClear( GL_DEPTH_BUFFER_BIT); <<< clears the current depth buffer.

The last line clears the depth buffer after each object that is drawn, meaning that the next object drawn is not occluded properly. You should only clear the depth buffer once every frame.

like image 129
Maurice Avatar answered Oct 16 '22 03:10

Maurice