如何在 OpenGL 中渲染 RGBA4444 图像?

How can I render RGBA4444 image in OpenGL?

我尝试在不转换为 RGBA8888 的情况下以 RGBA4444 渲染我的图像,但是....

// Define vertices
float vertices[] = {-1.f, 1.f,  0.f, 0.f, 1.f,   // left top
                    1.f,  1.f,  0.f, 1.f, 1.f,   // right top
                    -1.f, -1.f, 0.f, 0.f, 0.f,   // left bottom
                    1.f,  -1.f, 0.f, 1.f, 0.f};  // right bottom

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA4, image.width, image.height, 0, GL_BGRA,
             GL_UNSIGNED_SHORT_4_4_4_4_REV, image.pixels.data());

默认情况下,OpenGL 假设图像的每一行的开始对齐到 4 个字节。这是因为GL_UNPACK_ALIGNMENT参数默认为4。由于格式为GL_UNSIGNED_SHORT_4_4_4_4_REV的图像的一个像素只需要2个字节,所以图像的一行大小可能不会对齐到4个字节。
当图像加载到纹理对象并且 2*image.width 不能被 4 整除时,必须将 GL_UNPACK_ALIGNMENT 设置为 2,然后再使用 glTexImage2D:

指定纹理图像
glPixelStorei(GL_UNPACK_ALIGNMENT, 2);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA4, image.width, image.height, 0, GL_BGRA,
               GL_UNSIGNED_SHORT_4_4_4_4_REV, image.pixels.data());