83
CIS 350 – I Game Programming Instructor: Rolf Lakaemper

CIS 350 – I Game Programming Instructor: Rolf Lakaemper

  • Upload
    yoko

  • View
    21

  • Download
    0

Embed Size (px)

DESCRIPTION

CIS 350 – I Game Programming Instructor: Rolf Lakaemper. 3D Basics. What ?. In this lecture we will learn how to use 3D transformations to change the position of 3d vertices. This enables us to create animations and views of different perspective. 3D Basics. Basic 3D Mathematics. - PowerPoint PPT Presentation

Citation preview

Page 1: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

CIS 350 – I

Game Programming

Instructor: Rolf Lakaemper

Page 2: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

3DBasics

Page 3: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

What ?

In this lecture we will learn howto use 3D transformations to

change the position of 3d vertices.

This enables us to create animations and views of different

perspective.

Page 4: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

3D Basics

Basic 3D Mathematics

Page 5: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

3D Basics

Everything we describe in our 3D worlds, e.g. vertices to describe objects, speed of objects, forces on objects, will be defined by

3D VECTORS

i.e. triplets of 3 real values.

Page 6: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

3D Basics

3D Euclidean Coordinate System(or 3D Cartesian Coordinate System)

X

y

z

V = (x, y, z)

Page 7: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

3D Basics

A vector v=(x,y,z) has the properties

• DIRECTION, the relative values of x,y,z to each other. Two vectors v,w have the same direction iff:

s: v = sw

• MAGNITUDE (length) |v|. We’ll use the euclidean length:

|v|=sqrt(x²+y²+z²)

Page 8: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

3D Basics

A vector v=(x,y,z) is normalized if

|v| = 1

Normalizing a vector v is easy, just scale it by 1/length:

V v / |v|

i.e. | v/|v| | = 1Proof ?

Page 9: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

3D Basics

It’s usually handy to deal with normalized

vectors.

We’ll see.

Page 10: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

3D Basics

Basic Vector Operations

Vector addition:

v + w = (vx+wx, vy+wy, vz+wz)

v

w v+w

Page 11: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

3D Basics

Basic Vector Operations

Scalar multiplication with s:

s*v = v*s = (s*vx, s*vy, s*vz)

v

s*v•Does not change direction

•Changes magnitude:

|s*v| = s * |v|

Page 12: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

3D Basics

Dot Product

v . w = vx*wx + vy*wy + vz*wz

= |v| * |w| * cos ((v,w))

v

w

w * cos ((v,w))

Page 13: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

3D Basics

• The dot product is a scalar value !• Having 2 vectors v,w it can be

used to determine the angle between v and w:

(v,w) = acos (v.w / (|v| * |w|))

For normalized vectors:

(v,w) = acos (v.w)

Page 14: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

3D Basics

• The dot product determines the INNER angle between v and w

• always 0 <= (v,w) <= 180• v.w > 0 => (v,w) < 90°• v.w < 0 => (v,w) > 90°• in 2D direction of angle can be

determined by

sign(vx*wy – vy*wx)

Page 15: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

3D Basics

The Cross Product

v x w = ( vy*wz – vz*wy, no x

comp.

vx*wz – vz*wx, no y

comp.

vx*wy – vy*wx) no z

comp.

= |v| * |w| * sin((v,w)) * n

with n being a normal vector: n orthogonal to v and w

Page 16: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

3D Basics

• The result of the cross product is a vector !

• (The result of the dot product is a scalar value)

• The cross product gives us the NORMAL vector to the plane defined by v and w. This is an extremely important tool in game programming (lighting, physical modelling,…)

Page 17: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

3D Basics

Basic 3D Transformations

To move/animate objects or to change the camera’s position we

have to transform the vertices defining our objects.

Page 18: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

3D Basics

The basic transformations are

• Scaling• Translation• Rotation

Page 19: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

ScalingComponent wise multiplication with scaling vector s = (sx,sy,sz):

