8
CORIOLIS GROUP BOOK Advanced Graphics Programming Using C/C++ Loren Heiny John Wiley & Sons, Inc. New York • Chichester • Brisbane • Toronto • Singapore

Advanced Graphics Programming Using C/C++

  • Upload
    others

  • View
    13

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Advanced Graphics Programming Using C/C++

CORIOLIS GROUP BOOK

Advanced Graphics Programming Using

C/C++

Loren Heiny

John Wiley & Sons, Inc. New York • Chichester • Brisbane • Toronto • Singapore

Page 2: Advanced Graphics Programming Using C/C++

Contents Preface

Who This Book Is For A Glance Inside What You'II Need About the Code

Chapter 1 Introduction to Ray Tracing Capturing Images with Ray Tracing

Tracing Light Rays Using a Camera Model From Rays to Images Representing a Scene

A Simple Ray Tracing Program The Major Data Structures Specifying Colors Specifying Light Sources Specifying Objects Specifying Vectors and Points Describing the World

Overview of SIMPLRAY The Main Function Generating Rays Getting Oriented Calculating a Ray Determining What a Ray Hits Working with Planes Representing a Ray Intersecting a Ray and a Plane Representing a Sphere Finding the Closest Object Calculating the Normal

Chapter 2 Adding a Lighting Model A Good Lighting Model

Ambient Intensity Diffuse Light Specular Light

xiii xiii xiv xiv XV

1 1 2 2 5 5 5 6 6 6 7 8 9 9

10 10 11 12 16 16 17 18 20 22 23

25 25 26 26 28

Page 3: Advanced Graphics Programming Using C/C++

vi • Contents

Adding the Light Components Multiple Light Sources

Back to Tracing Rays Applying the Lighting Model

Calculating the Ambient Color Calculating the Diffuse Color Scaling the Light Adding the Specular Component Calculating the Reflecting Ray Handling Shadows Tracing the Ray Setting the Pixel

Compiling SIMPLRAY for DOS Compiling for Windows Choosing a Video Mode

Setting the Video Mode DOS Versus Windows Applications Modifying SIMPLRAY Computing Power and Ray Tracing

Chapter 3 An Enhanced Ray Tracer Using C++ Effectively Overview of the RAY Program The New Objects

Deriving Rectangles Triangle Objects Supporting Spheres Ellipsoids Cylinder Objects

Managing Objects An Enhanced Lighting Model

New Light Sources Point Light Sources Directed Light Sources Cone Light Sources Scaling Light

Managing9Light Sources Translucent Objects Antialiasing Bounding Boxes

29 30 30 32 32 32 34 34 36 36 38 40 40 41 42 43 43 44 45

55 55 56 57 59 63 67 68 69 72 73 73 74 75 76 77 78 80 80 81 83

Page 4: Advanced Graphics Programming Using C/C++

Contents • vii

Building a Hierarchy of Objects 85 Supporting Bounding Boxes 86

Chapter 4 Creating a World 133 The Scene Description Language 133 The Parser 136

Reading Tokens 136 Parsing a File 137 Processing Parameters 138 Handling Nested Commands 138

Creating an Object 139 Grouping Objects 139 Handling Parser Errors 140 Using the Scene Description Language 140

Setting the Display Parameters 140 Setting the Viewing Parameters 141 Specifying Lights 142

Specifying Objects 142 Nesting Files 143 Saving an Image 143

Compiling and Using RAY 144 Sample Scenes 145 Things Are Shaping Up 145 Generating Swept Surfaces 146

Chapter 5 Textures and Patterns 173 Rendering a Pattern 173

Setting the Texture 174 Mapping onto Three-Dimensional Objects 175

Rendering a Checker Pattern 176 Mapping the Checker Pattern 177

Rendering Non-Checker Patterns 177 Mapping to Planar Objects 178 Mapping to a Rectangle 178 Mapping to a Sphere and Ellipse 180 Mapping to a Cylinder 181

Defining a Checker Pattern 182 Mapping Images 183

Managing Images 184 From Point to Pixel 185 Defining an Image Map 186

Page 5: Advanced Graphics Programming Using C/C++

viii • Contents

