Transcript Document

3D Game Programming

王銓彰 [email protected]

2005 1

課程大綱

               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  Wang’s Method for Real-time 3D Game Development 2

課程要求

   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.

3

王銓彰

(1/3)

  

目前

 數位內容學院 專任講師 / 顧問   宇峻奧汀 顧問 鈊象電子 3D 技術顧問

學歷

 台灣大學土木工程學系畢業

資歷

      04-04 資策會網路多媒體研究所 專案顧問 97-04 昱泉國際股份有限公司 技術長 96-96 虛擬實境電腦動畫股份有限公司 研發經理 93-96 西基電腦動畫股份有限公司 研發經理 90-93 國家高速電腦中心 助理研究員 89-90 台灣大學土木工程學系 CAE Lab 研究助理 4

王銓彰

(2/3)

 Game

作品

 Current  TheFly3D 3D Engine   昱泉國際  DragonFly 3D Game Engine  M2 神甲奇兵 , VRLobby, 天劍記  Lizard 3D Game Engine  幻影特攻、笑傲江湖 I & II 、神鵰俠侶 I & II 、風雲、小李飛刀、 笑傲江湖網路版、怪獸總動員、聖劍大陸、笑傲外傳 西基電腦動畫  Ultimate Fighter  1 st real-time 3D fighting game in Taiwan 5

王銓彰

(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) 6

1st Introduction to Game Development

7

Introduction to Game Development

      Game platform Game types Game team Game development pipeline Game software system Tools 8

Game 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 9

Game 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 10

Game 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 11

Game 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 12

Game Team Members

    

開發團隊

   製作人 執行製作人 企劃團隊   程式團隊 美術團隊

行銷業務團隊

 產品經理 (PM)

測試團隊 遊戲審議委員會

 Game project approval

遊戲經營團隊

   線上遊戲 game master (GM) Customer services MIS 13

Game Producer

遊戲製作人

       Team leader (usually)

資源管理 行政管理

(Resource management) (Administration)

專案管理

(Project management)

向上負責

(Upward management)

團隊的決策 風險管理

14

遊戲執行製作人

  

專案管理執行

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 15

遊戲企劃

(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) 16

遊戲企劃

(2/2)

  

場景設定

(Scene setup) Game document writing Game quality checking 17

遊戲美術

  Visual setup for game design   2D setup 3D setup Graphics design and production  場景 (Terrain)        人物 (Character) 建模 (Models) 材質 (Textures) 動作 (Motion / Animation) 特效 (FX) User Interface 行銷支援 ( 封面 .

海報 ..

等 ) 18

遊戲程式

     

遊戲程式

(Game Program)

撰寫 遊戲開發工具

  Level editor Scene editor    FX editor Script editor Game editor (Game Tools)

開發 遊戲

Data exporters from 3D animation Software  3dsMax / Maya / Softimage Game engine development Game technique research Online game server development 19

遊戲開發流程

Basic Procedures for Game Development Idea Proposal Production      Concept Approval Prototype

發想 提案

(Idea) (Proposal)

製作

(Production)

整合

(Integration)

測試

(Testing)  

除錯

(Debug)

調適

(Tuning) Integration Debug Testing Tuning Pre-alpha Alpha Beta Final > Concept approval >

雛形

(prototype) > Pre-alpha > Alpha > Beta 20

遊戲發想

(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) 21

遊戲提案

(Proposal)

       

系統分析

(System analysis) GDD

撰寫

MDD

撰寫

(Game design document) (Media design document) TDD

撰寫

(Technical design document)

遊戲專案建立

(Game project)    Schedule Milestones / Check points Risk management

測試計畫書 團隊建立

(Team building)

產出物

    GDD MDD TDD The Team 22

遊戲開發

(Production)

   

美術量產製作

  Modeling Textures    Animation Motion FX

程式開發

(Coding)

企劃數值設定

… 量產

!

23

遊戲整合

