ogl纹理崩溃的窗口上的lodepng [英] lodepng on windows of ogl texture crash

查看:189
本文介绍了ogl纹理崩溃的窗口上的lodepng的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用lodePNG从png文件加载纹理,但是加载纹理时程序崩溃.

I'm using lodePNG to load texture from a png file but the program crashes when I load the texture.

char* filename = "texture.png";
unsigned width, height;
std::vector<unsigned char>image;
GLuint texture[1];
//decode
unsigned error = lodepng::decode(image, width, height, filename);
if(error) std::cerr << "decoder error " << error << ": " << lodepng_error_text(error) << std::endl;
glBindTexture(GL_TEXTURE_2D, texture[0]);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, &image[0]);

我使用VS 2010进行编译,没有错误,但是当我调用glTexImage2D时发生崩溃. 我的系统是Windows 7,在图形卡上支持OGL 3.3.

I used VS 2010 to compile and there are no errors, but the crash happens when I call glTexImage2D. My system is Windows 7, with support for OGL 3.3 on the graphics card.

我有另一个系统,即Mac OSX 10.6,我一直在移植代码,而在Mac上完全没有问题.有没有建议的修复方法,可以将其正确加载到Windows上?

I have another system, which is a Mac OSX 10.6, and I'm always porting the code, and there are no problems with it on the Mac at all. Is there a suggested fix I could make so it will load the texture correctly on windows?

这是我获取lodePNG文件的站点: http://lodev.org/lodepng/

Here is the site where I got the lodePNG files: http://lodev.org/lodepng/

推荐答案

在您的代码中,您声明了GLuint texture [1],它是一个纹理句柄的数组.然后,在此行上绑定到此未初始化的纹理句柄:

In your code, you declare GLuint texture[1], an array of one texture handle. You then bind to this uninitialized texture handle on this line:

glBindTexture(GL_TEXTURE_2D, texture[0]);

这是不正确的.您正在绑定到尚未初始化的纹理句柄.相反,您需要做的是通过调用glGenTextures生成纹理句柄.然后,您可以绑定到纹理.所以试试这个:

This is incorrect. You are binding to a texture handle that has not yet been initialized. Instead, what you need to do is generate the texture handle by calling glGenTextures. THEN you may bind to the texture. So try this:

添加此内容:

glGenTextures(1,&texture[0]);

在此行之前:

glBindTexture(GL_TEXTURE_2D, texture[0]);

调用glGenTextures后,您的句柄(texture [0])应该为非零值.

After calling glGenTextures, your handle (texture[0]) should be a non-zero value.

这篇关于ogl纹理崩溃的窗口上的lodepng的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