I am writing a support class for the functionality of the sprite / texture atlas using C # with OpenTK. Most of the features still work fine (simple 2D tiles in the spelling image).
My problem is due to unexpected display results when calling the GDI + Bitmap.MakeTransparent () method to set the color (Magenta / 0xFFFF00FF) to use as a color key.
It would seem that I am using the wrong pixel format options for calls to bitmap.LockBits () and GL.TexImage2D (). My code was based on examples that really worked, but which had in common that the rectangle passed by LockBits () was for the whole image.
Challenges related to this process:
<!-- language: C# --> Bitmap bitmap = new Bitmap(filename); bitmap.MakeTransparent(Color.Magenta); GL.GenTextures(1, out texture_id); GL.BindTexture(TextureTarget.Texture2D, texture_id);
I tested a small demo using the above code on different boxes, and am observing these results:
- Window 7: magenta pixels act as transparent (desired result)
- Windows XP window: magenta pixels displayed as black
- Ubuntu Linux box: magenta pixels displayed as magenta.
This surprises me as I expected that (GDI + and OpenGL and OpenTK bindings) would act the same in different blocks.
To the extent that I have delved into the documentation of the GDI + API and OpenGL / OpenTK, I think that my perplexity is connected with these two points:
What is the correct way to call MakeTransparent () + LockBits () + GL.TexImage2D () to ensure the transparency of the specified color?
Why do I see strange display results (as if the "step" was not calculated correctly) for some combinations of pixel format parameters when LockBits () is called for a sub-rectangle, and not the whole image?
Update: I shortened my code in a small Github project: https://github.com/sglasby/OpenGL_Transparent_Sprite
Also, I came across a combination of parameters that works (arg 3 from LockBits () - Format32bppPArgb), although it is not clear why this works, given that the documentation implies a different pixel format: http://msdn.microsoft.com/en -us / library / 8517ckds.aspx (which says that the bitmap will be in Format32bppArgb after calling MakeTransparent).
source share