Silicon Graphics
(HTML edition information)

OpenGL Programming Guide


The Official Guide to Learning OpenGL, Release 1

OpenGL Architecture Review Board

Jackie Neider

Tom Davis

Mason Woo

Addison-Wesley Publishing Company

Reading, Massachusetts Menlo Park, CaliforniaNew York Don Mills, Ontario Wokingham, EnglandAmsterdam Bonn Sydney Singapore Tokyo MadridSan Juan Paris Seoul Milan Mexico City Taipei

Silicon Graphics, the Silicon Graphics logo, and IRIS are registered trademarks and OpenGL and IRIS Graphics Library are trademarks of Silicon Graphics, Inc. X Window System is a trademark of Massachusetts Institute of Technology. Display PostScript is a registered trademark of Adobe Systems Incorporated.

The authors and publishers have taken care in preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein.

Copyright © 1994 by Silicon Graphics, Inc.

All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording or otherwise, without the prior written permission of the publisher. Printed in the United States of America. Published simultaneously in Canada.

Authors: Jackie Neider, Tom Davis, and Mason Woo

Sponsoring Editor: David Rogelberg

Project Editor: Joanne Clapp Fullagar

Cover Image: Thad Beier

Cover Design: Jean Seal

Text Design: Electric Ink, Ltd., and Kay Maitz

Set in 10-point Stone Serif

ISBN 0-201-63274-8

First Printing, 1993

123456789-AL-9695949392


About This Guide

The OpenGL graphics system is a software interface to graphics hardware. (The GL stands for Graphics Library.) It allows you to create interactive programs that produce color images of moving three-dimensional objects. With OpenGL, you can control computer-graphics technology to produce realistic pictures or ones that depart from reality in imaginative ways. This guide explains how to program with the OpenGL graphics system to deliver the visual effect you want. 

What This Guide Contains

This guide has the ideal number of chapters: 13. The first six chapters present basic information that you need to understand to be able to draw a properly colored and lit three-dimensional object on the screen: The remaining chapters explain how to add sophisticated features to your three-dimensional scene. You might choose not to take advantage of many of these features until you're more comfortable with OpenGL. Particularly advanced topics are noted in the text where they occur. In addition, there are several appendices that you will likely find useful: Finally, an extensive Glossary defines the key terms used in this guide.

How to Obtain the Sample Code

This guide contains many sample programs to illustrate the use of particular OpenGL programming techniques. These programs make use of a small auxiliary library that was written for this guide. The section "OpenGL-related Libraries" gives more information about this auxiliary library. You can obtain the source code for both the sample programs and the auxiliary library for free via ftp (file-transfer protocol) if you have access to the Internet.

First, use ftp to go to the host sgigate.sgi.com, and use anonymous as your user name and your_name@machine as the password. Then type the following:

cd pub/opengl
binary
get opengl.tar.Z
bye
The file you receive is a compressed tar archive. To restore the files, type:
uncompress opengl.tar
tar xf opengl.tar
The sample programs and auxiliary library are created as subdirectories from wherever you are in the file directory structure.

Many implementations of OpenGL might also include the code samples and auxiliary library as part of the system. This source code is probably the best source for your implementation, because it might have been optimized for your system. Read your machine-specific OpenGL documentation to see where the code samples can be found. 


What You Should Know Before Reading This Guide

This guide assumes only that you know how to program in the C language and that you have some background in mathematics (geometry, trigonometry, linear algebra, calculus, and differential geometry). Even if you have little or no experience with computer-graphics technology, you should be able to follow most of the discussions in this book. Of course, computer graphics is a huge subject, so you may want to enrich your learning experience with supplemental reading: Once you begin programming with OpenGL, you might want to obtain the OpenGL Reference Manual by the OpenGL Architecture Review Board (Reading, Mass.: Addison-Wesley Publishing Co., 1993), which is designed as a companion volume to this guide. The Reference Manual provides a technical view of how OpenGL operates on data that describes a geometric object or an image to produce an image on the screen. It also contains full descriptions of each set of related OpenGL commands - the parameters used by the commands, the default values for those parameters, and what the commands accomplish.

"OpenGL" is really a hardware-independent specification of a programming interface. You use a particular implementation of it on a particular kind of hardware. This guide explains how to program with any OpenGL implementation. However, since implementations may vary slightly - in performance and in providing additional, optional features, for example - you might want to investigate whether supplementary documentation is available for the particular implementation you're using. In addition, you might have OpenGL-related utilities, toolkits, programming and debugging support, widgets, sample programs, and demos available to you with your system. 


Style Conventions

These style conventions are used in this guide: Code examples are set off from the text in a monospace font, and command summaries are shaded with gray boxes.

Topics that are particularly complicated - and that you can skip if you're new to OpenGL or computer graphics - are marked with the Advanced icon. This icon can apply to a single paragraph or to an entire section or chapter.

Advanced

Exercises that are left for the reader are marked with the Try This icon.

Try This


Acknowledgments

