Title: 3D Game Programming
13D Game Programming
- ???
- kevin.cwang_at_msa.hinet.net
- 2005
2????
- Introduction to Game Development (3hr)
- Game System Analysis (3hr)
- The Game Main Loop (3hr)
- 3D Game Engine Training (Using TheFly3D) (6hr)
- Game Mathematics Geometry (3hr)
- Terrain (3hr)
- Characters (3hr)
- Game Control System (3hr)
- Advanced Scene Management System (3hr)
- Game AI (6hr)
- Game Physics (3hr)
- Game FX (3hr)
- Network Gaming (3hr)
- MMOG (3hr)
- Summary
- Wangs Method for Real-time 3D Game Development
3????
- One term project
- The students are in teams.
- Use TheFly3D 3D engine to code a Real-time 3D
Game. - Action RPG
- The teacher will provide graphics materials and
setup the game design. - Final examination
- Homework will be closely coupled with the term
project.
4??? (1/3)
- ??
- ?????? ???? / ??
- ???? ??
- ???? 3D????
- ??
- ????????????
- ??
- 04-04 ??????????? ????
- 97-04 ?????????? ???
- 96-96 ?????????????? ????
- 93-96 ???????????? ????
- 90-93 ???????? ?????
- 89-90 ?????????? CAE Lab ????
5??? (2/3)
- Game??
- Current
- TheFly3D 3D Engine
- ????
- DragonFly 3D Game Engine
- M2????, VRLobby, ???
- Lizard 3D Game Engine
- ????????? I II????? I II??????????????????????
?????????? - ??????
- Ultimate Fighter
- 1st real-time 3D fighting game in Taiwan
6??? (3/3)
- ?? (Expertise)
- 3D Computer Graphics
- Geometric Modeling
- Numerical Methods
- Character Animation
- Photo-realistic Rendering
- Real-time Shading
- Volume Rendering
- ???? (Applications)
- ??3D???? (Real-time 3D Game Development)
- ???? (Computer Animation)
- ???? (Virtual Reality)
- ?????? (Computer-aided Design, CAD)
- ???? (Scientific Visualization)
71st Introduction to Game Development
8Introduction to Game Development
- Game platform
- Game types
- Game team
- Game development pipeline
- Game software system
- Tools
9Game Platform
- PC
- Single player
- Match Makings
- MMOG (Massive Multi-player Online Game)
- Web-based Games
- Console
- Sony PS2
- MS Xbox
- Nintendo GameCube
- Arcade
- Mobile
- Nintendo GBA
- Nintendo DS
- Sony PSP
- Hand-held
10Game Development on PC
- PC is designed for general office application.
- Not for entertainment purpose
- A virtual memory system
- Unlimited system memory
- But video memory is limited.
- For frame buffers, z buffers, textures, vertices,
- PCI / AGP might be a problem for performance.
- Open architecture
- Hardware driver version issue
- Different capabilities
- Different performance
- Compatibility test is very important.
- Development is easy to setup.
- Visual C/C with DirectX
11Game Development for Consoles
- Specific hardware designed for games
- Single user OS
- Single process OS
- No hard disk drive (?)
- Closed system
- Native coding environment
- Proprietary SDK
- Hardware related features
- C language with assembly
- Limited resources
- Memory for everything
- 32M for PS2
- 64M for Xbox
- One console runs, the others do !
- Use gamepad and no keyboard
12Game Types
- RPG (Role playing games)
- AVG (Adventure games)
- RTS (Real-time strategy games)
- FPS (First-person shooting games)
- RSLG (??)
- STG
- Sports
- Action
- Puzzle games
- Table games
- MMORPG
- Massive Multiple Player Online Role Playing Games
13Game Team Members
- ????
- ???
- ?????
- ????
- ????
- ????
- ??????
- ????(PM)
- ????
- ???????
- Game project approval
- ??????
- ???? game master (GM)
- Customer services
- MIS
14Game Producer ?????
- Team leader (usually)
- ???? (Resource management)
- ???? (Administration)
- ???? (Project management)
- ???? (Upward management)
- ?????
- ????
15???????
- ??????
- Daily ??
- House keeping
- Meeting coordinator
- Schedule checking
- Cross-domain communication
- Usually not a full-time job position
- A position for training and becoming a producer
16???? (1/2)
- ???? (Story telling)
- ???? (Scripting)
- ???? (Game play design)
- ????(Character design)
- ????(Animation design)
- ???? (Level design)
- ????(Effect design)
- ????
- ????(User Interface design)
- ???? (Game tuning)
- ???? (Numerical setup)
- AI ?? (Game AI design)
- ???? (Sound FX setup)
17???? (2/2)
- ???? (Scene setup)
- Game document writing
- Game quality checking
18????
- Visual setup for game design
- 2D setup
- 3D setup
- Graphics design and production
- ?? (Terrain)
- ?? (Character)
- ?? (Models)
- ?? (Textures)
- ?? (Motion / Animation)
- ?? (FX)
- User Interface
- ???? (??.??..?)
19????
- ???? (Game Program) ??
- ?????? (Game Tools) ??
- Level editor
- Scene editor
- FX editor
- Script editor
- Game editor
- ??Data exporters from 3D animation Software
- 3dsMax / Maya / Softimage
- Game engine development
- Game technique research
- Online game server development
20??????
Basic Procedures for Game Development
Idea
Proposal
Production
Integration
Testing
Debug
Tuning
Concept Approval
Prototype
Pre-alpha
Alpha
Beta
Final
- ?? (Idea)
- ?? (Proposal)
- ?? (Production)
- ?? (Integration)
- ?? (Testing)
- ?? (Debug)
- ?? (Tuning)
gt Concept approval
gt ?? (prototype)
gt Pre-alpha
gt Alpha
gt Beta
21????(Concept Design)
- ???? (Game types)
- ????? (Game world)
- ?? (Story)
- ???? (Features)
- ???? (Game play)
- ???? (Game product positioning)
- Target player
- Marketing segmentation / positioning
- ??????
- ???? (Risk)
- SWOT (Strength/Weakness/Opportunity/Threat) ??
- ???
- Concept Design Document (CDD)
22????(Proposal)
- ???? (System analysis)
- GDD ?? (Game design document)
- MDD ?? (Media design document)
- TDD ?? (Technical design document)
- ?????? (Game project)
- Schedule
- Milestones / Check points
- Risk management
- ?????
- ???? (Team building)
- ???
- GDD
- MDD
- TDD
- The Team
23????(Production)
- ??????
- Modeling
- Textures
- Animation
- Motion
- FX
- ???? (Coding)
- ??????
?? !
24????(Integration)
- ???? (Level integration)
- ???? (Number tuning)
- ???? (Audio)
- ??????
- ???????
- Testing within the game team
- Focus group (User study)
- Release some playable levels for focus group.
- Get the feedback from focus group to adjust the
game play. - Invited outside game players but evaluation
in-house
25????(Test)
- Alpha ??
- ?? (Debug)
- Make the game stable
- Beta ??
- ????
- Game play ??
- ??????? (MMOG)
- ???? (Closed beta)
- Invited game players
- ???? (Open beta)
- Free for public players
- ???? (Critical testing)
- Only for MMOG
- Continuously implementing
- For servers
26Bugs
- Bug ?? (Bug Classification)
- A Bug
- B Bug
- C Bug
- S Bug
- Principles
- Bug ????
- Tester vs Debugger
27Game Software System
Game
Game Play Layer
Engine Layer
System Layer
Hardware
28System Layer APIs (1/2)
- 3D Graphics API
- DirectX 9.0 SDK Direct3D
- Newest update DirectX 9.0c SDK Update (June,
2005) - OpenGL
- 2.0
- 2D API
- DirectX 9.0 SDK - DirectMedia
- Win32 GDI
- Input device
- DirectX 9.0 SDK DirectInput
- Audio
- DirectX 9.0 SDK DirectSound / Direct3DSound /
DirectMedia - OpenAL
29System Layer APIs (2/2)
- OS API
- Win32 SDK
- MFC
- Network
- DirectX 9.0 SDK DirectPlay
- Socket library
30Engine Layer (1/2)
- 3D scene management system
- Scene graph
- Shaders
- 2D sprite system
- Audio system
- Gamepad
- Hotkeys
- Mouse
- Timers
- Network
- DDK interface
31Engine Layer (2/2)
- Terrain system
- Advanced scene management system
- Space partition technique
- BSP Tree
- Octree
- Character system
- Bone-skin
- Motion Blending
- Dynamics
- Particle system
- Rigid-body dynamics
- Collision detection
- Sound FX
- User interface
32Game Play Layer
- NPC (Non-playable characters) management
- Game AI
- Path finding
- Finite state machine (FSM)
- Steering behavior
- Avatar
- Combat system
- FX system
- Script system
- Trading system
- Number system
33Game Development Tools for Programming (1/2)
- System Tools
- Visual C/C
- .Net 2003
- VC/C 7.1
- Visual C/C 6.0 SP5
- NuMega BoundsChecker
- Finding memory leaking
- Intel vTune
- Finding computation performance bottlenecks
- for CPU
- PIX
- Finding graphics performance bottlenecks
- For GPU
34Game Development Tools for Programming (2/2)
- SDKs
- System API
- Win32 SDK or MFC
- DirectX SDK or OpenGL
- Socket library
- Middleware (Game engine)
- Renderware
- Unreal
-
- Physics
- ODE
35Game Development Tools for Artists
- 3D tools
- Discrete 3dsMax
- Maya
- Softimage XSI
- 2D tools
- Photoshop
- Illustrator
- Motion tools
- Motion capture devices
- Motion Builder
- FiLMBOX
362nd Game System Analysis
37What Will We Talk
- The idea about system analysis (SA)
- Mind mapping
- Case study - Term project
38Why System Analysis (1/2)
- For ???? analysis
- The main program
- Game development tools
- To identify ???
- New game engine ?
- Re-used code ?
- Tools needed to be developed ?
- For ?? management
- Total man month
- How many programmers ?
- How good the programmers ?
- For job dependency analysis
39Why System Analysis (2/2)
- To do technical possibility analysis
- ???????
- RD ?
- Where is the technical bottleneck ?
- Pre-processor for
- Technical design document
- Project management
- Bridge from game design to programming
40Something about System Analysis
- No standard procedures or approaches
- Its not a theory.
- Experience
- You can use your own method/tool
- UML
- Mind mapping
- ????
- This is the one we will use in this course
41Wangs System Analysis Steps
- Brainstorming
- Integration
- Dependency analysis
- Create the project
- Technical design document (TDD) writing
42Brainstorming
- Based on the game design to put everything as
many as you could - Use mind mapping
- Including
- Game system
- Combat / Village / Puzzle /
- Program modulus
- Camera / PC control / NPC AI / UI / FX /
- Tools
- Level editor / Scene editor /
- Entities in games
- Characters / vehicle / terrain / audio /
43Integration
- Confirm the resource limitation
- Technical implement possibility
- Put all related items together
- Man month analysis
- How many ?
- Who ?
- Jobs / System identification
44Dependency Analysis
- Sort the Jobs
- By job dependency
- By programmers schedule
- Prototype for scheduling
45System Analysis Create the Project
- Scheduling
- Job assignment
- Resource allocation
- Check points
- Milestones
- Major check points
- Output
- Risk management
- Alternatives
- Risk management policy
46Technical Design Document
- Specification
- Resources
- Design in details
- Implement methods (??)
- Algorithms
- Project
- Output in each milestone
- SOP (optional)
- TDD Template
47Mind Map
- ????
- A radiant thinking tool
- Applications
- ????
- Proposal
- ????
- ??
- System Analysis
-
- Reference
- Programs
- Visio
- MindManager
- Books
- Tony Buzan, Barry Buzan
- The Mind Map Book How to Use Radiant Thinking
to Maximize Your Brain's Untapped Potential
48(No Transcript)
49(No Transcript)
50Mind Map Demo Using MindManager
- Use MindManager X5 pro
- Developed by MindJet
513rd The Game Main Loop
52Win32 Application (1/3)
int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE
hPrevInst, LPSTR lpCmdLine, int nCmdShow)
WNDCLASSEX wc ... // register window
class ZeroMemory(wc, sizeof(WNDCLASSEX))
wc.style CS_OWNDC CS_HREDRAW
CS_VREDRAW CS_DBLCLKS wc.lpfnWndProc
KKMainProc ... RegisterClassEx(wc)
... // the main loop KKMainLoop() //
unregister the window class UnregisterClass(wc.
lpszClassName, hInst) return id
53Win32 Application (2/3)
LRESULT CALLBACK KKMainProc(HWND hWnd, UINT uMsg,
WPARAM wParam, LPARAM lParam) LRESULT l
FALSE ... // switch for all incoming
messages from WindowsXX switch (uMsg)
case WM_KEYDOWN ... l TRUE
break ... // echo the result if
(l) return l else
return DefWindowProc(hWnd, uMsg, wParam,
lParam)
54Win32 Application (3/3)
void KKMainLoop() MSG msg BOOL kkBeQuit
FALSE // the main loop while
(!kkBeQuit) // check window's messages
while (PeekMessage(msg, NULL, 0, 0,
PM_REMOVE)) if (msg.message
WM_QUIT) kkBeQuit TRUE
// invoke the WindowsX to handle the
incoming messages TranslateMessage(msg)
DispatchMessage(msg)
// do your jobs here, for example, check the
timing and do something in regular ...
55Event-driven Programming
- Win32 programs are event-driven
- Messages events
- So as all windows system (for example X window)
- We need an infinitive loop to check all incoming
events. - In the loop
- Check if there are incoming events (messages)
- Handle the events
- Check the time and do something in regular
- Incoming events
- Interrupts
- System requests
56Timers Events (1/2)
- Timers (do something in regular timing)
- The sub-system to handle timing
- Must be precise to at least 1 ms or less
- 30fps 1/30 second 33.333 ms
- On win32 platform, you can use performance
counter instead of the win32s WM_TIMER
message - For windows9x, WM_TIMER 18.2 fps (maximum)
- Events
- Input devices
- Mouse
- Keyboard
- Something coming from network
- System requests
- Re-draw
- Losing/getting the input focus
57Timers Events (2/2)
- Two types of jobs (Callbacks) to do (Call)
- In regular
- Timers callbacks
- By requests
- Input device callbacks
- So as the game main program
- A game is an interactive application.
- Mouse
- Hotkeys
- Gamepads
- A game is time-bound.
- Rendering locked in 30fps or 60fps
- Motion data produced in 30fps
- Game running in 30fps
58Implement the Timer (1/6)
- On PC platform use Performance Counter
- QueryPerformanceFrequency()
- QueryPerformanceCounter()
// timers data structure typedef struct BOOL
beAble // is the timer is
enabled/disabled ? BOOL be1st // is
this the 1st time for the timer to be checked
// after last
initialization ? BOOL beLockFps // is
locked on FPS ? double initTime // initial
time double timeInv // system ticks for
one frame double nxtTime // next checking
time void (timer)(int) // timer's callback
double resetTime // reset time TIMERs,
TIMERptr
59Implement the Timer (2/6)
/------------------------------------------------
---------------------- initialize a timer and
bind a user-defined timer callback
--------------------------------------------------
----------------------/ void FyBindTimer(DWORD
id, float fps, void (fun)(int), BOOL beLock)
if (id lt 0 id gt MAXTIMERS) return /
assign the timer's callback /
fyTimerid.timer fun / set lock-to-fps
flag / fyTimerid.beLockFps beLock /
calculate the ticks for one frame /
fyTimerid.timeInv (double) (fyFreq) /
(double) fps fyTimerid.be1st TRUE
fyTimerid.beAble TRUE
60Implement the Timer (3/6)
/------------------------------------ get
current system clock tick -----------------------
---------------/ double FYGetCurrentSystemTick()
LARGE_INTEGER timeCount / get current
tick / QueryPerformanceCounter(timeCount)
return (double) timeCount.QuadPart / //
get the system ticks for one second
QueryPerformanceFrequency(timeFreq) fyFreq
timeFreq.LowPart /
61Implement the Timer (4/6)
/------------------------------------------------
------------ invoke the TheFly3D system to
handle the timers -------------------------------
-------------------------------/ void
FyInvokeTheFly(BOOL beTimer) MSG msg if
(fyBeQuit) return while (!fyBeQuit)
// check window's messages while
(PeekMessage(msg, NULL, 0, 0, PM_REMOVE))
if (msg.message WM_QUIT) fyBeQuit TRUE
TranslateMessage(msg)
DispatchMessage(msg) // check
the timer if (beTimer fyBeTimer)
FYInvokeTimer()
62Implement the Timer (5/6)
/--------------------- check all timers
----------------------/ void FYInvokeTimer()
int i, skipS double dTime // get
current time dTime FYGetCurrentSystemTick()
for (i 0 i lt MAXTIMERS i) if
(fyTimeri.beAble fyTimeri.timer ! NULL)
// for the first time .....
if (fyTimeri.be1st) // initialize
the timer fyTimeri.be1st FALSE
fyTimeri.initTime dTime
fyTimeri.nxtTime dTime fyTimeri.timeInv
((fyTimeri.timer))(1)
63Implement the Timer (6/6)
else if
(fyTimeri.beLockFps) if (dTime
gt fyTimeri.nxtTime) //
calculate skip frames skipS
(int)((dTime - fyTimeri.nxtTime) /
(double)fyTimeri.timeInv) 1
// get next checking time
fyTimeri.nxtTime (double) (skipS
fyTimeri.timeInv) // check
some abnormal conditions ...
// invoke the timer callback
((fyTimeri.timer))(skipS)
else
((fyTimeri.timer))(1)
64Game Loop (1/2)
65Game Loop (2/2)
Loop
y
Check game over
Exit
n
Peek user input
Receive messages
From network
Timer callbacks
To network
Send messages
Rendering
66Jobs in Regular (In Timers)
- Check Win/Lose
- Check Quit
- Make objects moving
- Play characters motion to the next frame
- Play animation to the next frame
- Models
- Textures
-
- Perform game logic calculation
- Perform geometry associated calculation
- i.e. LOD
- Perform AI Thinking
- Perform collision detection
- Perform the 3D rendering
- Play FX
67Jobs By Request
- Mouse Input
- Press/release the mouse button
- Drag
- Double-click
- Move
- Keyboard Input
- Hotkey
- Typing
- Gamepad
- Same behavior as the hotkey
- Except looking not like the keyboard
- Network
- System
684th TheFly3D Game Engine
69The Main Program
void main(int argc, char argv) // create
the game world 3D scene ... // set
Hotkeys FyDefineHotKey(FY_ESCAPE, QuitGame,
FALSE) ... // define some mouse
functions FyBindMouseFunction(LEFT_MOUSE,
InitPivot, PivotCam, EndPivot, NULL) ...
// bind a timer for rendering, frame rate 60
fps FyBindTimer(0, 60.0f, RenderIt, TRUE)
// bind a timer for game AI, frame rate 30 fps
FyBindTimer(1, 30.0f, GameAI, TRUE) //
invoke the system FyInvokeTheFly(TRUE)
70Hotkey Callback
//------------------- // quit the
game //------------------- void QuitGame(WORLDid
gID, BYTE code, BOOL value) if (code
FY_ESCAPE) if (value)
FyWin32EndWorld(gID)
71Mouse Callback
/-----------------------------------------
initialize the pivot of the camera
------------------------------------------/ void
InitPivot(WORLDid g, int x, int y) oldX
x oldY y
/------------------ pivot the camera
-------------------/ void PivotCam(WORLDid g,
int x, int y) FnModel model if (x !
oldX) model.Object(cID)
model.Rotate(Z_AXIS, (float) (x - oldX),
GLOBAL) oldX x if (y ! oldY)
model.Object(cID)
model.Rotate(X_AXIS, (float) (y - oldY),
GLOBAL) oldY y
72The Timer Callback
//------------------------------------------------
---------------------------------------- //
Render callback which will be invoked by TheFly3D
every 1/60 second //------------------------------
--------------------------------------------------
-------- void RenderIt(int skip) FnViewport
vp FnWorld gw // render the scene
vp.Object(vID) vp.Render(cID, TRUE, TRUE)
// perform double-buffering gw.Object(gID)
gw.SwapBuffers()
73Introduction to TheFly3D
- A real-time 3D graphics programming library
- Using C
- Cross-platform
- DirectX9.0c
- OpenGL 1.5
- An API for 3D graphics developers
- Provide a fundamental scene management system
- Scene tree
- Built-in visibility culling
- According to the experiences from the author,
some game development features are added. - Characters
- Terrain system
-
- Current version 0.8a1 (0920, 2005)
- Shader has been added in D3D version
74TheFly3D in A Chart
Game
Game Play Layer
Terrain
Collision
Character
UI
Dynamics
Sound FX
Engine Layer
3D Scene Mngmt
2D Sprite
Gamepad
Network
Audio
3D Graphics API
2D API
Input Device
OS API
System Layer
Hardware
Developing
Developed
75Development Environment
- .NET2003 Visual C 7.1
- DirectX9.0c SDK (Dec, 2004)
- Two include files (must!)
- TheFly.h
- TheFlyWin32.h (Win32 version D3D)
- Linked libraries (API version)
- TheFlyLibD_08_01.lib
- d3d9.lib d3dx9.lib dsound.lib dxguid.lib
winmm.lib
76Create the Visual C Project for TheFly3D
- Create folders for TheFly3D API
- \include
- \lib
- New a Win32 application project
- Set the additional include/library directories to
TheFly3D API - Add DirectX 9.0 SDKs include/lib to additional
search directories - Add d3d9.lib d3dx9.lib dsound.lib dxguid.lib
winmm.lib to additional dependencies - Add TheFly.h, TheFlyWin32.h, TheFly3DLibD_xxxx.lib
into the project
77The 1st TheFly3D Program hello.cpp
- Create a 3D world
- Create a viewport
- Create a scene
- Create 3D entities
- A camera
- A teapot model
- A light source
- Translate the camera to show the model
- Bind callbacks to make the program interactive
78Demo - Hello
Do it!
79The Basics to Write TheFly3D Program
- All Win32 code is transparent.
- void main(int argc, char argv)
- All Win32 DirectX code are hidden within the
engine. - ID Function class
- TheFly3D creates the objects for you.
- Return the object ID
- TheFly3D owns the objects.
- You have the right to handle the objects.
- Use function classes
- No internal data structure functions revealed
// create a viewport vID
gw.CreateViewport(ox, oy, ww, hh) FnViewport
vp vp.Object(vID) vp.SetBackgroundColor(0.
3f, 0.3f, 0.0f)
80Initialize TheFly3D
- In general the 1st function to use TheFly3D is
- FyWin32CreateWorld()
- After the calling successfully, you can get the
non-zero ID (WORLDid) of a world object. - Assign the ID to a world function class object
for manipulating the world.
// create a new world WORLDid gID
FyWin32CreateWorld(Tester", 0, 0, 800, 600, 16,
FALSE) FnWorld gw gw.Object(gID)
gw.SetTexturePath("Data\\textures")
81The World in TheFly3D
- A world is a set of graphics layers where the 3D
objects acts on.
82The 3D Graphics Layer
- A 3D graphics layer is a projection of the
rendering of a 3D view. - The set of the 3D objects in the view
- We call it the Scene.
- The projection we call the Viewport
83The Viewports Scenes
- TheFly3D supports the multiple viewports.
- A scene can be rendered on different viewports.
- Viewports scenes are created by the world
object.
// create a new world WORLDid gID gID
FyWin32CreateWorld(Tester", 0, 0, 800, 600, 16,
FALSE) FnWorld world world.Object(gID)
SCENEid sID world.CreateScene()
VIEWPORTid vID world.CreateViewport(0, 0, 400,
300) world.DeleteScene(sID)
world.DeleteViewport(vID)
84The Scene
- A scene is not a real 3D object, just a set
of 3D objects. - A scene provides multiple rendering groups to
handle the priority sorting for the rendering of
3D objects. - There are three object lists within a rendering
group. - Invisible list
- Opacity list
- Semi-transparent list
- The objects are rendered by the order of
rendering groups. - In the same rendering group, the opaque objects
are rendered first. - And the semi-transparent objects are sorted and
rendered from far to near
85The 3D Entities Objects
- A 3D scene is constructed by a set of objects
which are the basic entities in a scene. - An object is a carrier to carry real 3D data
including - Model geometry
- Camera data
- Lighting data
- Terrain data
- Particle emitters
- Forces
- Audio
- Objects are created/deleted by its host scene.
- Objects can be switched between scenes.
- Objects should be assigned to a rendering group.
86The Objects Can
- Can have shapes (geometric data)
- Can be grouped (hierarchy)
- Can move (transformation)
- Can look alike (clone or data sharing)
- Can perform (animation or deformation)
- Can be affected (lighted, listened)
- Can be changed dynamically (modification)
87A Scene Object
88A Model Object
- An object to carry a set of geometry data is a
model object - You can load the model data from files.
- TheFly3D loads .cw3 model files in default.
// create 3D entities nID
scene.CreateObject(ROOT) FnObject model
model.Object(nID) // load a teapot
model.Load("Teapot.cw3")
89TheFly3D Scene Tree
- A tree-based representation
- Simplified scene graph
Root
90TheFly3D Scene Tree Is Simplified Scene Graph
- A tree-based representation
- Simplified scene graph
Root
91Object Hierarchy
nID scene.CreateObject(ROOT) cID
scene.CreateCamera(ROOT) FnObject
model model.Object(nID) model.SetParent(cID)
cID
nID
92Clone a Model Object
OBJECTid nID scene.CreateObject(ROOT) FnObject
model model.Object(nID) OBJECTid nID1
model.Instance()
nID
nID1
Data
instance
93TheFly3D Major Model Object Functions (1/2)
- void model.SetParent(parent_object_ID)
- void model.Show(be_show)
- void model.SetOpacity(opacity)
- opacity 0.0 1.0
- void model.SetRenderMode(mode)
- mode WIREFRAME or TEXTURE
- OBJECTid clonedID model.Instance()
- void model.ChangeScene(sID)
- BOOL model.Load(char file)
- Load a .cw3 model file to a model object
- void model.ShowBoundingBox(beShow)
- void model.Translate(x, y, z, op)
94TheFly3D Major Model Object Functions (2/2)
- model.SetRenderOption(item, value)
- (item, value)
- (Z_BUFFER, TRUE/FALSE)
- (Z_BUFFER_WRITE, TRUE/FALSE)
- (ALPHA, TRUE/FALSE)
- Add/remove the model to/from alpha sorting list
- (FOG, TRUE/FALSE)
- (SPECULAR, TRUE/FALSE)
- (LIGHTING, TRUE/FALSE)
- (ANTIALIASING, TRUE, FALSE)
- (SOURCE_BLEND_MODE BLEND_ZERO / BLEND_ONE /
BLEND_SRC_COLOR / BLEND_INV_SRC_COLOR /
BLEND_SRC_ALPHA /
BLEND_INV_SRC_ALPHA / BLEND_DEST_ALPHA /
BLEND_INV_DEST_ALPHA /
BLEND_DEST_COLOR / BLEND_INV_DEST_COLOR /
BLEND_SRC_ALPHA_SAT / BLEND_BOTH_SRC_ALPHA /
BLEND_BOTH_INV_SRC_ALPHA) - (DESTINATION_BLEND_MODE values are same as the
SOURCE_BLEND_MODE)
95Coordinate System
- Every model should have its own local coordinate
system. - Local space
- Model space
- The space when its modeled
- To its parent model, it is in the global space.
- Global space
- The space for reference
- World space
- The global space of the Root
96Transformation
- Three basic linear transformations used in
TheFly3D. - Translate
- Rotate
- Scale
- Principles
- Right-handed rule
- v v M0 M1
- Matrix in 12-element (I call the M12)
97Translation
model.Translate(dx, dy, dz, op)
x x dx y y dy z z dz
(dx dy dz)
1 0 0 0 1 0 0 0 1 dx dy dz
T
98Rotation
i.e. rotate with z axis
model.Rotate(Z_AXIS, 30.0f, op)
z
x x cosq y sinq y x sinq y cosq z z
y
cosq sinq 0 -sinq cosq 0 0 0 1 0
0 0
x
Rz
99Scaling
model.Scale(sx, sy, sz, op)
x x sx y y sy z z sz
sx 0 0 0 sy 0 0 0 sz 0 0 0
T
100Matrix Operations
- Matrix operation
- REPLACE, LOCAL, GLOBAL
Z
Y
X
op LOCAL
z
op REPLACE
x
y
M
ML
MG
op GLOBAL
Object Transformation Matrix
101TheFly3D Model Transformation Functions
- model.Translate(x, y, z, op)
- op LOCAL, GLOBAL, or REPLACE
- model.Rotate(axis, angle, op)
- op LOCAL, GLOBAL, or REPLACE
- axis X_AXIS, Y_AXIS, Z_AXIS
- model.Scale(sx, sy, sz, op)
- op LOCAL, GLOBAL, or REPLACE
- model.Quaternion(w, x, y, z, op)
- w the scalar part of the quaternion
- x, y, z the vector part of the quaternion
- The quaternion should be a unit quaternion
- op LOCAL, GLOBAL, or REPLACE
- model.SetMatrix(M12, op)
- M12 an M12 matrix
- op LOCAL, GLOBAL, or REPLACE
- float model.GetMatrix()
- Get the pointer of the model objects matrix
102Transformations vs Movements
- Transformation is the term used in computer
graphics but not friendly for games. - We use movements to control the 3D objects moving
around in the scene. - Move forward
- Move right
- Move up
- Turn right
- Turn left
103Facing Direction and Up Direction
- Each object is modeled with a facing direction
and up direction visually - In TheFly3D, we use y axis as the default facing
direction for a model, z axis as the default up
direction - But for a camera
- -z axis is the facing direction
- y axis is the up direction
104Move Forward (1/2)
new position old position
distance (facing direction in unit)
105Move Forward (2/2)
- The object has a local coordinate system.
- Align a local axis of the object with the facing
direction - Make a translation to move the object align the
local axis - Apply the matrix first before to apply the
existing transformations (op LOCAL) - Then the object is moving forward!
-
FnObject model model.Object(nID) model.Translat
e(0.0f, -dist, 0.0f, LOCAL) // facing to -y
106Turn Right/Left (1/2)
- An example (rotate with an arbitrary axis)
-1 -1
-1 M T1 R1 R2 Rx(angle) R2 R1
T1 T1 / 1 0 0 0 R1 / cs2 -sn2 0
0 0 1 0 0 -z sn2 cs2 0 0
0 0 1 0 0 0 1 0
-x -y -z 1 / 0 0 0 1 / R2
/ cs1 0 -sn1 0 Rx / 1 0 0 0 y
0 1 0 0 0 cs sn 0 sn1
0 cs1 0 0 -sn cs 0 0 0
0 1 / 0 0 0 1 /
107Turn Right/Left (2/2)
- The object has a local coordinate system
- Align a local axis of the object with the up
direction - Make a rotation matrix to turn the object along
the local axis - Apply the matrix first before to apply the
existing transformations - Then the object is turning !
-
FnObject model model.Object(nID) model.Rotate(Z
_AXIS, -angle, LOCAL) // turn right
108Terrain
- A terrain is a place for 3D objects to walk on
- A terrain can be generated from a model file
- Neighboring triangles are the next searching
target for current triangle for terrain following - A terrain for terrain following is not the same
as the terrain in visual
109Generate Terrain in TheFly3D
// create a terrain object tID
scene.CreateTerrain() FnTerrain
t t.Object(tID) // load a terrain model (just
like a regular object) // but a terrain is
invisible in default t.Load("test_terrain_followin
g.cw3") // generate the neighboring data for
terrain following // otherwise, the terrain model
is for visual only t.GenerateTerrainData()
110Put a Model on Terrain Using TheFly3D
// if tID is the terrain object (OBJECTid) //
load a model OBJECTid nID scene.CreateObject(RO
OT) FnObject obj obj.Object(nID) // put the
model on terrain float pos3 pos0 x pos1
y pos2 10000.0f // should be higher
than the terrain! obj.SetPosition(pos) obj.PutOnT
errain(tID, be3D, offset, rangeF, rangeB, angle)
111Terrain Following
Terrain Following (3D)
offset
Terrain Following (2D)
offset
112Probe for a Model on Terrain
probeBack
probeFront
terrain following check point
113TheFly3D Model Movement Functions (1/3)
- void model.GetPosition(pos)
- pos is a 3D vector to get the position of the
model object - void model.GetDirection(faceDir, upDir)
- If you just want to get one of the directions,
just send NULL pointer to the one that you do not
want to query - void model.SetPosition(pos)
- The position is related to its parent object
- void model.SetDirection(faceDIr, upDir)
- If you just want to set one of the directions,
just send NULL pointer to the one that you do not
want to set - Void model.SetDirectionAlignment(fDAxis, uDAxis)
- You can change the local axes for facing and up
directions
114TheFly3D Model Movement Functions (2/3)
- BOOL model.PutOnTerrain(tID, be3D, offset,
probeFront, probeBack, probeAngle, hightLimit) - tID is a terrain object
- be3D TRUE for 3D terrain following
- Offset is the height above the terrain
- hightLimit is the terrain following height limit
- Return TURE if you successfully put the model on
a terrain - int model.MoveForward(dist, beTerrainFollow,
be3D, offset) - If you just want to move the model forward but
not on terrain, set beTerrainFollow to FALSE - You should put a model on terrain first. Then you
can move it forward on terrain. - Return value
- WALK (ok for moving)
- BOUNDARY (hit the terrain boundary)
- BLOCK (not implemented yet, for collision)
- DO_NOTHING (something wrong )
115TheFly3D Model Movement Functions (3/3)
- int MoveRight(dist, beTerrainFollow, be3D,
offset) - Same as the MoveForward except moving in
right-hand direction - void model.TurnRight(float angle)
- Angle is in degree format
- int model.GetCurrentTerrainTriangle()
- Get current triangle ID in terrain, where the
model is standing
116A Character
117Character with motion
118Introduction to Characters
- The characters are the actors of the games.
- Three types of characters implemented in games
- Segmented
- Mesh
- Bone-skin
- Root-base concept
- Production
- 3D animation tools
- Motion capture (MoCap)
- For motion data
119A Segmented Character
- A character is composed by a set of models with
motion data to simulate a live creature in real
world
- Benefits
- Hierarchical structure
- Easy to implement in a scene tree
- Drawbacks
- Segment-like
120- The scene tree of a segmented character
head
up arm
hand
body
groin
fore arm
Base
thigh
groin
foot
shin
body
thigh_r
thigh_l
head
up_arm_r
shin_r
up_arm_l
shin_l
fore_arm_l
fore_arm_r
foot_r
hand_l
foot_l
hand_r
121A Mesh Character
- Vertex animation on skins
- Animated positional data on skins
- 3D warping
- Benefits
- Easy to implement
- Flexible mesh in animation
- Drawbacks
- No hierarchy
- Each frame is independent.
- Massive dataset
122A Bone-skin Character
- Bone-skin skeleton
- Hierarchical bones
- Skin deformation run-timely
- Benefits
- Hierarchical structure
- Not segmented look
- Drawbacks
- More complicated than the other solutions
- Skin deformation need more CPU cost than
transformation only
123Motion Production by 3D Animation Tools (1/2)
- Keyframe system
- 3ds MAX
- Softimage
- Maya
124Motion Production by 3D Animation Tools (2/2)
- Low cost (relatively)
- Easy to combine animations
- Hand-made animations
- Hard to make good motions
- Long production time
125Motion Production by Motion Capture (1/2)
- Motion Capture
- MoCap in short
- Types
- Optical
- Magnetic
- ...
126Motion Production by Motion Capture (2/2)
- Expensive solution
- Hardware and software
- Every frame is a keyframe
- Very live motion
- Noise !
- Need post-processing for games
- Patching the data
- Re-keyframing
- Cleaning noise
- Re-targeting
- Hard to combine motions
127The Root-Base Concept (1/2)
- Use root-base structure to construct the character
Base
Root (groin)
Base
128The Root-Base Concept (2/2)
- A character has some models to be the geometry
roots of the character system. - The root plays as the gravity center of the
character. - The root can be translated and rotated.
- The others are joints.
- The joints can rotate only.
- A ghost object is added to be the parent of the
root, which is the base of the character. - The base is the center for the characters
movement. - We move the base to perform characters moves.
129Motion Data - Pose
- A set of frames to describe a characters motion
- For examples Walk, run, attack,
- Keyframed or non-keyframed
- Motion data in
- Position (pivot) quaternion
- Position (pivot) Euler angles
- Position (pivot) (q, n)
- Matrix
130Load a Character
FnScene scene CHARACTERid actorID scene.Object(
sceneID) actorID scene.LoadCharacter(fm.cwc
")
.cwc is a character description file.
131Play a Pose
BLENDTREEid btID actor.GetBlendTree()
FnBlendTree bt bt.Object(btID) // the
2nd motion definition BLENDNODEid aaaID
aaaID bt.CreateAnimationNode(2) // start
to play a pose (1st time) actor.PlayBlendNode(a
aaID, (float) 0, START, TRUE) // continue to
play a pose actor.PlayBlendNode(CURRENT,
(float) skip, LOOP, TRUE)
132Make a Character to Move Forward
FnCharacter actor // play walking
pose actor.Object(actorID) actor.PlayBlendNode(CU
RRENT, (float) skip, LOOP, TRUE) // move it
forward actor.MoveForward(dist, beTF, be3D,
offset)
133TheFly3D Character Movement Functions (1/2)
- void actor.GetPosition(pos)
- pos is a 3D vector to get the position of the
character - void actor.GetDirection(faceDir, upDir)
- If you just want to get one of the directions,
just send NULL pointer to the one that you do not
want to query - void actor.SetPosition(pos)
- The position is related to its parent object
- void actor.SetDirection(faceDIr, upDir)
- If you just want to set one of the directions,
just send NULL pointer to the one that you do not
want to set - BOOL actor.PutOnTerrain(tID, be3D, offset,
probeFront, probeBack, probeAngle, hightLimit) - tID is a terrain object
- be3D TRUE for 3D terrain following
- Offset is the height above the terrain
- hightLimit is the terrain following height limit
- Return TURE if you successfully put the character
on a terrain
134TheFly3D Character Movement Functions (2/2)
- void actor.MoveForward(dist, beTF, be3D, offset)
- If you just want to move the character forward
but not on terrain, set beTF to FALSE - You should put a character on terrain first. Then
you can move it forward on terrain. - A character is always using his local -y-axis as
facing direction - void actor.TurnRight(float angle)
- Angle is in degree
- A character is always using his local z-axis as
up direction
135TheFly3D Character Functions
- OBJECTid actor.GetBaseObject()
- You can get the base object of the character
- OBJECTid actor.GetObjectByName(name)
- You can get the each model part of the character
by its name in character file - For a bone-skin character, this function can get
the bones in the skeleton - BOOL actor.PlayFrame(frame, beIncludeBase)
- The function is to play the motion for a specific
frame - Set beIncludeBase TRUE to play the base
objects motion - BLENDTREEid actor.GetBlendTree()
- Get the characters blend tree data
- You can define all poses in a blend tree (blend
nodes) - float actor.PlayBlendNode(btNodeID, skipFrame,
mode, beIncludeBase) - All poses for a character is defined as a blend
node - Skip frames can be floating-point
- Mode can be ONCE or LOOP!
1365th Game Mathematics
137Essential Mathematics for Game Development
- Matrices
- Vectors
- Fixed-point Real Numbers
- Triangle Mathematics
- Intersection Issues
- Euler Angles
- Angular Displacement
- Quaternion
- Differential Equation Basics
138Matrices (1/7)
- Matrix basics
- Definition
- Transpose
- Addition
a11 .. a1n . . . . am1 .. amn
A (aij)
C A T cij aji
C A B cij aij bij
139Matrices (2/7)
- Scalar-matrix multiplication
- Matrix-matrix multiplication
C aA cij aaij
r
C A B cij Saikbkj
k 1
140Matrices (3/7)
- Transformations in Matrix form
- A point or a vector is a row matrix (de facto
convention)
V x y z
- Using matrix notation, a point V is transformed
under translation, scaling and rotation as
V V D V VS V VR
where D is a translation vector and S and R are
scaling and rotation matrices
141Matrices (4/7)
- To make translation be a linear transformation,
we introduce the homogeneous coordinate system
V (x, y, z, w)
, where w is always 1
- Translation Transformation
x x Tx y y Ty z z Tz
V VT
1 0 0 0 0 1 0 0 0 0 1 0 Tx Ty
Tz 1
x y z 1 x y z 1 x y z
1 T
142Matrices (5/7)
x xSx y ySy z zSz
V VS
Sx 0 0 0 0 Sy 0 0 0 0 Sz 0 0
0 0 1
x y z 1 x y z 1 x y z
1 S
Here Sx, Sy and Sz are scaling factors.
143Matrices (6/7)
1 0 0 0 0 cosq sinq 0 0 -sinq cosq 0 0
0 0 1
Rx Ry Rz
cosq 0 -sinq 0 0 1 0 0 sinq 0 cosq 0 0
0 0 1
cosq sinq 0 0 -sinq cosq 0 0 0 0 1 0
0 0 0 1
144Matrices (7/7)
- Net Transformation matrix
x y z 1 x y z 1 M1 and x y z
1 x y z 1 M2 then the transformation
matrices can be concatenated M3 M1
M2 and x y z 1 x y z 1 M3
- Matrix multiplication are not commutative
M1 M2 M2 M1
145Vectors (1/5)
- A vector is an entity that possesses magnitude
and direction. - A 3D vector is a triple
- V (v1, v2, v3), where each component vi is a
scalar. - A ray (directed line segment), that possesses
position, magnitude and direction.
V (x2-x1, y2-y1, z2-z1)
(x1,y1,z1)
(x2,y2,z2)
146Vectors (2/5)
X V W (x1, y1, z1) (v1 w1, v2 w2,
v3 w3)
W
V W
W
V W
V
V
V (v12 v22 v32)1/2 U V / V
147Vectors (3/5)
- Cross product of vectors
- Definition
X V X W (v2w3-v3w2)i (v3w1-v1w3)j
(v1w2-v2w1)k where i, j and k are standard
unit vectors i (1, 0, 0), j (0, 1, 0), k
(0, 0, 1)
- Application
- A normal vector to a polygon is calculated from 3
(non-collinear) vertices of the polygon.
Np
polygon defined by 4 points
V2
Np V1 X V2
V1
148Vectors (4/5)
- Transformation of the normal vectors
N(X) detJ J-1T N(x) where X F(x) J the
Jacobian matrix, Ji(x)
dF(x)
dxi
"Global and Local Deformations of Solid
Primitives" Alan H.
Barr Computer Graphics Volume 18, Number 3 July
1984
149Vectors (5/5)
- Dot product of vectors
- Definition
X V . W v1w1 v2w2 v3w3
V
q
W
V . W
cosq
VW
150Fixed Point Arithmetics (1/2)
- Fixed point arithmetics n bits (signed) integer
- Example N 16 gives range 32768 ? a ? 32767
- We can use fixed scale to get the decimals.
a a / 28
8 integer bits
1
1
1
8 fractional bits
a 315, a 1.2305
151Fixed Point Arithmetics (2/2)
- Multiplication then requires rescaling
e a.c a / 28 . c / 28 ? e (a . c) / 28
- Addition just like normal
e ac a / 28 c / 28 ? e a c
152Fixed Point Arithmetics - Application
- Compression for floating-point real numbers
- 4 bytes reduced to 2 bytes
- Lost some accuracy but affordable
- Network data transfer
- Software 3D rendering (without hardware-assistant)
153Triangular Coordinate System
ha
(xa,ya,za)
Ac
p
Ab
hb
h
(xb,yb,zb)
Aa
hc
(xc,yc,zc)
Aa
Ab
Ac
h ha hb hc
where A Aa Ab Ac If (Aa lt 0 Ab lt 0
Ac lt 0) than the point is outside the
triangle Triangular Coordinate System
A
A
A
154Triangle Area 2D
Area of a triangle in 2D xa ya
A ½ xb yb xc yc
xa ya ½ (xayb xbyc xcya
xbya xcyb xayc)
(xa,ya,za)
(xb,yb,zb)
(xc,yc,zc)
155Triangle Area 3D
Area of a triangle in 3D A ½ (N.
Sum(Pi1 cross Pi2))
where (i1, i2) (a,b), (b,c),
(c,a)
float GmArea3(float x0, float x1, float x2,
float n) float area, len, sum1, sum2,
sum0 len (float) sqrt(n0 n0 n1
n1 n2 n2) 2.0f / find sum of
cross products / sum0 x11 (-x02
x22) x21 (-x12 x02)
x01 (-x22 x12) sum1 x12
(-x00 x20) x22 (-x10 x00)
x02 (-x20 x10) sum2
x10 (-x01 x21) x20 (-x11
x01) x00 (-x21
x11) / find the area / return (sum0
n0 sum1 n1 sum2 n2) / len
156Triangular Coordinate System - Application
- Terrain following
- Interpolating the height of arbitrary point
within the triangle - Hit test