S(v,s) = (sx*vx, sy*vy, sz*vz)

3D Basics

X

y

z

v = (x, y, z)

S(v,s) = (2x,1y, 3z), with s = (2,1,3)

Page 20: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Translation (Shift)Component wise addition with translation vectort = (tx, ty, tz)

T(v,t) = (tx+vx, ty+vy, tz+vz)

3D Basics

X

y

z

v = (x, y, z) t

v + t

Page 21: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

RotationTo make it easier: 2D rotation first:Defined by center c and angle a: R(v,c,a)Rotation around center can be described by

translation and rotation around origin:

v T(v,-c) R(v,0,a) T(v,c)

We therefore only need to define the rotation around the origin, R(v,0,a) =: R(v,a)

3D Basics

Page 22: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Rotation around originThe counterclockwise (=positive angle) 2D rotation

is described by :

R(v,a) = (cos(a)*vx-sin(a)*vy, sin(a)*vx+cos(a)*vy)

3D Basics

Page 23: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Rotation around origin written as matrix:

3D Basics

cos -sin

sin cos

vx

vy

Page 24: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

3D Rotation

Defined by angle, center and rotation axis. As in 2D case, the center can be

translated to origin.

The rotation around an arbitrary axis can be substituted by subsequent rotation

around the main coordinate axes.

3D Basics

Page 25: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

We therefore only need to define the rotation around

the x, y and z axis.

3D Basics

Page 26: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Rotation around x axis:

3D Basics

1 0 0

0 Cos -Sin

0 Sin CosX

y

z

Page 27: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Rotation around y axis:

3D Basics

Cos 0 Sin

0 1 0

-Sin 0 CosX

y

z

Page 28: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Rotation around z axis:

3D Basics

Cos -sin 0

Sin Cos 0

0 0 1X

y

z

Page 29: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

The order of rotation is NOT exchangeable !

e.g.

R(R(v,a1,X),a2,Y) R(R(v,a2,Y),a1,X)

3D Basics

Page 30: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Combinations of transformations can be

described by

Matrix Multiplication

3D Basics

Page 31: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Remember ?

3D Basics

a11 a12

a21 a22

b11 b12

b21 b22* =

Page 32: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Remember ?

3D Basics

a11 a12

a21 a22

b11 b12

b21 b22* =

Page 33: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

With Rx being a rotation matrix around the X axis,Ry being a rotation matrix around the Y axis,

Ry * Rx * vis a rotation of v around X followed by a

rotation around Y.

Since Ry*Rx = Rc is a single 3x3 matrix, the 2 rotations can be written as Rc * c, i.e. a single rotation.

3D Basics

Page 34: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

If we describe all transformations as matrices, the combination of

subsequent transformations can be written as

matrixmultiplication, and, if all parameters are known, as a

SINGLE transformation matrix !

3D Basics

Page 35: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Rotation was already defined as matrix. Here comes scaling:

3D Basics

sx 0 0

0 sy 0

0 0 sz

Page 36: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

What about translation ?

Impossible as 3x3 matrix, since addition not multiplication is involved.

Here comes a nice trick: increase the dimension !

3D Basics

Page 37: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Homogeneous Coordinates

(x,y,z) (x,y,z,1)

3D Basics

a11 a12 a13

a21 a22 a23

a31 a32 a33

a11 a12 a13 0

a21 a22 a23 0

a31 a32 a33 0

0 0 0 1

Page 38: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Now the translation by (tx,ty,tz) can be written as:

3D Basics

1 0 0 tx

0 1 0 ty

0 0 1 tz

0 0 0 1

Page 39: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Homogeneous coordinates allow us to describe ALL

transformations mentioned as matrix multiplications.

Sequences of transformations can hence be described by a

SINGLE 4x4 matrix.

3D Basics

Page 40: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Example Transformations

3D Basics

1 0 0 tx0 1 0 ty0 0 1 tz0 0 0 1

Sx 0 0 00 Sy 0 00 0 Sz 00 0 0 1

cos 0 -sin 00 1 0 0

sin 0 cos 00 0 0 1

translation scaling Rotation (y)

cos 0 -sin tx0 1 0 ty

sin 0 cos tz0 0 0 1

Translation by t andRotation (y) (which one’s first ?)

Page 41: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Projections

Though we handle 3D worlds, they are displayed in 2D by our monitors. We have to project

our 3D world into 2D.

3D Basics

Page 42: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Orthographic Projection(Parallel Projection)

a system of drawing views of an object using perpendicular projectors from the object to a plane of projection…

…or…

3D Basics

Page 43: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Display object by rotation followed by z coordinate

removal

Illustrations taken from http://engineering-ed.org/CAD/documents/Orthographic%20Projection.ppt3D Basics

Page 44: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Parallel lines…

3D Basics

Page 45: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

The six main projections

3D Basics

Page 46: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

The six main projections

3D Basics

Page 47: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

The six main projections

3D Basics

Page 48: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

The projection matrix of the front view is very simple:

3D Basics

1 0 0 00 1 0 00 0 0 00 0 0 1

Page 49: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Perspective Projection

• Produces a view where the object’s size depends on the distance from the viewer

• An object farther away becomes smaller

3D Basics

Page 50: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Perspective Projection

3D Basics

Page 51: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Perspective Projection

3D Basics

Page 52: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

More about the perspective projection and its usage later in

connection with OpenGL

3D Basics

Page 53: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

End of 3D math basics

3D Basics

Page 54: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Coordinate Transformations and

OpenGL Matrices

OpenGL Transformations

Page 55: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

OpenGL Transformations

Rendering 3D scenes, vertices pass through different types of transformations before they

are finally rendered on the screen:

•Viewing transformation: Specifies the location of the camera•Modeling transformation: Moves objects around the scene•Projection transformation: Defines the viewing volume and clipping planes•Viewport transformation: maps the 2D projection of the scene into the rendering window

Page 56: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

OpenGL Transformations

To understand the actual rendering it is helpful to get a view on the vertex

transformation pipeline

Page 57: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

OpenGL Transformations

OpenGL uses 2 different matrices to transform the 3D world into 2d coordinates:

the ModelView Matrixand the

Projection Matrix

Page 58: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

OpenGL Transformations

These matrices are nothing miraculous but simply 4x4 matrices (why 4x4 ?) applied to each and every vertex processed between

glBegin and glEnd.

They define the pose (position and heading) of the camera as well as the object (model) transformations as well as the view volume, i.e. the basic clipping planes and projection.

Page 59: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

OpenGL Transformations

Desired pose of camera and model transformations should/must be put into the

modelview matrix.

Clipping information must be put into the projection matrix.

(After all, these are just matrices, and they can easily be demystified by using your own transformations)

Page 60: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

OpenGL Transformations

There is no special camera matrix in OpenGL. Camera (view) transformations belong into the

ModelView matrix.

Do NOT put them into the projection matrix, since the view information is needed for lighting & fog, taken from the modelView matrix BEFORE the projection matrix is

utilized.

Page 61: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

OpenGL Transformations

For the basic understanding imagine the OpenGL world coordinate system as follows:

X

y

z

camera

Page 62: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

OpenGL Transformations

The camera is placed at (0,0,0) and looks along the negative z axis, y is up.

X

y

z

camera

Page 63: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

OpenGL Transformations

What happens to a vertex that is placed at (0.5, 0.5, -0.5) in the coordinate system ?

X

y

z

camera

Page 64: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

OpenGL Transformations

Answer:

X

y

z

camera

Well, it is placed at (0.5, 0.5, -0.5) in the coordinate system.

Really ?

Page 65: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

OpenGL Transformations

It is not. It undergoes all the transformations defined in the modelView and projection

matrix first, then it is clipped, scaled to the viewport and finally brought up to your

