0

I am attempting to load a texture into OpenGL using Devil, and i am having a segmentation fault upon the calling of this constructor

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. 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

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

DrPresident
  • 7
  • 1
  • 6
  • Also i am very fairly new to opengl and this is the very first time i have tried anything with image loading or textures so i may be using some of the functions very wrong – DrPresident Feb 21 '15 at 00:16

2 Answers2

2

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. So glTexImage2D reads through your stack and eventually attempts to access memory it was not supposed to access and you get segmentation fault.

I guess you'd want to use ilGetData() instead.

n0rd
  • 11,850
  • 5
  • 35
  • 56
  • i tried that and i still get a seg fault, what your saying makes sense but wouldnt ilGetData() reference the same memory as &tex? – DrPresident Feb 21 '15 at 18:16
  • `tex` is of type `ILuint` and is allocated on stack, therefore it occupies fixed amount of memory (and is likely just typedef for `usigned int`), you cannot possibly load arbitrarily sized image into it. So it's very likely just a descriptor similar to the one you get by calling `glGenTextures`. Run your program with debugger attached, you would be able to figure out exact crash location. – n0rd Feb 21 '15 at 18:22
  • I was able to find it as you can see in the edit above, but that was just by going through and commenting out one line at a time and running the program to find when it wouldnt crash, how can i have gdb enter that function instead of just main? when i use it it only debugs main – DrPresident Feb 21 '15 at 18:34
  • Your code lacks error checking. Are you sure image loads fine? – n0rd Feb 22 '15 at 08:31
  • Would an invalid load cause a segmentation fault? i had thought that the memory would still be allocated like a null pointer – DrPresident Feb 22 '15 at 18:14
  • When trying to print the iLoadImage function because it returns a boolean the segfault occurs and the program terminates before the function can return – DrPresident Feb 22 '15 at 18:24
  • I"m not sure what you are talking about, but if `ilGetData()` returns null pointer, it will very well cause segmentation fault when you'd try to read from it. – n0rd Feb 22 '15 at 20:11
  • the problem is that my program never gets that far as it exits at ilLoadImage – DrPresident Feb 23 '15 at 04:25
  • Now since you know where it crashes you can start removing stuff until it stops. Or create [MCVE](http://stackoverflow.com/help/mcve) with `ilLoadImage()` call to see if it reproduces with minimal code. – n0rd Feb 23 '15 at 06:06
  • So i setup a MCVE and after playing around a little i found out that it has something to do with the image that i load, because after changing the image the segmentation fault went away, but then would happen again with other images so i think it has something to do with opengl. Thank you for your help. – DrPresident Feb 23 '15 at 07:08
0

Make sure you have DevIL initialized with ilInit ( ) and change &tex to ilGetData ( ) and then it should work.

Vakuza
  • 1