drm: add matrix
This commit is contained in:
parent
b2d084eb9e
commit
5716b2bc24
1
.gitignore
vendored
1
.gitignore
vendored
@ -9,3 +9,4 @@
|
|||||||
*.mod
|
*.mod
|
||||||
*.mod.*
|
*.mod.*
|
||||||
*.ko
|
*.ko
|
||||||
|
*.gch
|
||||||
@ -1,14 +1,16 @@
|
|||||||
OPT = -O0
|
OPT = -O0
|
||||||
|
|
||||||
CFLAGS += -g
|
CFLAGS += -g
|
||||||
CFLAGS += -Wall -Werror -Wfatal-errors -Wno-error=unused-variable
|
CFLAGS += -Wall -Werror -Wfatal-errors -Wno-error=unused-variable -Wno-narrowing
|
||||||
CFLAGS += $(shell pkg-config --cflags libdrm)
|
|
||||||
|
|
||||||
LDFLAGS += $(shell pkg-config --libs libdrm) -lm
|
LDFLAGS += $(shell pkg-config --cflags --libs libdrm) -lm
|
||||||
|
|
||||||
%: %.c
|
%: %.c
|
||||||
$(CC) $(ARCH) $(CFLAGS) $(LDFLAGS) $(OPT) $< -o $@
|
$(CC) $(ARCH) $(CFLAGS) $(LDFLAGS) $(OPT) $< -o $@
|
||||||
|
|
||||||
|
%: %.cpp
|
||||||
|
$(CXX) $(ARCH) $(CFLAGS) $(LDFLAGS) $(OPT) $< -o $@
|
||||||
|
|
||||||
%.vs.inc: %.vs.asm
|
%.vs.inc: %.vs.asm
|
||||||
PYTHONPATH=../regs/ python -m assembler.vs $< > $@
|
PYTHONPATH=../regs/ python -m assembler.vs $< > $@
|
||||||
|
|
||||||
|
|||||||
15
drm/math/float_types.hpp
Normal file
15
drm/math/float_types.hpp
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "vec2.hpp"
|
||||||
|
#include "vec3.hpp"
|
||||||
|
#include "vec4.hpp"
|
||||||
|
#include "mat2x2.hpp"
|
||||||
|
#include "mat3x3.hpp"
|
||||||
|
#include "mat4x4.hpp"
|
||||||
|
|
||||||
|
using vec2 = vec<2, float>;
|
||||||
|
using vec3 = vec<3, float>;
|
||||||
|
using vec4 = vec<4, float>;
|
||||||
|
using mat2x2 = mat<2, 2, float>;
|
||||||
|
using mat3x3 = mat<3, 3, float>;
|
||||||
|
using mat4x4 = mat<4, 4, float>;
|
||||||
26
drm/math/mat.hpp
Normal file
26
drm/math/mat.hpp
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
template <int R, int C, typename T>
|
||||||
|
struct mat;
|
||||||
|
|
||||||
|
template <int R, int C, typename T>
|
||||||
|
inline constexpr typename mat<R, C, T>::col_type
|
||||||
|
col(mat<R, C, T> const& m, int c)
|
||||||
|
{
|
||||||
|
typename mat<R, C, T>::col_type v;
|
||||||
|
for (int r = 0; r < R; r++) {
|
||||||
|
v[r] = m[r][c];
|
||||||
|
}
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <int R, typename T>
|
||||||
|
inline constexpr vec<3, T>
|
||||||
|
col(mat<R, 4, T> const& m, int c)
|
||||||
|
{
|
||||||
|
vec<3, T> v;
|
||||||
|
for (int r = 0; r < 3; r++) {
|
||||||
|
v[r] = m[r][c];
|
||||||
|
}
|
||||||
|
return v;
|
||||||
|
}
|
||||||
112
drm/math/mat2x2.hpp
Normal file
112
drm/math/mat2x2.hpp
Normal file
@ -0,0 +1,112 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <utility>
|
||||||
|
#include "vec2.hpp"
|
||||||
|
#include "mat.hpp"
|
||||||
|
|
||||||
|
//
|
||||||
|
// mat2x2
|
||||||
|
//
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct mat<2, 2, T>
|
||||||
|
{
|
||||||
|
typedef vec<2, T> row_type;
|
||||||
|
typedef vec<2, T> col_type;
|
||||||
|
|
||||||
|
private:
|
||||||
|
row_type value[2];
|
||||||
|
|
||||||
|
public:
|
||||||
|
inline constexpr mat();
|
||||||
|
|
||||||
|
inline constexpr mat
|
||||||
|
(
|
||||||
|
T const& a00, T const& a01,
|
||||||
|
T const& a10, T const& a11
|
||||||
|
);
|
||||||
|
|
||||||
|
inline static constexpr int length() { return 4; }
|
||||||
|
|
||||||
|
inline constexpr typename mat<2, 2, T>::row_type &
|
||||||
|
operator[](int i);
|
||||||
|
|
||||||
|
inline constexpr typename mat<2, 2, T>::row_type const &
|
||||||
|
operator[](int i) const;
|
||||||
|
|
||||||
|
void operator=(const mat<2, 2, T>&) = delete;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline constexpr mat<2, 2, T>::mat()
|
||||||
|
: value{std::move(row_type(1, 0)),
|
||||||
|
std::move(row_type(0, 1))}
|
||||||
|
{ }
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline constexpr mat<2, 2, T>::mat
|
||||||
|
(
|
||||||
|
T const& a00, T const& a01,
|
||||||
|
T const& a10, T const& a11
|
||||||
|
)
|
||||||
|
: value{std::move(row_type(a00, a01)),
|
||||||
|
std::move(row_type(a10, a11))}
|
||||||
|
{ }
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr typename mat<2, 2, T>::row_type &
|
||||||
|
mat<2, 2, T>::operator[](int i)
|
||||||
|
{
|
||||||
|
return value[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr typename mat<2, 2, T>::row_type const &
|
||||||
|
mat<2, 2, T>::operator[](int i) const
|
||||||
|
{
|
||||||
|
return value[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline constexpr mat<2, 2, T> operator*(mat<2, 2, T> const& m1, mat<2, 2, T> const& m2)
|
||||||
|
{
|
||||||
|
#define c(i, j) ( \
|
||||||
|
m1[i][0] * m2[0][j] \
|
||||||
|
+ m1[i][1] * m2[1][j])
|
||||||
|
|
||||||
|
return mat<2, 2, T>(c(0,0), c(0,1),
|
||||||
|
c(1,0), c(1,1));
|
||||||
|
#undef c
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline constexpr typename mat<2, 2, T>::row_type operator*
|
||||||
|
(
|
||||||
|
mat<2, 2, T> const& m,
|
||||||
|
typename mat<2, 2, T>::col_type const& v
|
||||||
|
)
|
||||||
|
{
|
||||||
|
#define c(i) ( \
|
||||||
|
m[i][0] * v[0] \
|
||||||
|
+ m[i][1] * v[1])
|
||||||
|
|
||||||
|
return typename mat<2, 2, T>::row_type(c(0), c(1));
|
||||||
|
#undef c
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline constexpr mat<2, 2, T> transpose(mat<2, 2, T> const& m)
|
||||||
|
{
|
||||||
|
return mat<2, 2, T>(
|
||||||
|
m[0][0], m[1][0],
|
||||||
|
m[0][1], m[1][1]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline constexpr float determinant(mat<2, 2, T> const& a)
|
||||||
|
{
|
||||||
|
return a[0][0] * a[1][1] - a[0][1] * a[1][0];
|
||||||
|
}
|
||||||
201
drm/math/mat3x3.hpp
Normal file
201
drm/math/mat3x3.hpp
Normal file
@ -0,0 +1,201 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <utility>
|
||||||
|
#include "vec3.hpp"
|
||||||
|
#include "mat.hpp"
|
||||||
|
|
||||||
|
//
|
||||||
|
// mat3x3
|
||||||
|
//
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct mat<3, 3, T>
|
||||||
|
{
|
||||||
|
typedef vec<3, T> row_type;
|
||||||
|
typedef vec<3, T> col_type;
|
||||||
|
|
||||||
|
private:
|
||||||
|
row_type value[3];
|
||||||
|
|
||||||
|
public:
|
||||||
|
inline constexpr mat();
|
||||||
|
|
||||||
|
inline constexpr mat
|
||||||
|
(
|
||||||
|
T const& a00, T const& a01, T const& a02,
|
||||||
|
T const& a10, T const& a11, T const& a12,
|
||||||
|
T const& a20, T const& a21, T const& a22
|
||||||
|
);
|
||||||
|
|
||||||
|
inline static constexpr int length() { return 3; }
|
||||||
|
|
||||||
|
inline constexpr typename mat<3, 3, T>::row_type &
|
||||||
|
operator[](int i);
|
||||||
|
|
||||||
|
inline constexpr typename mat<3, 3, T>::row_type const &
|
||||||
|
operator[](int i) const;
|
||||||
|
|
||||||
|
//void operator=(const mat<3, 3, T>&) = delete;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline constexpr mat<3, 3, T>::mat()
|
||||||
|
: value{std::move(row_type(1, 0, 0)),
|
||||||
|
std::move(row_type(0, 1, 0)),
|
||||||
|
std::move(row_type(0, 0, 1))}
|
||||||
|
{ }
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline constexpr mat<3, 3, T>::mat
|
||||||
|
(
|
||||||
|
T const& a00, T const& a01, T const& a02,
|
||||||
|
T const& a10, T const& a11, T const& a12,
|
||||||
|
T const& a20, T const& a21, T const& a22
|
||||||
|
)
|
||||||
|
: value{std::move(row_type(a00, a01, a02)),
|
||||||
|
std::move(row_type(a10, a11, a12)),
|
||||||
|
std::move(row_type(a20, a21, a22))}
|
||||||
|
{ }
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr typename mat<3, 3, T>::row_type &
|
||||||
|
mat<3, 3, T>::operator[](int i)
|
||||||
|
{
|
||||||
|
return value[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr typename mat<3, 3, T>::row_type const &
|
||||||
|
mat<3, 3, T>::operator[](int i) const
|
||||||
|
{
|
||||||
|
return value[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline constexpr mat<3, 3, T> operator+(mat<3, 3, T> const& m1, mat<3, 3, T> const& m2)
|
||||||
|
{
|
||||||
|
#define c(i, j) ( m1[i][j] + m2[i][j] )
|
||||||
|
|
||||||
|
return mat<3, 3, T>(c(0,0), c(0,1), c(0,2),
|
||||||
|
c(1,0), c(1,1), c(1,2),
|
||||||
|
c(2,0), c(2,1), c(2,2));
|
||||||
|
#undef c
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline constexpr mat<3, 3, T> operator*(mat<3, 3, T> const& m1, mat<3, 3, T> const& m2)
|
||||||
|
{
|
||||||
|
#define c(i, j) ( \
|
||||||
|
m1[i][0] * m2[0][j] \
|
||||||
|
+ m1[i][1] * m2[1][j] \
|
||||||
|
+ m1[i][2] * m2[2][j] )
|
||||||
|
|
||||||
|
return mat<3, 3, T>(c(0,0), c(0,1), c(0,2),
|
||||||
|
c(1,0), c(1,1), c(1,2),
|
||||||
|
c(2,0), c(2,1), c(2,2));
|
||||||
|
#undef c
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline constexpr mat<3, 3, T> operator*(mat<3, 3, T> const& m1, float s)
|
||||||
|
{
|
||||||
|
#define c(i, j) ( m1[i][j] * s )
|
||||||
|
|
||||||
|
return mat<3, 3, T>(c(0,0), c(0,1), c(0,2),
|
||||||
|
c(1,0), c(1,1), c(1,2),
|
||||||
|
c(2,0), c(2,1), c(2,2));
|
||||||
|
#undef c
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline constexpr typename mat<3, 3, T>::row_type operator*
|
||||||
|
(
|
||||||
|
mat<3, 3, T> const& m,
|
||||||
|
typename mat<3, 3, T>::col_type const& v
|
||||||
|
)
|
||||||
|
{
|
||||||
|
#define c(i) ( \
|
||||||
|
m[i][0] * v[0] \
|
||||||
|
+ m[i][1] * v[1] \
|
||||||
|
+ m[i][2] * v[2] )
|
||||||
|
|
||||||
|
return typename mat<3, 3, T>::row_type(c(0), c(1), c(2));
|
||||||
|
#undef c
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline constexpr mat<3, 3, T> transpose(mat<3, 3, T> const& m)
|
||||||
|
{
|
||||||
|
return mat<3, 3, T>(
|
||||||
|
m[0][0], m[1][0], m[2][0],
|
||||||
|
m[0][1], m[1][1], m[2][1],
|
||||||
|
m[0][2], m[1][2], m[2][2]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline constexpr mat<2, 2, T> submatrix(mat<3, 3, T> const& a, int r, int c)
|
||||||
|
{
|
||||||
|
mat<2, 2, T> b;
|
||||||
|
int row2 = 0;
|
||||||
|
for (int row3 = 0; row3 < 3; row3++) {
|
||||||
|
if (row3 == r) continue;
|
||||||
|
int col2 = 0;
|
||||||
|
for (int col3 = 0; col3 < 3; col3++) {
|
||||||
|
if (col3 == c) continue;
|
||||||
|
b[row2][col2] = a[row3][col3];
|
||||||
|
col2++;
|
||||||
|
}
|
||||||
|
row2++;
|
||||||
|
}
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline constexpr float minor(mat<3, 3, T> const& a, int r, int c)
|
||||||
|
{
|
||||||
|
mat<2, 2, T> s = submatrix(a, r, c);
|
||||||
|
float ret = determinant(s);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline constexpr float cofactor(mat<3, 3, T> const& a, int r, int c)
|
||||||
|
{
|
||||||
|
float m = minor(a, r, c);
|
||||||
|
if ((r + c) & 1)
|
||||||
|
return -m;
|
||||||
|
else
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline constexpr float determinant(mat<3, 3, T> const& a)
|
||||||
|
{
|
||||||
|
float f0 = cofactor(a, 0, 0);
|
||||||
|
float f1 = cofactor(a, 0, 1);
|
||||||
|
float f2 = cofactor(a, 0, 2);
|
||||||
|
return
|
||||||
|
a[0][0] * f0 +
|
||||||
|
a[0][1] * f1 +
|
||||||
|
a[0][2] * f2;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline constexpr mat<3, 3, T> inverse(mat<3, 3, T> const& a)
|
||||||
|
{
|
||||||
|
mat<3, 3, T> m;
|
||||||
|
float idet = 1.0f / determinant(a);
|
||||||
|
m[0][0] = cofactor(a, 0, 0) * idet;
|
||||||
|
m[1][0] = cofactor(a, 0, 1) * idet;
|
||||||
|
m[2][0] = cofactor(a, 0, 2) * idet;
|
||||||
|
m[0][1] = cofactor(a, 1, 0) * idet;
|
||||||
|
m[1][1] = cofactor(a, 1, 1) * idet;
|
||||||
|
m[2][1] = cofactor(a, 1, 2) * idet;
|
||||||
|
m[0][2] = cofactor(a, 2, 0) * idet;
|
||||||
|
m[1][2] = cofactor(a, 2, 1) * idet;
|
||||||
|
m[2][2] = cofactor(a, 2, 2) * idet;
|
||||||
|
return m;
|
||||||
|
}
|
||||||
247
drm/math/mat4x4.hpp
Normal file
247
drm/math/mat4x4.hpp
Normal file
@ -0,0 +1,247 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <utility>
|
||||||
|
#include "vec4.hpp"
|
||||||
|
#include "mat.hpp"
|
||||||
|
|
||||||
|
//
|
||||||
|
// mat4x4
|
||||||
|
//
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct mat<4, 4, T>
|
||||||
|
{
|
||||||
|
typedef vec<4, T> row_type;
|
||||||
|
typedef vec<4, T> col_type;
|
||||||
|
|
||||||
|
private:
|
||||||
|
row_type value[4];
|
||||||
|
|
||||||
|
public:
|
||||||
|
inline constexpr mat();
|
||||||
|
|
||||||
|
inline constexpr mat
|
||||||
|
(
|
||||||
|
T const& a00, T const& a01, T const& a02, T const& a03,
|
||||||
|
T const& a10, T const& a11, T const& a12, T const& a13,
|
||||||
|
T const& a20, T const& a21, T const& a22, T const& a23,
|
||||||
|
T const& a30, T const& a31, T const& a32, T const& a33
|
||||||
|
);
|
||||||
|
|
||||||
|
inline static constexpr int length() { return 4; }
|
||||||
|
|
||||||
|
inline constexpr typename mat<4, 4, T>::row_type &
|
||||||
|
operator[](int i);
|
||||||
|
|
||||||
|
inline constexpr typename mat<4, 4, T>::row_type const &
|
||||||
|
operator[](int i) const;
|
||||||
|
|
||||||
|
//void operator=(const mat<4, 4, T>&) = delete;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline constexpr mat<4, 4, T>::mat()
|
||||||
|
: value{std::move(row_type(1, 0, 0, 0)),
|
||||||
|
std::move(row_type(0, 1, 0, 0)),
|
||||||
|
std::move(row_type(0, 0, 1, 0)),
|
||||||
|
std::move(row_type(0, 0, 0, 1))}
|
||||||
|
{ }
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline constexpr mat<4, 4, T>::mat
|
||||||
|
(
|
||||||
|
T const& a00, T const& a01, T const& a02, T const& a03,
|
||||||
|
T const& a10, T const& a11, T const& a12, T const& a13,
|
||||||
|
T const& a20, T const& a21, T const& a22, T const& a23,
|
||||||
|
T const& a30, T const& a31, T const& a32, T const& a33
|
||||||
|
)
|
||||||
|
: value{std::move(row_type(a00, a01, a02, a03)),
|
||||||
|
std::move(row_type(a10, a11, a12, a13)),
|
||||||
|
std::move(row_type(a20, a21, a22, a23)),
|
||||||
|
std::move(row_type(a30, a31, a32, a33))}
|
||||||
|
{ }
|
||||||
|
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr typename mat<4, 4, T>::row_type &
|
||||||
|
mat<4, 4, T>::operator[](int i)
|
||||||
|
{
|
||||||
|
return value[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr typename mat<4, 4, T>::row_type const &
|
||||||
|
mat<4, 4, T>::operator[](int i) const
|
||||||
|
{
|
||||||
|
return value[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline constexpr mat<4, 4, T> operator+(mat<4, 4, T> const& m1, mat<4, 4, T> const& m2)
|
||||||
|
{
|
||||||
|
#define c(i, j) ( m1[i][j] + m2[i][j] )
|
||||||
|
|
||||||
|
return mat<4, 4, T>(c(0,0), c(0,1), c(0,2), c(0,3),
|
||||||
|
c(1,0), c(1,1), c(1,2), c(1,3),
|
||||||
|
c(2,0), c(2,1), c(2,2), c(2,3),
|
||||||
|
c(3,0), c(3,1), c(3,2), c(3,3));
|
||||||
|
#undef c
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline constexpr mat<4, 4, T> operator*(mat<4, 4, T> const& m1, mat<4, 4, T> const& m2)
|
||||||
|
{
|
||||||
|
#define c(i, j) ( \
|
||||||
|
m1[i][0] * m2[0][j] \
|
||||||
|
+ m1[i][1] * m2[1][j] \
|
||||||
|
+ m1[i][2] * m2[2][j] \
|
||||||
|
+ m1[i][3] * m2[3][j] )
|
||||||
|
|
||||||
|
return mat<4, 4, T>(c(0,0), c(0,1), c(0,2), c(0,3),
|
||||||
|
c(1,0), c(1,1), c(1,2), c(1,3),
|
||||||
|
c(2,0), c(2,1), c(2,2), c(2,3),
|
||||||
|
c(3,0), c(3,1), c(3,2), c(3,3));
|
||||||
|
#undef c
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline constexpr mat<4, 4, T> operator*(mat<4, 4, T> const& m1, float s)
|
||||||
|
{
|
||||||
|
#define c(i, j) ( m1[i][j] * s )
|
||||||
|
|
||||||
|
return mat<4, 4, T>(c(0,0), c(0,1), c(0,2), c(0,3),
|
||||||
|
c(1,0), c(1,1), c(1,2), c(1,3),
|
||||||
|
c(2,0), c(2,1), c(2,2), c(2,3),
|
||||||
|
c(3,0), c(3,1), c(3,2), c(3,3));
|
||||||
|
#undef c
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline constexpr typename mat<4, 4, T>::row_type operator*
|
||||||
|
(
|
||||||
|
mat<4, 4, T> const& m,
|
||||||
|
typename mat<4, 4, T>::col_type const& v
|
||||||
|
)
|
||||||
|
{
|
||||||
|
#define c(i) ( \
|
||||||
|
m[i][0] * v[0] \
|
||||||
|
+ m[i][1] * v[1] \
|
||||||
|
+ m[i][2] * v[2] \
|
||||||
|
+ m[i][3] * v[3] )
|
||||||
|
|
||||||
|
return typename mat<4, 4, T>::row_type(c(0), c(1), c(2), c(3));
|
||||||
|
#undef c
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline constexpr vec<3, T> operator*
|
||||||
|
(
|
||||||
|
mat<4, 4, T> const& m,
|
||||||
|
vec<3, T> const& v
|
||||||
|
)
|
||||||
|
{
|
||||||
|
#define c(i) ( \
|
||||||
|
m[i][0] * v[0] \
|
||||||
|
+ m[i][1] * v[1] \
|
||||||
|
+ m[i][2] * v[2] \
|
||||||
|
+ m[i][3] )
|
||||||
|
|
||||||
|
return vec<3, T>(c(0), c(1), c(2));
|
||||||
|
#undef c
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline constexpr mat<4, 4, T> transpose(mat<4, 4, T> const& m)
|
||||||
|
{
|
||||||
|
return mat<4, 4, T>(
|
||||||
|
m[0][0], m[1][0], m[2][0], m[3][0],
|
||||||
|
m[0][1], m[1][1], m[2][1], m[3][1],
|
||||||
|
m[0][2], m[1][2], m[2][2], m[3][2],
|
||||||
|
m[0][3], m[1][3], m[2][3], m[3][3]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline constexpr mat<3, 3, T> submatrix(mat<4, 4, T> const& a, int r, int c)
|
||||||
|
{
|
||||||
|
mat<3, 3, T> b;
|
||||||
|
int row3 = 0;
|
||||||
|
for (int row4 = 0; row4 < 4; row4++) {
|
||||||
|
if (row4 == r) continue;
|
||||||
|
int col3 = 0;
|
||||||
|
for (int col4 = 0; col4 < 4; col4++) {
|
||||||
|
if (col4 == c) continue;
|
||||||
|
b[row3][col3] = a[row4][col4];
|
||||||
|
col3++;
|
||||||
|
}
|
||||||
|
row3++;
|
||||||
|
}
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline constexpr float minor(mat<4, 4, T> const& a, int r, int c)
|
||||||
|
{
|
||||||
|
mat<3, 3, T> s = submatrix(a, r, c);
|
||||||
|
float ret = determinant(s);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline constexpr float cofactor(mat<4, 4, T> const& a, int r, int c)
|
||||||
|
{
|
||||||
|
float m = minor(a, r, c);
|
||||||
|
if ((r + c) & 1)
|
||||||
|
return -m;
|
||||||
|
else
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline constexpr float determinant(mat<4, 4, T> const& a)
|
||||||
|
{
|
||||||
|
float f0 = cofactor(a, 0, 0);
|
||||||
|
float f1 = cofactor(a, 0, 1);
|
||||||
|
float f2 = cofactor(a, 0, 2);
|
||||||
|
float f3 = cofactor(a, 0, 3);
|
||||||
|
return
|
||||||
|
a[0][0] * f0 +
|
||||||
|
a[0][1] * f1 +
|
||||||
|
a[0][2] * f2 +
|
||||||
|
a[0][3] * f3;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline constexpr mat<4, 4, T> inverse(mat<4, 4, T> const& a)
|
||||||
|
{
|
||||||
|
mat<4, 4, T> m;
|
||||||
|
float idet = 1.0f / determinant(a);
|
||||||
|
m[0][0] = cofactor(a, 0, 0) * idet;
|
||||||
|
m[1][0] = cofactor(a, 0, 1) * idet;
|
||||||
|
m[2][0] = cofactor(a, 0, 2) * idet;
|
||||||
|
m[3][0] = cofactor(a, 0, 3) * idet;
|
||||||
|
m[0][1] = cofactor(a, 1, 0) * idet;
|
||||||
|
m[1][1] = cofactor(a, 1, 1) * idet;
|
||||||
|
m[2][1] = cofactor(a, 1, 2) * idet;
|
||||||
|
m[3][1] = cofactor(a, 1, 3) * idet;
|
||||||
|
m[0][2] = cofactor(a, 2, 0) * idet;
|
||||||
|
m[1][2] = cofactor(a, 2, 1) * idet;
|
||||||
|
m[2][2] = cofactor(a, 2, 2) * idet;
|
||||||
|
m[3][2] = cofactor(a, 2, 3) * idet;
|
||||||
|
m[0][3] = cofactor(a, 3, 0) * idet;
|
||||||
|
m[1][3] = cofactor(a, 3, 1) * idet;
|
||||||
|
m[2][3] = cofactor(a, 3, 2) * idet;
|
||||||
|
m[3][3] = cofactor(a, 3, 3) * idet;
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline constexpr mat<4, 4, T> identity()
|
||||||
|
{
|
||||||
|
return mat<4, 4, T>(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);
|
||||||
|
}
|
||||||
167
drm/math/transform.hpp
Normal file
167
drm/math/transform.hpp
Normal file
@ -0,0 +1,167 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "vec.hpp"
|
||||||
|
#include "mat.hpp"
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline constexpr mat<4, 4, T> translate(vec<3, T> t)
|
||||||
|
{
|
||||||
|
return {
|
||||||
|
1, 0, 0, t.x,
|
||||||
|
0, 1, 0, t.y,
|
||||||
|
0, 0, 1, t.z,
|
||||||
|
0, 0, 0, 1
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr mat<4, 4, T> scale(vec<3, T> s)
|
||||||
|
{
|
||||||
|
return {
|
||||||
|
s.x, 0, 0, 0,
|
||||||
|
0, s.y, 0, 0,
|
||||||
|
0, 0, s.z, 0,
|
||||||
|
0, 0, 0, 1
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr mat<4, 4, T> scale(T s)
|
||||||
|
{
|
||||||
|
return {
|
||||||
|
s, 0, 0, 0,
|
||||||
|
0, s, 0, 0,
|
||||||
|
0, 0, s, 0,
|
||||||
|
0, 0, 0, 1
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr mat<4, 4, T> rotate_x(T t)
|
||||||
|
{
|
||||||
|
return {
|
||||||
|
1, 0, 0, 0,
|
||||||
|
0, cos(t), -sin(t), 0,
|
||||||
|
0, sin(t), cos(t), 0,
|
||||||
|
0, 0, 0, 1
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr mat<4, 4, T> rotate_y(T t)
|
||||||
|
{
|
||||||
|
return {
|
||||||
|
cos(t), 0, sin(t), 0,
|
||||||
|
0, 1, 0, 0,
|
||||||
|
-sin(t), 0, cos(t), 0,
|
||||||
|
0, 0, 0, 1
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr mat<4, 4, T> rotate_z(T t)
|
||||||
|
{
|
||||||
|
return {
|
||||||
|
cos(t), -sin(t), 0, 0,
|
||||||
|
sin(t), cos(t), 0, 0,
|
||||||
|
0, 0, 1, 0,
|
||||||
|
0, 0, 0, 1
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr mat<4, 4, T> rotate_axis_angle(vec<3, T> u, T t)
|
||||||
|
{
|
||||||
|
T st = sin(t);
|
||||||
|
T ct = cos(t);
|
||||||
|
T oct = 1.0 - ct;
|
||||||
|
|
||||||
|
T xx = u.x * u.x;
|
||||||
|
T xy = u.x * u.y;
|
||||||
|
T xz = u.x * u.z;
|
||||||
|
T yy = u.y * u.y;
|
||||||
|
T yz = u.y * u.z;
|
||||||
|
T zz = u.z * u.z;
|
||||||
|
|
||||||
|
return {
|
||||||
|
xx * oct + ct, xy * oct - u.z * st, xz * oct + u.y * st, 0,
|
||||||
|
xy * oct + u.z * st, yy * oct + ct, yz * oct - u.x * st, 0,
|
||||||
|
xz * oct - u.y * st, yz * oct + u.x * st, zz * oct + ct, 0,
|
||||||
|
0, 0, 0, 0
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr mat<4, 4, T> rotate_axis_angle(vec<4, T> u)
|
||||||
|
{
|
||||||
|
return rotate_axis_angle({u.x, u.y, u.z}, u.w);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr mat<4, 4, T> rotate_quaternion(vec<4, T> r)
|
||||||
|
{
|
||||||
|
T xx2 = 2 * r.x * r.x;
|
||||||
|
T xy2 = 2 * r.x * r.y;
|
||||||
|
T xz2 = 2 * r.x * r.z;
|
||||||
|
T xw2 = 2 * r.x * r.w;
|
||||||
|
T yy2 = 2 * r.y * r.y;
|
||||||
|
T yz2 = 2 * r.y * r.z;
|
||||||
|
T yw2 = 2 * r.y * r.w;
|
||||||
|
T zz2 = 2 * r.z * r.z;
|
||||||
|
T zw2 = 2 * r.z * r.w;
|
||||||
|
|
||||||
|
return {
|
||||||
|
1 - yy2 - zz2, xy2 - zw2, xz2 + yw2, 0,
|
||||||
|
xy2 + zw2, 1 - xx2 - zz2, yz2 - xw2, 0,
|
||||||
|
xz2 - yw2, yz2 + xw2, 1 - xx2 - yy2, 0,
|
||||||
|
0, 0, 0, 1,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr mat<4, 4, T> look_at(vec<3, T> eye, vec<3, T> center, vec<3, T> up)
|
||||||
|
{
|
||||||
|
vec<3, T> z = normalize(eye - center);
|
||||||
|
vec<3, T> y = up;
|
||||||
|
vec<3, T> x = cross(y, z);
|
||||||
|
y = cross(z, x);
|
||||||
|
x = normalize(x);
|
||||||
|
y = normalize(y);
|
||||||
|
|
||||||
|
mat<4, 4, T> mat = {
|
||||||
|
x.x, x.y, x.z, -dot(x, eye),
|
||||||
|
y.x, y.y, y.z, -dot(y, eye),
|
||||||
|
z.x, z.y, z.z, -dot(z, eye),
|
||||||
|
0, 0, 0, 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
return mat;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<3, T> normal_multiply(mat<4, 4, T> m, vec<3, T> n)
|
||||||
|
{
|
||||||
|
vec<4, T> n4 = m * (vec<4, T>){n.x, n.y, n.z, 0.0};
|
||||||
|
return {n4.x, n4.y, n4.z};
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr T inverse_length(vec<3, T> v)
|
||||||
|
{
|
||||||
|
float f = dot(v, v);
|
||||||
|
return 1.0f / sqrt(f);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr T screen_transform(T x, T y)
|
||||||
|
{
|
||||||
|
T x2 = x / 2.0;
|
||||||
|
T y2 = y / 2.0;
|
||||||
|
|
||||||
|
return {
|
||||||
|
y2, 0, 0, x2,
|
||||||
|
0, y2, 0, y2,
|
||||||
|
0, 0, 1, 0,
|
||||||
|
0, 0, 0, 1
|
||||||
|
};
|
||||||
|
}
|
||||||
29
drm/math/vec.hpp
Normal file
29
drm/math/vec.hpp
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
template <int L, typename T>
|
||||||
|
struct vec;
|
||||||
|
|
||||||
|
template <int L, typename T>
|
||||||
|
inline constexpr T magnitude(vec<L, T> const& v)
|
||||||
|
{
|
||||||
|
return sqrt(dot(v, v));
|
||||||
|
}
|
||||||
|
|
||||||
|
template <int L, typename T>
|
||||||
|
inline constexpr T magnitude_squared(vec<L, T> const& v)
|
||||||
|
{
|
||||||
|
return dot(v, v);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <int L, typename T>
|
||||||
|
inline constexpr vec<L, T> normalize(vec<L, T> const& v)
|
||||||
|
{
|
||||||
|
T d = 1.0f / magnitude(v);
|
||||||
|
return v * d;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <int L, typename T>
|
||||||
|
inline constexpr vec<3, T> reflect(vec<L, T> const& i, vec<L, T> const& n)
|
||||||
|
{
|
||||||
|
return i - dot(n, i) * n * static_cast<T>(2.0);
|
||||||
|
}
|
||||||
154
drm/math/vec2.hpp
Normal file
154
drm/math/vec2.hpp
Normal file
@ -0,0 +1,154 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "vec.hpp"
|
||||||
|
|
||||||
|
//
|
||||||
|
// vec3
|
||||||
|
//
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct vec<2, T>
|
||||||
|
{
|
||||||
|
union {
|
||||||
|
T val[2];
|
||||||
|
struct { T x, y; };
|
||||||
|
struct { T u, v; };
|
||||||
|
};
|
||||||
|
|
||||||
|
inline constexpr vec();
|
||||||
|
inline constexpr vec(T scalar);
|
||||||
|
inline constexpr vec(T _x, T _y);
|
||||||
|
|
||||||
|
constexpr inline vec<2, T> operator-() const;
|
||||||
|
inline constexpr T & operator[](int i);
|
||||||
|
inline constexpr T const& operator[](int i) const;
|
||||||
|
inline constexpr vec<2, T>& operator=(vec<2, T> const& v);
|
||||||
|
inline constexpr vec<2, T>& operator+=(vec<2, T> const& v);
|
||||||
|
inline constexpr vec<2, T>& operator-=(vec<2, T> const& v);
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<2, T>::vec()
|
||||||
|
: x(0), y(0)
|
||||||
|
{}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<2, T>::vec(T scalar)
|
||||||
|
: x(scalar), y(scalar)
|
||||||
|
{}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<2, T>::vec(T _x, T _y)
|
||||||
|
: x(_x), y(_y)
|
||||||
|
{}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
constexpr inline vec<2, T> vec<2, T>::operator-() const
|
||||||
|
{
|
||||||
|
return vec<2, T>(-x, -y);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr T & vec<2, T>::operator[](int i)
|
||||||
|
{
|
||||||
|
return val[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr T const& vec<2, T>::operator[](int i) const
|
||||||
|
{
|
||||||
|
return val[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<2, T>& vec<2, T>::operator=(vec<2, T> const& v)
|
||||||
|
{
|
||||||
|
this->x = static_cast<T>(v.x);
|
||||||
|
this->y = static_cast<T>(v.y);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<2, T>& vec<2, T>::operator+=(vec<2, T> const& v)
|
||||||
|
{
|
||||||
|
*this = *this + vec<2, T>(v);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<2, T>& vec<2, T>::operator-=(vec<2, T> const& v)
|
||||||
|
{
|
||||||
|
*this = *this - vec<2, T>(v);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<2, T> operator+(vec<2, T> const& v1, vec<2, T> const& v2)
|
||||||
|
{
|
||||||
|
return vec<2, T>(v1.x + v2.x,
|
||||||
|
v1.y + v2.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<2, T> operator-(vec<2, T> const& v1, vec<2, T> const& v2)
|
||||||
|
{
|
||||||
|
return vec<2, T>(v1.x - v2.x,
|
||||||
|
v1.y - v2.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<2, T> operator*(vec<2, T> const& v1, vec<2, T> const& v2)
|
||||||
|
{
|
||||||
|
return vec<2, T>(v1.x * v2.x,
|
||||||
|
v1.y * v2.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<2, T> operator*(vec<2, T> const& v1, T const& scalar)
|
||||||
|
{
|
||||||
|
return v1 * vec<2, T>(scalar);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<2, T> operator*(T const& scalar, vec<2, T> const& v1)
|
||||||
|
{
|
||||||
|
return vec<2, T>(scalar) * v1;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<2, T> operator/(vec<2, T> const& v1, vec<2, T> const& v2)
|
||||||
|
{
|
||||||
|
return vec<2, T>(v1.x / v2.x,
|
||||||
|
v1.y / v2.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<2, T> operator/(vec<2, T> const& v1, T const& scalar)
|
||||||
|
{
|
||||||
|
return v1 / vec<2, T>(scalar);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr T dot(vec<2, T> const& v1, vec<2, T> const& v2)
|
||||||
|
{
|
||||||
|
vec<2, T> tmp(v1 * v2);
|
||||||
|
return tmp.x + tmp.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr T cross(vec<2, T> const& v1, vec<2, T> const& v2)
|
||||||
|
{
|
||||||
|
return v1.x * v2.y - v2.x * v1.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<2, T> functor1(T (&func) (T const& x), vec<2, T> const& v)
|
||||||
|
{
|
||||||
|
return vec<2, T>(func(v.x), func(v.y));
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, typename U>
|
||||||
|
inline constexpr vec<2, U> functor1(U (&func) (T const& x), vec<2, T> const& v)
|
||||||
|
{
|
||||||
|
return vec<2, U>(func(v.x), func(v.y));
|
||||||
|
}
|
||||||
171
drm/math/vec3.hpp
Normal file
171
drm/math/vec3.hpp
Normal file
@ -0,0 +1,171 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "vec.hpp"
|
||||||
|
|
||||||
|
//
|
||||||
|
// vec3
|
||||||
|
//
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct vec<3, T>
|
||||||
|
{
|
||||||
|
union {
|
||||||
|
T val[3];
|
||||||
|
struct { T x, y, z; };
|
||||||
|
struct { T r, g, b; };
|
||||||
|
};
|
||||||
|
|
||||||
|
inline constexpr vec();
|
||||||
|
inline constexpr vec(T scalar);
|
||||||
|
inline constexpr vec(T _x, T _y, T _z);
|
||||||
|
|
||||||
|
constexpr inline vec<3, T> operator-() const;
|
||||||
|
inline constexpr T & operator[](int i);
|
||||||
|
inline constexpr T const& operator[](int i) const;
|
||||||
|
inline constexpr vec<3, T>& operator=(vec<3, T> const& v);
|
||||||
|
inline constexpr vec<3, T>& operator+=(vec<3, T> const& v);
|
||||||
|
inline constexpr vec<3, T>& operator-=(vec<3, T> const& v);
|
||||||
|
inline constexpr vec<3, T>& operator*=(T const& scalar);
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<3, T>::vec()
|
||||||
|
: x(0), y(0), z(0)
|
||||||
|
{}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<3, T>::vec(T scalar)
|
||||||
|
: x(scalar), y(scalar), z(scalar)
|
||||||
|
{}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<3, T>::vec(T _x, T _y, T _z)
|
||||||
|
: x(_x), y(_y), z(_z)
|
||||||
|
{}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
constexpr inline vec<3, T> vec<3, T>::operator-() const
|
||||||
|
{
|
||||||
|
return vec<3, T>(-x, -y, -z);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr T & vec<3, T>::operator[](int i)
|
||||||
|
{
|
||||||
|
return val[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr T const& vec<3, T>::operator[](int i) const
|
||||||
|
{
|
||||||
|
return val[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<3, T>& vec<3, T>::operator=(vec<3, T> const& v)
|
||||||
|
{
|
||||||
|
this->x = static_cast<T>(v.x);
|
||||||
|
this->y = static_cast<T>(v.y);
|
||||||
|
this->z = static_cast<T>(v.z);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<3, T>& vec<3, T>::operator+=(vec<3, T> const& v)
|
||||||
|
{
|
||||||
|
*this = *this + vec<3, T>(v);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<3, T>& vec<3, T>::operator-=(vec<3, T> const& v)
|
||||||
|
{
|
||||||
|
*this = *this - vec<3, T>(v);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<3, T>& vec<3, T>::operator*=(T const& scalar)
|
||||||
|
{
|
||||||
|
*this = *this * scalar;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<3, T> operator+(vec<3, T> const& v1, vec<3, T> const& v2)
|
||||||
|
{
|
||||||
|
return vec<3, T>(v1.x + v2.x,
|
||||||
|
v1.y + v2.y,
|
||||||
|
v1.z + v2.z);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<3, T> operator-(vec<3, T> const& v1, vec<3, T> const& v2)
|
||||||
|
{
|
||||||
|
return vec<3, T>(v1.x - v2.x,
|
||||||
|
v1.y - v2.y,
|
||||||
|
v1.z - v2.z);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<3, T> operator*(vec<3, T> const& v1, vec<3, T> const& v2)
|
||||||
|
{
|
||||||
|
return vec<3, T>(v1.x * v2.x,
|
||||||
|
v1.y * v2.y,
|
||||||
|
v1.z * v2.z);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<3, T> operator*(vec<3, T> const& v1, T const& scalar)
|
||||||
|
{
|
||||||
|
return v1 * vec<3, T>(scalar);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<3, T> operator*(T const& scalar, vec<3, T> const& v1)
|
||||||
|
{
|
||||||
|
return vec<3, T>(scalar) * v1;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<3, T> operator/(vec<3, T> const& v1, vec<3, T> const& v2)
|
||||||
|
{
|
||||||
|
return vec<3, T>(v1.x / v2.x,
|
||||||
|
v1.y / v2.y,
|
||||||
|
v1.z / v2.z);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<3, T> operator/(vec<3, T> const& v1, T const& scalar)
|
||||||
|
{
|
||||||
|
return v1 / vec<3, T>(scalar);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr T dot(vec<3, T> const& v1, vec<3, T> const& v2)
|
||||||
|
{
|
||||||
|
return
|
||||||
|
v1.x * v2.x +
|
||||||
|
v1.y * v2.y +
|
||||||
|
v1.z * v2.z;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<3, T> cross(vec<3, T> const& v1, vec<3, T> const& v2)
|
||||||
|
{
|
||||||
|
return vec<3, T>(v1.y * v2.z - v2.y * v1.z,
|
||||||
|
v1.z * v2.x - v2.z * v1.x,
|
||||||
|
v1.x * v2.y - v2.x * v1.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<3, T> functor1(T (&func) (T const& x), vec<3, T> const& v)
|
||||||
|
{
|
||||||
|
return vec<3, T>(func(v.x), func(v.y), func(v.z));
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, typename U>
|
||||||
|
inline constexpr vec<3, U> functor1(U (&func) (T const& x), vec<3, T> const& v)
|
||||||
|
{
|
||||||
|
return vec<3, U>(func(v.x), func(v.y), func(v.z));
|
||||||
|
}
|
||||||
164
drm/math/vec4.hpp
Normal file
164
drm/math/vec4.hpp
Normal file
@ -0,0 +1,164 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "vec.hpp"
|
||||||
|
|
||||||
|
//
|
||||||
|
// vec4
|
||||||
|
//
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct vec<4, T>
|
||||||
|
{
|
||||||
|
union {
|
||||||
|
T val[4];
|
||||||
|
struct { T x, y, z, w; };
|
||||||
|
struct { T a, r, g, b; };
|
||||||
|
};
|
||||||
|
|
||||||
|
inline constexpr vec();
|
||||||
|
inline constexpr vec(T scalar);
|
||||||
|
inline constexpr vec(T _x, T _y, T _z, T _w);
|
||||||
|
inline constexpr vec(const vec<3, T>& v);
|
||||||
|
|
||||||
|
constexpr inline vec<4, T> operator-() const;
|
||||||
|
inline constexpr T & operator[](int i);
|
||||||
|
inline constexpr T const& operator[](int i) const;
|
||||||
|
inline constexpr vec<4, T>& operator=(vec<4, T> const& v);
|
||||||
|
inline constexpr vec<4, T>& operator+=(vec<4, T> const& v);
|
||||||
|
inline constexpr vec<4, T>& operator-=(vec<4, T> const& v);
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<4, T>::vec()
|
||||||
|
: x(0), y(0), z(0), w(0)
|
||||||
|
{}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<4, T>::vec(T scalar)
|
||||||
|
: x(scalar), y(scalar), z(scalar), w(scalar)
|
||||||
|
{}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<4, T>::vec(T _x, T _y, T _z, T _w)
|
||||||
|
: x(_x), y(_y), z(_z), w(_w)
|
||||||
|
{}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<4, T>::vec(const vec<3, T>& v)
|
||||||
|
: x(v.x), y(v.y), z(v.z), w(1.f)
|
||||||
|
{}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
constexpr inline vec<4, T> vec<4, T>::operator-() const
|
||||||
|
{
|
||||||
|
return vec<4, T>(-x, -y, -z, -w);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr T & vec<4, T>::operator[](int i)
|
||||||
|
{
|
||||||
|
return val[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr T const& vec<4, T>::operator[](int i) const
|
||||||
|
{
|
||||||
|
return val[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<4, T>& vec<4, T>::operator=(vec<4, T> const& v)
|
||||||
|
{
|
||||||
|
this->x = static_cast<T>(v.x);
|
||||||
|
this->y = static_cast<T>(v.y);
|
||||||
|
this->z = static_cast<T>(v.z);
|
||||||
|
this->w = static_cast<T>(v.w);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<4, T>& vec<4, T>::operator+=(vec<4, T> const& v)
|
||||||
|
{
|
||||||
|
*this = *this + vec<4, T>(v);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<4, T>& vec<4, T>::operator-=(vec<4, T> const& v)
|
||||||
|
{
|
||||||
|
*this = *this - vec<4, T>(v);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<4, T> operator+(vec<4, T> const& v1, vec<4, T> const& v2)
|
||||||
|
{
|
||||||
|
return vec<4, T>(v1.x + v2.x,
|
||||||
|
v1.y + v2.y,
|
||||||
|
v1.z + v2.z,
|
||||||
|
v1.w + v2.w);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<4, T> operator-(vec<4, T> const& v1, vec<4, T> const& v2)
|
||||||
|
{
|
||||||
|
return vec<4, T>(v1.x - v2.x,
|
||||||
|
v1.y - v2.y,
|
||||||
|
v1.z - v2.z,
|
||||||
|
v1.w - v2.w);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<4, T> operator*(vec<4, T> const& v1, vec<4, T> const& v2)
|
||||||
|
{
|
||||||
|
return vec<4, T>(v1.x * v2.x,
|
||||||
|
v1.y * v2.y,
|
||||||
|
v1.z * v2.z,
|
||||||
|
v1.w * v2.w);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<4, T> operator*(vec<4, T> const& v1, T const& scalar)
|
||||||
|
{
|
||||||
|
return v1 * vec<4, T>(scalar);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<4, T> operator*(T const& scalar, vec<4, T> const& v1)
|
||||||
|
{
|
||||||
|
return vec<4, T>(scalar) * v1;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<4, T> operator/(vec<4, T> const& v1, vec<4, T> const& v2)
|
||||||
|
{
|
||||||
|
return vec<4, T>(v1.x / v2.x,
|
||||||
|
v1.y / v2.y,
|
||||||
|
v1.z / v2.z,
|
||||||
|
v1.w / v2.w);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<4, T> operator/(vec<4, T> const& v1, T const& scalar)
|
||||||
|
{
|
||||||
|
return v1 / vec<4, T>(scalar);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr T dot(vec<4, T> const& v1, vec<4, T> const& v2)
|
||||||
|
{
|
||||||
|
vec<4, T> tmp(v1 * v2);
|
||||||
|
return tmp.x + tmp.y + tmp.z + tmp.w;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr vec<4, T> functor1(T (&func) (T const& x), vec<4, T> const& v)
|
||||||
|
{
|
||||||
|
return vec<4, T>(func(v.x), func(v.y), func(v.z), func(v.w));
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, typename U>
|
||||||
|
inline constexpr vec<4, U> functor1(U (&func) (T const& x), vec<4, T> const& v)
|
||||||
|
{
|
||||||
|
return vec<4, U>(func(v.x), func(v.y), func(v.z), func(v.w));
|
||||||
|
}
|
||||||
1186
drm/matrix.cpp
Normal file
1186
drm/matrix.cpp
Normal file
File diff suppressed because it is too large
Load Diff
11
drm/matrix.vs.asm
Normal file
11
drm/matrix.vs.asm
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
--
|
||||||
|
-- dot(m[0], v), dot(m[1], v), dot(m[2], v), dot(m[3], v)
|
||||||
|
--
|
||||||
|
|
||||||
|
temp[1].x = VE_DOT const[0].xyzw input[0].xyzw ;
|
||||||
|
temp[1].y = VE_DOT const[1].xyzw input[0].xyzw ;
|
||||||
|
temp[1].z = VE_DOT const[2].xyzw input[0].xyzw ;
|
||||||
|
temp[1].w = VE_DOT const[3].xyzw input[0].xyzw ;
|
||||||
|
|
||||||
|
out[0].xyzw = VE_MAX temp[1].xyzw temp[1].xyzw ;
|
||||||
|
out[1].xyzw = VE_MAX input[1].xyzw input[1].xyzw ;
|
||||||
6
drm/matrix.vs.inc
Normal file
6
drm/matrix.vs.inc
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
0x00102001, 0x00d10002, 0x00d10001, 0x01ffe001,
|
||||||
|
0x00202001, 0x00d10022, 0x00d10001, 0x01ffe001,
|
||||||
|
0x00402001, 0x00d10042, 0x00d10001, 0x01ffe001,
|
||||||
|
0x00802001, 0x00d10062, 0x00d10001, 0x01ffe001,
|
||||||
|
0x00f00207, 0x00d10020, 0x00d10020, 0x01ffe020,
|
||||||
|
0x00f02207, 0x00d10021, 0x00d10021, 0x01ffe021,
|
||||||
Loading…
x
Reference in New Issue
Block a user