着色器将 uint8 转换为浮动,并将其重新解释为 uint

Ann*_*inn 5 c++ opengl shader glsl

我有一个顶点属性被我的着色器非常奇怪地咀嚼。它作为 a 上传到 VBO,(uint8)1但是当片段着色器看到它时,它被解释为 a 10653532160,或者0x3F800000你们中的一些人可能认为它是 a 1.0fin 浮点的位模式。

我不知道为什么?我可以确认它是作为 1 ( 0x00000001)上传到 VBO 的。

顶点属性定义为:

struct Vertex{
    ...
    glm::u8vec4 c2; // attribute with problems
};
// not-normalized
glVertexAttribPointer(aColor2, 4, GL_UNSIGNED_BYTE, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, c2));
Run Code Online (Sandbox Code Playgroud)

虽然着色器具有绑定的属性

glBindAttribLocation(programID, aColor2, "c2");
Run Code Online (Sandbox Code Playgroud)

顶点着色器非常顺利地传递属性:

#version 330
in lowp uvec4 c2; // <-- this value is uploaded to the VBO as 0x00, 0x00, 0x00, 0x01;
flat out lowp uvec4 indices;

void main(){
    indices = c2;
}
Run Code Online (Sandbox Code Playgroud)

最后片段着色器得到了它:

flat in lowp uvec4 indices; // <-- this value is now 0, 0, 0, 0x3F800000
out lowp vec4 fragColor;
void main(){
    fragColor = vec4(indices) / 256.0;
}
Run Code Online (Sandbox Code Playgroud)

indices不同的叶子顶点着色器作为0x3F800000用于indices.w根据我的着色检查,使一些奇怪的正在发生的事情吗?什么可能导致这种情况?

Rab*_*d76 3

如果顶点属性的类型是整型的,那么你必须使用glVertexAttribIPointer而不是glVertexAttribPointer(重点关注I)。看glVertexAttribPointer

中指定的类型glVertexAttribPointer是源缓冲区中数据的类型,并没有指定着色器中的目标属性类型。如果使用glVertexAttribPointer,则着色器程序中的属性类型被假定为浮点型,并且转换为整数数据。
如果您使用glVertexAttribIPointer,那么值将保留为整数值。