From 07bd22d2ee7e62137ff1be14a03a3931d2e91b7f Mon Sep 17 00:00:00 2001 From: yyc12345 Date: Fri, 9 Jan 2026 20:16:21 +0800 Subject: [PATCH] adapt dx11 engine with new object loader interface --- BasaltPresenter/.gitignore | 1 + .../Plugins/Engine/DirectX11Engine/main.cpp | 102 +++++++++--------- BasaltPresenter/README.md | 61 +++++++++++ BasaltPresenter/Shared/basalt/engine.hpp | 12 ++- 4 files changed, 121 insertions(+), 55 deletions(-) diff --git a/BasaltPresenter/.gitignore b/BasaltPresenter/.gitignore index e3be0cb..05cead1 100644 --- a/BasaltPresenter/.gitignore +++ b/BasaltPresenter/.gitignore @@ -3,6 +3,7 @@ out/ build/ install/ +external/ # Ignore CMake generated stuff CMakeSettings.json diff --git a/BasaltPresenter/Plugins/Engine/DirectX11Engine/main.cpp b/BasaltPresenter/Plugins/Engine/DirectX11Engine/main.cpp index fde2a53..e7d6299 100644 --- a/BasaltPresenter/Plugins/Engine/DirectX11Engine/main.cpp +++ b/BasaltPresenter/Plugins/Engine/DirectX11Engine/main.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -101,50 +102,6 @@ static bool EventLoop() { struct Vertex { float x, y, z; }; -// 立方体顶点 -Vertex CubeVertices[] = { - // 前面 - {-1, -1, -1}, - {1, -1, -1}, - {1, 1, -1}, - {-1, -1, -1}, - {1, 1, -1}, - {-1, 1, -1}, - // 后面 - {-1, -1, 1}, - {-1, 1, 1}, - {1, 1, 1}, - {-1, -1, 1}, - {1, 1, 1}, - {1, -1, 1}, - // 左面 - {-1, -1, -1}, - {-1, -1, 1}, - {-1, 1, 1}, - {-1, -1, -1}, - {-1, 1, 1}, - {-1, 1, -1}, - // 右面 - {1, -1, -1}, - {1, 1, -1}, - {1, 1, 1}, - {1, -1, -1}, - {1, 1, 1}, - {1, -1, 1}, - // 顶面 - {-1, 1, -1}, - {1, 1, -1}, - {1, 1, 1}, - {-1, 1, -1}, - {1, 1, 1}, - {-1, 1, 1}, - // 底面 - {-1, -1, -1}, - {-1, -1, 1}, - {1, -1, 1}, - {-1, -1, -1}, - {1, -1, 1}, - {1, -1, -1}}; // 简单顶点着色器(只传位置) const char* g_VS = R"( @@ -160,9 +117,11 @@ const char* g_PS = R"( } )"; +namespace guid = ::basalt::shared::guid; namespace engine = ::basalt::shared::engine; using engine::EngineConfig; using engine::IEngine; +using ::basalt::shared::math::Vector3; class DirectX11Engine : public IEngine { public: @@ -170,7 +129,7 @@ public: virtual ~DirectX11Engine() {} private: - HWND window; ///< Win32窗口 + HWND window; ///< Win32窗口 ComPtr device; ///< 设备 ComPtr context; ///< 上下文 ComPtr swap_chain; ///< 交换链 @@ -188,8 +147,25 @@ private: ComPtr depth_state; ///< 深度状态 ComPtr rasterizer_state; ///< 光栅化状态 std::vector depth_data; ///< 深度数据 + UINT vertex_count; ///< 顶点数量 public: + virtual guid::Guid get_guid() const { return guid::DIRECTX11_ENGINE; } + //virtual void enumerate_devices() override { + // ComPtr factory; + // ComPtr adapter; + // HRESULT hr = CreateDXGIFactory(IID_PPV_ARGS(&factory)); + // if (SUCCEEDED(hr)) { + // UINT i = 0; + // while (factory->EnumAdapters(i, &adapter) != DXGI_ERROR_NOT_FOUND) { + // DXGI_ADAPTER_DESC desc; + // if (SUCCEEDED(adapter->GetDesc(&desc))) { + // std::wcout << L"Adapter " << i << L": " << desc.Description << std::endl; + // } + // ++i; + // } + // } + //} virtual void startup(EngineConfig&& config) override { IEngine::startup(std::move(config)); @@ -272,14 +248,38 @@ public: D3D11_INPUT_ELEMENT_DESC layout[] = {{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0}}; DXCHK(device->CreateInputLayout(layout, 1, vs_blob->GetBufferPointer(), vs_blob->GetBufferSize(), &input_layout)); - // 顶点缓冲 + // Create vertex buffer from object loader data + std::vector vertices; + size_t total_vertices = 0; + // Count total vertices from all objects + for (size_t i = 0; i < this->config.object_loader->get_object_count(); ++i) { + const auto& obj = this->config.object_loader->get_object(i); + total_vertices += obj.get_vertices_count(); + } + // Collect all vertices from all objects + for (size_t i = 0; i < this->config.object_loader->get_object_count(); ++i) { + const auto& obj = this->config.object_loader->get_object(i); + const auto* obj_vertices = obj.get_vertices(); + size_t obj_vertex_count = obj.get_vertices_count(); + + for (size_t j = 0; j < obj_vertex_count; ++j) { + Vertex v; + v.x = obj_vertices[j].x; + v.y = obj_vertices[j].y; + v.z = obj_vertices[j].z; + vertices.push_back(v); + } + } + // Create vertex buffer with dynamic size D3D11_BUFFER_DESC vb_desc = {}; - vb_desc.ByteWidth = sizeof(CubeVertices); + vb_desc.ByteWidth = static_cast(vertices.size() * sizeof(Vertex)); vb_desc.Usage = D3D11_USAGE_DEFAULT; vb_desc.BindFlags = D3D11_BIND_VERTEX_BUFFER; - D3D11_SUBRESOURCE_DATA vb_data = {CubeVertices}; + D3D11_SUBRESOURCE_DATA vb_data = {vertices.data()}; DXCHK(device->CreateBuffer(&vb_desc, &vb_data, &vertex_buffer)); - + // Update draw call to use actual vertex count + vertex_count = vertices.size(); + // 深度测试启用 D3D11_DEPTH_STENCIL_DESC ds_desc = {}; ds_desc.DepthEnable = TRUE; @@ -342,8 +342,8 @@ public: context->ClearRenderTargetView(rtv.Get(), clear_color); context->ClearDepthStencilView(dsv.Get(), D3D11_CLEAR_DEPTH, 1.0f, 0); - // 绘制立方体 - context->Draw(sizeof(CubeVertices) / sizeof(Vertex), 0); // 自动计算顶点数 + // Draw vertices from object loader or fallback cube + context->Draw(vertex_count, 0); //// 复制深度缓冲到 staging texture //context->CopyResource(depth_staging.Get(), depth_buffer.Get()); diff --git a/BasaltPresenter/README.md b/BasaltPresenter/README.md index 031bb2d..3bc2dfb 100644 --- a/BasaltPresenter/README.md +++ b/BasaltPresenter/README.md @@ -2,9 +2,70 @@ ## Dependencies +* [spdlog](https://github.com/gabime/spdlog) * [tinyobjloader](https://github.com/tinyobjloader/tinyobjloader) * [cgltf](https://github.com/jkuhlmann/cgltf) +We assume that you executing following commands in the root directory of `BasaltPresenter` project. + +### spdlog + +```sh +cd external +git clone -b 1.17.0 https://github.com/gabime/spdlog.git + +cd spdlog +mkdir out +cd out +mkdir build +mkdir install +``` + +In Windows: + +```bat +cmake -A x64 ../.. +cmake --build . --config Release +cmake --install . --prefix=../install +``` + +Or in POSIX: + +```sh +cmake -DCMAKE_BUILD_TYPE=Release ../.. +cmake --build . +cmake --install . --prefix=../install +``` + +#### tinyobjloader + +```sh +cd external +git clone -b v2.0.0 https://github.com/tinyobjloader/tinyobjloader.git + +cd tinyobjloader +mkdir out +cd out +mkdir build +mkdir install +``` + +In Windows: + +```bat +cmake -A x64 ../.. +cmake --build . --config Release +cmake --install . --prefix=../install +``` + +Or in POSIX: + +```sh +cmake -DCMAKE_BUILD_TYPE=Release ../.. +cmake --build . +cmake --install . --prefix=../install +``` + ## Warning This project was not written robustly. diff --git a/BasaltPresenter/Shared/basalt/engine.hpp b/BasaltPresenter/Shared/basalt/engine.hpp index 59f5142..0ddfb5d 100644 --- a/BasaltPresenter/Shared/basalt/engine.hpp +++ b/BasaltPresenter/Shared/basalt/engine.hpp @@ -1,15 +1,19 @@ #pragma once #include "guid.hpp" +#include "object_loader.hpp" +#include "anime_loader.hpp" #include #include namespace basalt::shared::engine { struct EngineConfig { - bool headless; ///< Whether enable headless mode (No Window created). - std::uint32_t width; ///< Window width. - std::uint32_t height; ///< Window height. - guid::Guid deliver; ///< The GUID of deliver. + bool headless; ///< Whether enable headless mode (No Window created). + std::uint32_t width; ///< Window width. + std::uint32_t height; ///< Window height. + guid::Guid deliver; ///< The GUID of deliver. + object_loader::IObjectLoader* object_loader; ///< The instance of object loader. + anime_loader::IAnimeLoader* anime_loader; ///< The instance of anime loader. }; enum class EngineStatus {