(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 24

遊戲測試

(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 25

Bugs

  Bug

分級

    A Bug B Bug C Bug S Bug (Bug Classification) Principles  Bug 分級從嚴  Tester vs Debugger Bug N Bug Classification Bug Dispatch Debug Verify Y FAQ ?

26

Game Software System

NPC System Fighting System Virtual Agent FX System Terrain Game Collision Character Trading System Game AI Dynamics Script System Sound FX Story UI 3D Scene Mngmt 3D Graphics API 2D Sprite Gamepad 2D API Hardware Audio Input Device Network OS API Game Play Layer Engine Layer System Layer 27

System 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 28

System Layer – APIs (2/2)

  OS API   Win32 SDK MFC Network   DirectX 9.0 SDK – DirectPlay Socket library 29

Engine Layer (1/2)

          3D scene management system  Scene graph Shaders 2D sprite system Audio system Gamepad Hotkeys Mouse Timers Network DDK interface 30

Engine 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 31

Game 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 … 32

Game 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 33

Game 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 34

Game Development Tools for Artists

   3D tools    Discrete 3dsMax Maya Softimage XSI 2D tools   Photoshop Illustrator Motion tools    Motion capture devices Motion Builder FiLMBOX 35

2nd Game System Analysis

36

What Will We Talk

   The idea about system analysis (SA) Mind mapping Case study - Term project 37

Why 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 Job A Job B Job C Job D 38

Why System Analysis (2/2)

   To do technical possibility analysis    技術可行性分析 R&D ?

Where is the technical bottleneck ?

Pre-processor for   Technical design document Project management Bridge from game design to programming 39

Something about System Analysis

   No standard procedures or approaches It’s not a theory.

 Experience You can use your own method/tool   UML Mind mapping   …  心智圖法 This is the one we will use in this course 40

Wang’s System Analysis Steps

     Brainstorming Integration Dependency analysis Create the project Technical design document (TDD) writing 41

Brainstorming

   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 / … 42

Integration

     Confirm the resource limitation Technical implement possibility Put all related items together Man month analysis   How many ?

Who ?

Jobs / System identification 43

Dependency Analysis

  Sort the Jobs   By job dependency By programmers’ schedule Prototype for scheduling 44

System Analysis – Create the Project

      Scheduling Job assignment Resource allocation Check points Milestones   Major check points Output  Risk management  Alternatives Risk management policy 45

Technical Design Document

         Specification Resources Design in details Implement methods (

工法

) Algorithms “Project” Output in each milestone SOP (optional)

TDD Template

46

Mind 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” 47

48

49

Mind Map Demo Using MindManager

  Use MindManager X5 pro Developed by MindJet 50

3rd The Game Main Loop

51

Win32 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; 52

Win32 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); } 53

Win32 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 ...

54

Event-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 55

Timers & 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 win32’s “WM_TIMER” message  Events For windows9x, WM_TIMER = 18.2 fps (maximum)    Input devices  Mouse  Keyboard Something coming from network System requests  Re-draw   Losing/getting the input focus … 56

Timers & 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 57

Implement 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; 58

Implement 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 < 0 || id >= MAXTIMERS) return; /* assign the timer's callback */ fyTimer[id].timer = fun; /* set lock-to-fps flag */ fyTimer[id].beLockFps = beLock; } /* calculate the ticks for one frame */ fyTimer[id].timeInv = (double) (fyFreq) / (double) fps; fyTimer[id].be1st = TRUE; fyTimer[id].beAble = TRUE; 59

Implement 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; */ 60

Implement 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(); 61

Implement the Timer (5/6)

/*-------------------- { check all timers ----------------------*/ void FYInvokeTimer() int i, skipS; double dTime; // get current time dTime = FYGetCurrentSystemTick(); for (i = 0; i < MAXTIMERS; i++) { if (fyTimer[i].beAble && fyTimer[i].timer != NULL) { // for the first time ....

.

if (fyTimer[i].be1st) { // initialize the timer fyTimer[i].be1st = FALSE; fyTimer[i].initTime = dTime; fyTimer[i].nxtTime = dTime + fyTimer[i].timeInv; (*(fyTimer[i].timer))(1); } 62

Implement the Timer (6/6)

else { if (fyTimer[i].beLockFps) { if (dTime >= fyTimer[i].nxtTime) { // calculate skip frames skipS = (int)((dTime - fyTimer[i].nxtTime) / (double)fyTimer[i].timeInv) + 1; // get next checking time fyTimer[i].nxtTime += (double) (skipS * fyTimer[i].timeInv); // check some abnormal conditions ...

} } } } // invoke the timer callback (*(fyTimer[i].timer))(skipS); } } else { (*(fyTimer[i].timer))(1); } 63

Game Loop (1/2)

 Single player Loop Check game over n Peek player input y Implement timer callback Rendering Exit the loop 64

Game Loop (2/2)

 Network client Loop Check game over n Peek user input y Receive messages Timer callbacks Exit From network Send messages Rendering To network 65

Jobs in Regular (In Timers)

            Check “Win/Lose” Check “Quit” Make objects moving … Play character’s 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 … 66

Jobs 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 … 67

4th TheFly3D Game Engine

68

The 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); 69

Hotkey Callback

//------------------ // quit the game //------------------ void QuitGame(WORLDid gID, BYTE code, BOOL value) { if (code == FY_ESCAPE) { if (value) { FyWin32EndWorld(gID); } } } 70

Mouse 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; } 71

The 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(); 72

Introduction 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 73

TheFly3D in A Chart

Game NPC System Combat System Virtual Agent FX System Trading System Game AI Story Script System Terrain Collision Character Dynamics Sound FX UI 3D Scene Mngmt 3D Graphics API 2D Sprite Gamepad Audio 2D API Hardware Input Device Network OS API Game Play Layer Engine Layer System Layer Developing Developed 74

Development 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

75

Create 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 SDK’s 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 76

The 1

st

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 77

Demo - Hello

Do it!

78

The 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

); 79

