Pipes - 1

This tutorial will show you how to create a VB OpenGL app using the glxCtl.ocx. It will also give you an overview of how the Pipes screen saver works and an idea of how to go about porting a fairly large C OpenGL app to Visual Basic. For this example, will we not use the template project, but build the project from scratch.

1. Start a new VB project.

Select 'Project | References', then check 'VBOpenGL API 1.2'. Select 'Add Project' and add the 'glxCtl.vbp'. Change the form's name to 'frmMain', then save everything.

2. Add the ocx.

Place a glxCtl on the form. Draw it anywhere. Then open the code window for the form, and look at the events the glxCtl fires. The only ones of importance here are the 'Init', 'InitGL', 'Draw', and 'Resize'. We are going to create a class to handle these events.

3. Create a class to handle ocx events.

Create a new class, we'll call it 'CPipes'. Add the following procedures to the CPipes class: 'Init', 'InitGL', 'Resize', 'Draw'. Make 'Resize' a function returning a Boolean, and the others Subs. We'll need a couple of modules, so create them now,too. Call one 'mConstants' and the other 'mMain'.

4. Create a sub 'Main'.

In the 'mMain' module, add a 'Sub Main', then select 'Project | Properties' and change the 'Startup form' to 'Sub Main'.

We'll put all the global variables in mMain, so add the following lines at the top of the file:
  Public gPipes As CPipes 'the main class
  Public gCtl As glxCtl   'the ocx
We'll use the Main sub to coordinate the creation and initialization of the objects in the app. Add the following code to the Main sub:
  If App.PrevInstance Then End
  Set gPipes = New CPipes
  Load frmMain
  Set gCtl = frmMain.glxCtl1
  gCtl.Init
  frmPipes.Show
  gCtl.Animate = True
The order of events is important here. We preload the form so the ocx will be created and we can call its utility functions (such as FillArray), but we can't call any GL functions until after the control is initialized. The CPipes class has 2 initializations - the first (Init) is for pre-GL stuff. When gCtl.Init is called, the ocx fires 'Init', then sets up the GL environment and then fires 'InitGL'. The CPipes class will do any GL initialization there.

5. Wire the ocx events to the main class.

We can now wire the ocx to the class by adding the following code to frmMain:
Private Sub Form_Resize()
glxCtl1.Move 0, 0, ScaleWidth, ScaleHeight
End Sub

Private Sub glxCtl1_InitGL()
gPipes.InitGL
End Sub

Private Sub glxCtl1_Update()
gPipes.Draw
End Sub

Private Sub glxCtl1_Resize(Width As Long, _
        Height As Long, Default As Boolean)
Default = gPipes.Resize
End Sub
Ok, save everything and run it. You should see a black window in the client space of frmMain. This is the OpenGL window. If you see a grey window, then the GL setup in the ocx has probably failed. Examine the m_SetupGL routine in the ocx.

5. Draw something to test the setup.

Before we go on, let's draw something and see where we are. In CPipes, add a variable dec:
Dim q&
Then add this code to 'InitGL':
glClearColor 0.3, 0.3, 0.3, 1
With gCtl.Camera
    .NearPlane = 0.01
    .FarPlane = 200
    .fieldOfView = 60
    .SetEyePos -10, 10, 50
End With
With gCtl.Lights(liLight0)
    .SetAmbient 0.5, 0.3, 0.2
    .Mode = lmPoint
    .SetPosition 0, 100, 0
    .Enabled = True
End With
With gCtl.Lights(liLight1)
    .SetDiffuse 0.5, 0.5, 0.8
    .SetPosition 100, 10, 10
    .Enabled = True
End With
gCtl.SetWorldSize 40, 40, 40
gCtl.Grid = glxGridXYZ
gCtl.GridStep = 5
q = gluNewQuadric 'very important! VB will crash if you forget this!
and add this code to 'Draw':
    gCtl.DrawGrids
    gluSphere q, 1, 16, 16
Last, return 'True' from Reshape.

The code in 'InitGL' sets up the viewing parameters and turns on 2 lights. Then it sets up the grid which the ocx draws to aid in debugging.
The code in 'Draw' sets the viewpoint, draws the grids, then draws a sphere at the origin. The result should look something like this:

More information:

This tutorial is not intented to be a general OpenGL tutorial or even a VB5 tutorial. It shows how to get a moderately complex collection of C code (the SSPipes screen saver sample) up and running in VB, but doesn't delve into the problems of translating C code or address the question of how OpenGL code is best structured in VB. The translations used are more or less literal transcriptions of the original C code when possible, with changes introduced to simplify the code or deal with C-VB incompatibilities. Many features of the original code were eliminated, such as palette and Unicode support and even the screen saver functionality.

Some good books on programming OpenGL in Windows include:

OpenGL SuperBible - Richard Wright and Michael Sweet

Power 3D - Kyle Lussier

OpenGL in Win32 - Ron Fosner.

You will want to get the Red Book (OpenGL Programming Guide) as well.

If you have problems getting the ocx to work, you may want to compare the GL setup code in the ocx to that in the 'Common' SDK sample. The SDK sample uses a more general technique for selecting pixel formats.



<< Previous ........ Next >>




Please send suggestions, bug reports, and such to:



Home Page   |  Related Sites