beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

50
Beginning Direct3D Game Programming: Mathematics 6 Transforms [email protected] Division of Digital Contents, DongSeo University. April 2016

Upload: jintaek-seo

Post on 13-Feb-2017

38 views

Category:

Engineering


0 download

TRANSCRIPT

Page 1: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

Beginning Direct3D Game Programming:Mathematics 6

[email protected]

Division of Digital Contents, DongSeo University.April 2016

Page 2: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

2D Rotation: Do you remember the answer?

2

Page 3: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

We can rewrite this in matrix form as follows.

3

Page 4: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

Solution for this in different way When a point P is rotated about θ, we assume that axis

is rotated about θ. We assume that rotated x and y-axis about θ is x' and y'-

axis.

4

Now, the new point P' is a point on (cosθ, sinθ) x-axis, and (-sinθ, cosθ) y-axis.

So, p'=px(cosθ, sinθ)+py(-sinθ, cosθ)

Page 5: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

Caution When a vector P is in the left side of the matrix, the ma-

trix must be transposed.

5

Page 6: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

2D Transformations 2D Translation

2×3 augmented matrix

6

Page 7: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

We can add a dummy linear equation.

It constructs the linear system of 3 linear equations.

Now, we get:

7

Page 8: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

2D Rotation Transform

2D Scaling Transform

8

Page 9: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

Linear Transformations Suppose that we have established a 3D coordinate sys-

tem C consisting of an origin and three coordinate axes, in which a point P has the coordinates x, y, z .

This constitutes a linear transformation from C to C′ and can be written in matrix form as follows

9

Page 10: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

Assuming the transformation is invertible, the linear transformation from C′ to C is given by

We need to combine the 3×3 matrix and translation vector T into a single 4× 4 transformation matrix.

10

Page 11: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

Orthogonal Matrices Most 3×3 matrices arising in computer graphics applica-

tions are orthogonal. An orthogonal matrix is simply one whose inverse is equal to its transpose.

If the n × n matrix M is orthogonal, then M preserves lengths and angles.

11

Page 12: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

Handedness In three dimensions, a basis for a coordinate system

given by the 3D vectors V1, V2, and V3 possesses a property called handedness.

A left-handed basis is one for which (V1×V2)⋅V3 < 0.– in LHS V1×V2=-V3

• Left-handed coordinates on the leftRight-handed coordinates on the right.

12

Page 13: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

Cross Product

13

The cross product of two three-dimensional vectors, also known as the vector product, returns a new vec-tor that is perpendicular to both of the vectors being multiplied together.

• The cross-product in respect to a left-handed coordi-nate system.

Page 14: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

Coordinate Systems in Direct3D

• Direct3D uses a left-handed coordinate system. If you are porting an application that is based on a right-handed coordinate system, you must make two changes to the data passed to Direct3D.

14

Page 15: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

Scaling Transforms To scale a vector P by a factor of a, we simply calculate

P′ = aP. In three dimensions, this operation can also be expressed as the matrix product.

15

Page 16: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

Nonuniform Scaling A Diagonal entries are not necessarily all equal.

16

Page 17: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

Rotation Transforms We can find 3×3 matrices that rotate a coordinate sys-

tem through an angle θ about the x, y, or z axis..

17

Page 18: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

2D Rotation: Do you remember the answer?

18

Page 19: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

We can rewrite this in matrix form as follows.

The matrix Rz(θ ) that performs a rotation through the angle θ about the z axis is thus given by

19

Page 20: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

Solution for this in different way When a point P is rotated about θ, we assume that axis

is rotated about θ. We assume that rotated x and y-axis about θ is x' and y'-

axis.

20

Now, the new point P' is a point on (cosθ, sinθ) x-axis, and (-sinθ, cosθ) y-axis.

So, p'=px(cosθ, sinθ)+py(-sinθ, cosθ)

Page 21: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

Caution When a vector P is in the left side of the matrix, the ma-

trix must be transposed.

21

Page 22: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

Similarly, we can derive the following 3×3 matrices Rx(θ ) and Ry(θ ) that perform rotations through an angle θ about the x and y axes, respectively:

22

Page 23: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

Rotation about an Arbitrary Axis

Combining these terms and setting c = cosθ and s = sinθ gives us the following formula for the matrix RA(θ) that rotates a vector through an angle θ about the axis A.

23

Page 24: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

Translation To transform a point P from one coordinate system to

another, we usually find ourselves performing the opera-tion

P′ =MP + T where M is some invertible 3×3 matrix and T is a 3D

translation vector. Fortunately, there is a compact and elegant way to rep-

resent these transforms within a single mathematical entity.

24

Page 25: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

Translation P′ =MP + T Fortunately, there is a compact and elegant way to rep-

resent these transforms within a single mathematical entity.

25

Page 26: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

26

P =M-1P' – M-1T We would therefore expect the inverse of the 4× 4 ma-

trix F to be

Page 27: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

27

Page 28: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

When a input vector is at left of a matrix. When a input vector is at left of a matrix, the translation

vector T will be located at bottom row.

28

Page 29: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

Points and Directions Unlike points, direction vectors should remain invariant

under translation. To transform direction vectors using the same 4× 4

transformation matrices that we use to transform points, we extend direction vectors to four dimensions by set-ting the w coordinate to 0.

(x,y,z,w) (px,py,pz,0)

The upper left 3×3 portion of the matrix to affect the di-rection vector.

29

Page 30: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

Linear Systems Matrices provide a compact and convenient way to rep-

resent systems of linear equations. For instance, the lin-ear system given by the equations

can be represented in matrix form as

30

Page 31: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

The matrix preceding the vector x, y, z of unknowns is called the coefficient matrix, and the column vector on the right side of the equals sign is called the con-stant vector. Linear systems for which the constant vector is nonzero (like the example above) are called nonhomogeneous.

Linear systems for which every entry of the constant vector is zero are called homogeneous.– The Geometric meaning of homogeneous is all the 3-planes

meet at origin (0,0,0).31

Page 32: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

The augmented matrix formed by concatenating the coefficient matrix and constant vector is

When the linear system is homogeneous, then the ma-trix cab be written like this

32

Page 33: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

Homogeneous Matrix We will use 4x4 matrices to consistently represent

translation, scaling and rotation in 3D space.

In that case a14, a24 and a34 is always zero, when the in-put vector is at left of a matrix, this is a Homogeneous Matrix.

33

Page 34: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

Transform Matrices in Direct3D You can transform any point (x,y,z) into another point

(x', y', z') by using a 4x4 matrix, as shown in the follow-ing equation.

34

Page 35: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

35

Translation in Direct3D The following equation translates the point (x, y, z) to a

new point (x', y', z').

D3DXMATRIX Translate(const float dx, const float dy, const float dz) { D3DXMATRIX ret;

D3DXMatrixIdentity(&ret); ret(3, 0) = dx; ret(3, 1) = dy; ret(3, 2) = dz; return ret;} // End of Translate

Page 36: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

Scaling in Direct3D The following equation scales the point (x, y, z) by arbi-

trary values in the x-, y-, and z-directions to a new point (x', y', z').

36

Page 37: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

Rotations in Direct3D The following equation rotates the point (x, y, z) around

the x-axis, producing a new point (x', y', z').

37

D3DXMATRIX* WINAPI D3DXMatrixRotationX ( D3DXMATRIX *pOut, float angle ){ float sin, cos; sincosf(angle, &sin, &cos); // Determine sin and cos of angle

pOut->_11 = 1.0f; pOut->_12 = 0.0f; pOut->_13 = 0.0f; pOut->_14 = 0.0f; pOut->_21 = 0.0f; pOut->_22 = cos; pOut->_23 = sin; pOut->_24 = 0.0f; pOut->_31 = 0.0f; pOut->_32 = -sin; pOut->_33 = cos; pOut->_34 = 0.0f; pOut->_41 = 0.0f; pOut->_42 = 0.0f; pOut->_43 = 0.0f; pOut->_44 = 1.0f;

return pOut;}

Page 38: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

The following equation rotates the point around the y-axis.

The following equation rotates the point around the z-axis.

38

Page 39: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

Concatenating Matrices One advantage of using matrices is that you can com-

bine the effects of two or more matrices by multiplying them.

This means that, to rotate a model and then translate it to some location, you don't need to apply two matrices.

In this equation, C is the composite matrix being cre-ated, and M₁ through Mn are the individual matrices.

Use the D3DXMatrixMultiply function to perform matrix multiplication.

39

Page 40: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

Win32 3D Cube Project

40

Page 41: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

Step2

41

Page 42: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

42

Page 43: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

43

Page 44: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

Step3: Matrix Rotation about x-axis

44

Page 45: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

Projection Transform

45

Page 46: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

46

Page 47: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

47

Page 48: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

48

Page 49: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks

References Lengyel, "Mathematics for 3D Game Programming and

Computer Graphics",3rd, 2011 https://msdn.microsoft.com/en-us/library/windows/deskt

op/bb206269(v=vs.85).aspx

49

Page 50: Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks