Added: assimp implementation

This commit is contained in:
2026-10-19 18:30:50 +02:00
parent 13691e3da3
commit 2246370722
18 changed files with 205 additions and 34 deletions
+4 -1
View File
@@ -418,4 +418,7 @@ FodyWeavers.xsd
# JetBrains Rider # JetBrains Rider
.idea/ .idea/
*.sln.iml *.sln.iml
# vcpkg
vcpkg_installed/
+2 -2
View File
@@ -3,7 +3,7 @@
UINT Camera::init(UINT width, UINT height) UINT Camera::init(UINT width, UINT height)
{ {
// view matrix // view matrix
XMVECTOR position = XMVectorSet(0.0f, 0.0f, -2.0f, 0.0f); XMVECTOR position = XMVectorSet(0.0f, 100.0f, -500.0f, 0.0f);
XMVECTOR forward = XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f); XMVECTOR forward = XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f);
XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
@@ -14,7 +14,7 @@ UINT Camera::init(UINT width, UINT height)
float fov = XMConvertToRadians(60.0f); float fov = XMConvertToRadians(60.0f);
float aspectRatio = static_cast<float>(width) / static_cast<float>(height); float aspectRatio = static_cast<float>(width) / static_cast<float>(height);
float nearClippingPlane = 0.3f; float nearClippingPlane = 0.3f;
float farClippingPlane = 10.0f; float farClippingPlane = 1000.0f;
XMMATRIX projection = XMMatrixPerspectiveFovLH(fov, aspectRatio, nearClippingPlane, farClippingPlane); XMMATRIX projection = XMMatrixPerspectiveFovLH(fov, aspectRatio, nearClippingPlane, farClippingPlane);
XMStoreFloat4x4(&_projectionMatrix, projection); XMStoreFloat4x4(&_projectionMatrix, projection);
Binary file not shown.
Binary file not shown.
+3
View File
@@ -97,6 +97,9 @@
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup> </ImportGroup>
<PropertyGroup Label="UserMacros" /> <PropertyGroup Label="UserMacros" />
<PropertyGroup Label="Vcpkg">
<VcpkgEnableManifest>true</VcpkgEnableManifest>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile> <ClCompile>
<WarningLevel>Level3</WarningLevel> <WarningLevel>Level3</WarningLevel>
+35 -1
View File
@@ -54,8 +54,39 @@ INT Direct3D::init(HWND hWnd, UINT width, UINT height, BOOL isFullscreen)
safeRelease(pBackBuffer); safeRelease(pBackBuffer);
// 4. create depth stencil buffer // 4. create depth stencil buffer
ID3D11Texture2D* pDepthStencilBuffer = nullptr;
D3D11_TEXTURE2D_DESC depthStencilBufferDesc = {};
depthStencilBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
depthStencilBufferDesc.Width = width;
depthStencilBufferDesc.Height = height;
depthStencilBufferDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthStencilBufferDesc.ArraySize = 1;
depthStencilBufferDesc.Usage = D3D11_USAGE_DEFAULT;
depthStencilBufferDesc.SampleDesc.Count = 1;
hr = _pD3DDevice->CreateTexture2D(&depthStencilBufferDesc, nullptr, &pDepthStencilBuffer);
CheckFailed(hr, 225);
hr = _pD3DDevice->CreateDepthStencilView(pDepthStencilBuffer, nullptr, &_pD3DDepthStencilView);
CheckFailed(hr, 230);
safeRelease(pDepthStencilBuffer);
D3D11_DEPTH_STENCIL_DESC depthStencilDesc = {};
depthStencilDesc.DepthEnable = true;
depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS_EQUAL;
depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
hr = _pD3DDevice->CreateDepthStencilState(&depthStencilDesc, &_pD3DDepthStencilState);
CheckFailed(hr, 240);
// 5. create rasterizer state // 5. create rasterizer state
D3D11_RASTERIZER_DESC rasterizerDesc = {};
rasterizerDesc.FillMode = D3D11_FILL_SOLID;
rasterizerDesc.CullMode = D3D11_CULL_BACK;
hr = _pD3DDevice->CreateRasterizerState(&rasterizerDesc, &_pD3DRasterizerState);
CheckFailed(hr, 250);
// 6. describe viewport // 6. describe viewport
D3D11_VIEWPORT viewport = {}; D3D11_VIEWPORT viewport = {};
@@ -66,7 +97,9 @@ INT Direct3D::init(HWND hWnd, UINT width, UINT height, BOOL isFullscreen)
// 7. setup render pipeline (device context) // 7. setup render pipeline (device context)
_pD3DDeviceContext->RSSetViewports(1, &viewport); _pD3DDeviceContext->RSSetViewports(1, &viewport);
_pD3DDeviceContext->OMSetRenderTargets(1, &_pD3DRenderTargetView, nullptr); // TODO: create depth stencil view and set it for output merger stage _pD3DDeviceContext->OMSetRenderTargets(1, &_pD3DRenderTargetView, _pD3DDepthStencilView);
_pD3DDeviceContext->OMSetDepthStencilState(_pD3DDepthStencilState, 0xFFFFFFFF);
_pD3DDeviceContext->RSSetState(_pD3DRasterizerState);
return 0; return 0;
} }
@@ -79,6 +112,7 @@ void Direct3D::beginScene()
// 8. clear the buffers // 8. clear the buffers
_pD3DDeviceContext->ClearRenderTargetView(_pD3DRenderTargetView, color); _pD3DDeviceContext->ClearRenderTargetView(_pD3DRenderTargetView, color);
_pD3DDeviceContext->ClearDepthStencilView(_pD3DDepthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0xFFFFFFFF);
} }
void Direct3D::endScene() void Direct3D::endScene()
+3
View File
@@ -19,5 +19,8 @@ private:
ID3D11DeviceContext* _pD3DDeviceContext = nullptr; // access to render pipeline, e.g. set matrices or draw command, used in run phase ID3D11DeviceContext* _pD3DDeviceContext = nullptr; // access to render pipeline, e.g. set matrices or draw command, used in run phase
IDXGISwapChain* _pDXGISwapChain = nullptr; // holds references to front & back buffer & swaps them IDXGISwapChain* _pDXGISwapChain = nullptr; // holds references to front & back buffer & swaps them
ID3D11RenderTargetView* _pD3DRenderTargetView = nullptr; // target device context is rendering to ID3D11RenderTargetView* _pD3DRenderTargetView = nullptr; // target device context is rendering to
ID3D11DepthStencilView* _pD3DDepthStencilView = nullptr; // saves depth & stencil value of every pixel
ID3D11DepthStencilState* _pD3DDepthStencilState = nullptr; // set the current settings for depth-stenciling
ID3D11RasterizerState* _pD3DRasterizerState = nullptr; // set additional settings for the render pipeline
}; };
+122 -26
View File
@@ -2,16 +2,16 @@
#include "Utilities.h" #include "Utilities.h"
#include "Vertex.h" #include "Vertex.h"
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <vector>
UINT Mesh::init(ID3D11Device* pD3DDevice) UINT Mesh::init(ID3D11Device* pD3DDevice)
{ {
UINT error = 0; UINT error = 0;
error = createVertexBuffer(pD3DDevice);
CheckError(error);
error = createIndexBuffer(pD3DDevice);
CheckError(error);
XMStoreFloat4x4(&_worldMatrix, XMMatrixIdentity()); XMStoreFloat4x4(&_worldMatrix, XMMatrixIdentity());
return 0; return 0;
@@ -22,11 +22,14 @@ void Mesh::update(FLOAT dt)
static float x = 0.0f; static float x = 0.0f;
static float y = 0.0f; static float y = 0.0f;
static float z = 0.0f; static float z = 0.0f;
static float rotY = 0.0f;
static float rotZ = 0.0f; static float rotZ = 0.0f;
static float scale = 0.5f;
rotY += XM_PIDIV4 * dt;
//rotZ += XM_PIDIV4 * dt; //rotZ += XM_PIDIV4 * dt;
float move = 10.0f * dt; float move = 100.0f * dt;
if (GetAsyncKeyState(VK_LEFT) & 0x8000 || GetAsyncKeyState('A') & 0x8000) x -= move; if (GetAsyncKeyState(VK_LEFT) & 0x8000 || GetAsyncKeyState('A') & 0x8000) x -= move;
if (GetAsyncKeyState(VK_RIGHT) & 0x8000 || GetAsyncKeyState('D') & 0x8000) x += move; if (GetAsyncKeyState(VK_RIGHT) & 0x8000 || GetAsyncKeyState('D') & 0x8000) x += move;
@@ -36,10 +39,10 @@ void Mesh::update(FLOAT dt)
if (GetAsyncKeyState(VK_ADD) & 0x8000 || GetAsyncKeyState('E') & 0x8000) z += move; if (GetAsyncKeyState(VK_ADD) & 0x8000 || GetAsyncKeyState('E') & 0x8000) z += move;
XMMATRIX translation = XMMatrixTranslation(x, y, z); XMMATRIX translation = XMMatrixTranslation(x, y, z);
XMMATRIX rotation = XMMatrixRotationRollPitchYaw(0.0f, 0.0f, rotZ); XMMATRIX rotation = XMMatrixRotationRollPitchYaw(0.0f, rotY, rotZ);
XMMATRIX scale = XMMatrixScaling(1.0f, 1.0f, 1.0f); XMMATRIX localScale = XMMatrixScaling(scale, scale, scale);
XMStoreFloat4x4(&_worldMatrix, scale * rotation * translation); XMStoreFloat4x4(&_worldMatrix, localScale * rotation * translation);
} }
void Mesh::render(ID3D11DeviceContext* pD3DDeviceContext) void Mesh::render(ID3D11DeviceContext* pD3DDeviceContext)
@@ -77,11 +80,94 @@ void Mesh::deInit()
safeRelease(_pIndexBuffer); safeRelease(_pIndexBuffer);
} }
UINT Mesh::createVertexBuffer(ID3D11Device* pD3DDevice) UINT Mesh::loadMeshFromFile(ID3D11Device* pD3DDevice, string filename)
{ {
// clear old buffers
safeRelease(_pVertexBuffer);
safeRelease(_pIndexBuffer);
// assimp implementation
Assimp::Importer importer;
const aiScene* pScene = importer.ReadFile(
filename,
aiProcess_Triangulate | aiProcess_JoinIdenticalVertices | aiProcess_ConvertToLeftHanded | aiProcess_PreTransformVertices
);
if (pScene == nullptr || !pScene->HasMeshes()) return 350;
const aiMesh* pMesh = pScene->mMeshes[0];
// import vertices
vector<Vertex> vertices;
for (size_t v = 0; v < pMesh->mNumVertices; v++)
{
// position
const aiVector3D& position = pMesh->mVertices[v];
// uv
if (pMesh->HasTextureCoords(0))
{
const aiVector3D& uv = pMesh->mTextureCoords[0][v];
vertices.emplace_back(
position.x, position.y, position.z,
uv.x, uv.y
);
}
// vertex color
if (pMesh->HasVertexColors(0))
{
const aiColor4D& color = pMesh->mColors[0][v];
vertices.emplace_back(
position.x, position.y, position.z,
color.r, color.g, color.b
);
}
}
// import indices
vector<USHORT> indices;
if (pMesh->mNumVertices > USHRT_MAX) return 360;
for (size_t f = 0; f < pMesh->mNumFaces; f++)
{
const aiFace& face = pMesh->mFaces[f];
if (face.mNumIndices != 3) continue;
indices.push_back(face.mIndices[0]);
indices.push_back(face.mIndices[1]);
indices.push_back(face.mIndices[2]);
}
// update mesh properties
_vertexCount = vertices.size();
_vertexStride = sizeof(Vertex);
_indexCount = indices.size();
UINT error = createVertexBuffer(pD3DDevice, vertices.data());
CheckError(error);
error = createIndexBuffer(pD3DDevice, indices.data());
CheckError(error);
return 0;
}
UINT Mesh::loadDefaultMesh(ID3D11Device* pD3DDevice)
{
// clear old buffers
safeRelease(_pVertexBuffer);
safeRelease(_pIndexBuffer);
// TODO: create a cube // TODO: create a cube
// 1. define the structure (vertices) // vertices
Vertex vertices[] = { Vertex vertices[] = {
// triangle // triangle
//Vertex(0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f), // 0 //Vertex(0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f), // 0
@@ -97,9 +183,30 @@ UINT Mesh::createVertexBuffer(ID3D11Device* pD3DDevice)
_vertexCount = std::size(vertices); _vertexCount = std::size(vertices);
_vertexStride = sizeof(Vertex); _vertexStride = sizeof(Vertex);
// indices
USHORT indices[] = {
// triangles
//0, 1, 2
// 2. create the buffer // quad
// for Usage: https://learn.microsoft.com/en-us/windows/win32/api/d3d11/ne-d3d11-d3d11_usage 0, 1, 2,
0, 2, 3
};
_indexCount = std::size(indices);
UINT error = createVertexBuffer(pD3DDevice, vertices);
CheckError(error);
error = createIndexBuffer(pD3DDevice, indices);
CheckError(error);
return 0;
}
UINT Mesh::createVertexBuffer(ID3D11Device* pD3DDevice, Vertex* vertices)
{
D3D11_BUFFER_DESC desc = {}; D3D11_BUFFER_DESC desc = {};
desc.ByteWidth = _vertexCount * _vertexStride; // size of the buffer desc.ByteWidth = _vertexCount * _vertexStride; // size of the buffer
desc.BindFlags = D3D11_BIND_VERTEX_BUFFER; // buffer type desc.BindFlags = D3D11_BIND_VERTEX_BUFFER; // buffer type
@@ -114,19 +221,8 @@ UINT Mesh::createVertexBuffer(ID3D11Device* pD3DDevice)
return 0; return 0;
} }
UINT Mesh::createIndexBuffer(ID3D11Device* pD3DDevice) UINT Mesh::createIndexBuffer(ID3D11Device* pD3DDevice, USHORT* indices)
{ {
USHORT indices[] = {
// triangles
//0, 1, 2
// quad
0, 1, 2,
0, 2, 3
};
_indexCount = std::size(indices);
D3D11_BUFFER_DESC desc = {}; D3D11_BUFFER_DESC desc = {};
desc.ByteWidth = _indexCount * sizeof(USHORT); desc.ByteWidth = _indexCount * sizeof(USHORT);
desc.BindFlags = D3D11_BIND_INDEX_BUFFER; desc.BindFlags = D3D11_BIND_INDEX_BUFFER;
+8 -2
View File
@@ -1,8 +1,12 @@
#pragma once #pragma once
#include <d3d11.h> #include <d3d11.h>
#include <DirectXMath.h> #include <DirectXMath.h>
#include <string>
#include "Vertex.h"
using namespace DirectX; using namespace DirectX;
using namespace std;
class Mesh class Mesh
{ {
@@ -13,10 +17,12 @@ public:
void deInit(); void deInit();
const XMFLOAT4X4& getWorldMatrix() const { return _worldMatrix; } const XMFLOAT4X4& getWorldMatrix() const { return _worldMatrix; }
UINT loadMeshFromFile(ID3D11Device* pD3DDevice, string filename);
UINT loadDefaultMesh(ID3D11Device* pD3DDevice);
private: private:
UINT createVertexBuffer(ID3D11Device* pD3DDevice); UINT createVertexBuffer(ID3D11Device* pD3DDevice, Vertex* vertices);
UINT createIndexBuffer(ID3D11Device* pD3DDevice); UINT createIndexBuffer(ID3D11Device* pD3DDevice, USHORT* indices);
ID3D11Buffer* _pVertexBuffer = nullptr; // dumps mesh vertices ID3D11Buffer* _pVertexBuffer = nullptr; // dumps mesh vertices
ID3D11Buffer* _pIndexBuffer = nullptr; // dumps mesh indices ID3D11Buffer* _pIndexBuffer = nullptr; // dumps mesh indices
Binary file not shown.
@@ -5,9 +5,10 @@ struct PixelInput
{ {
float4 position : SV_Position; float4 position : SV_Position;
float2 uv : TEXCOORD; float2 uv : TEXCOORD;
float4 color : COLOR;
}; };
float4 main(PixelInput INPUT) : SV_TARGET float4 main(PixelInput INPUT) : SV_TARGET
{ {
return MainTexture.Sample(MainSampler, INPUT.uv); return MainTexture.Sample(MainSampler, INPUT.uv) * INPUT.color;
} }
Binary file not shown.
@@ -9,12 +9,14 @@ struct VertexInput
{ {
float4 position : POSITION; float4 position : POSITION;
float2 uv : TEXCOORD; float2 uv : TEXCOORD;
float4 color : COLOR;
}; };
struct VertexOutput struct VertexOutput
{ {
float4 position : SV_Position; float4 position : SV_Position;
float2 uv : TEXCOORD; float2 uv : TEXCOORD;
float4 color : COLOR;
}; };
VertexOutput main(VertexInput INPUT) VertexOutput main(VertexInput INPUT)
@@ -24,6 +26,7 @@ VertexOutput main(VertexInput INPUT)
OUTPUT.position = mul(INPUT.position, WorldViewProjectionMatrix); OUTPUT.position = mul(INPUT.position, WorldViewProjectionMatrix);
OUTPUT.uv = INPUT.uv; OUTPUT.uv = INPUT.uv;
//OUTPUT.uv = INPUT.uv * float2(1.1f, 1.1f) + float2(-0.05f, -0.05f); // uv * tiling + offset //OUTPUT.uv = INPUT.uv * float2(1.1f, 1.1f) + float2(-0.05f, -0.05f); // uv * tiling + offset
OUTPUT.color = INPUT.color;
return OUTPUT; return OUTPUT;
} }
Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB

+4 -1
View File
@@ -36,6 +36,9 @@ int WINAPI WinMain(
Mesh mesh = {}; Mesh mesh = {};
error = mesh.init(d3d.getDevice()); error = mesh.init(d3d.getDevice());
CheckError(error); CheckError(error);
//error = mesh.loadDefaultMesh(d3d.getDevice());
error = mesh.loadMeshFromFile(d3d.getDevice(), "ship-pirate-large.fbx");
CheckError(error);
// 4. create a camera // 4. create a camera
Camera camera = {}; Camera camera = {};
@@ -49,7 +52,7 @@ int WINAPI WinMain(
// 6. create a material // 6. create a material
Material material = {}; Material material = {};
error = material.init(d3d.getDevice(), TEXT("wall.jpg")); error = material.init(d3d.getDevice(), TEXT("colormap.png"));
CheckError(error); CheckError(error);
// 7. create light source // 7. create light source
Binary file not shown.
@@ -0,0 +1,14 @@
{
"default-registry": {
"kind": "git",
"baseline": "3af1d1e60af2b2abf55760538cd607829029b07a",
"repository": "https://github.com/microsoft/vcpkg"
},
"registries": [
{
"kind": "artifact",
"location": "https://github.com/microsoft/vcpkg-ce-catalog/archive/refs/heads/main.zip",
"name": "microsoft"
}
]
}
+5
View File
@@ -0,0 +1,5 @@
{
"dependencies": [
"assimp"
]
}