Tải bản đầy đủ (.pdf) (527 trang)

pro java 6 3d game development

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (14.7 MB, 527 trang )

this print for content only—size & color not accurate
7" x 9-1/4" / CASEBOUND / MALLOY
(1.0625 INCH BULK 528 pages 50# Thor)
The eXPeRT’s VOIce
®
In JaVa

TechnOlOgy
Andrew Davison
Pro
Java

6 3D
Game Development
Java 3D,

JOGL, JInput, and JOAL APIs
Learn the key techniques needed to create fun, exciting
3D games in Java

technology, with an emphasis on
3D landscape construction
eMPOWeRIng PRODUcTIVITy FOR The JaVa

DeVelOPeR
Pro Java

6 3D Game Development:
Java 3D,

JOGL, JInput, and JOAL APIs


Dear Reader,
Throw away your keyboard and mouse, and explore strange new lands filled
with mysterious objects (cows frozen in blocks of ice, chirping penguins, gold-
en globes with wavering eyes), armed with a gamepad, a power glove, or just
your bare hands.
In this book, I show you how to develop and program 3D games in Java™
technology on a PC, with an emphasis on the construction of 3D landscapes. I
assume you have a reasonable knowledge of Java, the sort of things picked up
in a first Java course at school.
Different from other Java games programming books, here you’ll find the
focus is on the latest 3D APIs, covering a range of libraries that you can’t find
described in one place anywhere else.
I’ve split the topics into three sections: Java 3D

API, nonstandard input
devices for game playing, and JOGL. Java 3D is a high-level 3D graphics API,
and JOGL is a lower-level Java wrapper around the popular OpenGL graphics
API. I look at three nonstandard input devices: the webcam, the gamepad, and
the P5 data glove. Along the way, I utilize several other games-related libraries,
including JInput, JOAL, JMF, and Odejava. I describe the latest Java SE 6 features
relevant to gaming, including splash screens, JavaScript

scripting, and the
desktop and system tray interfaces.
Read, enjoy, and create—new techniques and endless possibilities await,
using Java 6 for your 3D games programming.
Regards,
Andrew Davison
THE APRESS JAVA™ ROADMAP
Beginning Java™ SE 6

Beginning Java™ Objects,
Second Edition
Java™ 6
Platform Revealed
Pro Java™ 6 3D
Game Development
Pro Java

6 3D Game Development:
Java 3D,

JOGL, JInput, and JOAL APIs
Davison
cyan
MagenTa
yellOW
BlacK
PanTOne 123 c
The unconventionally
handsome author of
Killer Game Programming
in Java
ISBN-13: 978-1-59059-817-7
ISBN-10: 1-59059-817-2
9 781590 598177
5 9 0 0 0
Shelve in
Java/Games Programming
User level:
Intermediate–Advanced

www.apress.com
java.apress.com
SOURCE CODE ONLINE
Companion eBook

See last page for details
on $10 eBook version
Companion
eBook Available
Includes
Java

SE 6
Platform
Includes
Java

SE 6
Platform
/>Andrew Davison
Pro Java

6 3D Game
Development
Java 3D

, JOGL, JInput,
and JOAL APIs
817-2FM.qxd 3/27/07 3:40 PM Page i
Pro Java™ 6 3D Game Development: Java 3D™, JOGL, JInput, and JOAL APIs

Copyright © 2007 by Andrew Davison
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,
electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner and the publisher.
ISBN-13: 978-1-59059-817-7
ISBN-10: 1-59059-817-2
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence
of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark
owner, with no intention of infringement of the trademark.
Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the
United States and other countries. Apress Inc. is not affiliated with Sun Microsystems, Inc. and this book
was written without endorsement from Sun Microsystems, Inc.
Lead Editor: Steve Anglin
Technical Reviewers: Chien Yang and Shawn Kendall
Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Jason Gilmore, Jonathan Gennick,
Jonathan Hassell, Chris Mills, Matthew Moodie, Jeffrey Pepper, Paul Sarknas, Dominic Shakeshaft,
Jim Sumser, Matt Wade
Project Manager: Denise Santoro Lincoln
Copy Edit Manager: Nicole Flores
Copy Editor: Jennifer Whipple
Assistant Production Director: Kari Brooks-Copony
Senior Production Editor: Laura Cheu
Compositor: Gina Rexrode
Proofreader: Elizabeth Berry
Indexer: Becky Hornyak
Artist: Andrew Davison, April Milne
Cover Designer: Kurt Krames
Manufacturing Director: Tom Debolski
Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor,

New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail
, or
visit .
For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley,
CA 94710. Phone 510-549-5930, fax 510-549-5939, e-mail , or visit .
The information in this book is distributed on an “as is” basis, without warranty. Although every precau-
tion has been taken in the preparation of this work, neither the author(s) nor Apress shall have any
liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly
or indir
ectly b
y the infor
mation contained in this work.
The source code for this book is available to readers at in the Source Code/
Download section.
It is also available at the author’s web site at
/>817-2FM.qxd 3/27/07 3:40 PM Page ii
To Supatra and John
817-2FM.qxd 3/27/07 3:40 PM Page iii
817-2FM.qxd 3/27/07 3:40 PM Page iv
Contents at a Glance
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
About the Technical Reviewers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
PART 1
■ ■ ■
Java 3D
■CHAPTER 1 Introducing Java 3D. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
■CHAPTER 2 Get a Life (in 3D). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
■CHAPTER 3 Get a Life (the Java 6 Way) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49

■CHAPTER 4 The Colliding Grabbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
■CHAPTER 5 When Worlds Collide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
■CHAPTER 6 A Multitextured Landscape . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
■CHAPTER 7 Walking Around the Models. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
■CHAPTER 8 More Backgrounds and Overlays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
PART 2
■ ■ ■
Nonstandard Input Devices
■CHAPTER 9 Webcam Snaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
■CHAPTER 10 Navigating a 3D Scene by Waving Your Arm . . . . . . . . . . . . . . . . . . . . . . . . 251
■CHAPTER 11 Building a Gamepad Controller with JInput . . . . . . . . . . . . . . . . . . . . . . . . . 273
■CHAPTER 12 Gamepad Grabbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
■CHAPTER 13 3D Sound with JO
AL
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
327
■CHAPTER 14 The P5 Glove . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
PART 3
■ ■ ■
JOGL
■CHAPTER 15 T
w
o JOGL Prog
ramming Frameworks
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
■CHAPTER 16 Touring the World. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
■CHAPTER 17 Loading Models. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
475
v

817-2FM.qxd 3/27/07 3:40 PM Page v
817-2FM.qxd 3/27/07 3:40 PM Page vi
Contents
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
About the Technical Reviewers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
PART 1
■ ■ ■
Java 3D
■CHAPTER 1 Introducing Java 3D. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Overview of Java 3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Overview of the Scene Graph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Some Java 3D Scene Graph Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
The HelloUniverse Scene Graph. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Java 3D Strengths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
The Scene Graph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Performance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Unique Features. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Java Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Documentation and Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Criticisms of Java 3D for Games Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Java 3D’s Level of Abstraction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Java 3D Games . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Java 3D Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Alternatives to Java 3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
■CHAPTER 2 Get a Life (in 3D). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
The Game of Life . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Running Life3D. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

16
Configuring Life3D. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
A Life3D Screensaver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
17
An Overview of the Life3D Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Deciding How to Start . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
vii
817-2FM.qxd 3/27/07 3:40 PM Page vii
Displaying the 3D Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Integrating Java 3D and Swing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Window Sizing .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Processing Key Presses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Scene Graph Creation .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Lighting the Scene. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
The Scene’s Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Building the Cells Grid and Making It Behave. . . . . . . . . . . . . . . . . . . . . . . . 25
Viewer Positioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Viewer Movement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Behaviors in Java 3D. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
A Time-Based Behavior. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Managing the Grid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Accessing Properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Creating the Grid Scene Graph. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Updating the Cells States . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Will the Cell Live or Die? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Rotating the Grid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
The Cell. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Building the Cell’s Scene Graph. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

Coloring the Cells. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Setting the Cell’s Visibility. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Changing a Cell’s Life State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Visual Changes to a Cell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Time for Screensavers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Changing Life3D into a Screensaver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Problems with Screensavers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
The SaverBeans SDK. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
More Life Required? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
■CHAPTER 3 Get a Life (the Java 6 Way) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Life3D Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
An Overview of the Life3D Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Making a Splash. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Anima
ting a Clock
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
53
Drawing onto a Splash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Dra
wing the Clocks
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
JAR Packaging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Adding ClockAnimation to Life3D. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
■CONTENTSviii
817-2FM.qxd 3/27/07 3:40 PM Page viii
The Desktop API. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Using the Desktop Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
What Other Browser Capabilities Are There? .
. . . . . . . . . . . . . . . . . . . . . . . 57

The System Tray . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Creating Life3D’s Popup Menu.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
The Menu Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Creating the TrayIcon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Building the Popup Menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Listening for Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Using a Text Editor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Launching an E-mail Client. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Listening for the Check Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Scripting in Java SE 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Executing a Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Communicating with a Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Speeding Things Up. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Calling Script Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Letting a Script Use Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
More Scripting Information. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Scripting in Life3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Initializing the Grid. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Changing the Grid’s State. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Executing the Script Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
The Rules Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
■CHAPTER 4 The Colliding Grabbers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Building Articulated Arms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Class Diagrams for Arms3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Creating the Application Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Drawing the 3D Scene. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Processing the Keys. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Monitoring Grabber Joints. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89

Managing the Grabbers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Adding the Grabbers to the Scene Graph . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Processing Keyboard Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
93
Translating the Grabbers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Rota
ting a Grabber Joint
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Collecting the Collision Joints. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Collision Detection and Recovery. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Why Bother with JointsBeha
vior?
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
97
Touching the Floor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
■CONTENTS ix
817-2FM.qxd 3/27/07 3:40 PM Page ix
The Grabber . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Making the Grabber’s Appearance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
The Grabber Shape .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Local and Global Coordinate Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Making the Fingers .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Handling Rotation Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Collision Detection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Implementing Collision Detection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Initializing the Bounding Spheres. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Positioning the Bounding Spheres . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Touching the Ground. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115

The Floor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
The Colored Tiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
The Floor’s Axes Labels. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
■CHAPTER 5 When Worlds Collide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Odejava and ODE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Installing Odejava. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Documentation, Examples, and Online Help. . . . . . . . . . . . . . . . . . . . . . . . 123
Bouncing a Ball . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Three-Sta
ge Simulation
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Initializing the Engines. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Initializing Static Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Initializing Dynamic Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Executing the Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Performing a Simulation Step. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Examining the Contact Points. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Cleaning Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Visualizing Balls in a Box. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Creating the Scene . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
The Box. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Managing the Spheres . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
A Sphere . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
I’m Steppin’ Out…. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
A Note of
Application Development
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
143
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144

■CONTENTSx
817-2FM.qxd 3/27/07 3:40 PM Page x
■CHAPTER 6 A Multitextured Landscape. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Surveying the Landscape .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Building the Scene. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Creating the Floor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Start the Balls Moving . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
Moving Around the Scene. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
The Multitextured Floor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
Heights Map Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
The Floor’s Geometry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
The Floor’s Appearance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
The Splash Shape . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
The Splash Shape’s Geometry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
The Splash Shape’s Appearance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Moving Balls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Positioning the Ball . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Moving About . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Driving the Balls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
More Multitexturing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Bump Mapping. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Shaders. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
■CHAPTER 7 Walking Around the Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Populating a Scene . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Class Diagrams for ObjView3D. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
A Spherical Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
A Skybox Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
A Skybox Built with Quads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183

Terragen and NConvert. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
From Images to Textures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
Making the Skybox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Texturing a Plane. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
The Size of the Skybox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
Loading Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Loading OBJ Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
189
The OBJ Model Loader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
190
Positioning a Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
■CONTENTS xi
817-2FM.qxd 3/27/07 3:40 PM Page xi
Ground Cover . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Manipulating the User’s Viewpoint. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Positioning the User’s Viewpoint .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
Moving the Viewpoint at Runtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
Setting Up the Key Behavior .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Processing a Key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
■CHAPTER 8 More Backgrounds and Overlays . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Retained, Immediate, and Mixed Modes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
The Earth, Moon, and Mars. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
Building the Scene. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
The Background. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
Some Variations on a Theme . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
Spinning the Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Building the Scene and Terminating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213

Rotating the Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
Manipulating the Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Rotation and Clipping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Avoiding Gaps in the Rotated Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
Terminating the Thread. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
Dra
wing the Background Image
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
The Model Viewer with a Shifting Background and Overlays . . . . . . . . . . . . . . . 219
Setting Up the Canvas. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Modifying the Key Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
A Canvas with a Background and Overlays. . . . . . . . . . . . . . . . . . . . . . . . . 223
Drawing the Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
The Deep Blue Sea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Adding Overlays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
PART 2
■ ■ ■
Nonstandard Input Devices
■CHAPTER 9 Webcam Snaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Webcam Snaps with TWAIN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Displaying Pictures Using TWAIN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
Snapping a Picture Again and Again and . . . . . . . . . . . . . . . . . . . . . . . . 235
The TWAIN Capture Device. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
TWAIN Timing Tests. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
■CONTENTSxii
817-2FM.qxd 3/27/07 3:40 PM Page xii
Webcam Snaps with JMF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Again Taking Pictures Again and Again. . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
The Capture Device with JMF.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Comparing TWAIN and JMF Capture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
QTJ .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Other Uses for Webcam Snaps. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
■CHAPTER 10 Navigating a 3D Scene by Waving Your Arm. . . . . . . . . . . . . . . . 251
Using the Wrist Strap. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Image Processing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Image Processing Issues. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
Dealing with the Issues. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
Finding the Bands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
FindBands Class Diagrams. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
Image Processing Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
The Bands Analyzer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
The Band Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
Representing a Blob . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Arm Navigation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
Creating the 3D Scene . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Moving the User’
s Viewpoint
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
From Analysis to Action. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Other Approaches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
JAI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
ImageJ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Image Processing Books. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
■CHAPTER 11 Building a Gamepad Controller with JInput. . . . . . . . . . . . . . . . . 273
JInput . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

273
The Gamepad and Windows. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Installing and Testing JInput. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Examining the Input Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
The Other JInput Test Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
Three JInput Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Listing the Controllers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
Viewing Controller Details. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
Testing a Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
■CONTENTS xiii
817-2FM.qxd 3/27/07 3:40 PM Page xiii
A Gamepad Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Initializing the Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
Checking the Components .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
Finding the Rumblers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
Polling the Device .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
Reading the Stick Axes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Reading the POV Hat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
Reading the Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
Using the Rumbler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
Other Approaches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
Swing and JInput . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Constructing the Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
Polling the Gamepad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
Alternatives to JInput. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
■CHAPTER 12 Gamepad Grabbers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
Example Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303

Playing Sounds. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Background Music with SoundsPlayer . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
Obstacle Noises with SoundsPlayer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
Mana
ging Obstacles
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
Making an Obstacle. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
Making the Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
Collision Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
Sending Input to the Grabbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
Processing Keyboard Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
Building a Grabber Operation for a Key Press. . . . . . . . . . . . . . . . . . . . . . . 314
Processing Gamepad Input. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
Building a Grabber Operation for the Gamepad . . . . . . . . . . . . . . . . . . . . . 318
The Grabbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Connecting the Grabbers to the Camera. . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Constructing the Grabbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
Processing an Operation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
The Grabber Arms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
Summar
y
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
326
■CHAPTER 13 3D Sound with JOAL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
Why JOAL and Java 3D?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
Background on OpenAL and JOAL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
328
What About JOAL?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
Installing JOAL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
■CONTENTSxiv

817-2FM.qxd 3/27/07 3:40 PM Page xiv
Managing JOAL Sounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
Initializing JOAL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
Initializing the Listener .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
JOAL Clean Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
Loading a Sound .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
Positioning a Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
Playing, Stopping, and Pausing a Source . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Moving the Listener. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Turning the Listener . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
Using JOALSoundMan. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
Moving a Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
Moving the Listener. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
Moving the Listener Between Sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
Turning the Listener . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
JOAL and Swing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
Other Source Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
Ambient Sounds. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
Cone Sounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
■CHAPTER 14 The P5 Glove. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
Introducing the P5 Glove. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
Using Kenner’s Java API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
Examining the Glove’s Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
What ShowGlove Displays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
ShowGlove Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
Initializing and Terminating the Glove . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356
Polling the Glove . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356

The Glove . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
Updating and Accessing the Glove. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
Closing Down . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
A Specialized Glove Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
Initializing the Glove. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
Updating and Closing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
Getting the Position . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
On a Roll . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
361
Clenching My Fist . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
Pressing the
A Button
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
A Test Rig for FPSGlove. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
■CONTENTS xv
817-2FM.qxd 3/27/07 3:40 PM Page xv
Visiting the Musical Cow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
The Glove Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Adding a Musical Cow.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Initializing the Viewpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Cleaning Up .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
The P5 Glove and JInput . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
PART 3
■ ■ ■
JOGL
■CHAPTER 15 Two JOGL Programming Frameworks . . . . . . . . . . . . . . . . . . . . . . . 377
What Is JOGL? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377

Installing JOGL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
The Callback Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
Using GLCanvas. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
Using GLJPanel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
Rotating a GLCanvas Cube with Callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
Building the Top-Level Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382
Connecting the Canvas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
Building the Listener . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
Measuring FPS Accuracy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
Rotating a GLJPanel Cube with Callbacks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
Building the Panels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
The Background Panel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
Making the 3D Background Transparent . . . . . . . . . . . . . . . . . . . . . . . . . . 392
Timing the GLJPanel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
More Visual Effects with GLJPanel. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
Callback Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
The Active Rendering Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
Rotating a Cube with Active Rendering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
Building the A
pplica
tion
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
396
Dealing with Window Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
Preparing the Canvas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
Thread Rendering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
The Game Life Cycle Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
Statistics Reporting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
Java 3D and JOGL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
More Informa

tion on JOGL and OpenGL
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
■CONTENTSxvi
817-2FM.qxd 3/27/07 3:40 PM Page xvi
■CHAPTER 16 Touring the World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
Application Details .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
Constructing the Canvas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
The Camera Position . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
Responding to Key Presses. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
Let the Rendering Commence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416
Rendering Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
Loading Textures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
Lighting the Scene. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419
Creating the Ground Shapes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
The Rendering Loop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
Updating the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
Rendering the Scene . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423
Drawing the Tree Billboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423
Drawing the Ground Shapes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
The Planet Earth. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
The Skybox. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430
Adding a 2D Overlay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
Drawing the Floor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
Drawing the Tiles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
Drawing the Axes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438
■CHAPTER 17 Loading Models. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
Overviews of the Examples. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439

The OBJ File Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
Shape Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
Grouping Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
Material Use Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
The MTL File Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
The OBJ File Loader. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
Reading in the OBJ File. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
Reading a Face . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
Reading in an MTL File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
Recording Material Use. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
450
Centering and Resizing a Model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
451
Creating a Display List for the Model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
F
inding a Ma
terial
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
Rendering with a Material. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
Rendering a Face. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455
Dra
wing a Model
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
457
When to Create the Display List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
■CONTENTS xvii
817-2FM.qxd 3/27/07 3:40 PM Page xvii
Viewing a Model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
Loading the Model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458
Drawing the Model .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
Other JOGL Model Loaders. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
The TourModelsGL Application.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
Adding Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
Let’s Be Picky. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
Gleem: A Different Way of Picking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
A Singing Penguin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470
The Fog Descends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
■CONTENTSxviii
817-2FM.qxd 3/27/07 3:40 PM Page xviii
About the Author
■ANDREW DAVISON received his PhD from Imperial College in London in
1
989. He was a lecturer at the University of Melbourne for six years before
moving to Prince of Songkla University in Thailand in 1996. He has also
taught in Bangkok, Khon Kaen, and Hanoi.
His research interests include scripting languages, logic programming,
visualization, and teaching methodologies. This latter topic led to an inter-
est in teaching games programming in 1999.
His O’Reilly book,
Killer Game Programming in Java, was published in
2005 and is accompanied by a web site at
/>~ad/jg/
.
One of Andrew’s favorite hobbies is reading to his son, John, (currently
they’re on
The Iron Man by Ted Hughes). He has also been known to play PC and mobile phone

games, but purely for r
esear
ch purposes.
xix
817-2FM.qxd 3/27/07 3:40 PM Page xix
817-2FM.qxd 3/27/07 3:40 PM Page xx
About the Technical Reviewers
■CHIEN YANG is a software engineer at Sun Microsystems, Inc., where he has
w
orked since 1996. He works on graphics technologies for the Java Client
Group. He has worked on the design and implementation of Java 3D API
since its inception. He has also done work on graphics features simulation
and graphics pipeline software. He holds a BS degree in computer science
from the University of Iowa and an MS in computer science from the Uni-
versity of C
alifor
nia at Berkeley.
■SHAWN KENDALL has developed cutting-edge Java and Java 3D–based
game technology demos for F
ull Sail Real World Education and Sun
Microsystems, displayed at various conferences such as GDC and SIG-
GRAPH since 1999. In 2002, Shawn founded Immediate Mode Interactive
(
), a game technology company dedicated to the
use of Java in games. Shawn has been developing in Java since 1995,
starting with JDK 1.0 on SGI machines and Java 3D since 1998. Shawn
gr
aduated from the University of Central Florida with a BS degree in liberal
science in 1995 and a computer science BS in 2002. Shawn maintains
several Java 3D content loaders, as well as a host of Java 3D game demo

pr
ojects.
xxi
817-2FM.qxd 3/27/07 3:40 PM Page xxi
817-2FM.qxd 3/27/07 3:40 PM Page xxii
Acknowledgments
My best regards to the many people at Apress who helped me get this book finished. They include
Steve Anglin (Java editor), Denise Santoro Lincoln (project manager), Jennifer Whipple (copy editor),
and Laura Cheu (production editor). Thanks also to the astute technical reviewers, Chien Yang and
Shawn Kendall. I should also express my gratitude to the numerous people who sent me comments
about the early drafts of this book, which are online at
/>Any remaining technical errors or poorly explained gobbledygook are my fault.
I must acknowledge my department (Computer Engineer
ing), faculty (E
ngineering), and uni-
versity (Prince of Songkla University) for being so supportive. They’ve always been understanding
and have offered every encour
agement. I r
ecommend Thailand as a great place to live and work.
None of this would have been possible, or even worth attempting, without my family, Supatra
and John, who I love very dearly.
xxiii
817-2FM.qxd 3/27/07 3:40 PM Page xxiii
817-2FM.qxd 3/27/07 3:40 PM Page xxiv

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×