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

3D Game Programming Geometric Transformations

Embed Size (px)

DESCRIPTION

3D Game Programming Geometric Transformations. Ming- Te Chi Department of Computer Science, National Chengchi University. Outline. Geometric Transformations ( 4ed ch4 ) ( 5 th ch4 ) Basic transformation The coordinates Hierarchy transformation. Transformation Terminology. Viewing - PowerPoint PPT Presentation

Citation preview

Page 1: 3D Game Programming Geometric  Transformations

3D Game ProgrammingGeometric

TransformationsMing-Te Chi

Department of Computer Science,National Chengchi University

Page 2: 3D Game Programming Geometric  Transformations

Outline

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

Page 3: 3D Game Programming Geometric  Transformations

Transformation Terminology

ViewingModelingModelviewProjectionViewport

Page 4: 3D Game Programming Geometric  Transformations

Transformations

Translation

Rotation

Scaling

Page 5: 3D Game Programming Geometric  Transformations

Rotation/Translationfrom world to object

+y

+x

+y

+x

+y

+x

+y

+x

+y

+x

+y

+x

Rotate()

Translate()

Rect()

Translate()

Rotate()

Rect()

Page 6: 3D Game Programming Geometric  Transformations

The Modelview Duality

+x

+y

+z

+x

+y

+z

View moving Model moving

Page 7: 3D Game Programming Geometric  Transformations

Projection

Orthographic Perspective

World space

Page 8: 3D Game Programming Geometric  Transformations

Matrix/vector

963

852

741

4

3

2

1

161284

151173

141062

13951

Page 9: 3D Game Programming Geometric  Transformations

Transformation functions

glTranslatef(tx, ty, tz);

glRotatef(angle, x, y, z);

glScalef(sx, sy, sz);

glLoadIdentity();

1000

0100

0010

0001

1000

000

000

000

sz

sy

sx

1000

100

010

001

tz

ty

tx

Page 10: 3D Game Programming Geometric  Transformations

TransformationPipeline

other calculations here– material è color– shade model (flat)– polygon rendering mode– polygon culling– clipping

10

vertex

ModelviewMatrix

ProjectionMatrix

PerspectiveDivision

ViewportTransform

Modelview

Modelview

Projection

lll

object eye clip normalizeddevice

window

CPUCPU

DLDL

Poly.Poly. Per

Vertex

PerVertex

RasterRaster

FragFrag

FBFB

PixelPixel

TextureTexture

Page 11: 3D Game Programming Geometric  Transformations

The Life of a vertex

Image by Philp Rideout

Page 12: 3D Game Programming Geometric  Transformations

Image by Philp Rideout

Page 13: 3D Game Programming Geometric  Transformations

Geometric Objects in glut

glutWireCube(10.0f);

glutSolidCube(10.0f);

Sphere, Cube, Cone, Torus, Dodecahedron, Octahedron, Tetrahedron, Icosahedron and Teapot

Page 14: 3D Game Programming Geometric  Transformations

Solid

Wire

glutWireSphere(GLdouble radius, GLint slices, GLint stacks);

glutSolidCone(GLdouble base, GLdouble height, GLint slices, GLint stacks);

glutWireTorus(GLdouble innerRadius, GLdouble outerRadius, GLint nsides, GLint rings);

Page 15: 3D Game Programming Geometric  Transformations

glPolygonMode

Select a polygon rasterization modevoid glPolygonMode(GLenum face, GLenum mode);

• face: • GL_FRONT, GL_BACK, GL_FRONT_AND_BACK

•mode: • GL_POINT, GL_LINE, and GL_FILL

Page 16: 3D Game Programming Geometric  Transformations

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 ); draw();

Page 17: 3D Game Programming Geometric  Transformations

Identity MatrixglTranslatef(0, 1, 0);

glutSolidSphere(1, 15, 15);

glTranslatef(1, 0, 0);

glutSolidSphere(1, 15, 15);

glTranslatef(0, 1, 0);

glutSolidSphere(1, 15, 15);

glLoadIdentity();

glTranslatef(1, 0, 0);

glutSolidSphere(1, 15, 15);

Page 18: 3D Game Programming Geometric  Transformations

The Matrix Stacks

Matrix Stack

glPushMatrix() glPopMatrix()

glGet(GL_MAX_MODELVIEW_STACK_DEPTH, &size)

Current matrix(modelview/projection)

Page 19: 3D Game Programming Geometric  Transformations

Atom example// First Electron Orbit// Save viewing transformationglPushMatrix();// Rotate by angle of revolutionglRotatef(fElect1, 0.0f, 1.0f, 0.0f);// Translate out from origin to orbit distanceglTranslatef(90.0f, 0.0f, 0.0f);// Draw the electronglutSolidSphere(6.0f, 15, 15); // Restore the viewing transformationglPopMatrix();

….//(2) Second Electron Orbit

Page 20: 3D Game Programming Geometric  Transformations

…// (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();

Page 21: 3D Game Programming Geometric  Transformations

21

Transformation Example 1

Page 22: 3D Game Programming Geometric  Transformations

22

Transformation Example 2

Page 23: 3D Game Programming Geometric  Transformations

23

Transformation Example 2

Page 24: 3D Game Programming Geometric  Transformations

Matrix in Modern OpenGL

OpenGL Mathematics– A C++ mathematics library for graphics

programming– http://glm.g-truc.net/

Page 25: 3D Game Programming Geometric  Transformations

#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));}

Page 26: 3D Game Programming Geometric  Transformations

MESH FORMAT

Page 27: 3D Game Programming Geometric  Transformations

Representing a Mesh

Consider a mesh

There are 8 nodes and 12 edges– 5 interior polygons– 6 interior (shared) edgesEach vertex has a location vi = (xi yi zi)

27

v1 v2

v7

v6v8

v5

v4

v3

e1

e8

e3

e2

e11

e6

e7

e10

e5

e4

e9

e12

Page 28: 3D Game Programming Geometric  Transformations

3D model format

SIMPLE Trianglevertex1_X  vertex1_Y  vertex1_Z  normal1_X  normal1_Y normal1_Zvertex2_X  vertex2_Y  vertex2_Z  normal2_X  normal2_Y normal2_Zvertex3_X  vertex3_Y  vertex3_Z  normal3_X  normal3_Y normal3_Z COLOR

Trianglefrontcolor_R  frontcolor_G  frontcolor_B  backcolor_R  backcolor_G  backcolor_Bvertex1_X  vertex1_Y  vertex1_Z  normal1_X  normal1_Y normal1_Zvertex2_X  vertex2_Y  vertex2_Z  normal2_X  normal2_Y normal2_Zvertex3_X  vertex3_Y  vertex3_Z  normal3_X  normal3_Y normal3_Z

Page 29: 3D Game Programming Geometric  Transformations

Simple Representation

Define each polygon by the geometric locations of its verticesLeads to OpenGL code such as

Inefficient and unstructured– Consider moving a vertex to a new location– Must search for all occurrences

29

glBegin(GL_POLYGON); glVertex3f(x1, x1, x1); glVertex3f(x6, x6, x6); glVertex3f(x7, x7, x7);glEnd();

Page 30: 3D Game Programming Geometric  Transformations

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

30

Page 31: 3D Game Programming Geometric  Transformations

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)

Page 33: 3D Game Programming Geometric  Transformations

Scene Graph

COLLADA

FBX

Alembic

Page 34: 3D Game Programming Geometric  Transformations
Page 35: 3D Game Programming Geometric  Transformations

Rotation/Translationfrom object to world+y

+x

+y

+x

+y

+x

+y

+x

+y

+x

+y

+x

Rotate()

Translate()

Rect()

Translate()

Rotate()

Rect()