WindowsDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


AddThis Social Bookmark Button

Using OpenGL with VB.NET
Pages: 1, 2

Next we override glDraw(). This is the place where we add code that actually draws something. Readers of this book, will recognize Example 1-1 from the "Red Book." Note how constants are used and when they're converted into UInt32.


Public Overrides Sub glDraw()
  GL.glClearColor(0.0, 0.0, 0.0, 0.0)
  GL.glClear( _
    Convert.ToUInt32( _
      GLConstants.GL_COLOR_BUFFER_BIT Or _
      GLConstants.GL_DEPTH_BUFFER_BIT))
  GL.glLoadIdentity()
  '
  ' Fun begins
  '
  GL.glColor3f(1.0, 1.0, 1.0)
  GL.glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0)
  GL.glBegin(Convert.ToUInt32(GLConstants.GL_POLYGON))
  GL.glVertex3f(0.25, 0.25, 0.0)
  GL.glVertex3f(0.75, 0.25, 0.0)
  GL.glVertex3f(0.75, 0.75, 0.0)
  GL.glVertex3f(0.25, 0.75, 0.0)
  GL.glEnd()
  GL.glFlush()
  '
End Sub

Then we override InitGLContext().


Protected Overrides Sub InitGLContext()
  GL.glShadeModel(Convert.ToUInt32(GLConstants.GL_SMOOTH))
  GL.glClearColor(0.0, 0.0, 0.0, 0.5)
  GL.glClearDepth(1.0)
  GL.glEnable(Convert.ToUInt32(GLConstants.GL_DEPTH_TEST))
  GL.glDepthFunc(Convert.ToUInt32(GLConstants.GL_LEQUAL))
  GL.glHint( _
    Convert.ToUInt32( _
       GLConstants.GL_PERSPECTIVE_CORRECTION_HINT), _
    Convert.ToUInt32(GLConstants.GL_NICEST))
End Sub

If we want to automatically resize the OpenGL viewport, we'll need to overload OnSizeChanged().


Protected Overloads Sub OnSizeChanged(ByVal e)
  Me.OnSizeChanged(e)
  Dim s As Size
  Dim aspect_ratio As Double
  s = Me.Size
  aspect_ratio = s.Width / s.Height
  GL.glMatrixMode(Convert.ToUInt32(GLConstants.GL_PROJECTION))
  GL.glLoadIdentity()
  GL.gluPerspective(45.0, aspect_ratio, 0.1, 100.0)
  GL.glMatrixMode(Convert.ToUInt32(GLConstants.GL_MODELVIEW))
  GL.glLoadIdentity()
End Sub

Finally, we need to add a keyboard event handler. This one watches the Escape key, but you can easily extend it to handle other keystrokes.


Protected Sub myView_OnKeyDown(ByVal Sender As Object, _
       ByVal kea As System.Windows.Forms.KeyEventArgs) _
                 Handles MyBase.KeyDown
  If (kea.KeyCode = Keys.Escape) Then
    Application.Exit()
  End If
End Sub

Well, that's it! Your first VB.NET OpenGL application looks now like this:

Imports CsGL.OpenGL
  Public Class Form1
      Inherits System.Windows.Forms.Form
      Private view As myOpenGL.myView
      Private thrOpenGL

  #Region " Windows Form Designer generated code "

    Public Sub New()
      MyBase.New()

      'This call is required by the Windows Form Designer.
      InitializeComponent()

      'Add any initialization after the InitializeComponent() call

      Me.view = New myOpenGL.myView()
      Me.view.Parent = Me
      Me.view.Dock = DockStyle.Fill
      Me.thrOpenGL = New Threading.Thread(AddressOf OpenGL_Start)
      Me.thrOpenGL.Start()

    End Sub

    Private Sub OpenGL_Start()
      While 1 = 1
        Me.view.Refresh()
      End While
    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose( _
                                 ByVal disposing As Boolean)
      If disposing Then
        If Not (components Is Nothing) Then
          components.Dispose()
        End If
      End If
      MyBase.Dispose(disposing)
      Me.thrOpenGL.Abort()
    End Sub

    'Required by the Windows Form Designer
    Private components As _
                    System.ComponentModel.IContainer

    'NOTE: The following procedure is required 
    'by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
      components = New System.ComponentModel.Container()
      Me.Text = "Form1"
    End Sub

  #End Region
  End Class

