Simulant  21.12-194
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
mat4.h
1 #pragma once
2 
3 #include <array>
4 #include <cstring>
5 #include <cstdint>
6 
7 namespace smlt {
8 
9 struct Quaternion;
10 struct Vec3;
11 struct Vec4;
12 struct Plane;
13 struct Degrees;
14 
15 enum FrustumPlane {
16  FRUSTUM_PLANE_LEFT = 0,
17  FRUSTUM_PLANE_RIGHT,
18  FRUSTUM_PLANE_BOTTOM,
19  FRUSTUM_PLANE_TOP,
20  FRUSTUM_PLANE_NEAR,
21  FRUSTUM_PLANE_FAR,
22  FRUSTUM_PLANE_MAX
23 };
24 
25 struct Mat4 {
26 private:
27  float m[16];
28 
29 public:
30 
31  Mat4() {
32  memset(m, 0, sizeof(float) * 16);
33  m[0] = m[5] = m[10] = m[15] = 1.0f;
34  }
35 
36  Mat4(const Quaternion& rhs);
37  Mat4(const Quaternion& rot, const Vec3& trans, const Vec3& scale);
38 
39  Mat4 operator*(const Mat4& rhs) const {
40 
41  Mat4 result;
42  const float *m1 = &this->m[0], *m2 = &rhs.m[0];
43 
44  result.m[0] = m1[0] * m2[0] + m1[4] * m2[1] + m1[8] * m2[2] + m1[12] * m2[3];
45  result.m[1] = m1[1] * m2[0] + m1[5] * m2[1] + m1[9] * m2[2] + m1[13] * m2[3];
46  result.m[2] = m1[2] * m2[0] + m1[6] * m2[1] + m1[10] * m2[2] + m1[14] * m2[3];
47  result.m[3] = m1[3] * m2[0] + m1[7] * m2[1] + m1[11] * m2[2] + m1[15] * m2[3];
48 
49  result.m[4] = m1[0] * m2[4] + m1[4] * m2[5] + m1[8] * m2[6] + m1[12] * m2[7];
50  result.m[5] = m1[1] * m2[4] + m1[5] * m2[5] + m1[9] * m2[6] + m1[13] * m2[7];
51  result.m[6] = m1[2] * m2[4] + m1[6] * m2[5] + m1[10] * m2[6] + m1[14] * m2[7];
52  result.m[7] = m1[3] * m2[4] + m1[7] * m2[5] + m1[11] * m2[6] + m1[15] * m2[7];
53 
54  result.m[8] = m1[0] * m2[8] + m1[4] * m2[9] + m1[8] * m2[10] + m1[12] * m2[11];
55  result.m[9] = m1[1] * m2[8] + m1[5] * m2[9] + m1[9] * m2[10] + m1[13] * m2[11];
56  result.m[10] = m1[2] * m2[8] + m1[6] * m2[9] + m1[10] * m2[10] + m1[14] * m2[11];
57  result.m[11] = m1[3] * m2[8] + m1[7] * m2[9] + m1[11] * m2[10] + m1[15] * m2[11];
58 
59  result.m[12] = m1[0] * m2[12] + m1[4] * m2[13] + m1[8] * m2[14] + m1[12] * m2[15];
60  result.m[13] = m1[1] * m2[12] + m1[5] * m2[13] + m1[9] * m2[14] + m1[13] * m2[15];
61  result.m[14] = m1[2] * m2[12] + m1[6] * m2[13] + m1[10] * m2[14] + m1[14] * m2[15];
62  result.m[15] = m1[3] * m2[12] + m1[7] * m2[13] + m1[11] * m2[14] + m1[15] * m2[15];
63 
64  return result;
65  }
66 
67  Vec4 operator*(const Vec4& rhs) const;
68 
69  void extract_rotation_and_translation(Quaternion& rotation, Vec3& translation) const;
70 
71  static Mat4 as_rotation_x(const Degrees& angle);
72  static Mat4 as_rotation_y(const Degrees& angle);
73  static Mat4 as_rotation_z(const Degrees& angle);
74  static Mat4 as_look_at(const Vec3& eye, const Vec3& target, const Vec3& up);
75  static Mat4 as_scaling(float s);
76  static Mat4 from_pos_rot_scale(const Vec3& pos, const Quaternion& rot, const Vec3& scale);
77 
78  inline const float& operator[](const uint32_t index) const {
79  return m[index];
80  }
81 
82  inline float& operator[](const uint32_t index){
83  return m[index];
84  }
85 
86  inline const float& operator[](const int index) const {
87  return m[index];
88  }
89 
90  inline float& operator[](const int index) {
91  return m[index];
92  }
93 
94  static Mat4 as_translation(const Vec3& v);
95 
96  static Mat4 as_projection(const Degrees& fov, float aspect, float near, float far);
97 
98  static Mat4 as_orthographic(float left, float right, float bottom, float top, float zNear, float zFar);
99 
100  void inverse();
101 
102  Mat4 inversed() const {
103  Mat4 ret = *this;
104  ret.inverse();
105  return ret;
106  }
107 
108  Plane extract_plane(FrustumPlane plane) const;
109 
110  const float* data() const {
111  return &m[0];
112  }
113 
114 };
115 
116 
117 }
smlt::Mat4
Definition: mat4.h:25
smlt::Vec3
Definition: vec3.h:23
smlt::Quaternion
Definition: quaternion.h:24
smlt
Definition: animation.cpp:25
smlt::Degrees
Definition: degrees.h:7
smlt::Plane
Definition: plane.h:18
smlt::Vec4
Definition: vec4.h:11