Open GL Super Bible

Previous Table of Contents Next

Use and Operation of WaiteGL.OCX

The OpenGL OCX is named WaiteGL.OCX and is in this chapter’s subdirectory on the CD. Using this control is very easy. Follow the directions in this chapter to register the control and install it into your environment. Then just place the control on a form and start calling its methods as if they were OpenGL functions and commands.

Each command is named just as it is in the OpenGL API, but with the leading gl dropped. By naming your own control gl, your code will look very much like C code that uses OpenGL. See the VB and Delphi examples that follow for an example of this.

This OCX fires two events that you can catch from your application. The first is SetupRC, which is called the first time the OCX tries to paint its client area. At this point, the pixel format and rendering context has already been created and set for the control. Here you can set up your lighting, background colors, and so on. The second event is Render, which is called every time the control needs to be painted. By placing your rendering code here, you effectively draw in the client area with OpenGL.

There are a few other caveats to bear in mind as you use the OCX.

  Since you may wish to have more than one OpenGL control in your application, the OCX cannot assume that the rendering context for any specific control will always be current. Therefore, two methods—MakeCurrent and MakeNotCurrent—are provided. All your OpenGL calls must be placed within calls to these two functions; this includes within SetupRC and Render.
  Furthermore, you may always call the OpenGL API directly when you have made the rendering context for the OpenGL OCX current. You can do this for performance reasons, or in case new functions are added to later versions of OpenGL that aren’t included in the OCX’s method list. The source is also included, so if you have Visual C++ and the inclination, you can always make modifications in the control yourself.
  A 3-3-2 palette is created for this control and is realized every time the control fires the Render event. Trying to manipulate the palette yourself can produce unexpected results.
  Finally, the control window is double buffered, so you will always need to call the SwapBuffers method to see your image.

OpenGL Flags

It is impossible to make any use of the OpenGL functions and commands without access to the many special flags and state variables. Each of these flag’s values is available via a method by the same name as the flag itself. The method name is in lowercase, however; having them match exactly leads to problems with the real defines in the header files. Although it would make sense for some of these state variables to be implemented as properties, for others it wouldn’t. For consistency, then, the methods match the OpenGL API as closely as possible.

Although many functions have several variations, they are only implemented once as a method. This means functions such as

void glVertex2fv(const GLfloat *v  );

would be implemented as a method as

Vertex2(float x, float y)

A help file is included (WaiteGL.hlp) that contains all the methods defined for WaiteGL. They are organized by the three OpenGL libraries (gl, glu, and glaux), and for all the constant macro definitions. To use the help file, find the OpenGL function needed and then look for the WaiteGL OCX method for that function.

Now let’s examine the specifics of setting up an OpenGL-enabled program in the two most popular 4GL environments. The next section discusses Visual Basic. If you are using Delphi 2.0 (the 32bit version), you can skip to the following section.

Installing and Using WaiteGL from VB 4.0

To make use of WaiteGL.ocx, it must first be registered as an OCX by the operating system (Windows NT or Windows 95). Copy the .ocx file into your system directory and run the supplied ocxreg.exe program. In the command line argument, specify the .ocx filename and either install or uninstall. For example:

ocxreg.exe WaiteGL.ocx install

You will find this program (with source) provided on the CD under the subdirectory for this chapter.

Installing the Control

Once the control has been registered with the operating system, it must be installed into the Visual Basic tool palette. Select Tools from the main menu, then Custom Controls. Choose the Waite Group OpenGL OCX from the dialog as shown in Figure 23-1, and click OK. Now you can drag the OpenGL control onto your forms, and size and place it accordingly.

Figure 23-1  Installing the WaiteGL OCX for use in Visual Basic

A Visual Basic Example

For our VB example, we placed our OpenGL control on a form and named it gl. We have also placed a timer on the form with a time interval of 200 milliseconds. See Figure 23-2. You may notice that the control does not paint or erase its client area. This is because the drawing code must be written in Basic and placed in the Render event handler.

Figure 23-2  A VB form with the OpenGL OCX

As mentioned earlier, two events defined by the OCX must be supported in our code. One is the SetupRC event. You put code here that initializes the rendering context by setting up the initial viewing volume, setting the background, and perhaps drawing colors and any lighting definitions you may want. Listing 23-1 is the code for our rendering context setup. This code simply sets the background and drawing color along with the viewing volume.

Listing 23-1 Set up the rendering context from Visual Basic

Private Sub gl_SetupRC()
    Rem Make the rendering context current

    Rem Set the background color to black
    gl.ClearColor 0#, 0#, 0#, 1#

    Rem Establish the viewing volume
    gl.Ortho -100#, 100#, -100#, 100#, -100#, 100#

    Rem Set the drawing color, flush, and
    Rem make the rendering context not current
    gl.Color 0, 0, 255, 255
End Sub

Previous Table of Contents Next