From 8bedf8a366cb6c1179bc89678c863517b9356d48 Mon Sep 17 00:00:00 2001 From: Frederico Linhares Date: Sat, 21 Jun 2025 16:51:22 -0300 Subject: refa Remove View2D View2D is almost useless and add too much complexity for the engine, so I am removing it. --- src/blu_cat/gra/graphics_pipeline_2d_solid.cpp | 36 ++--- src/blu_cat/gra/graphics_pipeline_2d_solid.hpp | 8 +- src/blu_cat/gra/graphics_pipeline_2d_wired.cpp | 37 ++--- src/blu_cat/gra/graphics_pipeline_2d_wired.hpp | 10 +- src/blu_cat/gra/graphics_pipeline_3d.cpp | 6 +- src/blu_cat/gra/graphics_pipeline_3d.hpp | 6 +- src/blu_cat/gra/graphics_pipeline_3d_skeletal.cpp | 6 +- src/blu_cat/gra/graphics_pipeline_3d_skeletal.hpp | 6 +- src/blu_cat/gra/graphics_pipeline_sprite_3d.cpp | 6 +- src/blu_cat/gra/graphics_pipeline_sprite_3d.hpp | 6 +- src/blu_cat/gra/renderer.cpp | 163 ++++++++++++++++------ src/blu_cat/gra/renderer.hpp | 28 ++-- src/blu_cat/gra/texture.cpp | 6 +- src/blu_cat/gra/view.cpp | 158 +++++++++++++++++++++ src/blu_cat/gra/view.hpp | 55 ++++++++ src/blu_cat/gra/view_2d.cpp | 160 --------------------- src/blu_cat/gra/view_2d.hpp | 60 -------- src/blu_cat/gra/view_3d.cpp | 155 -------------------- src/blu_cat/gra/view_3d.hpp | 48 ------- src/blu_cat/int/core.cpp | 10 +- src/candy_gear/candy_gear.cpp | 41 ++---- src/candy_gear/core.cpp | 6 +- src/candy_gear/sprite.cpp | 11 +- src/candy_gear/vector_4d.cpp | 12 +- src/candy_gear/view.cpp | 127 +++++++++++++++++ src/candy_gear/view.hpp | 27 ++++ src/candy_gear/view_2d.cpp | 90 ------------ src/candy_gear/view_2d.hpp | 33 ----- src/candy_gear/view_3d.cpp | 127 ----------------- src/candy_gear/view_3d.hpp | 27 ---- 30 files changed, 599 insertions(+), 872 deletions(-) create mode 100644 src/blu_cat/gra/view.cpp create mode 100644 src/blu_cat/gra/view.hpp delete mode 100644 src/blu_cat/gra/view_2d.cpp delete mode 100644 src/blu_cat/gra/view_2d.hpp delete mode 100644 src/blu_cat/gra/view_3d.cpp delete mode 100644 src/blu_cat/gra/view_3d.hpp create mode 100644 src/candy_gear/view.cpp create mode 100644 src/candy_gear/view.hpp delete mode 100644 src/candy_gear/view_2d.cpp delete mode 100644 src/candy_gear/view_2d.hpp delete mode 100644 src/candy_gear/view_3d.cpp delete mode 100644 src/candy_gear/view_3d.hpp (limited to 'src') diff --git a/src/blu_cat/gra/graphics_pipeline_2d_solid.cpp b/src/blu_cat/gra/graphics_pipeline_2d_solid.cpp index 7acca09..05dcbe1 100644 --- a/src/blu_cat/gra/graphics_pipeline_2d_solid.cpp +++ b/src/blu_cat/gra/graphics_pipeline_2d_solid.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2022-2024 Frederico de Oliveira Linhares + * Copyright 2022-2025 Frederico de Oliveira Linhares * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -235,26 +235,25 @@ GraphicsPipeline2DSolid::~GraphicsPipeline2DSolid() void GraphicsPipeline2DSolid::draw( - std::shared_ptr view, const VkCommandBuffer draw_command_buffer, - const size_t current_frame, const size_t next_frame, - const uint32_t image_index) + const VkCommandBuffer draw_command_buffer, const size_t current_frame, + const size_t next_frame, const uint32_t image_index) { // TODO set viewport just once per view, not once per pipeline. { // Set viewport VkViewport vk_viewport{}; - vk_viewport.x = view->region.x; - vk_viewport.y = view->region.y; - vk_viewport.width = view->region.z; - vk_viewport.height = view->region.w; + vk_viewport.x = 0; + vk_viewport.y = 0; + vk_viewport.width = static_cast(BluCat::INT::core.display_width); + vk_viewport.height = static_cast(BluCat::INT::core.display_height); vk_viewport.minDepth = 0.0f; vk_viewport.maxDepth = 1.0f; vkCmdSetViewport(draw_command_buffer, 0, 1, &vk_viewport); VkRect2D vk_scissor{}; - vk_scissor.offset.x = static_cast(view->region.x); - vk_scissor.offset.y = static_cast(view->region.y); - vk_scissor.extent.width = static_cast(view->region.z); - vk_scissor.extent.height = static_cast(view->region.w); + vk_scissor.offset.x = 0; + vk_scissor.offset.y = 0; + vk_scissor.extent.width = BluCat::INT::core.display_width; + vk_scissor.extent.height = BluCat::INT::core.display_height; vkCmdSetScissor(draw_command_buffer, 0, 1, &vk_scissor); } @@ -264,14 +263,17 @@ GraphicsPipeline2DSolid::draw( // FIXME: I know sorting is expensive, but I need to figure out better what // to do here. - std::sort(view->sprites_to_draw[current_frame].begin(), - view->sprites_to_draw[current_frame].end()); + std::sort(BluCat::INT::core.vk_renderer->sprites_to_draw[ + current_frame].begin(), + BluCat::INT::core.vk_renderer->sprites_to_draw[ + current_frame].end()); // Draw sprites - for(auto& sprite_to_draw: view->sprites_to_draw[current_frame]) + for(auto& sprite_to_draw: + BluCat::INT::core.vk_renderer->sprites_to_draw[current_frame]) { std::array vk_descriptor_sets{ - view->descriptor_sets_2d[image_index], + BluCat::INT::core.vk_renderer->descriptor_sets_2d[image_index], sprite_to_draw.sprite->texture->descriptor_sets[image_index]}; VkDeviceSize offsets[]{0}; @@ -292,7 +294,7 @@ GraphicsPipeline2DSolid::draw( } // Prepare for the next frame. - view->sprites_to_draw[next_frame].clear(); + BluCat::INT::core.vk_renderer->sprites_to_draw[next_frame].clear(); } } diff --git a/src/blu_cat/gra/graphics_pipeline_2d_solid.hpp b/src/blu_cat/gra/graphics_pipeline_2d_solid.hpp index c1520c2..c2d85d5 100644 --- a/src/blu_cat/gra/graphics_pipeline_2d_solid.hpp +++ b/src/blu_cat/gra/graphics_pipeline_2d_solid.hpp @@ -1,5 +1,5 @@ /* - * Copyright 2022-2024 Frederico de Oliveira Linhares + * Copyright 2022-2025 Frederico de Oliveira Linhares * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,7 +21,6 @@ #include "vulkan.hpp" #include "command_pool.hpp" -#include "view_2d.hpp" namespace BluCat::GRA { @@ -34,9 +33,8 @@ struct GraphicsPipeline2DSolid ~GraphicsPipeline2DSolid(); void - draw(std::shared_ptr view, const VkCommandBuffer draw_command_buffer, - const size_t current_frame, const size_t next_frame, - const uint32_t image_index); + draw(const VkCommandBuffer draw_command_buffer, const size_t current_frame, + const size_t next_frame, const uint32_t image_index); }; } diff --git a/src/blu_cat/gra/graphics_pipeline_2d_wired.cpp b/src/blu_cat/gra/graphics_pipeline_2d_wired.cpp index 20491ed..1273c67 100644 --- a/src/blu_cat/gra/graphics_pipeline_2d_wired.cpp +++ b/src/blu_cat/gra/graphics_pipeline_2d_wired.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2022-2024 Frederico de Oliveira Linhares + * Copyright 2022-2025 Frederico de Oliveira Linhares * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -247,33 +247,32 @@ GraphicsPipeline2DWired::~GraphicsPipeline2DWired() void GraphicsPipeline2DWired::draw( - std::shared_ptr view, const VkCommandBuffer draw_command_buffer, - const size_t current_frame, const size_t next_frame, - const uint32_t image_index) + const VkCommandBuffer draw_command_buffer, const size_t current_frame, + const size_t next_frame, const uint32_t image_index) { - // Set viewport - { + // TODO set viewport just once per view, not once per pipeline. + { // Set viewport VkViewport vk_viewport{}; - vk_viewport.x = view->region.x; - vk_viewport.y = view->region.y; - vk_viewport.width = view->region.z; - vk_viewport.height = view->region.w; + vk_viewport.x = 0; + vk_viewport.y = 0; + vk_viewport.width = static_cast(BluCat::INT::core.display_width); + vk_viewport.height = static_cast(BluCat::INT::core.display_height); vk_viewport.minDepth = 0.0f; vk_viewport.maxDepth = 1.0f; vkCmdSetViewport(draw_command_buffer, 0, 1, &vk_viewport); VkRect2D vk_scissor{}; - vk_scissor.offset.x = static_cast(view->region.x); - vk_scissor.offset.y = static_cast(view->region.y); - vk_scissor.extent.width = static_cast(view->region.z); - vk_scissor.extent.height = static_cast(view->region.w); + vk_scissor.offset.x = 0; + vk_scissor.offset.y = 0; + vk_scissor.extent.width = BluCat::INT::core.display_width; + vk_scissor.extent.height = BluCat::INT::core.display_height; vkCmdSetScissor(draw_command_buffer, 0, 1, &vk_scissor); } // Draw rectangles { std::array vk_descriptor_sets{ - view->descriptor_sets_2d[image_index]}; + BluCat::INT::core.vk_renderer->descriptor_sets_2d[image_index]}; VkDeviceSize offsets[]{0}; vkCmdBindDescriptorSets( @@ -287,9 +286,11 @@ GraphicsPipeline2DWired::draw( draw_command_buffer, this->index_buffer->buffer, 0, VK_INDEX_TYPE_UINT32); - for(auto i{0}; i < view->rectangles_to_draw[current_frame].size(); i++) + for(auto i{0}; i < BluCat::INT::core.vk_renderer->rectangles_to_draw[ + current_frame].size(); i++) { - auto &rect{view->rectangles_to_draw[current_frame][i]}; + auto &rect{BluCat::INT::core.vk_renderer->rectangles_to_draw[ + current_frame][i]}; UDOVector4D position{rect.position}; UDOVector3D color{rect.color}; @@ -308,7 +309,7 @@ GraphicsPipeline2DWired::draw( } // Prepare for the next frame. - view->rectangles_to_draw[next_frame].clear(); + BluCat::INT::core.vk_renderer->rectangles_to_draw[next_frame].clear(); } } diff --git a/src/blu_cat/gra/graphics_pipeline_2d_wired.hpp b/src/blu_cat/gra/graphics_pipeline_2d_wired.hpp index 7d2752e..9fc46c4 100644 --- a/src/blu_cat/gra/graphics_pipeline_2d_wired.hpp +++ b/src/blu_cat/gra/graphics_pipeline_2d_wired.hpp @@ -1,5 +1,5 @@ /* - * Copyright 2022-2024 Frederico de Oliveira Linhares + * Copyright 2022-2025 Frederico de Oliveira Linhares * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,7 +20,8 @@ #include #include "vulkan.hpp" -#include "view_2d.hpp" +#include "command_pool.hpp" +#include "destination_buffer.hpp" namespace BluCat::GRA { @@ -37,9 +38,8 @@ struct GraphicsPipeline2DWired ~GraphicsPipeline2DWired(); void - draw(std::shared_ptr view, const VkCommandBuffer draw_command_buffer, - const size_t current_frame, const size_t next_frame, - const uint32_t image_index); + draw(const VkCommandBuffer draw_command_buffer, const size_t current_frame, + const size_t next_frame, const uint32_t image_index); }; } diff --git a/src/blu_cat/gra/graphics_pipeline_3d.cpp b/src/blu_cat/gra/graphics_pipeline_3d.cpp index 1f7324a..288e186 100644 --- a/src/blu_cat/gra/graphics_pipeline_3d.cpp +++ b/src/blu_cat/gra/graphics_pipeline_3d.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2022-2024 Frederico de Oliveira Linhares + * Copyright 2022-2025 Frederico de Oliveira Linhares * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -257,7 +257,7 @@ GraphicsPipeline3D::~GraphicsPipeline3D() void GraphicsPipeline3D::draw( - std::shared_ptr view, const VkCommandBuffer draw_command_buffer, + std::shared_ptr view, const VkCommandBuffer draw_command_buffer, const size_t current_frame, const uint32_t image_index) { vkCmdBindPipeline( @@ -286,7 +286,7 @@ GraphicsPipeline3D::draw( std::array vk_descriptor_sets{ INT::core.vk_light->descriptor_sets_world[image_index], - view->descriptor_sets_3d[image_index], + view->descriptor_sets[image_index], instance->descriptor_sets[image_index], instance->texture->descriptor_sets[image_index]}; diff --git a/src/blu_cat/gra/graphics_pipeline_3d.hpp b/src/blu_cat/gra/graphics_pipeline_3d.hpp index 0dc0b03..2e30e02 100644 --- a/src/blu_cat/gra/graphics_pipeline_3d.hpp +++ b/src/blu_cat/gra/graphics_pipeline_3d.hpp @@ -1,5 +1,5 @@ /* - * Copyright 2022-2024 Frederico de Oliveira Linhares + * Copyright 2022-2025 Frederico de Oliveira Linhares * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,7 +21,7 @@ #include "vulkan.hpp" #include "command_pool.hpp" -#include "view_3d.hpp" +#include "view.hpp" namespace BluCat::GRA { @@ -34,7 +34,7 @@ struct GraphicsPipeline3D ~GraphicsPipeline3D(); void - draw(std::shared_ptr view, const VkCommandBuffer draw_command_buffer, + draw(std::shared_ptr view, const VkCommandBuffer draw_command_buffer, const size_t current_frame, const uint32_t image_index); }; diff --git a/src/blu_cat/gra/graphics_pipeline_3d_skeletal.cpp b/src/blu_cat/gra/graphics_pipeline_3d_skeletal.cpp index e8eda6b..79a85f1 100644 --- a/src/blu_cat/gra/graphics_pipeline_3d_skeletal.cpp +++ b/src/blu_cat/gra/graphics_pipeline_3d_skeletal.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2022-2024 Frederico de Oliveira Linhares + * Copyright 2022-2025 Frederico de Oliveira Linhares * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -266,7 +266,7 @@ GraphicsPipeline3DSkeletal::~GraphicsPipeline3DSkeletal() void GraphicsPipeline3DSkeletal::draw( - std::shared_ptr view, const VkCommandBuffer draw_command_buffer, + std::shared_ptr view, const VkCommandBuffer draw_command_buffer, const size_t current_frame, const uint32_t image_index) { vkCmdBindPipeline( @@ -295,7 +295,7 @@ GraphicsPipeline3DSkeletal::draw( std::array vk_descriptor_sets{ INT::core.vk_light->descriptor_sets_world[image_index], - view->descriptor_sets_3d[image_index], + view->descriptor_sets[image_index], instance->descriptor_sets[image_index], instance->texture->descriptor_sets[image_index]}; diff --git a/src/blu_cat/gra/graphics_pipeline_3d_skeletal.hpp b/src/blu_cat/gra/graphics_pipeline_3d_skeletal.hpp index bad117d..16d65b0 100644 --- a/src/blu_cat/gra/graphics_pipeline_3d_skeletal.hpp +++ b/src/blu_cat/gra/graphics_pipeline_3d_skeletal.hpp @@ -1,5 +1,5 @@ /* - * Copyright 2022-2024 Frederico de Oliveira Linhares + * Copyright 2022-2025 Frederico de Oliveira Linhares * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,7 +21,7 @@ #include "vulkan.hpp" #include "command_pool.hpp" -#include "view_3d.hpp" +#include "view.hpp" namespace BluCat::GRA { @@ -34,7 +34,7 @@ struct GraphicsPipeline3DSkeletal ~GraphicsPipeline3DSkeletal(); void - draw(std::shared_ptr view, const VkCommandBuffer draw_command_buffer, + draw(std::shared_ptr view, const VkCommandBuffer draw_command_buffer, const size_t current_frame, const uint32_t image_index); }; diff --git a/src/blu_cat/gra/graphics_pipeline_sprite_3d.cpp b/src/blu_cat/gra/graphics_pipeline_sprite_3d.cpp index 14796d3..63e10b9 100644 --- a/src/blu_cat/gra/graphics_pipeline_sprite_3d.cpp +++ b/src/blu_cat/gra/graphics_pipeline_sprite_3d.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2022-2024 Frederico de Oliveira Linhares + * Copyright 2022-2025 Frederico de Oliveira Linhares * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -265,7 +265,7 @@ GraphicsPipelineSprite3D::~GraphicsPipelineSprite3D() void GraphicsPipelineSprite3D::draw( - std::shared_ptr view, const VkCommandBuffer draw_command_buffer, + std::shared_ptr view, const VkCommandBuffer draw_command_buffer, const size_t current_frame, const uint32_t image_index) { vkCmdBindPipeline( @@ -290,7 +290,7 @@ GraphicsPipelineSprite3D::draw( { std::array vk_descriptor_sets{ INT::core.vk_light->descriptor_sets_world[image_index], - view->descriptor_sets_3d[image_index], + view->descriptor_sets[image_index], sprite.sprite_3d->descriptor_sets[image_index], sprite.sprite_3d->sprite->texture->descriptor_sets[image_index]}; VkDeviceSize offsets[]{0}; diff --git a/src/blu_cat/gra/graphics_pipeline_sprite_3d.hpp b/src/blu_cat/gra/graphics_pipeline_sprite_3d.hpp index 325d943..d03b8e8 100644 --- a/src/blu_cat/gra/graphics_pipeline_sprite_3d.hpp +++ b/src/blu_cat/gra/graphics_pipeline_sprite_3d.hpp @@ -1,5 +1,5 @@ /* - * Copyright 2022-2024 Frederico de Oliveira Linhares + * Copyright 2022-2025 Frederico de Oliveira Linhares * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,7 +22,7 @@ #include "vulkan.hpp" #include "command_pool.hpp" #include "sprite_3d.hpp" -#include "view_3d.hpp" +#include "view.hpp" namespace BluCat::GRA { @@ -35,7 +35,7 @@ struct GraphicsPipelineSprite3D ~GraphicsPipelineSprite3D(); void - draw(std::shared_ptr view, const VkCommandBuffer draw_command_buffer, + draw(std::shared_ptr view, const VkCommandBuffer draw_command_buffer, const size_t current_frame, const uint32_t image_index); }; diff --git a/src/blu_cat/gra/renderer.cpp b/src/blu_cat/gra/renderer.cpp index d13f814..36b9925 100644 --- a/src/blu_cat/gra/renderer.cpp +++ b/src/blu_cat/gra/renderer.cpp @@ -24,16 +24,40 @@ namespace { +void +load_2d_uniform_buffer(void *obj) +{ + auto self = static_cast(obj); + + try + { + self->ub_2d.reserve(BluCat::INT::core.vk_swapchain->images_count); + for(auto i{0}; i < BluCat::INT::core.vk_swapchain->images_count; i++) + self->ub_2d.emplace_back( + BluCat::INT::core.vk_device_with_swapchain, + sizeof(BluCat::GRA::UDOView2D)); + } + catch(const std::exception& e) + { + throw CommandError{e.what()}; + } +} + +void +unload_2d_uniform_buffer(void *obj) +{ + auto self = static_cast(obj); + + self->ub_2d.clear(); +} + void load_descriptor_pool(void *obj) { auto self = static_cast(obj); - uint32_t uniform_buffer_count = 0; - for(auto &view : self->views_3d) - uniform_buffer_count += (view->ub_3d.size() + view->ub_2d.size()); - for(auto &view : self->views_2d) - uniform_buffer_count += (view->ub_2d.size()); + uint32_t uniform_buffer_count = self->ub_2d.size(); + for(auto &view : self->views) uniform_buffer_count += view->ub_3d.size(); VkDescriptorPoolSize descriptor_pool_size{}; descriptor_pool_size.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; @@ -52,9 +76,7 @@ load_descriptor_pool(void *obj) &self->descriptor_pool) != VK_SUCCESS) throw CommandError{"Failed to create a Vulkan descriptor pool."}; - for(auto &view : self->views_3d) - view->load_descriptor_sets(self->descriptor_pool); - for(auto &view : self->views_2d) + for(auto &view : self->views) view->load_descriptor_sets(self->descriptor_pool); } @@ -63,14 +85,71 @@ unload_descriptor_pool(void *obj) { auto self = static_cast(obj); - for(auto &view : self->views_3d) view->unload_descriptor_sets(); - for(auto &view : self->views_2d) view->unload_descriptor_sets(); + for(auto &view : self->views) view->unload_descriptor_sets(); vkDestroyDescriptorPool( BluCat::INT::core.vk_device_with_swapchain->device, self->descriptor_pool, nullptr); } +void +load_descriptor_sets_2d(void *obj) +{ + auto self = static_cast(obj); + + std::vector layouts( + BluCat::INT::core.vk_swapchain->images_count, + BluCat::INT::core.vk_descriptor_set_layout->view); + + VkDescriptorSetAllocateInfo alloc_info{}; + alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; + alloc_info.descriptorPool = self->descriptor_pool; + alloc_info.descriptorSetCount = layouts.size(); + alloc_info.pSetLayouts = layouts.data(); + + self->descriptor_sets_2d.resize(layouts.size()); + if(vkAllocateDescriptorSets( + BluCat::INT::core.vk_device_with_swapchain->device, &alloc_info, + self->descriptor_sets_2d.data()) != VK_SUCCESS) + throw CommandError{"Failed to create Vulkan descriptor sets for view."}; +} + +void +load_resources_to_descriptor_sets_2d(void *obj) +{ + auto self = static_cast(obj); + + for(auto i{0}; i < self->ub_2d.size(); i++) + { + VkDescriptorBufferInfo view_2d_info{}; + view_2d_info.buffer = self->ub_2d[i].buffer; + view_2d_info.offset = 0; + view_2d_info.range = sizeof(BluCat::GRA::UDOView2D); + + std::array write_descriptors{}; + write_descriptors[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; + write_descriptors[0].dstSet = self->descriptor_sets_2d[i]; + write_descriptors[0].dstBinding = 0; + write_descriptors[0].dstArrayElement = 0; + write_descriptors[0].descriptorCount = 1; + write_descriptors[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; + write_descriptors[0].pBufferInfo = &view_2d_info; + write_descriptors[0].pImageInfo = nullptr; + write_descriptors[0].pTexelBufferView = nullptr; + + vkUpdateDescriptorSets( + BluCat::INT::core.vk_device_with_swapchain->device, write_descriptors.size(), + write_descriptors.data(), 0, nullptr); + + BluCat::GRA::UDOView2D ubo_view_2d; + ubo_view_2d.proj = glm::ortho( + 0.0f, self->projection_width, + 0.0f, self->projection_height, + 0.0f, 100.0f); + self->ub_2d[i].copy_data(&ubo_view_2d); + } +} + void load_queue_family(void *obj) { @@ -128,7 +207,10 @@ load_draw_command_buffer(void *obj) } const CommandChain loader{ + {&load_2d_uniform_buffer, &unload_2d_uniform_buffer}, {&load_descriptor_pool, &unload_descriptor_pool}, + {&load_descriptor_sets_2d, nullptr}, + {&load_resources_to_descriptor_sets_2d, nullptr}, {&load_queue_family, nullptr}, {&load_command_pool, &unload_command_pool}, {&load_draw_command_buffer, nullptr} @@ -139,21 +221,25 @@ const CommandChain loader{ namespace BluCat::GRA { -Renderer::Renderer(std::vector> views_2d, - std::vector> views_3d): +Renderer::Renderer( + std::vector> views, F32 width, F32 height): skeletal_models_to_draw{BluCat::INT::core.vk_swapchain->images_count}, static_models_to_draw{BluCat::INT::core.vk_swapchain->images_count}, sprites_3d_to_draw{BluCat::INT::core.vk_swapchain->images_count}, + rectangles_to_draw{BluCat::INT::core.vk_swapchain->images_count}, + sprites_to_draw{BluCat::INT::core.vk_swapchain->images_count}, + projection_width{width}, + projection_height{height}, clear_screen_color{0.0f, 0.0f, 0.0f, 1.0f}, - views_2d{views_2d}, - views_3d{views_3d} + views{views} { loader.execute(this); } -Renderer::Renderer(std::initializer_list> views_2d, - std::initializer_list> views_3d): - Renderer(std::vector(views_2d), std::vector(views_3d)) +Renderer::Renderer( + std::initializer_list> views, + F32 width, F32 height): + Renderer(std::vector(views), width, height) { } @@ -251,7 +337,7 @@ Renderer::draw() draw_command_buffer, &render_pass_begin, VK_SUBPASS_CONTENTS_INLINE); } - for(auto &view: this->views_3d) + for(auto &view: this->views) { { // Set viewport VkViewport vk_viewport{}; @@ -272,16 +358,17 @@ Renderer::draw() } BluCat::INT::core.vk_graphics_pipeline_3d->draw( - view, draw_command_buffer, BluCat::INT::core.vk_swapchain->current_frame, + view, draw_command_buffer, + BluCat::INT::core.vk_swapchain->current_frame, image_index); BluCat::INT::core.vk_graphics_pipeline_sprite_3d->draw( - view, draw_command_buffer, BluCat::INT::core.vk_swapchain->current_frame, - image_index); + view, draw_command_buffer, + BluCat::INT::core.vk_swapchain->current_frame, image_index); BluCat::INT::core.vk_graphics_pipeline_3d_skeletal->draw( - view, draw_command_buffer, BluCat::INT::core.vk_swapchain->current_frame, - image_index); + view, draw_command_buffer, + BluCat::INT::core.vk_swapchain->current_frame, image_index); { // Update view uniform buffers BluCat::GRA::UDOView3D ubo_view_3d{}; @@ -325,27 +412,15 @@ Renderer::draw() } { // 2D solid drawing - for(auto &view: this->views_2d) - BluCat::INT::core.vk_graphics_pipeline_2d_solid->draw( - view, draw_command_buffer, BluCat::INT::core.vk_swapchain->current_frame, - next_frame, image_index); - - for(auto &view: this->views_3d) - BluCat::INT::core.vk_graphics_pipeline_2d_solid->draw( - view, draw_command_buffer, BluCat::INT::core.vk_swapchain->current_frame, - next_frame, image_index); + BluCat::INT::core.vk_graphics_pipeline_2d_solid->draw( + draw_command_buffer, BluCat::INT::core.vk_swapchain->current_frame, + next_frame, image_index); } { // 2D wired drawing - for(auto &view: this->views_2d) - BluCat::INT::core.vk_graphics_pipeline_2d_wired->draw( - view, draw_command_buffer, BluCat::INT::core.vk_swapchain->current_frame, - next_frame, image_index); - - for(auto &view: this->views_3d) - BluCat::INT::core.vk_graphics_pipeline_2d_wired->draw( - view, draw_command_buffer, BluCat::INT::core.vk_swapchain->current_frame, - next_frame, image_index); + BluCat::INT::core.vk_graphics_pipeline_2d_wired->draw( + draw_command_buffer, BluCat::INT::core.vk_swapchain->current_frame, + next_frame, image_index); } vkCmdEndRenderPass(draw_command_buffer); @@ -415,10 +490,8 @@ Renderer::draw() BluCat::INT::core.vk_swapchain->current_frame].clear(); this->sprites_3d_to_draw[ BluCat::INT::core.vk_swapchain->current_frame].clear(); - for(auto &view: this->views_2d) - view->sprites_to_draw[BluCat::INT::core.vk_swapchain->current_frame].clear(); - for(auto &view: this->views_3d) - view->sprites_to_draw[BluCat::INT::core.vk_swapchain->current_frame].clear(); + this->sprites_to_draw[ + BluCat::INT::core.vk_swapchain->current_frame].clear(); } } diff --git a/src/blu_cat/gra/renderer.hpp b/src/blu_cat/gra/renderer.hpp index 82948cb..58b9972 100644 --- a/src/blu_cat/gra/renderer.hpp +++ b/src/blu_cat/gra/renderer.hpp @@ -22,14 +22,15 @@ #include #include "vulkan.hpp" +#include "queue_family.hpp" +#include "rectangle.hpp" #include "skeletal_mesh.hpp" #include "skeletal_model.hpp" #include "sprite_3d.hpp" +#include "sprite_to_draw.hpp" #include "static_mesh.hpp" #include "static_model.hpp" -#include "queue_family.hpp" -#include "view_2d.hpp" -#include "view_3d.hpp" +#include "view.hpp" namespace BluCat::GRA { @@ -52,16 +53,25 @@ struct Renderer VkDescriptorPool descriptor_pool; VkClearColorValue clear_screen_color; - std::vector> views_2d; - std::vector> views_3d; + + float projection_width, projection_height; + + // FIXME: if these vectors get resized, they can cause a segmentation fault! + std::vector ub_2d; + std::vector descriptor_sets_2d; + + std::vector> rectangles_to_draw; + std::vector> sprites_to_draw; + + std::vector> views; + QueueFamily *queue_family; VkCommandPool command_pool; std::vector draw_command_buffers; - Renderer(std::vector> views_2d, - std::vector> views_3d); - Renderer(std::initializer_list> views_2d, - std::initializer_list> views_3d); + Renderer(std::vector> views, F32 width, F32 height); + Renderer(std::initializer_list> views, + F32 width, F32 height); ~Renderer(); void diff --git a/src/blu_cat/gra/texture.cpp b/src/blu_cat/gra/texture.cpp index 5abf751..7546d56 100644 --- a/src/blu_cat/gra/texture.cpp +++ b/src/blu_cat/gra/texture.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2022-2024 Frederico de Oliveira Linhares + * Copyright 2022-2025 Frederico de Oliveira Linhares * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -173,8 +173,8 @@ load_sampler(void *obj) sampler_info.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO; sampler_info.pNext = nullptr; sampler_info.flags = 0; - sampler_info.magFilter = VK_FILTER_LINEAR; - sampler_info.minFilter = VK_FILTER_LINEAR; + sampler_info.magFilter = VK_FILTER_NEAREST; + sampler_info.minFilter = VK_FILTER_NEAREST; sampler_info.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR; sampler_info.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT; sampler_info.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT; diff --git a/src/blu_cat/gra/view.cpp b/src/blu_cat/gra/view.cpp new file mode 100644 index 0000000..26017ce --- /dev/null +++ b/src/blu_cat/gra/view.cpp @@ -0,0 +1,158 @@ +/* + * Copyright 2022-2025 Frederico de Oliveira Linhares + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "view.hpp" + +#include + +#include "../int/core.hpp" +#include "uniform_data_object.hpp" + +#include + +namespace +{ + +void +load_uniform_buffer(void *obj) +{ + auto self = static_cast(obj); + + try + { + self->ub_3d.reserve(BluCat::INT::core.vk_swapchain->images_count); + for(auto i{0}; i < BluCat::INT::core.vk_swapchain->images_count; i++) + self->ub_3d.emplace_back( + BluCat::INT::core.vk_device_with_swapchain, + sizeof(BluCat::GRA::UDOView3D)); + } + catch(const std::exception& e) + { + throw CommandError{e.what()}; + } +} + +void +unload_uniform_buffer(void *obj) +{ + auto self = static_cast(obj); + + self->ub_3d.clear(); +} + +void +load_descriptor_sets(void *obj) +{ + auto self = static_cast(obj); + + std::vector layouts( + BluCat::INT::core.vk_swapchain->images_count, + BluCat::INT::core.vk_descriptor_set_layout->view); + + VkDescriptorSetAllocateInfo alloc_info{}; + alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; + alloc_info.descriptorPool = self->descriptor_pool; + alloc_info.descriptorSetCount = layouts.size(); + alloc_info.pSetLayouts = layouts.data(); + + self->descriptor_sets.resize(layouts.size()); + if(vkAllocateDescriptorSets( + BluCat::INT::core.vk_device_with_swapchain->device, &alloc_info, + self->descriptor_sets.data()) != VK_SUCCESS) + throw CommandError{"Failed to create Vulkan descriptor sets for view."}; +} + +void +load_resources_to_descriptor_sets(void *obj) +{ + auto self = static_cast(obj); + + for(auto i{0}; i < self->ub_3d.size(); i++) + { + VkDescriptorBufferInfo view_3d_info{}; + view_3d_info.buffer = self->ub_3d[i].buffer; + view_3d_info.offset = 0; + view_3d_info.range = sizeof(BluCat::GRA::UDOView3D); + + std::array write_descriptors{}; + write_descriptors[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; + write_descriptors[0].dstSet = self->descriptor_sets[i]; + write_descriptors[0].dstBinding = 0; + write_descriptors[0].dstArrayElement = 0; + write_descriptors[0].descriptorCount = 1; + write_descriptors[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; + write_descriptors[0].pBufferInfo = &view_3d_info; + write_descriptors[0].pImageInfo = nullptr; + write_descriptors[0].pTexelBufferView = nullptr; + + vkUpdateDescriptorSets( + BluCat::INT::core.vk_device_with_swapchain->device, + write_descriptors.size(), write_descriptors.data(), 0, nullptr); + } +} + +const CommandChain loader{ + {&load_uniform_buffer, &unload_uniform_buffer} +}; + +const CommandChain descriptor_sets_loader{ + {&load_descriptor_sets, nullptr}, + {&load_resources_to_descriptor_sets, nullptr} +}; + +} + +namespace BluCat::GRA +{ + +View::View( + glm::vec4 region, float projection_width, + float projection_height): + region{region}, + projection_width{projection_width}, + projection_height{projection_height}, + field_of_view{45.0f}, + descriptor_pool{VK_NULL_HANDLE}, + camera_position{std::make_shared(0.0f, 0.0f, 0.0f)}, + camera_orientation{std::make_shared(0.0f, 0.0f, 0.0f, 0.0f)} +{ + loader.execute(this); +} + +View::~View() +{ + loader.revert(this); +} + +void +View::load_descriptor_sets(VkDescriptorPool descriptor_pool) +{ + if(this->descriptor_pool != VK_NULL_HANDLE) return; + + this->descriptor_pool = descriptor_pool; + ::descriptor_sets_loader.execute(this); +} + +void +View::unload_descriptor_sets() +{ + if(this->descriptor_pool == VK_NULL_HANDLE) return; + + this->descriptor_pool = VK_NULL_HANDLE; + ::descriptor_sets_loader.revert(this); +} + +} diff --git a/src/blu_cat/gra/view.hpp b/src/blu_cat/gra/view.hpp new file mode 100644 index 0000000..a61ac34 --- /dev/null +++ b/src/blu_cat/gra/view.hpp @@ -0,0 +1,55 @@ +/* + * Copyright 2022-2025 Frederico de Oliveira Linhares + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef BLU_CAT_GRA_VIEW_H +#define BLU_CAT_GRA_VIEW_H 1 + +#include +#include +#include + +#include "vulkan.hpp" +#include "uniform_buffer.hpp" + +namespace BluCat::GRA +{ + +struct View +{ + glm::vec4 region; + float field_of_view, projection_width, projection_height; + + // FIXME: if this vector get resized, it can cause a segmentation fault! + std::vector ub_3d; + + VkDescriptorPool descriptor_pool; + std::vector descriptor_sets; + + std::shared_ptr camera_position; + std::shared_ptr camera_orientation; + + void + load_descriptor_sets(VkDescriptorPool descriptor_pool); + void + unload_descriptor_sets(); + + View(glm::vec4 region, float projection_width, float projection_height); + ~View(); +}; + +} + +#endif /* BLU_CAT_GRA_VIEW_H */ diff --git a/src/blu_cat/gra/view_2d.cpp b/src/blu_cat/gra/view_2d.cpp deleted file mode 100644 index 948b145..0000000 --- a/src/blu_cat/gra/view_2d.cpp +++ /dev/null @@ -1,160 +0,0 @@ -/* - * Copyright 2022-2024 Frederico de Oliveira Linhares - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "view_2d.hpp" - -#include - -#include "../int/core.hpp" -#include "uniform_data_object.hpp" - -namespace -{ - -void -load_2d_uniform_buffer(void *obj) -{ - auto self = static_cast(obj); - - try - { - self->ub_2d.reserve(BluCat::INT::core.vk_swapchain->images_count); - for(auto i{0}; i < BluCat::INT::core.vk_swapchain->images_count; i++) - self->ub_2d.emplace_back( - BluCat::INT::core.vk_device_with_swapchain, sizeof(BluCat::GRA::UDOView2D)); - } - catch(const std::exception& e) - { - throw CommandError{e.what()}; - } -} - -void -unload_2d_uniform_buffer(void *obj) -{ - auto self = static_cast(obj); - - self->ub_2d.clear(); -} - -void -load_descriptor_sets_2d(void *obj) -{ - auto self = static_cast(obj); - - std::vector layouts( - BluCat::INT::core.vk_swapchain->images_count, - BluCat::INT::core.vk_descriptor_set_layout->view); - - VkDescriptorSetAllocateInfo alloc_info{}; - alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; - alloc_info.descriptorPool = self->descriptor_pool; - alloc_info.descriptorSetCount = layouts.size(); - alloc_info.pSetLayouts = layouts.data(); - - self->descriptor_sets_2d.resize(layouts.size()); - if(vkAllocateDescriptorSets( - BluCat::INT::core.vk_device_with_swapchain->device, &alloc_info, - self->descriptor_sets_2d.data()) != VK_SUCCESS) - throw CommandError{"Failed to create Vulkan descriptor sets for view."}; -} - -void -load_resources_to_descriptor_sets_2d(void *obj) -{ - auto self = static_cast(obj); - - for(auto i{0}; i < self->ub_2d.size(); i++) - { - VkDescriptorBufferInfo view_2d_info{}; - view_2d_info.buffer = self->ub_2d[i].buffer; - view_2d_info.offset = 0; - view_2d_info.range = sizeof(BluCat::GRA::UDOView2D); - - std::array write_descriptors{}; - write_descriptors[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; - write_descriptors[0].dstSet = self->descriptor_sets_2d[i]; - write_descriptors[0].dstBinding = 0; - write_descriptors[0].dstArrayElement = 0; - write_descriptors[0].descriptorCount = 1; - write_descriptors[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; - write_descriptors[0].pBufferInfo = &view_2d_info; - write_descriptors[0].pImageInfo = nullptr; - write_descriptors[0].pTexelBufferView = nullptr; - - vkUpdateDescriptorSets( - BluCat::INT::core.vk_device_with_swapchain->device, write_descriptors.size(), - write_descriptors.data(), 0, nullptr); - - BluCat::GRA::UDOView2D ubo_view_2d; - ubo_view_2d.proj = glm::ortho( - 0.0f, self->projection_width, - 0.0f, self->projection_height, - 0.0f, 100.0f); - self->ub_2d[i].copy_data(&ubo_view_2d); - } -} - -} - -namespace BluCat::GRA -{ - -const CommandChain View2D::loader{ - {&load_2d_uniform_buffer, &unload_2d_uniform_buffer} -}; - -const CommandChain View2D::descriptor_sets_loader{ - {&load_descriptor_sets_2d, nullptr}, - {&load_resources_to_descriptor_sets_2d, nullptr} -}; - -View2D::View2D( - glm::vec4 region, float projection_width, float projection_height): - projection_width{projection_width}, - projection_height{projection_height}, - region{region}, - descriptor_pool{VK_NULL_HANDLE}, - rectangles_to_draw{BluCat::INT::core.vk_swapchain->images_count}, - sprites_to_draw{BluCat::INT::core.vk_swapchain->images_count} -{ - loader.execute(this); -} - -View2D::~View2D() -{ - loader.revert(this); -} - -void -View2D::load_descriptor_sets(VkDescriptorPool descriptor_pool) -{ - if(this->descriptor_pool != VK_NULL_HANDLE) return; - - this->descriptor_pool = descriptor_pool; - descriptor_sets_loader.execute(this); -} - -void -View2D::unload_descriptor_sets() -{ - if(this->descriptor_pool == VK_NULL_HANDLE) return; - - this->descriptor_pool = VK_NULL_HANDLE; - descriptor_sets_loader.revert(this); -} - -} diff --git a/src/blu_cat/gra/view_2d.hpp b/src/blu_cat/gra/view_2d.hpp deleted file mode 100644 index 84b9f09..0000000 --- a/src/blu_cat/gra/view_2d.hpp +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright 2022-2024 Frederico de Oliveira Linhares - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef BLU_CAT_GRA_VIEW_2D_H -#define BLU_CAT_GRA_VIEW_2D_H 1 - -#include -#include -#include - -#include "vulkan.hpp" -#include "sprite_to_draw.hpp" -#include "rectangle.hpp" - -namespace BluCat::GRA -{ - -struct View2D -{ - glm::vec4 region; - float projection_width, projection_height; - - // FIXME: if these vectors get resized, they can cause a segmentation fault! - std::vector ub_2d; - - VkDescriptorPool descriptor_pool; - std::vector descriptor_sets_2d; - - std::vector> rectangles_to_draw; - std::vector> sprites_to_draw; - - View2D(glm::vec4 region, float projection_width, float projection_height); - virtual ~View2D(); - - void - virtual load_descriptor_sets(VkDescriptorPool descriptor_pool); - - void - virtual unload_descriptor_sets(); - -protected: - static const CommandChain loader, descriptor_sets_loader; -}; - -} - -#endif /* BLU_CAT_GRA_VIEW_2D_H */ diff --git a/src/blu_cat/gra/view_3d.cpp b/src/blu_cat/gra/view_3d.cpp deleted file mode 100644 index dc310e3..0000000 --- a/src/blu_cat/gra/view_3d.cpp +++ /dev/null @@ -1,155 +0,0 @@ -/* - * Copyright 2022-2024 Frederico de Oliveira Linhares - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "view_3d.hpp" - -#include - -#include "../int/core.hpp" -#include "uniform_data_object.hpp" - -namespace -{ - -void -load_3d_uniform_buffer(void *obj) -{ - auto self = static_cast(obj); - - try - { - self->ub_3d.reserve(BluCat::INT::core.vk_swapchain->images_count); - for(auto i{0}; i < BluCat::INT::core.vk_swapchain->images_count; i++) - self->ub_3d.emplace_back( - BluCat::INT::core.vk_device_with_swapchain, sizeof(BluCat::GRA::UDOView3D)); - } - catch(const std::exception& e) - { - throw CommandError{e.what()}; - } -} - -void -unload_3d_uniform_buffer(void *obj) -{ - auto self = static_cast(obj); - - self->ub_3d.clear(); -} - -void -load_descriptor_sets_3d(void *obj) -{ - auto self = static_cast(obj); - - std::vector layouts( - BluCat::INT::core.vk_swapchain->images_count, - BluCat::INT::core.vk_descriptor_set_layout->view); - - VkDescriptorSetAllocateInfo alloc_info{}; - alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; - alloc_info.descriptorPool = self->descriptor_pool; - alloc_info.descriptorSetCount = layouts.size(); - alloc_info.pSetLayouts = layouts.data(); - - self->descriptor_sets_3d.resize(layouts.size()); - if(vkAllocateDescriptorSets( - BluCat::INT::core.vk_device_with_swapchain->device, &alloc_info, - self->descriptor_sets_3d.data()) != VK_SUCCESS) - throw CommandError{"Failed to create Vulkan descriptor sets for view."}; -} - -void -load_resources_to_descriptor_sets_3d(void *obj) -{ - auto self = static_cast(obj); - - for(auto i{0}; i < self->ub_3d.size(); i++) - { - VkDescriptorBufferInfo view_3d_info{}; - view_3d_info.buffer = self->ub_3d[i].buffer; - view_3d_info.offset = 0; - view_3d_info.range = sizeof(BluCat::GRA::UDOView3D); - - std::array write_descriptors{}; - write_descriptors[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; - write_descriptors[0].dstSet = self->descriptor_sets_3d[i]; - write_descriptors[0].dstBinding = 0; - write_descriptors[0].dstArrayElement = 0; - write_descriptors[0].descriptorCount = 1; - write_descriptors[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; - write_descriptors[0].pBufferInfo = &view_3d_info; - write_descriptors[0].pImageInfo = nullptr; - write_descriptors[0].pTexelBufferView = nullptr; - - vkUpdateDescriptorSets( - BluCat::INT::core.vk_device_with_swapchain->device, write_descriptors.size(), - write_descriptors.data(), 0, nullptr); - } -} - -const CommandChain loader{ - {&load_3d_uniform_buffer, &unload_3d_uniform_buffer} -}; - -const CommandChain descriptor_sets_loader{ - {&load_descriptor_sets_3d, nullptr}, - {&load_resources_to_descriptor_sets_3d, nullptr} -}; - -} - -namespace BluCat::GRA -{ - -View3D::View3D( - glm::vec4 region, float projection_width, float projection_height): - View2D{region, projection_width, projection_height}, - field_of_view{45.0f}, - camera_position{std::make_shared(0.0f, 0.0f, 0.0f)}, - camera_orientation{std::make_shared(0.0f, 0.0f, 0.0f, 0.0f)} -{ - ::loader.execute(this); -} - -View3D::~View3D() -{ - ::loader.revert(this); -} - -void -View3D::load_descriptor_sets(VkDescriptorPool descriptor_pool) -{ - if(this->descriptor_pool != VK_NULL_HANDLE) return; - - auto parent = dynamic_cast(this); - this->descriptor_pool = descriptor_pool; - View2D::descriptor_sets_loader.execute(parent); - ::descriptor_sets_loader.execute(this); -} - -void -View3D::unload_descriptor_sets() -{ - if(this->descriptor_pool == VK_NULL_HANDLE) return; - - auto parent = dynamic_cast(this); - this->descriptor_pool = VK_NULL_HANDLE; - ::descriptor_sets_loader.revert(this); - View2D::descriptor_sets_loader.revert(parent); -} - -} diff --git a/src/blu_cat/gra/view_3d.hpp b/src/blu_cat/gra/view_3d.hpp deleted file mode 100644 index f09c70f..0000000 --- a/src/blu_cat/gra/view_3d.hpp +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright 2022-2024 Frederico de Oliveira Linhares - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef BLU_CAT_GRA_VIEW_3D_H -#define BLU_CAT_GRA_VIEW_3D_H 1 - -#include "view_2d.hpp" - -namespace BluCat::GRA -{ - -struct View3D: public View2D -{ - float field_of_view; - // FIXME: if this vector get resized, it can cause a segmentation fault! - std::vector ub_3d; - - std::vector descriptor_sets_3d; - - std::shared_ptr camera_position; - std::shared_ptr camera_orientation; - - View3D(glm::vec4 region, float projection_width, float projection_height); - ~View3D(); - - void - load_descriptor_sets(VkDescriptorPool descriptor_pool); - - void - unload_descriptor_sets(); -}; - -} - -#endif /* BLU_CAT_GRA_VIEW_3D_H */ diff --git a/src/blu_cat/int/core.cpp b/src/blu_cat/int/core.cpp index d631d17..7e78b89 100644 --- a/src/blu_cat/int/core.cpp +++ b/src/blu_cat/int/core.cpp @@ -692,13 +692,13 @@ load_renderer(void *obj) { try { + auto width{static_cast(BluCat::INT::core.display_width)}; + auto height{static_cast(BluCat::INT::core.display_height)}; glm::vec4 region( - 0.f, 0.f, - static_cast(BluCat::INT::core.display_width), - static_cast(BluCat::INT::core.display_height)); + 0.f, 0.f, width, height); BluCat::INT::core.vk_renderer = new BluCat::GRA::Renderer( - {}, - {std::make_shared(region, region.z, region.w)}); + {std::make_shared(region, region.z, region.w)}, + width, height); } catch(const CommandError &e) { diff --git a/src/candy_gear/candy_gear.cpp b/src/candy_gear/candy_gear.cpp index b1758a4..7b5a9d9 100644 --- a/src/candy_gear/candy_gear.cpp +++ b/src/candy_gear/candy_gear.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2022-2024 Frederico de Oliveira Linhares + * Copyright 2022-2025 Frederico de Oliveira Linhares * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,8 +21,7 @@ #include #include "core.hpp" -#include "view_2d.hpp" -#include "view_3d.hpp" +#include "view.hpp" static mrb_value cg_mCandyGear_set_game_name(mrb_state *mrb, mrb_value self) @@ -36,40 +35,28 @@ cg_mCandyGear_set_game_name(mrb_state *mrb, mrb_value self) } static mrb_value -cg_mCandyGear_set_views(mrb_state *mrb, mrb_value self) +cg_mCandyGear_change_views(mrb_state *mrb, mrb_value self) { - struct RClass *cg_m, *cg_cView2D, *cg_cView3D; + struct RClass *cg_m, *cg_cView; mrb_value *array; mrb_int array_len; + mrb_float width, height; - std::vector> views_2d; - std::vector> views_3d; + std::vector> views; cg_m = mrb_module_get(mrb, "CandyGear"); - cg_cView2D = mrb_class_get_under(mrb, cg_m, "View2D"); - cg_cView3D = mrb_class_get_under(mrb, cg_m, "View3D"); + cg_cView = mrb_class_get_under(mrb, cg_m, "View"); - mrb_get_args(mrb, "a", &array, &array_len); + mrb_get_args(mrb, "aff", &array, &array_len, &width, &height); for(mrb_int i{0}; i < array_len; i++) { - if(mrb_obj_is_kind_of(mrb, array[i], cg_cView2D)) - { - auto v = (std::shared_ptr*)DATA_PTR(array[i]); - views_2d.push_back(*v); - } - else if(mrb_obj_is_kind_of(mrb, array[i], cg_cView3D)) - { - auto v = (std::shared_ptr*)DATA_PTR(array[i]); - views_3d.push_back(*v); - } + auto v = (std::shared_ptr*)DATA_PTR(array[i]); + views.push_back(*v); } - // A Renderer need at least one view to work. - if(views_2d.size() > 0 || views_3d.size() > 0) - { - delete BluCat::INT::core.vk_renderer; - BluCat::INT::core.vk_renderer = new BluCat::GRA::Renderer({views_2d, views_3d}); - } + delete BluCat::INT::core.vk_renderer; + BluCat::INT::core.vk_renderer = new BluCat::GRA::Renderer( + {views, static_cast(width), static_cast(height)}); return self; } @@ -138,7 +125,7 @@ cg_candy_gear_init(mrb_state *mrb) cg_m = mrb_module_get(mrb, "CandyGear"); mrb_define_class_method( - mrb, cg_m, "views=", cg_mCandyGear_set_views, MRB_ARGS_REQ(1)); + mrb, cg_m, "change_views", cg_mCandyGear_change_views, MRB_ARGS_REQ(3)); mrb_define_class_method( mrb, cg_m, "log", cg_mCandyGear_log, MRB_ARGS_REQ(2)); mrb_define_class_method( diff --git a/src/candy_gear/core.cpp b/src/candy_gear/core.cpp index 6897921..0f5330c 100644 --- a/src/candy_gear/core.cpp +++ b/src/candy_gear/core.cpp @@ -31,8 +31,7 @@ #include "texture.hpp" #include "vector_3d.hpp" #include "vector_4d.hpp" -#include "view_2d.hpp" -#include "view_3d.hpp" +#include "view.hpp" #ifdef DEBUG #include @@ -117,8 +116,7 @@ load_mruby_interface(void *obj) cg_texture_init(cg_core.mrb); cg_vector_3d_init(cg_core.mrb); cg_vector_4d_init(cg_core.mrb); - cg_view_2d_init(cg_core.mrb); - cg_view_3d_init(cg_core.mrb); + cg_view_init(cg_core.mrb); } } diff --git a/src/candy_gear/sprite.cpp b/src/candy_gear/sprite.cpp index daf96a8..2847d76 100644 --- a/src/candy_gear/sprite.cpp +++ b/src/candy_gear/sprite.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2022-2024 Frederico de Oliveira Linhares + * Copyright 2022-2025 Frederico de Oliveira Linhares * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,7 +18,6 @@ #include "texture.hpp" #include "vector_4d.hpp" -#include "view_2d.hpp" #include "../blu_cat/gra/sprite.hpp" void @@ -61,17 +60,13 @@ cg_cSprite_initialize(mrb_state *mrb, mrb_value self) static mrb_value cg_cSprite_draw(mrb_state *mrb, mrb_value self) { - mrb_value view_value; - BluCat::GRA::View2D *view_2d; mrb_float x, y, w, h, z_index{0.0}; auto ptr = (std::shared_ptr*)DATA_PTR(self); - mrb_get_args(mrb, "offff|f", &view_value, &x, &y, &w, &h, &z_index); - - view_2d = cg_cView_to_view_2d(mrb, view_value); + mrb_get_args(mrb, "ffff|f", &x, &y, &w, &h, &z_index); glm::vec4 rect(x, y, x + w, y + h); - auto &sprites_to_draw = view_2d->sprites_to_draw[ + auto &sprites_to_draw = BluCat::INT::core.vk_renderer->sprites_to_draw[ BluCat::INT::core.vk_swapchain->current_frame]; sprites_to_draw.emplace_back(*ptr, rect, z_index); diff --git a/src/candy_gear/vector_4d.cpp b/src/candy_gear/vector_4d.cpp index b44b612..5ee674d 100644 --- a/src/candy_gear/vector_4d.cpp +++ b/src/candy_gear/vector_4d.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2022-2024 Frederico de Oliveira Linhares + * Copyright 2022-2025 Frederico de Oliveira Linhares * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,7 +22,7 @@ #include #include "vector_3d.hpp" -#include "view_2d.hpp" +#include "view.hpp" namespace { @@ -347,19 +347,15 @@ cg_cVector4D_collide(mrb_state *mrb, mrb_value self) static mrb_value cg_cVector4D_draw_rectangle(mrb_state *mrb, mrb_value self) { - mrb_value view_value; - BluCat::GRA::View2D *view_2d; std::shared_ptr *color; auto ptr = (std::shared_ptr*)DATA_PTR(self); - mrb_get_args(mrb, "od", &view_value, &color, &cg_vector_3d_type); - - view_2d = cg_cView_to_view_2d(mrb, view_value); + mrb_get_args(mrb, "d", &color, &cg_vector_3d_type); glm::vec4 position( (*ptr)->x, (*ptr)->y, (*ptr)->x + (*ptr)->z, (*ptr)->y + (*ptr)->w); BluCat::GRA::Rectangle rect{position, (**color)}; - auto &rectangles = view_2d->rectangles_to_draw[ + auto &rectangles = BluCat::INT::core.vk_renderer->rectangles_to_draw[ BluCat::INT::core.vk_swapchain->current_frame]; rectangles.push_back(rect); diff --git a/src/candy_gear/view.cpp b/src/candy_gear/view.cpp new file mode 100644 index 0000000..40a03a8 --- /dev/null +++ b/src/candy_gear/view.cpp @@ -0,0 +1,127 @@ +/* + * Copyright 2022-2025 Frederico de Oliveira Linhares + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "view.hpp" + +#include "sprite.hpp" +#include "orientation_3d.hpp" +#include "vector_3d.hpp" +#include "vector_4d.hpp" +#include "../blu_cat/gra/sprite.hpp" +#include "../blu_cat/gra/view.hpp" + +void +cg_free_view(mrb_state *mrb, void* obj) +{ + auto ptr = static_cast*>(obj); + + ptr->~shared_ptr(); + mrb_free(mrb, ptr); +} + +const struct mrb_data_type cg_view_type = { "CG_View", cg_free_view }; + +static mrb_value +cg_cView_initialize(mrb_state *mrb, mrb_value self) +{ + std::shared_ptr *region; + mrb_float projection_width, projection_height; + std::shared_ptr *ptr; + + mrb_get_args(mrb, "dff", ®ion, &cg_vector_4d_type, + &projection_width, &projection_height); + ptr = (std::shared_ptr*)DATA_PTR(self); + if(ptr) mrb_free(mrb, ptr); + ptr = (std::shared_ptr*)mrb_malloc( + mrb, sizeof(std::shared_ptr)); + + new(ptr)std::shared_ptr( + std::make_shared( + *region->get(), projection_width, projection_height)); + + mrb_data_init(self, ptr, &cg_view_type); + return self; +} + +static mrb_value +cg_cView_set_camera_position(mrb_state *mrb, mrb_value self) +{ + std::shared_ptr *camera_position; + auto ptr = (std::shared_ptr*)DATA_PTR(self); + + mrb_get_args(mrb, "d", &camera_position, &cg_vector_3d_type); + (*ptr)->camera_position = (*camera_position); + + return self; +} + +static mrb_value +cg_cView_set_camera_orientation(mrb_state *mrb, mrb_value self) +{ + std::shared_ptr *camera_orientation; + auto ptr = (std::shared_ptr*)DATA_PTR(self); + + mrb_get_args(mrb, "d", &camera_orientation, &cg_orientation_3d_type); + (*ptr)->camera_orientation = (*camera_orientation); + + return self; +} + +static mrb_value +cg_cView_get_field_of_view(mrb_state *mrb, mrb_value self) +{ + auto ptr = (std::shared_ptr*)DATA_PTR(self); + + return mrb_float_value(mrb, (*ptr)->field_of_view); + + return self; +} + +static mrb_value +cg_cView_set_field_of_view(mrb_state *mrb, mrb_value self) +{ + mrb_float fov; + auto ptr = (std::shared_ptr*)DATA_PTR(self); + + mrb_get_args(mrb, "f", &fov); + (*ptr)->field_of_view = fov; + + return self; +} + +void +cg_view_init(mrb_state *mrb) +{ + struct RClass *cg_m, *cg_cView; + + cg_m = mrb_module_get(mrb, "CandyGear"); + cg_cView = mrb_define_class_under(mrb, cg_m, "View", mrb->object_class); + MRB_SET_INSTANCE_TT(cg_cView, MRB_TT_DATA); + mrb_define_method( + mrb, cg_cView, "initialize", cg_cView_initialize, MRB_ARGS_REQ(3)); + mrb_define_method( + mrb, cg_cView, "camera_position=", cg_cView_set_camera_position, + MRB_ARGS_REQ(1)); + mrb_define_method( + mrb, cg_cView, "camera_orientation=", cg_cView_set_camera_orientation, + MRB_ARGS_REQ(1)); + mrb_define_method( + mrb, cg_cView, "field_of_view", cg_cView_get_field_of_view, + MRB_ARGS_NONE()); + mrb_define_method( + mrb, cg_cView, "field_of_view=", cg_cView_set_field_of_view, + MRB_ARGS_REQ(1)); +} diff --git a/src/candy_gear/view.hpp b/src/candy_gear/view.hpp new file mode 100644 index 0000000..ce1b556 --- /dev/null +++ b/src/candy_gear/view.hpp @@ -0,0 +1,27 @@ +/* + * Copyright 2022-2025 Frederico de Oliveira Linhares + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef CANDY_GEAR_VIEW_H +#define CANDY_GEAR_VIEW_H 1 + +#include "core.hpp" + +extern const struct mrb_data_type cg_view_type; + +void +cg_view_init(mrb_state *mrb); + +#endif /* CANDY_GEAR_VIEW_H */ diff --git a/src/candy_gear/view_2d.cpp b/src/candy_gear/view_2d.cpp deleted file mode 100644 index e0dc8a1..0000000 --- a/src/candy_gear/view_2d.cpp +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright 2022-2024 Frederico de Oliveira Linhares - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "view_2d.hpp" - -#include "sprite.hpp" -#include "vector_4d.hpp" -#include "view_3d.hpp" -#include "../blu_cat/gra/sprite.hpp" -#include "../blu_cat/gra/view_2d.hpp" - -void -cg_free_view_2d(mrb_state *mrb, void* obj) -{ - auto ptr = static_cast*>(obj); - - ptr->~shared_ptr(); - mrb_free(mrb, ptr); -} - -const struct mrb_data_type cg_view_2d_type = { "CG_View2D", cg_free_view_2d }; - -static mrb_value -cg_cView2D_initialize(mrb_state *mrb, mrb_value self) -{ - std::shared_ptr *region; - mrb_float projection_width, projection_height; - std::shared_ptr *ptr; - - mrb_get_args(mrb, "dff", ®ion, &cg_vector_4d_type, - &projection_width, &projection_height); - ptr = (std::shared_ptr*)DATA_PTR(self); - if(ptr) mrb_free(mrb, ptr); - ptr = (std::shared_ptr*)mrb_malloc( - mrb, sizeof(std::shared_ptr)); - - new(ptr)std::shared_ptr( - std::make_shared( - *region->get(), projection_width, projection_height)); - - mrb_data_init(self, ptr, &cg_view_2d_type); - return self; -} - -BluCat::GRA::View2D* -cg_cView_to_view_2d(mrb_state *mrb, mrb_value view_value) -{ - BluCat::GRA::View2D* view_2d; - const mrb_data_type *type = DATA_TYPE(view_value); - - if(type == &cg_view_2d_type) - view_2d = static_cast*>( - DATA_PTR(view_value))->get(); - else if (type == &cg_view_3d_type) - view_2d = static_cast( - static_cast*>( - DATA_PTR(view_value))->get()); - else - mrb_raisef( - mrb, E_TYPE_ERROR, "wrong argument type %s (expected %s or %s)", - type->struct_name, cg_view_2d_type.struct_name, - cg_view_3d_type.struct_name); - - return view_2d; -} - -void -cg_view_2d_init(mrb_state *mrb) -{ - struct RClass *cg_m, *cg_cView2D; - - cg_m = mrb_module_get(mrb, "CandyGear"); - cg_cView2D = mrb_define_class_under(mrb, cg_m, "View2D", mrb->object_class); - MRB_SET_INSTANCE_TT(cg_cView2D, MRB_TT_DATA); - mrb_define_method( - mrb, cg_cView2D, "initialize", cg_cView2D_initialize, MRB_ARGS_REQ(3)); -} diff --git a/src/candy_gear/view_2d.hpp b/src/candy_gear/view_2d.hpp deleted file mode 100644 index 9b742a0..0000000 --- a/src/candy_gear/view_2d.hpp +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2022-2024 Frederico de Oliveira Linhares - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef CANDY_GEAR_VIEW_2D_H -#define CANDY_GEAR_VIEW_2D_H 1 - -#include "core.hpp" - -extern const struct mrb_data_type cg_view_2d_type; - -// Receives a mrb_value that points to a View2D or a View3D and returns a -// pointer to View2D. If the mrb_value points to a View3D, the view will be -// cast to a BluCat::GRA::View2D. -BluCat::GRA::View2D* -cg_cView_to_view_2d(mrb_state *mrb, mrb_value view_value); - -void -cg_view_2d_init(mrb_state *mrb); - -#endif /* CANDY_GEAR_VIEW_2D_H */ diff --git a/src/candy_gear/view_3d.cpp b/src/candy_gear/view_3d.cpp deleted file mode 100644 index b829cca..0000000 --- a/src/candy_gear/view_3d.cpp +++ /dev/null @@ -1,127 +0,0 @@ -/* - * Copyright 2022-2024 Frederico de Oliveira Linhares - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "view_3d.hpp" - -#include "sprite.hpp" -#include "orientation_3d.hpp" -#include "vector_3d.hpp" -#include "vector_4d.hpp" -#include "../blu_cat/gra/sprite.hpp" -#include "../blu_cat/gra/view_3d.hpp" - -void -cg_free_view_3d(mrb_state *mrb, void* obj) -{ - auto ptr = static_cast*>(obj); - - ptr->~shared_ptr(); - mrb_free(mrb, ptr); -} - -const struct mrb_data_type cg_view_3d_type = { "CG_View3D", cg_free_view_3d }; - -static mrb_value -cg_cView3D_initialize(mrb_state *mrb, mrb_value self) -{ - std::shared_ptr *region; - mrb_float projection_width, projection_height; - std::shared_ptr *ptr; - - mrb_get_args(mrb, "dff", ®ion, &cg_vector_4d_type, - &projection_width, &projection_height); - ptr = (std::shared_ptr*)DATA_PTR(self); - if(ptr) mrb_free(mrb, ptr); - ptr = (std::shared_ptr*)mrb_malloc( - mrb, sizeof(std::shared_ptr)); - - new(ptr)std::shared_ptr( - std::make_shared( - *region->get(), projection_width, projection_height)); - - mrb_data_init(self, ptr, &cg_view_3d_type); - return self; -} - -static mrb_value -cg_cView3D_set_camera_position(mrb_state *mrb, mrb_value self) -{ - std::shared_ptr *camera_position; - auto ptr = (std::shared_ptr*)DATA_PTR(self); - - mrb_get_args(mrb, "d", &camera_position, &cg_vector_3d_type); - (*ptr)->camera_position = (*camera_position); - - return self; -} - -static mrb_value -cg_cView3D_set_camera_orientation(mrb_state *mrb, mrb_value self) -{ - std::shared_ptr *camera_orientation; - auto ptr = (std::shared_ptr*)DATA_PTR(self); - - mrb_get_args(mrb, "d", &camera_orientation, &cg_orientation_3d_type); - (*ptr)->camera_orientation = (*camera_orientation); - - return self; -} - -static mrb_value -cg_cView3D_get_field_of_view(mrb_state *mrb, mrb_value self) -{ - auto ptr = (std::shared_ptr*)DATA_PTR(self); - - return mrb_float_value(mrb, (*ptr)->field_of_view); - - return self; -} - -static mrb_value -cg_cView3D_set_field_of_view(mrb_state *mrb, mrb_value self) -{ - mrb_float fov; - auto ptr = (std::shared_ptr*)DATA_PTR(self); - - mrb_get_args(mrb, "f", &fov); - (*ptr)->field_of_view = fov; - - return self; -} - -void -cg_view_3d_init(mrb_state *mrb) -{ - struct RClass *cg_m, *cg_cView3D; - - cg_m = mrb_module_get(mrb, "CandyGear"); - cg_cView3D = mrb_define_class_under(mrb, cg_m, "View3D", mrb->object_class); - MRB_SET_INSTANCE_TT(cg_cView3D, MRB_TT_DATA); - mrb_define_method( - mrb, cg_cView3D, "initialize", cg_cView3D_initialize, MRB_ARGS_REQ(3)); - mrb_define_method( - mrb, cg_cView3D, "camera_position=", cg_cView3D_set_camera_position, - MRB_ARGS_REQ(1)); - mrb_define_method( - mrb, cg_cView3D, "camera_orientation=", cg_cView3D_set_camera_orientation, - MRB_ARGS_REQ(1)); - mrb_define_method( - mrb, cg_cView3D, "field_of_view", cg_cView3D_get_field_of_view, - MRB_ARGS_NONE()); - mrb_define_method( - mrb, cg_cView3D, "field_of_view=", cg_cView3D_set_field_of_view, - MRB_ARGS_REQ(1)); -} diff --git a/src/candy_gear/view_3d.hpp b/src/candy_gear/view_3d.hpp deleted file mode 100644 index 22aa084..0000000 --- a/src/candy_gear/view_3d.hpp +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright 2022 Frederico de Oliveira Linhares - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef CANDY_GEAR_VIEW_3D_H -#define CANDY_GEAR_VIEW_3D_H 1 - -#include "core.hpp" - -extern const struct mrb_data_type cg_view_3d_type; - -void -cg_view_3d_init(mrb_state *mrb); - -#endif /* CANDY_GEAR_VIEW_3D_H */ -- cgit v1.2.3