Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 3D Game Programming Using TheFly3D ©

Similar presentations


Presentation on theme: "1 3D Game Programming Using TheFly3D ©"— Presentation transcript:

1 1 3D Game Programming Using TheFly3D © 王銓彰kevin.cwang@msa.hinet.net

2 2 Introduction to Game Development (3hr) Introduction to Game Development (3hr) Game System Analysis (3hr) Game System Analysis (3hr) The Game Main Loop (3hr) The Game Main Loop (3hr) 3D Game Engine Training (TheFly3D) (6hr) 3D Game Engine Training (TheFly3D) (6hr) Game Mathematics (3hr) Game Mathematics (3hr) Geometry for Games (3hr) Geometry for Games (3hr) Advanced Scene Management System (6hr) Advanced Scene Management System (6hr) Terrain (3hr) Terrain (3hr) Game AI (6hr) Game AI (6hr) Game Physics (3hr) Game Physics (3hr) Game FX (3hr) Game FX (3hr) Network Gaming (3hr) Network Gaming (3hr) Introduction to MMOG (3hr) Introduction to MMOG (3hr) The Non-3D Parts (3hr) The Non-3D Parts (3hr)課程大綱

3 3 One Term Project One Term Project –The Students will divide into several teams –Use TheFly3D Game Engine to code a 3D Real-time Strategy Game –The Teacher will Provide Graphics Materials Two Examinations Two Examinations Homework will be closely coupled with the term project Homework will be closely coupled with the term project 課程要求

4 4 目前 目前 – 昱泉國際股份有限公司 技術顧問 – 數位內容學院 專任講師 / 顧問 – 資策會多媒體研究所 專案顧問 學歷 學歷 – 台灣大學土木工程學系畢業 資歷 資歷 –97-03 昱泉國際股份有限公司 技術長 –96-96 虛擬實境電腦動畫股份有限公司 研發經理 –93-96 西基電腦動畫股份有限公司 研發經理 –90-93 國家高速電腦中心 助理研究員 –89-90 台灣大學土木工程學系 CAE Lab 研究助理 王銓彰

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

6 6 專長 (Expertise) 專長 (Expertise) –3D Computer Graphics –Geometric Modeling –Numerical Methods –Character Animation –Photo-realistic Rendering –Real-time Shading –Volume Rendering 王銓彰

7 7 應用領域 (Applications) 應用領域 (Applications) – 即時 3D 遊戲開發 (Real-time 3D Game Development) – 電腦動畫 (Computer Animation) – 虛擬實境 (Virtual Reality) – 電腦輔助設計 (Computer-aided Design) – 科學視算 (Scientific Visualization) 王銓彰

8 8 IntroductionTo Game Development

9 9 Game Platform Game Platform Game Types Game Types Game Team Game Team Game Development Pipeline Game Development Pipeline Game Software System Game Software System Tools Tools Introduction to Game Dev

10 10 PC PC –Single player –Match Makings –MMOG (Massive Multi-player Online Game) –Web-based Games Console Console –Sony PS2 –MS Xbox –Nintedo GameCube Arcade Arcade Mobile Mobile –GBA –Hand-held Game Platform

11 11 Designed for Office Application Designed for Office Application Not for Entertainment Not for Entertainment A Virtual Memory System A Virtual Memory System –Unlimited memory using But Video Memory is Limited But Video Memory is Limited PCI/AGP might be a Challenge PCI/AGP might be a Challenge Open Architecture Open Architecture Compatibility Test is Important Compatibility Test is Important Development is Easy to Setup Development is Easy to Setup Game Development on PC

12 12 Specific Hardware Designed for Games Specific Hardware Designed for Games Single User / Single Process OS Single User / Single Process OS In General no Hard Disk Drive (??) In General no Hard Disk Drive (??) Closed System Closed System Very Native Coding Way Very Native Coding Way –Proprietary SDK –Hardware related features Limited Resources Limited Resources –Memory One Console runs, the others do! One Console runs, the others do! Game Development on Console

13 13 Game Types RPG (Role playing games) RPG (Role playing games) AVG (Adventure games) AVG (Adventure games) RTS (Real-time strategy games) RTS (Real-time strategy games) FPS (First-person shooting games) FPS (First-person shooting games) MMORPG MMORPG SLG (???, 戰棋 ) SLG (???, 戰棋 ) Simulation Simulation Sports Sports Puzzle games Puzzle games Table games Table games

14 14 Game Team 組成 開發團隊 開發團隊 – 製作人 – 執行製作人 – 企劃團隊 – 程式團隊 – 美術團隊 行銷業務團隊 行銷業務團隊 測試團隊 測試團隊 遊戲審議委員會 遊戲審議委員會

15 15 遊戲製作人 (Producer) Team Leader (always) Team Leader (always) 資源管理 (Resource Management) 資源管理 (Resource Management) 行政管理 (Administration) 行政管理 (Administration) 向上負責 (Upward Management) 向上負責 (Upward Management) 專案管理 (Project Management) 專案管理 (Project Management)

16 16 遊戲執行製作人 專案管理執行 (Project Management) 專案管理執行 (Project Management) Daily 運作 Daily 運作 House Keeping House Keeping Not full-time job position Not full-time job position

17 17 遊戲企劃 故事設計 (Story Telling) 故事設計 (Story Telling) 腳本設計 (Scripting) 腳本設計 (Scripting) 玩法設計 (Game Play Design) 玩法設計 (Game Play Design) 關卡設計 (Level Design) 關卡設計 (Level Design) 遊戲調適 (Game Tuning) 遊戲調適 (Game Tuning) 數值設定 (Numerical Setup) 數值設定 (Numerical Setup) AI 設計 (Game AI) AI 設計 (Game AI) 音效設定 (Sound FX Setup) 音效設定 (Sound FX Setup) 場景設定 (Scene Setup) 場景設定 (Scene Setup)

18 18 遊戲美術 場景 (Terrain) 場景 (Terrain) 人物 (Character) 人物 (Character) 建模 (Models) 建模 (Models) 材質 (Textures) 材質 (Textures) 動作 (Motion / Animation) 動作 (Motion / Animation) 特效 (FX) 特效 (FX) User Interface User Interface

19 19 遊戲程式 遊戲程式 (Game Program) 遊戲程式 (Game Program) 遊戲開發工具 (Game Tools) 遊戲開發工具 (Game Tools) –Level Editor –Scene Editor –FX Editor –Script Editor 遊戲 Data Exporters from 3D Software 遊戲 Data Exporters from 3D Software –3dsMax / Maya / Softimage Game Engine Development Game Engine Development Online Game Server Development Online Game Server Development

