GLSL将uint转换为浮点颜色
对于我的程序,我希望将数据作为无符号字节传递到顶点着色器,然后将这些字节转换为浮点值(介于0.0和1.0之间),用于顶点着色器中的颜色,如下所示:
#version 330 core
layout (location = 0) in uvec3 aPos;
layout (location = 1) in uvec4 aColor;
out VS_OUT {
vec4 color;
} vs_out;
void main()
{
vs_out.color = vec4(float(aColor.r) / 255.0f, float(aColor.g) / 255.0f, float(aColor.b) / 255.0f, 1.0f); //alpha set to 1 temporarily
gl_Position = vec4(aPos, 1.0);
}
但是,似乎无论我如何处理构造函数或操作,当输入的字节值不是0(0表示0.0)时,它总是会转换为1.0。如何修复此问题,以便获得介于0.0和1.0之间的正确颜色值?(或者这是OpenGL 3.3的错误?)
要传入数据的代码:
unsigned char test[] = {
0, 0, 0, 255, 1, 10, 255, 0, 255 //first 3 is position and next 4 is color but still outputs white
};
unsigned int VBO, VAO;
glGenBuffers(1, &VBO);
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(test), &test, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_UNSIGNED_BYTE, GL_FALSE, 9*sizeof(byte), 0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, GL_FALSE, 9 * sizeof(byte), (void*)(3 * sizeof(BYTE)));
glBindVertexArray(0);
...
// draw points
ourShader.use();// use pre-defined shaders
glBindVertexArray(VAO);
glDrawArrays(GL_POINTS, 0, 1);
解决方案
我想出来了。我只需将着色器中的in值设置为Vector 4,而不是uve4,并将vertex属性设置为Normatize,程序将自动转换它:
顶点着色器:
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec4 aColor;
out VS_OUT {
vec4 color;
} vs_out;
void main()
{
vs_out.color = aColor;
gl_Position = vec4(aPos, 1.0);
}
程序代码:
byte test[] = {//unsigned char
0, 0, 0, 25, 100, 136, 255, 0, 255 // ouputs teal color (index 3-6)
};
...
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, GL_TRUE, (9 * sizeof(byte)), (void*)(3 * sizeof(BYTE)));
相关文章