Introduction to OpenGL on OS/2

by Suzy Deffeyes

Have you ever wanted to program 3D function into your application? Suppose you are writing an application to design automobiles or airplanes. This application requires that you be able to animate and manipulate 3D images, and it might also be nice to have lighting, transparency, and texture mapping available in your application. You could have your car or plane rendered with particular material properties (usually cars and planes have very specular surfaces). You might also want smooth surfaces and realistic lighting.

Suppose you are doing some volume rendering of complex medical data, or mapping out oil reservoirs in a petroleum application. You might want to do an application for architects to walk through a designed building, with texture mapping on table tops, lights installed in the fixtures, and translucent drapes hanging at the windows. You could do all this using your own custom-built 3D rendering engine...but you don't have to.

OpenGL is an API toolset that lets you specify objects in three dimensions, set different pieces of OpenGL state, and generate realistic 3D rendered images at interactive speeds. OpenGL content is defined by the OpenGL Architectural Review Board (ARB), whose members include the following: The members of the ARB are currently finalizing the functions of OpenGL Version 1.1. Many companies in addition to the ARB members license the use of the OpenGL API, including Hewlett-Packard, Template Graphics, and Sun Microsystems. OpenGL is available (or is at beta level) on the following platforms: This Special Edition of the Developer Connection for OS/2 contains the release level of OpenGL Version 1.0. This means that you can develop applications based on OpenGL, and ship OpenGL binaries with your products. (Note: Please see the DevCon license agreement for redistribution instructions.) For PowerPC platforms, OpenGL is already included with the base operating system, and a development environment is included in the Developer's Toolkit for OS/2 Warp (PowerPC Edition). OpenGL may be included in the base OS/2 Warp product for x86 platforms in a future release.
 * Digital Equipment Corporation
 * Evans and Sutherland
 * IBM
 * Intel
 * Intergraph
 * Microsoft
 * Silicon Graphics, Inc.
 * Apple PowerMac
 * Most UNIX vendors
 * OS/2 Warp
 * Windows NT 3.5 and Windows 95

Why Should I Use OpenGL?
OpenGL is the industry standard for 3D graphics across a very wide range of workstations and PCs. It is a hardware-independent, vendor-neutral, windowing system-independent programming library. For 3D applications, that means your rendering code can be easily ported across many platforms and operating systems. All OpenGL implementations must pass strict conformance tests in order to use the OpenGL name. OpenGL on OS/2 passes all required conformance tests for 8- and 24-bit rendering; 16-bit rendering will be added in a future release.

OpenGL also provides you with extensive 3D rendering capabilities: Note: For more information, refer to the 3D and OpenGL programming books listed at the end of this article.
 * A wide assortment of graphical primitives can be drawn
 * Points, lines, segments, loops, triangles, quadrangles, triangle strips, and more
 * The utility library supports Non-Uniform Rational B Splines (NURBS) and complex polygons
 * Flexible data input
 * Object data can be specified in 2, 3, or 4 dimensions and numerous data formats
 * You can use immediate or display list mode
 * Complex light models
 * Up to 8 lights
 * Spotlights
 * Local or infinite lights
 * Complex material models
 * One or two sided
 * You can set ambient, diffuse, and specular components
 * Advanced texture mapping support
 * Automatic generation of texture coordinates
 * Two magnification filters
 * Six minification filters, including mipmapping
 * Decal, modulation, or blending of texture
 * Line and polygon anti-aliasing (removes "jaggies" or stair-steps along edges)
 * Fogging effects
 * Accumulation buffer that facilitates motion blur and full-scene anti-aliasing effects
 * Depth buffer and comparisons available for hidden surface removal
 * Alpha buffer and blending operations that facilitate transparency and compositing effects
 * Stencil buffer for Constructive Solid Geometry (CSG) modeling and shadows
 * Dithering that allows true color images to be drawn with a small palette
 * Double-buffering that allows for smooth animation

OpenGL grants developers and users access to graphical hardware acceleration when it is available. Currently many graphics board manufacturers are developing OpenGL accelerators for the PC market. OpenGL accelerators are common in the workstation market, and are making inroads into the PC market as their prices drop. Several board makers are currently working on OpenGL drivers for OS/2.

It is important to note that even in the absence of graphical hardware acceleration, today's newest processors (for example, the PowerPC and Intel's PentiumPro) can deliver usable OpenGL performance - actually better performance than workstations provided a few years ago.

OpenGL Performance Data
OpenGL benchmarking is controlled by the OpenGL Performance Characterization Committee (OPC). Currently, there is one OpenGL benchmarking tool that vendors are using to publish OpenGL performance data. OpenGL performance is directly proportional to the amount of hardware power you have. Complete software solutions running on a 486 system will run much more slowly than solutions that have hardware acceleration (such as the new SGI Indigo2 IMPACT). OS/2's software implementation is tuned for the PowerPC, but as you can see, performs quite well on modern processors with decent floating point performance.

