如何使用GL_RGB9_E5格式?

时间:2019-04-15 15:40:30

标签: c++ opengl opengl-es opengl-4

我正在尝试将 GL_RGB9_E5 格式用于3D纹理。为此,我创建了一个简单的测试来了解格式的用法。不知何故我没有得到我所期望的。以下是测试程序

GLuint texture;
const unsigned int depth = 1;
const unsigned int width = 7;
const unsigned int height = 3;

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_3D, texture);
const float color[3] = { 0.0f, 0.5f, 0.0f };
const rgb9e5 uColor = float3_to_rgb9e5(color);

GLuint * t1 = (GLuint*)malloc(width*height*depth* sizeof(GLuint));
GLuint * t2 = (GLuint*)malloc(width*height*depth* sizeof(GLuint));
int index = 0;
for (int i = 0; i < depth; i++)
    for (int j = 0; j < width; j++)
        for (int k = 0; k < height; k++)
        {
            t1[index] = uColor.raw;
            index++;
        }

glTexImage3D(GL_TEXTURE_3D, 0,
    GL_RGB9_E5,
    width, height, depth, 0,
    GL_RGB,
    GL_UNSIGNED_INT,
    (void*)t1);

glGetTexImage(GL_TEXTURE_3D, 0, GL_RGB, GL_UNSIGNED_INT_5_9_9_9_REV, t2);

index = 0;
for (int i = 0; i < depth; i++)
    for (int j = 0; j < width; j++)
        for (int k = 0; k < height; k++)
        {
            rgb9e5 t;
            t.raw = t2[index];
            index++;
        }

我期望的是 uColor.raw 我正在输入的 t1 我在 t2 中恢复了相同的原色。

我从Specification for GL_RGB9_E5开始采用了 float3_to_rgb9e5 方法 在底部可以找到代码。

如果有人可以给我提供有关如何正确使用它或正确使用方法的示例,那就太好了。

1 个答案:

答案 0 :(得分:6)

    GL_RGB,
    GL_UNSIGNED_INT,

这意味着您正在传递3通道像素数据,其中每个通道都是规范化的32位无符号整数。但这不是您的实际目的。

您实际上正在传递3通道数据,该数据打包成一个32位无符号整数,因此前5位代表浮点指数,其后是3组9位,每组代表无符号尾数浮游物。我们拼写为:

GL_RGB,
GL_UNSIGNED_INT_5_9_9_9_REV