TE AM FL Y Linux 3D Graphics Programming Norman Lin Wordware Publishing, Inc Library of Congress Cataloging-in-Publication Data Lin, Norman Linux 3D graphics programming / by Norman Lin p cm Includes bibliographical references and index ISBN 1-55622-723-X (pbk.) Computer graphics Linux Three-dimensional display systems I Title T385 L556 2001 006.6'93 dc21 00-69314 CIP © 2001, Wordware Publishing, Inc All Rights Reserved 2320 Los Rios Boulevard Plano, Texas 75074 No part of this book may be reproduced in any form or by any means without permission in writing from Wordware Publishing, Inc Printed in the United States of America ISBN 1-55622-723-X 10 0103 Blender is a registered trademark of Not a Number B V Other product names mentioned are used for identification purposes only and may be trademarks of their respective companies All inquiries for volume purchases of this book should be addressed to Wordware Publishing, Inc., at the above address Telephone inquiries may be made by calling: (972) 423-0090 iii Contents Preface xiii Acknowledgments xv Introduction xvii Chapter Introduction to 3D Graphics and Linux Why Linux Linux is Free and Open Linux is Powerful Linux is Compatible Linux is Contemporary A Survey of 3D Graphics Under Linux Stable Ideas, Projects, and Libraries Developing Ideas, Projects, and Libraries Developing APIs 3D Applications with Source 10 3D Modelers 10 Dead Ideas, Projects, and Libraries 11 Other Ideas, Projects, and Libraries 11 Scope of This Book, Revisited 12 Let’s Talk About X 12 Definition of X: A Client-Server System 13 Hardware Acceleration and X Servers 14 What is Hardware Acceleration? 15 Integrating Hardware Acceleration into X 17 Summary of Hardware Acceleration 19 “Hello, Linux”: A Sample Program 20 Entering the Sample Program 20 The Vi and Emacs Editors 20 Basic Editing with Emacs 22 Compiling and Linking the Sample Program 25 Overview of Makefiles 26 Compiling the Program with Make 27 Compiling the Program Manually 27 Analysis of Makefile 28 Executing the Sample Program 29 Debugging the Sample Program 30 The Text Debugger—Not Just for Masochists 34 Getting Online Help 35 iv Contents The Online Manual “man” The Online Info Documentation Other Ways of Finding Information Summary 35 37 38 38 Chapter Accessing the Screen 39 Overview 39 X Programming 39 The Role of X 39 Structure of an X Program 43 Xlib, the X Toolkit, and the Xt Intrinsics 45 Connecting to the X Server 46 Creating and Mapping the Window 48 Receiving X Events 50 Responding to X Events 51 Critique of the Sample Program 54 An Object-Oriented Rewrite 55 An Abstract Application Class 59 A Concrete Application Class 60 Factory Design Pattern 60 An Application Factory and Factory Manager 62 Execution of the Main Program 63 Object-Oriented: Is It Worth It? 64 Introduction to XImages 65 A Practical Example of OO Reuse 66 Running the New Subclassed Program 68 New Classes 69 X Server Depth and Visual Class 74 Graphics and Double Buffering with XImages 76 Requesting Notification for All Events 78 Visual Event Display 78 Summary 79 Displaying Graphics in XImages 79 Random Dots 79 A Word on Animation 83 Summary 83 Picture Elements—Pixels 84 2D Pixel Coordinates 84 Byte-to-Pixel Correspondence 86 Colors and XImages 88 Developing Reusable Graphics Classes: l3d Library 92 Sample Program Using l3d 93 l3d Directory Structure 96 Fundamental l3d Concepts 97 Overview of l3d Classes 104 Contents v Source Code Walk-through of l3d Classes Abstracting an Event-Driven Graphics Application: l3d_pipeline Abstracting Event Generation: l3d_event_source Abstracting the Event Dispatching Loop: l3d_dispatcher A Dispatcher for X: l3d_dispatcher_x11 Abstracting Control of the Screen: l3d_screen Abstracting Relevant Screen Attributes: l3d_screen_info Screen Information for TrueColor: l3d_screen_info_rgb Screen Information for Indexed Color: l3d_screen_info_indexed A Screen Under X: l3d_screen_x11 Abstracting the Rasterizer: l3d_rasterizer_2d A Rasterizer Implementation: l3d_rasterizer_2d_imp A Software Rasterizer Implementation: l3d_rasterizer_2d_sw_imp Choosing the Concrete Factories Summary of l3d Classes Covered So Far Summary Chapter 2D Rasterization Overview Software Rasterization Rasterizing Dots Rasterizing Lines Real Numbers in l3d: Type l3d_real Rasterizing Flat-shaded Polygons Specifying Coordinates Two-Part Vertex Lists Defining Polygons: Indices into a Vertex List Drawing the Polygon Sub-Pixel Correct Rasterization Polygon Clipping in 2D Discard the Polygon Completely Discard Invalid Vertices Only Scissoring or Scanline Clipping Analytical 2D Clipping The X11 SHM Extension: l3d_screen_x11_shm The X11 DGA Extension Sample Programs Sample Program XForm Sample Program Morph2d Hardware-Accelerated Rasterization Mesa and OpenGL The OpenGL Utility Toolkit (GLUT) A Sample Program with Mesa and GLUT Mesa and 3DFX Hardware Acceleration Classes for Using Mesa 105 105 106 106 107 108 111 113 119 124 130 133 133 141 143 146 147 147 148 149 150 152 154 156 162 168 180 184 187 188 188 188 190 196 200 201 201 205 211 212 212 212 215 216 vi Contents l3d_screen_mesa l3d_screen_info_rgb_mesa, l3d_screen_info_indexed_mesa l3d_dispatcher_mesa l3d_rasterizer_2d_mesa_imp Running the Sample Programs with Mesa Summary Chapter 3D Vision and Perception Overview The Goal of 3D Graphics Moving from 2D to 3D Vision and Optics: 3D in the Physical World The Retina: Biochemical Vision The Geometry of Light Single Point Two Points Lines A Cube Parallax Definition of Parallax How Parallax Comes About Why Parallax is Important “Runway Lights”—A Simple 3D Program Runway Lights: Physical Situation Code Listing Summary 216 219 220 222 224 226 227 227 227 229 230 230 230 231 232 233 235 236 236 236 238 238 238 239 246 Chapter Perspective Projection Overview Projection from 3D to 2D The Inputs and Outputs to the Projection Process Points Form Objects 3D Coordinate Systems and Vectors Definition of Vectors Adding and Subtracting Vectors and Points The Relationship Between Points and Vectors Multiplying a Vector by a Scalar Multiplying a Vector by a Vector: the Dot Product The Dot Product and the Concept of Length The Dot Product and Unit Vectors The Dot Product and the Concept of Angle The Dot Product and the side_of_point Function The Dot Product and the Concept of Vector Projection Multiplying a Vector by a Vector: the Cross Product Left-handed and Right-handed Coordinate Systems 247 247 247 249 249 250 252 254 256 258 258 259 260 260 262 263 264 265 Contents vii The l3d_vector Class Sample Program Using Vectors The Mathematics of Projection The Physical Viewing Situation Calculating Intersections Mapping to 2D Pixel Coordinates Field of View: the Connection Between Window Size and Distance Determining the Horizontal Field of View Term Determining Field of View Term as Ratio of Width/Distance Determining Field of View Term as the Cotangent of the Field of View Angle Summary of Perspective Projection Sample Program: Dots The Parts of a 3D Engine The Rendering Pipeline Database and Database Traversal Simulation Perspective Projection and Screen Mapping Culling Rasterization Blitting 3D Polygonal Graphics Overview of Program “Spikes” From Dots to Polygons Summary Chapter Matrices and Transformations Overview Introduction to Transformations Translation Rotation Scaling What is the Matrix? Definitions: Matrices and Vectors A Note on the Term “Vector” Writing 3D Points and 3D Vectors in Matrix Notation The Homogeneous W Coordinate The Meaning of W Why Homogeneous Coordinates Combining Matrices and Column Vectors Mathematical Operations on Matrices Scalar-Matrix Multiplication Matrix-Matrix Multiplication, or Composition Multiple Matrix Multiplications The Identity Matrix 265 270 273 273 275 278 282 283 283 284 285 285 293 293 294 295 298 298 299 299 299 304 305 307 309 309 310 310 312 316 317 317 318 318 319 319 320 321 321 321 322 324 325 viii Contents Matrix Inverse Matrix Transpose The l3d_matrix Class What’s All This Got to Do with 3D Graphics? Mathematical Equations for Translation, Rotation, and Scaling Translation Rotation Scaling Sample Program: Rotspikes Combining Transformations Simple Sequential Transformations Rotation or Scaling About an Arbitrary Point Arbitrary Camera Position and Orientation How to Specify an Arbitrary Camera Position and Orientation Deriving the Matrix The Final Camera Transformation Matrix The l3d_moveable Class The l3d_camera Class Rotation About an Arbitrary Axis The l3d_mat_rotu Function Sample Program: Camspikes Additional Notes on Matrices Inverting a Matrix Determinant of a Matrix Perspective Projection as a Transformation Matrix OpenGL, Matrices, and Hardware-Accelerated Geometry Matrices and Coordinate Systems Typical Coordinate Systems in 3D Graphics Right-to-left vs Left-to-right Interpretation Summary Chapter Polygonal Modeling and Rendering in 3D Overview Reasons for 3D Polygonal Modeling Extending Polygons from 2D into 3D Surface Normal Vectors Storing Normal Vectors as Base and Tip Points Orientation of Normal Vectors Vertex Normal Vectors Transforming Normal Vectors Transforming 3D Polygons Methods for Transforming a Polygon Step-by-Step Guide to Transforming 3D Polygons Near Z Clipping Define the Near Z Plane Mathematically 325 326 326 335 336 336 337 341 342 348 348 350 352 353 353 355 356 362 363 367 367 377 377 378 379 381 385 388 390 392 393 393 394 396 402 403 405 407 408 409 410 412 412 414 Contents ix Define the Inside and Outside Sides of the Near Z Plane Classify a Point as being Inside or Outside Calculate the Intersection Between a Crossing Segment and the Near Z Plane Putting It All Together Linked Lists of Polygons Drawing 3D Polygons: Flat Shading Sample Program: clipz Changes to the Spike Class Changes to the Pipeline Class Summary Grouping Polygons into Objects The l3d_object Class Creating and Copying 3D Objects Defining the Geometry of a 3D Object Defining Vertices Defining Polygons Specifying Transformations Implicit Position and Orientation Inheritable Behavior Plug-in Behavior The 3D Object Plug-in Interface Pointers to the Plug-in Functions and Data Writing a Plug-in Classes for Loading Plug-ins A New Factory Manager for Plug-ins Summary of Using Plug-ins Grouping Objects into a World The World Database Interaction with the Pipeline Updating the Entire World Drawing the Entire World Sample Program: fltsim A Subclassed 3D Object A Plug-in 3D Object Planes in 3D Defining a Plane Sides of a Plane The l3d_plane Class Clipping Polygons to an Arbitrary 3D Plane The l3d_polygon_3d_clippable Class The l3d_polygon_3d_flatshaded_clippable Class The l3d_object_clippable Class Sample Program: objcopy Summary 415 415 415 416 417 418 421 430 432 433 433 434 439 440 440 441 444 445 446 446 448 449 450 451 453 455 456 460 460 463 463 466 471 471 476 476 478 478 481 481 486 487 490 495 .. .Linux 3D Graphics Programming Norman Lin Wordware Publishing, Inc Library of Congress Cataloging-in-Publication Data Lin, Norman Linux 3D graphics programming / by Norman... Introduction to 3D Graphics and Linux Why Linux Linux is Free and Open Linux is Powerful Linux. .. l3d_plane Class Clipping Polygons to an Arbitrary 3D Plane The l3d_polygon _3d_ clippable Class The l3d_polygon _3d_ flatshaded_clippable Class The l3d_object_clippable