绘制顺序是否会影响对象的深度位置? (含图片) [英] Does the draw order affects objects position in depth? (images included)

查看:75
本文介绍了绘制顺序是否会影响对象的深度位置? (含图片)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在场景中有几个对象,即使我指定对象A的y = 10(最高的对象),也可以从TOP摄像机中看到通过对象A的底部对象.我的场景.

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.

直到今天,我才发现一个有趣的属性,该属性绘制模型的顺序很重要,我可能是错的.这是另一个图像,其中更改了"ship1"的绘制顺序,请注意:"ship1"是场景的下方,如果首先执行ship1.draw();,则飞船消失(正确),但是如果最后执行ship1.draw(); ,他出现在顶部(不正确).

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

视频: Opengl深度问题视频

  • Q1)抽签顺序是否总是很重要?
  • Q2)如何解决此问题,每次更改相机位置时是否应该更改绘制顺序?

编辑:我还将我的透视投影"类与glm库进行了比较,以确保这不是我的投影矩阵的问题.一切都正确.

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 :我在git上有我的项目: Arkanoid git存储库(Windows,项目已准备就绪,可以在安装了VS的任何计算机上运行)

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

Edit2 :我不使用法线或纹理.只是顶点和索引.

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

Edit3 :是否存在问题,如果场景中的每个对象都使用(共享)同一文件中的顶点?

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

Edit4 :我还更改了透视投影"值.我在0.0f处有近平面,现在我有near = 20.0f和far = 500.0f,角度=60º.但是什么都没有改变,但视图没有改变,但深度没有改变. =/

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 :这是我的顶点和片段"着色器.

Edit5: Here is my Vertex and Fragment shaders.

Edit6 :随时与我联系,我整天在这里,所以问我什么.目前,我正在从零重写所有项目.我有两个渲染效果很好的立方体,一个在另一个的前面.已经为以下项添加了我的类:相机,投影,着色器处理程序.转到创建和绘制对象的类.

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

一些代码:

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

推荐答案

我已经克隆了您链接的存储库,以查看问题是否在其他地方.在您的最新版本中,Object3D :: draw函数如下所示:

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.

这篇关于绘制顺序是否会影响对象的深度位置? (含图片)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