Answer the question
In order to leave comments, you need to log in
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;
}
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
BitmapData data = bitmap2.LockBits(new System.Drawing.Rectangle(x, y, weight, height),
ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
data
will be XRGB with a width of 32 bits. GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0,
OpenTK.Graphics.OpenGL.PixelFormat.Rgba, PixelType.UnsignedByte, data.Scan0);
PixelInternalFormat.Rgba
means that in GPU memory the texture will be represented in RGBA format float
with normalized channels. OpenTK.Graphics.OpenGL.PixelFormat.Rgba
means that the format data.Scan0
should be perceived as RGBA, which PixelType.UnsignedByte
means that the channels in data.Scan0
should be perceived as unsigned byte
. XRGB != RGBA
. Format32bppArgb
. But in this case, it is possible to make a mistake with the order of the channels between Format32bppArgb
and OpenTK.Graphics.OpenGL.PixelFormat.Rgba
. Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question