PDA

View Full Version : Loading external images for textures



Volt9000
12-04-2003, 08:40 PM
First off, hello everyone, I'm the new guy. http://www.opengl.org/discussion_boards/ubb/smile.gif 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...

dvm
12-04-2003, 09:55 PM
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 http://www.opengl.org/discussion_boards/ubb/smile.gif

Asshen
12-05-2003, 12:47 AM
Check out the following link: http://openil.sourceforge.net

Enjoy.
K.

Volt9000
12-05-2003, 10:16 AM
WOW! Excellent, thank you! http://www.opengl.org/discussion_boards/ubb/smile.gif

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

Mazy
12-05-2003, 10:37 AM
the init is done After you set a valid glContext? and i hope you use glEnable(GL_TEXTURE_2D) somewhere in the program http://www.opengl.org/discussion_boards/ubb/smile.gif

Volt9000
12-05-2003, 10:40 AM
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 http://www.opengl.org/discussion_boards/ubb/smile.gif

No, init is done before.

And yes I do have glEnable(GL_TEXTURE_2D).

Volt9000
12-05-2003, 11:14 AM
Oh wait nevermind, I got it. I was using the functions incorrectly. http://www.opengl.org/discussion_boards/ubb/biggrin.gif

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! http://www.opengl.org/discussion_boards/ubb/smile.gif

LordOfTheUniverse
12-05-2003, 12:35 PM
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).]

Volt9000
12-06-2003, 08:23 AM
Originally posted by LordOfTheUniverse:
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).]

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.