AftPeakTank's picture

VB.net - GLControl - Implementation - 01

So lets describe the situation...

Objective

In a few words, we want an opengl window inside a form.
We want to create something like a 3d viewer, editor, cad or program with similar functionality.

Because to manage Opengl with all the things the user might do to the window is really painfull, we will use the GLControl.
Thankfully it exists in OpenTK and it will help us avoid a lot of errors.

For this part we will try to create a render window with a background color.

What we need before coding

I am coding in VB.net express. So if you dont own VS.pro no problem. Download VS express and especially VB.net for what i describe.

Then i suppose you can go through the simple steps of creating a new project. After that...

1. Create a new Class (it will be your rendering control). I name mine (aoGLControl).
2. Create in your main Form a Panel in the place you want the rendering control to be positioned (Panel_aoGLControl_host).
3. Put your references to OpenTK.

OpenTK
OpenTK.Combatibility
OpenTK.GLControl

The Rendering Control code

So in ation...

1. First you need to to import namespaces.

Imports OpenTK
Imports OpenTK.GLControl
Imports OpenTK.Platform
Imports OpenTK.Graphics.OpenGL

2. Then we will inherit the functionality of GLControl and add a checking value to see the control is loaded.

Inherits GLControl
 
Dim _STARTED As Boolean = False

3. Then we add a color property.

    ' Background Color
    Private glbackcolor As Color
    Public Property glBackColorProp() As Color
        Get
            Return glbackcolor
        End Get
        Set(ByVal value As Color)
            glbackcolor = value
            If _STARTED = True Then
                GL.ClearColor(glbackcolor)
                Me.Invalidate()
            End If
        End Set
    End Property

4. Now we have to manage some events.

The events to manage are:
Load
Paint
Resize

Private Sub aoGLControl_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        _STARTED = True
        GL.ClearColor(glbackcolor)
        SetupViewport()
    End Sub
 
    Private Sub aoGLControl_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        If Not _STARTED Then Return
        render()
    End Sub
 
  Private Sub aoGLControl_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        If Not _STARTED Then Return
        render()
    End Sub
 
    Private Sub ResizeGL()
        SetupViewport()
        Me.Invalidate()
    End Sub

In the Load event we make sure that the _Started boolean is set to true so we will not try to draw in the Paint Routine and get an error.
The SetupViewport() will setup the screen size, projection (ortho in our case) and coordinate system.
The render() will render will render our scene.

5. Then we create the Subs that are called in the Paint and Resize events

Private Sub SetupViewport()
        Dim w As Integer = Me.Width
        Dim h As Integer = Me.Height
        GL.MatrixMode(MatrixMode.Projection)
        GL.LoadIdentity()
        GL.Ortho(0, w, 0, h, -1, 1)
        GL.Viewport(0, 0, w, h)
    End Sub
 
    Private Sub render()
        ' Clear screen
        GL.Clear(ClearBufferMask.ColorBufferBit)
        GL.Clear(ClearBufferMask.DepthBufferBit)
 
        ' Here Pass the data of what it is to be drawn
        DrawLines()
 
        ' Draw it!!!!
        Me.SwapBuffers()
    End Sub

The DrawLines() will draw lines in our window.

6. Finally we create our DrawLines() sub that will hold all the data to be drawn.

    Private Sub DrawLines()
        GL.MatrixMode(MatrixMode.Modelview)
        GL.LoadIdentity()
 
        'Add Translations   : ex. GL.Translate(x, 0, 0)
 
 
        'Add Rotations      : ex. GL.Rotate(rotation, Vector3.UnitZ)
 
 
        'Add Colors         : ex. GL.Color3(Color.Yellow)
 
 
 
        ' Add Geometry with above Translation/Rotation/Color
        GL.Begin(BeginMode.Triangles)   'Depending on type of object
 
 
        ' Add Geometry      : ex. GL.Vertex2(10, 20)
        '                         GL.Vertex2(100, 20)
        '                         GL.Vertex2(100, 50)
 
 
        GL.End()
    End Sub

Currently it will draw nothing, but if you want, you can type the lines in the comment "Add Geometry" to see something when you run the project.

So this is the code for our new control. Now we will add code to the form. The main concept is as follows:

1. We create our custom control.
2. At the load of the form we call a custom initialisation for our control.
3. This will put the control in the panel we added in the form and it will change the color of the background so we will see something.

The Form code

Imports OpenTK
Imports OpenTK.GLControl
Imports OpenTK.Platform
Imports OpenTK.Graphics.OpenGL
 
Public Class Form1
    Dim aoGLControl1 As New aoGLControl
 
 
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ' Initialisation Calls
        InitialiseAoGLControl()
 
    End Sub
 
 
 
#Region "Initialisation Routines"
 
    Private Sub InitialiseAoGLControl()
        ' Set Colors
        aoGLControl1.glBackColorProp = Color.Black
 
        ' Set Widths
 
        ' Set LineTypes
 
        ' Add the Control
        Panel_aoGLControl_host.Controls.Add(aoGLControl1)
        aoGLControl1.Dock = DockStyle.Fill
    End Sub
 
#End Region
 
 
End Class

Run the Project and enjoy :)