Initialize TheFly3D

   In general the 1 st  function to use TheFly3D is : FyWin32CreateWorld() After the calling successfully, you can get the non-zero (WORLDid) of a world object.

Assign the ID to a world function class object for manipulating the world.

ID // create a new world WORLDid gID = FyWin32CreateWorld (“Tester", 0, 0, 800, 600, 16, FALSE); FnWorld gw; gw.Object(gID); gw.SetTexturePath("Data\\textures"); 80

The World in TheFly3D

 A world is a set of graphics layers where the 3D objects acts on.

A World 3D Graphics Layers Frame Buffers (front + back) 81

The 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 ” Backdrop Lights Camera 3D Models Board A 3D Scene 82

The 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); 83

The 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… 84

The 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  Model geometry       Camera data Lighting data Terrain data Particle emitters Forces Audio to carry real 3D data including : Objects are created/deleted by its host scene.

Objects can be switched between scenes.

Objects should be assigned to a rendering group.

85

The 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) 86

A Scene Object

Shape

Parent Object

Hierarchy

Parameters

Move

Etc Transformation Geometric Data Motion Data

Animation Clone

87

A 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"); 88

TheFly3D Scene Tree

  A tree-based representation Simplified scene graph Root 89

TheFly3D Scene Tree Is Simplified Scene Graph

  A tree-based representation Simplified scene graph Root 90

Object Hierarchy

nID = scene.CreateObject(ROOT); cID = scene.CreateCamera(ROOT); FnObject model; model.Object(nID); model.SetParent(cID);

cID nID

91

Clone a Model Object

OBJECTid nID = scene.CreateObject(ROOT); FnObject model; model.Object(nID); OBJECTid nID1 = model.Instance();

nID nID1 Data

instance 92

TheFly3D 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); 93

TheFly3D 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 SOURCE_BLEND_MODE) values are same as the 94

Coordinate System

   Every model should have its own local coordinate system.

  Local space Model space  The space when it’s 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” z y Z Y x X 95

Transformation

  Three basic linear transformations used in TheFly3D.

   Translate Rotate Scale Principles :    Right-handed rule v’ = v M

0 M 1

Matrix in 12-element (I call the M12) Rotation matrix Translation vector a 0 a 3 a 6 a 9 a 1 a 4 a 7 a 10 a 2 a 5 a 8 a 11 0 0 0 1 96

Translation

model.Translate(dx, dy, dz, op);

x’ = x + dx y’ = y + dy z’ = z + dz T = 1 0 0 0 1 0 0 0 1 dx dy dz (dx dy dz)

97

Rotation

i.e. rotate with z axis model.Rotate(Z_AXIS, 30.0f, op); z

x’ = x cos

q

y’ = x sin

q

z’ = z – y sin

q

+ y cos

q

R

z = cos

q

-sin

q

sin cos

q q

0 0 0 0 1 0 0 0

x y 98

Scaling

model.Scale(sx, sy, sz, op);

x’ = x * sx y’ = y * sy z’ = z * sz T = sx 0 0 0 sy 0 0 0 sz 0 0 0

99

Matrix Operations

 Matrix operation  REPLACE, LOCAL, GLOBAL Z X op =

LOCAL

z op =

REPLACE

[M L ] [M] [M G ] op =

GLOBAL

y

Object Transformation Matrix

Y 100 x

TheFly3D 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 object’s matrix 101

Transformations vs Movements

  Transformation is the term used in computer graphics but not friendly games.

for We use movements to control the 3D objects moving around in the scene.

  Move forward Move right Turn right / left     Move up Turn right Turn left … Move up Move right Move forward 102

Facing Direction and Up Direction

   Each object is modeled with a facing direction and up direction “visually” In TheFly3D, we use –y axis for a model, z axis as the default facing direction as the default up direction But for a camera :   -z axis is the facing direction y axis is the up direction z y z up + facing to -y x 103

Move Forward (1/2)

new position = old position + distance *(facing direction in unit) 104

Move 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.Translate(0.0f, -dist, 0.0f, LOCAL); // facing to -y

105

Turn 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 / 106

Turn 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

107

Terrain

    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 108

Generate 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_following.cw3"); // generate the neighboring data for terrain following // otherwise, the terrain model is for visual only t.GenerateTerrainData(); 109

Put a Model on Terrain Using TheFly3D

// if tID is the terrain object (OBJECTid) // load a model OBJECTid nID = scene.CreateObject(ROOT); FnObject obj; obj.Object(nID); // put the model on terrain float pos[3]; pos[0] = x; pos[1] = y;

pos[2] = 10000.0f; // should be higher than the terrain!

obj.SetPosition(pos); obj.PutOnTerrain(tID, be3D, offset, rangeF, rangeB, angle); 110

Terrain Following

Terrain Following (3D) offset Terrain Following (2D) offset 111

Probe for a Model on Terrain

probeBack probeFront : terrain following check point 112

TheFly3D 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 113

TheFly3D 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 …) 114

TheFly3D 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 115

A Character

116

Character with motion 117

Introduction 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

Base

118

A 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 119

 The scene tree of a segmented character :

head up arm hand fore arm body foot groin thigh Base shin groin head up_arm_l body thigh_r thigh_l up_arm_r shin_r shin_l fore_arm_l fore_arm_r foot_r hand_l hand_r foot_l

120

A 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 121

A Bone-skin Character

   Bone-skin skeleton   Hierarchical bones Skin deformation run-timely Benefits   Drawbacks   Hierarchical structure Not segmented look More complicated than the other solutions Skin deformation need more CPU cost than transformation only Skin Bone B Bone A 122

Motion Production – by 3D Animation Tools (1/2)

 Keyframe system  3ds MAX   Softimage Maya 123

Motion 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 124

Motion Production – by Motion Capture (1/2)

  Mo tion Cap ture  “MoCap” in short Types :  Optical   Magnetic ...

125

Motion 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 126

The Root-Base Concept (1/2)

 Use root-base structure to construct the character

Root (groin) Base Base

127

The Root-Base Concept (2/2)

        A character has some the character system.

models to be the geometry roots The root plays as the gravity center of the character.

of 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 character’s movement.

We move the base to perform character’s moves.

128

Motion Data - Pose

    A set of frames to describe a character’s 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 walk run attack fall 129

Load a Character

FnScene scene; CHARACTERid actorID; .cwc is a character description file.

scene.Object(sceneID); actorID = scene.LoadCharacter(“fm.cwc"); 130

Play 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 (1 st time) actor.PlayBlendNode(

aaaID

, (float) 0,

START

, TRUE); // continue to play a pose actor.PlayBlendNode(

CURRENT

, (float) skip,

LOOP

, TRUE); 131

Make a Character to Move Forward

FnCharacter actor; // play walking pose actor.Object(actorID); actor.PlayBlendNode(CURRENT, (float) skip, LOOP, TRUE); // move it forward actor.MoveForward(dist, beTF, be3D, offset); 132

TheFly3D 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 133

TheFly3D 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 134

TheFly3D 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 object’s motion BLENDTREEid actor.GetBlendTree()  Get the character’s 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!

135

5th Game Mathematics

136

Essential Mathematics for Game Development

         Matrices Vectors Fixed-point Real Numbers Triangle Mathematics Intersection Issues Euler Angles Angular Displacement Quaternion Differential Equation Basics 137

Matrices (1/7)

 Matrix basics  Definition

A

= (a ij ) = a 11 .. a 1n . .

. .

a m1 .. a mn  Transpose

C

=

A

T c ij = a ji  Addition

C

=

A

+

B

c ij = a ij + b ij 138

Matrices (2/7)

 Scalar-matrix multiplication

C

= a

A

 Matrix-matrix multiplication

C

=

A B

c ij = a a ij c ij r = S a ik b kj k = 1 139

