载入中...
搜索中...
未找到
Mat4.cpp
浏览该文件的文档.
1#include "math/Mat4.h"
2#include "math/Vec2.h"
3#include "math/Vec3.h"
4
5#include "common/Exception.h"
6
7#include <glm/gtc/matrix_transform.hpp>
8#include <glm/gtc/type_ptr.hpp>
9
10namespace eve::math {
11
12Mat4::Mat4() : m_(1.f) {}
13Mat4::Mat4(const glm::mat4 &m) : m_(m) {}
14
15void Mat4::identity() { m_ = glm::mat4(1.f); }
16
17void Mat4::translate(float x, float y, float z) {
18 m_ = glm::translate(m_, glm::vec3(x, y, z));
19}
20
21void Mat4::rotateX(float radians) { m_ = glm::rotate(m_, radians, glm::vec3(1.f, 0.f, 0.f)); }
22void Mat4::rotateY(float radians) { m_ = glm::rotate(m_, radians, glm::vec3(0.f, 1.f, 0.f)); }
23void Mat4::rotateZ(float radians) { m_ = glm::rotate(m_, radians, glm::vec3(0.f, 0.f, 1.f)); }
24
25void Mat4::scale(float sx, float sy, float sz) {
26 m_ = glm::scale(m_, glm::vec3(sx, sy, sz));
27}
28
29void Mat4::multiply(const Mat4 *other) {
30 if (!other) throw eve::Exception("Mat4.multiply: other is null");
31 m_ = m_ * other->m_;
32}
33
34Mat4 *Mat4::multiplied(const Mat4 *other) const {
35 if (!other) throw eve::Exception("Mat4.multiplied: other is null");
36 return new Mat4(m_ * other->m_);
37}
38
40 if (!v) throw eve::Exception("Mat4.transformVec3: v is null");
41 glm::vec4 r = m_ * glm::vec4(v->getX(), v->getY(), v->getZ(), 1.f);
42 return new Vec3(r.x, r.y, r.z);
43}
44
46 if (!v) throw eve::Exception("Mat4.transformPoint2: v is null");
47 glm::vec4 r = m_ * glm::vec4(v->getX(), v->getY(), 0.f, 1.f);
48 return new Vec2(r.x, r.y);
49}
50
51float Mat4::get(int index) const {
52 if (index < 0 || index > 15) throw eve::Exception("Mat4.get: index must be 0..15");
53 return glm::value_ptr(m_)[index];
54}
55
56void Mat4::set(int index, float value) {
57 if (index < 0 || index > 15) throw eve::Exception("Mat4.set: index must be 0..15");
58 glm::value_ptr(m_)[index] = value;
59}
60
61Mat4 *Mat4::clone() const { return new Mat4(m_); }
62
63} // namespace eve::math
std::string value
int y
Definition Grass.cpp:135
int z
Definition Grass.cpp:135
int x
Definition Grass.cpp:135
int v
float m[16]
Column-major 4x4 matrix wrapping glm::mat4.
Definition Mat4.h:11
Vec3 * transformVec3(const Vec3 *v) const
Definition Mat4.cpp:39
Vec2 * transformPoint2(const Vec2 *v) const
Definition Mat4.cpp:45
void translate(float x, float y, float z)
Definition Mat4.cpp:17
float get(int index) const
Column-major element 0..15.
Definition Mat4.cpp:51
void identity()
Definition Mat4.cpp:15
void scale(float sx, float sy, float sz)
Definition Mat4.cpp:25
void multiply(const Mat4 *other)
Definition Mat4.cpp:29
Mat4 * clone() const
Definition Mat4.cpp:61
void rotateX(float radians)
Definition Mat4.cpp:21
void rotateZ(float radians)
Definition Mat4.cpp:23
Mat4 * multiplied(const Mat4 *other) const
Definition Mat4.cpp:34
void set(int index, float value)
Definition Mat4.cpp:56
void rotateY(float radians)
Definition Mat4.cpp:22
2D float vector (script-facing math module value).
Definition Vec2.h:8
3D float vector (script-facing math module value).
Definition Vec3.h:8