我已經有繪圖精靈在我的2D遊戲引擎中與OpenTK一起工作。我遇到的唯一問題是用opengl(除了精靈之外的任何東西)的自定義繪製對象顯示爲背景顏色。例如:繪製四邊形,線,opentk中的點不顯示正確
我在這裏繪製2.4f寬度的黑線。這個例子中還有一個四元組和一個點,但它們並不重疊任何實際可見的東西。該線與品紅色精靈重疊,但顏色錯誤。我的問題是:我錯過了OpenGL功能,還是做了一些可怕的錯誤?
這是我關於繪畫的項目的樣本:(你也可以找到關於https://github.com/Villermen/HatlessEngine的項目,如果有關於代碼的問題)
初始化:
Window = new GameWindow(windowSize.Width, windowSize.Height);
//OpenGL initialization
GL.Enable(EnableCap.PointSmooth);
GL.Hint(HintTarget.PointSmoothHint, HintMode.Nicest);
GL.Enable(EnableCap.LineSmooth);
GL.Hint(HintTarget.LineSmoothHint, HintMode.Nicest);
GL.Enable(EnableCap.Blend);
GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
GL.ClearColor(Color.Gray);
GL.Enable(EnableCap.Texture2D);
GL.Enable(EnableCap.DepthTest);
GL.DepthFunc(DepthFunction.Lequal);
GL.ClearDepth(1d);
GL.DepthRange(1d, 0d); //does not seem right, but it works (see it as duct-tape)
每次抽獎週期:
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
//reset depth and color to be consistent over multiple frames
DrawX.Depth = 0;
DrawX.DefaultColor = Color.Black;
foreach(View view in Resources.Views)
{
CurrentDrawArea = view.Area;
GL.Viewport((int)view.Viewport.Left * Window.Width, (int)view.Viewport.Top * Window.Height, (int)view.Viewport.Right * Window.Width, (int)view.Viewport.Bottom * Window.Height);
GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();
GL.Ortho(view.Area.Left, view.Area.Right, view.Area.Bottom, view.Area.Top, -1f, 1f);
GL.MatrixMode(MatrixMode.Modelview);
//drawing
foreach (LogicalObject obj in Resources.Objects)
{
//set view's coords for clipping?
obj.Draw();
}
}
GL.Flush();
Window.Context.SwapBuffers();
DrawX.Line:
public static void Line(PointF pos1, PointF pos2, Color color, float width = 1)
{
RectangleF lineRectangle = new RectangleF(pos1.X, pos1.Y, pos2.X - pos1.X, pos2.Y - pos1.Y);
if (lineRectangle.IntersectsWith(Game.CurrentDrawArea))
{
GL.LineWidth(width);
GL.Color3(color);
GL.Begin(PrimitiveType.Lines);
GL.Vertex3(pos1.X, pos1.Y, GLDepth);
GL.Vertex3(pos2.X, pos2.Y, GLDepth);
GL.End();
}
}
編輯:如果我之前禁用了blendcap,並在繪製完成後啓用它,它確實顯示出正確的顏色,但我必須將它混合。
愚蠢的問題:你檢查,你試圖使用的顏色是你真正想要的顏色? – GraphicsMuncher
是的,我甚至用函數GL.Color4替換函數中的顏色變量 – Villermen