Sign in to follow this  
silverphyre673

OpenGL Specifying multiple textures causes only the last one to be usable [A-HA! I suck!]

Recommended Posts

Bug Squashed. Apparently, I suck at OpenGL. For anyone else who sucks like I do: DON'T try to use glBindTexture in between calls to glBegin and glEnd! I've got a fairly simple program that uses four different textures. I can load all the textures successfully and texture map them. My problem is that when I specify the textures using glTexImage2D, only the last one seems to be usable. Here's a non-code example of what I mean: For four textures stored in files "dirt.png", "grass.png", "road.png", and "water.png", I call the LoadTexture function, which loads the image from the file specified, sets up a new OpenGL texture, and returns the texture ID the texture is bound to. What happens is that no matter which texture I bind using glBindTexture after loading the textures, only the last texture will be displayed. For example, if I loaded the textures above in the above order, and they were given the texture bindings 1, 2, 3, and 4 by glGenTextures, the "water.png" texture would be displayed whether I called glBindTexture with 1, 2, 3, or 4. Furthermore, if I remove the call to load "water.png" as a texture, then "road.png" will be displayed for all textures. I've made sure that the correct values are being passed to glBindTexture when I'm rendering. I think the problem is somewhere in my LoadTexture function. Could you take a look at it, please? Thanks for any help:
bool LoadTexture(const char * file_name, GLuint & texture)
{
	SDL_Surface * tex = IMG_Load(file_name);

	if (tex == NULL)
		return false;

	glGenTextures(1, &texture);

	glBindTexture(GL_TEXTURE_2D, texture);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tex->w, tex->h, 0, GL_RGB, GL_UNSIGNED_BYTE,
		tex->pixels);

	SDL_FreeSurface(tex);

	return true;
}


You can pretty much ignore the SDL-specific code. I'm pretty confident that the problem is somewhere in my OpenGL code. Thanks a bunch! [Edited by - silverphyre673 on June 5, 2007 12:11:05 PM]

Share this post


Link to post
Share on other sites
That is a strange problem indeed.
Just a shot in the dark, but have you tried explicitly setting glActiveTexture(GL_TEXTURE0); before using glBindTexture ? Maybe the texture units are changed somewhere.

I really dont know, the code you provided should work just fine...

Share this post


Link to post
Share on other sites
Yep. What I've been doing is calling the LoadTexture function with different offsets of an std::vector<GLuint>, like this:


std::vector<GLuint> TEXTURES;

void LoadTextures()
{
LoadTexture("dirt.png", TEXTURES[0]);
LoadTexture("grass.png", TEXTURES[1]);
LoadTexture("pavement.png", TEXTURES[2]);
LoadTexture("water.png", TEXTURES[3]);
}





After the LoadTextures() function is called, TEXTURES stores the values {1, 2, 3, 4}, as should be expected.

I think I'm just going to forget about using straight OpenGL and go with OGRE instead. Maybe that'll clear up some of these issues...

[EDIT]

Screw it. I just rewrote the image loading code. It looks almost exactly the same to me -- and doesn't look like I changed anything that would have made a different. However, it seems to work now, which makes me a confused panda.

[EDIT #2]

Nevermind. It worked on my test rendering code (just rendering a single quad); I was able to cycle through each texture normally. Unfortunately, when I try to render the map I was originally having issues drawing, I get the same error. I think the problem must be in my drawing code:


//I checked, and the value of top_texture is as it should be for error-free
//code.
glBindTexture(GL_TEXTURE_2D, top_texture);
glTexCoord2f(0.0, 0.0); glVertex3f(x, y, z);
glTexCoord2f(1.0, 0.0); glVertex3f(x + cell_size, y, z);
glTexCoord2f(1.0, 1.0); glVertex3f(x + cell_size, y + cell_size, z);
glTexCoord2f(0.0, 1.0); glVertex3f(x, y + cell_size, z);



[Edited by - silverphyre673 on June 5, 2007 12:42:08 PM]

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