留学生作业代写 OSC363

6 Mathematical Preliminaries
R. Mukundan Department of Computer Science and Software Engineering University of Canterbury, .

GLM Library

Copyright By PowCoder代写 加微信 powcoder

The OpenGL Mathematics (GLM) library is a convenient C++ library for developing graphics applications.
 Header only library
#include
#include
 Homepage: https://glm.g-truc.net/0.9.9/
 Several functions and variables use similar naming convention as OpenGL and GLSL
 Particularly useful for matrix operations, lighting computations, ray tracing and OpenGL-4 shader development.
R. Mukundan, CSSE, University of Canterbury

Homogeneous Coordinates
 A point with Cartesian coordinates (x, y, z) can be expressed in homogeneous coordinates as (hx, hy, hz, h) where h is any non-zero real number.
glVertex3f (10, 2, 3); glVertex4f (10, 2, 3, 1); glVertex4f (60, 12, 18, 6) glVertex4f (20, 4, 6, 2)
Different representations of the same point
 To convert from homogeneous coordinates to Cartesian coordinates, divide the first three components by the fourth element: (a, b, c, d)  (a/d, b/d, c/d)
Example: The xyz coordinates of the point (12, 16, 1, 4) are (3, 4, 0.25)
 A vector with components (x, y, z) is represented in
homogeneous coordinates as (x, y, z, 0).
R. Mukundan, CSSE, University of Canterbury

Points and Vectors
C++ and GLM
float p[] = {-10, 8, 3};
float lgt[] = {3, 100, -2, 1};
glm::vec3 p(-10, 8, 3);
glm::vec4 lgt(3, 100, -2, 1);
Copying an array
float q[3];
for(int i = 0; i < 3; i++) q[i] = p[i]; glm::vec3 q = p; Adding two arrays float r[3]; for(int i = 0; i < 3; i++) r[i] = p[i] + q[i]; glm::vec3 r=p+q; Scalar multiplication float s = 10; for(int i = 0; i < 3; i++) q[i] = s * p[i]; float s = 10; glm::vec3 q=s*p; for(int i = 0; i < 3; i++) cout << p[i] << “ ”; cout << endl; cout << glm::to_string(p) << endl; R. Mukundan, CSSE, University of Canterbury Vector Magnitude If v = (x, y, z) denotes a vector, its magnitude (or length) is given x2 y2 z2 Example: v = (3, 2, 6). v  9  4  36  7 Unit Vector u =(3/7, 2/7, 6/7) Normalization is the process of converting a vector to a unit vector by dividing each of its components by its magnitude. The distance between two points P and Q is the length of the vector P-Q. glm::vec3 u = glm::normalize(v); //Unit vector float len = glm::length(v); //Magnitude float dist = glm::distance(p, q); //Distance R. Mukundan, CSSE, University of Canterbury Vector Dot Product  The dot product of two vectors v1= (x1, y1, z1) and v • w = 59 y2, z2) is given by v1•v2 = x1x2 + y1y2 + z1z2 v= (6, 8, 3) w = (5, 4, 1)  The dot product is a scalar value, not a vector. float dprod = glm::dot(v, w); //Dot product  If v1 and v2 denote unit vectors, then v1•v2 = cos() , where  is the angle between the two vectors.  = cos-1(v1•v2) Example: Computation of angle between vectors (2, 3, 3) and (1,1, 0): • Normalize both vectors: (0.426, 0.64, 0.64), (0.707, 0.707, 0) • Compute the dot product: 0.754 (= cos) •  = cos-1(0.754) = 41.06 Degs. R. Mukundan, CSSE, University of Canterbury Orthogonality of Vectors  If two vectors v1 , v2 are perpendicular (orthogonal) to each other, then, v1•v2 = 0.  = ± 90 degs  cos = 0  v1•v2 = 0 v2  Example: Show that vectors (5, 2, 8) and (2, 7, 3) are perpendicular. Compute the dot product: 10 + 14  24 = 0  Since the dot product is 0, the vectors are orthogonal to each other. (There is no need to normalize the vectors) R. Mukundan, CSSE, University of Canterbury Relative Orientation of Two Vectors It is often required to know if two vectors v1 and v2 are separated by an acute angle ( </2) or obtuse angle ( >/2).
1 cos>0  cos<0 v v1•v2 >0
Polygon not visible to camera
Polygon in shadow
R. Mukundan, CSSE, University of Canterbury

