3D Game Programming Geometric Transformations

Slides:



Advertisements
Similar presentations
OpenGL Computer Graphics
Advertisements

SI23 Introduction to Computer Graphics
1Computer Graphics Building Models John Shearer Culture Lab – space 2
Computer Graphics 2D & 3D Transformation.
COMP 175 | COMPUTER GRAPHICS Remco Chang1/6103b – Shapes Lecture 03b: Shapes COMP 175: Computer Graphics February 3, 2015.
OPENGL.
Hierarchical Transformation. Summary Alternatively, OpenGL thinks: A transformation updates the coordinate system. For each change, the transformation.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Building Models modified by Ray Wisman Ed Angel Professor of Computer Science,
1 Building Models. 2 Objectives Introduce simple data structures for building polygonal models ­Vertex lists ­Edge lists OpenGL vertex arrays.
Using GLU/GLUT Objects GLU/GLUT provides very simple object primitives glutWireCube glutWireCone gluCylinder glutWireTeapot.
Graphics Pipeline.
2 COEN Computer Graphics I Evening’s Goals n Discuss the fundamentals of lighting in computer graphics n Analyze OpenGL’s lighting model n Show.
CS 4363/6353 BASIC RENDERING. THE GRAPHICS PIPELINE OVERVIEW Vertex Processing Coordinate transformations Compute color for each vertex Clipping and Primitive.
Computer Graphic Creator: Mohsen Asghari Session 2 Fall 2014.
CS 4363/6353 INTRODUCTION TO COMPUTER GRAPHICS. WHAT YOU’LL SEE Interactive 3D computer graphics Real-time 2D, but mostly 3D OpenGL C/C++ (if you don’t.
CHAPTER 4 Geometric Transformations: The Pipeline Vivian by Richard S. Wright Jr.
Drawing 3D Scenes in OpenGL We want to transform objects in order to orient and position them as desired in a 3D scene. OpenGL provides the necessary functions.
OpenGL (II). How to Draw a 3-D object on Screen?
1 Lecture 5 Rendering 3D graphical primitives. 2 3D Rendering Example:
3D Rendering with JOGL Introduction to Java OpenGL Graphic Library By Ricardo Veguilla
3D Transformation. In 3D, we have x, y, and z. We will continue use column vectors:. Homogenous systems:. 3D Transformation glVertex3f(x, y,z);
CS 4731: Computer Graphics Lecture 9: Introduction to 3D Modeling Emmanuel Agu.
CS 470 Introduction to Computer Graphics Basic 3D in OpenGL.
Computer Graphics: Programming, Problem Solving, and Visual Communication Steve Cunningham California State University Stanislaus and Grinnell College.
Geometric Objects and Transformations Chapter 4. Points, Scalars and Vectors  Points - position in space  Scalars - real numbers, complex numbers obey.
UniS CS297 Graphics with Java and OpenGL Viewing, the model view matrix.
1 OpenGL Basics A Graphics Standard ©Mel Slater, Anthony Steed
3D Objects Subject:T0934 / Multimedia Programming Foundation Session:12 Tahun:2009 Versi:1/0.
Chapter 4 10 February Agenda Program 2 – Due 2/17 Chapter 4 – transformations GLUT solids.
TWO DIMENSIONAL GEOMETRIC TRANSFORMATIONS CA 302 Computer Graphics and Visual Programming Aydın Öztürk
Image Synthesis Rabie A. Ramadan, PhD 2. 2 Java OpenGL Using JOGL: Using JOGL: Wiki: You can download JOGL from.
Transformations With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin.
Introduction to OpenGL 1. 2 OpenGL A Graphics rendering API introduced in 1992 by Silicon Graphics Inc Provide the low-level functions to access graphics.
Ch 2 Graphics Programming page 1 CSC 367 Coordinate Systems (2.1.2) Device coordinates, or screen coordinates (pixels) put limitations on programmers and.
1 Computer Graphics Week9 -3D Geometric Transformation.
Computer Graphics Bing-Yu Chen National Taiwan University.
Representation. Objectives Introduce concepts such as dimension and basis Introduce coordinate systems for representing vectors spaces and frames for.
16/5/ :47 UML Computer Graphics Conceptual Model Application Model Application Program Graphics System Output Devices Input Devices API Function.
OpenGL: Introduction Yanci Zhang Game Programming Practice.
Project 5 Lamp Shader Fri, Nov 7, 2003 Due Mon, Nov 17, 2003.
Image Synthesis Rabie A. Ramadan, PhD 4. 2 Review Questions Q1: What are the two principal tasks required to create an image of a three-dimensional scene?
1 Building Models Ed Angel Professor Emeritus of Computer Science University of New Mexico Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley.
2 COEN Computer Graphics I Evening’s Goals n Discuss viewing and modeling transformations n Describe matrix stacks and their uses n Show basic geometric.
Viewing and Transformation. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture.
Review on Graphics Basics. Outline Polygon rendering pipeline Affine transformations Projective transformations Lighting and shading From vertices to.
Chapters 5 2 March Classical & Computer Viewing Same elements –objects –viewer –projectors –projection plane.
OpenGL: The Open Graphics Language Introduction By Ricardo Veguilla.
Chap 3 Viewing and Transformation
What are shaders? In the field of computer graphics, a shader is a computer program that runs on the graphics processing unit(GPU) and is used to do shading.
Learning Objectives Affine transformations Affine transformations Translation Translation Rotation Rotation Scaling Scaling Reflection Reflection Shear.
Computer Graphics I, Fall 2010 Building Models.
Revision Sheet Computer Graphics and Human Interaction By Dr. HANY ELSALAMONY1.
Coordinate Systems Lecture 20 Wed, Oct 15, Object Coordinates Each object has its own “local” coordinate system, called object coordinates. Normally.
CSC Graphics Programming Budditha Hettige Department of Statistics and Computer Science.
4. Geometric Objects and Transformations
OpenGL: The Open Graphics Language Technology and Historical Overview By Ricardo Veguilla.
Introduction to Computer Graphics
Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009
School of Computer Science
Transformations Introduction to Computer Graphics and Animation
Camera Position (5.6) we specify the position and orientation of the camera to determine what will be seen. use gluLookAt (eye x, y, z, at x, y, z, up.
Reference1. [OpenGL course slides by Rasmus Stenholt]
Modeling 101 For the moment assume that all geometry consists of points, lines and faces Line: A segment between two endpoints Face: A planar area bounded.
Building Models Ed Angel
Building Models Ed Angel Professor Emeritus of Computer Science
Isaac Gang University of Mary Hardin-Baylor
Angel: Interactive Computer Graphics5E © Addison-Wesley 2009
The Modelview Matrix Lecture 8 Mon, Sep 10, 2007.
Chapter 4 15 March 2006 EventPro Strategies is looking for a part-time programmer (any language) who knows SQL. For more info, contact Ryan Taylor,
Presentation transcript:

3D Game Programming Geometric Transformations Ming-Te Chi Department of Computer Science, National Chengchi University 2014

Outline Geometric Transformations (4ed ch4) (5th ch4) Basic transformation The coordinates Hierarchy transformation

Transformation Terminology Viewing Modeling Modelview Projection Viewport

Transformations Translation Rotation Scaling

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的變化

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

Matrix/vector 在繪圖中常需要利用矩陣和向量處理 各式的affine transformation可以4x4的矩陣表示 向量可表示位置(x, y, z, w)

Transformation functions glTranslatef(tx, ty, tz); glRotatef(angle, x, y, z); glScalef(sx, sy, sz); glLoadIdentity(); 本頁列出基本幾何轉換的函式呼叫和對應的矩陣 由於旋轉矩陣是支援任意軸旋轉,也就是以給定(x, y, z)為旋轉軸,依右手定則的方向,旋轉angle角度 他對應的矩陣相對複雜,因此不列出。有興趣可參考 http://www.opengl.org/sdk/docs/man/xhtml/glRotate.xml

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

Geometric Objects in glut glutWireCube(10.0f); glutSolidCube(10.0f); Sphere, Cube, Cone, Torus, Dodecahedron, Octahedron, Tetrahedron, Icosahedron and Teapot http://www.opengl.org/documentation/specs/glut/spec3/node80.html#SECTION000120000000000000000 GLUT提供多種幾何物體的function,詳細功能可參考網頁的說明

GLdouble radius, GLint slices, GLint stacks); glutWireTorus( glutSolidCone(GLdouble base, GLdouble height, GLint slices, GLint stacks); Solid Wire 各種物體有不同的參數, 如球體需設定半徑radius,另由於這些幾何物體多是先用二次曲面方程式表示,opengl繪圖是以三角面或四角面為主 故需再經過tesslization進行分割,也就是指定slices和stacks的數字,可以想做橫向和縱向的切割 數字越大,形狀會越平滑,但也會帶來比較大的計算負擔 glutWireSphere( GLdouble radius, GLint slices, GLint stacks); glutWireTorus( GLdouble innerRadius, GLdouble outerRadius, GLint nsides, GLint rings);

glPolygonMode Select a polygon rasterization mode void glPolygonMode(GLenum face, GLenum mode); face: GL_FRONT, GL_BACK, GL_FRONT_AND_BACK mode: GL_POINT, GL_LINE, and GL_FILL

Wireframe with hidden-line removal glEnable(GL_POLYGON_OFFSET_FILL); glPolygonOffset( param1, param2 ); glPolygonMode(GL_FRONT , GL_FILL); draw(); glDisable(GL_POLYGON_OFFSET_FILL); glPolygonMode(GL_FRONT, GL_LINE );

Identity Matrix glTranslatef(0, 1, 0); glutSolidSphere(1, 15, 15); glTranslatef(1, 0, 0); glTranslatef(0, 1, 0); glutSolidSphere(1, 15, 15); glLoadIdentity(); glTranslatef(1, 0, 0); glLoadIdentity(); 的作用是將current transformation matrix設成單位矩陣 也等於是歸零之前的幾何轉換的作用,因此右下角的glutSolidSphere(1, 15, 15); 只受glTranslatef(1, 0, 0); 的影響

The Matrix Stacks glGet(GL_MAX_MODELVIEW_STACK_DEPTH, &size) Current matrix(modelview/projection) glPushMatrix() glPopMatrix() OpenGL維護了一個matrix stack(通常長度為32,可用glGet()查詢 ) 利用glPushMatrix() 可儲存目前的幾何轉換狀態 反之用glPopMatrix() 可還原之前儲存的狀態 Matrix Stack glGet(GL_MAX_MODELVIEW_STACK_DEPTH, &size)

Atom example // First Electron Orbit // Save viewing transformation glPushMatrix(); // Rotate by angle of revolution glRotatef(fElect1, 0.0f, 1.0f, 0.0f); // Translate out from origin to orbit distance glTranslatef(90.0f, 0.0f, 0.0f); // Draw the electron glutSolidSphere(6.0f, 15, 15); // Restore the viewing transformation glPopMatrix(); …. //(2) Second Electron Orbit 這個範例以原子和三個電子提供了push/pop機制的使用,以及rotation和translation的組合 每個電子glutSolidSphere(),相對原子做公轉,公轉可透過隨時間增加的變數fElect1,產生旋轉,再以原子和電子的距離進行位移 另外為了避免三個電子各自的公轉影響到對方,會使用push/pop隔離公轉的位移

// Third Electron Orbit … // (2) Second Electron Orbit glPushMatrix(); glRotatef(45.0f, 0.0f, 0.0f, 1.0f); glRotatef(fElect1, 0.0f, 1.0f, 0.0f); glTranslatef(-70.0f, 0.0f, 0.0f); glutSolidSphere(6.0f, 15, 15); glPopMatrix(); // Third Electron Orbit glPushMatrix(); glRotatef(360-45.0f, 0.0f, 0.0f, 1.0f); glRotatef(fElect1, 0.0f, 1.0f, 0.0f); glTranslatef(0.0f, 0.0f, 60.0f); glutSolidSphere(6.0f, 15, 15); glPopMatrix();

Transformation Example 1

Transformation Example 2 複雜的場景也可以利用同樣的機制,決定物體的相對位置

Transformation Example 2

Matrix in Modern OpenGL OpenGL Mathematics A C++ mathematics library for graphics programming http://glm.g-truc.net/

#include <glm/vec3. hpp> // glm::vec3 #include <glm/vec4 #include <glm/vec3.hpp> // glm::vec3 #include <glm/vec4.hpp> // glm::vec4, glm::ivec4 #include <glm/mat4x4.hpp> // glm::mat4 #include <glm/gtc/matrix_transform.hpp> // glm::translate, glm::rotate, glm::scale, glm::perspective #include <glm/gtc/type_ptr.hpp> // glm::value_ptr void func(GLuint LocationMVP, float Translate, glm::vec2 const & Rotate) { glm::mat4 Projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.f); glm::mat4 ViewTranslate = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -Translate)); glm::mat4 ViewRotateX = glm::rotate(ViewTranslate, Rotate.y, glm::vec3(-1.0f, 0.0f, 0.0f)); glm::mat4 View = glm::rotate(ViewRotateX, Rotate.x, glm::vec3(0.0f, 1.0f, 0.0f)); glm::mat4 Model = glm::scale(glm::mat4(1.0f), glm::vec3(0.5f)); glm::mat4 MVP = Projection * View * Model; glUniformMatrix4fv(LocationMVP, 1, GL_FALSE, glm::value_ptr(MVP)); }

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的座標轉換動作