-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform.cpp
More file actions
110 lines (83 loc) · 2.4 KB
/
transform.cpp
File metadata and controls
110 lines (83 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
////////////////////////////////////////////////////////////////////////
// A small library of 4x4 matrix operations needed for graphics
// transformations. glm::mat4 is a 4x4 float matrix class with indexing
// and printing methods. A small list or procedures are supplied to
// create Rotate, Scale, Translate, and Perspective matrices and to
// return the product of any two such.
#include <glm/glm.hpp>
#include "math.h"
#include "transform.h"
float* Pntr(glm::mat4& M)
{
return &(M[0][0]);
}
//@@ The following procedures should calculate and return 4x4
//transformation matrices instead of the identity.
// Return a rotation matrix around an axis (0:X, 1:Y, 2:Z) by an angle
// measured in degrees. NOTE: Make sure to convert degrees to radians
// before using sin and cos. HINT: radians = degrees*PI/180
const float pi = 3.14159f;
glm::mat4 Rotate(const int i, const float theta)
{
const float radians = theta * (pi / 180);
glm::mat4 R(1.0);
const int j = (i + 1) % 3;
const int k = (i + 2) % 3;
R[j][j] = cos(radians);
R[k][k] = cos(radians);
//R[][] = 1;
R[k][j] = -1 * sin(radians);
R[j][k] = sin(radians);
return R;
}
// Return a scale matrix
glm::mat4 Scale(const float x, const float y, const float z)
{
glm::mat4 S(1.0);
S[0][0] = x;
S[1][1] = y;
S[2][2] = z;
return S;
}
// Return a translation matrix
glm::mat4 Translate(const float x, const float y, const float z)
{
glm::mat4 T(1.0);
T[3][0] = x;
T[3][1] = y;
T[3][2] = z;
return T;
}
// Returns a perspective projection matrix
glm::mat4 Perspective(const float rx, const float ry,
const float front, const float back)
{
glm::mat4 P(1.0);
P[0][0] = 1 / rx;
P[1][1] = 1 / ry;
P[2][2] = -1 * (back + front) / (back - front);
P[3][2] = -1 * (2 * front * back) / (back - front);
P[2][3] = -1;
P[3][3] = 0;
return P;
}
glm::mat4 LookAt(const glm::vec3 Eye, const glm::vec3 Center, const glm::vec3 Up)
{
glm::vec3 V = (Center - Eye);
glm::vec3 A = glm::cross(V, Up);
V = glm::normalize(V);
A = glm::normalize(A);
glm::vec3 B = glm::cross(A, V);
glm::mat4 const eye = Translate(-Eye.x, -Eye.y, -Eye.z);
glm::mat4 R(1.0);
R[0][0] = A.x;
R[1][0] = A.y;
R[2][0] = A.z;
R[0][1] = B.x;
R[1][1] = B.y;
R[2][1] = B.z;
R[0][2] = -V.x;
R[1][2] = -V.y;
R[2][2] = -V.z;
return R * eye;
}