Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I render RGBA4444 image in OpenGL?

I try to render my image in RGBA4444 without converting to RGBA8888,but….

// 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());

MapleStory

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

By default OpenGL assumes that the start of each row of an image is aligned to 4 bytes. This is because the GL_UNPACK_ALIGNMENT parameter by default is 4. Since a pixel of an image with the format GL_UNSIGNED_SHORT_4_4_4_4_REV only needs 2 bytes, the size of a row of the image may not be aligned to 4 bytes.
When the image is loaded to a texture object and 2*image.width is not divisible by 4, GL_UNPACK_ALIGNMENT has to be set to 2, before specifying the texture image with 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());
             
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading