Does the depth of the objects justify the draw order? (including images)

I have several objects on the scene, and even if I point out that object A has y = 10 (the highest object), from the TOP camera I can see the lower objects through object A. Here is an image from my scene.

enter image description here

And only today I found an interesting property that affects the order of models, maybe I'm wrong. Here is another image in which I change the drawing order of "ship1", note: "ship1" is the path below my scene if I ship1.draw(); , the ship disappears (correctly), but if I do ship1.draw(); in the latter, it appears from above (incorrectly).

enter image description here

Video : Opengl Inaccessibility Video

  • Q1) Does it always have a drawing order?
  • Q2) How can I fix this, should I change the drawing order every time I change the camera position?

Change I also compared the Perspective Projection class with the glm library to make sure this is not a problem with my design matrix. All is correct.

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

Edit2 . I do not use normals or textures. Just the tops and indexes.

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

Edit4 . I also changed my predicted projection values. I had about a plane at 0.0f, now I have about = 20.0f and far = 500.0f, angle = 60ΒΊ. But nothing changes, but the view is not deeper. = /

Edit5 . Here are my Vertex and Fragment shaders.

Edit6 : contact me anytime, I'm here all day, so ask me. At the moment I am rewriting the entire project from scratch. I have two cubes that look good, one opposite the other. My class has already been added for: camera, projection, shader handler. Moving to a class that 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 codes:

  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); } 
+5
source share
1 answer

I cloned the repository you linked to find out if the problem is located elsewhere. In your latest 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, which means that the next drawn object is not closed properly. You should only clear the depth buffer once.

+7
source

All Articles