diff --git a/data/test_assets/triangle.vert b/data/test_assets/triangle.vert index fd74d90..fb16d7c 100644 --- a/data/test_assets/triangle.vert +++ b/data/test_assets/triangle.vert @@ -1,21 +1,26 @@ #version 450 core -vec2 positions[3] = vec2[]( - vec2(0.0, -0.5), - vec2(0.5, 0.5), - vec2(-0.5, 0.5) +layout(push_constant ) uniform pc { + mat4 view_projection; +}; + +vec3 positions[3] = vec3[]( + vec3(0.0, -0.5, 0.5), + vec3(0.5, 0.5, 0.5), + vec3(-0.5, 0.5, 0.5) ); vec3 colors[3] = vec3[]( - vec3(1.0, 0.0, 0.0), - vec3(0.0, 1.0, 0.0), - vec3(0.0, 0.0, 1.0) + vec3(0.0, 0.0, 0.0), + vec3(0.0, 0.0, 0.0), + vec3(0.0, 0.0, 0.0) ); layout(location = 0) out vec3 out_frag_color; void main() { - gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0); + gl_Position = view_projection * vec4(positions[gl_VertexIndex], 1.0); out_frag_color = colors[gl_VertexIndex]; } + diff --git a/data/test_assets/triangle.vert.asset b/data/test_assets/triangle.vert.asset index 52d32ab..6ffe543 100644 Binary files a/data/test_assets/triangle.vert.asset and b/data/test_assets/triangle.vert.asset differ diff --git a/modules/camera/CMakeLists.txt b/modules/camera/CMakeLists.txt index 35083e5..e45b507 100644 --- a/modules/camera/CMakeLists.txt +++ b/modules/camera/CMakeLists.txt @@ -1,3 +1,3 @@ -add_library_module(camera camera.cpp scene.cpp) +add_library_module(camera) -target_link_libraries(camera PUBLIC math) +target_link_libraries(camera INTERFACE math) diff --git a/modules/camera/private/camera.cpp b/modules/camera/private/camera.cpp deleted file mode 100644 index e8862f7..0000000 --- a/modules/camera/private/camera.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include - -namespace lt { - - -} diff --git a/modules/camera/private/scene.cpp b/modules/camera/private/scene.cpp deleted file mode 100644 index c7d174d..0000000 --- a/modules/camera/private/scene.cpp +++ /dev/null @@ -1,84 +0,0 @@ -#include -#include -#include -#include - -namespace lt { - -SceneCamera::SceneCamera() - : m_orthographic_specification { .size = 1000.0f, .near_plane = -1.0f, .far_plane = 10000.0f } - , m_perspective_specification { .vertical_fov = math::radians(45.0f), - .near_plane = 0.01f, - .far_plane = 10000.0f } - , m_aspect_ratio(16.0f / 9.0f) - -{ - calculate_projection(); -} - -void SceneCamera::set_viewport_size(unsigned int width, unsigned int height) -{ - m_aspect_ratio = static_cast(width) / static_cast(height); - calculate_projection(); -} - -void SceneCamera::set_projection_type(ProjectionType projection_type) -{ - m_projection_type = projection_type; - calculate_projection(); -} - -void SceneCamera::set_orthographic_size(float size) -{ - m_orthographic_specification.size = size; - calculate_projection(); -} - -void SceneCamera::set_orthographic_far_plane(float far_plane) -{ - m_orthographic_specification.far_plane = far_plane; - calculate_projection(); -} - -void SceneCamera::set_orthographic_near_plane(float near_plane) -{ - m_orthographic_specification.near_plane = near_plane; - calculate_projection(); -} - -void SceneCamera::set_perspective_vertical_fov(float vertical_fov) -{ - m_perspective_specification.vertical_fov = vertical_fov; - calculate_projection(); -} - -void SceneCamera::set_perspective_far_plane(float far_plane) -{ - m_perspective_specification.far_plane = far_plane; - calculate_projection(); -} - -void SceneCamera::set_perspective_near_plane(float near_plane) -{ - m_perspective_specification.near_plane = near_plane; - calculate_projection(); -} - -void SceneCamera::calculate_projection() -{ - // TODO(Light): implement ortho perspective - if (m_projection_type == ProjectionType::Orthographic) - { - // throw std::runtime_error { "ortho perspective not supported yet" }; - } - - // defaults to perspective for now... - m_projection = math::perspective( - m_perspective_specification.vertical_fov, - m_aspect_ratio, - m_perspective_specification.near_plane, - m_perspective_specification.far_plane - ); -} - -} // namespace lt diff --git a/modules/camera/public/camera.hpp b/modules/camera/public/camera.hpp deleted file mode 100644 index b6c4fa7..0000000 --- a/modules/camera/public/camera.hpp +++ /dev/null @@ -1,35 +0,0 @@ -#pragma once - -#include -#include - -namespace lt { - -class Camera -{ -public: - Camera() = default; - - [[nodiscard]] auto get_projection() const -> const math::mat4 & - { - return m_projection; - } - - [[nodiscard]] auto get_background_color() const -> const math::vec4 & - { - return m_background_color; - } - - void set_background_color(const math::vec4 &color) - { - m_background_color = color; - } - -protected: - math::mat4 m_projection; - -private: - math::vec4 m_background_color = math::vec4(1.0f, 0.0f, 0.0f, 1.0f); -}; - -} // namespace lt diff --git a/modules/camera/public/component.hpp b/modules/camera/public/component.hpp deleted file mode 100644 index 0bd75ea..0000000 --- a/modules/camera/public/component.hpp +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once - -#include - -namespace lt { - -struct CameraComponent -{ - CameraComponent() = default; - - CameraComponent(const CameraComponent &) = default; - - CameraComponent(SceneCamera _camera, bool _isPrimary = false) - : camera(_camera) - , isPrimary(_isPrimary) - { - } - - operator SceneCamera() const - { - return camera; - } - - SceneCamera camera; - - bool isPrimary {}; -}; - -} // namespace lt diff --git a/modules/camera/public/components.hpp b/modules/camera/public/components.hpp new file mode 100644 index 0000000..cae4b0e --- /dev/null +++ b/modules/camera/public/components.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include + +namespace lt::camera::components { + +struct PerspectiveCamera +{ + float vertical_fov {}; + + float near_plane {}; + + float far_plane {}; + + float aspect_ratio {}; + + math::vec4 background_color; + + bool is_primary {}; +}; + +} // namespace lt::camera::components diff --git a/modules/camera/public/scene.hpp b/modules/camera/public/scene.hpp deleted file mode 100644 index 9d02897..0000000 --- a/modules/camera/public/scene.hpp +++ /dev/null @@ -1,100 +0,0 @@ -#pragma once - -#include - -namespace lt { - -class SceneCamera: public Camera -{ -public: - enum class ProjectionType - { - Orthographic = 0, - Perspetcive = 1 - }; - - struct OrthographicSpecification - { - float size; - - float near_plane; - - float far_plane; - }; - - struct PerspectiveSpecification - { - float vertical_fov; - - float near_plane; - - float far_plane; - }; - - SceneCamera(); - - void set_viewport_size(unsigned int width, unsigned int height); - - void set_projection_type(ProjectionType projection_type); - - void set_orthographic_size(float size); - - void set_orthographic_far_plane(float far_plane); - - void set_orthographic_near_plane(float near_plane); - - void set_perspective_vertical_fov(float vertical_fov); - - void set_perspective_far_plane(float far_plane); - - void set_perspective_near_plane(float near_plane); - - [[nodiscard]] auto get_orthographic_size() const -> float - { - return m_orthographic_specification.size; - } - - [[nodiscard]] auto get_orthographic_far_plane() const -> float - { - return m_orthographic_specification.far_plane; - } - - [[nodiscard]] auto get_orthographic_near_plane() const -> float - { - return m_orthographic_specification.near_plane; - } - - [[nodiscard]] auto get_perspective_vertical_fov() const -> float - { - return m_perspective_specification.vertical_fov; - } - - [[nodiscard]] auto get_perspective_far_plane() const -> float - { - return m_perspective_specification.far_plane; - } - - [[nodiscard]] auto get_perspective_near_plane() const -> float - { - return m_perspective_specification.near_plane; - } - - [[nodiscard]] auto get_projection_type() const -> ProjectionType - { - return m_projection_type; - } - - -private: - OrthographicSpecification m_orthographic_specification; - - PerspectiveSpecification m_perspective_specification; - - float m_aspect_ratio; - - ProjectionType m_projection_type { ProjectionType::Orthographic }; - - void calculate_projection(); -}; - -} // namespace lt diff --git a/modules/math/public/algebra.hpp b/modules/math/public/algebra.hpp index af2ed26..f285420 100644 --- a/modules/math/public/algebra.hpp +++ b/modules/math/public/algebra.hpp @@ -31,25 +31,29 @@ namespace lt::math { * * the 1 at [z][3] is to save the Z axis into the resulting W for perspective division. * - * thanks to pikuma: https://www.youtube.com/watch?v=EqNcqBdrNyI + * @ref Thanks to pikuma for explaining the math behind this: + * https://www.youtube.com/watch?v=EqNcqBdrNyI */ template constexpr auto perspective(T field_of_view, T aspect_ratio, T z_near, T z_far) { const T half_fov_tan = std::tan(field_of_view / static_cast(2)); - auto result = mat4_impl { T { 0 } }; + auto result = mat4_impl::identity(); result[0][0] = T { 1 } / (aspect_ratio * half_fov_tan); - + // result[1][1] = T { 1 } / (half_fov_tan); - - result[2][2] = -(z_far + z_near) / (z_far - z_near); - + // + // result[2][2] = -(z_far + z_near) / (z_far - z_near); + // + result[2][2] = z_far / (z_far - z_near); + // result[2][3] = -T { 1 }; - - result[3][2] = -(T { 2 } * z_far * z_near) / (z_far - z_near); - + // + // result[3][2] = -(T { 2 } * z_far * z_near) / (z_far - z_near); + result[3][2] = -(z_far * z_near) / (z_far - z_near); + // return result; } diff --git a/modules/math/public/mat4.hpp b/modules/math/public/mat4.hpp index c94dd12..242b907 100644 --- a/modules/math/public/mat4.hpp +++ b/modules/math/public/mat4.hpp @@ -9,6 +9,7 @@ template struct mat4_impl { using Column_T = vec4_impl; + constexpr explicit mat4_impl(T scalar = 0) : values( { @@ -43,7 +44,7 @@ struct mat4_impl { } - [[nodiscard]] constexpr auto identity() -> mat4_impl + [[nodiscard]] static constexpr auto identity() -> mat4_impl { return mat4_impl { { 1 }, {}, {}, {}, // diff --git a/modules/mirror/CMakeLists.txt b/modules/mirror/CMakeLists.txt index 4934d14..d79fae4 100644 --- a/modules/mirror/CMakeLists.txt +++ b/modules/mirror/CMakeLists.txt @@ -1,5 +1,6 @@ add_library_module(libmirror) -target_link_libraries(libmirror INTERFACE app time input surface renderer) +target_link_libraries(libmirror INTERFACE app time input surface renderer + camera) add_test_module( libmirror layers/editor_layer.test.cpp panels/asset_browser.test.cpp diff --git a/modules/mirror/private/entrypoint/mirror.cpp b/modules/mirror/private/entrypoint/mirror.cpp index d99b242..ce985d7 100644 --- a/modules/mirror/private/entrypoint/mirror.cpp +++ b/modules/mirror/private/entrypoint/mirror.cpp @@ -2,9 +2,11 @@ #include #include #include +#include #include #include