Loading external images for textures

First off, hello everyone, I’m the new guy. Been programming with OGL for a few months now on and off, and only recently have I started working on a little project.

Anyways, onto my question.

Is there any way I can load external images (BMP, TGA, whatever) to use as textures? I’ve already searched this forum for similar topics, and found only posts like “You gotta get another library to do it for ya.” Well I’ve scoured the Internet but to no avail.

I’ve already checked out NeHe’s tutorials, which are great, but they use the GL Aux library, which I heard is awful because it’s unsupported, outdated, etc.

So I was wondering, is there any quick and easy way to accomplish this? I really don’t want to have to generate all my textures internally at runtime, that would really stink…

Since you’re keen on loading the files yourself I recommend you find a copy of OpenGL Game Programming. It has the code to load .bmp and targa image files. You might have to tweak arguments in glTexImage2D (the byte ordering) to load bitmaps apart from the book’s but otherwise it’s great. Also you could check out an open source library to see how to load other formats. Good luck

Check out the following link: http://openil.sourceforge.net

Enjoy.
K.

WOW! Excellent, thank you!

But I’m having a problem, I can’t seem to be able to load more than one texture. Well, I can, but only the second texture is being displayed when I specify the first.

Here’s what I mean…

GLuint tex[2];

// initialization routine
void init(void)
{
… // initializiations

glGenTextures(2, &tex);
ilBindImage(tex[0]);
ilutGLLoadImage("image1.tga");
ilBindImage(tex[1]);
ilutGLLoadImage("image2.tga");

}

// display routine
void display(void)
{
glClear(COLOR_BUFFER_BIT);

ilBindImage(tex[0]);
glBegin(GL_QUADS);
    glTexCoord2f(-1, -1);
    glVertex3f(-5, -5, 0);
    glTexCoord2f(-1, 1);
    glVertex3f(-5, 5, 0);
    glTexCoord2f(1, 1);
    glVertex3f(5, 5, 0);
    glTexCoord2f(1, -1);
    glVertex3f(5, -5, 0);
glEnd();
glFlush();

}

When I do this, image2.tga is being displayed, even though I specifically specified the first one. I’ve tried different variations of glBindTexture, ilBindImage, etc. and one of two things will happen:

  1. The program will crash and exit
  2. A solid white block will be displayed, untextured

The above method is the onle one I’ve found that will actually display a texture. Anyone have any ideas?

[This message has been edited by Volt9000 (edited 12-05-2003).]

the init is done After you set a valid glContext? and i hope you use glEnable(GL_TEXTURE_2D) somewhere in the program

Originally posted by Mazy:
the init is done After you set a valid glContext? and i hope you use glEnable(GL_TEXTURE_2D) somewhere in the program

No, init is done before.

And yes I do have glEnable(GL_TEXTURE_2D).

Oh wait nevermind, I got it. I was using the functions incorrectly.

This is how it must be done:

GLuint tex[2];

// initialization routine
void init(void)
{
… //initializations

glGenTextures(2, &tex);
tex[0] = ilutGLLoadImage("image1.tga");
tex[1] = ilutGLLoadImage("image2.tga");

}

Then in my display routine I just use glBindTexture as normal and it works!

Don’t actually know if I’m right, but I use

glGenTextures(1, &texPointer);

So I don’t have to know global number of textures.

[This message has been edited by LordOfTheUniverse (edited 12-05-2003).]

Originally posted by LordOfTheUniverse:
[b]Don’t actually know if I’m right, but I use

glGenTextures(1, &texPointer);

So I don’t have to know global number of textures.

[This message has been edited by LordOfTheUniverse (edited 12-05-2003).][/b]

That will only generate the space required for 1 texture, whereas I need it for 2. The first parameter is the number of textures you wish to generate.