-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath_3d.h
More file actions
200 lines (150 loc) · 3.93 KB
/
math_3d.h
File metadata and controls
200 lines (150 loc) · 3.93 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
Copyright 2010 Etay Meiri
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MATH_3D_H
#define MATH_3D_H
#include <stdio.h>
#ifdef WIN32
#define _USE_MATH_DEFINES
#include <cmath>
#else
#define _USE_MATH_DEFINES
#include <cmath>
#endif
#define ToRadian(x) (float)(((x) * M_PI / 180.0f))
#define ToDegree(x) (float)(((x) * 180.0f / M_PI))
struct Vector2i
{
int x;
int y;
};
struct Vector2f
{
float x;
float y;
Vector2f()
{
}
Vector2f(float _x, float _y)
{
x = _x;
y = _y;
}
};
struct Vector3f
{
float x;
float y;
float z;
Vector3f()
{
}
Vector3f(float _x, float _y, float _z)
{
x = _x;
y = _y;
z = _z;
}
Vector3f& operator+=(const Vector3f& r)
{
x += r.x;
y += r.y;
z += r.z;
return *this;
}
Vector3f& operator-=(const Vector3f& r)
{
x -= r.x;
y -= r.y;
z -= r.z;
return *this;
}
Vector3f& operator*=(float f)
{
x *= f;
y *= f;
z *= f;
return *this;
}
Vector3f Cross(const Vector3f& v) const;
Vector3f& Normalize();
void Rotate(float Angle, const Vector3f& Axis);
void Print() const
{
printf("(%.02f, %.02f, %.02f", x, y, z);
}
};
inline Vector3f operator+(const Vector3f& l, const Vector3f& r)
{
Vector3f Ret(l.x + r.x,
l.y + r.y,
l.z + r.z);
return Ret;
}
inline Vector3f operator-(const Vector3f& l, const Vector3f& r)
{
Vector3f Ret(l.x - r.x,
l.y - r.y,
l.z - r.z);
return Ret;
}
inline Vector3f operator*(const Vector3f& l, float f)
{
Vector3f Ret(l.x * f,
l.y * f,
l.z * f);
return Ret;
}
class Matrix4f
{
public:
float m[4][4];
Matrix4f()
{
}
inline void InitIdentity()
{
m[0][0] = 1.0f; m[0][1] = 0.0f; m[0][2] = 0.0f; m[0][3] = 0.0f;
m[1][0] = 0.0f; m[1][1] = 1.0f; m[1][2] = 0.0f; m[1][3] = 0.0f;
m[2][0] = 0.0f; m[2][1] = 0.0f; m[2][2] = 1.0f; m[2][3] = 0.0f;
m[3][0] = 0.0f; m[3][1] = 0.0f; m[3][2] = 0.0f; m[3][3] = 1.0f;
}
inline Matrix4f operator*(const Matrix4f& Right) const
{
Matrix4f Ret;
for (unsigned int i = 0 ; i < 4 ; i++) {
for (unsigned int j = 0 ; j < 4 ; j++) {
Ret.m[i][j] = m[i][0] * Right.m[0][j] +
m[i][1] * Right.m[1][j] +
m[i][2] * Right.m[2][j] +
m[i][3] * Right.m[3][j];
}
}
return Ret;
}
void InitScaleTransform(float ScaleX, float ScaleY, float ScaleZ);
void InitRotateTransform(float RotateX, float RotateY, float RotateZ);
void InitTranslationTransform(float x, float y, float z);
void InitCameraTransform(const Vector3f& Target, const Vector3f& Up);
void InitPersProjTransform(float FOV, float Width, float Height, float zNear, float zFar);
};
struct Quaternion
{
float x, y, z, w;
Quaternion(float _x, float _y, float _z, float _w);
void Normalize();
Quaternion Conjugate();
};
Quaternion operator*(const Quaternion& l, const Quaternion& r);
Quaternion operator*(const Quaternion& q, const Vector3f& v);
#endif /* MATH_3D_H */