Main Computer Graphics Through OpenGL: From Theory to Experiments

Computer Graphics Through OpenGL: From Theory to Experiments

0 / 0
How much do you like this book?
What’s the quality of the file?
Download the book for quality assessment
What’s the quality of the downloaded files?
COMPREHENSIVE COVERAGE OF SHADERS AND THE PROGRAMMABLE PIPELINE



From geometric primitives to animation to 3D modeling to lighting, shading and texturing,Computer Graphics Through OpenGL(R) From Theory to Experimentsis a comprehensive introduction to computer graphics which uses an active learning style to teach key concepts. Equally emphasizing theory and practice, the book provides an understanding not only of the principles of 3D computer graphics, but also the use of the OpenGL(R) Application Programming Interface (API) to code 3D scenes and animation, including games and movies.

The undergraduate core of the book takes the student from zero knowledge of computer graphics to a mastery of the fundamental concepts with the ability to code applications using fourth-generation OpenGL(R). The remaining chapters explore more advanced topics, including the structure of curves and surfaces, applications of projective spaces and transformations and the implementation of graphics pipelines.

This book can be used for introductory undergraduate computer graphics courses over one to two semesters. The careful exposition style attempting to explain each concept in the simplest terms possible should appeal to the self-study student as well.

Features

- Covers the foundations of 3D computer graphics, including animation, visual techniques and 3D modeling
- Comprehensive coverage of OpenGL(R) 4.x, including the GLSL and vertex, fragment, tessellation and geometry shaders
- Includes 180 programs with 270 experiments based on them
- Contains 750 exercises, 110 worked examples, and 700 four-color illustrations
- Requires no previous knowledge of computer graphics
- Balances theory with programming practice using a hands-on interactive approach to explain the underlying concepts
Year:
2019
Edition:
3rd
Publisher:
CRC Press
Language:
english
Pages:
732 / 761
ISBN 10:
1138612642
ISBN 13:
9781138612648
File:
PDF, 291.01 MB
Download (pdf, 291.01 MB)

You may be interested in Powered by Rec2Me

 

Most frequently terms

 
0 comments
 

You can write a book review and share your experiences. Other readers will always be interested in your opinion of the books you've read. Whether you've loved the book or not, if you give your honest and detailed thoughts then people will find new books that are right for them.
1

His Favorites

Year:
2018
Language:
english
File:
EPUB, 1.96 MB
2

Here to Stay

Year:
2018
Language:
english
File:
EPUB, 3.76 MB


Computer Graphics Through OpenGL 
From Theory to Experiments

Third Edition



http://taylorandfrancis.com


Computer Graphics Through OpenGL
From Theory to Experiments

Third Edition

Sumanta Guha
Asian Institute of Technology, Thailand



CRC Press
Taylor & Francis Group
6000 Broken Sound Parkway NW, Suite 300
Boca Raton, FL 33487-2742

© 2019 by Taylor & Francis Group, LLC
CRC Press is an imprint of Taylor & Francis Group, an Informa business

No claim to original U.S. Government works

Printed on acid-free paper
Version Date: 20181022

International Standard Book Number-13:  978-1-138-61264-8 (Hardback)

This book contains information obtained from authentic and highly regarded sources. Reasonable efforts have been made to publish reliable data and 
information, but the author and publisher cannot assume responsibility for the validity of all materials or the consequences of their use. The authors and 
publishers have attempted to trace the copyright holders of all material reproduced in this publication and apologize to copyright holders if permission 
to publish in this form has not been obtained. If any copyright material has not been acknowledged please write and let us know so we may rectify in any 
future reprint.

Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or utilized in any form by any electronic, 
mechanical, or other means, now known or hereafter invented, including photocopying, microfilming, and recording, or in any information storage or 
retrieval system, without written permission from the publishers.

For permission to photocopy or use material electronically from this work, please access www.copyright.com (http://www.copyright.com/) or contact 
the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400. CCC is a not-for-profit organization that provides 
licenses and registration for a variety of users. For organizations that have been granted a photocopy license by t; he CCC, a separate system of payment 
has been arranged.

Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only for identification and explanation 
without intent to infringe.

Visit the Taylor & Francis Web site at
http://www.taylorandfrancis.com

and the CRC Press Web site at
http://www.crcpress.com

Library of Congress Cataloging-in-Publication Data

Names: Guha, Sumanta,
author.
Title: Computer graphics through openGL / Sumanta Guha.
Description: Third edition. | Boca Raton : Taylor & Francis, a CRC title,
part of the Taylor & Francis imprint, a member of the Taylor & Francis
Group, the academic division of T&F Informa, plc, 2018. | Includes
bibliographical references and index.
Identifiers: LCCN 2018027365 | ISBN 9781138612648 (hardback : acid-free paper)
Subjects: LCSH: Computer graphics. | Microcomputers--Programming. | OpenGL. |
BISAC: COMPUTERS / Computer Graphics. | MATHEMATICS / General.
Classification: LCC T385 .G85 2018 | DDC 006.6/633--dc23
LC record available at https://lccn.loc.gov/2018027365

Cover Designed by Sumanta Guha

www.copyright.com
http://www.copyright.com/) or
http://www.taylorandfrancis.com
http://www.crcpress.com
https://lccn.loc.gov/2018027365


To my parents Utpal Chandra and
Santa Guha and to Kamaladi



http://taylorandfrancis.com


Contents

PREFACE XVII

ABOUT THE AUTHOR XXVII

I Hello World 1

1 AN INVITATION TO COMPUTER GRAPHICS 3
1.1 Brief History of Computer Graphics . . . . . . . . . . . . . . . . . . . 5

1.2 Overview of a Graphics System . . . . . . . . . . . . . . . . . . . . . . 8
1.2.1 Input Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.2.2 Output Devices . . . . . . . . . . . . . . . . . . . . . . . . . . 12

1.3 Quick Preview of the Adventures Ahead . . . . . . . . . . . . . . . . . 14

2 ON TO OPENGL AND 3D COMPUTER GRAPHICS 17
2.1 First Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

2.2 Orthographic Projection, Viewing Box and World Coordinates . . . . 19

2.3 The OpenGL Window and Screen Coordinates . . . . . . . . . . . . . 24

2.4 Clipping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

2.5 Color, OpenGL State Machine and Interpolation . . . . . . . . . . . . 26

2.6 OpenGL Geometric Primitives . . . . . . . . . . . . . . . . . . . . . . 29

2.7 Approximating Curved Objects . . . . . . . . . . . . . . . . . . . . . . 35

2.8 Three Dimensions, the Depth Buffer and Perspective Projection . . . . 37
2.8.1 A Vital 3D Utility: The Depth Buffer . . . . . . . . . . . . . . 38
2.8.2 A Helix and Perspective Projection . . . . . . . . . . . . . . . 40

2.9 Drawing Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

2.10 Approximating Curved Objects Once More . . . . . . . . . . . . . . . 46

2.11 An OpenGL Program End to End . . . . . . . . . . . . . . . . . . . . 49

2.12 Summary, Notes and More Reading . . . . . . . . . . . . . . . . . . . . 51

II Tricks of the Trade 53

3 AN OPENGL TOOLBOX 55
3.1 Vertex Arrays and Their Drawing Commands . . . . . . . . . . . . . . 56

3.2 Vertex Buffer Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . 60

3.3 Vertex Array Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

3.4 Display Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 vii



CONTENTS 3.5 Drawing Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66

3.6 Programming the Mouse . . . . . . . . . . . . . . . . . . . . . . . . . . 67

3.7 Programming Non-ASCII Keys . . . . . . . . . . . . . . . . . . . . . . 69

3.8 Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70

3.9 Line Stipples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71

3.10 FreeGLUT Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72

3.11 Clipping Planes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73

3.12 Frustum, Differently . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75

3.13 Viewports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77

3.14 Multiple Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78

3.15 Summary, Notes and More Reading . . . . . . . . . . . . . . . . . . . . 78

III Movers and Shapers 79

4 TRANSFORMATION, ANIMATION AND VIEWING 81
4.1 Modeling Transformations . . . . . . . . . . . . . . . . . . . . . . . . . 82

4.1.1 Translation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
4.1.2 Scaling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
4.1.3 Rotation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85

4.2 Composing Modeling Transformations . . . . . . . . . . . . . . . . . . 88

4.3 Placing Multiple Objects . . . . . . . . . . . . . . . . . . . . . . . . . . 96

4.4 Modelview Matrix Stack and Isolating Transformations . . . . . . . . 101

4.5 Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
4.5.1 Animation Technicals . . . . . . . . . . . . . . . . . . . . . . . 103
4.5.2 Animation Code . . . . . . . . . . . . . . . . . . . . . . . . . . 106
4.5.3 Animation Projects . . . . . . . . . . . . . . . . . . . . . . . . 115

4.6 Viewing Transformation . . . . . . . . . . . . . . . . . . . . . . . . . . 117
4.6.1 Understanding the Viewing Transformation . . . . . . . . . . . 117
4.6.2 Simulating a Viewing Transformation with Modeling Transfor-

mations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
4.6.3 Orientation and Euler Angles . . . . . . . . . . . . . . . . . . . 130
4.6.4 Viewing Transformation and Collision Detection in Animation 133

4.7 More Animation Code . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
4.7.1 Animating an Articulated Figure . . . . . . . . . . . . . . . . 136
4.7.2 Simple Orthographic Shadows . . . . . . . . . . . . . . . . . . 138

4.8 Selection and Picking . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
4.8.1 Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
4.8.2 Picking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143

4.9 Summary, Notes and More Reading . . . . . . . . . . . . . . . . . . . . 145

5 INSIDE ANIMATION: THE THEORY OF TRANSFORMATIONS 147
5.1 Geometric Transformations in 2-Space . . . . . . . . . . . . . . . . . . 148

5.1.1 Translation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
5.1.2 Scaling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
5.1.3 Rotation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
5.1.4 Reflection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151

5.2 Affine Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
5.2.1 Affine Transformations Defined . . . . . . . . . . . . . . . . . . 154
5.2.2 Affine Transformations and OpenGL . . . . . . . . . . . . . . 158
5.2.3 Affine Transformations and Homogeneous Coordinates . . . . 160

5.3 Geometric Transformations in 2-Space Continued . . . . . . . . . . . . 162viii



CONTENTS5.3.1 Affine Geometric Transformations . . . . . . . . . . . . . . . . 162
5.3.2 Euclidean and Rigid Transformations . . . . . . . . . . . . . . 166

5.4 Geometric Transformations in 3-Space . . . . . . . . . . . . . . . . . . 173
5.4.1 Translation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
5.4.2 Scaling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
5.4.3 Rotation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
5.4.4 Reflection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
5.4.5 Affine Geometric Transformations . . . . . . . . . . . . . . . . 187
5.4.6 Accessing and Manipulating the Current Modelview Matrix . 189
5.4.7 Euclidean and Rigid Transformations . . . . . . . . . . . . . . 191

5.5 Summary, Notes and More Reading . . . . . . . . . . . . . . . . . . . . 196

6 ADVANCED ANIMATION TECHNIQUES 197
6.1 Frustum Culling by Space Partitioning . . . . . . . . . . . . . . . . . . 197

6.1.1 Space Partitioning . . . . . . . . . . . . . . . . . . . . . . . . . 199
6.1.2 Quadtrees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
6.1.3 Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . 201
6.1.4 More about Space Partitioning . . . . . . . . . . . . . . . . . . 202

6.2 Occlusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
6.2.1 Querying and Culling . . . . . . . . . . . . . . . . . . . . . . . 202
6.2.2 Conditional Rendering . . . . . . . . . . . . . . . . . . . . . . 205

6.3 Timer Queries and Performance Measurememt . . . . . . . . . . . . . 205

6.4 Animating Orientation Using Euler Angles . . . . . . . . . . . . . . . . 206
6.4.1 Euler Angles and the Orientation of a Rigid Body . . . . . . . 206
6.4.2 Animating Orientation . . . . . . . . . . . . . . . . . . . . . . 207
6.4.3 Problems with Euler Angles: Gimbal Lock and Ambiguity . . 208

6.5 Quaternions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
6.5.1 Quaternion Math 101 . . . . . . . . . . . . . . . . . . . . . . . 210
6.5.2 Quaternions and Orientation . . . . . . . . . . . . . . . . . . . 213

6.6 Summary, Notes and More Reading . . . . . . . . . . . . . . . . . . . . 221

IV Geometry for the Home Office 223

7 CONVEXITY AND INTERPOLATION 225
7.1 Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225

7.2 Convex Combinations . . . . . . . . . . . . . . . . . . . . . . . . . . . 226

7.3 Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231

7.4 Convexity and the Convex Hull . . . . . . . . . . . . . . . . . . . . . . 234

7.5 Summary, Notes and More Reading . . . . . . . . . . . . . . . . . . . . 239

8 TRIANGULATION 241
8.1 Definition and Justification . . . . . . . . . . . . . . . . . . . . . . . . 241

8.2 Steiner Vertices and the Quality of a Triangulation . . . . . . . . . . . 244

8.3 Triangulation in OpenGL and the Trouble with Non-Convexity . . . . 245

8.4 Summary, Notes and More Reading . . . . . . . . . . . . . . . . . . . . 248

9 ORIENTATION 249
9.1 Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249

9.2 OpenGL Procedure to Determine Front and Back Faces . . . . . . . . 250

9.3 Consistently Oriented Triangulation . . . . . . . . . . . . . . . . . . . 255

9.4 Culling Obscured Faces . . . . . . . . . . . . . . . . . . . . . . . . . . 259

9.5 Transformations and the Orientation of Geometric Primitives . . . . . 261 ix



CONTENTS 9.6 Summary, Notes and More Reading . . . . . . . . . . . . . . . . . . . . 262

V Making Things Up 263

10 MODELING IN 3D SPACE 265
10.1 Curves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266

10.1.1 Specifying Plane Curves . . . . . . . . . . . . . . . . . . . . . 266
10.1.2 Specifying Space Curves . . . . . . . . . . . . . . . . . . . . . 269
10.1.3 Drawing Curves . . . . . . . . . . . . . . . . . . . . . . . . . . 271
10.1.4 Polynomial and Rational Parametrizations . . . . . . . . . . . 274
10.1.5 Conic Sections . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
10.1.6 Curves More Formally . . . . . . . . . . . . . . . . . . . . . . . 277

10.2 Surfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
10.2.1 Polygons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
10.2.2 Meshes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
10.2.3 Planar Surfaces . . . . . . . . . . . . . . . . . . . . . . . . . . 285
10.2.4 General Surfaces . . . . . . . . . . . . . . . . . . . . . . . . . . 286
10.2.5 Drawing General Surfaces . . . . . . . . . . . . . . . . . . . . . 287
10.2.6 Swept Surfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
10.2.7 Drawing Projects . . . . . . . . . . . . . . . . . . . . . . . . . 296
10.2.8 Ruled Surfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
10.2.9 Quadric Surfaces . . . . . . . . . . . . . . . . . . . . . . . . . . 299
10.2.10 GLU Quadric Objects . . . . . . . . . . . . . . . . . . . . . . . 301
10.2.11 Regular Polyhedra . . . . . . . . . . . . . . . . . . . . . . . . . 303
10.2.12 Surfaces More Formally . . . . . . . . . . . . . . . . . . . . . . 307

10.3 Bézier Phrase Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
10.3.1 Curves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
10.3.2 Surfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314

10.4 Importing Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318

10.5 Fractals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322

10.6 Summary, Notes and More Reading . . . . . . . . . . . . . . . . . . . . 324

VI Lights, Camera, Equation 327

11 COLOR AND LIGHT 329
11.1 Vision and Color Models . . . . . . . . . . . . . . . . . . . . . . . . . . 330

11.1.1 RGB Color Model . . . . . . . . . . . . . . . . . . . . . . . . . 331
11.1.2 CMY and CMYK Color Models . . . . . . . . . . . . . . . . . 333
11.1.3 HSV (or HSB) Color Model . . . . . . . . . . . . . . . . . . . 334
11.1.4 Summary of the Models . . . . . . . . . . . . . . . . . . . . . . 334

11.2 Phong’s Lighting Model . . . . . . . . . . . . . . . . . . . . . . . . . . 335
11.2.1 Phong Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
11.2.2 Specifying Light and Material Values . . . . . . . . . . . . . . 337
11.2.3 Calculating the Reflected Light . . . . . . . . . . . . . . . . . 337
11.2.4 First Lighting Equation . . . . . . . . . . . . . . . . . . . . . . 342

11.3 OpenGL Light and Material Properties . . . . . . . . . . . . . . . . . 344
11.3.1 Light Properties . . . . . . . . . . . . . . . . . . . . . . . . . . 345
11.3.2 Material Properties . . . . . . . . . . . . . . . . . . . . . . . . 346
11.3.3 Experimenting with Properties . . . . . . . . . . . . . . . . . . 347
11.3.4 Color Material Mode . . . . . . . . . . . . . . . . . . . . . . . 350

11.4 OpenGL Lighting Model . . . . . . . . . . . . . . . . . . . . . . . . . . 351

11.5 Directional Lights, Positional Lights and Attenuation of Intensity . . . 353x



CONTENTS11.6 Spotlights . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355

11.7 OpenGL Lighting Equation . . . . . . . . . . . . . . . . . . . . . . . . 356

11.8 OpenGL Shading Models . . . . . . . . . . . . . . . . . . . . . . . . . 357

11.9 Animating Light . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358

11.10 Partial Derivatives, Tangent Planes and Normal Vectors 101 . . . . . 358

11.11 Computing Normals and Lighting Surfaces . . . . . . . . . . . . . . . 365
11.11.1 Polygons and Planar Surfaces . . . . . . . . . . . . . . . . . . 365
11.11.2 Meshes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
11.11.3 General Surfaces . . . . . . . . . . . . . . . . . . . . . . . . . . 368
11.11.4 FreeGLUT, Bézier and Quadric Surfaces . . . . . . . . . . . . 371
11.11.5 Transforming Normals . . . . . . . . . . . . . . . . . . . . . . 373
11.11.6 Normalizing Normals . . . . . . . . . . . . . . . . . . . . . . . 375

11.12 Phong’s Shading Model . . . . . . . . . . . . . . . . . . . . . . . . . . 375

11.13 Lighting Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377

11.14 Summary, Notes and More Reading . . . . . . . . . . . . . . . . . . . 378

12 TEXTURE 379
12.1 Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379

12.2 Texture Space, Coordinates and Map . . . . . . . . . . . . . . . . . . . 382

12.3 Repeating and Clamping . . . . . . . . . . . . . . . . . . . . . . . . . . 385

12.4 Filtering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387

12.5 Specifying the Texture Map . . . . . . . . . . . . . . . . . . . . . . . . 393
12.5.1 Parametrized Surfaces . . . . . . . . . . . . . . . . . . . . . . . 393
12.5.2 Bézier and Quadric Surfaces . . . . . . . . . . . . . . . . . . . 394
12.5.3 Spheres and Mercator Projection . . . . . . . . . . . . . . . . . 395

12.6 Texture Matrix and Animating Textures . . . . . . . . . . . . . . . . . 397

12.7 Lighting Textures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397

12.8 Multitexturing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399

12.9 Rendering to Texture with Framebuffer Objects . . . . . . . . . . . . . 401

12.10 Summary, Notes and More Reading . . . . . . . . . . . . . . . . . . . 405

13 SPECIAL VISUAL TECHNIQUES 407
13.1 Blending . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407

13.1.1 Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
13.1.2 Experiments . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
13.1.3 Opaque and Translucent Objects Together . . . . . . . . . . . 412
13.1.4 Blending Textures . . . . . . . . . . . . . . . . . . . . . . . . . 414
13.1.5 Creating Reflections . . . . . . . . . . . . . . . . . . . . . . . . 415
13.1.6 Motion Blur in World Space . . . . . . . . . . . . . . . . . . . 416

13.2 Fog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416

13.3 Billboarding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418

13.4 Antialiasing Points and Lines, Multisampling Polygons . . . . . . . . . 419
13.4.1 Antialiasing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419
13.4.2 Multisampling . . . . . . . . . . . . . . . . . . . . . . . . . . . 421

13.5 Point Sprites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422

13.6 Environment Mapping: Sphere Mapping and Cube Mapping . . . . . . 422
13.6.1 Sphere Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . 423
13.6.2 Cube Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . 424

13.7 Stencil Buffer Techniques . . . . . . . . . . . . . . . . . . . . . . . . . 429
13.7.1 OpenGL Buffers . . . . . . . . . . . . . . . . . . . . . . . . . . 429
13.7.2 Using the Stencil Buffer . . . . . . . . . . . . . . . . . . . . . . 430

13.8 Image Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434 xi



CONTENTS 13.9 Bump Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435

13.10 Shadow Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437

13.11 Summary, Notes and More Reading . . . . . . . . . . . . . . . . . . . 442

VII Pixels, Pixels, Everywhere 443

14 RASTER ALGORITHMS 445
14.1 Cohen-Sutherland Line Clipper . . . . . . . . . . . . . . . . . . . . . . 445

14.2 Sutherland-Hodgman Polygon Clipper . . . . . . . . . . . . . . . . . . 449

14.3 DDA and Bresenham’s Line Rasterizers . . . . . . . . . . . . . . . . . 451

14.4 Scan-Based Polygon Rasterizer . . . . . . . . . . . . . . . . . . . . . . 456
14.4.1 Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
14.4.2 Optimizing Using Edge Coherence – Active Edge List . . . . . 462

14.5 Summary, Notes and More Reading . . . . . . . . . . . . . . . . . . . . 466

VIII Programming Pipe Dreams 467

15 OPENGL 4.3, SHADERS AND THE PROGRAMMABLE PIPELINE: LIFT-
OFF 469
15.1 New Pipeline for OpenGL . . . . . . . . . . . . . . . . . . . . . . . . . 470

15.1.1 Shaders in the Rendering Pipeline . . . . . . . . . . . . . . . . 470
15.1.2 New OpenGL . . . . . . . . . . . . . . . . . . . . . . . . . . . 471

15.2 GLSL Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473

15.3 First Core GL 4.3 Program (Dissected) . . . . . . . . . . . . . . . . . 476

15.4 Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484

15.5 Lighting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486
15.5.1 Per-Vertex Lighting . . . . . . . . . . . . . . . . . . . . . . . . 487
15.5.2 Per-Pixel Lighting . . . . . . . . . . . . . . . . . . . . . . . . . 490

15.6 Textures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490
15.6.1 Procedural Textures . . . . . . . . . . . . . . . . . . . . . . . . 493
15.6.2 Specular Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
15.6.3 Normal Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . 496

15.7 Summary, Notes and More Reading . . . . . . . . . . . . . . . . . . . . 498

16 OPENGL 4.3, SHADERS AND THE PROGRAMMABLE PIPELINE: ES-
CAPE VELOCITY 499
16.1 Toolbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499

16.1.1 VAOs and Instanced Rendering Instead of Display Lists . . . . 499
16.1.2 Clipping Planes . . . . . . . . . . . . . . . . . . . . . . . . . . 502
16.1.3 Rest of the Toolbox . . . . . . . . . . . . . . . . . . . . . . . . 503

16.2 Shader Control Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505
16.2.1 Shader Subroutines . . . . . . . . . . . . . . . . . . . . . . . . 505
16.2.2 Multiple Program Objects . . . . . . . . . . . . . . . . . . . . 506

16.3 Special Visual Techniques . . . . . . . . . . . . . . . . . . . . . . . . . 508
16.3.1 Blending . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508
16.3.2 Points and Sprites . . . . . . . . . . . . . . . . . . . . . . . . . 509
16.3.3 Remaining Effects . . . . . . . . . . . . . . . . . . . . . . . . . 511

16.4 More Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512
16.4.1 Picking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512
16.4.2 Transform Feedback . . . . . . . . . . . . . . . . . . . . . . . . 515

16.5 Tessellation Shaders . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518xii



CONTENTS16.5.1 TCS (Tessellation Control Shader) . . . . . . . . . . . . . . . . 520
16.5.2 TES (Tessellation Evaluation Shader) . . . . . . . . . . . . . . 522
16.5.3 TPG (Tessellation Primitive Generator) . . . . . . . . . . . . . 524

16.6 Geometry Shaders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529
16.6.1 Particle System with Transform Feedback . . . . . . . . . . . . 535

16.7 Summary, Notes and More Reading . . . . . . . . . . . . . . . . . . . . 538

IX Anatomy of Curves and Surfaces 539

17 BÉZIER 541
17.1 Bézier Curves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541

17.1.1 Linear Bézier Curves . . . . . . . . . . . . . . . . . . . . . . . 542
17.1.2 Quadratic Bézier Curves . . . . . . . . . . . . . . . . . . . . . 543
17.1.3 Cubic Bézier Curves . . . . . . . . . . . . . . . . . . . . . . . . 545
17.1.4 General Bézier Curves . . . . . . . . . . . . . . . . . . . . . . . 547

17.2 Bézier Surfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553

17.3 Summary, Notes and More Reading . . . . . . . . . . . . . . . . . . . . 556

18 B-SPLINE 557
18.1 Problems with Bézier Primitives: Motivating B-Splines . . . . . . . . . 558

18.2 B-Spline Curves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 561
18.2.1 First-Order B-Splines . . . . . . . . . . . . . . . . . . . . . . . 562
18.2.2 Linear B-Splines . . . . . . . . . . . . . . . . . . . . . . . . . . 563
18.2.3 Quadratic B-Splines . . . . . . . . . . . . . . . . . . . . . . . . 566
18.2.4 Cubic B-Splines . . . . . . . . . . . . . . . . . . . . . . . . . . 569
18.2.5 General B-Splines and Non-uniform Knot Vectors . . . . . . . 571

18.3 B-Spline Surfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583

18.4 Drawing B-Spline Curves and Surfaces . . . . . . . . . . . . . . . . . . 584
18.4.1 B-Spline Curves . . . . . . . . . . . . . . . . . . . . . . . . . . 584
18.4.2 B-Spline Surfaces . . . . . . . . . . . . . . . . . . . . . . . . . 585
18.4.3 Lighting and Texturing a B-Spline Surface . . . . . . . . . . . 586
18.4.4 Trimmed B-Spline Surface . . . . . . . . . . . . . . . . . . . . 587

18.5 Summary, Notes and More Reading . . . . . . . . . . . . . . . . . . . . 589

19 HERMITE 591
19.1 Hermite Splines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 591

19.2 Natural Cubic Splines . . . . . . . . . . . . . . . . . . . . . . . . . . . 595

19.3 Cardinal Splines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596

19.4 Hermite Surface Patches . . . . . . . . . . . . . . . . . . . . . . . . . . 597

19.5 Summary, Notes and More Reading . . . . . . . . . . . . . . . . . . . . 599

X Well Projected 601

20 APPLICATIONS OF PROJECTIVE SPACES: PROJECTION TRANSFOR-
MATIONS AND RATIONAL CURVES 603
20.1 OpenGL Projection Transformations . . . . . . . . . . . . . . . . . . . 604

20.1.1 Viewing Box to Canonical Viewing Box . . . . . . . . . . . . . 606
20.1.2 Viewing Frustum to Canonical Viewing Box . . . . . . . . . . 607
20.1.3 Projection Matrix in the Pipeline . . . . . . . . . . . . . . . . 610

20.2 Rational Bézier and NURBS Curves and Surfaces . . . . . . . . . . . . 612
20.2.1 Rational Bézier Curves Basics . . . . . . . . . . . . . . . . . . 612
20.2.2 Drawing Rational Bézier Curves . . . . . . . . . . . . . . . . . 615 xiii



CONTENTS 20.2.3 Rational Bézier Curves and Conic Sections . . . . . . . . . . . 616
20.2.4 Properties of Rational Bézier Curves . . . . . . . . . . . . . . 617
20.2.5 Rational Bézier Curves and Projective Invariance . . . . . . . 618
20.2.6 Rational Bézier Curves in the Real World . . . . . . . . . . . . 622
20.2.7 Rational Bézier Surfaces . . . . . . . . . . . . . . . . . . . . . 622
20.2.8 The ‘R’ in NURBS . . . . . . . . . . . . . . . . . . . . . . . . 623

20.3 Summary, Notes and More Reading . . . . . . . . . . . . . . . . . . . . 624

XI Time for a Pipe 625

21 PIPELINE OPERATION 627
21.1 Synthetic-Camera Pipeline . . . . . . . . . . . . . . . . . . . . . . . . . 627

21.1.1 Pipeline: Preliminary Version . . . . . . . . . . . . . . . . . . 628
21.1.2 Perspective Division by Zero . . . . . . . . . . . . . . . . . . . 629
21.1.3 Rasterization with Perspectively Correct Interpolation . . . . 632
21.1.4 Revised Pipeline . . . . . . . . . . . . . . . . . . . . . . . . . . 636
21.1.5 OpenGL Fixed-function Pipeline . . . . . . . . . . . . . . . . . 637
21.1.6 1D Primitive Example . . . . . . . . . . . . . . . . . . . . . . 638
21.1.7 Exercising the Pipeline . . . . . . . . . . . . . . . . . . . . . . 641

21.2 Ray Tracing Pipeline . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641
21.2.1 Going Global: Shadows . . . . . . . . . . . . . . . . . . . . . . 643
21.2.2 Going Even More Global: Recursive Reflection and Transmission 645
21.2.3 Implementing Ray Tracing . . . . . . . . . . . . . . . . . . . . 648

21.3 Radiosity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 649
21.3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 649
21.3.2 Basic Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . 650
21.3.3 Computing Form Factors . . . . . . . . . . . . . . . . . . . . . 652
21.3.4 Solving the Radiosity Equation to Determine Patch Brightnesses 654
21.3.5 Implementing Radiosity . . . . . . . . . . . . . . . . . . . . . . 655

21.4 Summary, Notes and More Reading . . . . . . . . . . . . . . . . . . . . 656

Appendices 657

A PROJECTIVE SPACES AND TRANSFORMATIONS 657
A.1 Motivation and Definition of the Projective Plane . . . . . . . . . . . . 658

A.2 Geometry on the Projective Plane and Point-Line Duality . . . . . . . 660

A.3 Homogeneous Coordinates . . . . . . . . . . . . . . . . . . . . . . . . . 660

A.4 Structure of the Projective Plane . . . . . . . . . . . . . . . . . . . . . 662
A.4.1 Embedding the Real Plane in the Projective Plane . . . . . . . 662
A.4.2 A Thought Experiment . . . . . . . . . . . . . . . . . . . . . . 662
A.4.3 Regular Points and Points at Infinity . . . . . . . . . . . . . . 663

A.5 Snapshot Transformations . . . . . . . . . . . . . . . . . . . . . . . . . 665

A.6 Homogeneous Polynomial Equations . . . . . . . . . . . . . . . . . . . 669
A.6.1 More About Point-Line Duality . . . . . . . . . . . . . . . . . 670
A.6.2 Lifting an Algebraic Curve from the Real to the Projective Plane 671
A.6.3 Snapshot Transformations Algebraically . . . . . . . . . . . . . 674

A.7 The Dimension of the Projective Plane and Its Generalization to Higher
Dimensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 674

A.8 Projective Transformations Defined . . . . . . . . . . . . . . . . . . . . 675

A.9 Projective Transformations Geometrically . . . . . . . . . . . . . . . . 676

A.10 Relating Projective, Snapshot and Affine Transformations . . . . . . . 681
A.10.1 Snapshot Transformations via Projective Transformations . . . 682xiv



CONTENTSA.10.2 Affine Transformations via Projective Transformations . . . . 683

A.11 Designer Projective Transformations . . . . . . . . . . . . . . . . . . . 685

B MATH SELF-TEST 691

C MATH SELF-TEST SOLUTIONS 697

BIBLIOGRAPHY 705

SUBJECT INDEX 713

PROGRAM INDEX 730

xv



http://taylorandfrancis.com


Preface

W
elcome to the third edition of Computer Graphics Through OpenGL:
From Theory to Experiments! The first edition appeared late 2010 and
the second four years after in 2014. Now, it’s been another four years in

which I received a lot of thoughtful and, happily, mostly positive feedback. I heard
from instructors, students, as well as individuals using the book for self-study. Their
observations together with my own classroom experience started me off a year ago
writing a new edition. It’s been a fairly intense past several months, most of my
waking hours spent sitting where I am now in front of the computer. But it’s been
fun too – being in communion with an imaginary reader who I am trying to enlighten
and keep engaged at the same time – and I am pleased with the result. I hope you
will be too. Let’s get to the facts.

About the Book

This is an introductory textbook on computer graphics with equal emphasis on theory
and practice. The programming language used is C++, with OpenGL as the graphics
API, which means calls are made to the OpenGL library from C++ programs. OpenGL
is taught from scratch.

The book has been written to be used as a textbook for a first college course, as
well as for self-study.

After Chapters 1-16 – the undergraduate core of the book – the reader will have a
good grasp of the concepts underpinning 3D computer graphics, as well as an ability
to code sophisticated 3D scenes and animation, including games and movies. We
begin with classical pre-shader OpenGL before proceeding to the latest OpenGL 4.x
(more about our exposition style further on). Chapters 17-21, though advanced, but
still mainstream, could be selected topics for an undergraduate course or part of a
second course.

Specs

This book comprises 21 chapters, an extended appendix on a fundamental math topic,
plus two more appendices containing a math self-test and its solutions. It comes with
approximately 180 programs, 270 experiments based on these programs, 750 exercises,
including theory and programming exercises, 110 worked examples, and 700 four-color
illustrations, include drawings and screenshots. An instructor’s manual containing
solutions to selected exercises is available from the publisher. The book was typeset
using LATEX and figures drawn in Adobe Illustrator.

From the Second Edition to the Third

• Cover to cover revision and reorganization of topics.
New topics include: xvii



Preface • Timer queries and performance measurement.

• Importing externally created objects.

• Texturing spheres.

• Framebuffer objects.

• Rendering to texture.

• Texture matrices.

• Cube mapping a skybox.

• Shadow mapping curved surfaces.

• OpenGL 4.x:

◦ Procedural textures.
◦ Specular maps.
◦ Normal maps.
◦ Multiple program objects.
◦ Particle systems with transform feedback.

• 10 new programs, 20 new experiments, 100 new exercises, 10 new examples, 50
new figures.

• Programming environment simplified, programs developed on Windows 10 and
Microsoft Visual Studio (MSVS) 2015 and tested on MSVS 2017.

Target Audience

• Students in a first university CG course, typically offered by a CS department
at a junior/senior level. This is the primary audience for which the book was
written.

• Students in a second or advanced CG course, who may use the book as
preparation or reference, depending on the goals. For example, the book would
be a useful reference for a study of 3D design – particularly, Bézier, B-spline
and NURBS theory – and of projective transformations and their applications
in CG.

• Students in a non-traditional setting, e.g., studying alone or in a professional
course or an on-line program. The author has tried to be especially considerate
of the reader on her own.

• Professional programmers, to use the book as a reference.

Prerequisites

Zero knowledge of computer graphics is presumed. However, the student is expected
to know the following:

• Basic C++ programming. There is no need to be an expert programmer. The
C++ program serves mainly as an environment for the OpenGL calls, so there’s
rarely need for fancy footwork in the C++ part itself.

• Basic math. This includes coordinate geometry, trigonometry and linear algebra,
all at college first-course level (or, even strong high school in some cases). For
intended readers of the book who may be unsure of their math preparation, we
have a self-test in Appendix B, with solutions in Appendix C. The test should
tell exactly how ready you are and where the weaknesses are.xviii



PrefaceResources

The following are available through the book’s website www.sumantaguha.com:

• Sample chapters, table of contents, preface, subject and program index, math
self-test and solutions at the Home page.

• Program source code, developed on a Windows 10 platform using the Microsoft
Visual Studio Community 2015 IDE and subsequently tested to run with MSVS
Community 2017, which should run on other versions of Windows/MSVS, as
well as Mac OS and Linux platforms. The programs are arranged chapter-wise
in the top-level folder ExperimenterSource at the Downloads page.

• Guide to installing OpenGL and running the programs on a Windows/MSVS
platform at the Downloads page.

• Multiplatform Experimenter software to run the experiments at the Downloads
page. Experimenter’s interface is Experimenter.pdf, a file containing all the
experiments from the book; except for those in Chapter 1, each is clickable
to bring up the related program and workspace. Experimenter is only an aid
and not mandatory – each program is stand-alone. However, it is the most
convenient way to run experiments in their book order.

• Book figures in jpg format arranged in sequence as one PowerPoint presentation
per chapter at the Instructor page.

• Instructor’s manual with solutions to 100 problems – instructors who have
adopted the textbook can submit a request at the Instructor page.

Pedagogical Approach

Code and theory have been intertwined as far as possible in what may be called a
theory-experiment-repeat loop: often, following a theoretical discussion, the reader is
asked to perform validating experiments (run code, that is); sometimes, too, the other
way around, an experiment is followed by an explanation of what is observed. It’s
kind of like discovering physics.

Why use an API?

Needless to say, I am not a fan of the API-agnostic approach to teaching CG, where
focus is on principles only, with no programming practice.

Undergrads, typically, love to code and make things happen, so there is little
justification to denying the new student the joy of creating scenes, movies and games,
not to mention the pride of achievement. And, why not leverage the way code and
theory reinforce one another when teaching the subject, or learning on one’s own,
when one can? Would you want Physics 101 without a lab section?

Moreover, OpenGL is very well-designed and the learning curve short enough to
fully integrate into a first CG course. And, it is supported on every OS platform with
drivers for almost every graphics card on the market; so, in fact, OpenGL is there to
use for anyone who cares to.
Note to student : Our pedagogical style means that for most parts of the book you
want a computer handy to run experiments. So, if you are going to snuggle up with it
at night, make it a threesome with a notebook.
Note to instructor : Lectures on most topics – both of the theory and programming
practice – are best based around the book’s experiments, as well as those you develop
yourself. The Experimenter resource makes this convenient. Slides other than the
plentiful book figures, the latter all available on-line, are rarely necessary.

How to teach modern shader-based OpenGL?

Our point of view needs careful explanation as it is different from some of our peers’.
Firstly, to push the physics analogy one more time, even though relativistic mechanics
seems to rule the universe, in the classroom one might prefer doing classical physics
before relativity theory. xix

www.sumantaguha.com


Preface Shaders, which are the programmable parts of the modern OpenGL pipeline, add
great flexibility and power. But, so too, do they add a fair bit of complexity – even
a cursory comparison of our very first program square.cpp from Chapter 2 with
its equivalent in fourth-generation OpenGL, squareShaderized.cpp, complemented
with a vertex and a fragment shader in Chapter 15, should convince the reader of this.

Consider more carefully, say, a vertex shader. It must compute the position
coordinates of a vertex, taking into account all transformations, both modelview and
projection. However, in the classical fixed-function pipeline the user can simply issue
commands such as glTranslatef(), glFrustum(), etc., leaving to OpenGL actual
computation of the transformed coordinates; not so for the programmable pipeline,
where the reader must write herself all the needed matrix operations in the vertex
shader. We firmly believe that the new student is best served learning first how to
transform objects according to an understanding of simply how a scene comes together
physically (e.g., a ball falls to the ground, a robot arm bends at the elbow, etc.) with
the help of ready-to-use commands like glTranslatef(), and, only later, how to
define these transforms mathematically.

Such consideration applies as well to other automatic services of the fixed-function
pipeline which allow the student to focus on phenomena, disregarding initially
implementation. For example, as an instructor, I would much prefer to teach first how
diffuse light lends three-dimensionality, specular light highlights, and so on, gently
motivating Phong’s lighting equation, leaving OpenGL to grapple with its actual
implementation, which is exactly what we do in Chapter 11.

In fact, we find an understanding of the fixed-function pipeline makes the subsequent
learning of the programmable one significantly easier because it’s then clear exactly
what the shaders should try to accomplish. For example, following the fixed-function
groundwork in Chapter 11, writing shaders to implement Phong lighting, as we do in
Chapter 15, is near trivial.

We take a similarly laissez-faire attitude to classical OpenGL syntax. So long as it
eases the learning curve we’ll put up with it. Take for example the following snippet
from our very first program square.cpp:

glBegin(GL_POLYGON);

glVertex3f(20.0, 20.0, 0.0);

glVertex3f(80.0, 20.0, 0.0);

glVertex3f(80.0, 80.0, 0.0);

glVertex3f(20.0, 80.0, 0.0);

glEnd();

Does it not scream square – even though it’s immediate mode and uses the discarded
polygon primitive? So, we prefer this for our first lesson, avoiding thereby the
distraction of a vertex array and the call glDrawArrays(GL TRIANGLE STRIP, 0, 4),
as in the equivalent 4.x program squareShaderized.cpp, our goal being a simple
introduction of the synthetic-camera model.

With these thoughts in mind the book starts in Chapter 2 with classical pre-shader
OpenGL, progressing gradually deeper into the API, developing CG ideas in parallel,
in a so-called theory-experiment-repeat loop. So, what exactly is an experiment?
An experiment consists either of running a book program – each usually simple for
the purpose of elucidating a single idea – or attempting to modify one based on an
understanding of the theory in order, typically, to achieve a particular visual result.

By the end of Chapter 14 the student will have acquired proficiency in pre-shader
OpenGL, a perfectly good API in itself. As well, equally importantly, she will have
an understanding of CG principles and those underlying the OpenGL pipeline, which
will dramatically ease her way through the concepts and syntax of OpenGL 4.x, the
newest generation of the API, covered in Chapters 15-16.

Does this kind of introduction to modern OpenGL, via the old and, possibly,
obsolete, not ingrain bad habits? Not at all, from our experience. When push comes
to shove, how hard is it to replace polygons with triangle strips? Or, use vertex bufferxx



Prefaceobjects (VBOs) and vertex array objects (VAOs) to store data? Does our approach
cost timewise? If the goal is OpenGL 4.x, then, yes, it does take somewhat longer,
but there are various possible learning sequences through the book and 4.x certainly
can be reached and covered in a semester.

In short, then, we believe the correct way to modern OpenGL is through the
classical version of the API because this allows the learning process to begin at
a high level, so that the student can concentrate on gaining an overall end-to-end
understanding of the CG pipeline first, leaving the OpenGL system to manage low-level
processes (i.e., those inside the pipeline like setting transformation and projection
matrices, defining fragment colors, and such). Once she has a high-level mastery,
subsequently “descending” into the pipeline to take charge of fixed-function parts in
order to program them instead will, in fact, be far less arduous than if she tried to do
both – learn the basics and program the pipeline – at the same time.

Another point to note in this context is that, as noted before, classical OpenGL is
a complete API in itself which, in fact, can be more convenient for certain applications
(e.g., it allows one access to the readymade GLUT objects like spheres and toruses).
There are, as well, thousands of currently live applications written in classical OpenGL,
which are not going go to be discarded or rewritten any time soon – the reason, in fact,
for the Khronos Group to retain the compatibility version of the API – so familiarity
with older syntax can be useful for the intending professional.

What about Vulkan?

We thought you might ask. Vulkan is the much-hyped “successor” to OpenGL. It is a
highly explicit API, taking the programmer close to the hardware and asking her to
specify almost all facets of the pipeline from end to end. Benefits of programming
near the hardware include thin drivers, reduced run-time overhead and the ability to
expose parallelism in the GPU. (Vulkan is not only a 3D graphics API, but used to
program GPU-heavy compute applications as well.)

Figure 1: Screenshot of
squareVulkanized.cpp, a
1000+ line Vulkan
program.

However, Vulkan’s explicitness and consequent verbosity make it highly unsuitable
as an introductory CG API. Here are some program sizes to begin with. The first
OpenGL program in the book, square.cpp, which draws a black square on a white
background, is about 90 lines of code in pre-shader 2nd generation OpenGL; a
functionally equivalent program, squareShaderized.cpp, written in OpenGL 4.3
later on in the book is 190 lines plus 25 lines of shader code; a minimal equivalent
Vulkan program, squareVulkanized.cpp, written separately by the author is 1,100
lines (no, that’s no misprint – the reader will find the program at the Downloads page
of the book’s website) plus 30 lines of shader code. Figure 1 is a screenshot.

Moreover, explicitness requires a Vulkan programmer to be familiar with the
functioning of the graphics pipeline at a low level in order to specify it, which almost
instantly disqualifies it from being a beginner’s API. Further, delaying programming
until after the pipeline has been covered goes utterly against our own pedagogical
approach which is to engage students with code the first day.

So, is OpenGL, or for that matter, this book, of any use for someone intending to
learn Vulkan? Well:

(a) The Vulkan graphics pipeline is essentially the same as OpenGL’s. Therefore,
learning OpenGL is progress toward Vulkan. Moreover, once a programmer has
mastered OpenGL, she has most of what’s needed to “take full charge” of the
pipeline, which is what Vulkan is all about.

(b) The runtime gains of Vulkan don’t begin to show up in any significant way
until one gets to complex scenes with lots of textures, objects and animation.
Less complicated applications - including, obviously, those in any introductory
CG book - benefit little performance-wise from being written (or, rewritten) in
Vulkan, not justifying the huge overhead in code.

This means that many OpenGL apps are going to stay that way and new ones
continue to be written. It’s a matter of knowing which tool to use: pre-shader xxi



Preface OpenGL, OpenGL 4.x, Vulkan, . . .. (Hooking up a U-Haul trailer to the back of
a Ferrari is never a good idea.)

Nevertheless, if you are of the Vulkan or bust frame of mind then the advice we would
have is to study this book up to Chapter 16, when you will have a solid understanding
of fourth-generation OpenGL, then pick up, say, the canonical Vulkan guide [131],
through which you should then be able to make quick progress.

Capsule Chapter Descriptions

Part I: Hello World

Chapter 1: An Invitation to Computer Graphics

A non-technical introduction to the field of computer graphics.

Chapter 2: On to OpenGL and 3D Computer Graphics

Begins the technical part of the book. It introduces OpenGL and fundamental
principles of 3D CG.

Part II: Tricks of the Trade

Chapter 3: An OpenGL Toolbox

Describes a collection of OpenGL programming devices, including vertex arrays, vertex
buffer and array objects, mouse and key interaction, pop-up menus, and several more.

Part III: Movers and Shapers

Chapter 4: Transformation, Animation and Viewing

Introduces the theory and programming of animation and the virtual camera. Explains
user interactivity via object selection. Foundational chapter for game and movie
programming.

Chapter 5: Inside Animation: The Theory of Transformations

Presents the mathematical theory behind animation, particularly linear and affine
transformations in 3D.

Chapter 6: Advanced Animation Techniques

Describes frustum culling, occlusion culling as well as orienting animation using both
Euler angles and quaternions, techniques essential to programming games and busy
scenes.

Part IV: Geometry for the Home Office

Chapter 7: Convexity and Interpolation

Explains the theory of convexity and the role it plays in interpolation, which is the
procedure of spreading material properties from the vertices of a primitive to its
interior.

Chapter 8: Triangulation

Describes how and why complex objects should be split into triangles for efficient
rendering.

Chapter 9: Orientation

Describes how the orientation of a primitive is used to determine the side of it that
the camera sees, and the importance of consistently orienting a collection of primitives
making up a single object.

Part V: Making Things Up

Chapter 10: Modeling in 3D Spacexxii



PrefaceSystematizes the principles of modeling both curves and surfaces, including Bézier and
fractal. Shows how to import objects from external design environments. Foundational
chapter for object design.

Part VI: Lights, Camera, Equation

Chapter 11: Color and Light
Explains the theory of light and material color, the interaction between the two, and
describes how to program light and color in 3D scenes. Foundational chapter for scene
design.

Chapter 12: Textures
Explains the theory of texturing and how to apply textures to objects and render to a
texture.

Chapter 13: Special Visual Techniques
Describes a set of special techniques to enhance the visual quality of a scene,
including, among others, blending, billboarding, stencil buffer methods, image and
pixel manipulation, cube mapping a skybox, and shadow mapping.

Part VII: Pixels, Pixels, Everywhere

Chapter 14: Raster Algorithms
Describes low-level rendering algorithms to determine the set of pixels on the screen
corresponding to a line or a polygon.

Part VIII: Programming Pipe Dreams

Chapter 15: OpenGL 4.3, Shaders and the Programmable Pipeline: Liftoff
Introduces 4th generation OpenGL and GLSL (OpenGL Shading Language) and how
to vertex and fragments shaders to program the pipeline, particularly to animate,
light and apply textures.

Chapter 16: OpenGL 4.3, Shaders and the Programmable Pipeline: Escape Velocity
Continuing onto advanced 4th generation OpenGL topics, including, among others,
instanced rendering, shader subroutines, transform feedback, particle systems, as well
as tessellation and geometry shaders.

Part IX: Anatomy of Curves and Surfaces

Chapter 17: Bézier
Describes the theory and programming of Bézier primitives, including curves and
surfaces.

Chapter 18: B-Spline
Describes the theory and programming of (polynomial) B-spline primitives, including
curves and surfaces.

Chapter 19: Hermite
Introduces the basics of Hermite curves and surfaces.

Part X: Well Projected

Chapter 20: Applications of Projective Spaces: Projection Transformations and
Rational Curves
Applies the theory of projective spaces to deduce the projection transformation in
the graphics pipeline. Introduces rational Bézier and B-spline, particularly NURBS,
theory and practice.

Part XI: Time for a Pipe

Chapter 21: Pipeline Operation xxiii



Preface Gives a detailed view of the synthetic-camera and ray-tracing pipelines and introduces
radiosity.

Appendix A: Projective Spaces and Transformations
A CG-oriented introduction to the mathematics of projective spaces and transforma-
tions. Provides a complete theoretical background for Chapter 20 on applications of
projective spaces.

Appendix B: Math Self-Test
A self-test to assess math readiness for intending readers.

Appendix C: Math Self-Test Solutions
Solutions for the math self-test.

An Invitation
to Computer 
Graphics

Chapter 1
On to OpenGL
and 3D Computer 
Graphics

Chapter 2

Inside Animation:
The Theory of 
Transformations

Chapter 5
Advanced 
Animation 
Techniques

Chapter 6

Convexity and 
Interpolation

Chapter 7

Orientation

Chapter 9

Modeling in 
3D Space

Chapter 10

Color and Light

Chapter 11

Texture

Chapter 12

Special Visual 
Techniques

Chapter 13

Raster 
Algorithms

Chapter 14

Bézier

Chapter 17

B-Spline

Chapter 18

Hermite

Chapter 19
Applications of 
Projective 
Spaces: ...

Chapter 20

Projective
Spaces and
Transformations

Appendix A

Chapter 21

OpenGL 4.3,
Shaders ... :
Liftoff

Chapter 15

Triangulation

Chapter 8

An OpenGL 
Toolbox

Chapter 3
Transformation,
Animation and 
Viewing

Chapter 4

OpenGL 4.3, 
Shaders ... :
Escape Velocity

Chapter 16

Pipeline 
Operation

Figure 2: Chapter dependence chart: dashed arrows represent weak dependencies.

xxiv



PrefaceSuggested Course Outlines

See the chapter dependencies in Figure 2.

(1) Undergraduate one-semester first CG course:

This course should be based on Chapters 1-16, though full coverage might be
ambitious for one semester. Instructors may pick topics to emphasize or skip,
depending on their goals for the course and the chapter dependence chart.

For example, for more practice and less theory, a possible sequence would be 1→
2 → 3 → 4 → 6 (only frustum culling) → 7 → 8 → 9 → 10 (skip curve/surface
theory) → 11 → 12 → 13 → 15 → 16.
Even this abbreviated sequence may be hard to pack into one semester. Please
keep in mind that when choosing what to write about the author preferred to
err on the side of excess rather than less. So, almost always will the instructor
find more material in a chapter than she cares to teach – we leave her to pick
her way out.

The most effective teaching method with this book is to base discussion around
experiments – both from the book and those the instructor develops herself.
Our Experimenter software makes this especially convenient. Students should be
involved in the experiments, running code simultaneously on their own machines
in class. Use of slides should be minimized except, possibly, for the plentiful
book figures, which are available to download, arranged as one PowerPoint
presentation per chapter.

(2) Advanced CG courses:

This book could serve as a reference for a study of 3D design – particularly,
Bézier (Chapter 17), B-spline (Chapter 18) and rational Bézier and NURBS
theory (Chapter 20) – and of projective transformations and their applications
(Appendix A and Chapter 20). From a practical point of view, Chapters 15-16
go fairly deep into the fourth generation of OpenGL and the GLSL, useful for
students who may be familiar with only the classical pipeline.

(3) Self-study:

A recommended first pass would be 1 → 2 → 3 → 4 → 7 → 8 → 9 (go light on
7-9 if your math is rusty) → 10 (skip theory) → 11 → 12 → 13 → 15 → 16.
Following this the student should take up a fair-sized programming project,
returning to the book as needed. For the theoretically-inclined there’s a lot to
keep her busy in Chapters 5 and 17-21.

Acknowledgments

I owe a lot to many people, most of all students whom I have had the privilege of
teaching in my CG classes over the years at UW-Milwaukee and then the Asian
Institute of Technology.

I thank KV, Ichiro Suzuki, Glenn Wardius, Mahesh Kumar, Le Phu Binh, Maria
Sell and, especially, Paul McNally, for their support at UWM, where I began to teach
CG and learn OpenGL.

I am grateful to my colleagues and the staff and students at AIT for such a pleasant
environment, which allowed me to combine teaching and research commitments with
the writing of a book.

Particular thanks at AIT to Vu Dinh Van, Nguyen Duc Cong Song, Ahmed
Waliullah Kazi, Hameedullah Kazi, Long Hoang, Songphon Klabwong, Robin Chanda,
Sutipong Kiatpanichgij, Samitha Kumara, Somchok Sakjiraphong, Pyae Phyo Myint
Soe, Adbulrahman Otman, Sushanta Paudyal, Akila de Silva, Nitchanun Saksinchai,
Thee Thet Zun, Suwanna Xanthavanij, Visutr Boonnateephisit, Matt Dailey, and our
ever-helpful secretaries K. Siriporn and K. Tong. xxv



Preface I am grateful to Kumpee Teeravech, Kanit Tangkathach, Thanapoom Veeranitinun,
Pongpon Nilaphruek, and Wuttinan Sereethavekul, students of my CG course at AIT,
for allowing me to use programs they wrote.

I owe an enormous debt of gratitude to my former student Chansophea Chuon for
hundreds of hours of help with the first edition, which got this book off the ground in
the first place. I thank Somying Pongpimol for her brilliant Illustrator drawings. She
drew several of the figures based on my rather amateurish original Xfig sketches. I
would like to thank Olivier Nicole for help with the book’s website.

I am especially grateful to Brian Barsky for encouraging me to persevere after
seeing an early and awkward draft of the first edition, and subsequently inviting the
finished product to the series he was then editing. Relatedly, I thank my neighbor
Doug Cooper two floors down for putting me in touch with Brian at the time Brian
was scouting prospective authors.

I want to acknowledge the production team at Taylor & Francis who went out of
their way for this book. Particularly, I want to thank my editor Randi Cohen who is
as professional and efficient as she is pleasant to deal with.

I am grateful to readers of the first and second editions, as well as reviewers
who looked over drafts and proposals, whose comments led, hopefully, to significant
improvements.

I have nothing but praise for the DJs and kindly staff of the Mixx nightclub in
downtown Bangkok whose dance floor provided a much-needed sanctuary for me to
blow off steam after long hours on the computer.

I acknowledge the many persons and businesses who were kind enough to allow
me to include images to which they own copyrights.

On a personal note, I express my deep gratitude to Dr. Anupam De for keeping
Kamaladi healthy enough that I could concentrate on the first edition through the
few years that I spent writing it.

Finally, I must say that had I not had the opportunity to study computer science
in the United States and teach there, I would never have reached a position where I
could even contemplate writing a textbook. It’s true, too, that had I not moved to
Thailand, this book would never have begun to be written. This is an enchanting
country with a strangely liberating and lightening effect – to which thousands of
expats can attest – that encourages one to express oneself.

Website and Contact Information

The book’s website is at www.sumantaguha.com. Users of the book will find there
various resources including downloads. The author welcomes feedback, corrections
and suggestions for improvement emailed to him at sg@sumantaguha.com.

xxvi

www.sumantaguha.com
mailto:sg@sumantaguha.com


About the Author

Sumanta Guha earned a Ph.D. in mathematics from the Indian Statistical Institute,
Kolkata, in 1987. From 1984 to 1987 he taught mathematics at Jadavpur University
in Kolkata. He left in 1987 to study computer science at the University of Michigan
in Ann Arbor, where he earned a Ph.D. in 1991. On graduating from Michigan he
joined the Electrical Engineering and Computer Science faculty of the University of
Wisconsin-Milwaukee where he taught from 1991 to 2002. In 2002 he moved to the
Computer Science and Information Management program of the Asian Institute of
Technology in Thailand, where he is currently an adjunct professor. His research
interests include computational geometry, computer graphics, computational topology,
robotics and data mining.

xxvii



http://taylorandfrancis.com


Part I

Hello World

1



http://taylorandfrancis.com


CHAPTER 1
An Invitation to Computer Graphics

C
omputer graphics, or CG as it is often simply called, is the use of computers
to generate images. This is as opposed to the capture of images from the
real-world with, say, a camera, or the realization by hand of an artist’s

imagination on a drawing medium.

Figure 1.1: A cell phone, news opening graphics, car dashboard.

To not see the end product of CG, that being computer-generated imagery (CGI),
throughout your day, you would have to be on a deserted island. Images on the screen
of the cell phone you probably check first thing on waking are digitally synthesized
by a processor. Almost every frame on the TV showing the morning news has CGI
in some part. If you commute, then the vehicle carrying you to school or work likely
communicates with its operator through multiple computer-managed console panels,
displaying information ranging from fuel level to geographical location.

Figure 1.2: A computer at work, handheld game player, part of AIT home page (thanks Asian
Institute of Technology).

At work, if at all you use a computer, then, of course, there you are sitting right
at a fountainhead of computer graphics. And, CGI probably plays an even more
important role in your recreational life. Even the most casual video game amusing
commuters headed home nowadays has sophisticated interactive 3D graphics. The 3



Chapter 1

An Invitation to

Computer Graphics

web on which we spend so many hours a day is increasingly becoming a multimedia
smorgasbord synthesizing animation, movie clips, CGI and sound.

Figure 1.3: Khan Kluay, the first 3D animated Thai movie (courtesy Kantana Animation), an
anthropomorphic mouse, a massive (fortunately herbivorous) dinosaur.

When you watch a movie you are seeing a product from an industry, which together
with the gaming industry, has the biggest relationship with CG of any other, not only
as a consumer of the latest and greatest in technique, but also as a multimillion-dollar
promoter of cutting-edge research. A little blue elephant which grows into a mighty
warrior, an eccentric mouse with a ribald sense of humor, and a massive dinosaur
looking so hungrily for food that you would think its species had never really become
extinct more than fifty million years ago – to contemplate such achievements is to
be in awe of the human imagination, as well as the ingenuity of the engineers and
programmers who materialize these fantastical conceptions as palpable and believable
digital presences.

Figure 1.4: Clockwise from top left: Image of the human brain, flight simulator cockpit (from
NASA), engine design, hurricane over Florida, water drop on a leaf.

Then there’s the quiet CG impacting our lives some would say even more profoundly
than its more flamboyant manifestations. Doctors and surgeons practice their craft
in simulated environments detailed to the tiniest capillaries. Commercial pilots put
in hundreds of hours on a flight simulator before entering a real cockpit. (Flight
simulators are a sentimental favorite because they were the first killer CG app, drawing
attention and investment dollars to the then nascent field in the sixties.)

Automobiles, airplanes and almost any fairly complex manufactured object we see
around us are designed, fabricated and even put through regulatory tests as virtual
entities – which exist entirely as a collection of bits perceptible only as an image on a
monitor – gestating often for years before the first physical prototype is ever built.4



Section 1.1

Brief History of

Computer Graphics

Supercomputers implement extremely complex mathematical models of the weather,
but their predictions have to be visualized – again CGI – in order to be meaningful to
humans.

Because its business is the creation of pictures, computer graphics has an immediate
allure. But, it is a science as well, with intellectual challenges ranging from the routine
to about as deep and difficult as you please. Think of modeling a drop of water
rolling off a leaf. There would be a fair amount of physics and, probably, a differential
equation or two to solve on the way to getting just the mechanics of the rolling drop
right, not to mention texturing the leaf, creating a translucent (and changing) shape
for the drop, and determining illumination.

The field of computer graphics brings particular pleasure to students and
practitioners alike because it’s always about making something – just like sculpting
or painting. Part by part you watch your creation come together, and alive even, if
it is animated. Aside from the aesthetic, there are more tangible rewards to be had
too. One would be hard pressed to name a sphere of social or scientific or industrial
activity where CGI does not have a role. Wherever it is that ultimately you want
to be, medicine or fashion, rocket science or banking, weapons development or disco
dancing, CG skills not only can make a difference, but also make you a career.

1.1 Brief History of Computer Graphics

Although the term “computer graphics” itself was coined in 1960 by William Fetter, a
designer at Boeing, to describe his own job, the field can be said to have first arrived
with the publication in 1963 of Ivan Sutherland’s Sketchpad program, as part of his
Ph.D. thesis at MIT.

Sketchpad, as its name suggests, was a drawing program. Beyond the interactive
drawing of primitives such as lines and circles and their manipulation – in particular,
copying, moving and constraining – with use of the then recently invented light pen,
Sketchpad had the first fully-functional graphical user interface (GUI) and the first
algorithms for geometric operations such as clip and zoom. Interesting, as well, is
that Sketchpad’s innovation of an object-instance model to store data for geometric
primitives foretold object-oriented programming. Coincidentally, on the hardware
side, the year 1963 saw the invention by Douglas Engelbart at the Stanford Research
Institute of the mouse, the humble device even today carrying so much of GUI on its
thin shoulders.

Figure 1.5: Ivan Sutherland operating Sketchpad on a TX-2 (courtesy of Ivan Sutherland),
Douglas Engelbart’s original mouse (courtesy of John Chuang).

Although Sketchpad ran on a clunky Lincoln TX-2 computer with only 64KB in
memory and a bulky monochrome CRT monitor as its front-end, nevertheless, it thrust
CG to the attention of early researchers by showing what was possible. Subsequent
advances through the sixties came thick and fast: raster algorithms, the implementation
of parametric surfaces, hidden-surface algorithms and the representation of points
by homogeneous coordinates, the latter crucially presaging the foundational role of
projective geometry in 3D graphics, to name a few. Flight simulators were the killer
app of the day and companies such as General Electric and Evans & Sutherland, 5



Chapter 1

An Invitation to

Computer Graphics

co-founded by Douglas Evans and Ivan Sutherland, wrote simulators with real-time
graphics.

Interestingly, the advent of flight simulators actually predated that of CG – at
least Sutherland and his Sketchpad – by nearly two decades, when the US Navy
began the funding of Project Whirlwind at MIT during the Second World War for the
purpose of creating simulators to train bomber crews. Those early devices had actually
little graphics and consisted essentially of a simulated instrument panel reacting in
real-time to control input from the pilots, but Project Whirlwind helped fund the
talent and research environment at MIT which enabled Sutherland, a student then at
MIT, to create Sketchpad, launch computer graphics and, finally, complete the circle
by establishing a company to make flight simulators.

The next decade, the seventies, brought the z-buffer for hidden surface removal,
texture mapping, Phong’s lighting model – all crucial components of the OpenGL API
(Application Programming Interface) we’ll be using soon – as well as keyframe-based
animation. Photorealistic rendering of animated movie keyframes almost invariably
deploys ray tracers, which were born in the seventies too. Emblematic of the advances
in 3D design was Martin Newell’s 1975 Utah teapot, composed entirely of bicubic
Bézier patches, which became the testbed of choice for CG algorithms of that era.
The latter half of the decade saw, too, the Apple I and II personal computers make
their debut, bringing CG for the first time to the mass market.

Figure 1.6: Utah teapot (from Wikimedia), Apple II Plus (courtesy of Steven Stengel),
SIGGRAPH 2006 expo floor in Boston (courtesy of Jason Della Rocca).

From the academic point of view, particularly important were the establishment in
1969 of the SIGGRAPH (Special Interest Group in Graphics) by the ACM (Association
for Computing Machinery, the premier academic society for computers and computing)
and, subsequently, the first annual SIGGRAPH conference in 1973. These two
developments signaled the emergence of computer graphics as a major subdiscipline
of computer science. The SIGGRAPH conference has since then become the foremost
annual event in the CG world. In addition to being the most prestigious forum for
research papers, it hosts a giant exhibition which attracts hundreds of companies,
from software developers to book publishers, who set up booths to promote their
wares and recruit talent.

Since the early eighties, CG, both software and hardware, began rapidly to assume
the form we see today. The IBM PC, the Mac and the x86 chipsets all arrived, sparking
off the race to become faster (processor), smaller (size), bigger (memory) and cheaper
(particularly important if one has student loans to pay off). As computers became
consumer goods, the market for software spilled over from academia to individuals
and businesses.

Nintendo released Donkey Kong in 1981, the wildly successful arcade video game
which revolutionized the genre, and soon after Wavefront Technologies released its
Preview software, used then to create opening graphics for television programs. Now,
of course, Nintendo is a star of the video games industry producing the Wii and it
successors, while Wavefront has morphed into Alias (owned by Autodesk) whose 3D
graphics modeling package Maya is ubiquitous in the design world.

3D graphics began to displace its plainer 2D sister through the nineties as hardware
increasingly became capable of supporting the rendering needs of 3D models, even
in real-time, thus allowing interaction and its myriad consequences (such as gaming).6



Section 1.1

Brief History of

Computer Graphics

The difference between 2D and 3D graphics is that models in the latter are created in
a (virtual) 3D world, geometrically identical to real world, and then projected onto
the viewing screen, while all drawings in 2D graphics are on a flat plane.

Figure 1.7: Donkey Kong arcade game (from Wikimedia), Maya screenshot of Scary Boris
(courtesy of Sateesh Malla at www.sateeshmalla.com), 2D characters on the left of each column vs.
3D to their right ( c© Mediafreaks Cartoon Pte. Ltd., 2006. All rights reserved.).

Models drawn in 3D are more realistic because they belong to the space we actually
live in, but they are more complex as well; moreover, the 3D-to-2D projection step,
absent for 2D graphics, is computation-intensive too. Graphics cards, manufactured
by companies such as ATI and Nvidia, which not only manage the image output to
the display unit, but have, as well, additional hardware support for rendering of 3D
primitives, are now inexpensive enough that desktops and even notebooks can run
high-end 3D applications. How well they run 3D games often, in fact, is used to
benchmark personal computers.

Figure 1.8: T. Rex: heartthrob of the Jurassic Park movies, Quake 1 game (courtesy of
Quake R© c© 1996 id Software LLC, a ZeniMax Media Company, All Rights Reserved).

Through the nineties, as well, the use of 3D effects in movies became pervasive.
The Terminator and Star Wars series, and Jurassic Park, were among the early movies
to set the standard for CGI. Toy Story from Pixar, released in 1995, has special
importance in the history of 3D CGI as the first movie to be entirely computer-
generated – no scene was ever pondered through a glass lens, nor any recorded on a
photographic reel! It was cinema without film. Quake, released in 1996, the first of
the hugely popular Quake series of games, was the first fully 3D game.

Figure 1.9: OpenGL
and OpenGL ES logos
(used with permission of
Khronos).

Another landmark from the nineties of particular relevance to us was the release
in 1992 of OpenGL, the open-standard cross-platform and cross-language 3D graphics
API, by Silicon Graphics. OpenGL is actually a library of calls to perform 3D
tasks, which can be accessed from programs written in various languages and running
over various operating systems. That OpenGL was high-level (in that it frees the
applications programmer from having to care about such low-level tasks as representing
primitives like lines and triangles in the raster, or rendering them to the window) and
easy to use (much more so than its predecessor 3D graphics API, PHIGS, standing
for Programmer’s Hierarchical Interactive Graphics System) first brought 3D graphics
programming to the “masses”. What till then had been the realm of a specialist was
now open to a casual programmer following a fairly amicable learning curve. 7

www.sateeshmalla.com)


Chapter 1

An Invitation to

Computer Graphics

Since its release OpenGL has been rapidly adopted throughout academia and
industry. It’s only among game developers that Microsoft’s proprietary 3D API,
Direct3D, which came soon after OpenGL and bearing an odd similarity to it but
optimized for Windows, is more popular.

The story of the past decade has been one of steady progress, rather than spectacular
innovations in CG. Hardware continues to get faster, better, smaller and cheaper,
continually pushing erstwhile high-end software downmarket, and raising the bar for
new products. The almost complete displacement of CRT monitors by LCD and the
emergence of high-definition television are familiar consequences of recent hardware
evolution.

Of likely even greater economic impact is the migration of sophisticated software
applications – ranging from web browsers to 3D games – to handheld devices like
smartphones, on the back of small yet powerful processors. CG has now been
untethered from large immobile devices and placed into the hands and pockets
of consumers. In fact, a lightweight subset of OpenGL called OpenGL ES – ES
abbreviating Embedded Systems – released by the Khronos Group in 2003, is now the
most popular API for programming 3D graphics on small devices.

Another important development is the advent of WebGL – OpenGL for the web
as its name suggests. WebGL is based on OpenGL ES and is currently supported by
almost all browsers, making possible real-time interactive CGI via the web.

1.2 Overview of a Graphics System

