Talk:Using wxImage to load textures for OpenGL

From WxWiki

Jump to: navigation, search

I think that error is in the example. The command glTexImage2D(GL_TEXTURE_2D, 0, img->HasAlpha() ? 4 : 3, *imageWidth, *imageHeight, 0, img->HasAlpha() ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE, img->GetData()); will not work for images with alpha, because img->GetData() returns always only RGB data.
From my point of view the method GetData() should return RAW data, it means 1 byte per pixel for greyscale, 3 bytes for RGB and 4 bytes for RGBA, 6 bytes for 16bit per channel RGB (not supported now), etc.
TonyMi

using img->getData() and feeding that directly to OpenGL does work, though I wouldn't be surprised if OpenGL is realising you haven't passed it alpha values and is compensating automatically, since you're right and wxImage's getData function sends back RGB data, not RGBA.

This code does have an odd problem however: when an image is loaded that is non-power-of-two dimensions, it's loaded fine. When it is power-of-two dimensions, the image will be flipped - upside-down, y-flipped. There's a line in the non-power-of-two part of the code, int rev_val=(*imageHeight)-1;, which sets up a variable which is used to compensate for this. There's no such value in the power-of-two case. There's no way to tell OpenGL to compensate for this. I've found that replacing that chunk of the code with a modified version of the non-power-of-two case works reasonably well. Makes everything come out the way you'd expect it to, plus it has the alpha channel passed correctly.

---NegativeZero

Forgot to mention, if you're compiling this example in Linux you will need to rename the 'Drawable' class as the OpenGL libraries also include X11 headers, and X11 also defines a 'Drawable' class. So you'll need to either change the name or put it in a separate namespace.

---NegativeZero

Personal tools