20 20 遊戲開發流程 Basic Procedures for Game Development IdeaProposalProductionIntegration Testing DebugTuning Concept Approval PrototypePre-alphaAlphaBetaFinal 發想 (Idea) 發想 (Idea) 提案 (Proposal) 提案 (Proposal) 製作 (Production) 製作 (Production) 整合 (Integration) 整合 (Integration) 測試 (Testing) 測試 (Testing) – 除錯 (Debug) – 調適 (Tuning) > Concept Approval > 雛形 (Prototype) > Pre-alpha > Alpha > Beta

21 21 遊戲發想 (Concept Design) 遊戲類型 (Game Types) 遊戲類型 (Game Types) 遊戲世界觀 (Game World) 遊戲世界觀 (Game World) 故事 (Story) 故事 (Story) 遊戲特色 (Features) 遊戲特色 (Features) 遊戲玩法 (Game Play) 遊戲玩法 (Game Play) 遊戲定位 (Game Product Positioning) 遊戲定位 (Game Product Positioning) –Target player –Marketing segmentation / positioning 風險評估 (Risk) 風險評估 (Risk) –SWOT (Strength/Weakness/Opportunity/Threat)

22 22 遊戲提案 (Proposal) 系統分析 (System Analysis) 系統分析 (System Analysis) GDD 撰寫 (Game Design Document) GDD 撰寫 (Game Design Document) MDD 撰寫 (Media Design Document) MDD 撰寫 (Media Design Document) TDD 撰寫 (Technical Design Document) TDD 撰寫 (Technical Design Document) 遊戲專案建立 (Game Project) 遊戲專案建立 (Game Project) –Schedule –Milestones / Check points –Risk management 測試計畫書 測試計畫書 團隊建立 (Team Building) 團隊建立 (Team Building)

23 23 遊戲開發 (Production) 美術量產製作 美術量產製作 –Modeling –Textures –Animation –Motion –FX 程式開發 (Coding) 程式開發 (Coding) 企劃數值設定 企劃數值設定 … 量產 !

24 24 遊戲整合 (Integration) 關卡串聯 (Level Integration) 關卡串聯 (Level Integration) 數值調整 (Number Tuning) 數值調整 (Number Tuning) 音效置入 (Audio) 音效置入 (Audio) 完成所有美術 完成所有美術 程式與美術結合 程式與美術結合 Focus Group (User Study) Focus Group (User Study) –Release some playable levels for focus group

25 25 遊戲測試 (Testing) Alpha 測試 Alpha 測試 – 除錯 (Debug) Beta 測試 Beta 測試 – 數值微調 –Game play 微調 – 對線上遊戲而言 (MMOG) » 封閉測試 (Closed Beta) » 開放測試 (Open Beta) 極限測試 (Critical Testing) 極限測試 (Critical Testing) – 線上遊戲才有

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

27 27 Game Software System Hardware 3D Graphics API2D APIInput DeviceOS API3D Scene Mngmt2D SpriteGamepadNetworkAudioTerrainCollisionCharacterUIDynamicsSound FX Fighting SystemFX SystemGame AIScript System NPC SystemVirtual AgentTrading SystemStory Game Game Play Layer Engine Layer System Layer

28 28 System Layer - APIs 3D Graphics API 3D Graphics API –DirectX 9.0 SDK – Direct3D –OpenGL 2.0 2D API 2D API –DirectX 9.0 SDK - DirectMedia –Win32 GDI Input Device Input Device –DirectX 9.0 SDK – DirectInput Audio Audio –DirectX 9.0 SDK – DirectSound / Direct3DSound / DirectMedia –OpenAL

29 29 System Layer - APIs OS API OS API –Win32 SDK –MFC Network Network –DirectX 9.0 SDK – DirectPlay –Socket library

30 30 Engine Layer 3D Scene Management System 3D Scene Management System –Scene Graph Shaders Shaders 2D Sprite System 2D Sprite System Audio System Audio System Gamepad Gamepad Hotkey Hotkey Mouse Mouse Timers Timers Network Network DDK Interface DDK Interface

31 31 Engine Layer Terrain Terrain Advanced Scene Management – Space Partition Advanced Scene Management – Space Partition –BSP Tree –Octree Character System Character System Motion Blending Techniques Motion Blending Techniques Dynamics Dynamics Collision Detection Collision Detection SoundFX SoundFX User Interface User Interface

32 32 Game Play Modula NPC (Non-playable Characters) NPC (Non-playable Characters) Game AI Game AI –Path Finding –Finite State Machine –…–…–…–… Avatar Avatar Combat System Combat System FX System FX System Script System Script System Trading System Trading System Number System Number System …

33 33 Game Dev Tools Visual C/C++ Visual C/C++ –.net 2003 –Visual C/C++ 6.0+ SP5 DirectX DirectX –Current 9.0c NuMega BoundsChecker NuMega BoundsChecker Intel vTune Intel vTune 3D Tools 3D Tools –3dsMax/Maya/Softimage In-house Tools In-house Tools

34 34 Game System Analysis

35 35 What Will We Talk Here Idea about System Analysis (SA) Idea about System Analysis (SA) Mind mapping Mind mapping Case Study - Our Term Project Case Study - Our Term Project

36 36 Why SA ? (1/2) For 程式結構 Analysis For 程式結構 Analysis –Program modulus –Tools To Identify 工作量 To Identify 工作量 –Programs/tools under development For 資源 management For 資源 management –Man month –How many programmers ? –Development tools ? –Specific tools ? For Job Dependency Analysis For Job Dependency Analysis

37 37 Why SA ? (2/2) To make 技術可行性 Analysis To make 技術可行性 Analysis –R&D ? Pre-processor for Pre-processor for –Technical design document –Project management Bridge from Game Design to Programming Bridge from Game Design to Programming

38 38 Something about SA No Standard Procedures No Standard Procedures It’s Not a Theory, Just Something Must Be Done! It’s Not a Theory, Just Something Must Be Done! You Can Have Your Own Method You Can Have Your Own Method –UML –Mind mapping ( 心智圖法 ) »This is the one we will use for this course –…–…–…–…

39 39 The SA Steps I Used Brainstorming Brainstorming Integration Integration Dependency Analysis Dependency Analysis Create the Project Create the Project Write the Technical Design Document (TDD) Write the Technical Design Document (TDD)

40 40 SA – Brainstorming Based on the Game Design to Put Everything As Many As You Could Based on the Game Design to Put Everything As Many As You Could Use Mind mapping Use Mind mapping Including 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 / …

41 41 SA – Integration Confirm the Resource Limitation Confirm the Resource Limitation Technical Implement Possibility Technical Implement Possibility Put All Related Items Together Put All Related Items Together Man Month Analysis Man Month Analysis –How many ? –Who ? Jobs/System Identification Jobs/System Identification

42 42 SA – Dependency Analysis Sort the Jobs Sort the Jobs –By dependency –By programmers Prototype for scheduling Prototype for scheduling

43 43 SA – Create the Project Scheduling Scheduling Job Assignment Job Assignment Resource Allocation Resource Allocation Check points Check points Milestones Milestones Risk Management Policy Risk Management Policy

44 44 SA – Write the TDD Specification Specification Resources Resources Design in details Design in details Implement Methods ( 工法 ) Implement Methods ( 工法 ) Algorithms Algorithms The Project The Project Output in Each Milestone Output in Each Milestone SOP (optional) SOP (optional)

45 45 Mind Mapping 心智圖法 心智圖法 A Radiant Thinking Tool A Radiant Thinking Tool Applications Applications – 讀書心得 –Proposal – 上課筆記 – 遊記 –System Analysis –…–…–…–… Reference Reference –Program »Visio »MindManager –Tony Buzan, Barry Buzan, “ The Mind Map Book: How to Use Radiant Thinking to Maximize Your Brain's Untapped Potential ”

46 46

47 47

48 48 Mind Mapping Demo Use MindManager X5 pro Use MindManager X5 pro Developed By MindJet Developed By MindJet

49 49 Term Project System Analysis

50 50 Game Design (1/3) Real-time Strategy War Game Real-time Strategy War Game Mission-based Levels Mission-based Levels Mouse-driven Controls Mouse-driven Controls Player vs Computer Player vs Computer State-based AI State-based AI Group Movement Group Movement

51 51 Game Design (2/3) PC – Controlled by Player PC – Controlled by Player –“Blue Team” »Move »Attack –AI »Standby »Anti-attack NPC – Controlled by Computer NPC – Controlled by Computer –“Red Team” - Enemy »Triggered by time table –AI »Move »Standby »Anti-attack »Attack

52 52 Game Design (3/3) Game AI Game AI –Finite state machine –Path finding »3D »A* –Steering behavior »Flocks / Schools / Herds Flocks / Schools / HerdsFlocks / Schools / Herds »http://www.red3d.com/cwr/boids/ http://www.red3d.com/cwr/boids/ –AI Game Programming Wisdom, Charles River Media (1 & 2)

53 53 System Analysis Run MindManager.exe Run MindManager.exe

54 54 Game Main Loop

55 55 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; }

56 56 Win32 Application (2/3) LRESULT CALLBACK KKMainProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { LRESULT l = FALSE;... // switch for all incoming messages from WindowsX switch (uMsg) { case WM_KEYDOWN:... l = TRUE; break;... } // echo the result if (l) { return l; } else { return DefWindowProc(hWnd, uMsg, wParam, lParam); }

57 57 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... }

58 58 Event-driven Programming (1/3) Win32 Programs Are Event-driven Win32 Programs Are Event-driven –Messages = events –So as all windows system (for example : X window) We Need a Loop to Check All Incoming Events We Need a Loop to Check All Incoming Events The Loop The Loop –Check all incoming events (messages) –Handle the events –Check timing and do something in regular Incoming Events Incoming Events –Interrupts –System requests

59 59 Event-driven Programming (2/3) Timers (do something in regular timing) 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 »For windows9x, WM_TIMER = 18.2 fps (maximum) Events Events –Input devices »Mouse »Keyboard –Something coming from network –System requests »Re-draw »Losing/getting the input focus »…»…»…»…

60 60 Event-driven Programming (3/3) Therefore, You Can Have Two Types of Jobs (Callbacks) to Do (Call) Therefore, You Can Have Two Types of Jobs (Callbacks) to Do (Call) –In regular »Timers callbacks –By requests »Input device callbacks Same As a Game Main Program Same As a Game Main Program –A game is an interactive application –A game is time-bound »Rendering in 30fps or 60fps »Motion data in 30fps »Game running in 30fps »…»…»…»…

61 61 Implement the Timer (1/6) Use Performance Counter on PC Use Performance Counter on PC // 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;

62 62 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 = 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; }

63 63 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; */

64 64 Implement the Timer (4/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); }

65 65 Implement the Timer (5/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); }

66 66 Implement the Timer (6/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(); }

67 67 Game Loop (1/2) Single Player Single Player Check game over Peek player input Implement timer callback Exit the loop Rendering Loop y n

68 68 Game Loop (2/2) Network Client Network Client Check game over Peek user input Receive messages Send messages Timer callbacks Rendering Exit y n From network To network Loop

69 69 Jobs in Regular (Typically) Check Win/Loose Check Win/Loose Check Quit Check Quit Objects Moving … Objects Moving … Play Character’s Motion to Next Frame Play Character’s Motion to Next Frame Play Animation to Next Frame Play Animation to Next Frame –Models –Textures –…–…–…–… Perform Some Game Calculation Perform Some Game Calculation Perform Geometry Calculation Perform Geometry Calculation –LOD Perform AI “Thinking” Perform AI “Thinking” Perform Collision Detection Perform Collision Detection Perform the 3D Rendering Perform the 3D Rendering …

70 70 Jobs By Request (Typically) Mouse Input Mouse Input –Press/release the mouse button –Drag –Double-click –Move Keyboard Input Keyboard Input –Hotkey –Typing Gamepad Gamepad –Same as the hotkey Network Network System System …

71 71 TheFly3D Game Engine - The Main Program

72 72 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); } The Main Program

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

74 74 /*----------------------------------------- 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; } Mouse Callback

75 75 //---------------------------------------------------------------------------------------- // 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(); } Timer Callback

76 76 What will be included in TheFly3D Hardware 3D Graphics API2D APIInput DeviceOS API 3D Scene Mngmt2D SpriteGamepadNetworkAudio TerrainCollisionCharacterUIDynamicsSound FX Combat SystemFX SystemGame AIScript System NPC SystemVirtual AgentTrading SystemStory Game Game Play Layer Engine Layer System Layer Under Development

77 77 Introduction to TheFly3D 3D Graphics/Game Programming Library 3D Graphics/Game Programming Library –Using C++ Based on DirectX9.0a Based on DirectX9.0a –Currently A Frame Work for 3D Graphics Developers A Frame Work for 3D Graphics Developers According to the Experiences of the Author Some Game Development Features are added According to the Experiences of the Author Some Game Development Features are added Scene Management System Scene Management System –Scene tree Built-in Visibility Culling Built-in Visibility Culling Characters Characters Current version 0.5a1 (1009, 2004) Current version 0.5a1 (1009, 2004) –Win32 version

78 78 Development Environment.net2003 Visual C++ 7.1.net2003 Visual C++ 7.1 DirectX9.0a SDK DirectX9.0a SDK Include files Include files –TheFly.h –TheFlyWin32.h (win32 version + D3D) Linked libraries Linked libraries –TheFlyLibD_05a1.lib –d3d9.lib d3dx9.lib

79 79 Create the Visual C++ Project for “TheFly3D” Create folders for TheFly3D API Create folders for TheFly3D API –…\include –…\lib New a Win32 Application Project New a Win32 Application Project Set the additional include/library directories to TheFly3D API Set the additional include/library directories to TheFly3D API Add DirectX 9.0 include/lib to additional search directories Add DirectX 9.0 include/lib to additional search directories Add d3d9.lib d3dx9.lib to additional dependencies Add d3d9.lib d3dx9.lib to additional dependencies Add TheFly.h, TheFlyWin32.h, TheFlyLibD_xxxx.lib into the project Add TheFly.h, TheFlyWin32.h, TheFlyLibD_xxxx.lib into the project

80 80 The 1 st TheFly3D Program – hello.cpp Create a 3D world Create a 3D world Create a viewport Create a viewport Create a scene Create a scene Create 3D entities Create 3D entities –A camera –A teapot model –A light source Translate the camera to show the model Translate the camera to show the model Bind callbacks to make the program interactive Bind callbacks to make the program interactive

81 81 Demo - Hello Do it!

82 82 The Basics to Write TheFly3D Program All Win32 code is transparent here All Win32 code is transparent here –void main(int argc, char *argv[]) ID & Function class ID & Function class –TheFly3D creates the objects for you »Return the ID –TheFly3D owns the objects –You have the right to handle the objects »Use function classes // create a viewport vID = gw.CreateViewport(ox, oy, ww, hh); FnViewport vp; vp.Object(vID); vp.SetBackgroundColor(0.3f, 0.3f, 0.0f);

83 83 Initialize TheFly3D The 1 st function to use TheFly3D is The 1 st function to use TheFly3D is –FyWin32CreateWorld() After the calling successfully, you can get the non-zero ID of a world object After the calling successfully, you can get the non-zero ID of a world object Assign the ID to a world function for manipulating the world object Assign the ID to a world function for manipulating the world object // create a new world WORLDid gID = FyWin32CreateWorld(Tester", 0, 0, 800, 600, 16, FALSE); FnWorld gw; gw.Object(gID); gw.SetEnvironmentLighting(0.5f, 0.5f, 0.8f); gw.SetTexturePath("Data\\textures");

84 84 3D Graphics Layers A World Frame Buffers (front + back) The World in TheFly3D A world is a set of layers where the 3D objects acts on A world is a set of layers where the 3D objects acts on

85 85 Camera 3D Models Lights Backdrop Board A 3D Scene The 3D Grpahics Layer A 3D graphics layer is a projection of the rendering of a 3D world A 3D graphics layer is a projection of the rendering of a 3D world The 3D world we call the “Scene” The 3D world we call the “Scene” The place for projection we call the “Viewport” The place for projection we call the “Viewport”

86 86 The Viewports & Scenes TheFly3D supports the multiple viewports TheFly3D supports the multiple viewports A scene can be rendered on different viewports A scene can be rendered on different viewports Viewports & scenes are created by the world object Viewports & scenes are created by the world object // create a new world WORLDid 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);

87 87 Something about the Scene A scene is not a “real” 3D object, just a “set” of 3D objects A scene is not a “real” 3D object, just a “set” of 3D objects A scene provides multiple rendering group concept to handle the 1 st priority sorting for the rendering of 3D objects A scene provides multiple rendering group concept to handle the 1 st priority sorting for the rendering of 3D objects

88 88 The 3D Entities – Objects A 3D scene is constructed by a set of “objects” which are the basic entities in a scene. 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 cameras, lights, 3D models, audio sources, billboards, etc., An object is a carrier to carry real 3D data including cameras, lights, 3D models, audio sources, billboards, etc., Models Models Cameras Cameras Lights Lights Terrains Terrains Objects are created/deleted by his/her host scene Objects are created/deleted by his/her host scene Objects can be switched between scenes Objects can be switched between scenes

89 89 The Objects Can … Can have shapes (geometric data) Can have shapes (geometric data) Can be grouped (hierarchy) Can be grouped (hierarchy) Can move (transformation) Can move (transformation) Can look alike (clone or data sharing) Can look alike (clone or data sharing) Can perform (animation, deformation) Can perform (animation, deformation) Can be affected (lighted, listened) Can be affected (lighted, listened) Can be changed (modification) Can be changed (modification)

90 90 Etc Parameters Geometric Data Shape Transformation Move Parent Object Hierarchy Motion Data Animation Clone A Scene Object

91 91 A Model An object to carry a set of geometry data is a model object An object to carry a set of geometry data is a model object You can load the model data from files You can load the model data from files TheFly3D loads.cw3 &.lz3 TheFly3D loads.cw3 &.lz3 // create 3D entities nID = scene.CreateModel(ROOT); FnModel model; model.Object(nID); // load a teapot model.Load("Teapot.cw3");

92 92 A tree-based representation Simplified scene graph Root TheFly3D Scene Tree

93 93 A tree-based representation Simplified scene graph TheFly3D Scene Tree Is Simplified Scene Graph Root

94 94 nID = scene.CreateModel(ROOT); cID = scene.CreateCamera(ROOT); FnModel model; model.Object(nID); model.SetParent(cID); nID cID Object Hierarchy

95 95 MODELid nID = scene.CreateModel(ROOT); FnModel model; model.Object(nID); MODELid nID1 = model.Instance(); Clone a Model Object nIDnID1 Data instance

96 96 TheFly3D Model Functions (1/2) void model.SetParent(parent_object_ID); void model.SetParent(parent_object_ID); void model.Show(be_show); void model.Show(be_show); void model.SetOpacity(opacity); void model.SetOpacity(opacity); opacity = 0.0 – 1.0 opacity = 0.0 – 1.0 void model.SetRenderMode(mode); void model.SetRenderMode(mode); mode = WIREFRAME or TEXTURE mode = WIREFRAME or TEXTURE MODELid clonedID = Model.Instance(); MODELid clonedID = Model.Instance(); void model.ChangeScene(sID); void model.ChangeScene(sID); BOOL model.Load(char *file); BOOL model.Load(char *file); Load a.cw3 or.lz3 model file to a model object Load a.cw3 or.lz3 model file to a model object void model.ShowBoundingBox(beShow); void model.ShowBoundingBox(beShow);

97 97 TheFly3D Model Functions (2/2) Model.SetRenderOption(item, value); Model.SetRenderOption(item, value); (item, value) = (item, value) = (Z_BUFFER, TRUE/FALSE) (Z_BUFFER, TRUE/FALSE) (Z_BUFFER_WRITE, TRUE/FALSE) (Z_BUFFER_WRITE, TRUE/FALSE) (ALPHA, TRUE/FALSE) (ALPHA, TRUE/FALSE) Add/remove the model to/from alpha sorting list Add/remove the model to/from alpha sorting list (FOG, TRUE/FALSE) (FOG, TRUE/FALSE) (SPECULAR, TRUE/FALSE) (SPECULAR, TRUE/FALSE) (LIGHTING, TRUE/FALSE) (LIGHTING, TRUE/FALSE) (ANTIALIASING, TRUE, FALSE) (ANTIALIASING, TRUE, FALSE) (TEXTURE_ADDRESS_MODE, WRAP_TEXTURE / MIRROR_TEXTURE / CLAMP_TEXTURE / BORDER_TEXTURE / MIRROR_ONCE_TEXTURE) (TEXTURE_ADDRESS_MODE, WRAP_TEXTURE / MIRROR_TEXTURE / CLAMP_TEXTURE / BORDER_TEXTURE / MIRROR_ONCE_TEXTURE) (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 (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 (DESTINATION_BLEND_MODE values are same as the SOURCE_BLEND_MODE

98 98 Coordinate System Every model should have its own local coordinate system (local space) Every model should have its own local coordinate system (local space) The space when it’s modeled The space when it’s modeled To its parent model, it is in the global space To its parent model, it is in the global space The space for reference The space for reference X Y Z x y z

99 99 Rotation matrixTransformation Three basic linear transformations Three basic linear transformations Translate Translate Rotate Rotate Scale Scale Principles Principles Right-handed rule Right-handed rule v’ = v M 0 M 1 v’ = v M 0 M 1 Matrix in 12-element (I call the M12) Matrix in 12-element (I call the M12) x y z a 0 a 1 a 2 0 a 3 a 4 a 5 0 a 6 a 7 a 8 0 a 9 a 10 a 11 1 Translation vector

100 100 x’ = x + dx y’ = y + dy z’ = z + dz model.Translate(dx, dy, dz, op); T = 1 0 0 0 1 0 0 0 1 dx dy dz (dx dy dz)Translation

101 101Rotation model.Rotate(Z_AXIS, 30.0f, op); i.e. rotate with z axis x’ = x cos  – y sin  y’ = x sin  + y cos  z’ = z Rz =Rz = cos  sin  0 -sin  cos  0 0 0 1 0 0 0 x y z

102 102Scaling 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

103 103 Matrix Operations Matrix operation Matrix operation –REPLACE, LOCAL, GLOBAL x y z X Y Z [M][ML][ML][MG][MG] Object Transformation Matrix op = REPLACE op = GLOBAL op = LOCAL

104 104 TheFly3D Model Transformation Functions model.Translate(x, y, z, op); model.Translate(x, y, z, op); op = LOCAL, GLOBAL, or REPLACE op = LOCAL, GLOBAL, or REPLACE model.Rotate(axis, angle, op); model.Rotate(axis, angle, op); op = LOCAL, GLOBAL, or REPLACE op = LOCAL, GLOBAL, or REPLACE axis = X_AXIS, Y_AXIS, Z_AXIS axis = X_AXIS, Y_AXIS, Z_AXIS model.Scale(sx, sy, sz, op); model.Scale(sx, sy, sz, op); op = LOCAL, GLOBAL, or REPLACE op = LOCAL, GLOBAL, or REPLACE model.Quaternion(w, x, y, z, op); model.Quaternion(w, x, y, z, op); w : the scalar part of the quaternion w : the scalar part of the quaternion x, y, z : the vector part of the quaternion x, y, z : the vector part of the quaternion The quaternion should be a unit quaternion The quaternion should be a unit quaternion op = LOCAL, GLOBAL, or REPLACE op = LOCAL, GLOBAL, or REPLACE model.SetMatrix(M12, op); model.SetMatrix(M12, op); M12 : a M12 matrix M12 : a M12 matrix op = LOCAL, GLOBAL, or REPLACE op = LOCAL, GLOBAL, or REPLACE float *model.GetMatrix(); float *model.GetMatrix(); Get the pointer of the model object’s matrix Get the pointer of the model object’s matrix

105 105 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 / right … Move forward Move up Move right Turn right / leftMovements

106 106 x y z z up + facing to -y Facing 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

107 107 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 up Move right Turn right / leftMovements

108 108 new position = old position + distance *(facing direction in unit) Move Forward

109 109 The object has a local coordinate system. Align a local axis of the object with the facing direction Make a translation matrix to move the object align the axis Apply the matrix first before to apply the existing transformations Then the object is moving forward! Move Forward (2) FnModel model; model.Object(nID); model.Translate(0.0f, -dist, 0.0f, LOCAL);

110 110 -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 / An example Turn Right/Left

111 111 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 axis Apply the matrix first before to apply the existing transformations Then the object is turning! Turn Right/Left (2) FnModel model; model.Object(nID); model.Rotate(Z_AXIS, -angle, LOCAL); // turn right

112 112 new position = old position + distance *(facing direction in unit) Move Forward – Graphics Approach

113 113 The object model has a local coordinate system Align a local axis of the object with the facing direction Make a translation matrix to move the object align the axis Apply the matrix first before to apply the existing transformations Then the object is moving forward! Move Forward – Our Approach FnModel model; model.Object(nID); model.Translate(0.0f, -dist, 0.0f, LOCAL);

114 114 -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 / An example Turn Right/Left – Graphics Approach

115 115 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 axis Apply the matrix first before to apply the existing transformations Then the object is turning! Turn Right/Left – Our Approach FnModel model; model.Object(nID); model.Rotate(Z_AXIS, -angle, LOCAL); // turn left

116 116 A terrain is a place for 3D objects to walk on A terrain is a place for 3D objects to walk on A terrain is generated from a model node A terrain is generated from a model node Neighboring triangles are the next searching target for current triangle for terrain following 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 A terrain for terrain following is not the same as the terrain in visualTerrain

117 117 Terrain in TheFly3D // create a terrain object tID = scene.CreateTerrain(); FnTerrain t; t.Object(tID); // load a terrain model (just like a regular model) // but a terrain is invisible in default t.Load("test_terrain_following.cw3"); // generate the neighboring data for terrain following t.GenerateTerrainData();

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

119 119 TheFly3D Model Movement Functions (1/2) void model.GetPosition(pos); void model.GetPosition(pos); pos is a 3D vector to get the position of the model object pos is a 3D vector to get the position of the model object void model.GetDirection(faceDir, upDir); 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 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); void model.SetPosition(pos); The position is related to its parent object The position is related to its parent object void model.SetDirection(faceDIr, upDir); 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 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); Void model.SetDirectionAlignment(fDAxis, uDAxis); You can change the local axes for facing and up directions You can change the local axes for facing and up directions BOOL model.PutOnTerrain(tID, be3D, offset, probeFront, probeBack, probeAngle, hightLimit) BOOL model.PutOnTerrain(tID, be3D, offset, probeFront, probeBack, probeAngle, hightLimit) tID is a terrain object tID is a terrain object be3D = TRUE for 3D terrain following be3D = TRUE for 3D terrain following Offset is the height above the terrain Offset is the height above the terrain hightLimit is the terrain following height limit hightLimit is the terrain following height limit Return TURE if you successfully put the model on a terrain Return TURE if you successfully put the model on a terrain

120 120 Probe for a Model on Terrain probeFront probeBack : terrain following check point

121 121 TheFly3D Model Movement Functions (2/2) int model.MoveForward(dist, beTerrainFollow, be3D, offset); int model.MoveForward(dist, beTerrainFollow, be3D, offset); If you just want to move the model forward but not on terrain, set beTerrainFollow to FALSE 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. You should put a model on terrain first. Then you can move it forward on terrain. void model.TurnRight(float angle); void model.TurnRight(float angle); Angle is in degree Angle is in degree

122 122 Character Introduction

123 123

124 124 Keyframe System 3DS MAX Softimage Maya... Low Cost (Relatively) Easy to Combine Animations Hard to Make “Good” Motions Long Production Time Motion Production – by 3D Tools

125 125 Motion Capture Optical Magnetic... Costly Investment Every Frame is a Keyframe Very Live Motion Need Post-processing for Games Hard to Combine Motions Motion Production – by Motion Capture

126 126 A character is composed by a set of models with motion data to simulate a live creature in real world A character is composed by a set of models with motion data to simulate a live creature in real world A segmented character : A segmented character : head body up arm fore arm hand groin thigh shin foot A Segmented Character

127 127 groin body head thigh_r thigh_l shin_r shin_l foot_r foot_l up_arm_l up_arm_r fore_arm_l fore_arm_r hand_l hand_r Base The scene tree of a segmented character : The scene tree of a segmented character : Benefits Benefits –Hierarchical structure –Easy to implement in a scene tree Drawbacks Drawbacks –Segment-like

128 128 Use root-base structure to construct the character Use root-base structure to construct the character Base Root (groin) Base The Root-Base Concept (1/2)

129 129 A character has some models to be the geometry roots of the character system. 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 plays as the gravity center of the character The root can be translated and rotated The root can be translated and rotated The others are joints The others are joints The joints can rotate only The joints can rotate only A ghost object is added to be the parent of the root, which is the base of the character 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. The base is the center for the character’s movement. We move the base to perform character’s moves. The Root-Base Concept (2/2)

130 130 A set of frames to describe a character’s motion A set of frames to describe a character’s motion Walk, run, attack, … Walk, run, attack, … Keyframed or non-keyframed Keyframed or non-keyframed Motion data in Motion data in –Position (pivot) + quaternion –Position (pivot) + Euler angles –Position (pivot) + (q, n) –Matrix walkrunattackfall Motion Data - Pose

131 131 Load a Character FnScene scene; CHARACTERid actorID; Scene.Object(sceneID); actorID = scene.LoadCharacter("m012.lxx"); actorID = scene.LoadCharacter(“m60a3.lzc”);.lxx is a character description file which is a bone-skin character.lzc is a segmented character file

132 132 Play a Pose BLENDTREEid btID = actor.GetBlendTree(); FnBlendTree bt; bt.Object(btID); aaaID = bt.CreateAnimationNode(2); // 2 nd motion // start to play a pose (1 st time) actor.PlayBlendNode(aaaID, (float) 0, START, TRUE); // continue to play a pose actor.PlayBlendNode(CURRENT_ONE, (float) skip, LOOP, TRUE);

133 133 Make a Character to Move Forward FnCharacter actor; // play walking pose actor.Object(actorID); actor.PlayBlendNode(CURRENT_ONE, (float) skip, LOOP, TRUE); // move it forward actor.MoveForward(dist, beTF, be3D, offset);

134 134 TheFly3D Character Movement Functions (1/2) void actor.GetPosition(pos); void actor.GetPosition(pos); pos is a 3D vector to get the position of the character pos is a 3D vector to get the position of the character void actor.GetDirection(faceDir, upDir); 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 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); void actor.SetPosition(pos); The position is related to its parent object The position is related to its parent object void actor.SetDirection(faceDIr, upDir); 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 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) BOOL actor.PutOnTerrain(tID, be3D, offset, probeFront, probeBack, probeAngle, hightLimit) tID is a terrain object tID is a terrain object be3D = TRUE for 3D terrain following be3D = TRUE for 3D terrain following Offset is the height above the terrain Offset is the height above the terrain hightLimit is the terrain following height limit hightLimit is the terrain following height limit Return TURE if you successfully put the character on a terrain Return TURE if you successfully put the character on a terrain

135 135 TheFly3D Character Movement Functions (2/2) void actor.MoveForward(dist, beTerrainFollow, be3D, offset); void actor.MoveForward(dist, beTerrainFollow, be3D, offset); If you just want to move the character forward but not on terrain, set beTerrainFollow to FALSE If you just want to move the character forward but not on terrain, set beTerrainFollow to FALSE You should put a character on terrain first. Then you can move it forward on terrain. 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 A character is always using his local -y-axis as facing direction void actor.TurnRight(float angle); void actor.TurnRight(float angle); Angle is in degree Angle is in degree A character is always using his local z-axis as up direction A character is always using his local z-axis as up direction

136 136 TheFly3D Character Functions MODELid actor.GetBaseObject(); MODELid actor.GetBaseObject(); You can get the base object of the character You can get the base object of the character MODELid actor.GetObjectByName(name); MODELid actor.GetObjectByName(name); You can get the each model part of the character by its name in character file 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 For a bone-skin character, this function can get the bones in the skeleton BOOL actor.PlayFrame(frame, beIncludeBase); BOOL actor.PlayFrame(frame, beIncludeBase); The function is to play the motion for a specific frame The function is to play the motion for a specific frame Set beIncludeBase = TRUE to play the base object’s motion Set beIncludeBase = TRUE to play the base object’s motion BLENDTREEid actor.GetBlendTree(); BLENDTREEid actor.GetBlendTree(); Get the character’s blend tree data Get the character’s blend tree data You can define all poses in a blend tree (blend nodes) You can define all poses in a blend tree (blend nodes) float actor.PlayBlendNode(btNodeID, skipFrame, mode, beIncludeBase); float actor.PlayBlendNode(btNodeID, skipFrame, mode, beIncludeBase); All poses for a character is defined as a blend node All poses for a character is defined as a blend node Skip frames can be floating-point Skip frames can be floating-point Mode can be ONCE or LOOP! Mode can be ONCE or LOOP!

137 137 Game Mathematics

138 138 Matrices Matrices Vectors Vectors Fixed-point Real Numbers Fixed-point Real Numbers Triangle Mathematics Triangle Mathematics Intersection Issues Intersection Issues Euler Angles Euler Angles Angular Displacement Angular Displacement Quaternion Quaternion Differential Equation Basics Differential Equation Basics Essential Mathematics for Game Development

139 139 Matrix basics Matrix basics –Definition –Transpose –Addition Matrices A = (a ij ) = a 11.. a 1n.. a m1.. a mn C = A T c ij = a ji C = A + B c ij = a ij + b ij

140 140 –Scalar-matrix multiplication –Matrix-matrix multiplication C =  A c ij =  a ij C = A B c ij =  a ik b kj k = 1 r

141 141 Transformations in Matrix form 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

142 142 –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] = [x y z 1] T 1 0 0 0 0 1 0 0 0 0 1 0 T x T y T z 1

143 143 –Scaling Transformation x’ = xS x y’ = yS y z’ = zS z V’ = VS [x ’ y ’ z ’ 1] = [x y z 1] = [x y z 1] S S x 0 0 0 0 S y 0 0 0 0 S z 0 0 0 0 1 Here S x, S y and S z are scaling factors.

144 144 –Rotation Transformations 1 0 0 0 0 cos  sin  0 0 -sin  cos  0 0 0 0 1 R x = R y = R z = cos  0 -sin  0 0 1 0 0 sin  0 cos  0 0 0 0 1 cos  sin  0 0 -sin  cos  0 0 0 0 1 0 0 0 0 1

145 145 –Net Transformation matrix –Matrix multiplication are not commutative [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 M 1 M 2 = M 2 M 1

146 146 A vector is an entity that possesses magnitude and direction. A vector is an entity that possesses magnitude and direction. A 3D vector is a triple : 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. A ray (directed line segment), that possesses position, magnitude and direction.Vectors (x 1,y 1,z 1 ) (x 2,y 2,z 2 ) V = (x 2 -x 1, y 2 -y 1, z 2 -z 1 )

147 147 Addition of vectors Addition of vectors Length of vectors Length 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 V + W V W |V| = (v 1 2 + v 2 2 + v 3 2 ) 1/2 U = V / |V|

148 148 Cross product of vectors Cross product of vectors –Definition –Application »A normal vector to a polygon is calculated from 3 (non-collinear) vertices of the polygon. 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) NpNp V2V2 V1V1 polygon defined by 4 points N p = V 1 X V 2

149 149 »Normal vector transformation N(X) = detJ J -1T N(x) where X = F(x) J the Jacobian matrix, J i (x) =  F(x) xixi (take scaling as example) "Global and Local Deformations of Solid Primitives" Alan H. Barr Computer Graphics Volume 18, Number 3 July 1984

150 150 Dot product of vectors Dot product of vectors –Definition –Application |X| = V. W = v 1 w 1 + v 2 w 2 + v 3 w 3  V W cos  = V. WV. W |V||W|

151 151 Fixed Point Arithmetics : N bits (signed) Integer Fixed Point Arithmetics : N bits (signed) Integer –Example : N = 16 gives range –32768  ă  32767 –We can use fixed scale to get the decimals Fixed Point Arithmetics (1/2) a = ă / 2 8 111 8 integer bits 8 fractional bits ă = 315, a = 1.2305

152 152 Multiplication then Requires Rescaling Multiplication then Requires Rescaling Addition just Like Normal Addition just Like Normal Fixed Point Arithmetics (2/2) e = a. c = ă / 2 8. ĉ / 2 8  ĕ = (ă. ĉ) / 2 8 e = a+c = ă / 2 8 + ĉ / 2 8  ĕ = ă + ĉ

153 153 Compression for Floating-point Real Numbers Compression for Floating-point Real Numbers –4 bits reduced to 2 bits –Lost some accuracy but affordable –Network data transfer Software 3D Rendering Software 3D Rendering Fixed Point Arithmetics - Application

154 154 h haha hbhb hchc AaAa AcAc AbAb h = h a + h b + h 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” AaAa AbAb AcAc A A A p (x a,y a,z a ) (x b,y b,z b ) (x c,y c,z c ) Triangular Coordinate System

155 155 Area of a triangle in 2D x a y a A = ½ x b y b x c y c x a 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 ) Triangle Area – 2D (x a,y a,z a ) (x b,y b,z b ) (x c,y c,z c )

156 156 Area of a triangle in 3D A = ½ (N. Sum(P i1 cross P i2 )) where (i1, i2) = (a,b), (b,c), (c,a) Triangle Area – 3D 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; }

157 157 Terrain Following Terrain Following Hit Test Hit Test Ray Cast Ray Cast Collision Detection Collision Detection Triangular Coordinate System - Application

158 158 Ray Cast Ray Cast Containment Test Containment TestIntersection

159 159 Ray Cast – The Ray x = x 0 + (x 1 – x 0 ) t y = y 0 + (y 1 – y 0 ) t, t = 0, z = z 0 + (z 1 – z 0 ) t { Shot a Ray to Calculate the Intersection of the Ray with Models Shot a Ray to Calculate the Intersection of the Ray with Models Use Parametric Equation for a Ray Use Parametric Equation for a Ray 8 When t = 0, the Ray is on the Start Point (x 0,y 0,z 0 ) When t = 0, the Ray is on the Start Point (x 0,y 0,z 0 ) Only the t  0 is the Answer Candidate Only the t  0 is the Answer Candidate The Smallest Positive t is the Answer The Smallest Positive t is the Answer

160 160 Ray Cast – The Plane Each Triangle in the Models has its Plane Equation Each Triangle in the Models has its Plane Equation Use ax + by + cz + d = 0 as the Plane Equation Use ax + by + cz + d = 0 as the Plane Equation (a, b, c) is the Plane Normal Vector (a, b, c) is the Plane Normal Vector |d| is the Distance of the Plane to Origin |d| is the Distance of the Plane to Origin Substitute the Ray Equation into the Plane Substitute the Ray Equation into the Plane Solve the t to Find the Intersect Solve the t to Find the Intersect Check the Intersect Point Within the Triangle or not by Using “Triangle Area Test” (p. 154) Check the Intersect Point Within the Triangle or not by Using “Triangle Area Test” (p. 154)

161 161 Intersection = 1, inside Intersection = 2, outside Intersection = 0, outside Trick : Parametric equation for a ray which is parallel to the x-axis x = x 0 + t y = y 0, t = 0, { 8 (x 0, y 0 ) 2D Containment Test “if the No. of intersection is odd, the point is inside, otherwise, is outside”

162 162 3D Containment Test “if the No. of intersection is odd, the point is inside, otherwise, is outside” Same as the 2D containment test Same as the 2D containment test

163 163 A rotation is described as a sequence of rotations about three mutually orthogonal coordinates axes fixed in space A rotation is described as a sequence of rotations about three mutually orthogonal coordinates axes fixed in space –X-roll, Y-roll, Z-roll There are 6 possible ways to define a rotation There are 6 possible ways to define a rotation –3! R(  1,  2,   ) represents an x-roll, followed by y-roll, followed by z-roll R(  1,  2,    c 2 c 3 c 2 s 3 -s 2 0 s 1 s 2 c 3 -c 1 s 3 s 1 s 2 s 3 +c 1 c 3 s 1 c 2 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 where s i = sin  i and c i = cos  i Euler Angles

164 164 Interpolation happening on each angle Interpolation happening on each angle Multiple routes for interpolation Multiple routes for interpolation More keys for constrains More keys for constrains z x y R z x y R Euler Angles & Interpolation

165 165 R( , n), n is the rotation axis R( , n), n is the rotation axis n rRr  n r rvrv rhrh V  rvrv V Rr v r h = (n. r)n r v = r - (n. r)n, rotate into position Rr v V = nxr v = nxr Rr v = (cos  )r v + (sin  )V -> Rr = Rr h + Rr v = r h + (cos  )r v + (sin  )V = (n. r)n + (cos  )  r - (n. r)n) + (sin  ) nxr = (cos  )r + (1-cos  ) n (n. r) + (sin  ) nxr Angular Displacement

166 166 Sir William Hamilton (1843) Sir William Hamilton (1843) From Complex numbers (a + ib), i 2 = -1 From Complex numbers (a + ib), i 2 = -1 16,October, 1843, Broome Bridge in Dublin 16,October, 1843, Broome Bridge in Dublin 1 real + 3 imaginary = 1 quaternion 1 real + 3 imaginary = 1 quaternion q = a + bi + cj + dk q = a + bi + cj + dk i 2 = j 2 = k 2 = -1 i 2 = j 2 = k 2 = -1 ij = k & ji = -k, cyclic permutation i-j-k-i ij = k & ji = -k, cyclic permutation i-j-k-i q = (s, v), where (s, v) = s + v x i + v y j + v z k q = (s, v), where (s, v) = s + v x i + v y j + v z kQuaternion

167 167 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 ) 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) Noncommutative Quaternion Algebra

168 168 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 , sin  n), |n| = 1 R q (p) = (0, (cos 2  - sin 2  )r + 2sin 2  n(n. r) + 2cos  sin  nxr) = (0, cos2  r + (1 - cos2  )n(n. r) + sin2  nxr) Conclusion : The act of rotating a vector r by an angular displacement ( , n) is the same as taking this displacement, ‘lifting’ it into quaternion space, by using a unit quaternion (cos(  /2), sin(  /2)n) Quaternion VS Angular Displacement

169 169 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 q = (w,x,y,z) Quaternion VS Rotation Matrix

170 170 M 0 M 1 M 2 0 M 3 M 4 M 5 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]; }

171 171 Spherical linear interpolation, slerp Spherical linear interpolation, slerp A B P  t slerp(q 1, q 2, t) = q 1 + q 2 sin((1 - t)  ) sin  sin(t  ) Quaternion Interpolation

172 172 Initial value problems Initial value problems ODE ODE –Ordinary differential equation Numerical solutions Numerical solutions –Euler’s method –The midpoint method Differential Equation Basics

173 173 An ODE An ODE Vector field Vector field Solutions Solutions –Symbolic solution –Numerical solution x = f (x, t) wheref is a known function x is the state of the system, x is the x’s time derivative x & x are vectors x(t 0 ) = x 0, initial condition. Start here Follow the vectors … Initial Value Problems..

174 174 A numerical solution A numerical solution –A simplification from Tayler series Discrete time steps starting with initial value Discrete time steps starting with initial value Simple but not accurate Simple but not accurate –Bigger steps, bigger errors –O(  t 2 ) errors Can be unstable Can be unstable Not even efficient Not even efficient x(t +  t) = x(t) +  t f(x, t) Euler’s Method

175 175 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  x =  t f(x, t) b. Evaluate f at the midpoint f mid = f((x+  x)/2, (t+  t)/2) c. Take a step using the midpoint x(t+  t) = x(t) +  t f mid... a b c Error term The Midpoint Method

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

177 177 Dynamics Dynamics –Particle system Game FX System Game FX System Initial Value Problems - Application

178 178 Game Geometry

179 179 Geometry Geometry –Position / vertex normals / texture coordinates Topology Topology –Primitive »Lines / triangles / surfaces / … Property Property –Materials –Textures Motion Motion Hierarchy Hierarchy Game Models

180 180 Vertex position Vertex position –(x, y, z, w) –In model space or screen spane Vertex normal Vertex normal –(n x, n y, n z ) Vertex color Vertex color –(r, g, b) or (diffuse, specular) Texture coordinates on vertex Texture coordinates on vertex –(u 1, v 1 ), (u 2, v 2 ), … Skin weights Skin weights –(bone 1, w 1, bone 2, w 2, …) Geometry Data

181 181 Lines Lines –Line segments –Polyline »Open / closed Indexed triangles Indexed triangles Triangle Strips / Fans Triangle Strips / Fans Surfaces Surfaces –Non-uniform Rational B Spline (NURBS) Subdivision Subdivision Topology Data

182 182 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, v r, v g, v b, t u, t v ) Topology Face v 0 v 3 v 6 v 7 Edge table v0v0 v3v3 v6v6 v7v7 Right-hand rule for indexing polygon normal vertex normal Indexed Triangles

183 183 v0v0 v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 T0T0 T1T1 T2T2 T3T3 T4T4 T5T5 v 0, v 1, v 2, v 3, v 4, v 5, v 6, v 7 Triangle Strips


Download ppt "1 3D Game Programming Using TheFly3D ©"

Similar presentations


Ads by Google