응애맘마조

Assimp 로드 본문

공부/3D과제

Assimp 로드

TH.Wert 2023. 6. 21. 22:29

230621 강의 https://keisukeaso.tistory.com/198에 이어서 본인이 원하는 파일을 로드하는 것이 과제였습니다.

#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 ProcessNode(aiNode* node, const aiScene* scene, GameObject* parent)
{
	// 현재 노드의 메시 처리
	for (unsigned int i = 0; i < node->mNumMeshes; i++)
	{
		aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];

		// 메시 정보 가져오기
		vector<VertexPTN> vertices;
		vector<UINT> indices;

		for (unsigned int j = 0; j < mesh->mNumVertices; j++)
		{
			VertexPTN vertex;

			// 텍스쳐 좌표가 있다면
			if (mesh->HasTextureCoords(0))
			{
				aiVector3D textureCoord = mesh->mTextureCoords[0][j];
				vertex.uv = Vector2(textureCoord.x, textureCoord.y);
			}

			// 법선 정보
			if (mesh->HasNormals())
			{
				aiVector3D normal = mesh->mNormals[j];
				vertex.normal = Vector3(normal.x, normal.y, normal.z);
			}

			// 위치 정보
			if (mesh->HasPositions())
			{
				aiVector3D position = mesh->mVertices[j];
				vertex.position = Vector3(position.x, position.y, position.z);
			}

			vertices.push_back(vertex);
		}

		// 인덱스 정보
		for (unsigned int j = 0; j < mesh->mNumFaces; j++)
		{
			aiFace face = mesh->mFaces[j];
			for (unsigned int k = 0; k < face.mNumIndices; k++)
			{
				indices.push_back(face.mIndices[k]);
			}
		}

		// GameObject 생성 및 설정
		GameObject* child = GameObject::Create(mesh->mName.C_Str());
		parent->AddChild(child);
		child->shader = make_shared<Shader>();
		child->shader->LoadFile("3.Cube.hlsl");
		child->mesh = make_shared<Mesh>();
		child->mesh->vertices = &vertices[0];
		child->mesh->vertexCount = vertices.size();
		child->mesh->indices = &indices[0];
		child->mesh->indexCount = indices.size();
		child->mesh->byteWidth = sizeof(VertexPTN);
		child->mesh->primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST;
		child->mesh->vertexType = VertexType::PTN;
		child->mesh->Reset();
	}

	// 자식 노드 처리
	for (unsigned int i = 0; i < node->mNumChildren; i++)
	{
		aiNode* childNode = node->mChildren[i];
		ProcessNode(childNode, scene, parent);
	}
}

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 && "Import Error");

		aiNode* rootNode = scene->mRootNode;
		ProcessNode(rootNode, scene, temp);

		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()
{
}

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;
}

로드는 되지만 xml 저장이나 material 로드를 하면 화면이 멈추거나 터지는 모습이 나옵니다. 아마 해결은 내일 될 것 같습니다.

 

읽어주셔서 감사합니다.

'공부 > 3D과제' 카테고리의 다른 글

노멀 매핑, 탄젠트 스페이스  (0) 2023.06.23
스키닝 애니메이션  (0) 2023.06.22
SIMD  (0) 2023.06.19
맵 안에서 캐릭터 움직이기2  (0) 2023.06.15
맵 안에서 캐릭터 움직이기  (0) 2023.06.13
Comments