No book comes into being without the help of many people. Probably the largest debt the authors owe is to the creators of OpenGL itself. The OpenGL team at Silicon Graphics has been led by Kurt Akeley, Bill Glazier, Kipp Hickman, Phil Karlton, Mark Segal, Kevin P. Smith, and Wei Yen. The members of the OpenGL Architecture Review Board naturally need to be counted among the designers of OpenGL: Dick Coulter and John Dennis of Digital Equipment Corporation; Jim Bushnell and Linas Vepstas of International Business Machines, Corp.; Murali Sundaresan and Rick Hodgson of Intel; and On Lee and Chuck Whitmore of Microsoft. Other early contributors to the design of OpenGL include Raymond Drewry of Gain Technology, Inc., Fred Fisher of Digital Equipment Corporation, and Randi Rost of Kubota Pacific Computer, Inc. Many other Silicon Graphics employees helped refine the definition and functionality of OpenGL, including Momi Akeley, Allen Akin, Chris Frazier, Paul Ho, Simon Hui, Lesley Kalmin, Pierre Tardiff, and Jim Winget.

Many brave souls volunteered to review this book: Kurt Akeley, Gavin Bell, Sam Chen, Andrew Cherenson, Dan Fink, Beth Fryer, Gretchen Helms, David Marsland, Jeanne Rich, Mark Segal, Kevin P. Smith, and Josie Wernecke from Silicon Graphics; David Niguidula, Coalition of Essential Schools, Brown University; John Dennis and Andy Vesper, Digital Equipment Corporation; Chandrasekhar Narayanaswami and Linas Vepstas, International Business Machines, Corp.; Randi Rost, Kubota Pacific; On Lee, Microsoft Corp.; Dan Sears; Henry McGilton, Trilithon Software; and Paula Womak.

Assembling the set of colorplates was no mean feat. The sequence of plates based on the cover image (Figure J-1 through Figure J-9 ) was created by Thad Beier of Pacific Data Images, Seth Katz of Xaos Tools, Inc., and Mason Woo of Silicon Graphics. Figure J-10 through Figure J-32 are snapshots of programs created by Mason. Gavin Bell, Kevin Goldsmith, Linda Roy, and Mark Daly (all of Silicon Graphics) created the fly-through program used for Figure J-34 . The model for Figure J-35 was created by Barry Brouillette of Silicon Graphics; Doug Voorhies, also of Silicon Graphics, performed some image processing for the final image. Figure J-36 was created by John Rohlf and Michael Jones, both of Silicon Graphics. Figure J-37 was created by Carl Korobkin of Silicon Graphics. Figure J-38 is a snapshot from a program written by Gavin Bell with contributions from the Inventor team at Silicon Graphics - Alain Dumesny, Dave Immel, David Mott, Howard Look, Paul Isaacs, Paul Strauss, and Rikk Carey. Figure J-39 and Figure J-40 are snapshots from a visual simulation program created by the Silicon Graphics IRIS Performer team - Craig Phillips, John Rohlf, Sharon Fischler, Jim Helman, and Michael Jones - from a database produced for Silicon Graphics by Paradigm Simulation, Inc. Figure J-41 is a snapshot from skyfly, the precursor to Performer, which was created by John Rohlf, Sharon Fischler, and Ben Garlick, all of Silicon Graphics.

Several other people played special roles in creating this book. If we were to list other names as authors on the front of this book, Kurt Akeley and Mark Segal would be there, as honorary yeoman. They helped define the structure and goals of the book, provided key sections of material for it, reviewed it when everybody else was too tired of it to do so, and supplied that all-important humor and support throughout the process. Kay Maitz provided invaluable production and design assistance. Kathy Gochenour very generously created many of the illustrations for this book. Tanya Kucak copyedited the manuscript, in her usual thorough and professional style.

And now, each of the authors would like to take the 15 minutes that have been allotted to them by Andy Warhol to say thank you.

I'd like to thank my managers at Silicon Graphics - Dave Larson and Way Ting - and the members of my group - Patricia Creek, Arthur Evans, Beth Fryer, Jed Hartman, Ken Jones, Robert Reimann, Eve Stratton (aka Margaret-Anne Halse), John Stearns, and Josie Wernecke - for their support during this lengthy process. Last but surely not least, I want to thank those whose contributions toward this project are too deep and mysterious to elucidate: Yvonne Leach, Kathleen Lancaster, Caroline Rose, Cindy Kleinfeld, and my parents, Florence and Ferdinand Neider.

 - JLN

In addition to my parents, Edward and Irene Davis, I'd like to thank the people who taught me most of what I know about computers and computer graphics - Doug Engelbart and Jim Clark.

 - TRD

I'd like to thank the many past and current members of Silicon Graphics whose accommodation and enlightenment were essential to my contribution to this book: Gerald Anderson, Wendy Chin, Bert Fornaciari, Bill Glazier, Jill Huchital, Howard Look, Bill Mannel, David Marsland, Dave Orton, Linda Roy, Keith Seto, and Dave Shreiner. Very special thanks to Karrin Nicol and Leilani Gayles of SGI for their guidance throughout my career. I also bestow much gratitude to my teammates on the Stanford B ice hockey team for periods of glorious distraction throughout the writing of this book. Finally, I'd like to thank my family, especially my mother, Bo, and my late father, Henry.

 - MW