R
R
Re7r02022-01-23 15:03:11
OpenGL
Re7r0, 2022-01-23 15:03:11

How to load an image in OpenTK c# with a transparent background, what does the background itself have to do with it being transparent?

How to load an image in OpenTK c# with a transparent background, what does the background itself have to do with it being transparent?
sprite loading:

public int LoadTexture(Bitmap bitmap2, int x, int y, int weight, int height)
            {
                int tex;
                GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);

                GL.GenTextures(1, out tex);
                GL.BindTexture(TextureTarget.Texture2D, tex);

                BitmapData data = bitmap2.LockBits(new System.Drawing.Rectangle(x, y, weight, height),
                    ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
                

                GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0,
                    OpenTK.Graphics.OpenGL.PixelFormat.Rgba, PixelType.UnsignedByte, data.Scan0);
                bitmap2.UnlockBits(data);

                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);

                return tex;
            }


display:
public static void DrawImage(int image, float x, float y, int weight, int height)
            {
                GL.MatrixMode(MatrixMode.Projection);
                GL.PushMatrix();
                GL.LoadIdentity();



                GL.Ortho(-x, 1920-(x+weight), 1080-(y+height), -y, 1, -1.0f);

                GL.MatrixMode(MatrixMode.Modelview);
                GL.PushMatrix();
                GL.LoadIdentity();

                GL.Disable(EnableCap.Lighting);

                GL.Enable(EnableCap.Texture2D);


                GL.BindTexture(TextureTarget.Texture2D, image);

                GL.Begin(BeginMode.Quads);

                GL.TexCoord2(0, 0);
                GL.Vertex3(0, 0, 0);

                GL.TexCoord2(1, 0);
                GL.Vertex3(256, 0, 0);

                GL.TexCoord2(1, 1);
                GL.Vertex3(256, 256, 0);

                GL.TexCoord2(0, 1);
                GL.Vertex3(0, 256, 0);

                GL.End();

                GL.Disable(EnableCap.Texture2D);
                GL.PopMatrix();

                GL.MatrixMode(MatrixMode.Projection);
                GL.PopMatrix();

                GL.MatrixMode(MatrixMode.Modelview);
            }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Shatunov, 2022-01-23
@Re7r0

BitmapData data = bitmap2.LockBits(new System.Drawing.Rectangle(x, y, weight, height),
    ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);

This is where image memory comes into play. The pixel format in datawill be XRGB with a width of 32 bits.
Attention here should be paid to the fact that the format is specified as RGB32. This means that the alpha channel in the format is not represented in any way and, most likely, each pixel will be zero, because the place under it in the format is declared.
Further.
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0,
    OpenTK.Graphics.OpenGL.PixelFormat.Rgba, PixelType.UnsignedByte, data.Scan0);

PixelInternalFormat.Rgbameans that in GPU memory the texture will be represented in RGBA format floatwith normalized channels.
OpenTK.Graphics.OpenGL.PixelFormat.Rgbameans that the format data.Scan0should be perceived as RGBA, which PixelType.UnsignedBytemeans that the channels in data.Scan0should be perceived as unsigned byte.
There is a discrepancy between the format of the image read from the file and the format of the texture being created. And if the sizes and number of channels between formats are the same, then the interpretation of the channels themselves is not. XRGB != RGBA.
Everything needs to be brought to a single format. For example, to read the Format32bppArgb. But in this case, it is possible to make a mistake with the order of the channels between Format32bppArgband OpenTK.Graphics.OpenGL.PixelFormat.Rgba.
Still, as an option, you can initialize an unused channel in the source data.
In a word, after reading from the file, the future texture data must be adjusted to the desired texture format.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question