【发布时间】:2012-12-17 11:13:40
【问题描述】:
我最近一直在学习 wxWidgets(2.9.4 版),但在让纹理在 OpenGL 窗口中正常工作时遇到了问题(但 glColor4f 工作正常)。
我尝试按照 wxWidget 的教程进行操作,包括从 wxImage 加载纹理。我还尝试使用 stbi_image 加载纹理,但无论我做什么,纹理始终保持白色。这是我正在使用的代码:
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;
这是我在画布类中使用的参数:
int args[] = { WX_GL_RGBA, WX_GL_DOUBLEBUFFER, WX_GL_DEPTH_SIZE, 16, 0 };
我用谷歌搜索了这个问题并注意到其他人也有这个问题,但除了切换到 wxWidgets 2.8 之外,我似乎找不到任何真正的解决方案,它在 VS2010 中无法正确构建(什么我用)。
如果需要,我可以提供其他代码,我不确定这个问题的根源是什么。
【问题讨论】:
-
运行纹理加载代码时是否有当前的 GL 上下文?
-
是的,我调用 context = new wxGLContext(this);在构造函数中,我有 wxGLCanvas::SetCurrent(*context);在我的渲染循环中。但是我尝试将其直接移动到 context = new wxGLContext(this);线。我从中得到了一个纹理,但调试器抛出了错误断言“IsShownOnScreen()”失败
-
到底是什么问题? “无法正常工作”确实不够准确。
-
纹理都是白色的,抱歉不够清楚。
-
glEnable(GL_TEXTURE_2D);?