Skip to content

GL \ OpenGL.GlControl (System.Windows.Forms)

Luca Piccioni edited this page Jan 19, 2018 · 6 revisions

The OpenGL.GlControl

OpenGL.Net comes with a System.Windows.Forms.UserControl control that allows drawing using OpenGL.Net without much hassle. You need to just design the control instance in your application, and handle the relevant events to manage the drawing operations. The control supports WGL and GLX platform backends.

Designer view image.

Context attributes

Since the GlControl creates an OpenGL context at handle creation time, there are designer properties that control the context creation.

In the case the driver implements OpenGL 3.1 (or the WGL_ARB_create_context extension), GlControl can setup the OpenGL context attributes; this will allow you to:

  • optionally request a specific OpenGL version (i.e. 3.3);
  • setup debug and forward compatibility context flags;
  • request a specific OpenGL profile (only if WGL_ARB_create_context_profile extension is supported)
    • Core profile
    • Compatibility profile
  • request a robust context (only if WGL_ARB_create_context_robustness extension is supported)

In the case the driver does not support the minimum requirements, it creates a standard context.

Framebuffer attributes

The pixel format is essential in the application initialization. The GlControl allow the definition of the minimum requirements of the buffers. The properties are:

  • Color bits: it defaults to 24. To accept any color depth set to 0.
  • Depth bits: it defaults to 0 (no depth buffer). Set to a higher value in the case you need a depth buffer.
  • Stencil bits: it defaults to 0 (no stencil buffer). Set to a higher value in the case you need stencil buffer.
  • Multisample bits: it defaults to 0. Set to a higher value in the case you need a multisample buffer. This value is considered only if GL_ARB_multisample is supported.
  • Double buffering: it defaults to true. It is recommended to keep the default value.
  • Swap interval: setup vertical sync. If _(WGL|GLX)swap_interval is supported, set to 1 to enable V-Sync or set to 0 to disable V-Sync; higher values are allowed. If the _(WGL|GLX)swap_interval_tear is supported, you can set to -1 to enable adaptive V-Sync; if it is not supported, V-Sync will result enabled.

Drawing and Animation

The Render event is raised whenever the GlControl invokes the Paint event; this event is raised only when necessary, and it is determined automatically by the hosting windowing system. The application can force the Render event by calling the Invalidate method.

If you need a continuous animation, you can set the Animation property to true (it is available in designer too); by default its value is false. If the AnimationTime property is zero (its default value), the control will update itself continuously, keeping into account the Swap interval property described previously. If you plan to use continuous animation with other Control instances, you need to set AnimationTimer to true.

Events

Registering delegates to the GlControl events, it is possible to allocate resources at GL context creation time, execute drawing commands and update application logic.

private void RenderControl_ContextCreated(object sender, GlControlEventArgs e)
{
	// Here you can allocate resources or initialize state
    Gl.MatrixMode(MatrixMode.Projection);
    Gl.LoadIdentity();
    Gl.Ortho(0.0, 1.0f, 0.0, 1.0, 0.0, 1.0);

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

private void RenderControl_Render(object sender, GlControlEventArgs e)
{
    Control senderControl = (Control)sender;

    Gl.Viewport(0, 0, senderControl.ClientSize.Width, senderControl.ClientSize.Height);
    Gl.Clear(ClearBufferMask.ColorBufferBit);

    Gl.Begin(PrimitiveType.Triangles);
    Gl.Color3(1.0f, 0.0f, 0.0f); Gl.Vertex2(0.0f, 0.0f);
    Gl.Color3(0.0f, 1.0f, 0.0f); Gl.Vertex2(0.5f, 1.0f);
    Gl.Color3(0.0f, 0.0f, 1.0f); Gl.Vertex2(1.0f, 0.0f);
    Gl.End();
}

private void RenderControl_ContextDestroying(object sender, GlControlEventArgs e)
{
    // Here you can dispose resources allocated in RenderControl_ContextCreated
}