我的着色器有一个顶点属性,它的处理方式非常奇怪。它以 (uint8)1
的形式上传到 VBO,但当片段着色器看到它时,它会被解释为 10653532160
或 0x3F800000
,其中一些您可能会认为这是浮点 1.0f
的位模式。
我不知道为什么?不过,我可以确认它已作为 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));
虽然着色器具有绑定(bind)该属性
glBindAttribLocation(programID, aColor2, "c2");
顶点着色器非常顺利地传递属性:
#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;
}
最后片段着色器得到它:
flat in lowp uvec4 indices; // <-- this value is now 0, 0, 0, 0x3F800000
out lowp vec4 fragColor;
void main(){
fragColor = vec4(indices) / 256.0;
}
根据我的着色器检查器,indices
的变化使顶点着色器成为 indices.w
的 0x3F800000
,所以那里发生了一些奇怪的事情?可能是什么原因造成的?
最佳答案
如果顶点属性的类型是整数,那么您必须使用glVertexAttribIPointer
而不是glVertexAttribPointer
(重点关注I
)。请参阅glVertexAttribPointer
.
glVertexAttribPointer
中指定的类型是源缓冲区中数据的类型,并没有指定着色器中的目标属性类型。如果使用glVertexAttribPointer
,则着色器程序中属性的类型被假定为浮点型,并转换为整数数据。
如果您使用glVertexAttribIPointer,则值保留为整数值。
关于c++ - 着色器将 uint8 转换为 float ,并将其重新解释回 uint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58898963/