Delphi™ Developer’s 
Guide to OpenGL




Jon Q. Jacobs


Wordware Publishing, Inc.

PAGE i
 Library of Congress Cataloging-in-Publication Data

Jacobs, Jon (Jon Q.)
Delphi developer’s guide to OpenGL / by Jon Jacobs.
  p. cm.
Includes index.
ISBN 1-55622-657-8 (pbk)
1. Computer graphics. 2. OpenGL. 3. Delphi (Computer file).
I. Title.
T385.J334  1998                 99-27203
006.6'6--dc21                     CIP



Copyright © 1999, Wordware Publishing, Inc.
All Rights Reserved
2320 Los Rios Boulevard
Plano, Texas 75074


No part of this book may be reproduced in any form or by
any means without permission in writing from
Wordware Publishing, Inc.

Printed in the United States of America



ISBN 1-55622-657-8
10 9 8 7 6 5 4 3 2 1
9906

Delphi is a trademark of Inprise Corporation.
Windows and Microsoft are registered trademarks of Microsoft Corporation.
Other product names mentioned are used for identification purposes only and may be trademarks of their respective 
companies.


All inquiries for volume purchases of this book should be addressed to Wordware Publishing, Inc., at the 
above address. Telephone inquiries may be made by calling:
(972) 423-0090

PAGE ii
 Contents


Introduction	xv
Chapter 1
The First Steps	1
Directory Structure	1
Preliminary Code	1
Getting Ready to Start to Begin	1
Device Context	2
Descriptor Fields	3
Starting to Begin	4
Rendering Context	4
Cleanup	4
Begin	5
Uses Clause	5
Paint	5
Error Checking	6
More Device Context	7
More Error Handling	8
Identifier Reference	10
Summary	12
Chapter 2
Basics	13
Viewport	13
Event Handler	13
Declaration	14
Analogy	15
Orthographic Projection	16
Meaning	16
Flexibility	17
Drawing	18
Command Placement	18
State Variables	18
Drawing Mode	20
Additional Considerations	23
Naming Convention	23
Error Handling in Depth	23
Order of Events	24

PAGE iii
 Matrices and Command Placement	26
Viewport Manipulation	26
Mixing Windows Components	27
Pushing the (X,Y,Z) Limits	28
Identifier Reference	31
Summary	32
Chapter 3
Introducing 3-D	33
Depth	33
Cube	35
Square Viewport	35
Distinguish Faces	35
Depth Test	37
Matrices Revisited	38
The ModelView Matrix	38
Rotation	39
Three Faces	39
glRotate	39
Purpose	39
Parameters	39
Direction	40
Action	40
Two-Faced Polygons	41
Open the Cube	41
Culling	43
Vertex Direction	43
Translation	45
Menu	45
Accumulation	45
More Clipping	45
Scale	46
Menu	46
Description	46
Order of Transformations	47
A New Interface	47
Simultaneous Commands	58
Identifier Reference	58
Summary	59
Chapter 4
Perspective	61
Illustration	61
Command	62
Clipping Volume	62
Parameters	63
Depth	63

PAGE iv
 Previous Cube	63
Test	64
Better Cube	65
Movement	67
Rotation	67
Translation	67
Multiple Objects	68
Matrix Stack	68
Reuse an Object	69
Make a Scene	70
Independent Movement	71
Translate	72
Rotate	73
Transformation Order Revisited	75
Another View	76
Identifier Reference	76
Summary	77
Chapter 5
Lighting	79
Enable Lighting	79
Ambient Light	80
Definition	80
Commands and Constants	80
The Easy Way	81
The Hard Way	82
Diffuse Light	85
Definition	85
Positional Lamps	86
Different Material Colors	89
Spotlights	91
Directional Spotlights	94
Emission	96
Definition	96
Implementation	96
Specular Light	98
Definition	98
Implementation	98
How Much Light?	100
Identifier Reference	101
Summary	102
Chapter 6
Components	105
Setup	105
OpenGL Component	105
Preliminary Types	106

PAGE v
 The Component Fields and Other Private Matters	107
The Public Section	107
The Published Section	108
Implementation	109
Start	109
Running	111
Finish	113
Registration	115
Transformation Component	116
Units	116
The Class Declaration	116
The Private Section	116
The Protected Section	117
The Public Section	117
The Published Section	117
Implementation	118
Material Component	119
Units	119
Preliminary Types	120
The Class Declaration	120
The Private Section	121
The Public Section	121
The Published Section	122
Variables	122
Implementation	123
Light Component	126
Units	126
The Class Declaration	127
The Private Section	127
The Public Section	128
The Published Section	128
Implementation	129
Path	134
Icons	134
Installing the Components	135
Delphi 2	136
Delphi 3, 4, Etc.	136
Summary	136
Chapter 7
Picking	137
Filling Out the Form	137
TOpenGL	137
TTransform	138
FormPaint	138
FormResize	139

