problem with glGenTextures()

Started by
5 comments, last by Butter 18 years, 9 months ago
I am having a problem with multiple textures. I am able to use glGenTextures() to get 1 texture ID and then bind the texture. Then when I render, I see the texture perfectly. But, when I try to load a second texture, the call to glGenTextures() returns 1 again (the same texture name as the first texture). My code proceeds to over-write the first texture with the new one and then renders only the new texture. Any ideas why this is happening?
Advertisement
Are you checking glError after each call?
yes - I have a method that calls glGetError() and prints any errors to my log file. I have added MANY calls to this method - I'm not seeing any errors.

Because the glGenTextures() is returning 1 each time, there is only one texture that is really loaded - so it gets mapped onto everything I am rendering. All of my loaded Materials have a textureId of 1.

I have added a second call to glGenTextures() in my texture loading code:
glGenTextures(1, &textureId[0]);
glGenTextures(1, &textureId[0]);

With a smaller model (2 cubes in a .obj file - one material for each):
when the first material is loaded the first glGenTextures() returns 1 and the second 2. On loading the second Material the first glGenTextures returns 2 and the second 3.

So then my Materials now have different texture IDs and glIsTexture() says both are valid, but the first cube renders with no texture (second one gets its texture).

Seems like the first texture hasn't finished loading or something?
you should only have to call glGenTextures() once in the life of your program. Try calling it like this - glGenTextures(2, textureID); then Bind your textures, and load them. Bind one, load one, bind next load next, etc... I'm pretty sure that will fix your problem
I'm going to try that now, but for a real "engine" you have no idea how many textures you will need until you are done parsing in all of your objects.
glGenTextures returns names that are not in-use. A texture name may only be considered in-use once it has been bound using glBindTexture. Try binding/uploading data for the first name before calling glGenTextures again.

The glGenTextures man page still indicates that this is a bug though.
I have been making the calls in that order. Each time my model loader excounters a "newmtl" in the .obj model's .mtl file it loads the material; which includes the glGenTextures()/glBindTexture()/glTexImage2D() and so on.

I have modified a NeHe's lesson #6 to load multiple textures this way and that works so . . . I am going to try to rewrite some of my stuff since I can't find the problem and it is to much code to post right now.

Thanks for the help.

This topic is closed to new replies.

Advertisement