2017-08-03 75 views
0

我正在编写一个C#Windows窗体应用程序。我从NuGet软件包使用OpenGL.Net和OpenGL.Net Win Forms v0.5.2。我已经为我的表单添加了一个glControl。我想在进入任何有趣的事情之前正确设置它。为什么Gl.Ortho()抛出异常,我该如何解决它?

这里是我的glControl

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

负荷事件这是我的渲染事件的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(); 
    } 

我从打电话Gl.Ortho得到一个异常()。如果我将其注释掉,我不会遇到任何运行时问题。

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) 

我不明白我可以如何使openGL调用,只有我的Gl.Ortho()引发异常。到底是怎么回事?

+1

确保在将glControl1传递给Gl.Ortho()之前设置了“glControl1”。 – Ripi2

+0

@ Ripi2:如果异常来自属性访问'glControl1.Width'(或'Height'),那么'gl.Ortho'不会出现在调用堆栈中。 –

+0

@Michael:在OpenGL上下文初始化之前,''GLControl'有时会导致'Paint'和'Render'回调。尝试在初始化之前使用OpenGL调用将失败。尝试在Load事件中设置一个布尔标志(一个好名字是'isGLReady'),如果该标志没有设置,则在Paint或Render中立即返回。 –

回答

0

实际拨打Gl.Ortho指向glOrthof,这是一个OpenGL 1.0 ES命令。由于您有桌面GL上下文没有函数加载,因此NullReferenceException

为了解决你的问题,只要使用正确的方法重载:

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

这个问题在项目wiki已经解释。

相关问题