kudorgyozo's picture

How do I use TexSubImage2D to replace a part of the texture

Hello, noob question:

I want to have dynamically generated transparent holes in my texture. I have a Bitmap with the original texture already uploaded to the video memory and visible. I draw a transparent hole in it. If I save it as a png file to verify it is ok the hole is there. But when I try to use TexSubImage2D to modify the texture it only works if I replace the whole texture and not just the part with the hole. I use this method in a similar way to TexImage2D: Use LockBits to get the IntPtr with the data, upload the data, release the lock. But here it doesn't work, it replaces it with some garbage, it seems to me as if the number of rows and/or columns to replace are incorrect but I'm not sure.

Edit: I have used TexSubImage2D in C before, and it worked, I really don't know what am I doing wrong here.
Edit: This is what I try to do:

        private void drawHole(int x, int y, int r) {
            Rectangle rect = new Rectangle(x - r, y - r, r * 2, r * 2);
            Graphics gfx = Graphics.FromImage(texture);
            gfx.CompositingMode = CompositingMode.SourceCopy;
            gfx.FillEllipse(Brushes.Transparent, rect);
            GL.BindTexture(TextureTarget.Texture2D, texId);
            BitmapData bmp_data = texture.LockBits(rect,
                ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            GL.TexSubImage2D(TextureTarget.Texture2D, 0, rect.X, rect.Y,
                rect.Width, rect.Height, OpenTK.Graphics.OpenGL.PixelFormat.Bgra,
                PixelType.UnsignedByte, bmp_data.Scan0);
            texture.Save("szar.png", ImageFormat.Png);

Here is what I get:

IMAGE. This is one quad with a texture on it. White is solid and blue is transparent, it is the opengl clearcolor.


Comment viewing options

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

I found the solution to this. The short version is that I probably locked more 'bits' and uploaded more data than required, that is why it got distorted. The solution is create a new image with the new smaller dimensions and upload that, or to upload full rows of pixels from the original, or to upload everything again. Correct me if I'm wrong and you're very bored.