3D Game Programming Geometric Transformations Ming-Te Chi Department of Computer Science, National Chengchi University 2018
Outline Geometric Transformations Basic transformation The coordinates Hierarchy transformation
Transformation Terminology Viewing Modeling Modelview Projection Viewport
Transformations Translation Rotation Scaling
The Modelview Duality +y +y +x +x +z +z View moving Model moving Model space: 為物體相對世界座標的幾何轉換 View space: 為相機相對於世界座標的幾何轉換 兩者互為相對移動的關係,也就是移動相機等同於反方相移動所有的物體,是故會整合成modelview space,減少矩陣的變數 +z View moving Model moving
Projection World space Perspective Orthographic 在此列舉常見的兩種投影法 上圖是世界座標的觀點,同樣的場景 但在不同的投影法中,成像的結果也不一樣 左下為正交投影,右下為透視投影 Perspective Orthographic
Coordinate System Unity is a Left-Handed Coordinate System
Transform
Matrix/vector 在繪圖中常需要利用矩陣和向量處理 各式的affine transformation可以4x4的矩陣表示 向量可表示位置(x, y, z, w)
Transform Translate(tx, ty, tz); Rotate(…); Scale(sx, sy, sz); glLoadIdentity(); 本頁列出基本幾何轉換的函式呼叫和對應的矩陣 由於旋轉矩陣是支援任意軸旋轉,也就是以給定(x, y, z)為旋轉軸,依右手定則的方向,旋轉angle角度 他對應的矩陣相對複雜,因此不列出。有興趣可參考 http://www.opengl.org/sdk/docs/man/xhtml/glRotate.xml
Rotate public void Rotate(Vector3 eulerAngles, Space relativeTo = Space.Self); public void Rotate(Vector3 axis, float angle, Space relativeTo = Space.Self); public void RotateAround(Vector3 point, Vector3 axis, float angle);
Rotation/Translation from world to object Translate() Rotate() Rect() +y +x +y +x +y +x +y +x +y +x +y +x Rotate() Translate() Rect() 反之,由C/C++指令由上至下閱讀的順序 表示從world space往object space的變化
Transformation Pipeline An Interactive Introduction to OpenGL Programming Transformation Pipeline CPU DL Poly. Per Vertex Raster Frag FB Pixel Texture object eye normalized device clip window v e r t x Modelview Matrix Projection Perspective Division Viewport Transform l other calculations here material è color shade model (flat) polygon rendering mode polygon culling clipping Modelview matrix是記錄從object space到eye space間的頂點轉換 Projection matrix是記錄從eye space到clip space間的頂點投影轉換 一般來說以stack的方式存在,以便儲存和回覆特定時間的矩陣狀態,stack的深度主要是看硬體實作而定,Modelview matrix一般至少有32,Projection matrix一般至少有2 另外material-to-color, flat-shading, and clipping calculations 會發生在Modelview matrix和Projection matrix之間 The polygon culling and rendering mode operations 發生在Viewport operations之後 此外,還有texture matrix stack,用來計算貼圖座標的自動投影
The Life of a vertex Image by Philp Rideout 這兩頁的圖片生動地呈現各種不同轉換發生的順序和對應的畫面變化 上下兩列可達到同樣的目的,主要是呈現model和view兩個矩陣轉換,兩者互為相對移動的關係,也就是移動相機等同於反方相移動所有的物體, 是故會整合成modelview space,減少矩陣的變數 Image by Philp Rideout
Image by Philp Rideout
Transformation Example 1
Transformation Example 2 複雜的場景也可以利用同樣的機制,決定物體的相對位置
Transformation Example 2
Mesh Format
Representing a Mesh 5 interior polygons 6 interior (shared) edges v1 v2 v7 v6 v8 v5 v4 v3 e1 e8 e3 e2 e11 e6 e7 e10 e5 e4 e9 e12 Consider a mesh There are 8 nodes and 12 edges 5 interior polygons 6 interior (shared) edges Each vertex has a location vi = (xi yi zi)
3D model format SIMPLE COLOR Triangle vertex1_X vertex1_Y vertex1_Z normal1_X normal1_Y normal1_Z vertex2_X vertex2_Y vertex2_Z normal2_X normal2_Y normal2_Z vertex3_X vertex3_Y vertex3_Z normal3_X normal3_Y normal3_Z COLOR Triangle frontcolor_R frontcolor_G frontcolor_B backcolor_R backcolor_G backcolor_B vertex1_X vertex1_Y vertex1_Z normal1_X normal1_Y normal1_Z vertex2_X vertex2_Y vertex2_Z normal2_X normal2_Y normal2_Z vertex3_X vertex3_Y vertex3_Z normal3_X normal3_Y normal3_Z http://graphics.csie.ntu.edu.tw/~ming/courses/icg/models.html
Simple Representation Define each polygon by the geometric locations of its vertices Leads to OpenGL code such as Inefficient and unstructured Consider moving a vertex to a new location Must search for all occurrences glBegin(GL_POLYGON); glVertex3f(x1, x1, x1); glVertex3f(x6, x6, x6); glVertex3f(x7, x7, x7); glEnd();
Inward and Outward Facing Polygons The order {v1, v6, v7} and {v6, v7, v1} are equivalent in that the same polygon will be rendered by OpenGL but the order {v1, v7, v6} is different The first two describe outwardly facing polygons Use the right-hand rule = counter-clockwise encirclement of outward-pointing normal OpenGL can treat inward and outward facing polygons differently
Wavefront obj format #example obj file v -1.63326156 -3.04798102 -8.81131839 …. vn 0.00379090 0.40057179 0.01256634 … vt 0.22390614 0.97395277 (texture) … f 4/2/4 3/1/3 2/2/2 (index to v/t/n)
Reference Obj format http://www.martinreddy.net/gfx/3d/OBJ.spec Ply format STL format - http://en.wikipedia.org/wiki/STL_(file_format)
Scene Graph COLLADA FBX Alembic
Rotation/Translation from object to world Rotate() Translate() Rect() +y +x +y +x +y +x Translate() Rotate() Rect() +y +x +y +x +y +x 每增加一個幾何轉換指令,可視為該指令前後座標系統的相對改變 本頁投影片示範,由最下方指令往上,依序產生的座標軸變化,也就是從object space往world space的座標轉換動作