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.

//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?

1 Answer 1

  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.
  • \$\begingroup\$ "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. \$\endgroup\$ Commented Mar 8, 2013 at 23:22
  • \$\begingroup\$ 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) \$\endgroup\$
    – Azmisov
    Commented Mar 9, 2013 at 0:44
  • \$\begingroup\$ 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? \$\endgroup\$ Commented Mar 9, 2013 at 0:50
  • \$\begingroup\$ 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)? \$\endgroup\$
    – Azmisov
    Commented Mar 9, 2013 at 23:11
  • \$\begingroup\$ @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). \$\endgroup\$ Commented Mar 11, 2013 at 0:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .