Game Programming Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp2 Building a Flexible Camera Class September 01, 2005 Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1 http://www.hallym.ac.kr/~sunkim/teach/2005/gp2 DirectX SDK & Sample Codes
29
Embed
Building a Flexible Camera Class - hallym.ac.krgraphics.hallym.ac.kr/teach/2005/gp2/src/01camera.pdf · Sun-Jeong Kim sunkim/teach/2005/gp2sunkim/teach/2005/gp1 File ÆNew Sun-Jeong
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Game Programming
Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp2
Building a Flexible Camera Class
September 01, 2005
Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1http://www.hallym.ac.kr/~sunkim/teach/2005/gp2
DirectX SDK & Sample Codes
Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1http://www.hallym.ac.kr/~sunkim/teach/2005/gp2
File New
Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1http://www.hallym.ac.kr/~sunkim/teach/2005/gp2
Copy Sample Files
Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1http://www.hallym.ac.kr/~sunkim/teach/2005/gp2
Project Add To Project
Files
Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1http://www.hallym.ac.kr/~sunkim/teach/2005/gp2
FileView
Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1http://www.hallym.ac.kr/~sunkim/teach/2005/gp2
Project Settings
Link
Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1http://www.hallym.ac.kr/~sunkim/teach/2005/gp2
Tool Options
Directories
Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1http://www.hallym.ac.kr/~sunkim/teach/2005/gp2
Sample: Camera
Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1http://www.hallym.ac.kr/~sunkim/teach/2005/gp2
Control Keys
W / S – walk forward / backward
A / D – Strafe left / right
R / F – Fly up / down
Up / Down arrow keys – Pitch
Left / Right arrow keys – Yaw
N / M – Roll
Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1http://www.hallym.ac.kr/~sunkim/teach/2005/gp2
Analysis of Sample: Camera
cameraApp.cpp
d3dUtility.cpp
d3dUtility.h
InitD3D();
Setup();
EnterMsgLoop();
Cleanup();
WndProc();
Display();
WinMain();
WndProc();
Setup();
Cleanup();
Display();
camera.h
camera.cpp
InitD3D();EnterMsgLoop();DrawBasicScene();
Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1http://www.hallym.ac.kr/~sunkim/teach/2005/gp2
Analysis of Sample: Camera
cameraApp.cpp
d3dUtility.cpp
d3dUtility.h
InitD3D();
Setup();
EnterMsgLoop();
Cleanup();
WndProc();
Display();
WinMain();
WndProc();
Setup();
Cleanup();
Display();
camera.h
camera.cpp
InitD3D();EnterMsgLoop();DrawBasicScene();
Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1http://www.hallym.ac.kr/~sunkim/teach/2005/gp2
WinMain( )
Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1http://www.hallym.ac.kr/~sunkim/teach/2005/gp2
Analysis of Sample: Camera
cameraApp.cpp
d3dUtility.cpp
d3dUtility.h
InitD3D();
Setup();
EnterMsgLoop();
Cleanup();
WndProc();
Display();
WinMain();
WndProc();
Setup();
Cleanup();
Display();
camera.h
camera.cpp
InitD3D();EnterMsgLoop();DrawBasicScene();
Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1http://www.hallym.ac.kr/~sunkim/teach/2005/gp2
Creating the Main Window
Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1http://www.hallym.ac.kr/~sunkim/teach/2005/gp2
Initializing Direct3D (Step 1)
Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1http://www.hallym.ac.kr/~sunkim/teach/2005/gp2
Initializing Direct3D (Step 2,3)
Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1http://www.hallym.ac.kr/~sunkim/teach/2005/gp2
Initializing Direct3D (Step 4)
Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1http://www.hallym.ac.kr/~sunkim/teach/2005/gp2
Analysis of Sample: Camera
cameraApp.cpp
d3dUtility.cpp
d3dUtility.h
InitD3D();
Setup();
EnterMsgLoop();
Cleanup();
WndProc();
Display();
WinMain();
WndProc();
Setup();
Cleanup();
Display();
camera.h
camera.cpp
InitD3D();EnterMsgLoop();DrawBasicScene();
Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1http://www.hallym.ac.kr/~sunkim/teach/2005/gp2
Setup ( ) – Projection
Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1http://www.hallym.ac.kr/~sunkim/teach/2005/gp2
Analysis of Sample: Camera
cameraApp.cpp
d3dUtility.cpp
d3dUtility.h
InitD3D();
Setup();
EnterMsgLoop();
Cleanup();
WndProc();
Display();
WinMain();
WndProc();
Setup();
Cleanup();
Display();
camera.h
camera.cpp
InitD3D();EnterMsgLoop();DrawBasicScene();
Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1http://www.hallym.ac.kr/~sunkim/teach/2005/gp2
Creating the Buffers
Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1http://www.hallym.ac.kr/~sunkim/teach/2005/gp2
Accessing the Buffers
Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1http://www.hallym.ac.kr/~sunkim/teach/2005/gp2
Setting Rendering States
Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1http://www.hallym.ac.kr/~sunkim/teach/2005/gp2
Rendering Background
Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1http://www.hallym.ac.kr/~sunkim/teach/2005/gp2
Analysis of Sample: Camera
cameraApp.cpp
d3dUtility.cpp
d3dUtility.h
InitD3D();
Setup();
EnterMsgLoop();
Cleanup();
WndProc();
Display();
WinMain();
WndProc();
Setup();
Cleanup();
Display();
camera.h
camera.cpp
InitD3D();EnterMsgLoop();DrawBasicScene();
Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1http://www.hallym.ac.kr/~sunkim/teach/2005/gp2
EnterMsgLoop ( )
Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1http://www.hallym.ac.kr/~sunkim/teach/2005/gp2
Analysis of Sample: Camera
cameraApp.cpp
d3dUtility.cpp
d3dUtility.h
InitD3D();
Setup();
EnterMsgLoop();
Cleanup();
WndProc();
Display();
WinMain();
WndProc();
Setup();
Cleanup();
Display();
camera.h
camera.cpp
InitD3D();EnterMsgLoop();DrawBasicScene();
Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1http://www.hallym.ac.kr/~sunkim/teach/2005/gp2
WndProc ( )
Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1http://www.hallym.ac.kr/~sunkim/teach/2005/gp2
Analysis of Sample: Camera
cameraApp.cpp
d3dUtility.cpp
d3dUtility.h
InitD3D();
Setup();
EnterMsgLoop();
Cleanup();
WndProc();
Display();
WinMain();
WndProc();
Setup();
Cleanup();
Display();
camera.h
camera.cpp
InitD3D();EnterMsgLoop();DrawBasicScene();
Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1http://www.hallym.ac.kr/~sunkim/teach/2005/gp2
Handling Keyboard Events
Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1http://www.hallym.ac.kr/~sunkim/teach/2005/gp2
Camera & Rendering Scene
Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1http://www.hallym.ac.kr/~sunkim/teach/2005/gp2
Analysis of Sample: Camera
cameraApp.cpp
d3dUtility.cpp
d3dUtility.h
InitD3D();
Setup();
EnterMsgLoop();
Cleanup();
WndProc();
Display();
WinMain();
WndProc();
Setup();
Cleanup();
Display();
camera.h
camera.cpp
InitD3D();EnterMsgLoop();DrawBasicScene();
Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1http://www.hallym.ac.kr/~sunkim/teach/2005/gp2
Cleanup ( )
Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1http://www.hallym.ac.kr/~sunkim/teach/2005/gp2
Analysis of Sample: Camera
cameraApp.cpp
d3dUtility.cpp
d3dUtility.h
InitD3D();
Setup();
EnterMsgLoop();
Cleanup();
WndProc();
Display();
WinMain();
WndProc();
Setup();
Cleanup();
Display();
camera.h
camera.cpp
InitD3D();EnterMsgLoop();DrawBasicScene();
Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1http://www.hallym.ac.kr/~sunkim/teach/2005/gp2
Objective
To learn how to implement a flexible Cameraclass
suitable for light simulators games played from first-person perspective
D3DXMatrixLookAtLH( ) function
[+] positioning and aiming a camera in a fixed position
[−] moving camera that reacts to user input
Motivation
Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1http://www.hallym.ac.kr/~sunkim/teach/2005/gp2
Camera Design (1)
Four camera vectorsposition and orientation of the camera
right, up, look, and position vectors
(R, U, L): orientation vectors
orthonormal vectors
orthogonal matrix
inverse == transpose
World Space
PR
UL
P = Position vectorR = Right vectorU = Up vectorL = Look vector
Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1http://www.hallym.ac.kr/~sunkim/teach/2005/gp2
Camera Design (2)
Six operations – six degrees of freedomrotate around the right vector (pitch)
rotate around the up vector (yaw)
rotate around the look vector (roll)
strafe along the right vector
fly along the up vector
move along look vector
Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1http://www.hallym.ac.kr/~sunkim/teach/2005/gp2