The operation of a typical graphics system can be split into a three-part sequence:

Input −→ Processing −→ Output

The simplest example of this is when you click on a thumbnail image in, say, YouTube,
and a video clip pops up and begins to play. The click is the input. Your computer
then reacts to this input by processing, which involves downloading the movie file and
running it through the Adobe Flash Player, which in turn outputs video frames to
your monitor.

Figure 1.10: YouTube and Adobe Illustrator screenshots.

Graphics systems can be of two types, non-interactive and interactive. The playing
of a YouTube clip is an example of a non-interactive one: beyond the first click to
get the movie started you have little further say over the output process, other than
maybe to stop it or manipulate the window. On the other hand, if, say, you are using
a package like Adobe Illustrator, then the output – what you have drawn – changes in
real-time in response to input you provide by pressing keys and moving and clicking
the mouse; e.g., you can change a shape by dragging an anchor point with the mouse.
In an interactive system output continuously reacts to input via the processor.

Input/output devices (or I/O devices, or peripheral devices, as they are also called)
are of particular importance in interactive systems because they determine the scope
of the interaction. For example, an input device that functions like a steering wheel8



Section 1.2

Overview of a

Graphics System

would be essential to a video game to race cars; simulating flight through a virtual 3D
environment, on the other hand, needs something akin to a joystick used to maneuver
an aircraft.