PAGE vi
 TMaterial	140
Cube	140
More	142
Names	144
Numbers	144
Persistent State	144
Command	145
Stack	145
Coding for Names	145
Levels	146
Naming the Objects	146
Picking	147
The Challenge	147
Selection	148
Matrices	149
Get Mousey	151
Which Event	151
Collect the Data	152
Displaying the Data	155
Hierarchy	157
Identifier Reference	158
Summary	159
Chapter 8
Triangles	161
The Plane Problem	161
Building Shapes From Triangles	166
Triangle Fans	168
Order	168
Flat Fans	168
Winding and Faces	169
3-D Fans	173
Triangle Strips	178
Winding	178
Order	178
A Cube of Triangles	179
Shading	183
Curves	184
Cylinder	184
Cone	189
Dome	190
Highlights	191
Identifier Reference	195
Summary	196

PAGE vii
 Chapter 9
Animation	197
Mouse Movement	197
Properties	197
Picking	200
Fields and Methods	202
Action	207
Flicker	207
Double Buffer	208
Automatic Motion	208
Private	211
Idle	211
Methods	213
Frame Rates	214
Private	214
Initialize	214
Idle	215
Action	215
Independent Speed	215
Initialize	216
Idle	216
Action	217
Identifier Reference	217
Summary	217
Chapter 10
Effects	219
Transparency	219
The Project	219
Units	219
Components	220
Properties	220
Methods	223
Action	225
Enable	225
Glass	226
Fight the Jaggies	227
Antialiasing	227
Draw the Line	227
Smooth the Jagged Line	229
How Did it Do That?	230
Antialiasing Polygons	230
Points Also	231
Scaling	231
Homogeneous Coordinates	231
Opportunity	231
Blow up a Pyramid	231

PAGE viii
 Fog	236
Declarations	236
Project	237
Identifier Reference	243
Summary	244
Chapter 11
Textures	245
Complicated Surfaces	245
The Problem	245
The Solution	245
Texture Structure	246
Layout, Limits, and a Command	246
Make a Brick	248
Simple Texture, Simple Surface	250
Set Up Texturing	251
More Methods	253
Applying the Texture	253
Run	254
Stretch	255
Mapping Manner	255
Coordinates	256
Run	256
Replication	257
Two for One	257
Parameters	258
Coordinates	258
Run	258
A Brick Wall	260
Multiple Polygons	261
Triangle Strip Square	261
Still More Polygons	262
Windows Bitmaps as Textures	266
A Close One	266
The Project	266
The Differences	267
Run	269
Close, But No Cigar	269
Mipmapping	270
Lighting	275
Add Components	275
Alpha	278
Modulate	279
Identifier Reference	280
Summary	283

PAGE ix
 Chapter 12
Bitmaps	285
Structure	285
Project	285
Properties	285
Layout	286
Fill	287
New Commands	287
Event Handlers	288
Results	289
From Windows Bitmap	289
Monochrome Bitmaps	289
Project	290
Properties	290
Loading	292
Private	293
Event Handlers and Other Methods	294
Results	296
Make It Like the Others	297
Fonts	298
Project	298
New Commands	299
Properties	301
Mostly the Same	304
The Differences	304
Results	306
Interactive	306
Project	306
Properties	307
Private	309
Variables	310
Event Handlers	310
Other Methods	313
Result	316
With Texture	317
Project	317
Properties	317
Private	320
Variables	321
ReadBitmap	321
New Command	322
Event Handlers	322
Other Methods	326
Result	327
Identifier Reference	328
Summary	328

PAGE x
 Chapter 13
Drive-Through	329
Simple Scene	329
Project	329
Properties	330
Private	332
Variables	333
Event Handlers	334
Other Methods	337
Results	338
Opening	339
Project	339
Properties	339
Private	342
Event Handlers	343
Other Methods	344
Results	345
Textured Opening	346
Project	346
Other Differences	346
Copy Texture	346
Prepare to Read Texture	346
Initialize Texture	348
Apply the Texture	348
Result	349
Collision Detection	350
Project	350
Distance	350
Results	354
Textured Objects	355
Project	355
Properties	355
Methods and Other Routines	359
A Light Pole	359
An Array of Vertices	359
A Wall	360
Paint	363
Adjust the Adjust Method	364
Run	365
More	366
Globe	370
Skeleton	372
Stand Up	372
Skin	373
Earth	373
Go for a Spin	375

PAGE xi
 Who Turned Out the Lights?	377
