10 trans_matrix(0, 2) = value.
x;
11 trans_matrix(1, 2) = value.
y;
15static mat4f translate(
const vec3 &value) {
17 trans_matrix(0, 3) = value.
x;
18 trans_matrix(1, 3) = value.
y;
19 trans_matrix(2, 3) = value.
z;
23static mat4f x_rotation(
float angle) {
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);
33static mat4f y_rotation(
float angle) {
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);
43static mat4f z_rotation(
float angle) {
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);
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);
59 vec3 temp(axis * (1 - c));
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;
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;
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;
76static mat3f rotation(
const float &angle,
const vec2 &about =
vec2(0)) {
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);
84 return translate(about) * rot_matrix * translate(-about);
87static mat3f scaling(
const vec2 &value,
const vec2 &about =
vec2(0),
const float &angle_offset = 0) {
89 scale(0, 0) = value.
x;
90 scale(1, 1) = value.
y;
92 if (angle_offset != 0)
93 scale = rotation(angle_offset) * scale * rotation(-angle_offset);
95 scale = translate(about) * scale * translate(-about);
100static mat4f scaling3d(
const vec3 &value) {
102 scale(0, 0) = value.
x;
103 scale(1, 1) = value.
y;
104 scale(2, 2) = value.
z;
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;
121static mat3f reflection(
const float &m,
const float &c) {
122 float angle = std::atan(m);
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));
135 const vec3 z = (eye - center).normalize();
138 const vec3 c = -center;
146static mat4f persp(
const float &w,
const float &h,
const float &fovx,
float fovy = 0) {
147 float D2R = 3.14159265 / 180.0;
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,
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,
167static mat4f perspective(
float x,
float y,
float z,
float zvp) {
170 printf(
"DP is zero");
175 ret(0, 3) = x *
zvp / dp;
176 ret(1, 2) = -1 * y / dp;
177 ret(1, 3) = y *
zvp / dp;
187static mat4f oblique_projection(
float alpha,
float theta) {
188 float pi = 3.14159265;
189 if (alpha == 0 || alpha == 180 || alpha == 360)
193 float l1 = 1 / tan(alpha);
194 mat4f tranMatrix({1, 0, l1 * cosf(theta), 0,
195 0, 1, l1 * sinf(theta), 0,
A general matrix class for any size of matrix.
namespace that has all transformation matrices
static float dot(const vec3_T< float > &a, const vec3_T< float > &b)
static vec3_T< float > cross(const vec3_T< float > &a, const vec3_T< float > &b)
vec3_T< float > normalize()