screen.

To model that process it is the easiest to imagine two different views of the 3d world, the EYE coordinate system and the OBJECT

coordinate system.

Page 66: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

OpenGL Transformations

To make it short: the modelview matrix defines the transformation of the object

coordinate system (OCS) with respect to the eye coordinate system (ECS).

What is that ?

Page 67: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

OpenGL Transformations

Again: the modelview matrix defines the transformation of the object coordinate system (OCS) with respect to the eye

coordinate system (ECS).

In the beginning, the modelMatrix is set to the identity (e.g. by glLoadIdentity()) .

So the OCS is identical to the ECS.

Page 68: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

OpenGL Transformations

Red: ECSGreen: OCS

ex

ey

ez

ox

oy

oz

Page 69: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

OpenGL Transformations

The transformation functions change the modelView matrix, as long as glMatrixMode is

set to GL_MODELVIEW. If glTranslatef() is used, the OCS is translated relatively to the

ECS.

ex

ey

ez

Page 70: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

OpenGL Transformations

The transformation affected the OCS, NOT an object itself !

If we draw an object, it is always done with respect to the OCS. Hence translating the

OCS translates the object.

But it’s the easiest to always think in terms of the OCS, not the object !

Page 71: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

OpenGL Transformations

An example:

Draw a square, size 1x1, bottom left corner at the origin.

The result looks like this,if the transformation matrices are set to default values.

Page 72: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

OpenGL Transformations

Now Scale the rectangle by factor ½.

glMatrixMode(GL_MODELVIEW);glLoadIdentity();glScalef(0.5,0.5,0.5);

The result looks like this:

Page 73: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

OpenGL Transformations

If we think about the object itself, we’d say:

‘it’s a 0.5 x 0.5 rectangle’

…so translating it by (-0.5, -0.5, 0) would put the upper right corner into the image center.

It doesn’t.It centered the square.

Page 74: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

OpenGL Transformations

The explanation is simple if we think in terms of the OBJECT COORDINATE SYSTEM:

We didn’t scale the object, we scaled the OCS, i.e. the units on the OCS are now half the units of the ECS.

Page 75: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

OpenGL Transformations

The square is still a 1x1 square, but with respect to the OCS.

Page 76: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

OpenGL Transformations

The translation affects the OCS, with respect to its own units, NOT the ECS’s units !glTranslate(-0.5,-0.5, 0) therefore centers the 1x1 square.

Page 77: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

OpenGL Transformations

If we would rotate the object now, we would in fact rotate the OCS. Around its origin, of course:glRotatef(-90,0.0,0.0,-1.0)

Page 78: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

OpenGL Transformations

…back to the non rotated version. What happens if we rotate the scene by the PROJECTION matrix instead ? Btw.: this is bad openGL style. Rotations etc. belong into the modelView matrix.

Page 79: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

OpenGL Transformations

It is rotated around the ECS origin. The PROJECTION matrix affects the ECS coordinate system. This is especially true for the frustum and ortho definitions defining the view volume.

Page 80: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

OpenGL Transformations

It is rotated around the ECS origin. The PROJECTION matrix affects the ECS

coordinate system. This is especially true for the frustum and ortho parameters defining the

view volume.

Page 81: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

OpenGL Transformations

So what happens to our vertex until it is put onto the screen ?

• first the OCS is modified by the modelView matrix.• the vertex is put into the OCS• the vertex is interpreted in the ECS and not displayed if outside of the view volume• if inside the view volume, it is projected to the front face of the view volume, using the projection matrix

cont’d

Page 82: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

OpenGL Transformations

• the view model’s front face is translated to a 2D with x and y range -1,1. This can cause all kinds of distortions, if the front face is not a square.

Page 83: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

OpenGL Transformations

• finally, the -1,1 square is put into the window on your screen, transformed to the rectangle defined by the viewPort. Once again distortions might occur.

• that’s it.