sandbox
Loading...
Searching...
No Matches
skybox_subrenderer.hpp
1// SPDX-License-Identifier: MIT
2#ifndef LIBSBX_SCENES_SKYBOX_SUBRENDERER_HPP_
3#define LIBSBX_SCENES_SKYBOX_SUBRENDERER_HPP_
4
5#include <libsbx/graphics/subrenderer.hpp>
6
7#include <libsbx/graphics/images/cube_image.hpp>
8
9#include <libsbx/graphics/pipeline/graphics_pipeline.hpp>
10#include <libsbx/graphics/pipeline/vertex_input_description.hpp>
11#include <libsbx/graphics/pipeline/mesh.hpp>
12
13#include <libsbx/graphics/descriptor/descriptor_handler.hpp>
14
15#include <libsbx/graphics/buffers/uniform_handler.hpp>
16
17#include <libsbx/assets/assets_module.hpp>
18
19#include <libsbx/scenes/components/skybox.hpp>
20#include <libsbx/scenes/components/camera.hpp>
21
22namespace sbx::scenes {
23
24struct vertex3d {
25 math::vector3 position;
26}; // struct vertex3d
27
28class mesh : public graphics::mesh<vertex3d> {
29
30public:
31
32 mesh(std::vector<vertex3d>&& vertices, std::vector<std::uint32_t>&& indices)
33 : graphics::mesh<vertex3d>{std::move(vertices), std::move(indices)} { }
34
35 ~mesh() override = default;
36
37}; // class mesh
38
40
41 class pipeline : public graphics::graphics_pipeline {
42
43 inline static const auto pipeline_definition = graphics::pipeline_definition{
44 .depth = graphics::depth::read_only,
45 .compare_operation = graphics::compare_operation::less_or_equal,
46 .uses_transparency = false,
47 .rasterization_state = graphics::rasterization_state{
48 .polygon_mode = graphics::polygon_mode::fill,
49 .cull_mode = graphics::cull_mode::none,
50 .front_face = graphics::front_face::counter_clockwise
51 }
52 };
53
54 using base_type = graphics::graphics_pipeline;
55
56 public:
57
58 pipeline(const std::filesystem::path& path, const std::vector<graphics::attachment_description>& attachments)
59 : base_type{path, attachments, pipeline_definition} { }
60
61 ~pipeline() override = default;
62
63 }; // class pipeline
64
65public:
66
67 skybox_subrenderer(const std::vector<graphics::attachment_description>& attachments, const std::filesystem::path& path)
69 _pipeline{path, attachments},
70 _descriptor_handler{_pipeline, 0u},
71 _push_handler{_pipeline} { }
72
73 ~skybox_subrenderer() override = default;
74
75 auto render(graphics::command_buffer& command_buffer) -> void override {
76 auto& assets_module = core::engine::get_module<assets::assets_module>();
77 auto& graphics_module = core::engine::get_module<graphics::graphics_module>();
78 auto& scenes_module = core::engine::get_module<scenes::scenes_module>();
79 auto& scene = scenes_module.scene();
80
81 const auto camera_node = scene.camera();
82
83 if (!scene.has_component<scenes::skybox>(camera_node)) {
84 utility::logger<"scenes">::warn("Skybox subrenderer: No camera node with skybox component found");
85 return;
86 }
87
88 const auto& skybox = scene.get_component<scenes::skybox>(camera_node);
89
90 _pipeline.bind(command_buffer);
91
92 _descriptor_handler.push("scene", scene.uniform_handler());
93 _descriptor_handler.push("skybox", graphics_module.get_resource<graphics::cube_image>(skybox.cube_image));
94
95 _push_handler.push("tint", skybox.tint);
96
97 if (!_descriptor_handler.update(_pipeline)) {
98 return;
99 }
100
101 _descriptor_handler.bind_descriptors(command_buffer);
102 _push_handler.bind(command_buffer);
103
104 command_buffer.draw(3u, 1u, 0u, 0u);
105 }
106
107private:
108
109 pipeline _pipeline;
110
111 graphics::descriptor_handler _descriptor_handler;
112
113 graphics::push_handler _push_handler;
114
115}; // class skybox_subrenderer
116
117} // namespace sbx::scenes
118
119#endif // LIBSBX_SCENES_SKYBOX_SUBRENDERER_HPP_
Definition: command_buffer.hpp:15
Definition: cube_image.hpp:21
Definition: descriptor_handler.hpp:26
Definition: graphics_pipeline.hpp:113
Definition: mesh.hpp:39
Definition: push_handler.hpp:18
Definition: subrenderer.hpp:14
Definition: vector3.hpp:23
Definition: skybox_subrenderer.hpp:28
Definition: scene.hpp:70
Definition: scenes_module.hpp:21
Definition: skybox_subrenderer.hpp:39
Definition: logger.hpp:124
Definition: graphics_pipeline.hpp:104
Definition: graphics_pipeline.hpp:65
Definition: skybox.hpp:13
Definition: skybox_subrenderer.hpp:24