【发布时间】:2024-01-15 18:07:02
【问题描述】:
我在顶点或片段着色器的某个地方有一个非常令人困惑的错误。当我不主动使用片段着色器中的块接口时,渲染工作。但是当我使用它时,会出现错误 1281。下面你会看到工作的顶点和片段着色器。该错误在代码下方讨论。
顶点着色器:
#version 430
in layout (location = 0) vec3 position;
in layout (location = 1) vec4 color;
in layout (location = 2) vec3 normal;
in layout (location = 3) vec2 uv;
in layout (location = 4) vec3 tangent;
in layout (location = 5) int materialId;
uniform mat4 pr_matrix;
uniform mat4 vw_matrix = mat4(1.0);
uniform mat4 ml_matrix = mat4(1.0);
out VS_OUT {
vec4 color;
vec2 texture_coordinates;
vec3 normal;
vec3 tangent;
vec3 binormal;
vec3 worldPos;
int materialIdOut;
} vs_out;
out vec4 colorOut;
void main()
{
vs_out.color = color;
vs_out.texture_coordinates = uv;
mat3 normalMatrix = transpose ( inverse ( mat3 ( ml_matrix )));
vs_out.normal = normalize ( normalMatrix * normalize ( normal ));
gl_Position = ( pr_matrix * vw_matrix * ml_matrix ) * vec4 ( position, 1.0);
colorOut = vec4(vs_out.normal,1.0);
}
工作片段着色器(正常用作颜色):
#version 430
uniform vec3 cameraPos;
uniform mat4 ml_matrix;
uniform mat4 vw_matrix;
uniform sampler2D texSlots[32];
in VS_OUT {
vec4 color;
vec2 texture_coordinates;
vec3 normal;
vec3 tangent;
vec3 binormal;
vec3 worldPos;
int materialIdOut;
} fs_in;
out vec4 gl_FragColor;
in vec4 colorOut;
void main()
{
gl_FragColor = colorOut;
}
如果我尝试在片段着色器中使用块接口,例如 gl_FragColor = fs_in.color; 要么 gl_FragColor = vec4(fs_in.normal,1.0);
发生错误 1281。我真的不明白为什么这不起作用。
编辑:解决方案:
那里实际上有两个问题: 1) int 被解释为 float 从应用程序中,我通过使用将材质属性发送到着色器 glVertexAttribPointer(SHADER_MATERIAL_INDEX, 1, GL_INT, ...);这导致了问题,该属性在着色器中被解释为浮点数 通过使用 glVertexAttribIPointerEXT(SHADER_MATERIAL_INDEX, 1, GL_INT, ...);我们可以克服这个问题 2)程序链接不成功“错误c5215整数变化必须是平坦的”,导致顶点和片段着色器发生变化。
out VS_OUT {
vec4 color;
vec2 texture_coordinates;
vec3 normal;
vec3 tangent;
vec3 binormal;
vec3 worldPos;
flat int materialIdOut;
} vs_out;
和
in VS_OUT {
vec4 color;
vec2 texture_coordinates;
vec3 normal;
vec3 tangent;
vec3 binormal;
vec3 worldPos;
flat int materialIdOut;
} fs_in;
现在一切正常
【问题讨论】:
-
您在哪里得到错误 1281 (GL_INVALID_VALUE)?
-
vs_out 是如何连接到 fs_in 的?
-
@AdrianKrupa:如果我要替换 gl_FragColor = colorOut;通过 gl_FragColor = fs_in.color;发生错误。
-
@immibis:着色器链接在同一个程序中。这些块具有相同的名称和完全相同的成员(排序、类型和命名)。因此,据我所知,应该自动链接
-
glGetShaderInfoLog/glProgramInfoLog 说什么?顺便说一句:这些块没有相同的名称。一个叫 vs_out,另一个叫 fs_in。
标签: c++ opengl fragment-shader vertex-shader