Because it is, in fact, interactive computer graphics – theory and programming
– which we’ll be studying the next twenty chapters, we’ll survey in the next two
sections the most common I/O devices found in graphics systems nowadays. As
for the processor which comes between the I and the O, from the point of view
of the CG programmer, this is just a box to do her bidding, particularly, run her
apps. For the sake of completeness, though, here’s a list of the important ones, all
somewhat different one from the other in the context of CG (Figure 1.11 pictures them):

Computer : As far as we are concerned, this category includes PC’s, workstations,
servers and the like.

Portable computer : This, of course, is simply a small and light computer with a
built-in display, keyboard and pointing device. Because of the size constraint, limited
power supply and also the lack of space for a large cooling fan, CPU’s and graphics
cards in portables tend to underperform their desktop counterparts. Software writers
need to take this into account, especially for graphics-intensive applications.

Handheld
device

Portable
computer

Computer

Game
console

Figure 1.11: Processing devices clockwise from top: laptop, smartphone, game console (used with
permission from Microsoft), computer box.

Handheld device: The size-weight constraint on this class of devices – of which the
mobile phone is the most visible example – is even more severe than for portable
computers. Handhelds are expected to travel in bags and pockets. Low-end handhelds
often have no peripheral other than a limited keypad, while higher-end ones may come
equipped with a high-res end-to-end touchscreen. In addition to the possibly small
RAM and slower CPU, another consideration to keep in mind for graphics developers
for handhelds is the limited real estate of the display: busy scenes tend to become
chaotic on a handheld.

