From 761604f9e4815e8f4355637ebb46052314cbed86 Mon Sep 17 00:00:00 2001 From: vimene Date: Wed, 10 Dec 2025 20:40:42 +0100 Subject: renamed .h to .hpp --- src/Makefile.am | 40 ++++---- src/ctrl/keyboard.h | 52 ----------- src/ctrl/keyboard.hpp | 52 +++++++++++ src/ctrl/mouse.h | 23 ----- src/ctrl/mouse.hpp | 23 +++++ src/engine.cpp | 32 +++---- src/fb/chfb.cpp | 10 +- src/fb/chfb.h | 38 -------- src/fb/chfb.hpp | 38 ++++++++ src/fb/pixfb.cpp | 10 +- src/fb/pixfb.h | 40 -------- src/fb/pixfb.hpp | 40 ++++++++ src/math/mat4.h | 170 ---------------------------------- src/math/mat4.hpp | 170 ++++++++++++++++++++++++++++++++++ src/math/quat.h | 78 ---------------- src/math/quat.hpp | 78 ++++++++++++++++ src/math/tform.h | 49 ---------- src/math/tform.hpp | 49 ++++++++++ src/math/vector.h | 236 ----------------------------------------------- src/math/vector.hpp | 236 +++++++++++++++++++++++++++++++++++++++++++++++ src/o3d/camera.h | 24 ----- src/o3d/camera.hpp | 24 +++++ src/o3d/deriv_vertex.h | 21 ----- src/o3d/deriv_vertex.hpp | 21 +++++ src/o3d/mesh.cpp | 4 +- src/o3d/mesh.h | 24 ----- src/o3d/mesh.hpp | 24 +++++ src/o3d/obj3d.h | 17 ---- src/o3d/obj3d.hpp | 17 ++++ src/o3d/scene.h | 18 ---- src/o3d/scene.hpp | 18 ++++ src/o3d/tri.h | 22 ----- src/o3d/tri.hpp | 22 +++++ src/o3d/tri_deriv.cpp | 6 +- src/o3d/tri_deriv.h | 25 ----- src/o3d/tri_deriv.hpp | 25 +++++ src/o3d/vertex.h | 19 ---- src/o3d/vertex.hpp | 19 ++++ src/o3d/vertex_data.h | 29 ------ src/o3d/vertex_data.hpp | 29 ++++++ src/obj_parser.cpp | 8 +- src/obj_parser.h | 12 --- src/obj_parser.hpp | 12 +++ src/renderer.cpp | 12 +-- src/renderer.h | 41 -------- src/renderer.hpp | 41 ++++++++ 46 files changed, 999 insertions(+), 999 deletions(-) delete mode 100644 src/ctrl/keyboard.h create mode 100644 src/ctrl/keyboard.hpp delete mode 100644 src/ctrl/mouse.h create mode 100644 src/ctrl/mouse.hpp delete mode 100644 src/fb/chfb.h create mode 100644 src/fb/chfb.hpp delete mode 100644 src/fb/pixfb.h create mode 100644 src/fb/pixfb.hpp delete mode 100644 src/math/mat4.h create mode 100644 src/math/mat4.hpp delete mode 100644 src/math/quat.h create mode 100644 src/math/quat.hpp delete mode 100644 src/math/tform.h create mode 100644 src/math/tform.hpp delete mode 100644 src/math/vector.h create mode 100644 src/math/vector.hpp delete mode 100644 src/o3d/camera.h create mode 100644 src/o3d/camera.hpp delete mode 100644 src/o3d/deriv_vertex.h create mode 100644 src/o3d/deriv_vertex.hpp delete mode 100644 src/o3d/mesh.h create mode 100644 src/o3d/mesh.hpp delete mode 100644 src/o3d/obj3d.h create mode 100644 src/o3d/obj3d.hpp delete mode 100644 src/o3d/scene.h create mode 100644 src/o3d/scene.hpp delete mode 100644 src/o3d/tri.h create mode 100644 src/o3d/tri.hpp delete mode 100644 src/o3d/tri_deriv.h create mode 100644 src/o3d/tri_deriv.hpp delete mode 100644 src/o3d/vertex.h create mode 100644 src/o3d/vertex.hpp delete mode 100644 src/o3d/vertex_data.h create mode 100644 src/o3d/vertex_data.hpp delete mode 100644 src/obj_parser.h create mode 100644 src/obj_parser.hpp delete mode 100644 src/renderer.h create mode 100644 src/renderer.hpp (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index 5a3b41d..e45b82d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,25 +1,25 @@ bin_PROGRAMS = engine -engine_SOURCES = \ - engine.cpp \ - renderer.h renderer.cpp \ - obj_parser.h obj_parser.cpp \ - fb/fb.h fb/chfb.h fb/chfb.cpp fb/pixfb.h fb/pixfb.cpp \ - math/vector.h \ - math/mat4.h \ - math/quat.h \ - math/tform.h \ - o3d/mesh.h o3d/mesh.cpp \ - o3d/obj3d.h \ - o3d/vertex_data.h \ - o3d/vertex.h \ - o3d/deriv_vertex.h \ - o3d/tri.h \ - o3d/tri_deriv.h o3d/tri_deriv.cpp \ - o3d/camera.h \ - o3d/scene.h \ - ctrl/keyboard.h \ - ctrl/mouse.h +engine_SOURCES = \ + engine.cpp \ + renderer.hpp renderer.cpp \ + obj_parser.hpp obj_parser.cpp \ + fb/fb.hpp fb/chfb.hpp fb/chfb.cpp fb/pixfb.hpp fb/pixfb.cpp \ + math/vector.hpp \ + math/mat4.hpp \ + math/quat.hpp \ + math/tform.hpp \ + o3d/mesh.hpp o3d/mesh.cpp \ + o3d/obj3d.hpp \ + o3d/vertex_data.hpp \ + o3d/vertex.hpp \ + o3d/deriv_vertex.hpp \ + o3d/tri.hpp \ + o3d/tri_deriv.hpp o3d/tri_deriv.cpp \ + o3d/camera.hpp \ + o3d/scene.hpp \ + ctrl/keyboard.hpp \ + ctrl/mouse.hpp engine_CPPFLAGS = -std=gnu++23 -Wall -Wextra engine_LDFLAGS = -std=gnu++23 -Wall -Wextra diff --git a/src/ctrl/keyboard.h b/src/ctrl/keyboard.h deleted file mode 100644 index 4f3b4ef..0000000 --- a/src/ctrl/keyboard.h +++ /dev/null @@ -1,52 +0,0 @@ -#ifndef CTRL_KEYBOARD_H -#define CTRL_KEYBOARD_H - -#include -#include "math/vector.h" - -using engine::math::Vector2; - -namespace engine::controllers { - -enum class KeyboardKey { - fw, - bw, - key_left, - key_right, - zoom, -}; - -static constexpr std::size_t keyboard_key_count = 5; - -template -class Keyboard { - public: - constexpr Keyboard(KeyDownCallback key_down_cb, KeyUpCallback key_up_cb) : - key_down_cb{key_down_cb}, key_up_cb{key_up_cb}, key_down_list{{{}}} {} - - void key_down_event(KeyboardKey key) { - key_down_list[static_cast(key) / (sizeof(std::size_t) * 8)] - |= 1 << (static_cast(key) % (sizeof(std::size_t) * 8)); - key_down_cb(key); - } - - void key_up_event(KeyboardKey key) { - key_down_list[static_cast(key) / (sizeof(std::size_t) * 8)] - &= ~(1 << (static_cast(key) % (sizeof(std::size_t) * 8))); - key_up_cb(key); - } - - constexpr bool is_down(KeyboardKey key) const & { - return (key_down_list[static_cast(key) / (sizeof(std::size_t) * 8)] - & (1 << (static_cast(key) % (sizeof(std::size_t) * 8)))) != 0; - } - - private: - KeyDownCallback key_down_cb; - KeyUpCallback key_up_cb; - std::array key_down_list; -}; - -} - -#endif // CTRL_KEYBOARD_H diff --git a/src/ctrl/keyboard.hpp b/src/ctrl/keyboard.hpp new file mode 100644 index 0000000..c8ab983 --- /dev/null +++ b/src/ctrl/keyboard.hpp @@ -0,0 +1,52 @@ +#ifndef CTRL_KEYBOARD_H +#define CTRL_KEYBOARD_H + +#include +#include "math/vector.hpp" + +using engine::math::Vector2; + +namespace engine::controllers { + +enum class KeyboardKey { + fw, + bw, + key_left, + key_right, + zoom, +}; + +static constexpr std::size_t keyboard_key_count = 5; + +template +class Keyboard { + public: + constexpr Keyboard(KeyDownCallback key_down_cb, KeyUpCallback key_up_cb) : + key_down_cb{key_down_cb}, key_up_cb{key_up_cb}, key_down_list{{{}}} {} + + void key_down_event(KeyboardKey key) { + key_down_list[static_cast(key) / (sizeof(std::size_t) * 8)] + |= 1 << (static_cast(key) % (sizeof(std::size_t) * 8)); + key_down_cb(key); + } + + void key_up_event(KeyboardKey key) { + key_down_list[static_cast(key) / (sizeof(std::size_t) * 8)] + &= ~(1 << (static_cast(key) % (sizeof(std::size_t) * 8))); + key_up_cb(key); + } + + constexpr bool is_down(KeyboardKey key) const & { + return (key_down_list[static_cast(key) / (sizeof(std::size_t) * 8)] + & (1 << (static_cast(key) % (sizeof(std::size_t) * 8)))) != 0; + } + + private: + KeyDownCallback key_down_cb; + KeyUpCallback key_up_cb; + std::array key_down_list; +}; + +} + +#endif // CTRL_KEYBOARD_H diff --git a/src/ctrl/mouse.h b/src/ctrl/mouse.h deleted file mode 100644 index 521b0a4..0000000 --- a/src/ctrl/mouse.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef CTRL_MOUSE_H -#define CTRL_MOUSE_H - -#include "math/vector.h" - -namespace engine::controllers { - -template -class Mouse { - public: - constexpr Mouse(MouseMotionCallback mouse_motion_cb) : mouse_motion_cb{mouse_motion_cb} {} - - void mouse_motion_event(Vector2 rel) const & { - mouse_motion_cb(rel); - } - - private: - MouseMotionCallback mouse_motion_cb; -}; - -} - -#endif // CTRL_MOUSE_H diff --git a/src/ctrl/mouse.hpp b/src/ctrl/mouse.hpp new file mode 100644 index 0000000..8973499 --- /dev/null +++ b/src/ctrl/mouse.hpp @@ -0,0 +1,23 @@ +#ifndef CTRL_MOUSE_H +#define CTRL_MOUSE_H + +#include "math/vector.hpp" + +namespace engine::controllers { + +template +class Mouse { + public: + constexpr Mouse(MouseMotionCallback mouse_motion_cb) : mouse_motion_cb{mouse_motion_cb} {} + + void mouse_motion_event(Vector2 rel) const & { + mouse_motion_cb(rel); + } + + private: + MouseMotionCallback mouse_motion_cb; +}; + +} + +#endif // CTRL_MOUSE_H diff --git a/src/engine.cpp b/src/engine.cpp index 2acba89..1d17ec2 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -23,22 +23,22 @@ #include #endif -#include "fb/chfb.h" -#include "fb/pixfb.h" -#include "o3d/scene.h" -#include "o3d/mesh.h" -#include "o3d/obj3d.h" -#include "o3d/vertex_data.h" -#include "o3d/tri.h" -#include "o3d/camera.h" -#include "math/vector.h" -#include "math/mat4.h" -#include "math/quat.h" -#include "math/tform.h" -#include "ctrl/keyboard.h" -#include "ctrl/mouse.h" -#include "renderer.h" -#include "obj_parser.h" +#include "fb/chfb.hpp" +#include "fb/pixfb.hpp" +#include "o3d/scene.hpp" +#include "o3d/mesh.hpp" +#include "o3d/obj3d.hpp" +#include "o3d/vertex_data.hpp" +#include "o3d/tri.hpp" +#include "o3d/camera.hpp" +#include "math/vector.hpp" +#include "math/mat4.hpp" +#include "math/quat.hpp" +#include "math/tform.hpp" +#include "ctrl/keyboard.hpp" +#include "ctrl/mouse.hpp" +#include "renderer.hpp" +#include "obj_parser.hpp" using engine::Renderer, diff --git a/src/fb/chfb.cpp b/src/fb/chfb.cpp index 8068d8f..9f293ed 100644 --- a/src/fb/chfb.cpp +++ b/src/fb/chfb.cpp @@ -1,10 +1,10 @@ -#include "fb/chfb.h" +#include "fb/chfb.hpp" #include #include -#include "math/vector.h" -#include "math/quat.h" -#include "o3d/vertex_data.h" -#include "o3d/camera.h" +#include "math/vector.hpp" +#include "math/quat.hpp" +#include "o3d/vertex_data.hpp" +#include "o3d/camera.hpp" using namespace engine::fb; using diff --git a/src/fb/chfb.h b/src/fb/chfb.h deleted file mode 100644 index a866fef..0000000 --- a/src/fb/chfb.h +++ /dev/null @@ -1,38 +0,0 @@ -#ifndef FB_CHFB_H -#define FB_CHFB_H - -#include -#include "math/vector.h" -#include "o3d/vertex_data.h" - -namespace engine::fb { - -using engine::math::Vector3, engine::o3d::VertexData; - -class CharacterFrameBuffer { - public: - CharacterFrameBuffer(unsigned int w, unsigned int h); - void resize(unsigned int w, unsigned int h); - void clear(); - void draw_point(int x, int y, const Vector3& loc, const VertexData& vd, const Vector3& normal); - - constexpr unsigned int width() const & { - return w; - } - - constexpr unsigned int height() const & { - return h; - } - - constexpr const char* chars() const & { - return chars_vector.data(); - } - - private: - unsigned int w, h; - std::vector chars_vector; -}; - -} - -#endif // FB_CHFB_H diff --git a/src/fb/chfb.hpp b/src/fb/chfb.hpp new file mode 100644 index 0000000..8e47d6b --- /dev/null +++ b/src/fb/chfb.hpp @@ -0,0 +1,38 @@ +#ifndef FB_CHFB_H +#define FB_CHFB_H + +#include +#include "math/vector.hpp" +#include "o3d/vertex_data.hpp" + +namespace engine::fb { + +using engine::math::Vector3, engine::o3d::VertexData; + +class CharacterFrameBuffer { + public: + CharacterFrameBuffer(unsigned int w, unsigned int h); + void resize(unsigned int w, unsigned int h); + void clear(); + void draw_point(int x, int y, const Vector3& loc, const VertexData& vd, const Vector3& normal); + + constexpr unsigned int width() const & { + return w; + } + + constexpr unsigned int height() const & { + return h; + } + + constexpr const char* chars() const & { + return chars_vector.data(); + } + + private: + unsigned int w, h; + std::vector chars_vector; +}; + +} + +#endif // FB_CHFB_H diff --git a/src/fb/pixfb.cpp b/src/fb/pixfb.cpp index d49566e..55c356f 100644 --- a/src/fb/pixfb.cpp +++ b/src/fb/pixfb.cpp @@ -1,10 +1,10 @@ -#include "fb/pixfb.h" +#include "fb/pixfb.hpp" #include #include -#include "math/vector.h" -#include "math/quat.h" -#include "o3d/vertex_data.h" -#include "o3d/camera.h" +#include "math/vector.hpp" +#include "math/quat.hpp" +#include "o3d/vertex_data.hpp" +#include "o3d/camera.hpp" using namespace engine::fb; using diff --git a/src/fb/pixfb.h b/src/fb/pixfb.h deleted file mode 100644 index 81a7ac3..0000000 --- a/src/fb/pixfb.h +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef FB_PIXFB_H -#define FB_PIXFB_H - -#include -#include -#include "math/vector.h" -#include "o3d/vertex_data.h" - -namespace engine::fb { - -using engine::math::Vector3, engine::o3d::VertexData; - -class PixelFrameBuffer { - public: - PixelFrameBuffer(unsigned int w, unsigned int h); - void resize(unsigned int w, unsigned int h); - void clear(); - void draw_point(int x, int y, const Vector3& loc, const VertexData& vd, const Vector3& normal); - - constexpr unsigned int width() const & { - return w; - } - - constexpr unsigned int height() const & { - return h; - } - - constexpr const uint32_t* pixels() const & { - return pixels_vector.data(); - } - - private: - unsigned int w, h; - std::vector pixels_vector; - int face_ind; -}; - -} - -#endif // FB_PIXFB_H diff --git a/src/fb/pixfb.hpp b/src/fb/pixfb.hpp new file mode 100644 index 0000000..f7a7acb --- /dev/null +++ b/src/fb/pixfb.hpp @@ -0,0 +1,40 @@ +#ifndef FB_PIXFB_H +#define FB_PIXFB_H + +#include +#include +#include "math/vector.hpp" +#include "o3d/vertex_data.hpp" + +namespace engine::fb { + +using engine::math::Vector3, engine::o3d::VertexData; + +class PixelFrameBuffer { + public: + PixelFrameBuffer(unsigned int w, unsigned int h); + void resize(unsigned int w, unsigned int h); + void clear(); + void draw_point(int x, int y, const Vector3& loc, const VertexData& vd, const Vector3& normal); + + constexpr unsigned int width() const & { + return w; + } + + constexpr unsigned int height() const & { + return h; + } + + constexpr const uint32_t* pixels() const & { + return pixels_vector.data(); + } + + private: + unsigned int w, h; + std::vector pixels_vector; + int face_ind; +}; + +} + +#endif // FB_PIXFB_H diff --git a/src/math/mat4.h b/src/math/mat4.h deleted file mode 100644 index df8f533..0000000 --- a/src/math/mat4.h +++ /dev/null @@ -1,170 +0,0 @@ -#ifndef MATH_MAT4_H -#define MATH_MAT4_H - -#include -#include -#include "math/vector.h" - -namespace engine::math { - -struct Matrix4 { - static constexpr Matrix4 idty() { - return { - 1.f, 0.f, 0.f, 0.f, - 0.f, 1.f, 0.f, 0.f, - 0.f, 0.f, 1.f, 0.f, - 0.f, 0.f, 0.f, 1.f, - }; - } - - 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, - }; - } - - static constexpr Matrix4 scale(float fac) { - return { - fac, 0.f, 0.f, 0.f, - 0.f, fac, 0.f, 0.f, - 0.f, 0.f, fac, 0.f, - 0.f, 0.f, 0.f, 1.f, - }; - } - - static constexpr Matrix4 scale(const Vector3& facs) { - return { - facs.x, 0.f, 0.f, 0.f, - 0.f, facs.y, 0.f, 0.f, - 0.f, 0.f, facs.z, 0.f, - 0.f, 0.f, 0.f, 1.f, - }; - } - - static constexpr Matrix4 rot_x(float a) { - float c = std::cos(a); - 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, 0.f, 0.f, 1.f, - }; - } - - static constexpr Matrix4 rot_y(float a) { - float c = std::cos(a); - float s = std::sin(a); - return { - c, 0.f, s, 0.f, - 0.f, 1.f, 0.f, 0.f, - -s, 0.f, c, 0.f, - 0.f, 0.f, 0.f, 1.f, - }; - } - - static constexpr Matrix4 rot_z(float a) { - float c = std::cos(a); - float s = std::sin(a); - return { - 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, - }; - } - - static constexpr Matrix4 projection(float fov, float aspect_ratio, float min_z, float max_z) { - float inv_tan = 1.f / std::tan(fov / 2.f); - return {{ - aspect_ratio * inv_tan, 0.f, 0.f, 0.f, - 0.f, -inv_tan, 0.f, 0.f, - 0.f, 0.f, -2.f / (max_z - min_z), -(max_z + min_z) / (max_z - min_z), - 0.f, 0.f, -1.f, 0.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, - 0.f, 0.f, 0.f, 1.f, - }; - } - - std::array values; - - constexpr Matrix4 operator+() const & { - return *this; - } - - constexpr Matrix4 operator-() const & { - return { - -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], - }; - } - - constexpr Matrix4 operator+(const Matrix4& m) const & { - return { - values[ 0] + m.values[ 0], values[ 1] + m.values[ 1], values[ 2] + m.values[ 2], values[ 3] + m.values[ 3], - values[ 4] + m.values[ 4], values[ 5] + m.values[ 5], values[ 6] + m.values[ 6], values[ 7] + m.values[ 7], - values[ 8] + m.values[ 8], values[ 9] + m.values[ 9], values[10] + m.values[10], values[11] + m.values[11], - values[12] + m.values[12], values[13] + m.values[13], values[14] + m.values[14], values[15] + m.values[15], - }; - } - - constexpr Matrix4 operator-(const Matrix4& m) const & { - return *this + (-m); - } - - constexpr Matrix4 operator*(const Matrix4& m) const & { - Matrix4 ret; - for (int i = 0; i < 4; i++) { - for (int j = 0; j < 4; j++) { - ret.values[i * 4 + j] = 0.f; - for (int k = 0; k < 4; k++) - ret.values[i * 4 + j] += values[i * 4 + k] * m.values[k * 4 + j]; - } - } - return ret; - } - - 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, - }; - } - - constexpr std::array 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] }, - }}; - } -}; - -constexpr Matrix4 operator*(float fac, const Matrix4& m) { - return { - fac * m.values[ 0], fac * m.values[ 1], fac * m.values[ 2], fac * m.values[ 3], - fac * m.values[ 4], fac * m.values[ 5], fac * m.values[ 6], fac * m.values[ 7], - fac * m.values[ 8], fac * m.values[ 9], fac * m.values[10], fac * m.values[11], - fac * m.values[12], fac * m.values[13], fac * m.values[14], fac * m.values[15], - }; -} - -} - -#endif // MATH_MAT4_H diff --git a/src/math/mat4.hpp b/src/math/mat4.hpp new file mode 100644 index 0000000..dfc5a75 --- /dev/null +++ b/src/math/mat4.hpp @@ -0,0 +1,170 @@ +#ifndef MATH_MAT4_H +#define MATH_MAT4_H + +#include +#include +#include "math/vector.hpp" + +namespace engine::math { + +struct Matrix4 { + static constexpr Matrix4 idty() { + return { + 1.f, 0.f, 0.f, 0.f, + 0.f, 1.f, 0.f, 0.f, + 0.f, 0.f, 1.f, 0.f, + 0.f, 0.f, 0.f, 1.f, + }; + } + + 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, + }; + } + + static constexpr Matrix4 scale(float fac) { + return { + fac, 0.f, 0.f, 0.f, + 0.f, fac, 0.f, 0.f, + 0.f, 0.f, fac, 0.f, + 0.f, 0.f, 0.f, 1.f, + }; + } + + static constexpr Matrix4 scale(const Vector3& facs) { + return { + facs.x, 0.f, 0.f, 0.f, + 0.f, facs.y, 0.f, 0.f, + 0.f, 0.f, facs.z, 0.f, + 0.f, 0.f, 0.f, 1.f, + }; + } + + static constexpr Matrix4 rot_x(float a) { + float c = std::cos(a); + 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, 0.f, 0.f, 1.f, + }; + } + + static constexpr Matrix4 rot_y(float a) { + float c = std::cos(a); + float s = std::sin(a); + return { + c, 0.f, s, 0.f, + 0.f, 1.f, 0.f, 0.f, + -s, 0.f, c, 0.f, + 0.f, 0.f, 0.f, 1.f, + }; + } + + static constexpr Matrix4 rot_z(float a) { + float c = std::cos(a); + float s = std::sin(a); + return { + 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, + }; + } + + static constexpr Matrix4 projection(float fov, float aspect_ratio, float min_z, float max_z) { + float inv_tan = 1.f / std::tan(fov / 2.f); + return {{ + aspect_ratio * inv_tan, 0.f, 0.f, 0.f, + 0.f, -inv_tan, 0.f, 0.f, + 0.f, 0.f, -2.f / (max_z - min_z), -(max_z + min_z) / (max_z - min_z), + 0.f, 0.f, -1.f, 0.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, + 0.f, 0.f, 0.f, 1.f, + }; + } + + std::array values; + + constexpr Matrix4 operator+() const & { + return *this; + } + + constexpr Matrix4 operator-() const & { + return { + -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], + }; + } + + constexpr Matrix4 operator+(const Matrix4& m) const & { + return { + values[ 0] + m.values[ 0], values[ 1] + m.values[ 1], values[ 2] + m.values[ 2], values[ 3] + m.values[ 3], + values[ 4] + m.values[ 4], values[ 5] + m.values[ 5], values[ 6] + m.values[ 6], values[ 7] + m.values[ 7], + values[ 8] + m.values[ 8], values[ 9] + m.values[ 9], values[10] + m.values[10], values[11] + m.values[11], + values[12] + m.values[12], values[13] + m.values[13], values[14] + m.values[14], values[15] + m.values[15], + }; + } + + constexpr Matrix4 operator-(const Matrix4& m) const & { + return *this + (-m); + } + + constexpr Matrix4 operator*(const Matrix4& m) const & { + Matrix4 ret; + for (int i = 0; i < 4; i++) { + for (int j = 0; j < 4; j++) { + ret.values[i * 4 + j] = 0.f; + for (int k = 0; k < 4; k++) + ret.values[i * 4 + j] += values[i * 4 + k] * m.values[k * 4 + j]; + } + } + return ret; + } + + 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, + }; + } + + constexpr std::array 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] }, + }}; + } +}; + +constexpr Matrix4 operator*(float fac, const Matrix4& m) { + return { + fac * m.values[ 0], fac * m.values[ 1], fac * m.values[ 2], fac * m.values[ 3], + fac * m.values[ 4], fac * m.values[ 5], fac * m.values[ 6], fac * m.values[ 7], + fac * m.values[ 8], fac * m.values[ 9], fac * m.values[10], fac * m.values[11], + fac * m.values[12], fac * m.values[13], fac * m.values[14], fac * m.values[15], + }; +} + +} + +#endif // MATH_MAT4_H diff --git a/src/math/quat.h b/src/math/quat.h deleted file mode 100644 index 271070a..0000000 --- a/src/math/quat.h +++ /dev/null @@ -1,78 +0,0 @@ -#ifndef MATH_QUAT_H -#define MATH_QUAT_H - -#include - -namespace engine::math { - -struct Quaternion { - static constexpr Quaternion zero() { - return {0.f, 0.f, 0.f, 0.f}; - } - - static constexpr Quaternion one() { - return {1.f, 0.f, 0.f, 0.f}; - } - - static constexpr Quaternion euler_zxy(float rx, float ry, float rz) { - float ca = std::cos(rx / 2.f), sa = std::sin(rx / 2.f), - cb = std::cos(ry / 2.f), sb = std::sin(ry / 2.f), - cc = std::cos(rz / 2.f), sc = std::sin(rz / 2.f); - return { - ca * cb * cc + sa * sb * sc, - sa * cb * cc + ca * sb * sc, - ca * sb * cc - sa * cb * sc, - ca * cb * sc - sa * sb * cc, - }; - } - - static constexpr Quaternion rot_y(float a) { - return {std::cos(a / 2.f), 0.f, std::sin(a / 2.f), 0.f}; - } - - float w, x, y, z; - - constexpr Quaternion() {} - constexpr Quaternion(float w, float x, float y, float z) : w{w}, x{x}, y{y}, z{z} {} - - constexpr bool operator==(const Quaternion& other) const & { - return w == other.w && x == other.x && y == other.y && z == other.z; - } - - constexpr bool operator!=(const Quaternion& other) const & { - return !(*this == other); - } - - constexpr Quaternion operator+() const & { - return *this; - } - - constexpr Quaternion operator-() const & { - return { -w, -x, -y, -z }; - } - - constexpr Quaternion operator+(const Quaternion& other) const & { - return { w + other.w, x + other.x, y + other.y, z + other.z }; - } - - constexpr Quaternion operator-(const Quaternion& other) const & { - return *this + (-other); - } - - constexpr Quaternion operator*(const Quaternion& other) const & { - return { - w * other.w - x * other.x - y * other.y - z * other.z, - w * other.x + x * other.w + y * other.z - z * other.y, - w * other.y + y * other.w + z * other.x - x * other.z, - w * other.z + z * other.w + x * other.y - y * other.x, - }; - } - - constexpr Quaternion conjugate() const & { - return {w, -x, -y, -z}; - } -}; - -} - -#endif // MATH_QUAT_H diff --git a/src/math/quat.hpp b/src/math/quat.hpp new file mode 100644 index 0000000..271070a --- /dev/null +++ b/src/math/quat.hpp @@ -0,0 +1,78 @@ +#ifndef MATH_QUAT_H +#define MATH_QUAT_H + +#include + +namespace engine::math { + +struct Quaternion { + static constexpr Quaternion zero() { + return {0.f, 0.f, 0.f, 0.f}; + } + + static constexpr Quaternion one() { + return {1.f, 0.f, 0.f, 0.f}; + } + + static constexpr Quaternion euler_zxy(float rx, float ry, float rz) { + float ca = std::cos(rx / 2.f), sa = std::sin(rx / 2.f), + cb = std::cos(ry / 2.f), sb = std::sin(ry / 2.f), + cc = std::cos(rz / 2.f), sc = std::sin(rz / 2.f); + return { + ca * cb * cc + sa * sb * sc, + sa * cb * cc + ca * sb * sc, + ca * sb * cc - sa * cb * sc, + ca * cb * sc - sa * sb * cc, + }; + } + + static constexpr Quaternion rot_y(float a) { + return {std::cos(a / 2.f), 0.f, std::sin(a / 2.f), 0.f}; + } + + float w, x, y, z; + + constexpr Quaternion() {} + constexpr Quaternion(float w, float x, float y, float z) : w{w}, x{x}, y{y}, z{z} {} + + constexpr bool operator==(const Quaternion& other) const & { + return w == other.w && x == other.x && y == other.y && z == other.z; + } + + constexpr bool operator!=(const Quaternion& other) const & { + return !(*this == other); + } + + constexpr Quaternion operator+() const & { + return *this; + } + + constexpr Quaternion operator-() const & { + return { -w, -x, -y, -z }; + } + + constexpr Quaternion operator+(const Quaternion& other) const & { + return { w + other.w, x + other.x, y + other.y, z + other.z }; + } + + constexpr Quaternion operator-(const Quaternion& other) const & { + return *this + (-other); + } + + constexpr Quaternion operator*(const Quaternion& other) const & { + return { + w * other.w - x * other.x - y * other.y - z * other.z, + w * other.x + x * other.w + y * other.z - z * other.y, + w * other.y + y * other.w + z * other.x - x * other.z, + w * other.z + z * other.w + x * other.y - y * other.x, + }; + } + + constexpr Quaternion conjugate() const & { + return {w, -x, -y, -z}; + } +}; + +} + +#endif // MATH_QUAT_H diff --git a/src/math/tform.h b/src/math/tform.h deleted file mode 100644 index 74186f8..0000000 --- a/src/math/tform.h +++ /dev/null @@ -1,49 +0,0 @@ -#ifndef MATH_TFORM_H -#define MATH_TFORM_H - -#include -#include "math/vector.h" -#include "math/mat4.h" -#include "math/quat.h" - -namespace engine::math { - -class Transform { - public: - Vector3 loc; - Quaternion rot; - Vector3 scale; - - constexpr Transform(Vector3 loc, Quaternion rot, Vector3 scale) : loc{loc}, rot{rot}, scale{scale} {} - - constexpr Transform opposite() const & { - return {-loc, rot.conjugate(), 1.f / scale}; - } - - 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, - }; - } - - constexpr Matrix4 to_inverse_mat4() const & { - std::array 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, - }}; - 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, - }; - } -}; - -} - -#endif // MATH_TFORM_H diff --git a/src/math/tform.hpp b/src/math/tform.hpp new file mode 100644 index 0000000..cd9301f --- /dev/null +++ b/src/math/tform.hpp @@ -0,0 +1,49 @@ +#ifndef MATH_TFORM_H +#define MATH_TFORM_H + +#include +#include "math/vector.hpp" +#include "math/mat4.hpp" +#include "math/quat.hpp" + +namespace engine::math { + +class Transform { + public: + Vector3 loc; + Quaternion rot; + Vector3 scale; + + constexpr Transform(Vector3 loc, Quaternion rot, Vector3 scale) : loc{loc}, rot{rot}, scale{scale} {} + + constexpr Transform opposite() const & { + return {-loc, rot.conjugate(), 1.f / scale}; + } + + 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, + }; + } + + constexpr Matrix4 to_inverse_mat4() const & { + std::array 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, + }}; + 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, + }; + } +}; + +} + +#endif // MATH_TFORM_H diff --git a/src/math/vector.h b/src/math/vector.h deleted file mode 100644 index fcfc5bd..0000000 --- a/src/math/vector.h +++ /dev/null @@ -1,236 +0,0 @@ -#ifndef MATH_VECTOR_H -#define MATH_VECTOR_H - -#include -#include "math/quat.h" - -namespace engine::math { - -struct Vector2 { - float x, y; - - constexpr bool operator==(const Vector2& other) const & { - return x == other.x && y == other.y; - } - - constexpr bool operator!=(const Vector2& other) const & { - return !(*this == other); - } - - constexpr Vector2 operator+() const & { - return *this; - } - - constexpr Vector2 operator-() const & { - return { -x, -y }; - } - - constexpr Vector2 operator+(const Vector2& other) const & { - return { x + other.x, y + other.y }; - } - - constexpr Vector2 operator-(const Vector2& other) const & { - return *this + (-other); - } - - constexpr Vector2 operator+=(const Vector2& other) & { - x += other.x; - y += other.y; - return *this; - } - - constexpr float det(const Vector2& other) const & { - return this->x * other.y - other.x * this->y; - } - - constexpr Vector2 round() const & { - return { std::round(x), std::round(y) }; - } - - constexpr Vector2 mul_term(const Vector2& other) const & { - return { x * other.x, y * other.y }; - } -}; - -constexpr Vector2 operator*(float n, const Vector2& other) { - return { n * other.x, n * other.y }; -} - -constexpr Vector2 operator*(const Vector2& other, float n) { - return n * other; -} - -constexpr Vector2 operator/(const Vector2& other, float n) { - return { other.x / n, other.y / n }; -} - -constexpr Vector2 operator+(const Vector2& other, float n) { - return { other.x + n, other.y + n }; -} - -constexpr Vector2 operator-(const Vector2& other, float n) { - return { other.x - n, other.y - n }; -} - -struct Vector3; -constexpr Vector3 operator*(float n, const Vector3& other); -constexpr Vector3 operator/(const Vector3& other, float n); - -struct Vector3 { - static constexpr Vector3 bilerp(const Vector3& v1, const Vector3& v2, const Vector3& v3, float b0, float b1) { - return b0 * v1 + b1 * v2 + (1.f - b0 - b1) * v3; - } - - float x, y, z; - - constexpr bool operator==(const Vector3& other) const & { - return x == other.x && y == other.y && z == other.z; - } - - constexpr bool operator!=(const Vector3& other) const & { - return !(*this == other); - } - - constexpr Vector3 operator+() const & { - return *this; - } - - constexpr Vector3 operator-() const & { - return { -x, -y, -z }; - } - - constexpr Vector3 operator+(const Vector3& other) const & { - return { x + other.x, y + other.y, z + other.z }; - } - - constexpr Vector3 operator-(const Vector3& other) const & { - return *this + (-other); - } - - constexpr Vector3 operator+=(const Vector3& other) & { - x += other.x; - y += other.y; - z += other.z; - return *this; - } - - constexpr Vector3 round() { - return { std::round(x), std::round(y), std::round(z) }; - } - - constexpr Vector3 cross(const Vector3& other) const & { - return { - y * other.z - z * other.y, - z * other.x - x * other.z, - x * other.y - y * other.x - }; - } - - constexpr Vector3 rot(const Quaternion& q) const & { - return { - (2.f * (q.w * q.w + q.x * q.x) - 1.f) * x + (2.f * (q.x * q.y - q.w * q.z) ) * y + (2.f * (q.x * q.z + q.w * q.y) ) * z, - (2.f * (q.x * q.y + q.w * q.z) ) * x + (2.f * (q.w * q.w + q.y * q.y) - 1.f) * y + (2.f * (q.y * q.z - q.w * q.x) ) * z, - (2.f * (q.x * q.z - q.w * q.y) ) * x + (2.f * (q.y * q.z + q.w * q.x) ) * y + (2.f * (q.w * q.w + q.z * q.z) - 1.f) * z, - }; - } - - constexpr float dot(const Vector3& other) const & { - return x * other.x + y * other.y + z * other.z; - } - - constexpr float length_squared() const & { - return dot(*this); - } - - constexpr float length() const & { - return std::sqrt(length_squared()); - } - - constexpr Vector3 normalize() const & { - return *this / length(); - } -}; - -constexpr Vector3 operator*(float n, const Vector3& other) { - return { n * other.x, n * other.y, n * other.z }; -} - -constexpr Vector3 operator*(const Vector3& other, float n) { - return n * other; -} - -constexpr Vector3 operator/(float n, const Vector3& other) { - return { n / other.x, n / other.y, n / other.z }; -} - -constexpr Vector3 operator/(const Vector3& other, float n) { - return { other.x / n, other.y / n, other.z / n }; -} - -struct Vector4 { - float x, y, z, w; - - constexpr Vector4() {} - constexpr Vector4(float x, float y, float z, float w) : x{x}, y{y}, z{z}, w{w} {} - constexpr Vector4(float x, float y, float z) : x{x}, y{y}, z{z}, w{1.f} {} - constexpr Vector4(const Vector2& v, float z, float w) : x{v.x}, y{v.y}, z{z}, w{w} {} - constexpr Vector4(const Vector2& v, float z) : x{v.x}, y{v.y}, z{z}, w{1.f} {} - constexpr Vector4(const Vector3& v, float w) : x{v.x}, y{v.y}, z{v.z}, w{w} {} - constexpr Vector4(const Vector3& v) : x{v.x}, y{v.y}, z{v.z}, w{1.f} {} - - constexpr bool operator==(const Vector4& other) const & { - return x == other.x && y == other.y && z == other.z && w == other.w; - } - - constexpr bool operator!=(const Vector4& other) const & { - return !(*this == other); - } - - constexpr Vector4 operator+() const & { - return *this; - } - - constexpr Vector4 operator-() const & { - return { -x, -y, -z, -w }; - } - - constexpr Vector4 operator+(const Vector4& other) const & { - return { x + other.x, y + other.y, z + other.z, w + other.w }; - } - - constexpr Vector4 operator-(const Vector4& other) const & { - return *this + (-other); - } - - constexpr Vector4 round() const & { - return { std::round(x), std::round(y), std::round(z), std::round(w) }; - } - - constexpr Vector2 xy() const & { - return { x, y }; - } - - constexpr Vector3 xyz() const & { - return { x, y, z }; - } - - constexpr Vector4 div_by_w() const & { - return {x / w, y / w, z / w, w}; - } -}; - -constexpr Vector4 operator*(float n, const Vector4& other) { - return { n * other.x, n * other.y, n * other.z, n * other.w }; -} - -constexpr Vector4 operator*(const Vector4& other, float n) { - return n * other; -} - -constexpr Vector4 operator/(const Vector4& other, float n) { - return { other.x / n, other.y / n, other.z / n, other.w / n }; -} - -} - -#endif // MATH_VECTOR_H diff --git a/src/math/vector.hpp b/src/math/vector.hpp new file mode 100644 index 0000000..a0e58e1 --- /dev/null +++ b/src/math/vector.hpp @@ -0,0 +1,236 @@ +#ifndef MATH_VECTOR_H +#define MATH_VECTOR_H + +#include +#include "math/quat.hpp" + +namespace engine::math { + +struct Vector2 { + float x, y; + + constexpr bool operator==(const Vector2& other) const & { + return x == other.x && y == other.y; + } + + constexpr bool operator!=(const Vector2& other) const & { + return !(*this == other); + } + + constexpr Vector2 operator+() const & { + return *this; + } + + constexpr Vector2 operator-() const & { + return { -x, -y }; + } + + constexpr Vector2 operator+(const Vector2& other) const & { + return { x + other.x, y + other.y }; + } + + constexpr Vector2 operator-(const Vector2& other) const & { + return *this + (-other); + } + + constexpr Vector2 operator+=(const Vector2& other) & { + x += other.x; + y += other.y; + return *this; + } + + constexpr float det(const Vector2& other) const & { + return this->x * other.y - other.x * this->y; + } + + constexpr Vector2 round() const & { + return { std::round(x), std::round(y) }; + } + + constexpr Vector2 mul_term(const Vector2& other) const & { + return { x * other.x, y * other.y }; + } +}; + +constexpr Vector2 operator*(float n, const Vector2& other) { + return { n * other.x, n * other.y }; +} + +constexpr Vector2 operator*(const Vector2& other, float n) { + return n * other; +} + +constexpr Vector2 operator/(const Vector2& other, float n) { + return { other.x / n, other.y / n }; +} + +constexpr Vector2 operator+(const Vector2& other, float n) { + return { other.x + n, other.y + n }; +} + +constexpr Vector2 operator-(const Vector2& other, float n) { + return { other.x - n, other.y - n }; +} + +struct Vector3; +constexpr Vector3 operator*(float n, const Vector3& other); +constexpr Vector3 operator/(const Vector3& other, float n); + +struct Vector3 { + static constexpr Vector3 bilerp(const Vector3& v1, const Vector3& v2, const Vector3& v3, float b0, float b1) { + return b0 * v1 + b1 * v2 + (1.f - b0 - b1) * v3; + } + + float x, y, z; + + constexpr bool operator==(const Vector3& other) const & { + return x == other.x && y == other.y && z == other.z; + } + + constexpr bool operator!=(const Vector3& other) const & { + return !(*this == other); + } + + constexpr Vector3 operator+() const & { + return *this; + } + + constexpr Vector3 operator-() const & { + return { -x, -y, -z }; + } + + constexpr Vector3 operator+(const Vector3& other) const & { + return { x + other.x, y + other.y, z + other.z }; + } + + constexpr Vector3 operator-(const Vector3& other) const & { + return *this + (-other); + } + + constexpr Vector3 operator+=(const Vector3& other) & { + x += other.x; + y += other.y; + z += other.z; + return *this; + } + + constexpr Vector3 round() { + return { std::round(x), std::round(y), std::round(z) }; + } + + constexpr Vector3 cross(const Vector3& other) const & { + return { + y * other.z - z * other.y, + z * other.x - x * other.z, + x * other.y - y * other.x + }; + } + + constexpr Vector3 rot(const Quaternion& q) const & { + return { + (2.f * (q.w * q.w + q.x * q.x) - 1.f) * x + (2.f * (q.x * q.y - q.w * q.z) ) * y + (2.f * (q.x * q.z + q.w * q.y) ) * z, + (2.f * (q.x * q.y + q.w * q.z) ) * x + (2.f * (q.w * q.w + q.y * q.y) - 1.f) * y + (2.f * (q.y * q.z - q.w * q.x) ) * z, + (2.f * (q.x * q.z - q.w * q.y) ) * x + (2.f * (q.y * q.z + q.w * q.x) ) * y + (2.f * (q.w * q.w + q.z * q.z) - 1.f) * z, + }; + } + + constexpr float dot(const Vector3& other) const & { + return x * other.x + y * other.y + z * other.z; + } + + constexpr float length_squared() const & { + return dot(*this); + } + + constexpr float length() const & { + return std::sqrt(length_squared()); + } + + constexpr Vector3 normalize() const & { + return *this / length(); + } +}; + +constexpr Vector3 operator*(float n, const Vector3& other) { + return { n * other.x, n * other.y, n * other.z }; +} + +constexpr Vector3 operator*(const Vector3& other, float n) { + return n * other; +} + +constexpr Vector3 operator/(float n, const Vector3& other) { + return { n / other.x, n / other.y, n / other.z }; +} + +constexpr Vector3 operator/(const Vector3& other, float n) { + return { other.x / n, other.y / n, other.z / n }; +} + +struct Vector4 { + float x, y, z, w; + + constexpr Vector4() {} + constexpr Vector4(float x, float y, float z, float w) : x{x}, y{y}, z{z}, w{w} {} + constexpr Vector4(float x, float y, float z) : x{x}, y{y}, z{z}, w{1.f} {} + constexpr Vector4(const Vector2& v, float z, float w) : x{v.x}, y{v.y}, z{z}, w{w} {} + constexpr Vector4(const Vector2& v, float z) : x{v.x}, y{v.y}, z{z}, w{1.f} {} + constexpr Vector4(const Vector3& v, float w) : x{v.x}, y{v.y}, z{v.z}, w{w} {} + constexpr Vector4(const Vector3& v) : x{v.x}, y{v.y}, z{v.z}, w{1.f} {} + + constexpr bool operator==(const Vector4& other) const & { + return x == other.x && y == other.y && z == other.z && w == other.w; + } + + constexpr bool operator!=(const Vector4& other) const & { + return !(*this == other); + } + + constexpr Vector4 operator+() const & { + return *this; + } + + constexpr Vector4 operator-() const & { + return { -x, -y, -z, -w }; + } + + constexpr Vector4 operator+(const Vector4& other) const & { + return { x + other.x, y + other.y, z + other.z, w + other.w }; + } + + constexpr Vector4 operator-(const Vector4& other) const & { + return *this + (-other); + } + + constexpr Vector4 round() const & { + return { std::round(x), std::round(y), std::round(z), std::round(w) }; + } + + constexpr Vector2 xy() const & { + return { x, y }; + } + + constexpr Vector3 xyz() const & { + return { x, y, z }; + } + + constexpr Vector4 div_by_w() const & { + return {x / w, y / w, z / w, w}; + } +}; + +constexpr Vector4 operator*(float n, const Vector4& other) { + return { n * other.x, n * other.y, n * other.z, n * other.w }; +} + +constexpr Vector4 operator*(const Vector4& other, float n) { + return n * other; +} + +constexpr Vector4 operator/(const Vector4& other, float n) { + return { other.x / n, other.y / n, other.z / n, other.w / n }; +} + +} + +#endif // MATH_VECTOR_H diff --git a/src/o3d/camera.h b/src/o3d/camera.h deleted file mode 100644 index 832f473..0000000 --- a/src/o3d/camera.h +++ /dev/null @@ -1,24 +0,0 @@ -#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 { - 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(); - } -}; - -} - -#endif // O3D_CAMERA_H diff --git a/src/o3d/camera.hpp b/src/o3d/camera.hpp new file mode 100644 index 0000000..485c825 --- /dev/null +++ b/src/o3d/camera.hpp @@ -0,0 +1,24 @@ +#ifndef O3D_CAMERA_H +#define O3D_CAMERA_H + +#include "math/mat4.hpp" +#include "math/tform.hpp" + +using + engine::math::Matrix4, + engine::math::Transform; + +namespace engine::o3d { + +struct Camera { + 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(); + } +}; + +} + +#endif // O3D_CAMERA_H diff --git a/src/o3d/deriv_vertex.h b/src/o3d/deriv_vertex.h deleted file mode 100644 index c435a7e..0000000 --- a/src/o3d/deriv_vertex.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef O3D_DERIV_VERTEX_H -#define O3D_DERIV_VERTEX_H - -#include "math/vector.h" - -namespace engine::o3d { - -using engine::math::Vector4; - -struct DerivedVertex { - Vector4 vertex; - float b0, b1; - - constexpr DerivedVertex div_by_w() const & { - return {vertex.div_by_w(), b0, b1}; - } -}; - -} - -#endif // O3D_DERIV_VERTEX_H diff --git a/src/o3d/deriv_vertex.hpp b/src/o3d/deriv_vertex.hpp new file mode 100644 index 0000000..bc47780 --- /dev/null +++ b/src/o3d/deriv_vertex.hpp @@ -0,0 +1,21 @@ +#ifndef O3D_DERIV_VERTEX_H +#define O3D_DERIV_VERTEX_H + +#include "math/vector.hpp" + +namespace engine::o3d { + +using engine::math::Vector4; + +struct DerivedVertex { + Vector4 vertex; + float b0, b1; + + constexpr DerivedVertex div_by_w() const & { + return {vertex.div_by_w(), b0, b1}; + } +}; + +} + +#endif // O3D_DERIV_VERTEX_H diff --git a/src/o3d/mesh.cpp b/src/o3d/mesh.cpp index 2423e88..c1af5cd 100644 --- a/src/o3d/mesh.cpp +++ b/src/o3d/mesh.cpp @@ -1,8 +1,8 @@ -#include "o3d/mesh.h" +#include "o3d/mesh.hpp" #include #include #include -#include "math/vector.h" +#include "math/vector.hpp" using namespace engine::o3d; diff --git a/src/o3d/mesh.h b/src/o3d/mesh.h deleted file mode 100644 index acc48b5..0000000 --- a/src/o3d/mesh.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef O3D_MESH_H -#define O3D_MESH_H - -#include -#include -#include -#include -#include "math/vector.h" - -namespace engine::o3d { - -using engine::math::Vector3, engine::math::Vector4; - -struct Mesh { - static Mesh plane(float width, float height); - - std::vector vertices; - std::vector normals; - std::vector, 3>> indices; -}; - -} - -#endif // O3D_MESH_H diff --git a/src/o3d/mesh.hpp b/src/o3d/mesh.hpp new file mode 100644 index 0000000..2c3a065 --- /dev/null +++ b/src/o3d/mesh.hpp @@ -0,0 +1,24 @@ +#ifndef O3D_MESH_H +#define O3D_MESH_H + +#include +#include +#include +#include +#include "math/vector.hpp" + +namespace engine::o3d { + +using engine::math::Vector3, engine::math::Vector4; + +struct Mesh { + static Mesh plane(float width, float height); + + std::vector vertices; + std::vector normals; + std::vector, 3>> indices; +}; + +} + +#endif // O3D_MESH_H diff --git a/src/o3d/obj3d.h b/src/o3d/obj3d.h deleted file mode 100644 index 9f407f9..0000000 --- a/src/o3d/obj3d.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef O3D_OBJ3D_H -#define O3D_OBJ3D_H - -#include -#include "math/tform.h" -#include "o3d/mesh.h" - -namespace engine::o3d { - -struct Object3D { - Mesh mesh; - math::Transform transform; -}; - -} - -#endif // O3D_OBJ3D_H diff --git a/src/o3d/obj3d.hpp b/src/o3d/obj3d.hpp new file mode 100644 index 0000000..295d129 --- /dev/null +++ b/src/o3d/obj3d.hpp @@ -0,0 +1,17 @@ +#ifndef O3D_OBJ3D_H +#define O3D_OBJ3D_H + +#include +#include "math/tform.hpp" +#include "o3d/mesh.hpp" + +namespace engine::o3d { + +struct Object3D { + Mesh mesh; + math::Transform transform; +}; + +} + +#endif // O3D_OBJ3D_H diff --git a/src/o3d/scene.h b/src/o3d/scene.h deleted file mode 100644 index 3b9131c..0000000 --- a/src/o3d/scene.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef O3D_SCENE_H -#define O3D_SCENE_H - -#include -#include -#include "o3d/obj3d.h" -#include "o3d/camera.h" - -namespace engine::o3d { - -struct Scene { - Camera camera; - std::vector objs; -}; - -} - -#endif // O3D_SCENE_H diff --git a/src/o3d/scene.hpp b/src/o3d/scene.hpp new file mode 100644 index 0000000..65008ac --- /dev/null +++ b/src/o3d/scene.hpp @@ -0,0 +1,18 @@ +#ifndef O3D_SCENE_H +#define O3D_SCENE_H + +#include +#include +#include "o3d/obj3d.hpp" +#include "o3d/camera.hpp" + +namespace engine::o3d { + +struct Scene { + Camera camera; + std::vector objs; +}; + +} + +#endif // O3D_SCENE_H diff --git a/src/o3d/tri.h b/src/o3d/tri.h deleted file mode 100644 index 4b50f91..0000000 --- a/src/o3d/tri.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef O3D_TRI_H -#define O3D_TRI_H - -#include -#include "o3d/vertex.h" -#include "o3d/tri_deriv.h" - -namespace engine::o3d { - -struct Triangle { - Vertex vertex1; - Vertex vertex2; - Vertex vertex3; - - constexpr TriangleDerived to_derived() const & { - return {{vertex1.vertex, 1.f, 0.f}, {vertex2.vertex, 0.f, 1.f}, {vertex3.vertex, 0.f, 0.f}}; - } -}; - -} - -#endif // O3D_TRI_H diff --git a/src/o3d/tri.hpp b/src/o3d/tri.hpp new file mode 100644 index 0000000..7538d22 --- /dev/null +++ b/src/o3d/tri.hpp @@ -0,0 +1,22 @@ +#ifndef O3D_TRI_H +#define O3D_TRI_H + +#include +#include "o3d/vertex.hpp" +#include "o3d/tri_deriv.hpp" + +namespace engine::o3d { + +struct Triangle { + Vertex vertex1; + Vertex vertex2; + Vertex vertex3; + + constexpr TriangleDerived to_derived() const & { + return {{vertex1.vertex, 1.f, 0.f}, {vertex2.vertex, 0.f, 1.f}, {vertex3.vertex, 0.f, 0.f}}; + } +}; + +} + +#endif // O3D_TRI_H diff --git a/src/o3d/tri_deriv.cpp b/src/o3d/tri_deriv.cpp index a22067e..79e3fb4 100644 --- a/src/o3d/tri_deriv.cpp +++ b/src/o3d/tri_deriv.cpp @@ -1,7 +1,7 @@ -#include "o3d/tri_deriv.h" +#include "o3d/tri_deriv.hpp" #include -#include "math/vector.h" -#include "o3d/deriv_vertex.h" +#include "math/vector.hpp" +#include "o3d/deriv_vertex.hpp" using namespace engine::o3d; diff --git a/src/o3d/tri_deriv.h b/src/o3d/tri_deriv.h deleted file mode 100644 index 16a21f8..0000000 --- a/src/o3d/tri_deriv.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef O3D_TRI_VERTEX_H -#define O3D_TRI_VERTEX_H - -#include -#include "o3d/vertex.h" -#include "o3d/deriv_vertex.h" - -namespace engine::o3d { - -struct TriangleDerived { - DerivedVertex derived_vertex1; - DerivedVertex derived_vertex2; - DerivedVertex derived_vertex3; - - std::vector perspective_crop_xy_out(float x1, float x2, float y1, float y2) const; - std::vector crop_z_out(float z1, float z2) const; - - constexpr TriangleDerived div_by_w() const & { - return {derived_vertex1.div_by_w(), derived_vertex2.div_by_w(), derived_vertex3.div_by_w()}; - } -}; - -} - -#endif // O3D_TRI_VERTEX_H diff --git a/src/o3d/tri_deriv.hpp b/src/o3d/tri_deriv.hpp new file mode 100644 index 0000000..f82bd9b --- /dev/null +++ b/src/o3d/tri_deriv.hpp @@ -0,0 +1,25 @@ +#ifndef O3D_TRI_VERTEX_H +#define O3D_TRI_VERTEX_H + +#include +#include "o3d/vertex.hpp" +#include "o3d/deriv_vertex.hpp" + +namespace engine::o3d { + +struct TriangleDerived { + DerivedVertex derived_vertex1; + DerivedVertex derived_vertex2; + DerivedVertex derived_vertex3; + + std::vector perspective_crop_xy_out(float x1, float x2, float y1, float y2) const; + std::vector crop_z_out(float z1, float z2) const; + + constexpr TriangleDerived div_by_w() const & { + return {derived_vertex1.div_by_w(), derived_vertex2.div_by_w(), derived_vertex3.div_by_w()}; + } +}; + +} + +#endif // O3D_TRI_VERTEX_H diff --git a/src/o3d/vertex.h b/src/o3d/vertex.h deleted file mode 100644 index 438f8b3..0000000 --- a/src/o3d/vertex.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef O3D_VERTEX_H -#define O3D_VERTEX_H - -#include "math/vector.h" -#include "o3d/vertex_data.h" - -namespace engine::o3d { - -using engine::math::Vector3, engine::math::Vector4; - -struct Vertex { - Vector4 vertex; - Vector3 normal; - VertexData data; -}; - -} - -#endif // O3D_VERTEX_H diff --git a/src/o3d/vertex.hpp b/src/o3d/vertex.hpp new file mode 100644 index 0000000..70fca7b --- /dev/null +++ b/src/o3d/vertex.hpp @@ -0,0 +1,19 @@ +#ifndef O3D_VERTEX_H +#define O3D_VERTEX_H + +#include "math/vector.hpp" +#include "o3d/vertex_data.hpp" + +namespace engine::o3d { + +using engine::math::Vector3, engine::math::Vector4; + +struct Vertex { + Vector4 vertex; + Vector3 normal; + VertexData data; +}; + +} + +#endif // O3D_VERTEX_H diff --git a/src/o3d/vertex_data.h b/src/o3d/vertex_data.h deleted file mode 100644 index 22fa76b..0000000 --- a/src/o3d/vertex_data.h +++ /dev/null @@ -1,29 +0,0 @@ -#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.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.world_loc + b1 * vd2.world_loc + (1.f - b0 - b1) * vd3.world_loc, - }; - } - - Vector3 world_loc; -}; - -} - -#endif // O3D_VERTEX_DATA_H diff --git a/src/o3d/vertex_data.hpp b/src/o3d/vertex_data.hpp new file mode 100644 index 0000000..0ebc6c9 --- /dev/null +++ b/src/o3d/vertex_data.hpp @@ -0,0 +1,29 @@ +#ifndef O3D_VERTEX_DATA_H +#define O3D_VERTEX_DATA_H + +#include "math/vector.hpp" + +using + engine::math::Vector3; + +namespace engine::o3d { + +struct VertexData { + static constexpr VertexData lerp(const VertexData& vd1, const VertexData& vd2, float b0) { + return { + 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.world_loc + b1 * vd2.world_loc + (1.f - b0 - b1) * vd3.world_loc, + }; + } + + Vector3 world_loc; +}; + +} + +#endif // O3D_VERTEX_DATA_H diff --git a/src/obj_parser.cpp b/src/obj_parser.cpp index f765f5c..196261e 100644 --- a/src/obj_parser.cpp +++ b/src/obj_parser.cpp @@ -1,4 +1,4 @@ -#include "obj_parser.h" +#include "obj_parser.hpp" #include #include @@ -6,9 +6,9 @@ #include #include #include -#include "math/vector.h" -#include "o3d/mesh.h" -#include "o3d/vertex_data.h" +#include "math/vector.hpp" +#include "o3d/mesh.hpp" +#include "o3d/vertex_data.hpp" namespace engine { diff --git a/src/obj_parser.h b/src/obj_parser.h deleted file mode 100644 index 2a6d791..0000000 --- a/src/obj_parser.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef OBJ_PARSER_H -#define OBJ_PARSER_H - -#include "o3d/mesh.h" - -namespace engine { - -o3d::Mesh parse_object(const std::string& obj_path); - -} - -#endif // OBJ_PARSER_H diff --git a/src/obj_parser.hpp b/src/obj_parser.hpp new file mode 100644 index 0000000..f6e8ca1 --- /dev/null +++ b/src/obj_parser.hpp @@ -0,0 +1,12 @@ +#ifndef OBJ_PARSER_H +#define OBJ_PARSER_H + +#include "o3d/mesh.hpp" + +namespace engine { + +o3d::Mesh parse_object(const std::string& obj_path); + +} + +#endif // OBJ_PARSER_H diff --git a/src/renderer.cpp b/src/renderer.cpp index 7e868b4..5321a88 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -1,11 +1,11 @@ -#include "renderer.h" +#include "renderer.hpp" #include #include -#include "math/vector.h" -#include "o3d/tri.h" -#include "o3d/tri_deriv.h" -#include "fb/chfb.h" -#include "fb/pixfb.h" +#include "math/vector.hpp" +#include "o3d/tri.hpp" +#include "o3d/tri_deriv.hpp" +#include "fb/chfb.hpp" +#include "fb/pixfb.hpp" using namespace engine; using diff --git a/src/renderer.h b/src/renderer.h deleted file mode 100644 index b7a3a7e..0000000 --- a/src/renderer.h +++ /dev/null @@ -1,41 +0,0 @@ -#ifndef RENDERER_H -#define RENDERER_H - -#include -#include "math/vector.h" -#include "o3d/tri.h" -#include "o3d/tri_deriv.h" - -namespace engine { - -using math::Vector3, o3d::Triangle; - -template -class Renderer { - public: - FrameBuffer fb; - - template - Renderer(FBArg&& fb); - - void resize(unsigned int w, unsigned int h); - void clear(); - void draw_triangle(const Triangle& triangle); - - constexpr unsigned int width() const & { - return fb.width(); - } - - constexpr unsigned int height() const & { - return fb.height(); - } - - private: - std::vector depth_buf; - - void _draw_cropped_triangle(const o3d::Triangle& root, const o3d::TriangleDerived& triangle); -}; - -} - -#endif // RENDERER_H diff --git a/src/renderer.hpp b/src/renderer.hpp new file mode 100644 index 0000000..9093c2e --- /dev/null +++ b/src/renderer.hpp @@ -0,0 +1,41 @@ +#ifndef RENDERER_H +#define RENDERER_H + +#include +#include "math/vector.hpp" +#include "o3d/tri.hpp" +#include "o3d/tri_deriv.hpp" + +namespace engine { + +using math::Vector3, o3d::Triangle; + +template +class Renderer { + public: + FrameBuffer fb; + + template + Renderer(FBArg&& fb); + + void resize(unsigned int w, unsigned int h); + void clear(); + void draw_triangle(const Triangle& triangle); + + constexpr unsigned int width() const & { + return fb.width(); + } + + constexpr unsigned int height() const & { + return fb.height(); + } + + private: + std::vector depth_buf; + + void _draw_cropped_triangle(const o3d::Triangle& root, const o3d::TriangleDerived& triangle); +}; + +} + +#endif // RENDERER_H -- cgit v1.2.3