From dfef9fdccf6f720d760e70efa57d922e41ad6e1c Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Tue, 13 May 2025 16:57:37 -0400 Subject: [PATCH] Draw spheres at every instance object location Now we can see where each object is (there's no way to tell them apart yet.) The rendering is also extremely inefficient, so don't be surprised if it slows down on your computer. --- apps/mapeditor/include/mapview.h | 5 +- apps/mapeditor/include/objectpass.h | 8 +-- apps/mapeditor/src/mainwindow.cpp | 2 +- apps/mapeditor/src/mapview.cpp | 6 +-- apps/mapeditor/src/objectpass.cpp | 84 +++++++++++------------------ apps/mapeditor/src/primitives.cpp | 4 +- extern/libphysis | 2 +- 7 files changed, 46 insertions(+), 65 deletions(-) diff --git a/apps/mapeditor/include/mapview.h b/apps/mapeditor/include/mapview.h index 04f0df1..b1d33b4 100644 --- a/apps/mapeditor/include/mapview.h +++ b/apps/mapeditor/include/mapview.h @@ -10,13 +10,14 @@ #include struct GameData; +class AppState; class MapView : public QWidget { Q_OBJECT public: - explicit MapView(GameData *data, FileCache &cache, QWidget *parent = nullptr); + explicit MapView(GameData *data, FileCache &cache, AppState *appState, QWidget *parent = nullptr); MDLPart &part() const; @@ -28,4 +29,4 @@ private: GameData *data; FileCache &cache; -}; \ No newline at end of file +}; diff --git a/apps/mapeditor/include/objectpass.h b/apps/mapeditor/include/objectpass.h index 4980760..ee78260 100644 --- a/apps/mapeditor/include/objectpass.h +++ b/apps/mapeditor/include/objectpass.h @@ -7,20 +7,22 @@ class RenderManager; class Device; +class AppState; class ObjectPass : public RendererPass { public: - ObjectPass(RenderManager *renderer); + ObjectPass(RenderManager *renderer, AppState *appState); void render(VkCommandBuffer commandBuffer, Camera &camera) override; private: void createPipeline(); - VkPipeline pipeline_ = nullptr; - VkPipelineLayout pipelineLayout_ = nullptr; + VkPipeline m_pipeline = nullptr; + VkPipelineLayout m_pipelineLayout = nullptr; RenderManager *m_renderer; Device &m_device; + AppState *m_appState; }; diff --git a/apps/mapeditor/src/mainwindow.cpp b/apps/mapeditor/src/mainwindow.cpp index 5633d0e..886052b 100644 --- a/apps/mapeditor/src/mainwindow.cpp +++ b/apps/mapeditor/src/mainwindow.cpp @@ -34,7 +34,7 @@ MainWindow::MainWindow(GameData *data) objectListWidget->setMaximumWidth(400); dummyWidget->addWidget(objectListWidget); - mapView = new MapView(data, cache); + mapView = new MapView(data, cache, m_appState); dummyWidget->addWidget(mapView); setupActions(); diff --git a/apps/mapeditor/src/mapview.cpp b/apps/mapeditor/src/mapview.cpp index 4764620..6405a6e 100644 --- a/apps/mapeditor/src/mapview.cpp +++ b/apps/mapeditor/src/mapview.cpp @@ -9,15 +9,15 @@ #include "filecache.h" #include "objectpass.h" -MapView::MapView(GameData *data, FileCache &cache, QWidget *parent) +MapView::MapView(GameData *data, FileCache &cache, AppState *appState, QWidget *parent) : QWidget(parent) , data(data) , cache(cache) { mdlPart = new MDLPart(data, cache); mdlPart->enableFreemode(); - connect(mdlPart, &MDLPart::initializeRender, this, [this] { - mdlPart->manager()->addPass(new ObjectPass(mdlPart->manager())); + connect(mdlPart, &MDLPart::initializeRender, this, [this, appState] { + mdlPart->manager()->addPass(new ObjectPass(mdlPart->manager(), appState)); }); auto layout = new QVBoxLayout(); diff --git a/apps/mapeditor/src/objectpass.cpp b/apps/mapeditor/src/objectpass.cpp index ca4f798..1ed0ffc 100644 --- a/apps/mapeditor/src/objectpass.cpp +++ b/apps/mapeditor/src/objectpass.cpp @@ -10,9 +10,12 @@ #include -ObjectPass::ObjectPass(RenderManager *renderer) +#include "appstate.h" + +ObjectPass::ObjectPass(RenderManager *renderer, AppState *appState) : m_renderer(renderer) , m_device(m_renderer->device()) + , m_appState(appState) { createPipeline(); @@ -27,60 +30,35 @@ void ObjectPass::render(VkCommandBuffer commandBuffer, Camera &camera) labelExt.pLabelName = "Object Pass"; m_renderer->device().beginDebugMarker(commandBuffer, labelExt); - vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline_); + vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_pipeline); - Primitives::DrawSphere(commandBuffer); + for (const auto &[_, lgb] : m_appState->lgbFiles) { + for (int i = 0; i < lgb.num_chunks; i++) { + const auto chunk = lgb.chunks[i]; + for (int j = 0; j < chunk.num_layers; j++) { + const auto layer = chunk.layers[j]; + for (int z = 0; z < layer.num_objects; z++) { + const auto object = layer.objects[z]; + + glm::mat4 vp = camera.perspective * camera.view; + + vkCmdPushConstants(commandBuffer, m_pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(glm::mat4), &vp); + + auto m = glm::mat4(1.0f); + m = glm::translate(m, {object.transform.translation[0], object.transform.translation[1], object.transform.translation[2]}); + + vkCmdPushConstants(commandBuffer, m_pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, sizeof(glm::mat4), sizeof(glm::mat4), &m); + + Primitives::DrawSphere(commandBuffer); + } + } + } + } m_renderer->device().endDebugMarker(commandBuffer); } else { qWarning() << "Can't render object pass in non-simple renderer for now!!"; } - - /*VkClearValue clearValue = {}; - clearValue.color = {0.0f, 0.0f, 0.0f, 0.0f}; - - VkRenderPassBeginInfo renderPassInfo = {}; - renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; - renderPassInfo.renderPass = m_renderPass; - renderPassInfo.framebuffer = target->sobelFramebuffers[target->currentResource]; - renderPassInfo.renderArea.extent = target->extent; - renderPassInfo.clearValueCount = 1; - renderPassInfo.pClearValues = &clearValue; - - vkCmdBeginRenderPass(commandBuffer, &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE); - - vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline_); - - if(extraInfo != nullptr) { - for (auto mesh: collection.meshes) { - bool shouldRender = false; - for (int i = 0; i < extraInfo->numSelectedEntities; i++) { - if (extraInfo->selectedEntities[i] == mesh.entity) - shouldRender = true; - } - - if (shouldRender) { - glm::mat4 mvp; - mvp = glm::perspective(glm::radians(collection.camera.camera->fov), - (float) target->extent.width / target->extent.height, - collection.camera.camera->near, collection.camera.camera->far); - mvp *= glm::lookAt(collection.camera.transform->position, collection.camera.camera->target, - glm::vec3(0, -1, 0)); - mvp = glm::translate(mvp, mesh.transform->position); - - vkCmdPushConstants(commandBuffer, pipelineLayout_, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(glm::mat4), - &mvp); - - const VkDeviceSize offsets[] = {0}; - vkCmdBindVertexBuffers(commandBuffer, 0, 1, &mesh.mesh->mesh->vertexBuffer, offsets); - vkCmdBindIndexBuffer(commandBuffer, mesh.mesh->mesh->indexBuffer, 0, VK_INDEX_TYPE_UINT32); - - vkCmdDrawIndexed(commandBuffer, mesh.mesh->mesh->indices.size(), 1, 0, 0, 0); - } - } - } - - vkCmdEndRenderPass(commandBuffer);*/ } void ObjectPass::createPipeline() @@ -100,7 +78,7 @@ void ObjectPass::createPipeline() const std::array shaderStages = {vertexShaderStageInfo, fragmentShaderStageInfo}; VkVertexInputBindingDescription vertexBindingDescription = {}; - vertexBindingDescription.stride = sizeof(Vertex); + vertexBindingDescription.stride = sizeof(glm::vec3); VkVertexInputAttributeDescription positionAttributeDescription = {}; positionAttributeDescription.format = VK_FORMAT_R32G32B32_SFLOAT; @@ -156,7 +134,7 @@ void ObjectPass::createPipeline() pipelineLayoutInfo.pushConstantRangeCount = 1; pipelineLayoutInfo.pPushConstantRanges = &pushConstant; - vkCreatePipelineLayout(m_device.device, &pipelineLayoutInfo, nullptr, &pipelineLayout_); + vkCreatePipelineLayout(m_device.device, &pipelineLayoutInfo, nullptr, &m_pipelineLayout); VkPipelineDepthStencilStateCreateInfo depthStencil = {}; depthStencil.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO; @@ -179,8 +157,8 @@ void ObjectPass::createPipeline() pipelineInfo.pColorBlendState = &colorBlending; pipelineInfo.pDynamicState = &dynamicState; pipelineInfo.pDepthStencilState = &depthStencil; - pipelineInfo.layout = pipelineLayout_; + pipelineInfo.layout = m_pipelineLayout; pipelineInfo.renderPass = renderer->renderPass(); - vkCreateGraphicsPipelines(m_device.device, nullptr, 1, &pipelineInfo, nullptr, &pipeline_); + vkCreateGraphicsPipelines(m_device.device, nullptr, 1, &pipelineInfo, nullptr, &m_pipeline); } diff --git a/apps/mapeditor/src/primitives.cpp b/apps/mapeditor/src/primitives.cpp index a0e9000..5933d8e 100644 --- a/apps/mapeditor/src/primitives.cpp +++ b/apps/mapeditor/src/primitives.cpp @@ -18,8 +18,8 @@ void Primitives::Initialize(RenderManager *renderer) std::vector vertices; std::vector indices; - unsigned int xResolution = 64; - unsigned int yResolution = 64; + unsigned int xResolution = 2; + unsigned int yResolution = 2; float PI = 3.14159265359f; for (unsigned int y = 0; y <= yResolution; ++y) { diff --git a/extern/libphysis b/extern/libphysis index bf8aa83..f0b5391 160000 --- a/extern/libphysis +++ b/extern/libphysis @@ -1 +1 @@ -Subproject commit bf8aa831d0c57e40d724602bfd5c311cf1422390 +Subproject commit f0b53912ef8ef9361aaede623ae0a38d1bd833bb