Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
wxWidgets OpenGL纹理_Opengl_Wxwidgets_Textures - Fatal编程技术网

wxWidgets OpenGL纹理

wxWidgets OpenGL纹理,opengl,wxwidgets,textures,Opengl,Wxwidgets,Textures,我最近一直在学习wxWidgets(版本2.9.4),在OpenGL窗口中使纹理正常工作时遇到了问题(不过glColor4f工作得很好) 我尝试了遵循wxWidget的教程,包括从wxImage加载纹理。我还尝试使用stbi_图像加载纹理,但无论我做什么,纹理始终保持白色。以下是我使用的代码: wxImage* img = new wxImage(wxT("grass.png")); GLuint texture; glGenTextures(1, &texture); glBindT

我最近一直在学习wxWidgets(版本2.9.4),在OpenGL窗口中使纹理正常工作时遇到了问题(不过glColor4f工作得很好)

我尝试了遵循wxWidget的教程,包括从wxImage加载纹理。我还尝试使用stbi_图像加载纹理,但无论我做什么,纹理始终保持白色。以下是我使用的代码:

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;
我在谷歌上搜索了这个问题,发现其他人也遇到了这个问题,但除了切换到wxWidgets 2.8之外,我似乎找不到任何真正的解决方案,因为wxWidgets 2.8在VS2010中无法正常构建(我使用的)


如果需要,我可以提供其他代码,我不确定这个问题的根源是什么。

我设法找到了问题的根源,但是我无法找到一个干净的解决方案。结果我不得不调用wxGLCanvas::SetCurrent(*context);在尝试加载任何纹理之前,但wxGLCanvas::SetCurrent(*上下文);只能在显示窗口时调用(即,不在我加载纹理的构造函数中)。到目前为止,我的解决方案是:

static bool initialInit = false;
if(!initialInit) {
    wxGLCanvas::SetCurrent(*context);
    initGL();
    initialInit = true;
}

在我的渲染循环中调用,initGL()执行所有GL设置并加载我需要的纹理。这是正确的,但似乎是一个可怕的解决方案。

我通常会按照以下思路做一些事情:

// define a specialisation of wxGLCanvas

class cMyCanvas: public wxGLCanvas
{
 ...

// in frame constructor, call canvas constructor

cMyCanvas * myCanvas = new cMyCanvas( this );

// show the frame

Show();

// set context current

myCanvas->SetCurrent( context );

// initialize canvas

myCanvas-> initGL();

运行纹理加载代码时是否有当前GL上下文?是的,我调用context=new wxGLContext(此);在构造函数中,我有wxGLCanvas::SetCurrent(*context);在我的渲染循环中。然而,我尝试将其移动到context=newwxglcontext(this)下的直线;线路。我从中获得了一个纹理,但调试器抛出了错误断言“IsShownOnScreen()”失败。到底是什么问题?“很难让它工作”确实不够精确。纹理都是白色的,很抱歉没有让它足够清晰。
glEnable(GL_TEXTURE_2D)?这仍然会导致“IsShownOnScreen()”断言失败。除非在创建帧后调用SetCurrent,否则我似乎无法摆脱它。所谓创建,是指构造还是显示?我期待你的表演。所以你需要先展示一下画面。我将在答案中的代码中添加此选项。我的意思是构造,但这适用于Show();方法包括。谢谢你的帮助:)
// define a specialisation of wxGLCanvas

class cMyCanvas: public wxGLCanvas
{
 ...

// in frame constructor, call canvas constructor

cMyCanvas * myCanvas = new cMyCanvas( this );

// show the frame

Show();

// set context current

myCanvas->SetCurrent( context );

// initialize canvas

myCanvas-> initGL();