Vector Cross Product
 The cross product of two vectors v1= (x1, y1, z1) and v1  v2 v2= (x2, y2, z2) is a vector given by
v1v2 = (y1z2 y2z1 , z1x2 z2x1 , x1y2 x2y1).
 The above vector is perpendicular to both v1 and v2. The
 direction of v1v2 is given by the right-hand rule.
 If v1 and v2 denote unit vectors, then |v1  v2|= sin() , where  is the angle between the two vectors.
 If v1 and v2 are parallel vectors, v1  v2 = 0.
glm::vec3 cprod = glm::cross(v, w); //Cross product
R. Mukundan, CSSE, University of Canterbury

Surface Normal Vector: Triangle
Triangle: P = (x1, y1, z1) , Q = (x2, y2, z2), R = (x3, y3, z3).
v1 =(x2x1, y2y1, z2z1), v2 =(x3x1, y3y1, z3z1)  The cross product v1v2 gives the normal vector n:
(y2 y1)(z3 z1) (y3 y1)(z2 z1), (z2 z1)(x3 x1) (z3 z1)(x2 x1) , (x2 x1)(y3 y1) (x3 x1)(y2 y1) )
y1(z2  z3) + y2(z3  z1) + y3(z1  z2), z1(x2  x3) + z2(x3  x1) + z3(x1  x2), x1(y2y3)+x2(y3y1)+x3(y1y2) )
 We form two vectors at Q: v1 = Q  P, and v2 = R  P.
R. Mukundan, CSSE, University of Canterbury

Surface Normal Vector: Triangle
Input: 3 vertices of a triangle. v2
void normal(float x1, float y1, float z1, float x2, float y2, float z2,
float x3, float y3, float z3 )
float nx, ny, nz;
nx = y1*(z2-z3)+ y2*(z3-z1)+ y3*(z1-z2); ny = z1*(x2-x3)+ z2*(x3-x1)+ z3*(x1-x2); nz = x1*(y2-y3)+ x2*(y3-y1)+ x3*(y1-y2);
glNormal3f(nx, ny, nz);
glm::vec3 norm = glm::cross(q-p, r-p);
R. Mukundan, CSSE, University of Canterbury

 OpenGL uses 4×4 matrices for representing transformations.
 A 4×4 matrix may be stored in a single array m[k], k = 0..15, in either row-major order or column-major order. OpenGL and GLM store matrices in column-major order.
