【发布时间】:2014-08-26 11:45:40
【问题描述】:
我正在为在 C# 中使用 OpenTK 的游戏制作 2D 菜单。 目前,菜单分为 3 个不同的纹理四边形或“层”,看起来像 this。
第 1 层:按钮的基本外观。
第 2 层:鼠标悬停在“继续”按钮上时的外观。
第 3 层:保存按钮及其文本/名称的“齿轮”。
这些层中的每一层都包含一个绑定到 Quad 的半透明(32 位 .png)纹理。 仅绘制第 1 层和第 3 层时,纹理似乎工作正常,但当我也想显示第 2 层时,第 3 层从我的屏幕上消失,如 here 所示。在此图像中,仅绘制了我的基本按钮(第 1 层)和突出显示的“继续”按钮(第 2 层)。
我认为这是我的混合功能的问题,而且我正在绘制超过 2 个精灵。我对 OpenTK/OpenGL 比较陌生,我希望这里有人可以帮助我解决这个问题。我将在下面添加一些代码:
当游戏开始时,我设置了一些 GL 属性:
GL.Enable(EnableCap.DepthTest);
GL.Enable(EnableCap.CullFace);
GL.Enable(EnableCap.Texture2D);
GL.Enable(EnableCap.Blend); //I migh t be missing something here, like textEnv?
GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
这部分代码绘制了我的二维元素。我是从 OpenTK 的教程中获取的。
public void DrawHUD()
{
// Clear only the depth buffer, so that everything
// we draw for the HUD will appear in front of the
// world objects.
GL.Clear(ClearBufferMask.DepthBufferBit);
// Reset the ModelView matrix so the following
// objects are not affected by the camera position
GL.LoadIdentity();
////draw 3d hud elements (weapon in the main game)
// Disable lighting for the HUD graphics
// GL.Disable(EnableCap.Lighting);
// Save the current perspective projection
GL.MatrixMode(MatrixMode.Projection);
GL.PushMatrix();
// Switch to orthogonal view
GL.LoadIdentity();
GL.Ortho(0, Width, 0, Height, -1, 1); // Bottom-left corner pixel has coordinate (0, 0)
// Go back to working with the ModelView
GL.MatrixMode(MatrixMode.Modelview);
//// Draw the HUD elements
GameEngine.Draw2D();
// Switch back to perspective view
GL.MatrixMode(MatrixMode.Projection);
GL.PopMatrix();
GL.MatrixMode(MatrixMode.Modelview);
// Turn lighting back on
// GL.Enable(EnableCap.Lighting);
}
我将菜单“图层”添加到一个数组列表中,然后我用它来使用 Graphic 进行绘制,Graphic 是一个保存我的“图层”的位置/纹理信息的类
public virtual void Draw2D()
{
Vector2 pos;
int Z = 0; //to fight z-fighting? this fixed an issue where drawing a 2nd sprite would also make the 1st one dissapear partially
foreach (Graphic G in _2DList)
{
if (G.visible())
{
pos = G.position();
Renderer.DrawHUDSprite(pos.X, pos.Y, Z, G.W(), G.H(), G.Texture());
Z++;
}
}
}
最后,我的 hudsprites 绘制函数如下所示:
public static void DrawHUDSprite(float x, float y, float z, float width, float height, int textID)
{
// Save the ModelView matrix
GL.PushMatrix();
// Move to the correct location on screen
GL.Translate(x, y, z);
GL.BindTexture(TextureTarget.Texture2D, textID);
// Setup for drawing the texture
GL.Color3(Color.White);
// Draw a flat rectangle
GL.Begin(PrimitiveType.Quads);
GL.TexCoord2(0.0f, 1.0f); GL.Vertex3(0, 0, 0);
GL.TexCoord2(1.0f, 1.0f); GL.Vertex3(width, 0, 0);
GL.TexCoord2(1.0f, 0.0f); GL.Vertex3(width, height, 0);
GL.TexCoord2(0.0f, 0.0f); GL.Vertex3(0, height, 0);
GL.End();
// Restore the ModelView matrix
GL.BindTexture(TextureTarget.Texture2D, 0);
GL.PopMatrix();
}
如果这与我加载纹理的方式有关,我也会为此添加代码。
【问题讨论】:
-
我设法解决了这个问题!