Game consoles: All stops are off for programming these devices. Running graphics-
intensive applications at blinding speeds is what these machines were born to do.

1.2.1 Input Devices

The following is by no means a complete list of input devices, but it does cover the
ones we are most likely to encounter in everyday use. The devices are all pictured in
Figure 1.12, ringing the processing devices in the middle, and our list goes clockwise
starting from the top rightmost.

9



Chapter 1

An Invitation to

Computer Graphics

Keyboard : This device is a mandatory peripheral for any computer. Its alphanumeric
keys, evidently derived from the traditional typewriter, are used to enter text strings,
while additional keys, such as the arrow and function keys, perform special actions.

Mouse: This is an example of a pointing device which inputs spatial data to the
computer. As the mouse is moved by the user’s hand on a flat surface, a mechanical
ball or optical sensor at its base signals the amount of movement to the computer,
which correspondingly moves a cursor on the screen. Effectively, then, the user
determines the location of the cursor. Strictly speaking, a mouse is more than just
a pointing device if it has buttons, as most do, each of which can be clicked to give
binary input.

Wheel

Computer

Joystick

Keyboard

Mouse

Touchpad

Pointing stick

Gamepad

Haptic
device

Camera Data gloves

Handheld
device

Portable
computer

Trackball

Spaceball

