在opentk中绘制的四边形,线条,点不会显示正确

时间:2014-01-22 17:30:49

标签: c# opengl opentk

我已经在我的2D游戏引擎中使用OpenTK绘制精灵了。我唯一的问题是自定义绘制的对象与opengl(除了精灵之外的任何东西)都显示为背景颜色。示例:

我在这里画了一条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并在绘制线后启用它,它会显示正确的颜色,但我必须将它混合。

1 个答案:

答案 0 :(得分:0)

我忘了在纹理绘制方法中取消绑定纹理......

GL.BindTexture(TextureTarget.Texture2D, 0);