-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransform.cpp
More file actions
129 lines (119 loc) · 3.23 KB
/
Transform.cpp
File metadata and controls
129 lines (119 loc) · 3.23 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "Transform.h"
Matrix4 Transform::translate(Vector3 v)
{
Matrix4 result;
result.identity();
result.m[0][3] = v.x;
result.m[1][3] = v.y;
result.m[2][3] = v.z;
return result;
}
// http://www.fastgraph.com/makegames/3drotation/
Matrix4 Transform::rotate(double angle, const Vector3 &axis)
{
angle = angle / 180.0 * M_PI; // convert from degrees to radians
Matrix4 result = Matrix4().identity();
double c = cos(angle);
double s = sin(angle);
double t = 1 - cos(angle);
result.m[0][0] = t * axis.x * axis.x + c;
result.m[0][1] = t * axis.x * axis.y - s * axis.z;
result.m[0][2] = t * axis.x * axis.z + s * axis.y;
result.m[1][0] = t * axis.x * axis.y + s * axis.z;
result.m[1][1] = t * axis.y * axis.y + c;
result.m[1][2] = t * axis.y * axis.z - s * axis.x;
result.m[2][0] = t * axis.x * axis.z - s * axis.y;
result.m[2][1] = t * axis.y * axis.z + s * axis.x;
result.m[2][2] = t * axis.z * axis.z + c;
return result;
}
Matrix4 Transform::rotateX(double angle)
{
angle = angle / 180.0 * M_PI; // convert from degrees to radians
Matrix4 result;
result = result.identity();
result.m[1][1] = cos(angle);
result.m[2][1] = sin(angle);
result.m[1][2] = -sin(angle);
result.m[2][2] = cos(angle);
return result;
}
Matrix4 Transform::rotateY(double angle)
{
angle = angle / 180.0 * M_PI; // convert from degrees to radians
Matrix4 result;
result = result.identity();
result.m[0][0] = cos(angle);
result.m[0][2] = sin(angle);
result.m[2][0] = -sin(angle);
result.m[2][2] = cos(angle);
return result;
}
Matrix4 Transform::rotateZ(double angle)
{
angle = angle / 180.0 * M_PI; // convert from degrees to radians
Matrix4 result;
result = result.identity();
result.m[0][0] = cos(angle);
result.m[1][0] = sin(angle);
result.m[0][1] = -sin(angle);
result.m[1][1] = cos(angle);
return result;
}
Matrix4 Transform::scale(double s)
{
return scale(Vector3(s, s, s));
}
Matrix4 Transform::scale(Vector3 s)
{
Matrix4 result;
result.identity();
result.m[0][0] = s.x;
result.m[1][1] = s.y;
result.m[2][2] = s.z;
return result;
}
Matrix4 Transform::lookAt(Vector3 center, Vector3 eye, Vector3 up)
{
Matrix4 result;
Vector3 z = (center - eye).normalize();
Vector3 x = cross(up, z).normalize();
Vector3 y = cross(z, x);
result.m[0][0] = x.x;
result.m[0][1] = x.y;
result.m[0][2] = x.z;
result.m[1][0] = y.x;
result.m[1][1] = y.y;
result.m[1][2] = y.z;
result.m[2][0] = z.x;
result.m[2][1] = z.y;
result.m[2][2] = z.z;
result.m[3][3] = 1;
result = result.transpose();
result *= Transform::translate(center.negate());
return result;
}
#include <iostream>
Matrix4 Transform::perspective(double fov, double aspect, double near, double far)
{
Matrix4 result;
fov = fov / 180.0 * M_PI; // convert from degrees to radians
result.m[1][1] = 1 / tan(0.5 * fov);
result.m[0][0] = result.m[1][1] / aspect;
result.m[2][2] = (near + far) / (near - far);
result.m[2][3] = (2 * near * far) / (near - far);
result.m[3][2] = -1;
return result;
}
Matrix4 Transform::viewport(int x, int y, int width, int height)
{
Matrix4 result;
result.m[0][0] = (width - x) / 2;
result.m[0][3] = (width + x) / 2;
result.m[1][1] = (height - y) / 2;
result.m[1][3] = (height + y) / 2;
result.m[2][2] = 1 / 2;
result.m[2][3] = 1 / 2;
result.m[3][3] = 1;
return result;
}