Line data Source code
1 : #include <cstdlib>
2 : #include <wren/application.hpp>
3 : #include <wren/assets/manager.hpp>
4 : #include <wren/logging/log.hpp>
5 : #include <wren/physics/ray.hpp>
6 : #include <wren/scene/components/collider.hpp>
7 : #include <wren/scene/components/mesh.hpp>
8 : #include <wren/scene/entity.hpp>
9 : #include <wren/scene/scene.hpp>
10 :
11 : namespace components = wren::scene::components;
12 :
13 : struct GameData {
14 : std::shared_ptr<wren::Application> app;
15 : std::shared_ptr<wren::scene::Scene> scene;
16 : wren::math::Mat4f ortho_proj;
17 : };
18 :
19 0 : auto initialize(const GameData& data) -> wren::expected<void> {
20 0 : wren::log::info("Intializing shaders");
21 :
22 0 : std::vector<std::filesystem::path> asset_paths = {
23 : #ifdef WREN_BUILD_ASSETS_DIR
24 0 : WREN_BUILD_ASSETS_DIR
25 : #endif
26 : };
27 :
28 0 : wren::assets::Manager asset_manager(asset_paths);
29 0 : TRY_RESULT(const auto asset_path,
30 : asset_manager.find_asset("shaders/mesh.wren_shader"));
31 :
32 0 : TRY_RESULT(
33 : auto mesh_shader,
34 : wren::vk::Shader::create(
35 : data.app->context()->graphics_context->Device().get(), asset_path));
36 :
37 0 : wren::GraphBuilder builder(data.app->context());
38 :
39 : // TODO Get Scene here
40 : auto render_query =
41 0 : data.scene->world()
42 0 : .query_builder<const wren::scene::components::Transform,
43 : wren::scene::components::MeshRenderer>()
44 0 : .build();
45 0 : builder
46 0 : .add_pass(
47 0 : "main",
48 0 : wren::PassResources("swapchain_target")
49 0 : .add_shader("mesh", mesh_shader)
50 0 : .add_colour_target(),
51 0 : [data, app = data.app, mesh_shader, render_query](
52 : wren::RenderPass& pass, const vk::CommandBuffer& cmd) {
53 : struct GLOBALS {
54 0 : wren::math::Mat4f view = wren::math::Mat4f::identity();
55 0 : wren::math::Mat4f proj = wren::math::Mat4f::identity();
56 : };
57 0 : GLOBALS ubo{};
58 :
59 : // ubo.view = this->camera_.transform().matrix();
60 0 : ubo.proj = data.ortho_proj;
61 :
62 0 : pass.bind_pipeline("mesh");
63 0 : pass.write_scratch_buffer(cmd, 0, 0, ubo);
64 :
65 0 : render_query.each(
66 0 : [cmd, app, mesh_shader](
67 : const wren::scene::components::Transform& transform,
68 : wren::scene::components::MeshRenderer& mesh_renderer) {
69 0 : mesh_renderer.bind(app->context(), mesh_shader, cmd,
70 0 : transform.matrix());
71 0 : });
72 0 : })
73 0 : .build()
74 0 : .value();
75 :
76 0 : return {};
77 0 : }
78 :
79 : void update(const std::shared_ptr<wren::scene::Scene>& scene);
80 :
81 0 : auto main() -> int {
82 : // wren::log::set_level(spdlog::level::debug);
83 :
84 0 : const auto& err = wren::Application::create("nand");
85 0 : if (!err.has_value()) {
86 0 : wren::log::error("failed to create application: {}", err.error().message());
87 0 : return EXIT_FAILURE;
88 : }
89 :
90 0 : const auto app = err.value();
91 0 : const auto scene = wren::scene::Scene::create();
92 :
93 0 : GameData data{
94 0 : .app = app,
95 0 : .scene = scene,
96 0 : .ortho_proj = wren::math::ortho(0.0F, 80.0F, 0.0F, 60.0F, 0.1F, 100.0F)};
97 :
98 0 : const auto init_err = initialize(data);
99 0 : if (!init_err.has_value()) {
100 0 : wren::log::error("failed to initialize: {}", init_err.error().message());
101 0 : return EXIT_FAILURE;
102 : }
103 :
104 0 : auto quad = scene->create_entity("Quad");
105 0 : quad.add_component<components::MeshRenderer>();
106 0 : auto& mesh_renderer = quad.get_component<components::MeshRenderer>();
107 0 : mesh_renderer.update_mesh(wren::Mesh::create_quad());
108 :
109 0 : quad.add_component<wren::scene::components::BoxCollider2D::Ptr>(
110 0 : new components::BoxCollider2D());
111 :
112 0 : app->add_callback_to_update_phase([scene](auto) { update(scene); });
113 :
114 0 : quad.get_component<components::Transform>().position.z(1);
115 :
116 0 : app->run();
117 :
118 0 : return EXIT_SUCCESS;
119 0 : }
120 :
121 0 : void update(const std::shared_ptr<wren::scene::Scene>& scene) {
122 0 : wren::physics::RayHit hit;
123 0 : wren::physics::Ray ray;
124 0 : ray.origin = wren::math::Vec3f{100, 0, 1};
125 0 : ray.direction = wren::math::Vec3f{0, 0, -1};
126 0 : if (wren::physics::raycast(scene->world(), ray, hit)) {
127 0 : wren::log::info("Hit object");
128 0 : }
129 0 : }
|