Skip to content

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

Luca Piccioni edited this page Mar 9, 2017 · 6 revisions

The OpenGL.GlControl

OpenGL.Net comes with a System.Windows.Forms.UserControl control that allow to draw using OpenGL.Net without much hassle. You need to just design the control instance in you application, and handle the relevant events to manage the drawing operations.

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
}

Context attributes

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

In the case the driver implements OpenGL 3.1 or the WGL_ARB_create_context extension the GlControl implementation allow the control of 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. Set to an higher value in the case you need depth buffer.
  • Stencil bits: it defaults to 0. Set to an higher value in the case you need stencil buffer.
  • Multisample bits: it defaults to 0. Set to an higher value in the case you need multisampled 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; in the case it is not supported, V-Sync will result enabled.
Clone this wiki locally