Blended Colors A Textured Scene

Constructing the Room Constructing the Cabinets Making the Soup Can Making a Pot Using Bounding Boxes

Nothing's Perfect

Chapter 6 Adding Animation Animation Overview Supporting Animation

The Animating Structure Animating Objects Going the Next Step

The Animation Language Specifying the Number of Frames Specifying a Motion Path Moving the Viewer Saving the Image Sequence

Playing Back an Image Sequence An Animation Example

Chapter 7 Rendering Water and Clouds Designing a Model of Water The Essence of Water Animating Waves

Using Normals to Make Waves A Water Object

Creating Waves Adding Water to a Scene

Sample Water Scene A Watery Glass

Extending the Water Model Rendering Clouds A Two-Dimensional Sky

Implementing A Sky Plane Calculating the Texture Defining Two-Dimensional Clouds Cloudy Marble

187 189 189 190 192 192 193 193

199 199 200 201 202 204 204 205 205 205 206 207 207

215 215 216 217 218 218 219 221 221 222 224 224 225 226 227 227 228

Page 6: Advanced Graphics Programming Using C/C++

Contents A ix

Three-Dimensional Clouds Creating Three-Dimensional Clouds The Three-Dimensional Cloud Function

Defining Clouds Sample Clouds

Tips on Constructing Clouds

Chapter 8 Rendering Fractal Mountains Rendering Natural Objects

How Fractals Work Using Triangles

Generating Mountains Reading the Initial Triangles Setting the Fractal Parameters Subdividing Triangles Writing the Triangles Setting the Color Generating a Random Number Using Bounding Boxes

Compiling and Running GENMTN Designing Your Mountains Making More Interesting Images

Chapter 9 A PCX Toolkit Overview of PCX Files

The PCX File Format The PCX File Header Run-Length Encoding Decoding a PCX File

The PCX Toolkit Processing the Header Initializing a Header Writing the Header Writing an Image Processing the Palette

Reading a PCX File Reading a Header Reading the Image A Sample Read Function

Testing PCXTOOL.C

229 229 231 233 233 236

237 237 238 238 239 240 241 242 243 244 245 246 247 249 250

271 271 272 272 274 275 275 276 277 278 278 279 279 280 280 281 282

Page 7: Advanced Graphics Programming Using C/C++

x • Contents

Chapter 10 TIFF: A Flexible Image File Format TIFF Overview

Working with Classes The Pieces of a TIFF File Working with Tags The Image Format

The TIFF.C Toolkit Reading a TIFF File Processing the Tags Determining an Image's Size Reading the Image A High-Level Read Function Writing a TIFF File Writing the Image Data

Testing the TIFF Tools Compiling and Running TESTTIFF

Chapter 11 Image Processing Essentials Working with Images An Image Processing Toolkit

Using Histograms Contrast Enhancement Brightness Adjustment Reducing Noise Sharpening an Image Zooming and Resizing Images Mirroring and Rotating Images Converting a Color Image to Grayscale Colorizing an Image Gamma Correction

Testing IMTOOLS.C

Chapter 12 Morphing Magic How Morphing Works

Warping Images Mapping Images

Creating a Simple Morphing Program Controlling the MORPH Program Initializing the Morphing Process Generating Images Moving the Triangle Patches

293

293 294 294 296 299 299 300 302 303 303 304 305 306 306 308

331

331 332 333 334 335 336 336 338 339 340 340 341 342

355

355 355 356 357 357 358 359 359

Page 8: Advanced Graphics Programming Using C/C++

Contents • xi

Morphing the Images 360 Determining a Point in a Triangle 361 Generating the Output Color 362

Compiling MORPH 363 Using MORPH 363 Morphing Tips 365

Appendix A Vector Operations 377 Working with Vectors 377 Vector Operations 379

Appendix В A Graphics Toolkit 385 Setting the Mode 385 Setting a Pixel 386 Working with Colors 387 Supporting С and C++ Compilers 388

Appendix С Working with Windows 395 Overview of WINSHELL 395 Using WINSHELL 396

Preparing for Windows 396 Displaying an Image 396 Working with Colors 397

Compiling for Windows 397

Index 401