Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.NET PixelFormat to ES30 #53

Open
mludlum opened this issue Apr 24, 2021 · 4 comments
Open

.NET PixelFormat to ES30 #53

mludlum opened this issue Apr 24, 2021 · 4 comments

Comments

@mludlum
Copy link

mludlum commented Apr 24, 2021

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
Copy link
Member

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
Copy link
Author

mludlum commented Apr 26, 2021

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
Copy link
Member

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

@mludlum
Copy link
Author

mludlum commented Apr 26, 2021

GL ES please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants