Public gPipes As CPipes 'the main class Public gCtl As glxCtl 'the ocxWe'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 = TrueThe 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.
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.
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.
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.