GLSL将uint转换为浮点颜色

GLSL converting uint to float for color(GLSL将uint转换为浮点颜色)
本文介绍了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)));

这篇关于GLSL将uint转换为浮点颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

error when trying to run an overloaded function with float parameters.(尝试运行带有浮点参数的重载函数时出错。)
C++ lt;lt; operator overloading without friend function(没有友元函数的C++lt;lt;运算符重载)
C++ - How does the compiler decide between overloaded functions with reference types as parameter?(C++-编译器如何决定使用引用类型作为参数的重载函数?)
How can I invoke an overloaded () operator on the keyword this?(如何对关键字this调用重载()运算符?)
How do I denote a pure virtual function in a UML class diagram?(如何在UML类图中表示纯虚函数?)
MPI parallel IO in ASCII format (How do I do it?)(ASCII格式的MPI并行IO(我该怎么做?))