Team-Fly® Linux 3D Graphics Programming
Total Page:16
File Type:pdf, Size:1020Kb
Y L F M A E T Team-Fly® 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.) 1. Computer graphics. 2. Linux. 3. 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 10987654321 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 1 Introduction to 3D Graphics and Linux .........1 Why Linux ......................................3 Linux is Free and Open ..............................3 Linux is Powerful .................................4 Linux is Compatible ................................5 Linux is Contemporary ..............................6 A Survey of 3D Graphics Under Linux .......................6 Stable Ideas, Projects, and Libraries .......................8 Developing Ideas, Projects, and Libraries ....................9 Developing APIs ................................9 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” ............................35 The Online Info Documentation .........................37 Other Ways of Finding Information .......................38 Summary.......................................38 Chapter 2 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 ...................105 Abstracting an Event-Driven Graphics Application: l3d_pipeline .....105 Abstracting Event Generation: l3d_event_source ..............106 Abstracting the Event Dispatching Loop: l3d_dispatcher ..........106 A Dispatcher for X: l3d_dispatcher_x11...................107 Abstracting Control of the Screen: l3d_screen ...............108 Abstracting Relevant Screen Attributes: l3d_screen_info ..........111 Screen Information for TrueColor: l3d_screen_info_rgb ..........113 Screen Information for Indexed Color: l3d_screen_info_indexed .....119 A Screen Under X: l3d_screen_x11 .....................124 Abstracting the Rasterizer: l3d_rasterizer_2d ................130 A Rasterizer Implementation: l3d_rasterizer_2d_imp............133 A Software Rasterizer Implementation: l3d_rasterizer_2d_sw_imp ....133 Choosing the Concrete Factories.......................141 Summary of l3d Classes Covered So Far ..................143 Summary ......................................146 Chapter 3 2D Rasterization ....................147 Overview ......................................147 Software Rasterization ...............................148 Rasterizing Dots.................................149 Rasterizing Lines ................................150 Real Numbers in l3d: Type l3d_real .....................152 Rasterizing Flat-shaded Polygons .......................154 Specifying Coordinates ...........................156 Two-Part Vertex Lists ............................162 Defining Polygons: Indices into a Vertex List ................168 Drawing the Polygon.............................180 Sub-Pixel Correct Rasterization .......................184 Polygon Clipping in 2D.............................187 Discard the Polygon Completely.......................188 Discard Invalid Vertices Only ........................188 Scissoring or Scanline Clipping .......................188 Analytical 2D Clipping............................190 The X11 SHM Extension: l3d_screen_x11_shm................196 The X11 DGA Extension ............................200 Sample Programs ................................201 Sample Program XForm ...........................201 Sample Program Morph2d ..........................205 Hardware-Accelerated Rasterization .......................211 Mesa and OpenGL ...............................212 The OpenGL Utility Toolkit (GLUT) .....................212 A Sample Program with Mesa and GLUT ...................212 Mesa and 3DFX Hardware Acceleration ....................215 Classes for Using Mesa .............................216 vi Contents l3d_screen_mesa ...............................216 l3d_screen_info_rgb_mesa, l3d_screen_info_indexed_mesa ........219 l3d_dispatcher_mesa .............................220 l3d_rasterizer_2d_mesa_imp.........................222 Running the Sample Programs with Mesa ...................224 Summary ......................................226 Chapter 4 3D Vision and Perception ...............227 Overview ......................................227 The Goal of 3D Graphics .............................227 Moving from 2D to 3D .............................229 Vision and Optics: 3D in the Physical World ...................230 The Retina: Biochemical Vision ........................230 The Geometry of Light .............................230 Single Point..................................231 Two Points ..................................232 Lines .....................................233 A Cube ....................................235 Parallax .....................................236 Definition of Parallax ............................236 How Parallax Comes About .........................236 Why Parallax is Important ..........................238 “Runway Lights”—A Simple 3D Program...................238 Runway Lights: Physical Situation .....................238 Code Listing .................................239 Summary ......................................246