Why is Gl.Ortho() throwing an exception and how do I fix it?

1

I am writing a C# Windows Form application. I am using OpenGL.Net, and OpenGL.Net Win Forms v0.5.2 from the NuGet Packages. I have added a glControl to my form. I am trying to just get it setup correctly before I get into anything interesting.

Here is my load event for the glControl

    private void glControl1_Load(object sender, EventArgs e)
    {
        //Initialize Here
        Gl.ClearColor(0.0f, 0.0f, 1.0f, 1.0f);
    }

Here is my render event for the glControl

    private void glControl1_Render(object sender, GlControlEventArgs e)
    {
        //Clear first
        Gl.Clear(ClearBufferMask.ColorBufferBit);

        Gl.MatrixMode(MatrixMode.Projection);
        Gl.PushMatrix();
        Gl.LoadIdentity();
        Gl.Ortho(0, glControl1.Width, 0, glControl1.Height, -1, 1);

        Gl.MatrixMode(MatrixMode.Modelview);
        Gl.PushMatrix();
        Gl.LoadIdentity();

        Gl.Enable(EnableCap.Texture2d);
        Gl.Enable(EnableCap.Blend);
        Gl.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);

        //Draw Here


        Gl.Disable(EnableCap.Blend);
        Gl.Disable(EnableCap.Texture2d);
        Gl.BindTexture(TextureTarget.Texture2d, 0);

        Gl.PopMatrix();
        Gl.MatrixMode(MatrixMode.Projection);
        Gl.PopMatrix();

        Gl.Finish();
    }

I am getting an exception from calling Gl.Ortho(). If I comment it out I don't encounter any runtime issues.

System.NullReferenceException occurred
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=OpenGL.Net
StackTrace:
at OpenGL.Gl.Ortho(Single l, Single r, Single b, Single t, Single n, Single f)
at AnimationTool.Form1.glControl1_Render(Object sender, GlControlEventArgs e) 
Project\AnimationTool\AnimationTool\Form1.cs:line 53
at OpenGL.GlControl.OnRender()
at OpenGL.GlControl.OnPaint(PaintEventArgs e)

I don't understand how I can be making openGL calls and only my Gl.Ortho() throws an exception. What is going on?

c#
opengl
windows-forms-designer
asked on Stack Overflow Aug 3, 2017 by Michael • edited Aug 5, 2017 by Luca

1 Answer

1

The actual call to that Gl.Ortho points to glOrthof, which is an OpenGL 1.0 ES command. Since you have desktop GL context no function is loaded, hence the NullReferenceException.

To solve you issue, just use the correct method overload:

Gl.Ortho(0.0, (double)glControl1.Width, 0.0, (double)glControl1.Height, -1.0, 1.0);

This issue is already explained in the project wiki.

answered on Stack Overflow Aug 5, 2017 by Luca

User contributions licensed under CC BY-SA 3.0