Here are several recently published OpenGL benchmarks: CDRS-02    Approximate Composite   System Price Complete Software Solutions IBM OS/2 Warp on 90Mhz Pentium              1.27        3K IBM OS/2 Warp on 150Mhz PentiumPro          2.39        5K IBM OS/2 Warp on 120Mhz 604 PowerPC         2.37        4.5K Windows NT on 120Mhz Pentium                 .61        4.7K Windows NT on 100Mhz 604 PowerPC             .78        4K Windows 95 on 90Mhz Pentium                  .46        5K Hardware Solutions IBM GXT1000 AIX on 133Mhz 604              17.61        34K SGI Indigo2 High IMPACT                    20.37        35K Note: Composite number is a weighted geometric mean of several tests, and system price is provided by the vendor who submitted the results.

How Do I Learn OpenGL Programming?
OpenGL is a flexible, wonderfully powerful programming interface, and 3D graphics programming skills are highly sought after by many companies these days. These are both good reasons for you to do some OpenGL programming. The best way to learn OpenGL is to get the OpenGL Programming Guide and OpenGL Reference Manual (both are listed in the References). Read through the Programming Guide and try out the samples used in the book. They are included in the auxiliary library (AUX) demos we ship with OpenGL.

How Is OpenGL Implemented on OS/2?
As a software developer you're probably curious just how OpenGL works on OS/2. The OS/2 implementation is currently split into two .DLLs: OPENGL.DLL and RASTER.DLL. OPENGL.DLL contains the graphics utility library (GLU); the Presentation Manager GL (PGL, pronounced "piggle") calls (which are some OS/2-specific OpenGL APIs); the OpenGL API entry points; and the graphics pipeline that performs object transformation, lighting, culling, and clipping. Any program using OpenGL must link with OPENGL.DLL. This .DLL in turn calls RASTER.DLL, which contains the graphics rasterizer that performs the actual drawing of the graphics primitives. RASTER.DLL is device dependent, meaning that 3D graphics boards can supply their own RASTER.DLL. A vendor-supplied RASTER.DLL will offload pieces of OpenGL rasterization to specialized 3D graphics hardware. The RASTER.DLL supplied by this OpenGL implementation will work on all graphics cards that support the Direct Interface Video Extensions (DIVE).

When ancillary buffers (depth, alpha, stencil, accumulation) and multiple frame buffers are not available on the video board, they are allocated in main memory.

Because the rendering portion of the OpenGL API is intentionally windowing system-independent, a portion of the OpenGL API must deal with windowing-specific issues. On X Windows Systems, these calls and protocol are called GLX; on Windows NT and Windows 95, they are called WGL; on PowerMac, they are called AGL.

On OS/2, these calls make up the PGL (Presentation Manager GL) API. The PGL calls create and prepare an OpenGL context for rendering. They also control swapping of the front and back buffer, using OS/2 fonts in OpenGL, and synchronizing rendering streams.

The OpenGL API intentionally does not include window creation, colormap creation, event and input handling, or other functions that tend to vary between windowing systems. In order to do any OpenGL programming, you must use the PGL interface, or if you are using OpenGL for simple demos, you should use the AUX or GL utility library (GLUT) windowing toolkits. These toolkits provide simple windowing and event handling functions and are the same on all windowing systems. By recompiling, simple GLUT or AUX demo programs can run on a different platform.

Also, you can only have one current context and one current window at a time in a process. Additionally, contexts and windows cannot be shared across processes. If you create a context in one process and try to bind it to a window in another process, you'll get an error. You should structure your program to have one OpenGL rendering thread, and let other threads handle message processing.

Windows that are bound to OpenGL contexts must be created with a client window class (WinRegisterClass) that includes class style types CS_SIZEREDRAW and CS_MOVENOTIFY. If you create an OpenGL window as part of a USERDRAW area of a dialog box, it should also include CS_SIZEREDRAW and CS_MOVENOTIFY. When a window is bound to a context via pglMakeCurrent, you should not call WinSubclassWindow. You can, however, call WinSubclassWindow before you bind to the window.

If you are programming a GUI around an OpenGL window, remember that an OpenGL window does not get all 256 palette entries unless it is the window that has focus. For more information on palette management, read the section of documentation included in the OpenGL installation on this edition of the Developer Connection for OS/2.

Conclusion
OpenGL provides precise, high-quality 3D rendering function to developers. Code written using the OpenGL API is portable across all platforms that support OpenGL. OpenGL on OS/2 currently offers the best price performance for any OpenGL hardware and software solution under $15K.

OpenGL Web Sites

 * OpenGL official web page: http://www.sgi.com/Technology/openGL/opengl.html
 * OpenGL at IBM web page: http://www.austin.ibm.com/software/OpenGL
 * USENET news group: comp.graphics.api.opengl
 * OpenGL performance data: http://net1.uspro.fairfax.va.us/gpc
 * OpenGL on OS/2 ftp site (contains updates to OpenGL .DLLs): ftp://ps.boulder.ibm.com/rs6000/developer/os2/OpenGL
 * OpenGL Specification is available using anonymous ftp from sgi.com in /sgi/opengl/doc

About the Author
Suzy Deffeyes currently works for IBM Visual Systems in Austin, Texas. She is the development lead for OpenGL on OS/2. She also developed portions of the OpenGL extension to the AIX windows system. You can reach her at suzyq@austin.ibm.com.