0

I've got drawing sprites to work with OpenTK in my 2d game engine now. Only problem I'm having is that custom drawn objects with opengl (anything but sprites really) show up as the background color. Example:

I'm Drawing a 2.4f width black line here. There's also a quad and a point in the example, but they do not overlap anything that's actually visible. The line overlaps the magenta sprite, but the color is just wrong. My question is: Am I missing an OpenGL feature, or doing something horrible wrong?

These are the samples of my project concerning drawing: (you can also find the project on https://github.com/Villermen/HatlessEngine if there's questions about the code)

Initialization:

        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)

Every draw cycle:

        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();
        }
    }

Edit: If I disable the blendcap before and enable it after drawing the line it does show up with the right color, but I must have it blended.

genpfault
  • 51,148
  • 11
  • 85
  • 139
Villermen
  • 815
  • 2
  • 13
  • 28

1 Answers1

0

I forgot to unbind the texture in the texture-drawing method...

GL.BindTexture(TextureTarget.Texture2D, 0);
Villermen
  • 815
  • 2
  • 13
  • 28
  • Actually, this only makes the lines appear right. The quads and points are still invisible and need to have depthtest disabled to display... – Villermen Jan 23 '14 at 09:21