FCT
载入中...
搜索中...
未找到
Matrix.h
浏览该文件的文档.
1
2#include "./Vec.h"
3#pragma once
4
5namespace FCT
6{
7
8 class Mat4
9 {
10 public:
11 float m[16];
12
14 {
15 identity();
16 }
17 Mat4(float m11, float m12, float m13, float m14,
18 float m21, float m22, float m23, float m24,
19 float m31, float m32, float m33, float m34,
20 float m41, float m42, float m43, float m44)
21 {
22 m[0] = m11;
23 m[1] = m12;
24 m[2] = m13;
25 m[3] = m14;
26 m[4] = m21;
27 m[5] = m22;
28 m[6] = m23;
29 m[7] = m24;
30 m[8] = m31;
31 m[9] = m32;
32 m[10] = m33;
33 m[11] = m34;
34 m[12] = m41;
35 m[13] = m42;
36 m[14] = m43;
37 m[15] = m44;
38 }
39
40 void identity()
41 {
42 *this = {
43 1, 0, 0, 0,
44 0, 1, 0, 0,
45 0, 0, 1, 0,
46 0, 0, 0, 1};
47 }
48 void translate(float x, float y)
49 {
50 m[3] += x;
51 m[7] += y;
52 }
53 void translate(float x, float y, float z)
54 {
55 Mat4 trans = {
56 1, 0, 0, x,
57 0, 1, 0, y,
58 0, 0, 1, z,
59 0, 0, 0, 1
60 };
61 *this = *this * trans;
62 }
63 void rotateX(float degrees)
64 {
65 float radians = degrees * 3.14159265f / 180.0f;
66 float c = cosf(radians);
67 float s = sinf(radians);
68
69 Mat4 rot = {
70 1, 0, 0, 0,
71 0, c, -s, 0,
72 0, s, c, 0,
73 0, 0, 0, 1};
74 *this = *this * rot;
75 }
76 static Mat4 CreateTranslation(float x, float y)
77 {
78 Mat4 ret;
79 ret.m[3] += x;
80 ret.m[7] += y;
81 return ret;
82 }
83 static Mat4 CreateScale(float scaleX, float scaleY)
84 {
85 Mat4 ret = {
86 scaleX, 0, 0, 0,
87 0, scaleY, 0, 0,
88 0, 0, 1, 0,
89 0, 0, 0, 1};
90 return ret;
91 }
92 static Mat4 Translate(float x, float y)
93 {
94 Mat4 ret;
95 ret.m[8] += x;
96 ret.m[9] += y;
97 return ret;
98 }
99 static Mat4 Translate(float x, float y, float z)
100 {
101 Mat4 ret = {
102 1, 0, 0, x,
103 0, 1, 0, y,
104 0, 0, 1, z,
105 0, 0, 0, 1
106 };
107 return ret;
108 }
109
110
111 static Mat4 Ortho(float left, float right, float bottom, float top, float zNear, float zFar)
112 {
113 return Mat4(
114 2.0f / (right - left), 0, 0, -(right + left) / (right - left),
115 0, 2.0f / (top - bottom), 0, -(top + bottom) / (top - bottom),
116 0, 0, 1.0f / (zFar - zNear), -zNear / (zFar - zNear),
117 0, 0, 0, 1
118 );
119 }
120 static Mat4 LookAt(const Vec3& eye, const Vec3& center, const Vec3& up)
121 {
122 Vec3 f = (center - eye).normalize(); //z
123 Vec3 s = f.cross(up).normalize(); //x
124 Vec3 u = s.cross(f);
125
126 Mat4 result = {
127 s.x, s.y, s.z, -s.dot(eye),
128 u.x, u.y, u.z, -u.dot(eye),
129 f.x, f.y, f.z, -f.dot(eye),
130 0.0f, 0.0f, 0.0f, 1.0f
131 };
132
133 return result;
134 }
135 static Mat4 Scale(float scaleX, float scaleY)
136 {
137 Mat4 ret = {
138 scaleX, 0, 0, 0,
139 0, scaleY, 0, 0,
140 0, 0, 1, 0,
141 0, 0, 0, 1};
142 return ret;
143 }
144 static Mat4 Scale(float scaleX, float scaleY,float scaleZ)
145 {
146 Mat4 ret = {
147 scaleX, 0, 0, 0,
148 0, scaleY, 0, 0,
149 0, 0, scaleZ, 0,
150 0, 0, 0, 1};
151 return ret;
152 }
153 static Mat4 Perspective(float fovx, float aspect, float zNear, float zFar)
154 {
155 float angle = fovx * 3.14159265f / 180.0f;
156 float h = tanf(angle / 2.0f);
157 return Mat4(
158 1 / h, 0, 0, 0,
159 0, aspect/ h, 0, 0,
160 0, 0, zFar / (zFar - zNear), -zFar * zNear / (zFar - zNear),
161 0, 0, 1 , 0
162 );
163 }
164 void rotateY(float degrees)
165 {
166 float radians = degrees * 3.14159265f / 180.0f;
167 float c = cosf(radians);
168 float s = sinf(radians);
169
170 Mat4 rot = {
171 c, 0, s, 0,
172 0, 1, 0, 0,
173 -s, 0, c, 0,
174 0, 0, 0, 1};
175 *this = *this * rot;
176 }
177 void rotateZ(float degrees)
178 {
179 float radians = degrees * 3.14159265f / 180.0f;
180 float c = cosf(radians);
181 float s = sinf(radians);
182
183 Mat4 rot = {
184 c, -s, 0, 0,
185 s, c, 0, 0,
186 0, 0, 1, 0,
187 0, 0, 0, 1};
188 *this = *this * rot;
189 }
190 Vec4 operator*(const Vec4& vec) const
191 {
192 return {
193 m[0] * vec.x + m[1] * vec.y + m[2] * vec.z + m[3] * vec.w, // 第1行
194 m[4] * vec.x + m[5] * vec.y + m[6] * vec.z + m[7] * vec.w, // 第2行
195 m[8] * vec.x + m[9] * vec.y + m[10] * vec.z + m[11] * vec.w, // 第3行
196 m[12] * vec.x + m[13] * vec.y + m[14] * vec.z + m[15] * vec.w // 第4行
197 };
198 }
199 void scale(float x, float y, float z)
200 {
201 Mat4 scl = {
202 x, 0, 0, 0,
203 0, y, 0, 0,
204 0, 0, z, 0,
205 0, 0, 0, 1};
206 *this = *this * scl;
207 }
208
209 Mat4 operator*(const Mat4 &rhs) const
210 {
211 Mat4 result;
212 for (int i = 0; i < 4; ++i)
213 {
214 for (int j = 0; j < 4; ++j)
215 {
216 result.m[i * 4 + j] = 0.0f;
217 for (int k = 0; k < 4; ++k)
218 {
219 result.m[i * 4 + j] += m[i * 4 + k] * rhs.m[k * 4 + j];
220 }
221 }
222 }
223 return result;
224 }
225 Mat4& operator*=(const Mat4 &rhs)
226 {
227 *this = *this * rhs;
228 return *this;
229 }
230 friend std::ostream& operator<<(std::ostream& os, const Mat4& mat);
231 };
232 inline std::ostream& operator<<(std::ostream& os, const Mat4& mat)
233 {
234 os << std::fixed << std::setprecision(4);
235 os << "Matrix4x4:\n";
236 for (int i = 0; i < 4; ++i)
237 {
238 os << "[ ";
239 for (int j = 0; j < 4; ++j)
240 {
241 os << std::setw(10) << mat.m[i * 4 + j];
242 if (j < 3) os << ", ";
243 }
244 os << " ]\n";
245 }
246 return os;
247 }
248
249 class Mat3 : public Mat4
250 {
251
252 };
253} // namespace FCT
void rotateZ(float degrees)
friend std::ostream & operator<<(std::ostream &os, const Mat4 &mat)
static Mat4 CreateTranslation(float x, float y)
static Mat4 Perspective(float fovx, float aspect, float zNear, float zFar)
void translate(float x, float y, float z)
Mat4(float m11, float m12, float m13, float m14, float m21, float m22, float m23, float m24, float m31, float m32, float m33, float m34, float m41, float m42, float m43, float m44)
void rotateX(float degrees)
static Mat4 LookAt(const Vec3 &eye, const Vec3 &center, const Vec3 &up)
static Mat4 Scale(float scaleX, float scaleY)
static Mat4 Scale(float scaleX, float scaleY, float scaleZ)
void scale(float x, float y, float z)
void translate(float x, float y)
float m[16]
void identity()
static Mat4 Translate(float x, float y, float z)
Mat4 & operator*=(const Mat4 &rhs)
static Mat4 Ortho(float left, float right, float bottom, float top, float zNear, float zFar)
Mat4 operator*(const Mat4 &rhs) const
Vec4 operator*(const Vec4 &vec) const
static Mat4 CreateScale(float scaleX, float scaleY)
void rotateY(float degrees)
static Mat4 Translate(float x, float y)
Vec3 normalize(const Vec3 &v)
定义 Vec.h:176
std::ostream & operator<<(std::ostream &os, const Mat4 &mat)
float x
定义 Vec.h:81
float dot(const Vec3 &rhs) const
定义 Vec.h:172
Vec3 cross(const Vec3 &other) const
定义 Vec.h:146
Vec3 normalize() const
定义 Vec.h:141
float z
定义 Vec.h:81
float y
定义 Vec.h:81
float z
定义 Vec.h:196
float y
定义 Vec.h:196
float w
定义 Vec.h:196
float x
定义 Vec.h:196