
Using multiple textures
Posted Wednesday, 4 May, 2011 - 18:21 by fkj inThe texture examples I've seen only shows how to use a single texture (to keep it simple obviously), but I'm trying to use two textures, and am having a problem. I use the following code to load a texture (I believe there is nothing special about this code):
Bitmap bitmap = new Bitmap( path ); BitmapData data = bitmap.LockBits( new System.Drawing.Rectangle( 0, 0, bitmap.Width, bitmap.Height ), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb ); int textureId = GL.GenTexture(); GL.TexImage2D( TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0 ); bitmap.UnlockBits( data );
Loading two textures I get textureId's 1 and 2. This is done once in the start of the program (...OnLoad...).
I then use only the first texture on some quads (GL.BindTexture( 1 ) etc.).
It all works fine but for one thing, it always uses the last texture loaded! If I only load the first texture it looks fine, but it's like it always uses the last texture nomatter what id I try to use. Note that BindTexture() is never called using 2 anywhere in the code, and GetError() returns no error on neither TexImage2D or BindTexture. Both textures works fine if only one texture is loaded.
What am I missing? Do I have to do something extra to make two textures available?
(using: OpenTK 1.1 (by Subversion), my driver supports only OpenGL 2.1)


Comments
Re: Using multiple textures
Hello everyone,
I'm having the same problem as the original poster. I've done a lot of reading on this forum, as well as finding a quite helpful post on StackOverflow, but I am still stuck after several days trying to figure this out on my own. I'm hoping the helpful people in this forum can help me out.
Like the OP, I'm trying to pass two textures to my fragment shader. And like the OP, only the second texture seems to get loaded. I've taken the code straight from the OpenTK sample browser -- the rotating cube. I want to draw a texture on the side of the cube and tint it pink. This works just fine:
Then I try to add a second texture. Ideally I'd like to see MyTexture populated with the first texture, and MyTexture2 populated with the second texture).
If you run the code as-is, the cube shows the texture on each face of the cube tinted pink. It's the same texture whether you reference MyTexture or MyTexture2 in the fragment shader.
When I uncomment the line
// GL.BindTexture(TextureTarget.Texture2D, texture1);Then the sides of the cube become black. Same problem as the OP.
I'm sure I'm doing something stupid here, but I'm quite stuck. I can't figure out how to get the second texture to be passed to my program!
Here's the source code in full. It's almost identical to the OpenTK example browser code for the simple cube example. This should compile as-is if you have two files on your drive at
Here is the full source. Any help would be really appreciated! Thank you!
Re: Using multiple textures
Okay after some more digging and poking around I was able to solve it. I'm going to post what I did here in case others are confused:
Problem was that I had to do each texure's entire code path one at a time. So I changed the function to this:
This works. I can change the reference to MyTexture to MyTexture2 in the fragment shader and it shows the second texture.
Re: Using multiple textures
Okay, so I was *still* wrong. It turns out that my fundamental error was this
Instead, multiple textures should be loaded at Texture1!
Here is the code for my sample program, which loads 4 textures on each face of a cube (so a quad has multiple textures). The shader tints each texture to "prove" that it went through the shader. I'm embarassed to say this took me multiple days to figure out so I'm posting here hoping someone will get help from this. This seems to be a common enough problem
Re: Using multiple textures
this seems to be a common point of confusion, so I've created a blog post on this
http://www.opentk.com/node/2559
I think something lik ethis should be added as an example to the example browser, since many people seem to get caught up on this!
Re: Using multiple textures
Anytime you bind a texture you MUST set the texture state BEFORE a Begin call, like so: