编写一个简单的游戏来帮助学习OpenGL。在当前版本中,我的渲染方法使用代码定义的颜色绘制一些几何四边形,然后渲染一些精灵。到目前为止,一切正常并按预期工作。我的精灵的背景是Auqua的颜色,我已经将其设置为透明,但还没有。因此,目前游戏看起来很正常,除了所有精灵都以蓝色为界。我正处于希望蓝色消失的位置,而是透明,所以我将OnLoad方法修改为:
...
Bitmap bitmap = new Bitmap("RockTexture.bmp"); // existing line
bitmap.MakeTransparent(Color.Aqua); // new line
...
但是,现在当我运行游戏时,所有几何四边形都会从屏幕上消失!我的精灵确实保持透明状态。
我不确定为什么会这样。这是我的渲染方法:
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();
}
答案 0 :(得分:0)
假设您在MakeTransparent
功能中设置了Alpha通道,您还需要启用混合:
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:你正在那里使用立即模式调用(glBegin(...),glVertex(...),glEnd()),你应该放弃它们并使用Vertex Arrays和Vertex Buffer Objects。立即模式已弃用,在兼容性配置文件之外的OpenGL-3及更高版本中不再可用。