简体   繁体   English

OpenGL缺少三角形的漫反射着色器

[英]OpenGL Missing Triangles Diffuse Shader

I'm using C++ and when I implemented a diffuse shader, it causes every other triangle to disappear. 我正在使用C ++,当我实现了漫反射着色器时,它导致其他所有三角形消失。 立方体OBJ

圆环OBJ

I can post my render code if need be, but I believe the issue is with my normal matrix (which I wrote it to be the transpose inverse of the model view matrix). 我可以根据需要发布渲染代码,但是我认为问题出在我的普通矩阵(我将其写为模型视图矩阵的转置逆矩阵)。 Here is the shader code which is sort of similar to that of a tutorial on lighthouse tutorials. 这是着色器代码,有点类似于灯塔教程中的教程。 VERTEX SHADER 顶点着色器

#version 330

layout(location=0) in vec3 position;
layout(location=1) in vec3 normal;

uniform mat4 transform_matrix;
uniform mat4 view_model_matrix;
uniform mat4 normal_matrix;
uniform vec3 light_pos;

out vec3 light_intensity;

void main()
{
    vec3 tnorm = normalize(normal_matrix * vec4(normal, 1.0)).xyz;
    vec4 eye_coords = transform_matrix * vec4(position, 1.0);
    vec3 s = normalize(vec3(light_pos - eye_coords.xyz)).xyz;

    vec3 light_set_intensity = vec3(1.0, 1.0, 1.0);
    vec3 diffuse_color = vec3(0.5, 0.5, 0.5);

    light_intensity = light_set_intensity * diffuse_color * max(dot(s, tnorm), 0.0);
    gl_Position = transform_matrix * vec4(position, 1.0);
}

My fragment shader just outputs the "light_intensity" in the form of a color. 我的片段着色器仅以颜色形式输出“ light_intensity”。 My model is straight from Blender and I have tried different exporting options like keeping vertex order, but nothing has worked. 我的模型直接来自Blender,我尝试了不同的导出选项,例如保持顶点顺序,但没有任何效果。

This is not related to you shader. 这与您的着色器无关。

It appears to be depth test related. 它似乎与深度测试有关。 Here, the order of triangles in the depth relative to you viewport is messed up, because you do not make sure that only the nearest pixel to your camera gets drawn. 在这里,相对于视口的深度中的三角形顺序被弄乱了,因为您不能确保仅绘制与相机最近的像素。

Enable depth testing and make sure you have az buffer bound to your render target. 启用深度测试,并确保将z缓冲区绑定到渲染目标。 Read more about this here: http://www.opengl.org/wiki/Depth_Test 在此处详细了解此内容: http : //www.opengl.org/wiki/Depth_Test

深度测试错误的立方体的三角形。

Only the triangles highlighted in red should be visible to the viewer. 观看者只能看到以红色突出显示的三角形。 Due to the lack of a valid depth test, there is no chance to guarantee, what triangle is painted top most. 由于缺少有效的深度测试,因此没有机会保证最顶部绘制的三角形。 Thus blue triangles of the faces that should not be visible will cover parts of previously drawn red triangles. 因此,不可见的面的蓝色三角形将覆盖先前绘制的红色三角形的一部分。

The depth test would omit this, by comparing the depth in the z buffer with the depth of the pixel to be drawn at the current moment. 通过将z缓冲区中的深度与当前时刻要绘制的像素的深度进行比较,深度测试将忽略这一点。 Only the color information of a pixel that is closer to the viewer, ie has a smaller z value than the z value in the buffer, shall be written to the framebuffer in order to achieve a correct result. 为了获得正确的结果,只有像素的颜色信息才被写入帧缓冲区,该像素的颜色信息离观看者更近,即z值小于缓冲区中的z值。

(Backface culling, would be nice too and, if the model is correctly exported, also allow to show it correctly. But it would only hide the main problem, not solve it.) (背面剔除也很好,如果正确导出了模型,也可以正确显示它。但是它只会隐藏主要问题,而不能解决。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM