aboutsummaryrefslogtreecommitdiff
path: root/src/o3d
diff options
context:
space:
mode:
authorvimene <vincent.menegaux@gmail.com>2025-01-02 13:25:14 +0100
committervimene <vincent.menegaux@gmail.com>2025-01-02 13:25:14 +0100
commit26b4b82a7be2c029491f3009b66b3cbf8db5d93c (patch)
treeddc247a0600b5933185677212f158ebea5a87530 /src/o3d
parent9fdb5881d46f5d80626f961f9c9f133cc25dab70 (diff)
downloadengine-26b4b82a7be2c029491f3009b66b3cbf8db5d93c.tar.gz
various improvements
- cleaned up the computation of the camera's matrix - changed VertexData to being a struct which transmit data between the "vertex shader" and the "fragment shader" - started working on keyboard and mouse controls - added fov (field of view) - changed quaternion to euler angles conversion, from zyx to zxy - fixed computations of z coordinates in triangle rendering - improved naming in the triangle rasterizer
Diffstat (limited to 'src/o3d')
-rw-r--r--src/o3d/camera.h12
-rw-r--r--src/o3d/mesh.cpp27
-rw-r--r--src/o3d/mesh.h6
-rw-r--r--src/o3d/tri_deriv.cpp8
-rw-r--r--src/o3d/vertex_data.h13
5 files changed, 38 insertions, 28 deletions
diff --git a/src/o3d/camera.h b/src/o3d/camera.h
index 27c31eb..832f473 100644
--- a/src/o3d/camera.h
+++ b/src/o3d/camera.h
@@ -1,12 +1,22 @@
#ifndef O3D_CAMERA_H
#define O3D_CAMERA_H
+#include "math/mat4.h"
#include "math/tform.h"
+using
+ engine::math::Matrix4,
+ engine::math::Transform;
+
namespace engine::o3d {
struct Camera {
- engine::math::Transform transform;
+ float fov;
+ Transform transform;
+
+ constexpr Matrix4 to_mat4(float aspect_ratio, float min_z, float max_z) const & {
+ return Matrix4::projection(fov, aspect_ratio, min_z, max_z) * transform.to_inverse_mat4();
+ }
};
}
diff --git a/src/o3d/mesh.cpp b/src/o3d/mesh.cpp
index 60c7f8b..2423e88 100644
--- a/src/o3d/mesh.cpp
+++ b/src/o3d/mesh.cpp
@@ -6,26 +6,25 @@
using namespace engine::o3d;
-Mesh Mesh::plane() {
+Mesh Mesh::plane(float width, float height) {
+ const float w2 = width / 2,
+ h2 = height / 2;
return {
{
- {-1.f, 0.f, -1.f, 1.f},
- {+1.f, 0.f, -1.f, 1.f},
- {+1.f, 0.f, +1.f, 1.f},
- {-1.f, 0.f, +1.f, 1.f},
+ {-w2 / 2, 0.f, -h2 / 2, 1.f},
+ {+w2 / 2, 0.f, -h2 / 2, 1.f},
+ {+w2 / 2, 0.f, +h2 / 2, 1.f},
+ {-w2 / 2, 0.f, +h2 / 2, 1.f},
},
- { {0.f, 0.f, -1.f} },
{
- {0.f, 0.f},
- {1.f, 0.f},
- {1.f, 1.f},
- {0.f, 1.f},
+ {0.f, -1.f, 0.f},
+ {0.f, +1.f, 0.f},
},
{
- {{ {{0, 0, 0}}, {{1, 0, 1}}, {{2, 0, 2}} }},
- {{ {{2, 0, 2}}, {{3, 0, 3}}, {{0, 0, 0}} }},
- {{ {{0, 0, 0}}, {{3, 0, 3}}, {{2, 0, 2}} }},
- {{ {{2, 0, 2}}, {{1, 0, 1}}, {{0, 0, 0}} }},
+ {{ {{0, 0}}, {{1, 0}}, {{2, 0}} }},
+ {{ {{2, 0}}, {{3, 0}}, {{0, 0}} }},
+ {{ {{0, 1}}, {{3, 1}}, {{2, 1}} }},
+ {{ {{2, 1}}, {{1, 1}}, {{0, 1}} }},
}
};
}
diff --git a/src/o3d/mesh.h b/src/o3d/mesh.h
index 1c70ca4..acc48b5 100644
--- a/src/o3d/mesh.h
+++ b/src/o3d/mesh.h
@@ -6,19 +6,17 @@
#include <iterator>
#include <cstddef>
#include "math/vector.h"
-#include "o3d/vertex_data.h"
namespace engine::o3d {
using engine::math::Vector3, engine::math::Vector4;
struct Mesh {
- static Mesh plane();
+ static Mesh plane(float width, float height);
std::vector<Vector4> vertices;
std::vector<Vector3> normals;
- std::vector<VertexData> vertices_data;
- std::vector<std::array<std::array<std::size_t, 3>, 3>> indices;
+ std::vector<std::array<std::array<std::size_t, 2>, 3>> indices;
};
}
diff --git a/src/o3d/tri_deriv.cpp b/src/o3d/tri_deriv.cpp
index b4184bc..a22067e 100644
--- a/src/o3d/tri_deriv.cpp
+++ b/src/o3d/tri_deriv.cpp
@@ -51,7 +51,7 @@ static void _perspective_crop_x(std::vector<TriangleDerived>& tris, TriangleDeri
{
x,
q2->vertex.y + fac2 * dq2.y,
- fac2_b * q1->vertex.z + (1.f - fac2_b) * q2->vertex.z,
+ (fac2_b * q1->vertex.w * q1->vertex.z + (1.f - fac2_b) * q2->vertex.w * q2->vertex.z) / r2w,
r2w
},
fac2_b * q1->b0 + (1.f - fac2_b) * q2->b0,
@@ -65,7 +65,7 @@ static void _perspective_crop_x(std::vector<TriangleDerived>& tris, TriangleDeri
{
x,
q3->vertex.y + fac3 * dq3.y,
- fac3_b * q1->vertex.z + (1.f - fac3_b) * q3->vertex.z,
+ (fac3_b * q1->vertex.w * q1->vertex.z + (1.f - fac3_b) * q3->vertex.w * q3->vertex.z) / r3w,
r3w
},
fac3_b * q1->b0 + (1.f - fac3_b) * q3->b0,
@@ -144,7 +144,7 @@ static void _perspective_crop_y(std::vector<TriangleDerived>& tris, TriangleDeri
{
q2->vertex.x + fac2 * dq2.x,
y,
- fac2_b * q1->vertex.z + (1.f - fac2_b) * q2->vertex.z,
+ (fac2_b * q1->vertex.w * q1->vertex.z + (1.f - fac2_b) * q2->vertex.w * q2->vertex.z) / r2w,
r2w
},
fac2_b * q1->b0 + (1.f - fac2_b) * q2->b0,
@@ -158,7 +158,7 @@ static void _perspective_crop_y(std::vector<TriangleDerived>& tris, TriangleDeri
{
q3->vertex.x + fac3 * dq3.x,
y,
- fac3_b * q1->vertex.z + (1.f - fac3_b) * q3->vertex.z,
+ (fac3_b * q1->vertex.w * q1->vertex.z + (1.f - fac3_b) * q3->vertex.w * q3->vertex.z) / r3w,
r3w
},
fac3_b * q1->b0 + (1.f - fac3_b) * q3->b0,
diff --git a/src/o3d/vertex_data.h b/src/o3d/vertex_data.h
index 71f42cd..22fa76b 100644
--- a/src/o3d/vertex_data.h
+++ b/src/o3d/vertex_data.h
@@ -1,24 +1,27 @@
#ifndef O3D_VERTEX_DATA_H
#define O3D_VERTEX_DATA_H
+#include "math/vector.h"
+
+using
+ engine::math::Vector3;
+
namespace engine::o3d {
struct VertexData {
static constexpr VertexData lerp(const VertexData& vd1, const VertexData& vd2, float b0) {
return {
- b0 * vd1.tx + (1.f - b0) * vd2.tx,
- b0 * vd1.ty + (1.f - b0) * vd2.ty
+ b0 * vd1.world_loc + (1.f - b0) * vd2.world_loc,
};
}
static constexpr VertexData bilerp(const VertexData& vd1, const VertexData& vd2, const VertexData& vd3, float b0, float b1) {
return {
- b0 * vd1.tx + b1 * vd2.tx + (1.f - b0 - b1) * vd3.tx,
- b0 * vd1.ty + b1 * vd2.ty + (1.f - b0 - b1) * vd3.ty
+ b0 * vd1.world_loc + b1 * vd2.world_loc + (1.f - b0 - b1) * vd3.world_loc,
};
}
- float tx, ty;
+ Vector3 world_loc;
};
}