Symkortem's picture

ANother noob problem: More than one texture

Hi Communety!

I have a little noob Problem.
Sorry for that, but iam still learning.

In my Programm i want to draw many Quads, and every quad should get another bitmap as texture.
So i tried to change the code from "http://www.opentk.com/node/791" for my needs.
But all quads have the same texture.

Can anybody help me pls, how can i assign different textures to the quads?

best regards
Symi


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
puklaus's picture

When loading textures, keep every created textures ID in memory and
when rendering quads, change (bind) before rendering the wanted texture id:
GL.BindTexture(TextureTarget.Texture2D, textureID);

Symkortem's picture

OK.

First of all i load the bitmap.

bitmap = new Bitmap(@"Resources\Düne.JPG");

than i upload the texture

            GL.Enable(EnableCap.Texture2D);
            GL.Enable(EnableCap.Blend);
 
            GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
 
            if(bitmapindex >0 && texture[bitmapindex-1] == 0)
                texture[bitmapindex-1] = GL.GenTexture();
                //GL.GenTextures(1, out texture[bitmapindex - 1]);
 
            BitmapData data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
 
            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0,
                OpenTK.Graphics.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
 
            bitmap.UnlockBits(data);
 
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);

than i want to load/upload the second bitmap/texture.
same procedure.

after that, i want to draw 2 quads, which i have stored in a list.
in itemindex is stored, whether it is the first, or second texture
the first quad has itemindex = 0 and the 2nd has itemindex = 1

            if (itemlist.Count != 0)
            {
                foreach (Item i in itemlist)
                {
                    GL.Begin(BeginMode.Quads);
                    GL.BindTexture(TextureTarget.Texture2D, i.ItemIndex);
                    label2.Text = i.ItemIndex.ToString();
                    GL.TexCoord2(0f, 1f);
                    GL.Vertex3(i.Posx - 15, i.Posy - 15, i.Posz);
                    GL.TexCoord2(0f, 0f);
                    GL.Vertex3(i.Posx + 15, i.Posy - 15, i.Posz);
                    GL.TexCoord2(1f, 0f);
                    GL.Vertex3(i.Posx + 15, i.Posy + 15, i.Posz);
                    GL.TexCoord2(1f, 1f);
                    GL.Vertex3(i.Posx - 15, i.Posy + 15, i.Posz);
                    GL.End();
                }
 
 
            }

but now i have 2 quads with the same texture. and always the last one i have uploaded.

objarni's picture

BindTexture should be called before Begin.

Entropy's picture

I guess it's a personal preference, but I like to keep all my OpenGL texture/VBO/buffer/FBO handles in separate instances of fully OO classes. That way I might load a .png of a cow and a .png of a sheep into 2 Texture2D objects called, respecively, "cow" and "sheep", with nested draw methods (cow.Draw();, or maybe game.RenderFrame += sheep.Draw;).

Edit: corrected my terminology. Changed "classes" to "objects".

Symkortem's picture

Thats. That hint with the OO class was great...

now i just have to rewrite my game class.

thanks alot