OpenGL: mapping images to textures

Started by
0 comments, last by Promit 9 years, 1 month ago

I am very new to OpenGL and am reading about mapping images to textures and came across this API with the following signature:


void glTexImage2D(GLenum target,  GLint level,  GLint internalformat,
                  GLsizei width,  GLsizei height,  GLint border,  
                  GLenum format,  GLenum type,  const GLvoid * data);

Now, I have read many places that graphics hardware works better if things are aligned to 4 byte boundaries. So, I was wondering about the following case where I have a RGB image. So, the format parameter should be GL_RGB. However, to take advantage of this 4 byte alignment I want to keep it internally as GL_RGBA (where the alpha channel is not really used).

Reading the API, I got a bit confused as to whether I need to internally change my image to have this RGBA structure or would just setting the internal format to GL_RGBA and format to GL_RGB suffice?

Another question that I wonder is what if I have grayscale data i.e. just a single channel. Would it also be beneficial to convert that to RGBA structure at the cost of more memory or would I not get a performance hit by keeping it as GL_RED, for example.

Thanks,

Luca

Advertisement

Reading the API, I got a bit confused as to whether I need to internally change my image to have this RGBA structure or would just setting the internal format to GL_RGBA and format to GL_RGB suffice?

Yes - in fact some (most?) hardware will actually do best with GL_BGRA. But since this tends to be a guessing game and depends somewhat on the actual hardware in question, I prefer to simply send RGB and hope the driver sorts it out. Sometimes it'll quietly change it internally and as long as you don't try to read back, no big deal. But if you are more comfortable aligning it, use BGRA.


Another question that I wonder is what if I have grayscale data i.e. just a single channel. Would it also be beneficial to convert that to RGBA structure at the cost of more memory or would I not get a performance hit by keeping it as GL_RED, for example.

Nah, I prefer to just leave it as-is. The extra bandwidth costs you more than what alignment potentially gains, and the drivers are well versed in handling this.

SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.

This topic is closed to new replies.

Advertisement