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
+122 -26
View File
@@ -2,16 +2,16 @@
#include "Utilities.h"
#include "Vertex.h"
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <vector>
UINT Mesh::init(ID3D11Device* pD3DDevice)
{
UINT error = 0;
error = createVertexBuffer(pD3DDevice);
CheckError(error);
error = createIndexBuffer(pD3DDevice);
CheckError(error);
XMStoreFloat4x4(&_worldMatrix, XMMatrixIdentity());
return 0;
@@ -22,11 +22,14 @@ void Mesh::update(FLOAT dt)
static float x = 0.0f;
static float y = 0.0f;
static float z = 0.0f;
static float rotY = 0.0f;
static float rotZ = 0.0f;
static float scale = 0.5f;
rotY += 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_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;
XMMATRIX translation = XMMatrixTranslation(x, y, z);
XMMATRIX rotation = XMMatrixRotationRollPitchYaw(0.0f, 0.0f, rotZ);
XMMATRIX scale = XMMatrixScaling(1.0f, 1.0f, 1.0f);
XMMATRIX rotation = XMMatrixRotationRollPitchYaw(0.0f, rotY, rotZ);
XMMATRIX localScale = XMMatrixScaling(scale, scale, scale);
XMStoreFloat4x4(&_worldMatrix, scale * rotation * translation);
XMStoreFloat4x4(&_worldMatrix, localScale * rotation * translation);
}
void Mesh::render(ID3D11DeviceContext* pD3DDeviceContext)
@@ -77,11 +80,94 @@ void Mesh::deInit()
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
// 1. define the structure (vertices)
// vertices
Vertex vertices[] = {
// triangle
//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);
_vertexStride = sizeof(Vertex);
// indices
USHORT indices[] = {
// triangles
//0, 1, 2
// 2. create the buffer
// for Usage: https://learn.microsoft.com/en-us/windows/win32/api/d3d11/ne-d3d11-d3d11_usage
// quad
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 = {};
desc.ByteWidth = _vertexCount * _vertexStride; // size of the buffer
desc.BindFlags = D3D11_BIND_VERTEX_BUFFER; // buffer type
@@ -114,19 +221,8 @@ UINT Mesh::createVertexBuffer(ID3D11Device* pD3DDevice)
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 = {};
desc.ByteWidth = _indexCount * sizeof(USHORT);
desc.BindFlags = D3D11_BIND_INDEX_BUFFER;