Question

I've recently started using OpenTK for a simple game in C# However I can't seem to draw basic shapes like points i've tried a lot of things but i can't figure it out.

My drawing routine currently looks like this:

public static void RenderLoop(object Sender, OpenTK.FrameEventArgs Fea)
{
    OpenTK.Graphics.OpenGL.GL.Clear(OpenTK.Graphics.OpenGL.ClearBufferMask.ColorBufferBit | OpenTK.Graphics.OpenGL.ClearBufferMask.DepthBufferBit);

    OpenTK.Graphics.OpenGL.GL.Disable(OpenTK.Graphics.OpenGL.EnableCap.PointSmooth);
    OpenTK.Graphics.OpenGL.GL.Disable(OpenTK.Graphics.OpenGL.EnableCap.Blend);
    OpenTK.Graphics.OpenGL.GL.Disable(OpenTK.Graphics.OpenGL.EnableCap.DepthTest);
    OpenTK.Graphics.OpenGL.GL.Disable(OpenTK.Graphics.OpenGL.EnableCap.CullFace);

    OpenTK.Graphics.OpenGL.GL.Begin(OpenTK.Graphics.OpenGL.BeginMode.Points);
    OpenTK.Graphics.OpenGL.GL.Color3(1f, 1f, 1f);
    OpenTK.Graphics.OpenGL.GL.Vertex2(50, 50);
    OpenTK.Graphics.OpenGL.GL.End();


    OpenTK.Graphics.GraphicsContext.CurrentContext.SwapBuffers();
}

And the initialization is this:

    public static bool Init()
    {
        try
        {
            //1200,850

            GameWnd = new GameWindow(1200, 850, new GraphicsMode(new ColorFormat(32), 0), "OpenTK - OpenGL Renderer", GameWindowFlags.Default, DisplayDevice.Default,3,0,GraphicsContextFlags.Default);

            GL.Disable(EnableCap.DepthTest); //Disable Z-Buffer, 2D Rendering
            GL.Disable(EnableCap.CullFace);
            GL.Enable(EnableCap.Texture2D);
            GL.Enable(EnableCap.Blend);
            GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
            GL.PixelStore(PixelStoreParameter.UnpackAlignment, 1);
            GL.RenderMode(RenderingMode.Render);

            GL.MatrixMode(MatrixMode.Projection);

            return true;
        }
        catch (Exception ex)
        {
            ErrorHandler.Err(ex, "Renderer has failed to initialize");
            return false;
        }
    }
Was it helpful?

Solution

I think the problem is as Marcus implies; you're not setting up the projection matrix to anything useful (and not setting up the modelview matrix to anything at all). If left as the identity, (50, 50) will be outside of the view window. I suggest you look up the OpenTK mappings of glOrtho and glFrustum.

Supposing you have a width by height sized viewport, the following would set you up with a viewport where (0, 0) is the top left and (a, b) is a pixels to the right and b pixels down from there:

glOrthof(0, width, height, 0, 1, 10);

Though that puts the near clip plane at a depth of 1 from the viewer (and the far at a depth of 10), so you'd probably also want to do something like this at initialisation:

glMatrixMode(GL_MODELVIEW);
glTranslatef(0.0f, 0.0f, -5.5f);

To shift any future geometry that you specify in 2d to being exactly halfway between the two clip planes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top