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

折月煮酒 提交于 2020-02-03 05:29:04

问题


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.

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).

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);
}

回答1:


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.



来源:https://stackoverflow.com/questions/31091314/does-the-draw-order-affects-objects-position-in-depth-images-included

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!