Matrices (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

scaling and rotation as : is transformed under translation,

V’ V’ V’

=

V

=

VS

+

D

=

VR

where

D S

and

R

is a translation vector and are scaling and rotation matrices 140

Matrices (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 + T x y’ = y + T y z’ = z + T z

V’ =

VT

[x ’ y ’ z ’ 1] = [x y z 1] 1 0 0 0 0 1 0 0 0 0 1 0 T x T y T z 1 = [x y z 1]

T

141

Matrices (5/7)

 Scaling Transformation x’ = xS x y’ = yS y z’ = zS z

V’ =

VS

[x ’ y ’ z ’ 1] = [x y z 1] S x 0 0 0 0 S y 0 0 0 0 S z 0 0 0 1 0 = [x y z 1]

S

Here S x , S y and S z are scaling factors.

142

Matrices (6/7)

 Rotation Transformations

R x R y

= = 1 0 0 0 0 cos q 0 -sin q sin cos q q 0 0 0 0 0 1 cos q 0 -sin q 0 0 1 0 0 sin q 0 cos q 0 0 0 0 1

R z

= cos q -sin q sin cos q q 0 0 0 0 0 0 1 0 0 0 0 1 143

Matrices (7/7)

 Net Transformation matrix [x ’ y ’ z ’ 1] = [x y z 1]

M 1

and [x” y” z” 1] = [x’ y’ z’ 1]

M 2

then the transformation matrices can be concatenated

M 3

=

M 1 M 2

and [x” y” z” 1] = [x y z 1]

M 3

 Matrix multiplication are

not commutative M 1 M 2

=

M 2 M 1

144

Vectors (1/5)

   A vector is an entity that possesses

magnitude direction

.

and A 3D vector is a triple : 

V

= (v 1 , v 2 , v 3 ), where each component v i is a scalar.

A ray (directed line segment), that possesses

position

,

magnitude

and

direction

.

(x 1 ,y 1 ,z 1 )

V

= (x 2 -x 1 , y 2 -y 1 , z 2 -z 1 ) (x 2 ,y 2 ,z 2 ) 145

Vectors (2/5)

 Addition of vectors

X

=

V

+

W

= (x 1 , y 1 , z 1 ) = (v 1 + w 1 , v 2 + w 2 , v 3 + w 3 )

V

+

W W W V

+

W V V

 Length of vectors |

V

| = (v 1 2 + v 2 2 + v 3 2 ) 1/2

U

=

V

/ |

V

| 146

Vectors (3/5)

 Cross product of vectors  Definition

X

=

V

X

W

= (v 2 w 3 -v 3 w 2 )

i

+ (v 3 w 1 -v 1 w 3 )

j

+ (v 1 w 2 -v 2 w 1 )

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.

N p V 2

polygon defined by 4 points

N p

=

V 1

X

V 2 V 1

147

Vectors (4/5)

 Transformation of the normal vectors

N

(

X

) = det

J J

-1T

N

(

x

) where

X J

= F(

x

) the Jacobian matrix,

J

i (

x

) = d F(

x

) d

x

i "Global and Local Deformations of Solid Primitives" Alan H. Barr Computer Graphics Volume 18, Number 3 July 1984 148

Vectors (5/5)

 Dot product of vectors  Definition |

X

| =

V

.

= v 1 w 1

W

+ v 2 w 2 + v 3 w 3  Application

V

q

W

cos q =

V

.

W

|

V

||

W

| 149

Fixed Point Arithmetics (1/2)

 Fixed point arithmetics : n bits (signed) integer  Example : N = 16 gives range –32768  ă  32767  We can use fixed scale to get the decimals.

a =

ă / 2

8 8 integer bits 1 1 1 8 fractional bits

ă = 315, a = 1.2305

150

Fixed Point Arithmetics (2/2)

 Multiplication then requires rescaling e = a .

c = ă / 2 8 . ĉ / 2 8  ĕ = (ă .

ĉ) / 2 8  Addition just like normal e = a+c = ă / 2 8 + ĉ / 2 8  ĕ = ă + ĉ 151

Fixed 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) 152

Triangular Coordinate System

h b (x b ,y b ,z b ) h a (x a ,y a ,z a ) A c h

p

A b A a A a h = h a A A b + h A b h c (x c ,y c ,z c ) A c + h A c where A = A a + A b + A c If (A a < 0 || A b < 0 || A c < 0) than the point is outside the triangle

“Triangular Coordinate System”

153

Triangle Area – 2D

Area of a triangle in 2D x a A = ½ x b x c x a y a y b y c y a = ½ (x a *y b + x b *y c + x c *y a – x b *y a – x c *y b – x a *y c ) (x a ,y a ,z a ) (x b ,y b ,z b ) (x c ,y c ,z c )

154

Triangle Area – 3D

Area of a triangle in 3D A = ½ (N

.

Sum(P

i1

cross P

i2

)) 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(n[0] * n[0] + n[1] * n[1] + n[2] * n[2]) * 2.0f

; /* find sum of cross products */ sum0 = x1[ 1 ] * (-x0[ 2 ] + x2[ 2 ]) + x2[ 1 ] * (-x1[ 2 ] + x0[ 2 ]) + x0[ 1 ] * (-x2[ 2 ] + x1[ 2 ]); sum1 = x1[ 2 ] * (-x0[ 0 ] + x2[ 0 ]) + x2[ 2 ] * (-x1[ 0 ] + x0[ 0 ]) + x0[ 2 ] * (-x2[ 0 ] + x1[ 0 ]); sum2 = x1[ 0 ] * (-x0[ 1 ] + x2[ 1 ]) + x2[ 0 ] * (-x1[ 1 ] + x0[ 1 ]) + x0[ 0 ] * (-x2[ 1 ] + x1[ 1 ]); } /* find the area */ return = (sum0 * n[0] + sum1 * n[1] + sum2 * n[2]) / len; 155

Triangular Coordinate System - Application

    Terrain following  Interpolating the height of arbitrary point within the triangle Hit test  Intersection of a ray from camera to a screen position with a triangle Ray cast  Intersection of a ray with a triangle Collision detection  Intersection 156

Intersection

  Ray cast Containment test 157

