Setting transparency color on sprite bitmap makes my non-sprite quads vanish!
Writing a simple game to help learn OpenGL. In the current version, my render method draws some geometry quads with code defined colors and then renders some sprites. So far, everything is normal and working as intended. My sprites have as their开发者_JS百科 background the color Auqua which I've been meaning to set to transparent, but haven't yet. So for the moment the game looks normal except for the fact that all sprites appear bounded by the color blue. I'm at the point where I want the blue to vanish and instead be transparent, so I modify my OnLoad method to be:
...
Bitmap bitmap = new Bitmap("RockTexture.bmp"); // existing line
bitmap.MakeTransparent(Color.Aqua); // new line
...
However, now when I run my game all the geometry quads vanish from the screen! My sprites do remain and appear appropriately transparent.
I'm not sure why this is happening. Here is my render method:
protected override void OnRenderFrame(FrameEventArgs e)
{
base.OnRenderFrame(e);
GL.Clear(ClearBufferMask.ColorBufferBit);
GL.Begin(BeginMode.Quads);
Color prevColor = default(Color);
foreach (var quad in _view.GeomitryList)
{
if (prevColor != quad.Color)
{
GL.Color4(quad.Color);
prevColor = quad.Color;
}
GL.Vertex2(quad.UpperLeftBound.X, quad.UpperLeftBound.Y);
GL.Vertex2(quad.LowerRightBound.X, quad.UpperLeftBound.Y);
GL.Vertex2(quad.LowerRightBound.X, quad.LowerRightBound.Y);
GL.Vertex2(quad.UpperLeftBound.X, quad.LowerRightBound.Y);
}
GL.Color4(_unitColor); // reset color multiplier to not scew textures
prevColor = _unitColor;
GL.BindTexture(TextureTarget.Texture2D, _rockTextureId);
foreach (var sprite in _view.SpriteList)
{
float ulX = sprite.UpperLeftBound.X;
float ulY = sprite.UpperLeftBound.Y;
float lrX = sprite.LowerRightBound.X;
float lrY = sprite.LowerRightBound.Y;
GL.TexCoord2(0, 0); GL.Vertex2(ulX, ulY);
GL.TexCoord2(1, 0); GL.Vertex2(lrX, ulY);
GL.TexCoord2(1, 1); GL.Vertex2(lrX, lrY);
GL.TexCoord2(0, 1); GL.Vertex2(ulX, lrY);
}
RenderDashboard(ref prevColor);
GL.End();
GL.Flush();
SwapBuffers();
}
Supposing you're setting the alpha channel in your MakeTransparent
function, you also need to enable blending:
protected override void OnRenderFrame(FrameEventArgs e) { base.OnRenderFrame(e);
GL.Clear(ClearBufferMask.ColorBufferBit);
GL.Begin(BeginMode.Quads);
Color prevColor = default(Color);
foreach (var quad in _view.GeomitryList)
{
if (prevColor != quad.Color)
{
GL.Color4(quad.Color);
prevColor = quad.Color;
}
GL.Vertex2(quad.UpperLeftBound.X, quad.UpperLeftBound.Y);
GL.Vertex2(quad.LowerRightBound.X, quad.UpperLeftBound.Y);
GL.Vertex2(quad.LowerRightBound.X, quad.LowerRightBound.Y);
GL.Vertex2(quad.UpperLeftBound.X, quad.LowerRightBound.Y);
}
vvv
GL.Enable(GL_BLEND);
^^^
GL.BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
GL.Color4(_unitColor); // reset color multiplier to not scew textures
prevColor = _unitColor;
GL.BindTexture(TextureTarget.Texture2D, _rockTextureId);
foreach (var sprite in _view.SpriteList)
{
float ulX = sprite.UpperLeftBound.X;
float ulY = sprite.UpperLeftBound.Y;
float lrX = sprite.LowerRightBound.X;
float lrY = sprite.LowerRightBound.Y;
GL.TexCoord2(0, 0); GL.Vertex2(ulX, ulY);
GL.TexCoord2(1, 0); GL.Vertex2(lrX, ulY);
GL.TexCoord2(1, 1); GL.Vertex2(lrX, lrY);
GL.TexCoord2(0, 1); GL.Vertex2(ulX, lrY);
}
GL.End(); // I think this should go here.
GL.Disable(GL_BLEND); // enable and disable on a as-needed base
RenderDashboard(ref prevColor);
GL.Flush();
SwapBuffers();
BTW: You're using immediate mode calls there (glBegin(...), glVertex(...), glEnd()), you should abandon them and use Vertex Arrays and Vertex Buffer Objects instead. Immediate mode is deprecated, no longer available in OpenGL-3 and later outside the compatibility profile.
精彩评论