Terrain	378
Project	378
Properties	378
Private	381
Constants, Types, Variables, and Functions	381
Setup and Shutdown	382
The Usual	383
FormPaint	383
TransformPaint	383
Idle	384
Adjust	384
Terrain Implementation	385
Run	387
A Display List	387
Prefiltered Data	388
A Bumpy Ride	389
Identifier Reference	391
Summary	393
Chapter 14
Feedback	395
Commands and Constants	395
Capture the Triangle	397
Project	397
Properties	398
Private	399
Initialize	400
Standard Stuff	400
Rendering	401
Keyboard	401
Feedback	402
Result	405
Go for Three	405
Get the Points	407
Project	407
Properties and Declarations	407
Rendering Code	408
Other Changes in the Methods	409
Run	409
The Formula	410
Bitmap	412
Project	412
Variables	412
Add a Function	412
Rendering Code	413

PAGE xii
 Other Changes in the Methods	414
Run	414
Lines	415
Project	415
Support More Tokens	415
Draw the Lines	416
Run	416
Break Up	417
Project	417
Properties	417
Rendering	417
Run	418
Identification	418
Project	418
New Component	418
Properties	419
New Token	419
Rendering	420
Run	420
Real Coordinates	421
Project	421
Calculate	421
Run	422
Color	423
Texture	423
Project	423
Properties	424
Load a Texture	425
Initializing and Finishing	426
Paint	427
Keyboard	427
Rendering	427
Floor	428
Light	428
Vertex Values	428
Run	429
Shadows	429
Project	429
Properties	430
Implementation	433
Initialize	434
Paint	434
New Keys	435
Rendering	437
Idle	438

PAGE xiii
 Run	438
Capture Data	440
Draw Shadow	442
Run Again	443
Identifier Reference	443
Summary	445
Chapter 15
The End—the Beginning	447
More Buffers	447
More Curves	447
More Effects	447
More Math	448
Matrix Math	448
Algebra	448
Esoterica	448
More Information	448
Kinds of Data	448
Geometry Data	449
Pixel Data	449
More Efficiency	449
More Resources	450
Appendix A
Definitions	451

Index	457


PAGE xiv
 Introduction
About OpenGL
What It Is and Is Not
OpenGL is an Open Graphics Library. Although Silicon Graphics originally developed it for 
one of its own graphics workstations, it is not a proprietary system for a single platform. 
OpenGL is now an industry standardized library of graphics commands that enables 
programs to produce anything from simple lines to 3-D animation. The OpenGL 
Architecture Review Board works to ensure that each implementation complies with the 
library’s defined standard.
OpenGL is strictly about graphics. Unlike DirectX (for Windows), it has nothing to do with 
sound, joysticks, etc. It does one thing—graphics—and it does that one thing very well. It is 
not a resource hog; its two Windows libraries consume less than one megabyte of disk space. 
Although OpenGL gives the graphics programmer a rich and powerful set of commands, it 
provides a clean and simple application programming interface (API). Just the little bit of 
programming work introduced in Chapter 1 is sufficient to produce some graphics effects. 
Most of that work is simply getting Windows to work with OpenGL. Very little effort with 
OpenGL commands themselves is required to produce some basic graphics.
The Windows Implementation
In Windows, OpenGL is implemented in opengl32.dll for the main graphics commands and 
glu32.dll for the utility library. As those filenames imply, OpenGL is only available for 32-bit 
versions of Windows, such as NT and 95 or later. Windows NT, Windows 98, and OEM 
versions of Windows 95 (pre-installed by the computer manufacturer) include the OpenGL 
libraries, but a separately purchased copy of Windows 95 may require downloading the two 
DLLs from the Internet. Alternatively, you may obtain modules from the appropriate 
vendors that take advantage of hardware support in newer video cards. Such hardware-
supporting libraries may have names slightly different from the ones provided by Microsoft.

PAGE xv
 The two dynamic-link libraries (DLLs) contain a powerful set of routines by which one can 
produce truly impressive three-dimensional graphics scenes and animations, especially with 
16-bit graphics or better. The Windows implementation can work with 8-bit palettes, but that 
requires extra coding and has severe functionality limitations. For now, set the system to 
work with high color (16-bit) or true color (24-bit or 32-bit).
About the Reader
The intended audience for this book is the Delphi developer interested in graphics 
programming. While the reader does not require expert-level knowledge to benefit from this 
book, he or she should be familiar with Delphi’s event-driven model, with Object Pascal’s 
Unit structure, and with object-oriented programming. The reader should also be 
comfortable with Windows programming in general.
Not only should the reader be experienced in using the above programming concepts, but he 
or she should also be familiar enough with Delphi’s IDE (integrated development 
environment) to implement those concepts. The book, without detailed explanation, asks the 
reader to perform such tasks as generating specific event handlers or saving a project to a 
new location with a different name.
The reader also needs to know enough about the user interface for a suitable 32-bit version 
of Windows to be able to set the color depth to 16 or 24 bits. Of course, the reader must have 
direct access to Windows NT or 95 or later, as well as to a 32-bit version of Delphi.
Finally, the reader needs to have an interest in graphics programming. That’s it! The reader 
does not need to start out with much graphics knowledge, just an interest.
If you have read this far without cringing, this book must be for you. No longer are you (the 
reader) treated as if you were off in another room being discussed behind your back. 
Congratulations! Welcome to the wonderful world of graphics programming with Delphi 
and OpenGL.
About Delphi
OpenGL produces various graphic images in a window, which in Delphi is a form. The place 
to perform the OpenGL commands is Delphi event handlers. Since the Windows version of 
OpenGL is a 32-bit DLL, use at least Delphi 2. Finally, Delphi 3 and 4 bring a required 
feature: They come with an interface unit for OpenGL and its utility library. Using OpenGL 
with 

