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
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 11 of 11.
-
MDI application using OpenGL
2008-09-16 02:27:02 1269 [Reply | View]
HI, I need to develop a MDI application which uses OpenGL for drawings, where user can also open new windows using menus/toolbar etc. in other words I want to have an alternate to MVC architecture of VC++ or simply CView to use for OPenGL drawings, some other way to call active windows function to draw as we have OnDraw function in CView class. Any idea about it???
-
Eyeshot, the .NET OpenGL Component
2007-03-14 04:11:26 devDept [Reply | View]
Check out this brand new component from devDept Software:
Eyeshot, the .NET OpenGL Component
http://www.devdept.com/eyeshot
-
HELP!!!!
2004-03-18 13:34:55 ady1122 [Reply | View]
When i use glulookat in VB.net after writing the necessary code in the windows generated code.
An unhandled exception of type 'CsGL.OpenGL.OpenGLException' occurred in system.windows.forms.dll
Additional information: invalid operation
Can somebody help me out!!!!!
Cheers
Aditya
-
how can i create a child opengl window into a dialog form in vb .net
2003-11-15 19:29:20 anonymous2 [Reply | View]
hi.could u pls recommend me how can i create a child opengl window into a dialog form in vb .net.
-
Keyboard Event Handler Is Not Working Properly For Arrow Keys
2003-09-10 21:54:32 anonymous2 [Reply | View]
The codes given for keyboard event handler are fine for Escape key:
If (kea.KeyCode = Keys.Escape) Then
Application.Exit()
End If
However, if we change to use arrow keys instead of Escape key, i.e. replace the If-Then statement with either one of the below lines:
If (kea.KeyCode = Keys.Left) Then
If (kea.KeyCode = Keys.Right) Then
If (kea.KeyCode = Keys.Up) Then
If (kea.KeyCode = Keys.Down) Then
The keyboard handler is unable to catch the arrow keys.
Do anyone know why?
Regards,
Anonymous
-
Traps
2003-05-06 00:24:41 anonymous2 [Reply | View]
As an avid OpenGL AND C# (or VB.NET) programmer I would like to make two remarks:
1. Watch out for the 'OpenGL Thread'! When using a seperate thread things could go wrong: a) ALL the OpenGL code must be called on the SAME thread (OpenGL isn't multithreaded yet) and b) ALL the Window Form code must be called on the original (Form creating) thread.
2. (This is worse) the OnSizeChanged function calls itself... stack overflow is bound to follow; I guess (as it should) that the code had to be MyBase.OnSizeChanged(e) instead of Me.OnSizeChanged(e)
Happy Coding!
Erno
PS: I'm still wondering why CSGL changed to TAO... did it?
-
Traps
2003-05-06 20:28:59 anonymous2 [Reply | View]
>all the WIndow Form Code must be called on the original (Form Creating) thread
is this why I can't create 2 forms in Sub Main(), one a docked GL viewport and the other a panel of controls for it? if so, any tips? I want to create an MDI app with one form class a fully-docked GL renderer of the data being manipulated in the other forms...?
Total GL newbie here, not too good with Forms and threading either but I'll learn and that's the idea here.
thanks!
-
Forms
2003-05-07 03:17:34 anonymous2 [Reply | View]
Sure, you can create 2 Forms but you must be very carefull that the actual OpenGL calls are being done on the thread of the form with the OpenGL view. To do this follow the following procedures:
1. create functions on the view window that do the OpenGL calls
2. instead of calling these functions DIRECTLY from within the control window you should use the Invoke function from the view windows. (Look in the MSDN-help on Form.Invoke) This way the view form's thread will call the function!
Just a remark: I'm not to keen about scattering OpenGL functions thoughout my application. I prefer the Model-View-Controller design pattern. So I created a Renderer class that is called by the view window which does all the OpenGL work based upon data that describes the state of the models I want to show. The control window just changes that state. I really prefer OO programming AND design...
Hope this helps.
Regards,
Erno
-
Updated library...
2003-04-29 11:19:21 anonymous2 [Reply | View]
The successor to CsGL is available here:
http://www.randyridge.com/Tao/. It's also linked on the main CsGL page...
This updated version of CsGL has the additional benefits of being cross-runtime (Microsoft's and Mono), cross-platform, CLS compliant, API documentation, no extraneous native dll, additional API compliance and a few performance tweaks. The page also has a VB.NET sample (all of my examples are in C#) of the application presented here using the updated library. -
Updated library...
2004-01-13 14:50:42 anonymous2 [Reply | View]
Hi:
My name is Mauricio, I try to find some example of TAO (successor of CsGL) with VB .NET and I can't find anything!!!
I try the VB.NET-CsGL example and work fine but I need anything with TAO becouse CsGL is OUT!!
Please, please if anybody have some TAO VB .NET example please send me a mail to:
buho-1@entelchile.net
I post a message in the "TAO" web page but the site don't work vey well right now, maybe they are making some changes in the site.
For advance, thanks.





