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?

Luca
  • 10,886
  • 10
  • 64
  • 120
Michael
  • 23
  • 2
  • 1
    Be sure `glControl1` is set before passing it to Gl.Ortho(). – Ripi2 Aug 03 '17 at 15:23
  • @Ripi2: If the exception were from the property access `glControl1.Width` (or `Height`), then `gl.Ortho` wouldn't be on the call stack. – Ben Voigt Aug 03 '17 at 20:44
  • @Michael: Sometimes `GLControl` can cause `Paint` and `Render` callbacks before the OpenGL context is initialized. Trying to use OpenGL calls before initialization will fail miserably. Try setting a boolean flag (a good name would be `isGLReady`) in the `Load` event, and in `Paint` or `Render` just return immediately if that flag isn't set. – Ben Voigt Aug 03 '17 at 20:46
  • The other OpenGL calls work fine. In addition, I set break points on the glControl1's load function and that finishes before Render. Also, the width and height are valid and not what is causing the exception. – Michael Aug 04 '17 at 16:30

1 Answers1

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.

Luca
  • 10,886
  • 10
  • 64
  • 120
  • This trips me up _all the time._ I know that the `f`/`d`/`i` suffixes in the C API are seen as an undesirable necessity due to C's lack of overloading, but I kind of miss them in situations like this. In C# we have to resort to ugly casts or other tricks to ensure we're calling the correct version. :( – cdhowie May 13 '18 at 12:47
  • That's why the XML documentation (normally visible in editor) states the API relative to the method. Then, it is sufficient to use the correct type for variables or the relative suffix for literals. – Luca May 13 '18 at 17:39
  • Yeah I just wish they would've done something different. Like if the extension isn't available, delegate to the double overload -- they do the same thing anyway. – cdhowie May 13 '18 at 18:22