PAGE xvi
 Delphi requires a 32-bit version of Delphi, an OpenGL interface unit, and a working 
knowledge of developing Delphi applications.
The Interface Unit
If your version of Delphi does not have an OpenGL interface unit, you can download one 
from the Internet. Some links are available on the companion CD. Wherever you obtain the 
interface unit, simply reference it in the uses clause of any unit that invokes OpenGL 
commands. Be sure to place the .DCU (Delphi Compiled Unit) file in Delphi’s library path; 
otherwise give a full path to it in the .DPR (Delphi PRoject) file. For example, in Delphi 3, the 
interface file is in the LIB directory under the DELPHI 3 directory. To see or modify the 
library path, go to the Library tab of the Environment Options window.
Roll Your Own Interface
Actually, this book provides enough information about the OpenGL commands to construct 
an interface unit. A sample format of an interface unit follows. Notice the call in the 
initialization section that disables floating-point exceptions. Keep in mind that Silicon 
Graphics, Inc., holds the copyright to the OpenGL libraries, and the required unit is an 
interface to its libraries.

unit OpenGL;

interface

uses
  Windows;

const
  GL_COLOR_BUFFER_BIT = $4000;
Type
  HGLRC = THandle;
  GLenum = cardinal;
  GLbitfield = cardinal;
  GLfloat = single;
  GLclampf = single;

{regular commands}
procedure glClear(mask:GLbitfield); stdcall;
procedure glClearColor(red,green,blue,alpha:GLclampf); stdcall;
function  glGetError: GLenum; stdcall;

{utility commands}



PAGE xvii
 implementation

{regular commands}
procedure glClear; external opengl32;
procedure glClearColor; external opengl32;
function glGetError; external opengl32;

{utility commands}

initialization 
  Set8087CW($133F);
end.
About This Book
Style Convention
The typestyle used in this book is simple and straightforward. Bold type, larger font sizes, 
and capitalization indicate levels of organization of the topics in a fairly obvious way. Italics 
introduce new terms that are defined in Appendix A. Code examples and listings use a 
monospaced font, with certain key words, such as procedure, in boldface in a manner 
similar to the syntax highlighting used in the Delphi editor. The interface example above 
illustrates these techniques.
Content
This is a hands-on, learn-by-doing book. For greatest benefit from the book, place it near the 
computer, turn on the computer, and launch Delphi. Create a separate directory or folder in 
a suitable location for placing various OpenGL projects. Naming the directory OpenGL is a 
good choice.
The companion CD-ROM contains the source code for all the example programs, along with 
the text of the book and Internet links. Please see the last page of this book for more 
information.
Some people may prefer to develop a thorough background before they write their first line 
of code. I am not one of those people. While I do like to have a good understanding of the 
tools I am using and the tasks I am (hopefully) accomplishing, I also like to dive in and start 
doing something! I wrote this book the way someone like me would like to see books 
written.
Code writing begins as soon as possible, and explanation and theory appear as needed, 
building the concepts a little at a time. Each step has just enough explanation to use the 
program code under development, without a dumping of the whole load at once. Each place 
in the book that uses 

PAGE xviii
 a new OpenGL command or other identifier gives its precise declaration as well as an 
explanation of the way to use it in the given context. In addition, a reference section at the 
end of each chapter lists all newly introduced OpenGL identifiers alphabetically with a brief 
description.
This book generally takes each concept from the simple to the complex, making the basic 
steps obvious before obscuring them with a lot of detail. Do not be alarmed when some 
portions of code do not work very well. Sometimes the best way to show the reason for 
doing something a certain way is to take the direct approach first and, by the results, 
demonstrate why it must be a little different.
In short, this is a learning adventure, so fasten your seat belts, launch Delphi, and start!

PAGE xix
 





To my wife, Jill, who suffered through widowhood 
and my son, Nathan, who bravely endured being 
an orphan while this book was in creation.

PAGE xx