float m[16] = {.., .., ..,
mmmm 0123
mmmm 4567
mmmm 891011
12 13 14 15 (Row Major Order)
(Column Major Order)
OpenGL, GLM
mmmm 04812
mmmm 1 5 9 13
mmmm 261014
R. Mukundan, CSSE, University of Canterbury
glm::mat4 m;

 Identity Matrix 1 0 0 0
I = 0 1 0 0 0 0 1 0
glm::mat4 m = glm::mat4(1);
AI = IA = A
 For any matrix A,
 OpenGL Example (using C array):
float m[16]={0.5, 3.0, 0.1, 0, 0, 10., 6.0, 0,
8.0, 1.0,-4.2, 0, -2.0, 0, 9.0, 1.0};
glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glMultMatrixf(m);
0.5 0 8 2 3 10 1 0 0.1 6 4.2 9  0001
glm::mat4 m(0.5, 3.0, 0.1, 0, 0, 10., 6.0, 0,
8.0, 1.0,-4.2, 0, -2.0, 0, 9.0, 1.0); R. Mukundan, CSSE, University of OSC363

Matrix Product
Transformation of a point as a matrix product:
a00 a01 a02 a03x a00xa01ya02za03 a a a a ya xa ya za 
10 11 12 13 10 11 12 13 a20 a21 a22 a23z a20xa21ya22za23 a30 a31 a32 a331 a30xa31ya32za33
3 0 1 12 5 2 1 5 077  1 1 2 1 2 8 0 4 1 31 23
glm::mat4 m;
glm::vec4 p, q;
q = m * p;
R. Mukundan, CSSE, University of Canterbury

Matrix Product
a a a a b b b  . . . . 00 01 02 0300 01 03  
aaaabbb….  10 11 12 13 10 11 13 
c12 =a10 b02 +a11 b12 +a12 b22 +a13 b32 General formula: cij   aik bk j
a20 a21 a22 a23b20 b21 b23 . . . .   
aaaabbb…. 30 31 32 3330 31 33  
R. Mukundan, CSSE, University of Canterbury
1 0 1 20.7 0 0.7 0 0 0 1.4 2
0 1 0 0 0 1 0 0 0 1 0 0
1 0 1 50.7 0 0.7 0  1.4 0 0 5
Matrix multiplication is non-commutative. In general, AB ≠ BA
0 0 0 1 0 0 0 1 0 0 0 1    

Transformation Matrix
The transformation of a point (x, y, z, 1) to another point (x , y, z, 1) can be expressed as a matrix-vector multiplication:
xa a a ax p 00 01 02 03p
p 10 11 12 13 p ya a a a y 
za a a az p 20 21 22 23p
Transformed point
10 0 0 11
R. Mukundan, CSSE, University of Canterbury
A general transformation matrix
Input point

Translation Matrix
 The translation of a point (x, y, z, 1) by (a, b, c) yields another point (x+a , y+b , z+c , 1)
xa 1 0 0 ax yb0 1 0 by zc 0 0 1 cz
 1  0 0 0 11 Translation
 OpenGL function: glTranslatef(a, b, c)
R. Mukundan, CSSE, University of Canterbury

Translation Matrix
The translation matrix has no effect on a vector (x, y, z, 0):
x 1 0 0 ax y0 1 0 by z 0 0 1 cz
0 0 0 0 10 Translation
R. Mukundan, CSSE, University of Canterbury

Scale Matrix
 The scaling of a point (x, y, z, 1) by factors (a, b, c) yields another point (xa , yb , zc , 1)
xa a 0 0 0x yb  0 b 0 0y zc 0 0 c 0z
1 0 0 0 11 Scale Matrix
 OpenGL function: glScalef(a, b, c)
R. Mukundan, CSSE, University of Canterbury

Rotation About the Z-axis
 Equations:
x’ = x cos  y sin
y’ = x sin + y cos z’ = z
 Matrix Form:
y  sin z  0
cos 0 0y 0 1 0z 0 0 11
x cos sin 0 0x
OpenGLfunction: glRotatef(theta,0,0,1)
R. Mukundan, CSSE, University of Canterbury

Rotation About the X-axis
 Equations: x’ = x
y’ = y cos  z sin z’ = y sin + z cos
 Matrix Form:
0x 0y
x 1 0 0
y  0 cos sin
z 0 sin cos 0z
100 011 OpenGLfunction: glRotatef(theta,1,0,0)
R. Mukundan, CSSE, University of Canterbury

Rotation About the Y-axis
 Equations:
x’ = x cos + z sin
z’ = x sin + z cos
 Matrix Form:
0 sin 1 0
0x 0y
x  cos
y   0
z sin 0 cos 0z
1  0 0 0 11 OpenGLfunction: glRotatef(theta,0,1,0)
R. Mukundan, CSSE, University of Canterbury

General Rotation
 Suppose a vector OP needs to be transformed to another vector OQ, where P, Q are at the same distance from the origin O.
 If we can find the angle of rotation  from P to Q, and the axis of rotation (l, m, n), then we can apply a rotational transformation glRotatef(, l, m, n) to the vector OP.
 cos1(v v ) 12
(l,m,n)=v1 v2
R. Mukundan, CSSE, University of Transformations
User-defined transformations can be represented in matrix form and applied with other transforms.
float myMatrix[16]={0.5, 3.0, 0.1, 0,
0, 10., 6.0, 0,
8.0, 1.0,-4.2, 0, -2.0, 0, 9.0, 1.0};
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(…)
glPushMatrix();
glTranslatef(5, 2, -3); glMultMatrixf(myMatrix); glRotatef(25, 0, 1, 0); glutSolidTeapot(1);
glPopMatrix();
Teapot rotatedtransformed using myMatrix translated R. Mukundan, CSSE, University of Canterbury
3 10 1 0 0.1 6 4.2 9  0001

Affine Transformation
 A general linear transformation followed by a translation is called an affine transformation.
 Matrix form:
xa a a ax
p 00 01 02 03p p 10 11 12 13 p
ya a a a y  za a a az
p 20 21 22 23p 10 0 0 11
 Translation, rotation, scaling and shear transformations are all affine transformations.
 Under an affine transformation, line segments transform into line segments, and parallel lines transform into parallel lines.
R. Mukundan, CSSE, University of Canterbury

Re-orienting a 3D Object
If an object is required to follow a path specified by a polygonal line in 3D space, then at each vertex of the path, the object must be transformed from the initial to the required direction.
Initial direction
Current direction
Required direction: v = QP (Normalize this vector) Turn angle:  = cos-1(u.v)
Axis of rotation: uv = (l, m, n)
Transformation:
R. Mukundan, CSSE, University of Canterbury
glRotatef (, l, m, n)

Virtual Trackball
 A user interface for drag-rotating an object.
 Assume that the objects displayed on the screen are
attached to a virtual sphere.
 When the mouse is dragged from one point to another on the screen, a corresponding path of rotation is generated on the sphere.
Mouse Drag Rotation
Virtual Sphere
R. Mukundan, CSSE, University of Canterbury

VirtualTrackball
 Let M1M2 be the path through which the mouse is dragged, and P1 , P2, the corresponding points on the virtual sphere.
 The angle of rotation is the angle between unit vectors v1 and v2
 cos1(v v ) 1 2
 The axis of rotation is the axis perpendicular to both v1 and v2, given byv1 v2 =(l,m,n)
 Use glRotatef(, l, m, n) to rotate the object.
M (x, y) (0, 0)
x,y, R2x2y2 P
R. Mukundan, CSSE, University of Canterbury

Linear Interpolation
Linear interpolation is useful in computing an in-between value, given the values v1, v2 of some attribute at the end points of a path.
v=(1t)v1 +tv2 ,
v1 v 0t1
v1 =(0,1,1)
v2 =(1,0,1)
v =(1t)(0,1,1) + t(1,0,1)
(0,1,1) (t,1t,1) (1,0,1)
=(t, 1t, 1) COSC363
R. Mukundan, CSSE, University of Shadows
 To find the shadow of a vertex P of a polygon, we extend the line from the light source L through P to the floor plane, and use a linear extrapolation of coordinates.
L (l , l , l ) xyz
P (x, y, z)
x(1t)lx tx y(1t)ly ty0
P = (x, y, z )
z(1t)lz tz ly y
R. Mukundan, CSSE, University of Canterbury
Floor Plane ( y= 0 )
P(1t)LtP, t 1.

Planar Shadows
 The projection P of the vertex (x, y, z) on the floor- plane is given by the following coordinates:
xlxylyx ly y
y  0 zlzylyz
 The above equations can be written as a transformation:
sx  ly lx 0 0x s  0 0 0 0y
y 0 l l 0 sz  z y z
w 0 1 0 ly1 
R. Mukundan, CSSE, University of Canterbury
Homogeneous Coordinates
sx lxylyx s 0
sz lzylyz

Planar Shadows: Code
// Light source position = (lx, ly, lz)
float shadowMat[16] = { ly,0,0,0, -lx,0,-lz,-1,
// Draw object glEnable(GL_LIGHTING); glPushMatrix();
/* Transformations */
drawObject(); glPopMatrix();
// Draw shadow
glDisable(GL_LIGHTING);
glPushMatrix();
glMultMatrixf(shadowMat);
/* Transformations */ glColor4f(0.2, 0.2, 0.2, 1.0);
drawObject(); glPopMatrix();
0,0,ly,0, 0,0,0,ly };
Column vectors of the transformation matrix on the previous slide
R. Mukundan, CSSE, University of Canterbury

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com