PDA

View Full Version : Texture binding.


LongJumper
2003.10.19, 02:25 AM
having a little trouble with texture binding.

Right now my code stands as loading in a window texture(the background of a window), binding that to what is most likely the name 1. Then it goes and loads another texture for a button inside that window, which I know for a fact gets 2, and is done like this:


glGenTextures(1, &texture.texID);
glBindTexture(GL_TEXTURE_2D, texture.texID);
if (UseMipMaps)
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, texture.width, texture.height,
type, GL_UNSIGNED_BYTE, (GLvoid *)imageData);
}

Then after that, it loads the terrain texture which is a 128x128 tiled image. Which does some work with gluScaleImage and later calls glTexImage2D a whole lot of times while building the display list, but no binding or generating of textures.

Now the control(the one loaded second) appears as the terrain texture, I assume because the call to glTexImage2D is associated with the last glBindTexture and therefore changes the control's texture to whatever the last glTexImage2D was.

So I played around with it just putting glGenTextures and glBindTextures in various spots.. when I put it before the display list is built(LoadTile is called mapZ*mapX times, each time a glTexImage2D is called) and it makes it so the button flashes the texture its supposed to, then goes permanently to the terrain texture. T

hen I made it so it generated and binded a texture each time it called LoadTile, it turned it completly white(no texture?) then I did it so if no texture was generated in the terrain texture slot, it would generated and bind it, otherwise it would call glTexImage2D by itself. This made a pretty cool effect where everything worked 99% correct, except it left weird tile lines around each tile and one stripe from one side of a tile would be displayed on the opposite side of each tile.

I'm pretty stumped, since I've only been playing with OpenGl for awhile now and don't have time to read my book thoroughly on it, so any help is appreciated.

NCarter
2003.10.19, 06:00 AM
I'm not sure what's going on with your code. What does LoadTile actually do? From your description it sounds like you're loading and setting up the texture object every time you draw a tile on the map. If that's the case, you ought to change it so that it loads and sets up all the textures [i]once before anything gets drawn, then use glBindTexture() to recall each texture when you need to use it.

This made a pretty cool effect where everything worked 99% correct, except it left weird tile lines around each tile and one stripe from one side of a tile would be displayed on the opposite side of each tile.

Without seeing a screenshot I can't be sure, but it sounds like your tiles are set to GL_REPEAT mode, which makes their edges wrap around so you see part of the left side of the texture on the right and vice-versa. If you do this:

::glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
::glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);


(or possibly GL_CLAMP_TO_EDGE, depending on what you're doing), it'll keep repeating the same edge pixels instead of allowing them to wrap around. You may still see a bit of a seam unless you design your textures to deal with the junction.

If that's not the problem, I think you'll have to post again with a bit more information describing how LoadTile and your drawing routine work.

LongJumper
2003.10.20, 12:59 AM
Killa. It worked, thanks so much. I think what I did is probably dumb and will catch up to me later, but for now it works great.

The LoadTile function(the unupdated version is in another thread, the one above this I believe) goes through an image that was loaded and takes tiles out of it and calls glTexImage2D.

What I did was, when it loops thru to load the tile, I check if the terrain's texture id(totally useless, since I never really use it) is not 0, then I generate and bind that texture clal glTexImage2D, etc. That's the only time I bind it though, so it works out kind of weird. Before I bound it, the last texture I bind always becomes the last terrain tile texture...sorry if that is confusing, I've been staring at a monitor for 3 hours.