Presentation is loading. Please wait.

Presentation is loading. Please wait.

3D Game Programming Projection

Similar presentations


Presentation on theme: "3D Game Programming Projection"— Presentation transcript:

1 3D Game Programming Projection
Ming-Te Chi Department of Computer Science, National Chengchi University 2018

2 Projection

3 Multi-view 一般的建模軟體都提供多重視角的畫面,方便理解與建立3D model
從左上順時鐘方向依序為,正面正交投影、下方正交投影,相機視點透視投影和側面視點正交投影

4 Classical Projections
幾種工程繪圖中常見的透視方法 Angel: Interactive Computer Graphics 5E ©

5 Eye coordinates +y +y +x +x +z 由於成像是會依相機(觀察者)的位置和角度進行繪圖,
故整體來說程式碼由下而上需經歷從object space轉換成world space再轉到view space (camera space或Eye coordinates) 之後再經投影,rasterization

6 Projections Orthographic Projections Perspective Projections
以一個中空的立方體模型為例,相同相機觀察角度 在正交投影中,看不到中空內側的部份 但在透視投影中,距離越遠長度差距越小的透視觀點,便可觀察到中空內側表面的部份

7 Pinhole camera A pinhole camera is a simple camera without a lens and with a single small aperture – effectively a light-proof box with a small hole in one side. 電腦圖學通常以Pinhole camera(針孔成像原理),呈現出3D場景、鏡頭和成像平面間的關係

8 Frustum void glFrustum( );
GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble nearVal, GLdouble farVal ); far near Frustum 平面截頭體 可視為由相機為頂點,far plane為底面的大金字塔,截去以near plane為底面的小金字塔 這個形狀描述了針孔成像透視頭影的視野範圍(view volume)

9 Perspective Projections
// Reset coordinate system glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Produce the perspective projection gluPerspective(45.0f, fAspect, 1.0, 400.0); void gluPerspective( GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar); Frustum的參數設定較不直覺 gluPerspective提供了更方便的方式,讓開發者使用,內部再轉成Frustum的參數 Fovy 視角的大小,一般會設在30~60度之間 Aspect 長寬的比例,可根據視窗或viewport的長寬比例調整

10 攝影機使用與設定 Main Camera: 遊戲預設視角 點擊場景中的攝影機,可顯示預覽畫面

11 攝影機使用與設定 攝影機屬性 Background: 背景顏色 Projection:投影方式 Field of View(FOV)
Perspective: 透視投影 Orthographic: 正交投影 Field of View(FOV) 可見視角範圍 Clipping Planes 最近(Near)與最遠(Far)能看到的範圍

12 Viewport transformation
Modelview space Clipping space Viewport transformation Viewport 定義了Clipping space到視窗位置的轉換關係 一般而言,若只有一個viewport,會將viewport設定與視窗一樣大小,並根據viewport的長寬比例,調整投影的apsect ( gluPerspective()裡的長寬比例 ) 另外,一個視窗,可以由多個viewport對視窗進行分割顯示,每個viewport再對應到一個world space windows space

13 graphical perspective

14 First-Person perspective
A graphical perspective rendered from the viewpoint of the player character Mirror's Edge superhot doom Mirror's Edge

15 Second-person perspective
In second person point of view, the action is shot from the perspective of a character that is not the protagonist. Let players feel as if they are actually the character they are controlling mario 64 Mario 64

16 Third-person shooter The player character is seen at a distance from a number of different possible perspective angles. the player character is visible on-screen, and the gameplay consists primarily of shooting. mario 64 Gear of war 3

17 Top-down perspective From God view Star craft 2 simcity
Star craft 2 simcity

18 Animation Squash & stretch
The Prinicples of Animation, Squash and Stretch For hw2 Squash & stretch

19 Cameras and Actors

20 CameraFollow public GameObject player; private Vector3 offset; void start() { offset = transform.position –player.transform.position; } void LateUpdate() { transform.position = player.transform.position + offset;

21 Vector3.Lerp public static Vector3 Lerp(Vector3 a, Vector3 b, float t); 𝑜𝑢𝑡= 1−𝑡 ∗𝑎+𝑡∗𝑏

22 CameraFollow2 public GameObject player; public float smooth; private Vector3 offset; void start() { offset = transform.position –player.transform.position; } void LateUpdate() { Vector3 targetPos = player.transform.position + offset; transform.position = Vector3.Lerp (transform.position, targetPos, smooth*Time.deltaTime);

23 Lookat public void LookAt(Vector3 worldPosition); public void LookAt(Vector3 worldPosition, Vector3 worldUp = Vector3.up); +x +y +z up from void Update() { // Point the object at the world origin transform.LookAt(Vector3.zero); }

24 An Actor Frame vForward Z vUp Y vLocation vRight X

25 Move object Translate public void Translate(Vector3 translation, Space relativeTo = Space.Self); public void Translate(float x, float y, float z, Space relativeTo = Space.Self); vForward Z vUp Y vLocation vRight X 對於3D空間的物件和相機,我們可以透過Frame座標系統描述他在空間中的位置和方向 也就是空間座標(x, y, z)和三軸的向量(前方、上方和側邊), 這頁提供了一個GLFrame的C++ class,完整的實作可參見實習課的程式範例 由於三個軸是正交垂直,因此在class設計可只設定up和forward兩向量,用外積計算側邊向量 也可以透過這個物件本身座標系統來移動物體,沿著forward vector前進,是比較直覺的作法, 特別在遊戲中,上下左右的移動輸入,應該對應於物體本身的方向,而非對應世界座標軸的方向

26 Euler Angles vUp Y vForward Z vRight
public void Rotate(Vector3 eulers, Space relativeTo = Space.Self); public void Rotate(float xAngle, float yAngle, float zAngle, Space relativeTo = Space.Self); vRight X 另外,物體的旋轉可以Euler Angles定義,就如同飛機飛行移動的三個旋轉方向 藉由Frame的定義對三軸做旋轉


Download ppt "3D Game Programming Projection"

Similar presentations


Ads by Google