I've recently been learning wxWidgets (version 2.9.4) and have been having trouble getting textures to work correctly in an OpenGL window (glColor4f works fine however).
I've tried following the tutorials from wxWidget including loading textures from a wxImage. I also tried using stbi_image to load in the texture but no matter what I do the textures always remain white. Here's the code I'm using:
wxImage* img = new wxImage(wxT("grass.png"));
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
GLubyte *bitmapData=img->GetData();
GLubyte *alphaData=img->GetAlpha();
int bytesPerPixel = img->HasAlpha() ? 4 : 3;
int imageWidth = img->GetWidth();
int imageHeight = img->GetHeight();
int imageSize = imageWidth * imageHeight * bytesPerPixel;
GLubyte *imageData=new GLubyte[imageSize];
int rev_val=imageHeight-1;
for(int y=0; y<imageHeight; y++)
{
for(int x=0; x<imageWidth; x++)
{
imageData[(x+y*imageWidth)*bytesPerPixel+0]=
bitmapData[( x+(rev_val-y)*imageWidth)*3];
imageData[(x+y*imageWidth)*bytesPerPixel+1]=
bitmapData[( x+(rev_val-y)*imageWidth)*3 + 1];
imageData[(x+y*imageWidth)*bytesPerPixel+2]=
bitmapData[( x+(rev_val-y)*imageWidth)*3 + 2];
if(bytesPerPixel==4) imageData[(x+y*imageWidth)*bytesPerPixel+3]=
alphaData[ x+(rev_val-y)*imageWidth ];
}
}
glTexImage2D(GL_TEXTURE_2D,
0,
bytesPerPixel,
imageWidth,
imageHeight,
0,
img->HasAlpha() ? GL_RGBA : GL_RGB,
GL_UNSIGNED_BYTE,
imageData);
delete[] imageData;
wxDELETE(img);
return texture;
Here's the arguments I use in my canvas class:
int args[] = { WX_GL_RGBA, WX_GL_DOUBLEBUFFER, WX_GL_DEPTH_SIZE, 16, 0 };
I've googled the problem and noticed other people are having it too, but there doesn't seem to be any real solution that I can find other than switching to wxWidgets 2.8, which doesn't build properly in VS2010 (what I use).
I can provide other code if needed, I'm not really sure what the root of this problem is.
glEnable(GL_TEXTURE_2D);
? – Bartek Banachewicz