Tài liệu Teach Yourself DirectX 7 in 24Hours - Pdf 95

A Division of Macmillan USA
201 West 103rd St., Indianapolis, Indiana, 46290
Robert Dunlop
with Dale Shepherd,
Mark Martin, et al
DirectX 7
in
24
Hours
Teach Yourself

00 1634xFM 11/13/99 10:50 AM Page i
Sams Teach Yourself DirectX

7 in 24
Hours
Copyright  2000 by Sams Publishing
All rights reserved. No part of this book shall be reproduced, stored in a
retrieval system, or transmitted by any means, electronic, mechanical, photo-
copying, recording, or otherwise, without written permission from the pub-
lisher. No patent liability is assumed with respect to the use of the information
contained herein. Although every precaution has been taken in the preparation
of this book, the publisher and author assume no responsibility for errors or
omissions. Nor is any liability assumed for damages resulting from the use of
the information contained herein.
International Standard Book Number: 0-672-31634-x
Library of Congress Catalog Card Number: 98-83220
Printed in the United States of America
First Printing: December, 1999
01 00 99 4 3 2 1
Trademarks

TEAM COORDINATOR
Meggo Barthlow
MEDIA DEVELOPER
Dan Scherf
INTERIOR DESIGNER
Gary Adair
COVER DESIGNER
Aren Howell
COPY WRITER
Eric Borgert
PRODUCTION
Brad Lenser
00 1634xFM 11/13/99 10:50 AM Page ii
Contents at a Glance
Introduction 1
PART I Introduction to DirectX 3
1 About DirectX—The Pieces That Make It Happen 5
PART II Getting Started with DirectDraw 15
2 Our First Step—DirectDraw in a Windows Application 17
3 Moving On—Grabbing Control of the System 39
4 Creating the Game Loop 55
5 Make It Move—DirectDraw Animation Techniques 75
PART III Adding Music and Sound 95
6 DirectSound—Adding Ambience and Sound Effects to Your Game 97
7 Applying DirectSound 113
PART IV Welcome to 3D 133
8 DirectMusic—Interactive Music 135
9 Applying DirectMusic 153
10 Introduction to 3D Concepts 181
11 Rendering the 3D Scene 197

HOUR 1 About DirectX—The Pieces That Make It Happen 5
What Is DirectX? 5
DirectX Components 6
The DirectX SDK 6
The DirectX Media SDK 7
Preparing to Use DirectX 7
Preparing the Compiler 8
Including the SDK in Your Projects 9
A Brief Introduction to COM 9
Reusable Interfaces and Backward Compatibility 10
The
IUnknown Base Class 11
Querying for Interfaces 11
Reference Counting in COM 12
Summary 13
Q&A 13
Workshop 14
Quiz 14
Exercises 14
P
ART II Getting Started with DirectDraw 15
HOUR 2 Our First Step—DirectDraw in a Windows Application 17
Taking a Look at the Video System 18
The Components of DirectDraw 18
The
IDirectDraw7 Interface 19
DirectDraw Surfaces 21
Keeping Inside the Lines with the DirectDraw Clipper 22
Drawing on a Surface 23
A Function to Load Bitmaps to a DirectDraw Surface 23

Summary 53
Q&A 53
Workshop 54
Quiz 54
Exercises 54
H
OUR 4 Creating the Game Loop 55
Conceptual Overview of the Game Loop 55
Writing a Better Message Loop 57
Achieving Smooth Playback 58
Using Timers in the Loop 59
Selecting the Timer 59
Your First Piece of Animation: A Scrolling Background 61
Setting Up the Application 61
Setting Up Initialization 62
Controlling Motion Through Keyboard Input 64
Cleanup on Exit 65
Creating Your Game Loop 66
Rendering a Scrolling Background 69
vi Sams Teach Yourself DirectX 7 in 24 Hours
00 1634xFM 11/13/99 10:50 AM Page vi
Summary 72
Q&A 73
Workshop 73
Quiz 73
Exercises 73
H
OUR 5 Make It Move—DirectDraw Animation Techniques 75
Transparent Blits 75
The Two Flavors of Color Key 76

Quiz 111
Exercises 111
Contents vii
00 1634xFM 11/13/99 10:50 AM Page vii
HOUR 7 Applying DirectSound 113
DirectSound and Games 114
Working with Waves 115
A High-Level Use for Waves 115
Creating the
CWave Class 117
Using the
CWave Class 123
Playing Sound Effects with DirectSound 124
Summary 131
Q&A 131
Workshop 131
Quiz 131
Exercises 132
P
ART IV Welcome to 3D 133
HOUR 8 DirectMusic—Interactive Music 135
The Features of DirectMusic 136
The Microsoft Synthesizer 137
The Age of Interactive Music 139
Dynamic Creation of Music 139
Composition of Music in the Digital Realm 141
A Quick Primer on Musical Structure 142
Storing Compositions with the MIDI Format 143
Multitrack Music Synthesis 144
DirectMusic Architecture 144

