【问题标题】:Geometry shader calculated lines disappear on camera move几何着色器计算的线在相机移动时消失
【发布时间】:2016-05-18 21:25:25
【问题描述】:

我使用几何着色器绘制顶点法线。一切都按预期显示,除了当我移动相机时,有些线条会部分消失。首先,我认为这是由于截头锥体的大小,但场景中的其他对象比这个更大。

Before movement

After movement

如果有人能告诉我如何摆脱这种线消失的影响,我将不胜感激。

下面是我的几何着色器的代码

#version 330 core


layout (triangles) in;
layout (line_strip, max_vertices = 6) out;

in Data{
    vec4 position;
    vec4 t_position;
    vec4 normal;
    vec2 texCoord;
    vec4 color;
    mat4 mvp;
    mat4 view;
    mat4 mv;
} received[];


out Data{
   vec4 color;
   vec2 uv;
   vec4 normal;
   vec2 texCoord;
   mat4 view;
} gdata;


const float MAGNITUDE = 1.5f;

void GenerateLine(int index) {
    const vec4 green = vec4(0.0f, 1.0f, 0.0f, 1.0f);
    const vec4 blue = vec4(0.0f, 0.0f, 1.0f, 1.0f);

    gl_Position = received[index].t_position;
    //gdata.color = received[index].color;
    gdata.color = green;
    EmitVertex();

    gl_Position = received[index].t_position + received[index].normal * MAGNITUDE;
    //gdata.color = received[index].color;
    gdata.color = blue;
    EmitVertex();

    EndPrimitive();
}

void main() {
    GenerateLine(0); // First vertex normal
    GenerateLine(1); // Second vertex normal
    GenerateLine(2); // Third vertex normal
}

顶点着色器

#version 330


layout(location = 0) in vec3 Position;
layout(location = 1) in vec2 TexCoord;
layout(location = 2) in vec3 Normal;

out Data{
    vec4 position;
    vec4 t_position;
    vec4 normal;
    vec2 texCoord;
    vec4 color;
    mat4 mvp;
    mat4 view;
    mat4 mv;
} vdata;

//MVP
uniform mat4 model;
uniform mat4 projection;
uniform mat4 view;

void main() {
    vdata.position = vec4(Position, 1.0f);
    vdata.normal = view * model * vec4(Normal, 0.0);
    vdata.texCoord = TexCoord;

    vdata.view = view;

    vec4 modelColor = vec4(0.8f, 0.8f, 0.8f, 1.0f);
    vdata.color = modelColor;

    vdata.mvp = projection * view * model;
    vdata.mv = view * model;
    vdata.t_position = vdata.mvp * vdata.position;

    gl_Position = vdata.t_position;
};

【问题讨论】:

  • 绝对看起来像深度缓冲区剪辑。
  • 我是 OpenGL 新手。你会建议如何解决它?
  • @Illia 您可能需要在顶点着色器中添加一个投影矩阵,因为您现在似乎缺少一个。
  • @Xirema 感谢您的评论,但我的顶点着色器中确实有 proj 矩阵。我刚刚添加了我的顶点着色器的代码。还是您的意思不同?
  • @Illia:为什么要从顶点着色器输出 矩阵?为什么不让它们成为 GS 可以访问的制服

标签: c++ opengl geometry-shader


【解决方案1】:

参考 Illia 2016 年 5 月 18 日 22:53 的回答

我最初的 m_zNear 等于 0.1,但当将其切换到 1.0 时,线条停止消失。我不完全确定为什么会这样。有知道的请分享一下

消失的线是关于深度缓冲区裁剪的。顶点是projected(乘以MVP-matrix),然后在几何着色器(GS)中的投影之后更改顶点位置。 GS 中的这些变化导致 z 值在透视分割中落在normalized device coordinates (NDC) 之外。 zNear 值越大,投影的 z 值就越小,因此它不会落在 NDC 之外。尽管如果 GS 中 MAGNITUDE 的值足够大,那么即使使用更大的 zNear,线条也会被剪裁。解决此问题的一种方法是在 GS 中进行投影。

【讨论】:

    【解决方案2】:

    如果其他人遇到此问题,解决方案如下:

    glm::perspective(45.0f, m_aspectRatio, m_zNear, m_zFar);
    

    我最初的 m_zNear 等于 0.1,但当将其切换到 1.0 时,线条停止消失。我不完全确定为什么会这样。有知道的请分享一下。

    【讨论】:

      【解决方案3】:

      您是否尝试过用谷歌搜索 zFar zNear? Here you go.

      或者至少尝试用谷歌搜索那个神奇的 glm::perspective(...) 函数?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-16
        • 1970-01-01
        • 2017-09-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多