[DirectX11] 034~36 Model Editor
목차
Framework
Framework 모델 불러오기, 모델 렌더링 |
|
UnitTest | Model Editor |
Game Map Editor |
Assimp에서 파일을 부름 원하는 형태의 변수에 저장 원하는 형태의 파일 형식으로 저장 |
Importer -> Scene
< Model Editor >
Scene이 가지고 있는 정보들
- Materials
- Meshes
- SceneRoot
- body (- TransformMatrix - Material - Mesh - Vertice - Indice)
- 바퀴
- 바퀴
- 포탑
- 포신
- body (- TransformMatrix - Material - Mesh - Vertice - Indice)
- 재귀를 해서 찾으면 기본정보들을 찾아올 수 있다.
- 예시. Model - ModelBone - Model Mesh -> Render
https://github.com/assimp/assimp
Framework 세팅
Framework 우클릭 - 속성 - 빌드이벤트 - 빌드 후 이벤트 - 명령줄 추가
Mesh 정보 저장 방법
Mesh 정보 저장 방법
- Mesh->text 파일 : 느리다.
- Mesh->Byte file 또는 Mesh->Binary file: 빠르다
ModelEditor 폴더 설정
빌드종속성
- ModelEditor 폴더 우클릭 - 빌드종속성- 프로젝트 종속성
- Framework 체크하여 Framework가 먼저 빌드되게 설정
환경변수 설정
- ModelEditor 폴더 우클릭 - 빌드종속성- 프로젝트 종속성
- Framework 체크하여 Framework가 먼저 빌드되게 설정
stdafx.h
stdafx.h
더보기
#pragma once
#include "Framework.h"
#pragma comment(lib, "Framework.lib")
#include "Assimp/Importer.hpp"
#include "Assimp/scene.h"
#include "Assimp/postprocess.h"
#pragma comment(lib, "Assimp/assimp-vc140-mt.lib")
Type.h
Type.h
더보기
#pragma once
#include "stdafx.h"
struct asBone
{
int Index;
string Name;
int Parent;
Matrix Transform;
};
struct asMesh
{
string Name;
int BoneIndex;
aiMesh* Mesh;
string MaterialName;
vector<Model::ModelVertex> Vertices;
vector<UINT> Indices;
};
Model
Model.h
#pragma once
class Model
{
public:
typedef VertexTextureNormalTangentBlend ModelVertex;
};
Model.cpp
#include "Framework.h"
#include "Model.h"
Converter
Converter.h
더보기
#pragma once
#include "stdafx.h"
class Converter
{
public:
Converter();
~Converter();
void ReadFile(wstring file);
public:
void ExportMesh(wstring savePath);
private:
void ReadBoneData(aiNode* node, int index, int parent);
void ReadMeshData(aiNode* node, int bone);
void WriteMeshData(wstring savePath);
private:
wstring file;
Assimp::Importer* importer;
const aiScene* scene;
vector<struct asBone *> bones;
vector<struct asMesh *> meshes;
};
Converter.cpp
더보기
#include "stdafx.h"
#include "Converter.h"
#include "Types.h"
#include "Utilities/BinaryFile.h"
Converter::Converter()
{
importer = new Assimp::Importer();
}
Converter::~Converter()
{
SafeDelete(importer);
}
void Converter::ReadFile(wstring file)
{
this->file = L"../../_Assets/" + file;
scene = importer->ReadFile
(
String::ToString(this->file),
aiProcess_ConvertToLeftHanded
| aiProcess_Triangulate
| aiProcess_GenUVCoords
| aiProcess_GenNormals
| aiProcess_CalcTangentSpace
);
assert(scene != NULL);
}
void Converter::ExportMesh(wstring savePath)
{
savePath = L"../../_Models/" + savePath + L".mesh";
ReadBoneData(scene->mRootNode, -1, -1);
WriteMeshData(savePath);
}
void Converter::ReadBoneData(aiNode * node, int index, int parent)
{
asBone* bone = new asBone();
bone->Index = index;
bone->Parent = parent;
bone->Name = node->mName.C_Str();
Matrix transform(node->mTransformation[0]);
D3DXMatrixTranspose(&bone->Transform, &transform);
Matrix matParent;
if (parent < 0)
D3DXMatrixIdentity(&matParent);
else
matParent = bones[parent]->Transform;
bone->Transform = bone->Transform * matParent;
bones.push_back(bone);
ReadMeshData(node, index);
for (UINT i = 0; i < node->mNumChildren; i++)
ReadBoneData(node->mChildren[i], bones.size(), index);
}
void Converter::ReadMeshData(aiNode * node, int bone)
{
if (node->mNumMeshes < 1) return;
asMesh* mesh = new asMesh();
mesh->Name = node->mName.C_Str();
mesh->BoneIndex = bone;
for (UINT i = 0; i < node->mNumMeshes; i++)
{
UINT index = node->mMeshes[i];
aiMesh* srcMesh = scene->mMeshes[index];
aiMaterial* material = scene->mMaterials[srcMesh->mMaterialIndex];
mesh->MaterialName = material->GetName().C_Str();
UINT startVertex = mesh->Vertices.size();
for (UINT v = 0; v < srcMesh->mNumVertices; v++)
{
Model::ModelVertex vertex;
memcpy(&vertex.Position, &srcMesh->mVertices[v], sizeof(Vector3));
if (srcMesh->HasTextureCoords(0))
memcpy(&vertex.Uv, &srcMesh->mTextureCoords[0][v], sizeof(Vector2));
if(srcMesh->HasNormals())
memcpy(&vertex.Normal, &srcMesh->mNormals[v], sizeof(Vector3));
mesh->Vertices.push_back(vertex);
}
for (UINT f = 0; f < srcMesh->mNumFaces; f++)
{
aiFace& face = srcMesh->mFaces[f];
for (UINT k = 0; k < face.mNumIndices; k++)
mesh->Indices.push_back(face.mIndices[k] + startVertex);
}
meshes.push_back(mesh);
}
}
void Converter::WriteMeshData(wstring savePath)
{
Path::CreateFolders(Path::GetDirectoryName(savePath));
BinaryWriter* w = new BinaryWriter();
w->Open(savePath);
w->UInt(bones.size());
for (asBone* bone : bones)
{
w->Int(bone->Index);
w->String(bone->Name);
w->Int(bone->Parent);
w->Matrix(bone->Transform);
SafeDelete(bone);
}
w->UInt(meshes.size());
for (asMesh* meshData : meshes)
{
w->String(meshData->Name);
w->Int(meshData->BoneIndex);
w->String(meshData->MaterialName);
w->UInt(meshData->Vertices.size());
w->Byte(&meshData->Vertices[0], sizeof(Model::ModelVertex) * meshData->Vertices.size());
w->UInt(meshData->Indices.size());
w->Byte(&meshData->Indices[0], sizeof(UINT) * meshData->Indices.size());
SafeDelete(meshData);
}
w->Close(); //Binary 닫아줌. 안 닫으면 파일 접근불가.
SafeDelete(w);
}
ExportFile
ExporFile.h
더보기
#pragma once
#include "Systems/IExecute.h"
class ExportFile : public IExecute
{
public:
virtual void Initialize() override;
virtual void Ready() override {}
virtual void Destroy() override {}
virtual void Update() override {}
virtual void PreRender() override {}
virtual void Render() override {}
virtual void PostRender() override {}
virtual void ResizeScreen() override {}
private:
void Tank();
};
ExporFile.cpp
더보기
#include "stdafx.h"
#include "ExportFile.h"
#include "Converter.h"
void ExportFile::Initialize()
{
Tank();
}
void ExportFile::Tank()
{
Converter* conv = new Converter();
conv->ReadFile(L"Tank/Tank.fbx");
conv->ExportMesh(L"Tank/Tank");
SafeDelete(conv);
}
'⭐ DirectX > DirectX11 3D' 카테고리의 다른 글
[DirectX11] 038 Model Render (0) | 2023.02.06 |
---|---|
[DirectX11] 037 Model Reader (0) | 2023.02.05 |
[DirectX11] 033 Framework - CubeMap, CubeSky (0) | 2023.02.01 |
[DirectX11] 032 Renderer (0) | 2023.01.31 |
[DirectX11] 031 Framework (0) | 2023.01.30 |
댓글
이 글 공유하기
다른 글
-
[DirectX11] 038 Model Render
[DirectX11] 038 Model Render
2023.02.06 -
[DirectX11] 037 Model Reader
[DirectX11] 037 Model Reader
2023.02.05 -
[DirectX11] 033 Framework - CubeMap, CubeSky
[DirectX11] 033 Framework - CubeMap, CubeSky
2023.02.01 -
[DirectX11] 032 Renderer
[DirectX11] 032 Renderer
2023.01.31