Question

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]);

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.

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?

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

Was it helpful?

Solution

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]);

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:

Add this:

glGenTextures(1,&texture[0]);

Before this line:

glBindTexture(GL_TEXTURE_2D, texture[0]);

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top