응애맘마조

230609 강의 본문

공부/3D강의

230609 강의

TH.Wert 2023. 6. 11. 22:22

외부로부터 파일을 불러오는 방식에 대해서 강의를 했습니다.

#pragma once

class Main : public Scene
{
private:
	Camera*		Cam;
	Grid*		grid;
	Actor*		temp;
public:
	Main();
	~Main();
    
	void LoadMaterial(string file);
    
	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;
};
#include "stdafx.h"
#include "Main.h"

Main::Main()
{
}

Main::~Main()
{
}

void Main::LoadMaterial(string file)
{
	ifstream fin;

	fin.open("../Assets/" + file);
	if (fin.is_open())
}

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("Load", "Load Obj",
		".obj", "../Assets"))
	{
		string path = ImGuiFileDialog::Instance()->GetCurrentPath();
		Util::Replace(&path, "\\", "/");
		if (path.find("/Assets/") != -1)
		{
			size_t tok = path.find("/Assets/") + 8;
			path = path.substr(tok, path.length())
				+ "/" + ImGuiFileDialog::Instance()->GetCurrentFileName();
		}
		else
		{
			path = ImGuiFileDialog::Instance()->GetCurrentFileName();
		}
		ifstream fin;

		fin.open("../Assets/" + path);
		if (fin.is_open())
		{
			int GCount = 0;
			vector<VertexPTN> Vertices;
			vector<UINT> Indices;
			vector<Vector3> P;
			vector<Vector2> T;
			vector<Vector3> N;
			string name;
			while (!fin.eof())
			{
				string tag;
				fin >> tag;
				if (tag == "mtllib")
				{
					string file;
					fin >> file;
					LoadMaterial(file.substr(2, file.size()));
				}
				if (tag == "v")
				{
					Vector3 pos;
					fin >> pos.x >> pos.y >> pos.z;
					P.push_back(pos);
				}
				else if (tag == "vt")
				{
					Vector2 uv;
					float temp;
					fin >> uv.x >> uv.y >> temp;
					T.push_back(uv);
				}
				else if (tag == "vn")
				{
					Vector3 nor;
					fin >> nor.x >> nor.y >> nor.z;
					N.push_back(nor);
				}
				else if (tag == "f")
				{
					int idx[3]; char slash;
					VertexPTN ptn;
					for (int i = 0; i < 3; i++)
					{
						fin >> idx[0] >> slash >> idx[1] >> slash >> idx[2];
						ptn.position = P[idx[0] - 1];
						ptn.uv = T[idx[1] - 1];
						ptn.normal = N[idx[2] - 1];
						Indices.push_back(Vertices.size());
						Vertices.push_back(ptn);
					}
				}
				else if (tag == "g")
				{
					GCount++;

					if (GCount == 2)
					{
						fin >> name;
					}

					if (GCount  == 3)
					{
						GameObject* child = GameObject::Create(name);
						temp->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();
						child->texture = RESOURCE->textures.Load("window.png");

						GCount = 1;
						Vertices.clear();
						Indices.clear();
					}
				}
				char c[128];
				fin.getline(c, 128);
			}
		}
	}

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

헤더와 cpp 파일입니다.  mtl 파일을 열어보면 여러 가지 값들과 어떤 파일을 읽거나 불러오는지 적혀있고 값 앞에 어떤 텍스트가 적혀 있느냐에 따라 어떤 방식으로 불러오고 크기부터 위치까지 전부 알 수 있습니다.

일부 잘라와서 모든 내용은 다 적혀있지 않지만 dds 파일부터 3개씩 단위로 묶는 것을 볼 수 있습니다.

 

읽어주셔서 감사합니다.

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

230613 강의  (0) 2023.06.13
230612 강의  (0) 2023.06.12
230608 강의  (0) 2023.06.08
230607 강의  (0) 2023.06.07
회전보간  (0) 2023.06.05