opentk / LearnOpenTK

A port of learnopengl.com's tutorials to OpenTK and C#.
Creative Commons Attribution 4.0 International
463 stars 115 forks source link

.NET PixelFormat to ES30 #53

Open mludlum opened 3 years ago

mludlum commented 3 years ago

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);
NogginBops commented 3 years ago

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.

mludlum commented 3 years ago

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?

NogginBops commented 3 years ago

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

mludlum commented 3 years ago

GL ES please