Dinberg's picture

Two textures

Hello!

I have been fooling around with the example for the heightmap, and decided to try to make a field of magma to surround a textured mountain. I managed to load the texture for the mountain easily, and all went well - but upon trying to add the lava texture, it seems to override the mountains.

At first I thought I was in someway saving the image over the same slot, but I compared the values of 'texture' and 'magma' (see below, these are the ids for each texture) and they displayed 1 and 2 respectively. This is my first foray into 3d, so I'm sure i've made a silly mistake. Any help is much appreciated; below is the significant code.

In the initiation of the window:

Bitmap bmp = new Bitmap("tile.bmp");
            GL.Enable(EnableCap.Texture2D);
            GL.GenTextures(1, out texture);
            GL.BindTexture(TextureTarget.Texture2D, texture);
 
            BitmapData data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.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);
 
            bmp.UnlockBits(data);
 
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
 
            bmp = new Bitmap("magma.bmp");
 
            GL.GenTextures(1, out magma);
            Console.WriteLine("water:" + magma + ", texture:" + texture);
            GL.BindTexture(TextureTarget.Texture2D, magma);
            data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.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);
 
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
 
 
            bmp.UnlockBits(data);

Before rendering the landscape:

            GL.Enable(EnableCap.Texture2D);
            GL.BindTexture(TextureTarget.Texture2D, texture);

Before rendering the magma:
          GL.BindTexture(TextureTarget.Texture2D, magma);

Included is a screenie to elaborate ;)

Many thanks!

Inline Images

Comments

Comment viewing options

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

Ok I tidied the code up a bit once more, still no luck :(

What I have noticed is that using the BitmapData's scan0 parameter returns the same number for each texture.

inside LoadTexture, added...
Console.WriteLine(data.Scan0.ToString());

Output:

85786624
85786624

I was hoping that maybe a GC.Collect might force a different result; my belief was that in writing the load to the second texture, a common variable that both textures referenced (perhaps the IntPtr) was being overwritten such that the first texture no longer pointed to its true location, but to the second location. Still no luck, but I think this might be getting closer to the actual result :P

I've included a zip of the project code aswell, as of this writing; could someone possibly see if they get the same error? My pc is quite old, *cough* 7 years *cough*, though I dont think the graphics card is as old as the box ;).

Thanks so much for all the input so far, its been brilliant =D

edit: Fiddler, can I simply override the constructor in a new wrapper class for that texture, or will I definately need to change the source? At the moment i only have the binaries for openTK, (0.9.6). cheers!

AttachmentSize
DinbergFails.zip394.76 KB
the Fiddler's picture

Just copy and paste the code in your project, this is not a public API.

Inertia's picture

You're changing GL state inside a GL.Begin/End block.

Dinberg's picture

I havn't been able to test the suggestions yet, I've been very busy this weekend because of a deadline tomorrow (tuesday). Just wanted to let you know so you dont think the help has gone unappreciated! I hope to test this as soon as possible, probably wednesday afternoon.

Thanks again!

Dinberg's picture

Resolved as last!

Now I see the solution I can understand what you mean Intertia - sorry I did not understand before =D

In laymans terms, I was trying to perform a BindTexture within a Begin/End; the solution was to split the two types into separate Begin/End blocks and call the BindTexture before each block. I had no idea the first way was not possible!

Many thanks for all the replies and swift help.