Sign in to follow this  
jamwaffles

OpenGL Multiple textures with DevIL

Recommended Posts

I don't quite understand how texture storage works in OpenGL. I've written an object loader in C++ with a texture loading class using DevIL (image library). Everything works fine with one texture, but with more it fails, which brought me to the conclusion that my image loading code is incorrect.

I don't like pasting whole files of code, but here's my image loader class:


[b]devil.h[/b]
[code]#ifndef _DEVIL_H_
#define _DEVIL_H_

#include <SDL/SDL.h>
#include <GL/gl.h>
#include <IL/il.h>
#include <IL/ilu.h>
#include <IL/ilut.h>
#include <iostream>
using namespace std;

class DevilTexture
{
private:
ILuint texid; /* ILuint is a 32bit unsigned integer.
Variable texid will be used to store image name. */
ILboolean success; /* ILboolean is type similar to GLboolean and can equal GL_FALSE (0) or GL_TRUE (1)
it can have different value (because it's just typedef of unsigned char), but this sould be
avoided.
Variable success will be used to determine if some function returned success or failure. */
GLuint image;

bool loaded;

public:
int load(string filename);
void bind();

DevilTexture();
};

#endif[/code]

[b]devil.cpp[/b]
[code]#include "devil.h"

DevilTexture::DevilTexture()
{
loaded = false;
}

int DevilTexture::load(string filename)
{
if(loaded)
{
return 0;
}

glEnable(GL_TEXTURE_2D);

// Before calling ilInit() version should be checked.
if(ilGetInteger(IL_VERSION_NUM) < IL_VERSION)
{
// wrong DevIL version
SDL_Quit();
return -1;
}

ilInit(); // Initialization of DevIL
ilGenImages(1, &texid); // Generation of one image name
ilBindImage(texid); // Binding of image name

success = ilLoadImage((const ILstring)filename.c_str()); // Loading of image "image.jpg"

if(success) // If no error occured:
{
success = ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE); // Convert every colour component into unsigned byte. If your image contains alpha channel you can replace IL_RGB with IL_RGBA

if(!success)
{
// Error occured
SDL_Quit();
return -1;
}

glGenTextures(1, &image); // Texture name generation
glBindTexture(GL_TEXTURE_2D, image); // Binding of texture name
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); /* We will use linear
interpolation for magnification filter */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); /* We will use linear
interpolation for minifying filter */
glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), ilGetInteger(IL_IMAGE_WIDTH),
ilGetInteger(IL_IMAGE_HEIGHT), 0, ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE,
ilGetData()); // Texture specification
}
else
{
// Error occured
SDL_Quit();
return -1;
}

loaded = true;
ilDeleteImages(1, &texid); // Because we have already copied image data into texture data we can release memory used by image.
}

void DevilTexture::bind()
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, image);
}[/code]
The only idea I have as to why this segfaults with more than one texture is how I'm assigning a texture ID. Each texture uses it's own instance of DevilTexture, however OpenGL might not like the glGenImages(1, &image) line. I'm really not sure.

So, my question is this: am I loading the textures into memory properly, with the correct ID so multiple textures can be used? If not, how can I go about fixing my program? A global array perhaps?

Share this post


Link to post
Share on other sites
[quote name='capricorn' timestamp='1312715035' post='4845738']
Exactly what segfaults, if you please?
[/quote]

I run it in the Linux console and it just says `Segfault`. The only other information I can give you is a valgrind dump which actually reveals it's an SDL_GL_SwapBuffers() and possibly a driver issue.

EDIT: After re-installing my nVidia drivers (they broke), here's what I get:

