aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/engine.cpp9
-rw-r--r--src/math/mat4.hpp76
-rw-r--r--src/math/tform.hpp22
3 files changed, 52 insertions, 55 deletions
diff --git a/src/engine.cpp b/src/engine.cpp
index 142a8e1..792b99f 100644
--- a/src/engine.cpp
+++ b/src/engine.cpp
@@ -1700,13 +1700,10 @@ static int main_graphical() {
// update uniform buffer
{
// TODO: should use push constants
- // TODO: we shouldn't have to transpose, we could change the Matrix4 implementation to
- // make all operation on transpose, which wouldn't change anything for the software
- // renderer
engine::vk::UniformBufferObject ubo {
- .model = Matrix4::rot_z(time * PI / 2.f).transpose(),
- .view = Matrix4::look_at(Vector3(2.f, 2.f, 2.f), Vector3(0.f, 0.f, 0.f), Vector3(0.f, 0.f, 1.f)).transpose(),
- .proj = Matrix4::perspective(PI / 4.f, static_cast<float>(swapchain_extent.width) / static_cast<float>(swapchain_extent.height), .1f, 10.f).transpose(),
+ .model = Matrix4::rot_z(time * PI / 2.f),
+ .view = Matrix4::look_at(Vector3(2.f, 2.f, 2.f), Vector3(0.f, 0.f, 0.f), Vector3(0.f, 0.f, 1.f)),
+ .proj = Matrix4::perspective(PI / 4.f, static_cast<float>(swapchain_extent.width) / static_cast<float>(swapchain_extent.height), .1f, 10.f),
};
memcpy(uniform_buf_mems[frame_idx], &ubo, sizeof(engine::vk::UniformBufferObject));
}
diff --git a/src/math/mat4.hpp b/src/math/mat4.hpp
index 1b01e26..cbbee9a 100644
--- a/src/math/mat4.hpp
+++ b/src/math/mat4.hpp
@@ -7,6 +7,7 @@
namespace engine::math {
+// values are represented in memory as column first, meaning values[1] is the first column, 2nd row
struct Matrix4 {
static constexpr Matrix4 idty() {
return {
@@ -19,10 +20,10 @@ struct Matrix4 {
static constexpr Matrix4 translate(const Vector3& v) {
return {
- 1.f, 0.f, 0.f, v.x,
- 0.f, 1.f, 0.f, v.y,
- 0.f, 0.f, 1.f, v.z,
- 0.f, 0.f, 0.f, 1.f,
+ 1.f, 0.f, 0.f, 0.f,
+ 0.f, 1.f, 0.f, 0.f,
+ 0.f, 0.f, 1.f, 0.f,
+ v.x, v.y, v.z, 1.f,
};
}
@@ -49,8 +50,8 @@ struct Matrix4 {
float s = std::sin(a);
return {
1.f, 0.f, 0.f, 0.f,
- 0.f, c, -s, 0.f,
- 0.f, s, c, 0.f,
+ 0.f, c, s, 0.f,
+ 0.f, -s, c, 0.f,
0.f, 0.f, 0.f, 1.f,
};
}
@@ -59,9 +60,9 @@ struct Matrix4 {
float c = std::cos(a);
float s = std::sin(a);
return {
- c, 0.f, s, 0.f,
+ c, 0.f, -s, 0.f,
0.f, 1.f, 0.f, 0.f,
- -s, 0.f, c, 0.f,
+ s, 0.f, c, 0.f,
0.f, 0.f, 0.f, 1.f,
};
}
@@ -70,8 +71,8 @@ struct Matrix4 {
float c = std::cos(a);
float s = std::sin(a);
return {
- c, -s, 0.f, 0.f,
- s, c, 0.f, 0.f,
+ c, s, 0.f, 0.f,
+ -s, c, 0.f, 0.f,
0.f, 0.f, 1.f, 0.f,
0.f, 0.f, 0.f, 1.f,
};
@@ -79,33 +80,32 @@ struct Matrix4 {
static constexpr Matrix4 perspective(float fov, float aspect_ratio, float min_z, float max_z) {
float inv_tan = 1.f / std::tan(fov / 2.f);
- return {{
- inv_tan / aspect_ratio, 0.f, 0.f, 0.f,
- 0.f, -inv_tan, 0.f, 0.f,
- 0.f, 0.f, -1.f / (max_z - min_z), -min_z / (max_z - min_z),
- 0.f, 0.f, -1.f, 0.f,
- }};
+ return {
+ inv_tan / aspect_ratio, 0.f, 0.f, 0.f,
+ 0.f, -inv_tan, 0.f, 0.f,
+ 0.f, 0.f, -1.f / (max_z - min_z), -1.f,
+ 0.f, 0.f, -min_z / (max_z - min_z), 0.f,
+ };
}
- // TODO: improve
static constexpr Matrix4 look_at(const Vector3& eye, const Vector3& center, const Vector3& up) {
- Vector3 new_z = -(center - eye).normalize();
- Vector3 new_x = up.cross(new_z).normalize();
+ Vector3 new_z = (eye - center).normalize();
+ Vector3 new_x = up.cross(eye - center).normalize();
Vector3 new_y = new_z.cross(new_x);
- return Matrix4 {
- new_x.x, new_x.y, new_x.z, 0.f,
- new_y.x, new_y.y, new_y.z, 0.f,
- new_z.x, new_z.y, new_z.z, 0.f,
- 0.f, 0.f, 0.f, 1.f,
- } * Matrix4::translate(-eye);
+ return {
+ new_x.x, new_y.x, new_z.x, 0.f,
+ new_x.y, new_y.y, new_z.y, 0.f,
+ new_x.z, new_y.z, new_z.z, 0.f,
+ -new_x.dot(eye), -new_y.dot(eye), -new_z.dot(eye), 1.f,
+ };
}
// TODO: should be Quaternion::to_mat4
static constexpr Matrix4 from_quaternion(const Quaternion& q) {
return {
- 2.f * (q.w * q.w + q.x * q.x) - 1.f, 2.f * (q.x * q.y - q.w * q.z) , 2.f * (q.x * q.z + q.w * q.y) , 0.f,
- 2.f * (q.x * q.y + q.w * q.z) , 2.f * (q.w * q.w + q.y * q.y) - 1.f, 2.f * (q.y * q.z - q.w * q.x) , 0.f,
- 2.f * (q.x * q.z - q.w * q.y) , 2.f * (q.y * q.z + q.w * q.x) , 2.f * (q.w * q.w + q.z * q.z) - 1.f, 0.f,
+ 2.f * (q.w * q.w + q.x * q.x) - 1.f, 2.f * (q.x * q.y + q.w * q.z) , 2.f * (q.x * q.z - q.w * q.y) , 0.f,
+ 2.f * (q.x * q.y - q.w * q.z) , 2.f * (q.w * q.w + q.y * q.y) - 1.f, 2.f * (q.y * q.z + q.w * q.x) , 0.f,
+ 2.f * (q.x * q.z + q.w * q.y) , 2.f * (q.y * q.z - q.w * q.x) , 2.f * (q.w * q.w + q.z * q.z) - 1.f, 0.f,
0.f, 0.f, 0.f, 1.f,
};
}
@@ -142,9 +142,9 @@ struct Matrix4 {
Matrix4 ret;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
- ret.values[i * 4 + j] = 0.f;
+ ret.values[i + j * 4] = 0.f;
for (int k = 0; k < 4; k++)
- ret.values[i * 4 + j] += values[i * 4 + k] * m.values[k * 4 + j];
+ ret.values[i + j * 4] += values[i + k * 4] * m.values[k + j * 4];
}
}
return ret;
@@ -152,19 +152,19 @@ struct Matrix4 {
constexpr Vector4 operator*(const Vector4& v) const & {
return {
- values[ 0] * v.x + values[ 1] * v.y + values[ 2] * v.z + values[ 3] * v.w,
- values[ 4] * v.x + values[ 5] * v.y + values[ 6] * v.z + values[ 7] * v.w,
- values[ 8] * v.x + values[ 9] * v.y + values[10] * v.z + values[11] * v.w,
- values[12] * v.x + values[13] * v.y + values[14] * v.z + values[15] * v.w,
+ values[ 0] * v.x + values[ 4] * v.y + values[ 8] * v.z + values[12] * v.w,
+ values[ 1] * v.x + values[ 5] * v.y + values[ 9] * v.z + values[13] * v.w,
+ values[ 2] * v.x + values[ 6] * v.y + values[10] * v.z + values[14] * v.w,
+ values[ 3] * v.x + values[ 7] * v.y + values[11] * v.z + values[15] * v.w,
};
}
constexpr std::array<Vector4, 4> to_vecs() const & {
return {{
- { values[ 0], values[ 4], values[ 8], values[12] },
- { values[ 1], values[ 5], values[ 9], values[13] },
- { values[ 2], values[ 6], values[10], values[14] },
- { values[ 3], values[ 7], values[11], values[15] },
+ { values[ 0], values[ 1], values[ 2], values[ 3] },
+ { values[ 4], values[ 5], values[ 6], values[ 7] },
+ { values[ 8], values[ 9], values[10], values[11] },
+ { values[12], values[13], values[14], values[15] },
}};
}
diff --git a/src/math/tform.hpp b/src/math/tform.hpp
index cd9301f..d47e0ad 100644
--- a/src/math/tform.hpp
+++ b/src/math/tform.hpp
@@ -22,24 +22,24 @@ class Transform {
constexpr Matrix4 to_mat4() const & {
return {
- scale.x * (2.f * (rot.w * rot.w + rot.x * rot.x) - 1.f), scale.y * (2.f * (rot.x * rot.y - rot.w * rot.z) ), scale.z * (2.f * (rot.x * rot.z + rot.w * rot.y) ), loc.x,
- scale.x * (2.f * (rot.x * rot.y + rot.w * rot.z) ), scale.y * (2.f * (rot.w * rot.w + rot.y * rot.y) - 1.f), scale.z * (2.f * (rot.y * rot.z - rot.w * rot.x) ), loc.y,
- scale.x * (2.f * (rot.x * rot.z - rot.w * rot.y) ), scale.y * (2.f * (rot.y * rot.z + rot.w * rot.x) ), scale.z * (2.f * (rot.w * rot.w + rot.z * rot.z) - 1.f), loc.z,
- 0.f, 0.f, 0.f, 1.f,
+ scale.x * (2.f * (rot.w * rot.w + rot.x * rot.x) - 1.f), scale.x * (2.f * (rot.x * rot.y + rot.w * rot.z) ), scale.x * (2.f * (rot.x * rot.z - rot.w * rot.y) ), 0.f,
+ scale.y * (2.f * (rot.x * rot.y - rot.w * rot.z) ), scale.y * (2.f * (rot.w * rot.w + rot.y * rot.y) - 1.f), scale.y * (2.f * (rot.y * rot.z + rot.w * rot.x) ), 0.f,
+ scale.z * (2.f * (rot.x * rot.z + rot.w * rot.y) ), scale.z * (2.f * (rot.y * rot.z - rot.w * rot.x) ), scale.z * (2.f * (rot.w * rot.w + rot.z * rot.z) - 1.f), 0.f,
+ loc.x, loc.y, loc.z, 1.f,
};
}
constexpr Matrix4 to_inverse_mat4() const & {
std::array<float, 3 * 3> m{{
- (2.f * (rot.w * rot.w + rot.x * rot.x) - 1.f) / scale.x, (2.f * (rot.x * rot.y + rot.w * rot.z) ) / scale.x, (2.f * (rot.x * rot.z - rot.w * rot.y) ) / scale.x,
- (2.f * (rot.x * rot.y - rot.w * rot.z) ) / scale.y, (2.f * (rot.w * rot.w + rot.y * rot.y) - 1.f) / scale.y, (2.f * (rot.y * rot.z + rot.w * rot.x) ) / scale.y,
- (2.f * (rot.x * rot.z + rot.w * rot.y) ) / scale.z, (2.f * (rot.y * rot.z - rot.w * rot.x) ) / scale.z, (2.f * (rot.w * rot.w + rot.z * rot.z) - 1.f) / scale.z,
+ (2.f * (rot.w * rot.w + rot.x * rot.x) - 1.f) / scale.x, (2.f * (rot.x * rot.y - rot.w * rot.z) ) / scale.y, (2.f * (rot.x * rot.z + rot.w * rot.y) ) / scale.z,
+ (2.f * (rot.x * rot.y + rot.w * rot.z) ) / scale.x, (2.f * (rot.w * rot.w + rot.y * rot.y) - 1.f) / scale.y, (2.f * (rot.y * rot.z - rot.w * rot.x) ) / scale.z,
+ (2.f * (rot.x * rot.z - rot.w * rot.y) ) / scale.x, (2.f * (rot.y * rot.z + rot.w * rot.x) ) / scale.y, (2.f * (rot.w * rot.w + rot.z * rot.z) - 1.f) / scale.z,
}};
return {
- m[0], m[1], m[2], - (m[0] * loc.x + m[1] * loc.y + m[2] * loc.z),
- m[3], m[4], m[5], - (m[3] * loc.x + m[4] * loc.y + m[5] * loc.z),
- m[6], m[7], m[8], - (m[6] * loc.x + m[7] * loc.y + m[8] * loc.z),
- 0.f, 0.f, 0.f, 1.0f,
+ m[0], m[1], m[2], 0.f,
+ m[3], m[4], m[5], 0.f,
+ m[6], m[7], m[8], 0.f,
+ -loc.dot({ m[0], m[3], m[6] }), -loc.dot({ m[1], m[4], m[7] }), -loc.dot({ m[2], m[5], m[8] }), 1.f,
};
}
};