使用OpenGL和DevIL调用glTextureParameteri时发生访问冲突

Hei*_*ski 3 c++ opengl textures access-violation devil

我正在尝试使用DevIL加载图像,然后创建纹理。我有一个名为的类Texture,并且正在构造函数中进行所有这些操作。一个有效的上下文已创建。第一次调用时遇到访问冲突glTextureParameteri(),因此我猜纹理未正确绑定。我只是没有理由不绑定它。

这是构造函数:

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());
} 
Run Code Online (Sandbox Code Playgroud)

ilLoad返回true,因为未执行该if-block内的代码。这是错误(我正在使用Visual Studio 2013):

projectSDL.exe中0x74E1CB49的未处理异常:0xC0000005:执行位置0x00000000的访问冲突。

小智 5

该功能glTextureParameteri()是扩展,请尝试使用glTexParameteri()并绑定活动纹理单元glActiveTexture(GL_TEXTURE0)glTextureParameteri()还需要纹理ID,而不是目标。

  • `glTextureParameter()` 是 OpenGL 4.5 中的标准。我希望人们第一次看到它时会将它与 `glTexParameter()` 混淆。有时你想知道 OpenGL 中的 API 设计选择...... (2认同)