LearnOpenTK icon indicating copy to clipboard operation
LearnOpenTK copied to clipboard

.NET PixelFormat to ES30

Open mludlum opened this issue 4 years ago • 4 comments

How do I map System.Drawing.Imaging.PixelFormat to opentk.graphics.es30.pixelFormat? I can't find an example that uses ES30 when going from a call to Bitmap.LockBits to a call to GL.TexImage2D. I want my OpenTK code to work on mobile devices. I have a png that is a color gradient that I'm using to texture triangles using a shader with a texture. PixelFormat.Brga is not available, and the ES30 PixelFormat.Rgba doesn't seem to work.

TextureId = GL.GenTexture();
CheckError();
GL.BindTexture(TextureTarget.Texture2D, TextureId);
CheckError();

System.Drawing.Bitmap bmp = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile(TextureFilename);
System.Drawing.Imaging.BitmapData data = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height),
    System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);

GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
CheckError();
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);
CheckError();

GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
CheckError();
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
CheckError();
GL.PixelStore(PixelStoreParameter.PackAlignment, 1);
CheckError();

GL.TexImage2D(TextureTarget2d.Texture2D, 0, TextureComponentCount.Rgba, data.Width, data.Height, 0, 
    PixelFormat.Rgba, PixelType.UnsignedByte, data.Scan0);
CheckError();

GL.GenerateMipmap(TextureTarget.Texture2D);
CheckError();

GL.BindTexture(TextureTarget.Texture2D, 0);
CheckError();
bmp.UnlockBits(data);

mludlum avatar Apr 24 '21 14:04 mludlum

OpenGL ES 3.2 doesn't have a BGR format in it's standard. There is EXT_texture_format_BGRA8888 which gets you a BGR format in OpenGL ES, though I don't know how available it is.

If you don't want to use that extension you can swizzle the bytes yourself and hopefully the internal storage on GL ES devices is in RGB order so that the driver doesn't need to do unnecessary work swizzling the bytes.

NogginBops avatar Apr 26 '21 00:04 NogginBops

Thanks for the reply. Do you have a pairing for the last parameter of LockBits and the 3rd 7th parameters of TexImage2D that work for a transparent png?

mludlum avatar Apr 26 '21 17:04 mludlum

Are you looking for a GL ES answer or just a normal GL answer?

NogginBops avatar Apr 26 '21 20:04 NogginBops

GL ES please

mludlum avatar Apr 26 '21 21:04 mludlum