Sign in to follow this  
jamwaffles

OpenGL Multiple textures with DevIL

Recommended Posts

jamwaffles    122
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
jamwaffles    122
[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
capricorn    139
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
jamwaffles    122
[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
capricorn    139
[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  

  • Similar Content

    • By povilaslt2
      Hello. I'm Programmer who is in search of 2D game project who preferably uses OpenGL and C++. You can see my projects in GitHub. Project genre doesn't matter (except MMO's :D).
    • By ZeldaFan555
      Hello, My name is Matt. I am a programmer. I mostly use Java, but can use C++ and various other languages. I'm looking for someone to partner up with for random projects, preferably using OpenGL, though I'd be open to just about anything. If you're interested you can contact me on Skype or on here, thank you!
      Skype: Mangodoor408
    • By tyhender
      Hello, my name is Mark. I'm hobby programmer. 
      So recently,I thought that it's good idea to find people to create a full 3D engine. I'm looking for people experienced in scripting 3D shaders and implementing physics into engine(game)(we are going to use the React physics engine). 
      And,ye,no money =D I'm just looking for hobbyists that will be proud of their work. If engine(or game) will have financial succes,well,then maybe =D
      Sorry for late replies.
      I mostly give more information when people PM me,but this post is REALLY short,even for me =D
      So here's few more points:
      Engine will use openGL and SDL for graphics. It will use React3D physics library for physics simulation. Engine(most probably,atleast for the first part) won't have graphical fron-end,it will be a framework . I think final engine should be enough to set up an FPS in a couple of minutes. A bit about my self:
      I've been programming for 7 years total. I learned very slowly it as "secondary interesting thing" for like 3 years, but then began to script more seriously.  My primary language is C++,which we are going to use for the engine. Yes,I did 3D graphics with physics simulation before. No, my portfolio isn't very impressive. I'm working on that No,I wasn't employed officially. If anybody need to know more PM me. 
       
    • By Zaphyk
      I am developing my engine using the OpenGL 3.3 compatibility profile. It runs as expected on my NVIDIA card and on my Intel Card however when I tried it on an AMD setup it ran 3 times worse than on the other setups. Could this be a AMD driver thing or is this probably a problem with my OGL code? Could a different code standard create such bad performance?
    • By Kjell Andersson
      I'm trying to get some legacy OpenGL code to run with a shader pipeline,
      The legacy code uses glVertexPointer(), glColorPointer(), glNormalPointer() and glTexCoordPointer() to supply the vertex information.
      I know that it should be using setVertexAttribPointer() etc to clearly define the layout but that is not an option right now since the legacy code can't be modified to that extent.
      I've got a version 330 vertex shader to somewhat work:
      #version 330 uniform mat4 osg_ModelViewProjectionMatrix; uniform mat4 osg_ModelViewMatrix; layout(location = 0) in vec4 Vertex; layout(location = 2) in vec4 Normal; // Velocity layout(location = 3) in vec3 TexCoord; // TODO: is this the right layout location? out VertexData { vec4 color; vec3 velocity; float size; } VertexOut; void main(void) { vec4 p0 = Vertex; vec4 p1 = Vertex + vec4(Normal.x, Normal.y, Normal.z, 0.0f); vec3 velocity = (osg_ModelViewProjectionMatrix * p1 - osg_ModelViewProjectionMatrix * p0).xyz; VertexOut.velocity = velocity; VertexOut.size = TexCoord.y; gl_Position = osg_ModelViewMatrix * Vertex; } What works is the Vertex and Normal information that the legacy C++ OpenGL code seem to provide in layout location 0 and 2. This is fine.
      What I'm not getting to work is the TexCoord information that is supplied by a glTexCoordPointer() call in C++.
      Question:
      What layout location is the old standard pipeline using for glTexCoordPointer()? Or is this undefined?
       
      Side note: I'm trying to get an OpenSceneGraph 3.4.0 particle system to use custom vertex, geometry and fragment shaders for rendering the particles.
  • Popular Now