응애맘마조
230622 강의 본문
과제 풀이 겸 해서 Assimp 로드할 수 있게 코드를 작성 중이었습니다.
#pragma once
class Main : public Scene
{
private:
Camera* Cam;
Grid* grid;
Actor* temp;
string file;
Assimp::Importer importer;
const aiScene* scene;
public:
Main();
~Main();
virtual void Init() override;
virtual void Release() override; //해제
virtual void Update() override;
virtual void LateUpdate() override;//갱신
virtual void Render() override;
virtual void PreRender() override;
virtual void ResizeScreen() override;
void MakeHierarchy(aiNode* node, GameObject* node2);
void MakeMesh(aiNode* node, GameObject* node2);
};
#include "stdafx.h"
#include "Main.h"
Main::Main()
{
}
Main::~Main()
{
}
void Main::Init()
{
Cam = Camera::Create();
Cam->LoadFile("Cam.xml");
Camera::main = Cam;
Cam->width = App.GetWidth();
Cam->height = App.GetHeight();
Cam->viewport.width = App.GetWidth();
Cam->viewport.height = App.GetHeight();
grid = Grid::Create();
temp = Actor::Create();
}
void Main::Release()
{
}
void Main::Update()
{
ImGui::Begin("Hierarchy");
Cam->RenderHierarchy();
temp->RenderHierarchy();
grid->RenderHierarchy();
ImGui::End();
if (GUI->FileImGui("ModelImporter", "ModelImporter",
".fbx,.obj,.x", "../Assets"))
{
file = ImGuiFileDialog::Instance()->GetCurrentFileName();
string path = "../Assets/" + file;
importer.SetPropertyBool(AI_CONFIG_IMPORT_FBX_PRESERVE_PIVOTS, false);
scene = importer.ReadFile
(
path,
aiProcess_ConvertToLeftHanded
| aiProcess_Triangulate
| aiProcess_GenUVCoords
| aiProcess_GenNormals
| aiProcess_CalcTangentSpace
);
assert(scene != NULL and "Import Error");
temp->ReleaseMember();
GameObject* empty = GameObject::Create("empty");
temp->AddChild(empty);
MakeHierarchy(scene->mRootNode, empty);
importer.FreeScene();
}
Camera::ControlMainCam();
Cam->Update();
grid->Update();
temp->Update();
}
void Main::LateUpdate()
{
}
void Main::PreRender()
{
}
void Main::Render()
{
Cam->Set();
grid->Render();
temp->Render();
}
void Main::ResizeScreen()
{
}
void Main::MakeHierarchy(aiNode* node, GameObject* node2)
{
MakeMesh(node, node2);
GameObject* child = GameObject::Create(node->mName.C_Str());
node2->AddChild(child);
for (int i = 0; i < node->mNumChildren; i++)
{
MakeHierarchy(node->mChildren[i], child);
}
}
void Main::MakeMesh(aiNode* node, GameObject* node2)
{
//루트 노드에 담겨있는 메쉬 갯수만큼 반복
for (int i = 0; i < node->mNumMeshes; i++)
{
aiMesh* mesh = scene->mMeshes[i];
aiMaterial* mtl = scene->mMaterials[mesh->mMaterialIndex];
node2->shader = RESOURCE->shaders.Load("3.Cube.hlsl");
node2->mesh = make_shared<Mesh>();
node2->mesh->byteWidth = sizeof(VertexPTN);
node2->mesh->primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST;
node2->mesh->vertexType = VertexType::PTN;
node2->mesh->vertexCount = mesh->mNumVertices;
node2->mesh->vertices = new VertexPTN[mesh->mNumVertices];
vector<UINT> indexList;
for (int j = 0; j < mesh->mNumVertices; j++)
{
VertexPTN* vertex = (VertexPTN*)node2->mesh->vertices;
//텍스쳐 좌표가 있다면
if (mesh->HasTextureCoords(0))
{
vertex[j].uv.x =mesh->mTextureCoords[0][j].x;
vertex[j].uv.y =mesh->mTextureCoords[0][j].y;
}
if (mesh->HasNormals())
{
vertex[j].normal.x = mesh->mNormals[j].x;
vertex[j].normal.y = mesh->mNormals[j].y;
vertex[j].normal.z = mesh->mNormals[j].z;
}
if (mesh->HasPositions())
{
vertex[j].position.x = mesh->mVertices[j].x;
vertex[j].position.y = mesh->mVertices[j].y;
vertex[j].position.z = mesh->mVertices[j].z;
}
}
for (int j = 0; j < mesh->mNumFaces; j++)
{
for (int k = 0; k < mesh->mFaces[j].mNumIndices; k++)
{
indexList.push_back(mesh->mFaces[j].mIndices[k]);
}
}
node2->mesh->indexCount = indexList.size();
node2->mesh->indices = new UINT[indexList.size()];
copy(indexList.begin(), indexList.end(),
stdext::checked_array_iterator<UINT*>
(node2->mesh->indices, indexList.size()));
node2->mesh->Reset();
}
}
int WINAPI wWinMain(HINSTANCE instance, HINSTANCE prevInstance, LPWSTR param, int command)
{
App.SetAppName(L"ObjLoader");
App.SetInstance(instance);
WIN->Create();
D3D->Create();
Main * main = new Main();
main->Init();
int wParam = (int)WIN->Run(main);
main->Release();
SafeDelete(main);
D3D->DeleteSingleton();
WIN->DeleteSingleton();
return wParam;
}
이렇게 하면 Shader 없이도 로드가 가능하긴 한데 아직 이유를 찾지 못했는지 마지막에 있는 Mesh만 로드가 되어서 이전의 Mesh는 로드가 되어도 마지막 Mesh에 덮어 씌워지는지 실행 창에 표시가 되지 않는 문제가 있습니다. 과제나 내일 강의 중에 해결될 예정이고 다음 주에는 애니메이션 효과까지 한다고 했습니다.
읽어주셔서 감사합니다.