forked from BennyQBD/ModernOpenGLTutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
math3d.h
179 lines (148 loc) · 3.95 KB
/
math3d.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#ifndef MATH3D_INCLUDED_H
#define MATH3D_INCLUDED_H
#include <glm.hpp>
/*
namespace Math3D
{
static const double PI = 3.1415926535897932384626433832795;
static const double PIOVER2 = PI / 2.0;
static const double PIOVER180 = PI / 180.0;
static inline double ToRadians(double x) { return PIOVER180 * x; }
static inline double ToDegrees(double x) { return 180.0 * x / PI; }
};
struct Vector3f
{
public:
Vector3f(float x = 0.0f, float y = 0.0f, float z = 0.0f)
{
data.x = x;
data.y = y;
data.z = z;
}
inline float GetX() const { return data.x; }
inline float GetY() const { return data.y; }
inline float GetZ() const { return data.z; }
inline void SetX(float x) { data.x = x; }
inline void SetY(float y) { data.y = y; }
inline void SetZ(float z) { data.z = z; }
inline glm::vec3& GetRawVector() const { return glm::vec3(data); }
inline float Length()
{
return sqrtf(this->Dot(*this));
}
inline Vector3f Normalized()
{
return *this / Length();
}
inline float Dot(const Vector3f& r) const
{
return glm::dot(data, r.GetRawVector());
}
inline Vector3f Cross(const Vector3f& r) const
{
return Vector3f(glm::cross(data, r.GetRawVector()));
}
inline Vector3f Rotate(const Vector3f& axis, float angle) const
{
float sinAngle = sinf(angle);
float cosAngle = cosf(angle);
return this->Cross(axis * sinAngle) + //Rotation on local X
(*this * cosAngle) + //Rotation on local Z
axis * this->Dot(axis * (1 - cosAngle)); //Rotation on local Y
}
inline void operator+=(const Vector3f& r)
{
data.x += r.GetX();
data.y += r.GetY();
data.z += r.GetZ();
}
inline Vector3f operator+(const Vector3f& r) const
{
return Vector3f(data.x + r.GetX(),
data.y + r.GetY(),
data.z + r.GetZ());
}
inline Vector3f operator*(float r) const
{
return Vector3f(data.x * r,
data.y * r,
data.z * r);
}
inline Vector3f operator/(float r) const
{
return Vector3f(data.x / r,
data.y / r,
data.z / r);
}
private:
Vector3f(glm::vec3& data)
{
this->data.x = data.x;
this->data.y = data.y;
this->data.z = data.z;
}
glm::vec3 data;
};
struct Vector2f
{
public:
Vector2f(float x = 0.0f, float y = 0.0f)
{
data.x = x;
data.y = y;
}
inline float GetX() const { return data.x; }
inline float GetY() const { return data.y; }
inline void SetX(float x) { data.x = x; }
inline void SetY(float y) { data.y = y; }
private:
DirectX::XMFLOAT2 data;
};
struct Matrix4f
{
public:
static Matrix4f InitTranslation(const Vector3f& translation)
{
return Matrix4f(DirectX::XMMatrixTranslation(translation.GetX(), translation.GetY(), translation.GetZ()));
}
static Matrix4f InitRotation(const Vector3f& eulerAngles)
{
return Matrix4f(DirectX::XMMatrixRotationRollPitchYaw(eulerAngles.GetX(), eulerAngles.GetY(), eulerAngles.GetZ()));
}
static Matrix4f InitScale(const Vector3f& scale)
{
return Matrix4f(DirectX::XMMatrixScaling(scale.GetX(), scale.GetY(), scale.GetZ()));
}
static Matrix4f InitLookTo(const Vector3f& eye, const Vector3f& direction, const Vector3f& up)
{
return Matrix4f(DirectX::XMMatrixLookToLH(eye.GetDXVector(), direction.GetDXVector(), up.GetDXVector()));
}
static Matrix4f InitPerspective(float angle, float aspect, float zNear, float zFar)
{
return Matrix4f(DirectX::XMMatrixPerspectiveFovLH(angle, aspect, zNear, zFar));
}
Matrix4f()
{
m_Data = DirectX::XMMatrixIdentity();
}
Matrix4f Inverse() const
{
return Matrix4f(DirectX::XMMatrixInverse(NULL, m_Data));
}
Matrix4f Transpose() const
{
return Matrix4f(DirectX::XMMatrixTranspose(m_Data));
}
inline Matrix4f operator*(const Matrix4f& r) const
{
return Matrix4f(m_Data * r.m_Data);
}
private:
Matrix4f(const DirectX::XMMATRIX& data)
{
m_Data = data;
}
DirectX::XMMATRIX m_Data;
};
*/
#endif