Changing the Music to Reflect the Scene 175
Summary 177
Q&A 177
Workshop 179
Quiz 179
Exercises 180
H
OUR 10 Introduction to 3D Concepts 181
An Overview of the 3D Process 182
Object Definitions 182
The 3D Pipeline 182
Defining Locations in 3D Space 184
The New Origami—Building Objects in 3D 185
Relative Coordinates—Origins and Vectors 186
Matrixes—Making the World Go Round 187
The World Transform 189
The View Transform 194
Summary 195
Q&A 195
Workshop 196
Quiz 196
Exercises 196
H
OUR 11 Rendering the 3D Scene 197
Hierarchy of Direct3D Immediate Mode 197
The IDirect3D7 Interface 198
The
IDirect3DDevice7 Interface 198
The Viewing Frustum 199
Clipping the Frustum to the Field of View 199

Initializing the Application 227
Initializing DirectDraw for Use with Direct3D 228
Initializing Direct3D 230
Setting Up a Viewport 231
Object Creation 232
Putting It in Motion 233
Rendering the Scene 236
Handling User Input 238
The Finishing Touches 240
Running the Application 243
Summary 244
Q&A 244
Workshop 245
Quiz 245
Exercises 245
x Sams Teach Yourself DirectX 7 in 24 Hours
00 1634xFM 11/13/99 10:50 AM Page x
HOUR 13 Adding Textures and Z-Buffers to the Scene 247
Preparing a Z-Buffered Device 249
Selecting a Z-Buffer 249
Creating and Attaching the Z-Buffer 251
Enabling and Disabling the Z-Buffer 253
Adding Textures 253
Load a Texture 254
Prepare Geometry 259
Set Up the Device and Render 260
Summary 263
Q&A 263
Workshop 264
Quiz 264

00 1634xFM 11/13/99 10:50 AM Page xi
Converting 3D Studio Files: The CONV3DS Utility 300
Reading the X File Format 300
Adding Vehicles to Our Application 301
Reading the Model 301
Setting Up the Hierarchy 302
Adding the Object to the Scene 304
Cleaning Up 305
Summary 306
Q&A 306
Workshop 306
Quiz 306
Exercises 307
H
OUR 16 Modeling a Complex World—Applying Physics and Object
Hierarchies 309
Reactions and Effects: Defining Real-World Relationships 310
Motion Dynamics 311
Forward Kinematics 312
Pivot Points and Other Kinematics Features 312
Inverse Kinematics 314
Object Hierarchies 314
Using Matrices to Combine Motions 315
Object Interaction 318
Collision Detection 319
Object Reactions 321
Adding Collision Detection to Our Application 324
Defining Object Bounds 325
Testing for Collisions 328
Animating Reaction Vectors 329

Putting Force Feedback to Work 361
Enumerating and Creating Force Feedback Devices 363
Enumerating Force Feedback Effects 365
Creating Force Feedback Effects 367
Playing Force Feedback Effects 370
Altering Force Feedback Effects 371
Force Feedback Effect Recipes 371
Summary 373
Q&A 373
Workshop 374
Quiz 374
Exercises 374
H
OUR 19 3D Sound—From Panning to Doppler Effects 375
Introduction to 3D Sound 376
The Physics of Sound 376
How We Perceive Sound 378
The Architecture of DirectSound3D 380
DirectSound 3D Components 380
DirectSound 3D Buffers 380
DirectSound 3D Listener 386
Adding DirectSound 3D to Your Application 391
Creating the Listener 392
Loading Sounds 394
Creating the 3D Buffer 394
Animating Sound Effects 395
Release Interfaces 396
Summary 397
Q&A 398
Contents xiii

Lobby Support for the Game Client 434
Registering the Game as Lobbyable 435
Users, Groups, and Sessions 437
Providing Chat Services 439
Creating a Session 441
Launching a DirectPlay Lobby Application 443
Making the Game Lobbyable 443
Sending and Receiving Lobby Messages 444
Cleanup 445
Summary 445
Q&A 445
Workshop 446
Quiz 446
Exercises 446
xiv Sams Teach Yourself DirectX 7 in 24 Hours
00 1634xFM 11/13/99 10:50 AM Page xiv
PART VIII The DirectMedia SDK 447
HOUR 22 Adding Video with DirectShow 449
Introducing DirectShow 450
Video Playback Capabilities 450
Internet Streaming Video Applications 451
Graph Filters 454
Stringing Filters Together 455
Sample DirectShow Application 456
Initializing DirectShow 456
Setting Up the Filter 457
Setting Up the File Stream 458
Streaming the Movie 460
Cleanup 461
Summary 462

