Search code examples
c++opengltexturesaccess-violationdevil

Access violation when calling glTextureParameteri with OpenGL and DevIL


I'm trying to load an image with DevIL and then create a texture. I have a class named Texture, and I'm doing all this stuff in the constructor. A valid context has been created. I'm getting an access violation on the first call to glTextureParameteri(), so I guess the texture didn't bind correctly. I just see no reason why it would not bind.

Here's the constructor:

Texture::Texture(const std::string& filename){
//DevIL handle for the image
unsigned int ilID;

ilGenImages(1, &ilID);

ilBindImage(ilID);

ilEnable(IL_ORIGIN_SET);
ilOriginFunc(IL_ORIGIN_LOWER_LEFT);

if (!ilLoad(IL_BMP, (ILstring)filename.c_str())){
    DebugUtility::gl_log_err("Failed to load image &s\n", filename.c_str()); //Just function that prints to log file
    return;
}
ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);

glGenTextures(1, &texture);

//"texture" is global GLuint
glBindTexture(GL_TEXTURE_2D, texture);

//LINE BELOW CAUSES ACCESS VIOLATION!
glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);

glTextureParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTextureParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
    ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT),
    0, GL_RGBA, GL_UNSIGNED_BYTE, ilGetData());

ilDeleteImages(1, &ilID);
//Again, prints to log file
DebugUtility::gl_log("Succefully loaded and created texture %s", filename.c_str());
} 

ilLoad returns true, since the code inside that if-block is not executed. Here is the error (I'm using Visual Studio 2013):

Unhandled exception at 0x74E1CB49 in projectSDL.exe: 0xC0000005: Access violation executing location 0x00000000.


Solution

  • The function glTextureParameteri() is an extension, try using glTexParameteri() and binding an active texture unit glActiveTexture(GL_TEXTURE0). glTextureParameteri() also expects the texture ID, not the target.