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
v1v2 = (y1z2 y2z1 , z1x2 z2x1 , x1y2 x2y1).
The above vector is perpendicular to both v1 and v2. The
direction of v1v2 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 =(x2x1, y2y1, z2z1), v2 =(x3x1, y3y1, z3z1) The cross product v1v2 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(y2y3)+x2(y3y1)+x3(y1y2) )
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 a03x a00xa01ya02za03 a a a a ya xa ya za
10 11 12 13 10 11 12 13 a20 a21 a22 a23z a20xa21ya22za23 a30 a31 a32 a331 a30xa31ya32za33
3 0 1 12 5 2 1 5 077 1 1 2 1 2 8 0 4 1 31 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 0300 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 a23b20 b21 b23 . . . .
aaaabbb…. 30 31 32 3330 31 33
R. Mukundan, CSSE, University of Canterbury
1 0 1 20.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 50.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:
xa a a ax p 00 01 02 03p
p 10 11 12 13 p ya a a a y
za a a az p 20 21 22 23p
Transformed point
10 0 0 11
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)
xa 1 0 0 ax yb0 1 0 by zc 0 0 1 cz
1 0 0 0 11 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 ax y0 1 0 by z 0 0 1 cz
0 0 0 0 10 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 0x yb 0 b 0 0y zc 0 0 c 0z
1 0 0 0 11 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 0y 0 1 0z 0 0 11
x cos sin 0 0x
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:
0x 0y
x 1 0 0
y 0 cos sin
z 0 sin cos 0z
100 011 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
0x 0y
x cos
y 0
z sin 0 cos 0z
1 0 0 0 11 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.
cos1(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 rotatedtransformed 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:
xa a a ax
p 00 01 02 03p p 10 11 12 13 p
ya a a a y za a a az
p 20 21 22 23p 10 0 0 11
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 = QP (Normalize this vector) Turn angle: = cos-1(u.v)
Axis of rotation: uv = (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
cos1(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, R2x2y2 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=(1t)v1 +tv2 ,
v1 v 0t1
v1 =(0,1,1)
v2 =(1,0,1)
v =(1t)(0,1,1) + t(1,0,1)
(0,1,1) (t,1t,1) (1,0,1)
=(t, 1t, 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(1t)lx tx y(1t)ly ty0
P = (x, y, z )
z(1t)lz tz ly y
R. Mukundan, CSSE, University of Canterbury
Floor Plane ( y= 0 )
P(1t)LtP, t 1.
Planar Shadows
The projection P of the vertex (x, y, z) on the floor- plane is given by the following coordinates:
xlxylyx ly y
y 0 zlzylyz
The above equations can be written as a transformation:
sx ly lx 0 0x s 0 0 0 0y
y 0 l l 0 sz z y z
w 0 1 0 ly1
R. Mukundan, CSSE, University of Canterbury
Homogeneous Coordinates
sx lxylyx s 0
sz lzylyz
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