00 1634xFM 11/13/99 10:50 AM Page xv
HOUR 24 Integrating Media Into Web Pages and Applications with
DirectAnimation 487
DirectAnimation—One API, Many Uses 488
Scratching the Surface—A Look at the Interfaces 489
DirectAnimation Architecture 490
The DirectAnimation Model 491
A Versatile Programming Environment 492
DirectAnimation Programming in C++ 493
Using Scripts to Use DirectAnimation on the Web 494
Sample DirectAnimation Application in C++ 494
Initializing DirectAnimation 495
Loading 2D Images 500
Creating a 3D Object 501
Adding Some Motion 502
Creating the Camera and Lighting 503
Adding Sound 503
Animating the Scene 504
Cleanup 505
Doing it the DirectDraw Way 505
Sample Web Page Using DirectAnimation 506
Summary 508
Q&A 509
Workshop 509
Quiz 509
Exercises 510
P
ART IX Appendixes 511
APPENDIX A Answers 513
Hour 1, “About DirectX—The Pieces That Make It Happen” 513

Hour 15, “Importing 3D Objects and Animations Into the Scene” 524
Quiz 524
Hour 16, “Modeling a Complex World—Applying Physics and Object
Hierarchies” 524
Quiz 524
Hour 17, “Introducing DirectInput—Getting User Input” 525
Quiz 525
Hour 18, “Getting Through to the User—Force Feedback” 526
Quiz 526
Hour 19, “3D Sound—From Panning to Doppler Effects” 527
Quiz 527
Hour 20, “Putting Your Game on the Net—Writing Multiplayer Titles” 528
Quiz 528
Hour 21, “Game Central—Creating Lobbies” 530
Quiz 530
Hour 22, “Adding Video with DirectShow” 531
Quiz 531
Hour 23, “Bring Surfaces to Life with DirectX Transform” 531
Quiz 531
Hour 24, “Integrating Media Into Web Pages and Applications with
DirectAnimation” 532
Quiz 532
Exercise 533
A
PPENDIX B Prepare Your Application for Distribution with DirectSetup 535
Index 541
Contents xvii
00 1634xFM 11/13/99 10:50 AM Page xvii
About the Authors
Robert Dunlop is Microsoft’s first and only MVP (Most Valuable Professional) for

advent of DirectDraw and has continued to use both technologies in his component
development.
xviii Sams Teach Yourself DirectX 7 in 24 Hours
00 1634xFM 11/13/99 10:50 AM Page xviii
Recognition from the
Publisher
Sams Publishing would like to give a special thanks to Dale Shepherd. His timely and
accurate assistance on this project helped ensure its completion with true coverage of
Microsoft’s newest version of Microsoft’s DirectX—DirectX 7. By helping on this pro-
ject, Dale has created this, the best book available, for the new user of DirectX 7.
Contents xix
00 1634xFM 11/13/99 10:50 AM Page xix
Tell Us What You Think!
As the reader of this book, you are our most important critic and commentator. We value
your opinion and want to know what we’re doing right, what we could do better, what
areas you’d like to see us publish in, and any other words of wisdom you’re willing to
pass our way.
As an Associate Publisher for Sams, I welcome your comments. You can fax, email, or
write me directly to let me know what you did or didn’t like about this book—as well as
what we can do to make our books stronger.
Please note that I cannot help you with technical problems related to the topic of this
book, and that due to the high volume of mail I receive, I might not be able to reply to
every message.
When you write, please be sure to include this book’s title and author as well as your
name and phone or fax number. I will carefully review your comments and share them
with the author and editors who worked on the book.
Fax: 317-581-4770
Email: [email protected]
Mail: Associate Publisher
Sams Publishing

of functionality in their latest release, and they’ve made it easier than ever for you, as
programmer and designer, to use it.
So sit back, grab a can of pop, and enjoy our fun and fast ride. We’ve got a lot of ground
to cover, and only 24 hours to cover it in. You’ll enjoy every minute of it, as we’ve
enjoyed writing it. You’re well on your way to writing the next blockbuster fully immer-
sive and multiplayer 3D game. And we’re exited to see what will be new in DirectX 8.
01 1634xIntro 11/13/99 11:09 AM Page 1
01 1634xIntro 11/13/99 11:09 AM Page 2
Hour
1 About DirectX—The Pieces That Make It
Happen
PART I
Introduction to DirectX
02 1634xPart I 11/13/99 11:09 AM Page 3
02 1634xPart I 11/13/99 11:09 AM Page 4
HOUR
1
About DirectX—The
Pieces That Make It
Happen
This hour will prepare you for the first steps in learning to program with
DirectX by providing a general overview of DirectX and what you will need
to begin programming in DirectX.
In this hour, you will

Learn just what DirectX is

Learn the components of DirectX

Learn how to use DirectX


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status