3D City v-1.0
Computer Graphics Project
transformations.h
Go to the documentation of this file.
1#pragma once
2#include "maths.h"
3
7namespace trans {
8static mat3f translate(const vec2 &value) {
9 mat3f trans_matrix;
10 trans_matrix(0, 2) = value.x;
11 trans_matrix(1, 2) = value.y;
12 return trans_matrix;
13}
14
15static mat4f translate(const vec3 &value) {
16 mat4f trans_matrix;
17 trans_matrix(0, 3) = value.x;
18 trans_matrix(1, 3) = value.y;
19 trans_matrix(2, 3) = value.z;
20 return trans_matrix;
21}
22
23static mat4f x_rotation(float angle) {
24 angle *= radian;
25 mat4f rot_matrix;
26 rot_matrix(1, 1) = std::cos(angle);
27 rot_matrix(1, 2) = -std::sin(angle);
28 rot_matrix(2, 1) = -rot_matrix(1, 2);
29 rot_matrix(2, 2) = rot_matrix(1, 1);
30 return rot_matrix;
31}
32
33static mat4f y_rotation(float angle) {
34 angle *= radian;
35 mat4f rot_matrix;
36 rot_matrix(2, 2) = std::cos(angle);
37 rot_matrix(2, 0) = -std::sin(angle);
38 rot_matrix(0, 2) = -rot_matrix(2, 0);
39 rot_matrix(0, 0) = rot_matrix(2, 2);
40 return rot_matrix;
41}
42
43static mat4f z_rotation(float angle) {
44 angle *= radian;
45 mat4f rot_matrix;
46 rot_matrix(0, 0) = std::cos(angle);
47 rot_matrix(0, 1) = -std::sin(angle);
48 rot_matrix(1, 0) = -rot_matrix(0, 1);
49 rot_matrix(1, 1) = rot_matrix(0, 0);
50 return rot_matrix;
51}
52
53static mat4f rotation(float angle, const vec3 &v) {
54 float const a = angle;
55 float const c = cos(a);
56 float const s = sin(a);
57
58 vec3 axis(vec3::normalize(v));
59 vec3 temp(axis * (1 - c));
60
61 mat4f Rotate;
62 Rotate(0, 0) = c + temp.x * axis.x;
63 Rotate(0, 1) = temp.x * axis.y + s * axis.z;
64 Rotate(0, 2) = temp.x * axis.z - s * axis.y;
65
66 Rotate(1, 0) = temp.y * axis.x - s * axis.z;
67 Rotate(1, 1) = c + temp.y * axis.y;
68 Rotate(1, 2) = temp.y * axis.z + s * axis.x;
69
70 Rotate(2, 0) = temp.z * axis.x + s * axis.y;
71 Rotate(2, 1) = temp.z * axis.y - s * axis.x;
72 Rotate(2, 2) = c + temp.z * axis.z;
73 return Rotate;
74}
75
76static mat3f rotation(const float &angle, const vec2 &about = vec2(0)) {
77 mat3f rot_matrix;
78 rot_matrix(0, 0) = std::cos(angle);
79 rot_matrix(0, 1) = -std::sin(angle);
80 rot_matrix(1, 0) = -rot_matrix(0, 1);
81 rot_matrix(1, 1) = rot_matrix(0, 0);
82 if (about == vec2(0))
83 return rot_matrix;
84 return translate(about) * rot_matrix * translate(-about);
85}
86
87static mat3f scaling(const vec2 &value, const vec2 &about = vec2(0), const float &angle_offset = 0) {
88 mat3f scale;
89 scale(0, 0) = value.x;
90 scale(1, 1) = value.y;
91
92 if (angle_offset != 0)
93 scale = rotation(angle_offset) * scale * rotation(-angle_offset);
94 if (about != vec2(0))
95 scale = translate(about) * scale * translate(-about);
96
97 return scale;
98}
99
100static mat4f scaling3d(const vec3 &value) {
101 mat4f scale;
102 scale(0, 0) = value.x;
103 scale(1, 1) = value.y;
104 scale(2, 2) = value.z;
105
106 return scale;
107}
108
109static mat3f shearing(const vec2 &values, const vec2 &ref = vec2(0)) {
110 mat3f shear;
111 shear(0, 1) = values.x;
112 shear(1, 0) = values.y;
113 if (ref != vec2(0)) {
114 shear(0, 2) = -values.x * ref.y;
115 shear(1, 2) = -values.y * ref.x;
116 }
117 return shear;
118}
119
120//input a line in the form of y=mx+c
121static mat3f reflection(const float &m, const float &c) {
122 float angle = std::atan(m);
123 mat3f refl;
124 refl(0, 0) = cos(2 * angle);
125 refl(0, 1) = sin(2 * angle);
126 refl(0, 2) = -c * refl(0, 1);
127 refl(1, 0) = refl(0, 1);
128 refl(1, 1) = -refl(0, 0);
129 refl(1, 2) = c * (1 + refl(0, 0));
130
131 return refl;
132}
133
134static mat4f lookAt(const vec3 &eye, const vec3 &center, const vec3 &up) {
135 const vec3 z = (eye - center).normalize();
136 const vec3 y = vec3::normalize(up);
137 const vec3 x = vec3::cross(y, z).normalize();
138 const vec3 c = -center;
139
140 return mat4f({x.x, x.y, x.z, vec3::dot(x, c),
141 y.x, y.y, y.z, vec3::dot(y, c),
142 z.x, z.y, z.z, vec3::dot(z, c),
143 0, 0, 0, 1});
144}
145
146static mat4f persp(const float &w, const float &h, const float &fovx, float fovy = 0) {
147 float D2R = 3.14159265 / 180.0;
148
149 fovy = fovy == 0 ? fovx * h / w : fovy;
150 return mat4f({-w / (2 * tanf(D2R * fovx / 2)), 0, 0, 0,
151 0, -h / (2 * tanf(D2R * fovy / 2)), 0, 0,
152 0, 0, 1, 0,
153 0, 0, 1, 0});
154}
155
156static mat4f perspFOV(float fov, float aspect, float n, float f) {
157 const float D2R = 3.14159265 / 180.0;
158 const auto tan_fov_by2_inv = 1 / tanf(fov * D2R / 2);
159 const auto fmn = f - n;
160 mat4f ret({tan_fov_by2_inv / aspect, 0, 0, 0,
161 0, tan_fov_by2_inv, 0, 0,
162 0, 0, -f / fmn, f * n / fmn,
163 0, 0, -1, 0});
164 return ret;
165}
166
167static mat4f perspective(float x, float y, float z, float zvp) {
168 float dp = z - zvp;
169 if (!dp) {
170 printf("DP is zero");
171 return mat4f();
172 }
173 mat4f ret;
174 ret(0, 2) = -x / dp;
175 ret(0, 3) = x * zvp / dp;
176 ret(1, 2) = -1 * y / dp;
177 ret(1, 3) = y * zvp / dp;
178 ret(2, 2) = 1;
179 ret(2, 3) = 0;
180 ret(3, 2) = -1 / dp;
181 ret(3, 3) = z / dp;
182
183 return ret;
184}
185
186// oblique
187static mat4f oblique_projection(float alpha, float theta) {
188 float pi = 3.14159265;
189 if (alpha == 0 || alpha == 180 || alpha == 360)
190 return mat4f();
191 theta *= pi / 180;
192 alpha *= pi / 180;
193 float l1 = 1 / tan(alpha);
194 mat4f tranMatrix({1, 0, l1 * cosf(theta), 0,
195 0, 1, l1 * sinf(theta), 0,
196 0, 0, 0, 0,
197 0, 0, 0, 1});
198 return tranMatrix;
199}
200} // namespace trans
float zvp
Definition: application.cpp:33
A general matrix class for any size of matrix.
Definition: maths.h:447
constexpr float pi
Definition: maths.h:12
mat< float, 4, 4 > mat4f
Definition: maths.h:750
vec2_T< float > vec2
Definition: maths.h:153
constexpr float radian
Definition: maths.h:10
namespace that has all transformation matrices
T y
Definition: maths.h:26
T x
Definition: maths.h:26
static float dot(const vec3_T< float > &a, const vec3_T< float > &b)
Definition: maths.h:301
static vec3_T< float > cross(const vec3_T< float > &a, const vec3_T< float > &b)
Definition: maths.h:293
T z
Definition: maths.h:162
T x
Definition: maths.h:162
vec3_T< float > normalize()
Definition: maths.h:278
T y
Definition: maths.h:162