简体   繁体   English

用Devil分割错误将纹理加载到OpenGL中

[英]Segmentation fault loading texture with Devil into OpenGL

I am attempting to load a texture into OpenGL using Devil, and i am having a segmentation fault upon the calling of this constructor 我正在尝试使用Devil将纹理加载到OpenGL中,并且在调用此构造方法时遇到分段错误

Sprite::Sprite(const char *path){

    ILuint tex = 0;

    ilutEnable(ILUT_OPENGL_CONV);
    ilGenImages(1, &tex);
    ilBindImage(tex);
    ilLoadImage(path);
    ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);
    width  = (GLuint*)ilGetInteger(IL_IMAGE_WIDTH);
    height = (GLuint*)ilGetInteger(IL_IMAGE_HEIGHT);

    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);

    glTexImage2D(GL_TEXTURE_2D,
                 0,
                 GL_RGBA,
                 width,
                 height,
                 0,
                 GL_RGBA,
                 GL_UNSIGNED_BYTE,
                 &tex);

    ilBindImage(0);
    ilDeleteImages(1, &tex);
    ilutDisable(ILUT_OPENGL_CONV);

}

and texture is a protected member 纹理是受保护的成员

GLuint texture;

As soon as this constructor is called i recieve a segfault error and it exits and I am using freeglut, gl, il, ilu, and ilut. 一旦调用此构造函数,我就会收到一个segfault错误,并且退出并且我正在使用freeglut,gl,il,ilu和ilut。 any help would be appreciated 任何帮助,将不胜感激

Edit: 编辑:

I also decided to take a different approach and use 我还决定采用不同的方法和使用

texture = ilutGLLoadImage(path)

function to just load it directly into the gl texture because I located the segfault coming from 直接将其直接加载到gl纹理中的功能,因为我找到了来自

ilLoadImage(path)

but the compiler tells me that ilutGLLoadImage() is not declared in this scope, and i have IL/il.h IL/ilu.h and IL/ilut.h all included and initialized 但是编译器告诉我在此范围内未声明ilutGLLoadImage(),并且我将IL / il.h IL / ilu.h和IL / ilut.h都包括在内并进行了初始化

I never used DevIL, but glTexImage2D wants pointer to pixel data as the last argument and you pass pointer to local variable tex there instead, which is allocated on stack and does not contain expected information. 我从未使用过DevIL,但是glTexImage2D希望将指向像素数据的指针作为最后一个参数,而是将指针传递给局部变量tex ,该变量在堆栈中分配,并且不包含预期的信息。 So glTexImage2D reads through your stack and eventually attempts to access memory it was not supposed to access and you get segmentation fault. 因此, glTexImage2D读取您的堆栈,并最终尝试访问原本不应该访问的内存,并且您会遇到分段错误。

I guess you'd want to use ilGetData() instead. 我猜您想改用ilGetData()

确保已使用ilInit ( )初始化了ilInit ( ) ,并将&tex更改为ilGetData ( ) ,然后它将正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM