eumcoz's picture

Problem with Textures using Objarni's TexUtil

Hi,

I'm having a problem getting textures on my images. I am using objarni's TexUtil, found here http://www.opentk.com/node/750. I have been following what I found here http://www.opentk.com/node/909, but trying to change it for 2D.

What my problem seems to be is that I cannot load in multiple textures at a time. Whichever is the last texture to be loaded in, seems to be applied to everything. I have found a few weird things pop up so far. Below is some of my code.

 
             TexLib.TexUtil.InitTexturing();
            groundTexture = TexLib.TexUtil.CreateTextureFromFile("ground.png");
            int tmpTexture = TexLib.TexUtil.CreateTextureFromFile("mario.png");
            Mario.setTextures(tmpTexture);

This code is called when my program starts. Mario.SetTextures is used in my class to save different textures.

            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); 
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadIdentity();
            GL.PushMatrix();
            {
                GL.Begin(BeginMode.Quads);
                {
                    GL.BindTexture(TextureTarget.Texture2D, groundTexture);
                    GL.TexCoord2(0, 1);  GL.Vertex2(0, 0);
                    GL.TexCoord2(0, 0);  GL.Vertex2(0, 100); 
                    GL.TexCoord2(1, 0);  GL.Vertex2(Width, 100);
                    GL.TexCoord2(1, 1);  GL.Vertex2(Width, 0);
                }
                GL.End();
            }

This should draw the ground, however; instead of using the ground image, it is drawing the Mario image.

            GL.PushMatrix();
            {
               GL.Begin(BeginMode.Quads);
               Mario.Draw();
           }
           GL.PopMatrix();
 
        public void Draw()
        {
            GL.BindTexture(TextureTarget.Texture2D, Mario1Tex);
            GL.Disable(EnableCap.DepthTest);
            GL.Enable(EnableCap.Blend);
            GL.TexCoord2(0, 1); GL.Vertex2(0, 100);
            GL.TexCoord2(0, 0); GL.Vertex2(0, Height + 100);
            GL.TexCoord2(1, 0); GL.Vertex2(Width, Height + 100);
            GL.TexCoord2(1, 1); GL.Vertex2(Width, 100);
            GL.Disable(EnableCap.Blend);
            GL.Enable(EnableCap.DepthTest);
        }

This last bit of code should, and does draw mario. This only works however If the first bit of code is in that order, the initialization of ground and mario are switched, a squished image of the ground is drawn on Mario.

Also one strange thing that I found is that GL.BindTexture doesn't really do anything at all, I can comment both of the lines I have it in out, and it will still draw. Same with the EnableCap.DepthText/Blend, leaving the enabled / disabled produces no change.

One more strange thing I noticed. If I tried to draw an object with a GL.Color(), the object would take the background of the texture being drawn(For the Mario object, it was and Alpha background, so i could not see it, for the ground its a brownish background.

Am I using this Library wrong, or is it possibly something else with my code?

Any help would be great, let me know if you need any more info.

Thanks


Comments

Comment viewing options

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

In OpenGL, you need to set the "current texture" before you draw some primitive (like quads).

You do this with a call GL.BindTexture(Texture2d, ground);

So before you draw, call GL.BindTexture(ground OR tmpTexture).

Another point: I notice you call GL.Begin(..) without Gl.End() in a method. This will get you into trouble! Try to keep the GL.Begin/End in the same method.

eumcoz's picture

Ah, calling GL.BindTexture after the begin was my problem. And I just forgot to include the GL.End() when trying to weed out all my other code, already had problems with that, so it is there!.

I still noticed that when drawing my other objects(non-textured), they are still taking the background of my textures, any clue why this is happening. It's not a huge problem since just about everything will be textured, but it tells me there might be another problem in my code that I don't know about.

the Fiddler's picture

When something is not textured, call GL.BindTexture(0) or disable texturing completely. Otherwise, the last bound texture will be used.

eumcoz's picture

Awesome, thanks.

objarni's picture

eumcoz - Fiddler already gave you the solution.

Just wanted to elaborate on the "why" you got what you did when you didn't disable texturing for your solid quad.

The reason is that when you drew your solid quad, the texture coordinate in each corner of that rectangle got the texture coordinate you last specified (your last call to TexCoord). And then the color is determined by what color the texture bitmap has at that texture coordinate (probably the color of one of the corners of the bitmap).