Segfault in gluBuild2DMipmaps()

Member
Posts: 156
Joined: 2002.10
Post: #1
A bit of weirdness that I don't quite understand:

I've got code that loads a bunch of images from files and creates textures out of them:

Code:
SDL_Surface* img_data = IMG_Load(path);
        GLuint textureID;
        
        if (img_data == NULL){
            fprintf(stderr, "IMG_Load: %s\n", IMG_GetError());
            exit(1);
        }
        
    // Create Texture    
    glGenTextures(1, &textureID);
    glBindTexture(GL_TEXTURE_2D, textureID);
        
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR​);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
    gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA8, img_data->w, img_data->h, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, img_data->pixels);

This works fine for images of varying size, including a background texture I created which was 800x600. I wanted to up the level of detail and have created a 1024x768 texture, which I added, but when the code comes to call gluBuild2DMipmaps() for it, it crashes with a segfault. I'm sure the image format is correct as I've put a breakpoint before the call and checked the img_data struct looks OK (compared to images I know load OK).

Another thing I spotted was if I don't load the 800x600 texture, then the bigger one loads fine. This is suggesting to me that something is running out of memory (VRAM???)

Am I along the right lines? If so can I work around this? Or at least check when I have reached the limit so I can do something nicer than segfault?

Cheers

- Iain
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
What OS are you on? gluBuild2DMipmaps (gluScaleImage, really) would regularly crash for NPOT images or non-RGBA pixel formats on older OSes.

If you're targeting 10.3.9+, you should probably use SGIS_generate_mipmap instead, though of course that won't do the image scaling you seem to require.
Quote this message in a reply
Member
Posts: 156
Joined: 2002.10
Post: #3
I'm still on 10.3.9 (although will me moving to 10.5 when it eventually arrives...)

Is the crashing just buggy GL code, or am I actually doing something wrong? Your NPOT comment reminded me that should I be enabling NPOT textures, or rectangle textures, or something, rather than just straight GL_TEXTURE_2D?

- Iain
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #4
Mipmapping only works for power-of-two textures*. gluBuild2DMipmaps scales the given image to a power of two before building the mipmaps.

The crashes were just stupid bugs in the OS. Nothing you could do about them. I thought they were fixed by 10.3.9 though. Try RGBA, UNSIGNED_BYTE -- that was the format that could be counted on first.

* unless your OpenGL driver exports ARB_texture_non_power_of_two or OpenGL 2.0, which is currently only available to Intel Macs under 10.4.8.
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #5
IBethune Wrote:...a background texture I created which was 800x600.

Does a "background" even need mipmaps?
Quote this message in a reply
Member
Posts: 156
Joined: 2002.10
Post: #6
Good point, I think i'll add a flag to my image loading code so that individual images can be tagged for mip-mapping, or not.

i.e. I'd use mipmapping for things that are textures in the normal sense (applied to 3D models, where the camera may view from arbitrary angle/distance), and no mip-maps for things which are really just 'images' i.e. will be applied in a glOrtho view e.g. backdrops, buttons, HUD detail etc.

- Iain
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #7
More importantly, you should use RECTANGLE target for your NPOT background/button textures. gluBuild2DMipmaps is squishing your 800x600 to 1024x512, and eating an additional 33% of VRAM for the mipmap chain that you never use.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  EXC_BAD_ACCESS with gluBuild2DMipmaps Marjock 3 3,315 Aug 5, 2007 01:38 AM
Last Post: Marjock
  how to use Glubuild2DMipmaps, GlTexImage2D and glTexParameter 007gamer 1 4,267 Jan 31, 2007 04:36 AM
Last Post: OneSadCookie