Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am just starting out with OpenGL, so I need a little help understanding how textures are loaded. When I look at the OpenGL manual, it gives a list of accepted image formats, e.g.

GL_UNSIGNED_BYTE_3_3_2
GL_UNSIGNED_SHORT_5_6_5
GL_UNSIGNED_SHORT_5_5_5_1
GL_UNSIGNED_INT_10_10_10_2
//and a few others...
  • Can I have a RGB texture with a GL_UNSIGNED_SHORT_8_8_8_8, or do I have to pad the image so that it is RGBA (or maybe it doesn't matter either way...)?
  • What is the purpose of all the other internal formats (GL_R16 or GL_RGBA32I) if there is no corresponding image format type (like the ones listed above) to load that type of texture?
  • Oh, and one other question. Is there a maximum texture size? Or I guess, if there is no maximum size, is there an optimal size for how large textures can be?
share|improve this question

1 Answer 1

up vote 2 down vote accepted
  1. Yes. You specify the image format separately from the internal image format and OpenGL does the conversion for you. If your image data is an array of unsigned shorts with RGB values and you want to store it internally as unsigned shorts RGBA, you would use the following parameters for glTeximage2D: glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16, width, height, 0, GL_RGB, GL_UNSIGNED_SHORT, data). GL_RGBA16 means to internally store it as RGBA components of 16 bits each (size of a short), GL_RGB means you're passing in data for red, green, and blue channels in that order, GL_UNSIGNED_SHORT means the data you're passing in is an array of unsigned shorts.
  2. OpenGL can internally store textures with a number of different formats, regardless of how you pass the data in. You can pass in an RGB image and internally store it as GL_R16 and it will store only the red channel with 16 bits per pixel.
  3. You can get an estimate of maximum texture size with glGetIntegerv(GL_MAX_TEXTURE_SIZE, &size), but to get maximum supported size you have to use proxy textures as described in the OpenGL FAQ. In terms of optimal texture size, try and keep your textures as powers of two and under the maximum texture size.
share|improve this answer
    
"GL_RGB means you're passing in data for red, green, and blue channels" In that order. GL_BGR means blue, green, and red, in that order. –  Nicol Bolas Mar 8 '13 at 23:22
    
Just to clarify, can I use a sized type with four channels, but use GL_RGB? e.g. glTexImage2D(-,-,-,-,-,-,GL_RGB, GL_UNSIGNED_SHORT_4_4_4_4, data) –  Azmisov Mar 9 '13 at 0:44
    
No - the format and type parameters to glTexImage (and related) are connected, such that the format describes how the type is to be interpreted in terms of colour channels. If you have a type that uses 4 channels then the format must be something that makes sense for it. These have absolutely nothing to do with how the texture exists on the GPU - they just describe how the source data (which you're going to free immediately after creating the texture anyway) is laid out. Why the obsession with "GL_RGB" anyway? You're not falling into the old "wasting memory" nonsense are you? –  Jimmy Shelter Mar 9 '13 at 0:50
    
I'm just wondering about loading the texture. I have a .png image that is 24bit. So, I assume its RGB, 8_8_8, but maybe its stored as an array of shorts anyways (32bit)? –  Azmisov Mar 9 '13 at 23:11
    
@Azmisov: That would not be an array of shorts if it's 24 bit. A short is 16 bits. What you're going to get out of a .png is either 24bpp or 32bpp. GL_RGB can work with GL_UNSIGNED_BYTE (every 3 bytes is a component) for 24bpp. If it comes out as 32bpp, with an empty 4th component, you have to upload it as GL_RGBA (or more likely, GL_BGRA). –  Nicol Bolas Mar 11 '13 at 0:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.