[code]==2931== Memcheck, a memory error detector
==2931== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==2931== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
==2931== Command: ./WavefrontLoader-0.0.1
==2931==
==2931== Invalid read of size 8
==2931== at 0x5916A47: SDL_GL_SwapBuffers (in /usr/lib/libSDL-1.2.so.0.11.3)
==2931== by 0x402AFB: updateSimulation() (in /home/james/Projects/OpenGL/WavefrontLoader/WavefrontLoader-0.0.1)
==2931== by 0x402F8C: main (in /home/james/Projects/OpenGL/WavefrontLoader/WavefrontLoader-0.0.1)
==2931== Address 0x250 is not stack'd, malloc'd or (recently) free'd
==2931==
==2931==
==2931== Process terminating with default action of signal 11 (SIGSEGV)
==2931== Access not within mapped region at address 0x250
==2931== at 0x5916A47: SDL_GL_SwapBuffers (in /usr/lib/libSDL-1.2.so.0.11.3)
==2931== by 0x402AFB: updateSimulation() (in /home/james/Projects/OpenGL/WavefrontLoader/WavefrontLoader-0.0.1)
==2931== by 0x402F8C: main (in /home/james/Projects/OpenGL/WavefrontLoader/WavefrontLoader-0.0.1)
==2931== If you believe this happened as a result of a stack
==2931== overflow in your program's main thread (unlikely but
==2931== possible), you can try to increase the size of the
==2931== main thread stack using the --main-stacksize= flag.
==2931== The main thread stack size used in this run was 8388608.
==2931==
==2931== HEAP SUMMARY:
==2931== in use at exit: 4,555,017 bytes in 52,834 blocks
==2931== total heap usage: 68,503 allocs, 15,669 frees, 12,412,420 bytes allocated
==2931==
==2931== LEAK SUMMARY:
==2931== definitely lost: 1,722,174 bytes in 51,986 blocks
==2931== indirectly lost: 1,728 bytes in 38 blocks
==2931== possibly lost: 232,906 bytes in 28 blocks
==2931== still reachable: 2,598,209 bytes in 782 blocks
==2931== suppressed: 0 bytes in 0 blocks
==2931== Rerun with --leak-check=full to see details of leaked memory
==2931==
==2931== For counts of detected and suppressed errors, rerun with: -v
==2931== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 7 from 7)
Segmentation fault
[/code]

Share this post


Link to post
Share on other sites
Hmm, not much, not much. Perhaps you'd want to utilize gdb in the future (yes, I know using gdb from the console is a pain when you don't know it, but there are gui frontends for it out there. Code::Blocks, for example).

For now I can only say two things:

1) Move ilInit() outside of DevilTexture::load(): your application really needs just one call to ilInit().
2)
[color="#1C2837"][font="CourierNew, monospace"][size="2"][color="#000000"][code]
glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT), 0, ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE, ilGetData());
[/code][/color][/size][/font][font="CourierNew, monospace"] [/font][/color]
You'll be better off if you make proper conversion from IL enums to GL ones (i.e. for IL_IMAGE_FORMAT).

Otherwise, I don't see anything problematic concerning the textures.

Share this post


Link to post
Share on other sites
[quote name='capricorn' timestamp='1312721761' post='4845760']
Hmm, not much, not much. Perhaps you'd want to utilize gdb in the future (yes, I know using gdb from the console is a pain when you don't know it, but there are gui frontends for it out there. Code::Blocks, for example).

For now I can only say two things:

1) Move ilInit() outside of DevilTexture::load(): your application really needs just one call to ilInit().
2)
[color="#1C2837"][font="CourierNew, monospace"][size="2"][color="#000000"][code]
glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT), 0, ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE, ilGetData());
[/code][/color][/size][/font][font="CourierNew, monospace"] [/font][/color]
You'll be better off if you make proper conversion from IL enums to GL ones (i.e. for IL_IMAGE_FORMAT).

Otherwise, I don't see anything problematic concerning the textures.
[/quote]

I tried 1) and I still get a segfault. Number 2) also doesn't help :-( If you could give me some specific GDB commands to run so I can get you any information you need, please let me know. Thank you for your help once again capricorn.

Share this post


Link to post
Share on other sites
[quote name='jamwaffles' timestamp='1312722379' post='4845762']
If you could give me some specific GDB commands to run so I can get you any information you need, please let me know.
[/quote]

That's tough without having source (well I can tell you how to set a breakpoint on function entry, but that's not very interesting in this case). Anyway you yourself would be more comfortable with some ui for gdb. Try ddd at least :-)

So you say neither helped? Alright. The only thing that's left (and it correlates with your segfault dump from before) is SDL_Quit() calls. Generally, they shouldn't be in load() function anyway. If you want a fatal error exit, better use exceptions (you can catch them inside main(), print them and then exit, so at least you'll know the exact error). Seems like your code is calling SDL_Quit() at some point in load() (or even in some other part of the code you didn't show), but the application still proceeds, thus calling some other SDL function (like SDL_GL_SwapBuffers) and segfaulting. You may also want to add some debug output for any erroneous situation (like in those places where you currently call SDL_Quit).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this