1
0

adapt dx11 engine with new object loader interface

This commit is contained in:
2026-01-09 20:16:21 +08:00
parent 184b94c5fc
commit 07bd22d2ee
4 changed files with 121 additions and 55 deletions

View File

@@ -3,6 +3,7 @@
out/
build/
install/
external/
# Ignore CMake generated stuff
CMakeSettings.json

View File

@@ -1,5 +1,6 @@
#include <basalt/export_macro.hpp>
#include <basalt/engine.hpp>
#include <basalt/object_loader.hpp>
#include <windows.h>
#include <d3d11.h>
#include <d3dcompiler.h>
@@ -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<ID3D11Device> device; ///< 设备
ComPtr<ID3D11DeviceContext> context; ///< 上下文
ComPtr<IDXGISwapChain> swap_chain; ///< 交换链
@@ -188,8 +147,25 @@ private:
ComPtr<ID3D11DepthStencilState> depth_state; ///< 深度状态
ComPtr<ID3D11RasterizerState> rasterizer_state; ///< 光栅化状态
std::vector<BYTE> depth_data; ///< 深度数据
UINT vertex_count; ///< 顶点数量
public:
virtual guid::Guid get_guid() const { return guid::DIRECTX11_ENGINE; }
//virtual void enumerate_devices() override {
// ComPtr<IDXGIFactory> factory;
// ComPtr<IDXGIAdapter> 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,13 +248,37 @@ 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<Vertex> 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<UINT>(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 = {};
@@ -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());

View File

@@ -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.

View File

@@ -1,15 +1,19 @@
#pragma once
#include "guid.hpp"
#include "object_loader.hpp"
#include "anime_loader.hpp"
#include <string>
#include <cinttypes>
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 {