Библиотека Math3D
Библиотека Math3D содержит основные классы для выполнения математических операций при построении 3D графики.
Список файлов
Math3D.hpp
Vector2.hpp
Vector3.hpp
Vector4.hpp
Matrix4.hpp
Quaternion.hpp
Классы
Vector2
Vector3
Vector4
Matrix4
Quaternion
Vector2
Конструкторы
Vector2(float s)
Vector2(float x, float y)
Vector2()
Методы
float* getPtr() Указатель на данные
const float* getPtr() const
Указатель на данные float length() const
Длинна вектора
Статические методы
float length(const Vector2& v)
Длинна вектора.
float distance(const Vector2& v1, const Vector2& v2)
Расстояние между двумя векторами.
float dot(const Vector2& v1, const Vector2& v2)
Скалярное произведение иначе dot product.
Vector2 normalize(const Vector2& v)
Нормализация вектора. Приводит вектор к единичной длине.
Vector2 lerp(const Vector2& v1, const Vector2& v2, float a)
Линейная интерполяция между двумя векторами. Параметр a - положение, значение от 0 до 1.
Vector3
Конструкторы
Vector3(float x, const Vector2& v)
Vector3(const Vector2& v, float z = 0.0f)
Vector3(float s)
Vector3(float x, float y, float z = 0.0f)
Vector3()
Методы
float* getPtr() Указатель на данные
const float* getPtr() const
Указатель на данные float length() const
Длинна вектора.
Vector2 toVector2() const
operator Vector2() const
Конвертация Vector3 в Vector2. Отбрасывает элемент z.
Статические методы
float length(const Vector3& v)
Длинна вектора.
float distance(const Vector3& v1, const Vector3& v2)
Расстояние между двумя векторами.
float dot(const Vector3& v1, const Vector3& v2)
Скалярное произведение.
Vector3 cross(const Vector3& v1, const Vector3& v2)
Векторное произведение.
Vector3 normalize(const Vector3& v)
Нормализация.
Vector3 lerp(const Vector3& v1, const Vector3& v2, float a)
Линейная интерполяция. Параметр a - дистанция, значение от 0 до 1.
Vector4
Конструкторы
Vector4(const Vector2& v1, const Vector2& v2)
Vector4(float x, float y, const Vector2& v)
Vector4(float x, const Vector2& v, float w = 1.0f)
Vector4(const Vector2& v, float z = 0.0f, float w = 1.0f)
Vector4(float x, const Vector3& v)
Vector4(const Vector3& v, float w = 1.0f)
Vector4(float s)
Vector4(float x, float y, float z = 0.0f, float w = 1.0f)
Vector4()
Методы
float* getPtr() Указатель на данные
const float* getPtr() const
Указатель на данные float length() const
Длинна вектора.
Vector2 toVector2() constoperator Vector2() const
Конвертация Vector4 в Vector2. Отбрасывает элемент z, также умножает x и у на значение w.
Vector3 toVector3() const
operator Vector3() const
Конвертация Vector4 в Vector3. Возвращает x, у, z умноженные на w.
Статические методы
float length(const Vector4& v)
Длинна вектора.
float distance(const Vector4& v1, const Vector4& v2)
Расстояние между двумя векторами.
float dot(const Vector4& v1, const Vector4& v2)
Скалярное произведение.
Vector4 normalize(const Vector4& v)
Нормализация.
Vector4 lerp(const Vector4& v1, const Vector4& v2, float a)
Линейная интерполяция, параметр a - дистанция, значение от 0 до 1.
Matrix4
Конструкторы
Matrix4(float s)
Matrix4(аloat m00, float m01, float m02, float m03, float m04, float m05, float m06, float m07,float m08, float m09, float m10, float m11, float m12, float m13, float m14, float m15)
Matrix4()
Методы
float* getPtr() Указатель на данные
const float* getPtr() const
Указатель на данные Matrix4 transpose() const Транспонирование матрицы (отражение вдоль главной диагонали)
void loadIdentity()
Установит матрицу в единичное значение.
void rotate(float angle, const Vector3& vector)
Поворот вокруг вектора, угол задается в радианах.
void rotate(float angle, float x, float y, float z)
Поворот вокруг вектора, угол задается в радианах.
void rotateX(float angle)
Поворот вокруг оси X.
void rotateY(float angle)
Поворот вокруг оси Y.
void rotateZ(float angle)
Поворот вокруг оси Z.
void rotateEuler(float pitch, float yaw, float roll)
Поворот матрицы на углы Эйлера.
void scale(const Vector3& v)
Масштабирование.
void scale(float x, float y, float z)
Масштабирование.
void translate(const Vector3& v)
Перенос.
void translate(float x, float y, float z)
Перенос.
Статические методы
Matrix4 transpose(const Matrix4& m)
Транспонирование матрицы (отражение вдоль главной диагонали)
Matrix4 inverse(const Matrix4& m)
Инверсия. Возвращает матрицу обратную данной.
void loadIdentity(const Matrix4& m) { Установит матрицу в единичное значение. Также можно использовать скалярный конструктор со значением 1.0f (m = Matrix4(1.0f)).
Matrix4 rotate(const Matrix4& m, float angle, const Vector3& vector)
Поворот вокруг вектора, угол задается в радианах.
Matrix4 rotate(const Matrix4& m, float angle, float x, float y, float z)
Поворот вокруг вектора, угол задается в радианах.
Matrix4 rotateX(const Matrix4& m, float angle)
Поворот вокруг оси X.
Matrix4 rotateY(const Matrix4& m, float angle)
Поворот вокруг оси Y.
Matrix4 rotateZ(const Matrix4& m, float angle)
Поворот вокруг оси Z.
Matrix4 scale(const Matrix4& m, const Vector3& v)
Масштабирование.
Matrix4 scale(const Matrix4& m, float x, float y , float z)
Масштабирование.
Matrix4 translate(const Matrix4& m, const Vector3& v)
Перенос.
Matrix4 translate(const Matrix4& m, float x, float y, float z)
Перенос.
Matrix4 EulerToMatrix4(float pitch, float yaw, float roll)
Строит матрицу поворота на основе углов Эйлера.
Matrix4 EulerToMatrix4(const Vector3& eulerAngles)
Строит матрицу поворота на основе углов Эйлера.
Matrix4 ortho(float left, float right, float bottom, float top, float near, float far)
Возвращает матрицу орфографической проекции.
tx = - (right + left) / (right - left)
ty = - (top + bottom) / (top - bottom)
tz = - (far + near) / (far - near)
| -2 / (right - left) | 0 | 0 | tx |
| 0 | -2 / (top - bottom) | 0 | ty |
| 0 | 0 | -2 / (far - near) | tz |
| 0 | 0 | 0 | 1 |
Matrix4 frustum(float left, float right, float bottom, float top, float near, float far)
Возвращает перспективную матрицу .
a = (right + left) / (right - left)
b = (top + bottom) / (top - bottom)
c = - (far + near) / (far - near)
d = - (2 * near * far) / (far - near)
| (2 * near) / (right - left) | 0 | a | 0 |
| 0 | (2 * near) / (top - bottom) | b | 0 |
| 0 | 0 | c | d |
| 0 | 0 | -1 | 0 |
Matrix4 perspective(float fovy, float aspect, float near, float far)
Возвращает перспективную матрицу на основе угла обзора - fovy (радианы) и соотношения ширины к высоте - aspect.
f = cotangent(fovy / 2)
| f / aspect | 0 | 0 | 0 |
| 0 | f | 0 | 0 |
| 0 | 0 | (far + near) / (near - far) | (2 * far * near) / (near - far) |
| 0 | 0 | -1 | 0 |
Matrix4 lookAt(const Vector3& eye, const Vector3& center, const Vector3& up)
Возвращает матрицу камеры.
f = normalize(center - eye) s = normalize(cross(f, up)) u = cross(s, f)
| s.x | s.y | s.z | -dot(s, eye) |
| u.x | u.y | u.z | -dot(u, eye) |
| -f.x | -f.y | -f.z | dot(f, eye) |
| 0 | 0 | 0 | 1 |
const Matrix4 identityMatrix;
Матрица с единичным значением, применяется как способ установки матрицы в единичное значение m = Matrix4::identityMatrix;
| 1.0f | 0.0f | 0.0f | 0.0f |
| 0.0f | 1.0f | 0.0f | 0.0f |
| 0.0f | 0.0f | 1.0f | 0.0f |
| 0.0f | 0.0f | 0.0f | 1.0f |
Quaternion
Конструкторы
Quaternion(float pitch, float yaw, float roll)
Quaternion(const Vector3& eulerAngles)
Quaternion(float w, float x = 0.0f, float y = 0.0f, float z = 0.0f)
Quaternion()
Методы
float* getPtr() Указатель на данные
const float* getPtr() const
Указатель на данные float length() const
Длина.
Matrix4 toMatrix4() const
Конвертирует кватернион в матрицу поворота.
void rotate(float angle, const Vector3& vector)
Поворот вокруг вектора
void rotate(Quaternion q, float angle, float x, float y, float z)
Поворот вокруг вектора
Статические методы
Vector3 toEuler() const
Конвертирует кватернион в углы Эйлера
float getPitch() const
Вернет pitch угол Эйлера
float getYaw() const
Вернет pitch угол Эйлера
float getRoll() const
Вернет pitch угол Эйлера
float dot(const Quaternion& q1, const Quaternion q2)
Скалярное произведение.
Quaternion cross(const Quaternion& q1, const Quaternion& q2)
Векторное произведение.
float length(const Quaternion& q)
Длина.
Quaternion normalize(const Quaternion& q)
Нормализация.
Quaternion inverse(const Quaternion& q)
Инверсия.
Quaternion slerp(const Quaternion& q1, const Quaternion& q2, float a)
Сферическая линейная интерполяция кватерниона.
Quaternion EulerToQuaternion(float pitch, float yaw, float roll)
Возвращает кватернион повернутый на углы Эйлера
Quaternion EulerToQuaternion(const Vector3& eulerAngles)
Возвращает кватернион повернутый на углы Эйлера
Quaternion rotate(const Quaternion& q, float angle, const Vector3& vector)
Поворот вокруг вектора
Quaternion rotate(const Quaternion& q, float angle, float x, float y, float z)
Поворот вокруг вектора