응애맘마조

230710 강의 본문

공부/3D강의

230710 강의

TH.Wert 2023. 7. 10. 18:20

이전에 나타냈던 맵에서 노멀을 표시하는 것을 강의했습니다. 동시에 과제였습니다.

#pragma once

class Main : public Scene
{
private:
	Camera* Cam;
	Actor* Grid;

	Actor* Terrain;

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;
};
#include "stdafx.h"
#include "Main.h"

Main::Main()
{
}

Main::~Main()
{
}

void Main::Init()
{
    Cam = Camera::Create();
    Cam->LoadFile("Cam.xml");
    Camera::main = Cam;

    Grid = Actor::Create();
    Grid->LoadFile("Grid.xml");
	Cam->width = App.GetWidth();
	Cam->height = App.GetHeight();
	Cam->viewport.width = App.GetWidth();
	Cam->viewport.height = App.GetHeight();

	Terrain = Actor::Create();
	Terrain->material = new Material();
	Terrain->material->diffuseMap = RESOURCE->textures.Load("terrain.jpg");
	Terrain->material->diffuse.w = 1.0f;
	Terrain->shader = RESOURCE->shaders.Load("3.Cube.hlsl");
	
	VertexPTN* vertex = new VertexPTN[257 * 257];
	int vertexCount = 257 * 257;
	//z
	for (int i = 0; i < 257; i++)
	{
		//x
		for (int j = 0; j < 257; j++)
		{
			vertex[i * 257 + j].position.x = -128.5 + j;
			vertex[i * 257 + j].position.y = 0;
			vertex[i * 257 + j].position.z = 128.5 + -i;

			vertex[i * 257 + j].normal = Vector3(0, 0, 0);
			vertex[i * 257 + j].uv.x = j / 256.0f;
			vertex[i * 257 + j].uv.y = i / 256.0f;
		}
	}

	//y
	BinaryReader fin;
	fin.Open(L"../Assets/terrain.raw");

	for (int i = 0; i < 257 * 257; i++)
	{
		vertex[i].position.y = fin.Byte();
	}
	fin.Close();

	UINT* indices = new UINT[256 * 256 * 6];
	int indexCount = 256 * 256 * 6;
	
	for (int i = 0; i < 256; i++)
	{
		//x
		for (int j = 0; j < 256; j++)
		{

			Vector3 a = vertex[258 + j + (i * 257)].position - vertex[0 + j + (i * 257)].position;
			Vector3 b = vertex[1 + j + (i * 257)].position - vertex[0 + j + (i * 257)].position;
			Vector3 cross = a.Cross(b);

			//3개씩
			indices[(i * 256 + j) * 6 + 0] = 0 + j + (i * 257);
			indices[(i * 256 + j) * 6 + 1] = 1 + j + (i * 257);
			indices[(i * 256 + j) * 6 + 2] = 258 + j + (i * 257);

			vertex[0 + j + (i * 257)].normal += cross;
			vertex[1 + j + (i * 257)].normal += cross;
			vertex[258 + j + (i * 257)].normal += cross;

			a = vertex[257 + j + (i * 257)].position - vertex[0 + j + (i * 257)].position;
			b = vertex[258 + j + (i * 257)].position - vertex[0 + j + (i * 257)].position;
			cross = a.Cross(b);

			//3개씩
			indices[(i * 256 + j) * 6 + 3] = 0 + j + (i * 257);
			indices[(i * 256 + j) * 6 + 4] = 258 + j + (i * 257);
			indices[(i * 256 + j) * 6 + 5] = 257 + j + (i * 257);

			vertex[0 + j + (i * 257)].normal += cross;
			vertex[257 + j + (i * 257)].normal += cross;
			vertex[258 + j + (i * 257)].normal += cross;
		}
	}
	Terrain->mesh = make_shared<Mesh>(vertex, vertexCount, indices,
    indexCount, VertexType::PTN);
}

void Main::Release()
{
}

void Main::Update()
{
	Camera::ControlMainCam();

	ImGui::Text("FPS: %d", TIMER->GetFramePerSecond());
	ImGui::Begin("Hierarchy");
	Cam->RenderHierarchy();
	Grid->RenderHierarchy();
	Terrain->RenderHierarchy();
	ImGui::End();
	Cam->Update();
	Grid->Update();
	Terrain->Update();
}

void Main::LateUpdate()
{
}

void Main::PreRender()
{
}

void Main::Render()
{
	Cam->Set();
	Grid->Render();
	Terrain->Render();
}

void Main::ResizeScreen()
{
	Cam->width = App.GetWidth();
	Cam->height = App.GetHeight();
	Cam->viewport.width = App.GetWidth();
	Cam->viewport.height = App.GetHeight();
}

int WINAPI wWinMain(HINSTANCE instance, HINSTANCE prevInstance, LPWSTR param, int command)
{
	App.SetAppName(L"Game1");
	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;
}

Main의 헤더와 cpp 파일입니다. 전에 잡았던 맵에서 삼각형으로 외적 해서 나타내는 방식입니다.

빛의 반사와 입체감이 느껴지는 노멀이 추가된 영상

읽어주셔서 감사합니다.

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

230712 강의  (0) 2023.07.12
230711 강의  (0) 2023.07.11
230707 강의  (0) 2023.07.07
230706 강의  (0) 2023.07.06
230705 강의  (0) 2023.07.05
Comments