Description
Submission timestamps will be checked and enforced strictly by the CourseWeb; late submissions will not be accepted. Check the due date of this lab on the CourseWeb. Remember that, per the course syllabus, if you are not marked by your recitation instructor as having attended a recitation, your score will be cut in half.
For this lab, you are going to create your own library for vector and matrix operations. In this class, we are mainly use 4 1 matrices (column vectors) and 4 4 matrices. It is a good idea for you to create your own library to perform their operation. For this lab, you must create two les, header le and source le. You can name your le which ever you want (.h) for header le and (.c) for source le.
Vector and its Operations
A vector is a matrix (4 1) or column vector as shown below:
-
3
x
-
y 7
-
7
4z 5
w
In OpenGL (C programming) environment, a 4 1 column vector is simply a four consecutive region in the memory that contains four oating-point value. For simplicity, we can create a new type in your header le called vec4 as follows:
typedef struct
{
GLfloat x;
GLfloat y;
GLfloat z;
GLfloat w;
} vec4;
Note that if your system does not support GLfloat yet, just simply use float. GLfloat and float are identical. With the above declaration, a program can create the following vector:
2 3
1
627
v = 6 7
435
4
using the following code:
#include “common.h”
int main(void)
{
vec4 v = {1, 2, 3, 4};
:
}
CS 1566 | Introduction to Computer Graphics Page 1
Lab 2: Library for Vector and Matrix Operation
Note that you can also use a one-dimensional array of type GLfloat (or float) with four elements to represent a 4 1 column vector as shown below:
typedef float vec4[4];
int main(void)
{
vec4 v = {1,2,3,4};
:
}
In this section, create the following functions in your source le (and do not forget to put your function signatures into your header le). Note that you can change names and how your function passes values whichever way you prefer.
Prints a 4 1 column vector on the console screen: This function will be very useful to help you debug your code. To reduce the amount of space to print, you can simply print a vector like a row vector and limit each oating-point number to just two digits after decimal point (e.g., [1.23 -2.34 3.45 -4.56]).
Scalar-Vector Multiplication: Given a scalar value s (a oating-point number) and a vector v (4 1), this function should produce the result s v which is a 4 1 column vector.
-
2
y
3
=
2
y
3
6
x
7
6
x
7
z
z
6
w
7
6
w
7
4
5
4
5
Vector-Vector Addition: Given two vectors v1 and v2, this function should produce the result v1 + v2 which is a column vector.
-
2y1 3
+
2y2 3
= 2 y1
+ y2 3
6
x1
7
x2
7
6
x1
+ x2
7
z1
6z2
z1 + z2
6
w1
7
6w2
7
6
w1
+ w2
7
4
5
4
5
4
5
Vector-Vector Subtraction Given two vectors v1 and v2, this function should produce the
result v1 v2 which is a column vector.
-
2y1 3
2y2 3
= 2 y1
y2 3
6
x1
7
x2
7
6
x1
x2
7
z1
6z2
z1
z2
6
w1
7
6w2
7
6
w1
w2
7
4
5
4
5
4
5
Magnitude of a Vector The magnitude of a four-element vector v denoted by jvj is as
follows:
p
jvj = x2 + y2 + z2 + w2
CS 1566 | Introduction to Computer Graphics Page 2
Lab 2: Library for Vector and Matrix Operation
Normalize A normalized vector is a vector with magnitude 1. A normalized of a vector v is a vector v^ codirectional with v and jv^j = 1. The vector v^ can be calculated by
-
v^ = 1
2
y
3
x
6
z
7
jvj
6
w
7
4
5
Dot product: Given two vectors v1 and v2, this function should produce the result v1 v2.
Note that the result is a scalar value (a oating-point number).
2323
x1 x2
6y1 7 6y2 7 = (x x ) + (y y ) + (z z ) + (w w )
6767 1 2 1 2 1 2 1 2
4z1 5 4z2 5
w1 w2
Cross product: Given two vectors v1 and v2, this function should produce the result v1 v2 which is a column vector. Note that the fourth element of the result vector should always be 0. The calculation should ignore the fourth elements of both v1 and v2. Do not forget about the right-hand rule.
-
2y1
3
2y2 3 =
2(z1
x2)
(x1
z2)3
6
x1
7
6
x2
7
6
(y1
z2)
(z1
y2)
7
z1
z2
(x1 y2)
(y1
x2)
6
w1
7
6
w2
7
6
0:0
7
4
5
4
5
4
5
Again, you can name your functions and their signatures whichever way you want.
Matrix and its Operations
In OpenGL (C programming) environment, a 4 4 matrix is simply a four consecutive region in the memory that contains 16 oating-point value organized according to Column Major. With the declaration of a four-element column vector, a 4 4 matrix can be created as follows:
typedef struct
{
vec4 x;
vec4 y;
vec4 z;
vec4 w;
} mat4;
Note that the above mat4 data type is a column major. Each column is a vector (vec4). With the above data type, we can declare the following 4 4 matrix:
2
1
6 5
6
4 9
13
using the following code snippet:
6 |
7 |
8 |
3 |
2 |
3 |
4 |
|
10 |
11 |
127 |
|
5 |
|||
14 |
15 |
167 |
CS 1566 | Introduction to Computer Graphics Page 3
Lab 2: Library for Vector and Matrix Operation
mat4 m = {{1,5,9,13},{2,6,10,14},{3,7,11,15},{4,8,12,16}};
You can also use a 16-element one-dimensional array of oating-points as shown below:
typedef float mat4[16];
However, do not forget that it must be a column major. Suppose a 4 4 matrix is declared (from one-dimensional array of oating-points) as shown below:
typedef float mat4[16];
int main(void)
{
mat4 m = …;
:
}
the actual matrix represented by the above declaration is shown below:
-
2
m[0]
m[4]
m[8]
m[12]
3
6
m[1]
m[5]
m[9]
m[13]
7
m[2]
m[6]
m[10]
m[14]
4
5
6
m[3]
m[7]
m[11]
m[15]
7
In this section, create the following functions for matrix operations:
Print a matrix on the console screen for debugging purpose
Scalar-Matrix multiplication: Given a scalar value s and a matrix m, this function should
produce the result s m which is a matrix.
-
2a21
a22
a23
a243
=
2 a21
a22
a23
a243
a11
a12
a13
a14
7
a11
a12
a13
a14
7
6a31 a32
a33
a34
6 a31
a32
a33
a34
6a41
a42
a43
a44
7
6
a41
a42
a43
a44
7
4
5
4
5
Matrix-Matrix Addition: Given two matrices m1 and m2, this function should produce
the result m1 + m2 which is a matrix.
-
2
a21
a22
a23
a24
3
+
2
b21
b22
b23
b24
3
=
2a21
+ b21
a22
+ b22
a23
+ b23
a24
+ b24
3
6
a11
a12
a13
a14
7
6
b11
b12
b13
b14
7
a11
+ b11
a12
+ b12
a13
+ b13
a14
+ b14
7
a31
a32
a33
a34
b31
b32
b33
b34
6a31 + b31
a32 + b32
a33 + b33
a34
+ b34
6
a41
a42
a43
a44
7
6
b41
b42
b43
b44
7
6a41
+ b41
a42 + b42
a43 + b43
a44
+ b44
7
4
5
4
5
4
5
Matrix-Matrix Subtraction: Given two matrices m1 and m2, this function should produce
-
the result m1
m2 which is a matrix.
3
2a21
3
2
a21
a22
a23
a24
3
2
b21
b22
b23
b24
=
b21
a22
b22
a23
b23
a24
b24
6
a11
a12
a13
a14
7
6
b11
b12
b13
b14
7
a11
b11
a12
b12
a13
b13
a14
b14
7
a31
a32
a33
a34
b31
b32
b33
b34
6a31
b31
a32
b32
a33
b33
a34
b34
6
a41
a42
a43
a44
7
6
b41
b42
b43
b44
7
6a41
b41
a42
b42
a43
b43
a44
b44
7
4
5
4
5
4
5
CS 1566 | Introduction to Computer Graphics Page 4
Lab 2: Library for Vector and Matrix Operation
Matrix-Matrix multiplication: Given two matrices m1 and m2, this function should
produce the result m1 m2 which is a matrix.
-
2a21
a22
a23
a24
3
2
b21
b22
b23
b24
3
=
2c21
c22
c23
c24
3
a11
a12
a13
a14
7
6
b11
b12
b13
b14
7
c11
c12
c13
c14
7
6a31
a32
a33
a34
b31
b32
b33
b34
6c31
c32
c33
c34
6a41
a42
a43
a44
7
6
b41
b42
b43
b44
7
6c41
c42
c43
c44
7
4
5
4
5
4
5
where
c11 = (a11 b11) + (a12 b21) + (a13 b31) + (a14 c21 = (a21 b11) + (a22 b21) + (a23 b31) + (a24 c31 = (a31 b11) + (a32 b21) + (a33 b31) + (a34 c41 = (a41 b11) + (a42 b21) + (a43 b31) + (a44 c12 = (a11 b12) + (a12 b22) + (a13 b32) + (a14 c22 = (a21 b12) + (a22 b22) + (a23 b32) + (a24 c32 = (a31 b12) + (a32 b22) + (a33 b32) + (a34 c42 = (a41 b12) + (a42 b22) + (a43 b32) + (a44 c13 = (a11 b13) + (a12 b23) + (a13 b33) + (a14 c23 = (a21 b13) + (a22 b23) + (a23 b33) + (a24 c33 = (a31 b13) + (a32 b23) + (a33 b33) + (a34 c43 = (a41 b13) + (a42 b23) + (a43 b33) + (a44 c14 = (a11 b14) + (a12 b24) + (a13 b34) + (a14 c24 = (a21 b14) + (a22 b24) + (a23 b34) + (a24 c34 = (a31 b14) + (a32 b24) + (a33 b34) + (a34 c44 = (a41 b14) + (a42 b24) + (a43 b34) + (a44
b41) b41) b41) b41) b42) b42) b42) b42) b43) b43) b43) b43) b44) b44) b44) b44)
Inverse of a Matrix: Given a matrix m this function should produce the result m 1 which is a matrix. It may be a good idea for you to test by simply calculate whether m m 1 = 1 and m 1 m = 1. See the lecture slides on how to calculate the inverse of a 4 4 matrix.
Transpose of a Matrix: Given a matrix m this function should produce the result mT
which is a matrix.
-
2
a21
a22
a23
a24
3
T
2a12
a22
a32
a42
3
=
6
a11
a12
a13
a14
7
a11
a21
a31
a41
7
a31
a32
a33
a34
6a13
a23
a33
a43
6
a41
a42
a43
a44
7
6a14
a24
a34
a44
7
4
5
4
5
Matrix-Vector Multiplication: Given a matrix m and a vector v, this function should
produce the result m v which is a vector.
-
2a21
a22
a23
a24
3
2y 3 =
2(a21
x) + (a22
y) + (a23
z) + (a24
w)3
a11
a12
a13
a14
7
6
x
7
(a11
x) + (a12
y) + (a13
z) + (a14
w)
7
6a31 a32
a33
a34
z
6(a31 x) + (a32 y) + (a33 z) + (a34 w)
6a41
a42
a43
a44
7
6
w
7
6(a41
x) + (a42
y) + (a43
z) + (a44
w)
7
4
5
4
5
4
5
Again, you can name your functions and their signatures whichever way you want.
CS 1566 | Introduction to Computer Graphics Page 5
Lab 2: Library for Vector and Matrix Operation
Test Your Library
Again, your library should be in two les, a header le and a source le. Now, you should test your library a little bit. For this section, create a new program named test.c. In this program, create the following vectors and matrices:
-
v1
=
223
v2
=
263
m1 = 2
5
6
7
8
3
m2 = 2 8
7
6
5 3
1
7
5
7
6
1
2
3
4
7
6
4
3
2
1
7
63
67
9
10
11
12
12
11
10
9
64
7
68
7
6
13
14
15
16
7
6
16
15
14
137
4 5
4 5
4
5
4
5
and a scalar value s = 3:0. The test program should print out the following result in this order:
s v1
v1 + v2 v1 v2
jvj v^
v1 v2 v1 v2 s m1
m1 + m2 m1 m2
m1 m2
m1 1
mT1
m1 v1
Submission
Zip your header le, source le, and test le into a le named lab02.zip. Submit your lab02.zip le via CourseWeb before the due date stated on the CourseWeb.
CS 1566 | Introduction to Computer Graphics Page 6