Game
console

Touchscreen

Figure 1.12: Input devices clockwise from top right (surrounding processing devices in the middle):
keyboard, mouse, touchpad, pointing stick (courtesy of Long Zheng), trackball, spaceball (courtesy
of Logitech), haptic device ( c© SensAble Technologies, Inc.), joystick, wheel, gamepad, webcam,
touchscreen, data gloves (courtesy of www.5dt.com).

Touchpad : Another 2D pointing device, particularly common on portable computers,
the touchpad is a small rectangular area embedded with electronic sensors to determine
the position of a touching finger or stylus. Movement of the finger or stylus is echoed
by movement of the cursor.

Pointing stick : Yet another 2D pointing device common on portable computers, the
pointing stick is, typically, a rubber peg located between the ‘G’, ‘H’ and ‘B’ keys,
which moves the cursor in response to pressure applied with a finger.

10

www.5dt.com


Section 1.2

Overview of a

Graphics System

Trackball : This is essentially an upside-down mouse, with a socket containing a ball
which the user manipulates with her hand to make the cursor move.

Spaceball : This is a pointing device with six degrees of freedom versus the two of an
ordinary mouse. It is used in special applications such as manipulating a camera in
a 3D scene, where the camera not only moves, but also rotates. The spaceball itself
consists of a pressure-sensitive ball which can distinguish different kinds of forces –
including forward and backward, lateral and twist – the applied force manipulating
the selected object.

Haptic device: This is a pointing device which gives physical feedback to the user
based on the location of the cursor or, possibly, that of an object being moved along
with the cursor. The easiest way to understand the functioning of a haptic device,
if you have never used one, is to imagine a mouse with a mechanical ball which is
(somehow) programmed to lock and stop rolling when the cursor reaches the side of the
screen. The reaction the user then has is of that of the cursor running into a physical
obstacle at the edge of the screen, though evidently it is moving in virtual space. The
device depicted in Figure 1.12 is not a haptic mouse, of course, but one commonly
seen in HCI (human-computer interaction) labs. The three-link arm swiveling on a
ball gives it six degrees of freedom.

Haptics has numerous applications, a couple of noteworthy ones being the tele-
operation of robots (where the operator gets haptic feedback as she manipulates a
robot in either a virtual or a remote real environment) and simulated surgery training
in medicine (which is similar to training pilots on a flight simulator, except that
surgery has the added component of tactile feedback, mostly absent in flying).

Joystick : This is an input device popular in video games and applications such as
flight simulators. It originated from its namesake found in real aircraft cockpits.
A joystick pivots around a fixed base, gaining thus two degrees of freedom, and
usually has buttons which can be depressed to provide additional input. In a game or
simulator setting a joystick is typically used to control an object traveling through
space. Nowadays, high-end joysticks have embedded motors to provide haptic feedback
to user motion, e.g., resistance as a plane is banked.

Wheel : This again is a specialized input device for games and simulators, obviously
derived from the car steering wheel, and provides rotational input in an exactly similar
manner, most often to a virtual automobile. Again, haptic feedback to give the user a
sense of the vehicle’s response, and even of the terrain over which it is traveling, is
becoming increasingly popular.

Gamepad : This device is the standard controller for many modern game consoles.
Usual features include action buttons operated usually with the right thumb and a
cross-shaped directional controller with the left.

Camera: Although this input device needs no introduction, it’s worth noting the
increasingly sophisticated uses a peripheral camera is being put to with the help, e.g.,
of software to recognize faces, gestures and expressions.

Touchscreen: Increasingly popular as the interface of handheld devices such as
smartphones, a touchscreen is a display which can accept input via touch. It is
similar to touchpads and tablets in that it senses the location of a finger or stylus
– one or the other is usually preferable based on the particular technology used to
make the screen – on the display area. A common application of touchscreens is to
eliminate the need for a physical keyboard by displaying a virtual one responding to
taps on the screen.

Touchscreens often respond not only to the location of the touch, but also the 11



Chapter 1

An Invitation to

Computer Graphics

motion of the touching object. For example, a flicking motion with a finger may cause
a window to scroll. Multi-touch capability, now increasingly common, makes possible
for the device to respond to gestures with more than one finger, e.g., pinching and
spreading with two fingers.

Data gloves : This device is used particularly in virtual reality environments which are
programmed to react to the position of the gloves, the direction in which fingers are
pointing, as well as to hand motion and gestures. The gloves themselves are wired to
transmit not only their location, but also their configuration and orientation to the
processor, so that the latter can display the environment accordingly. For example,
an index finger pointing at a particular atom in a virtual-reality display of a molecule
may cause this atom to zoom up to the viewer.

1.2.2 Output Devices

Again, the following list is not meant to be comprehensive, but, rather, representative
of the most common output devices. We go clockwise around the outer ring of devices
pictured in Figure 1.14 beginning with the rightmost.

CRT (cathode-ray tube) monitor : Though now nearly obsolete CRT monitors are
worth a review because they were the first visual output devices, for which reason their
configuration greatly influenced the development of foundational graphics algorithms.

A CRT monitor has phosphors of the three primary colors – R(ed), G(reen) and
B(lue), the basis of CG color application – located at each one of a rectangular array
of pixels, called the raster. Additionally, it has three electron guns inside, causing its
infamous bulk, that each fires a beam at phosphors of one color. A mechanism to aim
and control their intensities causes the beams to travel together, striking one pixel
after another, row after row, exciting the RGB phosphors at each pixel to the values
specified for it in the color buffer. Figure 1.13(a) shows the electron beams striking
one pixel on a dog.

pixel

electron
guns

electr
on be

ams

(a) (b)

phosphors

Figure 1.13: (a) Color CRT monitor with electron beams aimed at a pixel with phosphors of the 3
primaries (b) A raster of pixels showing a rasterized triangle.

From the point of view of OpenGL and most CG theory, what matters is that the
pixels in a monitor are arranged in a rectangular raster (as depicted in Figure 1.13(b)).
For, this layout is the basis of the lowest-level CG algorithms, the so-called raster
algorithms, which actually select and color the pixels to represent user-specified
shapes such as lines and triangles on the monitor. Figure 1.13(b), for example, shows
the rasterization of a right-angled triangle (with terrible jaggies because of the low
resolution).

The number of rows and columns of pixels in the raster determines the monitor’s
resolution. Typical for a CRT monitor is a resolution in the range of 1024 × 768
(which means 1024 columns and 768 rows). High-definition monitors (as needed,12



Section 1.2

Overview of a

Graphics System

say, for high-definition TV, or HDTV as it’s acronymed) have higher resolution, e.g.,
1920× 1080 is common.

CRT monitor

Portable
computer
display

Handheld
device

Portable
computer

Computer

LCD monitor

Handheld display

Game
console

Stereoscopic
glasses

VR headset 3 D   d i
 s p l a y s

Figure 1.14: Output devices clockwise from the rightmost (surrounding processing devices in the
middle): CRT monitor, LCD monitor, notebook, mobile phone, 3D displays including a VR headset
and stereoscopic glasses tethered to a PC.

Moreover, a memory location called the color buffer, either in the CPU or graphics
card, contains, typically, 32 bits of data per raster pixel – 8 bits for each of RGB,
and 8 for the alpha value (used in blending). It is the RGB values in the color buffer
which determine the corresponding raster pixel’s color intensities. The values in the
color buffer are read by the raster – in other words, the raster is refreshed – at a
rate called the monitor’s refresh rate. Beyond this, the technology underlying the
particular display device, no matter how primitive or fancy, really matters little to
the CG programmer.

For decades a bulky CRT monitor, or two, was a fixture atop work tables. Now,
of course, they have been nearly totally supplanted by a sleeker successor which we
discuss next.

LCD (liquid crystal display) monitor : Pixels in an LCD monitor each consist of three
subpixels made of liquid crystal molecules, which separately filter lights of the primary
colors. The amount of light, coming from a backlight source, emerging through a
subpixel is controlled by an electric charge whose intensity is determined by subpixel’s
corresponding value in the color buffer. The absence of electron guns allows LCD
monitors to be made flat and thin – unlike CRT monitors – so they are one of the
class of flat panel displays.

Technologies other than LCD, e.g., plasma and OLED (organic light emitting
diode), are used as well in flat panel displays, though LCD is by far the most common
one found with computers. Keep in mind that what’s called an LED monitor is
simply a kind of LCD monitor where the backlighting comes from array of light-
emitting diodes (LEDs) - in fact, almost all LCD monitors nowadays have this kind of
backlighting, so would be classified as LED monitors. 13



Chapter 1

An Invitation to

Computer Graphics

Again, for a CG programmer the view to keep in mind of an LCD monitor, as of
any other flat panel display, is as a rectangular raster of pixels whose RGB intensities
are individually set by values in the computer’s color buffer.

Portable computer display : This display is again a raster of pixels whose RGB values
are read from a color buffer. The technology employed, typically, is TFT-LCD, a
variant of LCD which uses thin film transistors to improve image quality.

Handheld display : Handheld displays, such as those on devices like mobile phones,
commonly use the same TFT-LCD technology as portable computers. The resolution,
though, is necessarily smaller, e.g., 480× 640 would be in the ballpark for low-end
mobiles.

3D display : Almost all 3D displays are based on the principle of stereoscopy, in which
an illusion of depth is created by showing either eye of the viewer images of the scene
captured by one of two cameras slightly offset from one another (just like a pair of
eyes as in Figure 1.15). Once the scene has been recorded with two cameras, it is
in ensuring that each eye of the viewer sees frames only from one of them, called
stereoscopic viewing, that there are primarily two competing technologies.

Figure 1.15: An early
model stereo camera
filming a newish bike.

In the first, frames alternately from either camera are displayed on the monitor,
a process called alternate frame sequencing. The viewer herself wears stereoscopic
glasses, each lens embedded with a layer of liquid crystals which can be darkened with
an electrical signal (such glasses are also called LC shutter glasses). The glasses are
synchronized with the monitor’s refresh rate, either lens being alternately darkened
with successive frames. Consequently, each eye sees images from only one of the two
cameras, resulting in a stereoscopic effect. Typically, the frame rate is increased to
48 per second as well, so that both eyes experience a smooth-seeming 24 frames each
second. The great advantage of LC shutter glasses is that they can be used with any
computer which has a monitor with a refresh rate fast enough to support alternate
frame sequencing, as well as a graphics card with enough buffer space for two video
streams. So with these glasses even a high-end home system would qualify to play 3D
movies and games.

In the second, polarized 3D glasses are used to view two images, from either
camera, projected simultaneously on the same screen through orthogonal polarizing
filters. The lenses too contain orthogonal polarizing filters, each allowing through only
light of like polarization. Consequently, either lens sees images from only one or other
camera, engendering a stereoscopic view. Polarized 3D glasses are significantly less
expensive than LC shutter glasses and, moreover, require no synchronization with the
monitor. However, the projection system is complicated and expensive and primarily
used to equip theaters for 3D viewing.

VR headsets, increasing popular nowadays for an immersive 3D experience, are
each a two-stream projection system with twin-lens viewing all in one, which can use
either of the above technologies.

OpenGL, the API we’ll be using, is well-suited to making scenes and movies for
3D viewing because it allows multiple (virtual) cameras to be positioned arbitrarily.

1.3 Quick Preview of the Adventures Ahead

To round out this invitation to CG we want to show you four programs written
by students in their first college 3D CG course, taught by the author at the Asian
Institute of Technology using a draft of this book. They were written in C++ with
calls to OpenGL.

But, first, what exactly is OpenGL? You may have been wondering this awhile.
We said earlier in the section on CG history that OpenGL is a cross-platform 3D14



Section 1.3

Quick Preview of

the Adventures

Ahead

graphics API. It consists of a library of over 500 commands to perform 3D tasks,
which can be accessed from programs written in various languages. Well, here’s a
glimpse of something concrete – an example snippet from a C++ environment to draw
10 red points:

glColor3f(1.0, 0.0, 0.0);

glBegin(GL_POINTS);

for(int i = 0; i < 10; i++)

{
glVertex3i(i, 2*i, 0);

}
glEnd();

The first function call glColor3f(1.0, 0.0, 0.0) declares the red drawing color,
while the loop bracketed between the glBegin(GL POINTS) and glEnd() calls draws
a point at (i, 2i, 0) in each of ten iterations. There are calls in the OpenGL library to
draw straight lines, triangles, create light sources, apply textures, move and rotate
objects, maneuver the camera, and so on – in fact, not surprisingly, pretty much all
one needs to create and animate 3D scenes.

In the early days, coding CG meant pretty much addressing individual pixels
of the raster to turn on and off. Compared with using OpenGL today this is like
programming in assembly compared with programming in a high-level language like
C++. OpenGL gives an environment where we can focus on imagining and creating,
leaving low-level work all to the underlying engine.

Isn’t that old OpenGL, though, the code you show above? Yes, it is. Precisely,
it’s pre-shader OpenGL.

But, the fact you are asking this question probably means you are not familiar yet
with our pedagogical approach, which is described in the book’s preface. We explain
there why we believe in setting the reader’s foundations in the classical version of
OpenGL before proceeding to the new (namely, fourth generation or 4.x) of which
there is complete coverage later in the book. We urge you to read at least that part
of the preface in order to be comfortable with how we plan on doing things.

Getting back to the student programs, code itself is not of importance and would
actually be a distraction at this time. Instead, just running the programs and viewing
the output will give an idea of what can be accomplished even in a fairly short time
(ranging from 3 weeks to 3 months for the different programs) by persons coming to
CG with little more than a good grasp of C++ and some basic math. Of course, we’ll
get a feel as well for what goes into making 3D scenes.

Experiment 1.1. Open ExperimenterSource/Chapter1/Ellipsoid and run the
(Windows) executable there for the Ellipsoid program.∗ The program draws an
ellipsoid (an egg shape). The left of Figure 1.16 shows the initial screen. There’s
plenty of interactivity to try as well. Press any of the four arrow keys, as well as the
page up and down keys, to change the shape of the ellipsoid, and ‘x’, ‘X’, ‘y’, ‘Y’, ‘z’
and ‘Z’ to turn it.

It’s a simple object, but the three-dimensionality of it comes across rather nicely
does it not? As with almost all surfaces that we’ll be drawing ourselves, the ellipsoid
is made up of triangles. To see these press the space bar to enter wireframe mode.
Pressing space again restores the filled mode. Wireframe reveals the ellipsoid to be a
mesh of triangles decorated with large points. A color gradient has apparently been
applied toward the poles as well.

Drawing an ellipsoid with many triangles may seem a hard way to do things.
Interestingly, and often surprisingly for the beginner, OpenGL offers the programmer

∗Experimenter.pdf does not have clickable links to run the executables for this chapter. Clickable
links to bring up source code and project files start from the next chapter. 15



Chapter 1

An Invitation to

Computer Graphics

only a tiny set of low-level geometric primitives with which to make objects – in fact,
points, lines and triangles are, basically, it. So, a curved 3D object like an ellipsoid
has to be made, or, more accurately, approximated , using triangles. But, as we shall
see as we go along, the process really is not difficult.

That’s it, there’s really not much more to this program. It’s just a bunch of colored
triangles and points laid out in 3D space. The magic is in those last two words: 3D
space. 3D modeling is all about making things in 3D – not a flat plane – to create an
illusion of depth, even when viewing on a flat plane (the screen). End

(a) (b) (c) (d)

Figure 1.16: Screenshots of (a) Ellipsoid (b) AnimatedGarden (c) BezierShoe (d) Dominos.

Experiment 1.2. Our next program is animated. It creates a garden which
grows and grows and grows. You will find the executable in ExperimenterSource/-
Chapter1/AnimatedGarden. Press enter to start the animation; enter again to stop
it. The delete key restarts the animation, while the period key toggles between the
camera rotating and not. Again, the space key toggles between wireframe and filled.
The second image of Figure 1.16 is a screenshot a few seconds into the animation.

As you can see from the wireframe, there’s again a lot of triangles (in fact, the
flowers might remind you of the ellipsoid from the previous program). The plant stems
are thick lines and, if you look carefully, you’ll spot points as well. The one special
effect this program has that Ellipsoid did not is blending, as is not hard to see.
End

Experiment 1.3. The third program shows fairly fancy object modeling. It makes
a lady’s shoe with the help of so-called Bézier patches. The executable is in
ExperimenterSource/Chapter1/BezierShoe. Press ‘x’-‘Z’ to turn the shoe, ‘+’ and
‘-’ to zoom in and out, and the space bar to toggle between wireframe and filled. The
third image of Figure 1.16 is a screenshot.

Keep in mind that, as far as CG goes, the techniques involved in designing a shoe
are the same as for designing a spaceship! End

Experiment 1.4. Our final program is a movie which shows a Rube Goldberg
domino effect with “real” dominos. The executable is in ExperimenterSource/-
Chapter1/Dominos. Simply press enter to start and stop the movie. The screenshot
on the right of Figure 1.16 is from part way through.

This program has a bit of everything – textures, lighting, camera movement and,
of course, a nicely choreographed animation sequence. Neat, is it not? End

All fired up now and ready to rumble? Great! Let’s go.

Acknowledgments: Kumpee Teeravech wrote Ellipsoid and AnimatedGarden,
Pongpon Nilaphruek wrote BezierShoe, while Kanit Tangkathach and Thanapoom
Veeranitinunt wrote Dominos.

16



CHAPTER 2
On to OpenGL and 3D Computer
Graphics

T
he primary goal for this chapter is to be acquainted with OpenGL and begin
our journey into computer graphics using OpenGL as the API (Application
Programming Interface) of choice. We shall apply an experiment-discuss-

repeat approach where we run code and ask questions of what is seen, acquiring
thereby an understanding not only of the way the API functions, but underlying CG
concepts as well. Particularly, we want to gain insight into:

(a) The synthetic-camera model to record 3D scenes, which OpenGL implements.

(b) The approach of approximating curved objects, such as circles and spheres, with
the help of straight and flat geometric primitives, such as line segments and
triangles, which is fundamental to object design in computer graphics.

We begin in Section 2.1 with our first OpenGL program to draw a square, the
computer graphics equivalent of “Hello World”. Simple though it is, with a few careful
experiments and their analysis, square.cpp yields a surprising amount of information
through Sections 2.1-2.3 about orthographic projection, the fixed world coordinate
system OpenGL sets up and how the so-called viewing box in which the programmer
draws is specified in this system. We gain insight as well into the 3D-to-2D rendering
process.

Adding code to square.cpp we see in Section 2.4 how parts of objects outside the
viewing box are clipped off. Section 2.5 discusses OpenGL as a state machine. We
have in this section as well our first glimpse of property values, such as color, initially
specified at the vertices of a primitive, being interpolated throughout its interior.

Next is the very important Section 2.6 where all the drawing primitives of OpenGL
are introduced. These are the parts at the application programmer’s disposal with
which to assemble objects from thumbtacks to spacecrafts.

The first use of straight primitives to approxi