Ray Cast – The Ray

  Cast a ray to calculate the intersection of the ray with models Use parametric equation for a ray { x = x 0 y = y 0 z = z 0 + (x 1 + (y 1 + (z 1 – x 0 ) t – y 0 ) t, t = 0, – z 0 ) t 8    When t = 0, the ray is on the start point (x 0 ,y 0 ,z 0 ) Only the t  0 is the answer candidate The smallest positive t is the answer 158

Ray Cast – The Plane

       Each triangle in the 3D models has its plane equation.

Use ax + by + cz + d = 0 as the plane equation.

(a, b, c) |d| is the plane normal vector.

is the distance of the plane to origin.

Substitute the ray equation into the plane.

Solve the t to find the intersect point.

Check the intersect point within the triangle or not by using “Triangle Area Test” (p. 155) 159

2D Containment Test

Intersection = 1, inside Intersection = 2, outside

(x 0 , y 0 )

Intersection = 0, outside

Trick : Parametric equation for a ray which is parallel to the x-axis { x = x 0 y = y 0 + t , t = 0, 8 “

if the No. of intersection is odd, the point is inside, otherwise, is outside”

160

3D Containment Test

 Same as the 2D containment test “

if the No. of intersection is odd, the point is inside, otherwise, is outside”

161

Euler Angles

 A rotation is described as a sequence of rotations about three mutually orthogonal coordinates axes fixed in space  X-roll, Y-roll, Z-roll

R

( q 1 , q 2, q 3 ) represents an x-roll, followed by y-roll, followed by z-roll

R

( q 1 , q 2, q 3 ) = c 2 c 3 s 1 s 2 c 3 -c 1 s 3 where s i = sin q i c 2 s 3 s 1 s 2 s 3 +c 1 c 3 -s 2 s 1 c 2 0 0 c 1 s 2 c 3 +s 1 s 3 c 1 s 2 s 3 -s 1 c 3 c 1 c 2 0 0 0 0 1 and c i = cos q i  There are 6 possible ways to define a rotation.

 3!

162

Euler Angles & Interpolation

   Interpolation happening on each angle Multiple routes for interpolation More keys for constrains

R

z z y x x

R

y 163

Angular Displacement

R

( q ,

n

),

n

is the rotation axis.

q

r Rr n r r v V r h n r v

q

Rr v V r h r v = (n .

r)n = r - (n .

r)n

, rotate into position

Rr v V = n

x

r v = n

x

r

->

Rr v =

(cos q )

r v +

(sin q )

V Rr = Rr h = r h + + Rr v

(cos q )

r v +

(sin q )

V = (n .

r)n +

(cos q ) (

r - (n .

r)n) +

(sin q )

n

x

=

(cos q )

r +

(1-cos q )

n (n .

r) +

(sin q )

n

x

r r

164

Quaternion

        Sir William Hamilton (1843) From Complex numbers (a +

i

b),

i

2 = -1 16,October, 1843, Broome Bridge in Dublin 1 real + 3 imaginary = 1 quaternion

q

= a + b

i

+ c

j

+ d

k i

2 =

j

2

ij

=

k

=

k

2 = -1 &

ji

= -

k

, cyclic permutation

i

-

j

-

k

-

i q

= (s,

v

), where (s,

v

) = s + v

x

i

+ v

y

j

+ v

z

k

165

Quaternion Algebra

q 1

= (s 1 , v

1

) and q

2

= (s 2 , v

2

)

q 3

= q

1 q 2

= (s 1 s 2 - v

1 .

v 2

, s 1

v 2

+ s 2

v 1

+ v

1

xv

2

)

Noncommutative

Conjugate of q = (s, v), q = (s, -v) qq = s 2 + |v| 2 = |q| 2 A unit quaternion q = (s, v), where qq = 1 A pure quaternion p = (0, v)

166

Quaternion VS Angular Displacement

Take a pure quaternion p = (0, r) and a unit quaternion q = (s, v) where qq = 1 and define R

q

(p) = qpq

-1

where q

-1

= q for a unit quaternion

R q

(p) = (0, (s 2 - v

.

v)r + 2v(v

.

r) + 2svxr) Let q = (cos

q

, sin

q

n), |n| = 1

R q

(p) = (0, (cos 2

q

- sin 2

q

)r + 2sin 2

q

n(n

.

r) + 2cos

q

sin

q

= (0, cos2

q

r + (1 - cos2

q

)n(n

.

r) + sin2

q

nxr) nxr) Conclusion : The act of rotating a vector r by an angular displacement (

q

, n) is the same as taking this displacement, ‘lifting’ it into quaternion space, by using a unit quaternion (cos( sin(

q

/2)n)

q

/2),

167

Quaternion VS Rotation Matrix

q = (w,x,y,z)

1-2y 2 -2z 2 2xy-2wz 2xz+2wy 0 2xy+2wz 1-2x 2 -2z 2 2yz-2wx 0 2xz-2wy 2yz+2wx 1-2x 2 -2y 2 0 0 0 0 1 168

M 0 M 3 M 1 M 4 M 2 M 5 0 0 M 6 M 7 M 8 0 0 0 0 1

float tr, s; tr = m[0] + m[4] + m[8]; if (tr > 0.0f) { s = (float) sqrt(tr + 1.0f); q->w = s/2.0f; s = 0.5f/s; q->x = (m[7] - m[5])*s; q->y = (m[2] - m[6])*s; q->z = (m[3] - m[1])*s; } else { float qq[4]; int i, j, k; int nxt[3] = {1, 2, 0}; i = 0; if (m[4] > m[0]) i = 1; if (m[8] > m[i*3+i]) i = 2; j = nxt[i]; k = nxt[j]; s = (float) sqrt((m[i*3+i] - (m[j*3+j] + m[k*3+k])) + 1.0f); qq[i] = s*0.5f; if (s != 0.0f) s = 0.5f/s; qq[3] = (m[j+k*3] - m[k+j*3])*s; qq[j] = (m[i+j*3] + m[j+i*3])*s; qq[k] = (m[i+k*3] + m[k+i*3])*s; } q->w = qq[3]; q->x = qq[0]; q->y = qq[1]; q->z = qq[2];

169

Quaternion Interpolation

 Spherical linear interpolation,

slerp

A P t f B

slerp(q 1 , q 2

, t) = q

1

sin((1 - t)

f

) sin

f

+ q 2

sin(t

f

) sin

f 170

Differential Equation Basics

   Initial value problems ODE  Ordinary differential equation Numerical solutions   Euler’s method The midpoint method 171

Initial Value Problems

 An ODE

x

.

=

f

(

x

, t) where

f

x

is a known function is the state of the system,

x

.

x

&

x

.

are vectors

x

(t 0 ) =

x

0 , initial condition is the

x

’s time derivative   Vector field Solutions   Symbolic solution Numerical solution Start here Follow the vectors … 172

Euler’s Method

x

(t + D t) =

x

(t) + D t

f

(x, t)      A numerical solution  A simplification from Tayler series Discrete time steps starting with initial value Simple but not accurate   Bigger steps, bigger errors O( D t 2 ) errors Can be unstable Not even efficient 173

The Midpoint Method

Error term Concept :

x

(t 0

.

..

+ h) =

x

(t 0 ) + h

x

(t 0 ) + h 2 /2

x

(t 0 ) +

O

(h 3 ) Result :

x

(t 0 +h) =

x

(t 0 ) + h(

f

(x 0 + h/2

f

(x 0 )) Method : a. Compute an Euler step D

x

= D t

f

(

x

, t) b. Evaluate f at the midpoint

f mid

=

f

((

x

+ D

x

)/2, (t+ D t)/2) c. Take a step using the midpoint

x

(t+ D t) =

x

(t) + D t

f mid

c b a 174

The Runge-Kutta Method

  Midpoint = Runge-Kutta method of order 2 Runge-Kutta method of order 4  O(h 5 ) k 1 k 2 k 3 k 4 = h f(x 0 , t 0 ) = h f(x 0 = h f(x = h f(x 0 0 + k 1 /2, t 0 + k 2 /2, t 0 + k 3 , t 0 + h/2) + h/2) + h) x(t 0 +h) = x 0 + 1/6 k 1 + 1/3 k 2 + 1/3 k 3 + 1/6 k 4 175

Initial Value Problems - Application

   Dynamics  Particle system Game FX system Fluid simulation 176

6th Game Geometry

177

Game Models

      Geometry   Position vertex normals    vertex colors texture coordinates Tangent / Bi-normal Topology  Primitive  Lines / triangles / surfaces / … Property  Materials  Textures Motion Hierarchy Level-of-detail 178

Geometry Data

      Vertex position   (x, y, z , w ) In model space or screen space Vertex normal  (n x , n y , n z ) Vertex color  (r, g, b) or (diffuse, specular) Texture coordinates on vertex  (u 1 , v 1 ), (u 2 , v 2 ), … Skin weights  (bone 1 , w 1 , bone 2 , w 2 , …) Tangent and bi-normal Bn N T 179

Topology Data

     Lines   Line segments Polyline  Open / closed Indexed triangles Triangle strips / fans Surfaces  N on u niform R ational B S pline ( NURBS ) Subdivision 180

Indexed Triangles

  Geometric data  Vertex data    v 0 , v 1 , v 2 , v 3 , … (x, y, z, n x , n y , n z , t u , t v ) or (x, y, z, c r , c g , c b , t u , t v, … ) Topology   Face v 0 v 3 Edge table v 6 v 7

polygon normal

v 0

vertex normal

v 7 v 3 v 6

Right-hand rule for index

181

Triangle Strips

v 0 T 0 T 1 v 2 T 2 v T 3 v 4 T 4 v 5 T 5 6 v 1 v 3 v 0 , v 1 , v 2 , v 3 , v 4 , v 5 , v 6 , v 7 v 7

“Get great performance to use triangle strips for rendering on current hardware

182

Property on Surface

   Material Textures Shaders 183

Materials

  Material     Ambient  Environment  Non-lighted area Diffuse  Dynamic lighting Emissive  Self-lighting Specular with shineness  Hi-light   View-dependent Not good for hardware rendering Local illumination 184

Textures

 Textures    Single texture Texture coordinate animation Texture animation   Multiple textures Alphamap Base color texture Material or vertex colors Lightmap 185

Shaders

  Programmable shading language   Vertex shader Pixel shader Procedural way to implement some process of rendering  Transformation       Lighting Texturing BRDF Rasterization Pixel fill-in Post-processing for rendering 186

Shader Pipeline

Vertex Data Topology Data Classic Transform & Lighting Vertex Shader Clipping & Viewport Mapping Texturing Pixel Shader Fog Alpha, Stencil, Depth Testing Geometry Stage Rasterizer Stage 187

                

Powered by Shader

Per-pixel lighting Motion blur Volume / Height fog Volume lines Depth of field Fur rendering Reflection / Refraction NPR Shadow Linear algebra operators Perlin noise Quaternion Sparse matrix solvers Skin bone deformation Normal map Displacement map Particle shader   Procedural Morphing Water Simulation 188

Motion Data

   Time-dependent data Transformation data   Position Orientation Formats   Pivot Position vector    Quaternion Eurler angles Angular displacement 189

Level-of-detail

   Discrete LOD  Switch multiple resolution models run-timely Continuous LOD  Use progressive mesh to dynamically reduce the rendered polygons View-dependent LOD  Basically for terrain 190

Progressive Mesh

   Render a model in different Level-of-Detail at run time User-controlledly or automatically change the percentage of rendered vertices Use collapse map to control the simplification process Collapse map Vertex list Index Map 0 0 1 1 2 1 3 2 4 3 5 0 6 4 7 5 8 6 0 1 2 3 4 5 6 7 8 Triangle list 0 2 5 0 0 1 2 3 0 5 8 6 4 191

View-dependent LOD for Terrain - ROAM

     Real-time Optimal Adapting Meshes (ROAM) Use height map Run-timely to re-construct the active (for rendering) geometric topology (re-mesh) to get an optimal mesh (polygon count) to improve the rendering performance Someone calls this technique as the view-dependent level of-detail Very good for fly-simulation-like application 192

Level-of-detail Suggestion

   Apply progressive mesh for multi-resolution model generation Use in-game discrete LOD for performance tuning Why ?

  For modern game API / platform, dynamic vertex update is costly on performance Lock video memory stall CPU/GPU performance 193

Bounding Volume

     Bounding sphere Bounding cylinder Axis-aligned bounding box (AABB) Oriented bounding box (OBB) Discrete oriented polytope (k-DOP)

Bounding Sphere AABB Bounding Cylinder OBB k-DOP

194

Bounding Volume - Application

    Collision detection Visibility culling Hit test Steering behavior  In “Game AI” section 195

Application Example - Bounding Sphere

B 2 B 1 D c 2 c 1

Bounding sphere B 1 (c 1 , r 1 ), B 2 (c 2 , r 2 ) If the distance between two bounding spheres is larger than the sum of radius of the spheres, than these two objects have no chance to collide.

D > Sum(r 1 , r 2 )

196

Application Example - AABB

 Axis-aligned bounding box (AABB)   Simplified calculation using axis-alignment feature But need run-timely to track the bounding box

AABB

197

Application Example - OBB

  Oriented bounding box (OBB)  Need intersection calculation using the transformed OBB geometric data  3D containment test  Line intersection with plane For games, 

OBB

198

7th Terrain

199

Introduction

    Very game type oriented data Terrain    For visual purpose  Ground / Building / Static models / Dynamic models For terrain following  Polygon mesh  Grids For path finding   Polygon mesh Grids Terrain following  Make a 3D entity (character or model) walking on terrain Path finding  Find a “shortest” path to walk before moving   Will be taught in Game AI section.

A* algorithm 200

Terrain Formats

    Grid   2D Quadtree Height map   Procedural height map Using noise function to generate the height ROAM  R eal-time O ptimally A dapting M eshes Triangular mesh   Procedurally generated Created by artists Perlin Noise 201

Grid Map

 2D grid map   Rectangular or Hexagonal grids Attributes  Height   Walkable or not Texture pattern ID  Step look terrain  Application   2D games 3D games with god view  2D tile-based game terrain 202

Height Map

 Almost as same as 2D grid map but :"    Height on grid vertex Only height is saved Regular grid  Irregular grid but structured  Application   As the base data structure for ROAM terrain Water simulation Top view 203

ROAM

 Real-time optimally adapting mesh  http://www.llnl.gov/graphics/ROAM/  Application  Fly-simulation 204

Chunked LOD Terrain

 Use quad tree to construct the level-of-detail of terrain  A quad tree for LOD 205

Triangular Mesh

  Possibly the most popular way for 3D games   General Can be created by artists Multiple-layered terrain issue 206

Terrain Following Using Triangular Mesh

  Solve the terrain height for the object to stand on.

 Use the triangular coordinate system (p. 154) Find the next neighboring triangle  Half-edge data structure 207

Half-edge (1/2)

  Create cohesive relationship between triangles using “half edge” Use half-edge table to search the neighboring triangles

Edge = two halves

208

Half-edge (2/2)

struct HE_edge { HE_vert* vert; // vertex at the end of the half-edge HE_edge* pair; // oppositely oriented adjacent half-edge HE_face* face; // face the half-edge borders HE_edge* next; // next half-edge around the face }; struct HE_vert { float x; float y; float z; HE_edge* edge; // one of the half-edges // emantating from the vertex }; struct HE_face { HE_edge* edge; // one of the half-edges bordering the face };

http://www.flipcode.com/tutorials/tut_halfedge.shtml

209