Porting GLUT samples - 1

This tutorial will walk through the process of creating a simple OpenGL app with the glxCtl ocx control and explain how to port a GLUT sample written in C to Visual Basic.

1. Start a new template project

The glxCtl package contains 2 projects - a standard exe project and an ActiveX control project. Together they provide the framework for an OpenGL application. After some standard configuration, the project will run and display an OpenGL window with a sphere drawn in the center. To port a GLUT sample written in C to Visual Basic, you will add code to the single class in the standard exe project.

To start a new project, create a new directory - in this case we'll call it 'Quad' - and copy the template project to the new directory. (The template project is in the 'ProjectX' folder.) You should have 4 files: 'glxMain.frm', 'glxCXXX.cls', 'glxConstants.bas', and 'ProjectX.vbp'. Open the project. Change the name of class from 'CXXX' to 'CQuad'. There are two references to the class (in the basic module and in the form) - you can search for 'CXXX' and replace it with 'CQuad'. Now add the ocx project to the project group by selecting 'File | AddProject...' and selecting 'glxCtl.vbp'. Draw a glxCtl control on frmMain. OK, save the group as 'Quad.vbg' and then run it. The form should open with a blue sphere in the center as in the illustration.


2. What's happening?

The code in Form_Load creates an instance of the CQuad class and then calls the ocx to initialize:
  Set gCtl = glxCtl1
  Set CX = New CQuad
  glxCtl1.Init
  glxCtl1.Animate = True
The control in turn fires 2 events telling the CXXX class to initialize, the first, 'Init', for pre-GL initialization, and the second, 'InitGL', after GL has been initialized. When it returns, the code in Form_Load starts the animation timer. In the ocx, each time the timer fires, the 'Render' function is called, which clears the screen and fires the 'Draw' event. The 'Draw', 'Resize', and other events are passed by the form to the main class 'CQuad'. The CQuad 'Draw' simply draws a sphere at this point.

The order of events is important here. The class must be created first, because it must respond to events from the ocx. The ocx must exist before you can set any properties. If you want to alter the default setup parameters of OpenGL (such as double-buffering), you must do so after the control is created but before GL is initialized. The ocx will fire an 'Init' to give you a chance to do so. It will then create an OpenGL rendering context and then fire the 'InitGL' event when it is ready. At this point you can isuue OpenGL commands. The Quad class does no pre-initialization, and in 'InitGL' it sets up the viewing and lighting parameters. When 'Init' returns, everything is set up. When the timer is started, the app will start looping through the display code in the 'Draw' function.

3. Where did the grid come from?

The following code in 'InitGL' enables the grid and the right mouse button:
    With gCtl
        .MouseRotate = True
        .Trackball.Animate = False
        .Grid = glxGridX
        .axis = glxXYZ
        .Pick = True
    End With
This code in 'Draw' causes the grid to be displayed:
    
    With gCtl
        .Trackball.Update
        If m_Grids Then .DrawGrids
    End With
The Trackball and Grid must be manually called in each draw cycle.


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




Please send suggestions, bug reports, and such to:



Home Page   |  Related Sites