Namespace myOpenGL
  Public Class myView
      Inherits OpenGLControl
    Private Enum GLConstants
      GL_COLOR_BUFFER_BIT = &H4000
      GL_DEPTH_BUFFER_BIT = &H100
      GL_SMOOTH = &H1D01
      GL_DEPTH_TEST = &HB71
      GL_LEQUAL = &H203
      GL_PERSPECTIVE_CORRECTION_HINT = &HC50
      GL_NICEST = &H1102
      GL_PROJECTION = &H1701
      GL_MODELVIEW = &H1701
      GL_POLYGON = &H9
    End Enum

    Public Overrides Sub glDraw()
      GL.glClearColor(0.0, 0.0, 0.0, 0.0)
      GL.glClear( _
        Convert.ToUInt32( _
        GLConstants.GL_COLOR_BUFFER_BIT Or _ 
        GLConstants.GL_DEPTH_BUFFER_BIT))
      GL.glLoadIdentity()
      '
      ' Fun begins
      '
      GL.glColor3f(1.0, 1.0, 1.0)
      GL.glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0)
      GL.glBegin(Convert.ToUInt32(GLConstants.GL_POLYGON))
      GL.glVertex3f(0.25, 0.25, 0.0)
      GL.glVertex3f(0.75, 0.25, 0.0)
      GL.glVertex3f(0.75, 0.75, 0.0)
      GL.glVertex3f(0.25, 0.75, 0.0)
      GL.glEnd()
      GL.glFlush()
      '
    End Sub

    Protected Overrides Sub InitGLContext()
      GL.glShadeModel(Convert.ToUInt32(GLConstants.GL_SMOOTH))
      GL.glClearColor(0.0, 0.0, 0.0, 0.5)
      GL.glClearDepth(1.0)
      GL.glEnable(Convert.ToUInt32(GLConstants.GL_DEPTH_TEST))
      GL.glDepthFunc(Convert.ToUInt32(GLConstants.GL_LEQUAL))
      GL.glHint( _
        Convert.ToUInt32( _
          GLConstants.GL_PERSPECTIVE_CORRECTION_HINT), _
          Convert.ToUInt32(GLConstants.GL_NICEST))

    End Sub

    Protected Overloads Sub OnSizeChanged(ByVal e)
      Me.OnSizeChanged(e)
      Dim s As Size
      Dim aspect_ratio As Double
      s = Me.Size
      aspect_ratio = s.Width / s.Height
      GL.glMatrixMode(Convert.ToUInt32(GLConstants.GL_PROJECTION))
      GL.glLoadIdentity()
      GL.gluPerspective(45.0, aspect_ratio, 0.1, 100.0)
      GL.glMatrixMode(Convert.ToUInt32(GLConstants.GL_MODELVIEW))
      GL.glLoadIdentity()
    End Sub

    Protected Sub myView_OnKeyDown(ByVal Sender As Object, _
               ByVal kea As System.Windows.Forms.KeyEventArgs) _
               Handles MyBase.KeyDown
      If (kea.KeyCode = Keys.Escape) Then
        Application.Exit()
      End If
    End Sub
  End Class
End Namespace

Now, all you have to do is build this code (Ctrl+Shift+B) and run it with F5. What you should see is a white rectangle on a black background, as the "Red Book" describes it. For more information about which CsGL method does what and how to override or overload them, see the source of CsGL, available from the project's site.

Essential Resources

The best place to learn about CsGL is the project's site and mailing list. There you will find information about licensing, FAQs, and more.

As for OpenGL itself, you should not venture into writing OpenGL applications without the OpenGL Reference Manual (AKA the "Blue Book") and the OpenGL Programming Guide (AKA the "Red Book"). They are the ABCs of OpenGL. A few more titles are available, but these two should answer most of your questions. There are also quite a few online resources; I strongly recommend the famous NeHe tutorials. Links to more information for developers can be found on the official OpenGL site, in their developer section.

And if you are struggling with VB.NET or NET in general, have a look at O'Reilly's .NET section.

Best of luck hacking your OpenGL code in VB.NET!

Jacek Artymiak started his adventure with computers in 1986 with Sinclair ZX Spectrum. He's been using various commercial and Open Source Unix systems since 1991. Today, Jacek runs devGuide.net, writes and teaches about Open Source software and security, and tries to make things happen.


Return to ONDotnet.com