830 likes | 937 Views
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.
E N D
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
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.
3D Basics 3D Euclidean Coordinate System (or 3D Cartesian Coordinate System) y V = (x, y, z) X z
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²)
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| | = 1 Proof ?
3D Basics It’s usually handy to deal with normalized vectors. We’ll see.
3D Basics Basic Vector Operations Vector addition: v + w = (vx+wx, vy+wy, vz+wz) w v+w v
3D Basics Basic Vector Operations Scalar multiplication with s: s*v = v*s = (s*vx, s*vy, s*vz) • Does not change direction • Changes magnitude: • |s*v| = s * |v| s*v v
3D Basics Dot Product v . w = vx*wx + vy*wy + vz*wz = |v| * |w| * cos ((v,w)) w v w * cos ((v,w))
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)
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)
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
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,…)
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.
3D Basics • The basic transformations are • Scaling • Translation • Rotation
3D Basics Scaling Component wise multiplication with scaling vector s = (sx,sy,sz): S(v,s) = (sx*vx, sy*vy, sz*vz) y v = (x, y, z) X S(v,s) = (2x,1y, 3z), with s = (2,1,3) z
3D Basics Translation (Shift) Component wise addition with translation vector t = (tx, ty, tz) T(v,t) = (tx+vx, ty+vy, tz+vz) y t v = (x, y, z) v + t X z
3D Basics Rotation To 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 Rotation around origin The 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 Rotation around origin written as matrix:
3D Basics 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 We therefore only need to define the rotation around the x, y and z axis.
3D Basics Rotation around x axis: y X z
3D Basics Rotation around y axis: y X z
3D Basics Rotation around z axis: y X z
3D Basics 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 Combinations of transformations can be described by Matrix Multiplication
3D Basics Remember ? = *
3D Basics Remember ? = *
3D Basics With Rx being a rotation matrix around the X axis, Ry being a rotation matrix around the Y axis, Ry * Rx * v is 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 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 Rotation was already defined as matrix. Here comes scaling:
3D Basics What about translation ? Impossible as 3x3 matrix, since addition not multiplication is involved. Here comes a nice trick: increase the dimension !
3D Basics Homogeneous Coordinates (x,y,z) (x,y,z,1)
3D Basics Now the translation by (tx,ty,tz) can be written as:
3D Basics 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 Example Transformations translation scaling Rotation (y) Translation by t and Rotation (y) (which one’s first ?)
3D Basics 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 Orthographic Projection (Parallel Projection) a system of drawing views of an object using perpendicular projectors from the object to a plane of projection… …or…
Illustrations taken from http://engineering-ed.org/CAD/documents/Orthographic%20Projection.ppt 3D Basics Display object by rotation followed by z coordinate removal
3D Basics Parallel lines…
3D Basics The six main projections
3D Basics The six main projections
3D Basics The six main projections
3D Basics The projection matrix of the front view is very simple:
3D Basics • 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 Perspective Projection