DOTY

DirectX11. 4) Buffer, Shader II 본문

DirectX

DirectX11. 4) Buffer, Shader II

증식세포 2023. 6. 12. 16:58
728x90
반응형

1. ModelClass.h

#pragma once
#ifndef _MODELCLASS_H_
#define _MODELCLASS_H_

#include <d3d11.h>
#include <directxmath.h>

using namespace DirectX;

class ModelClass
{
private:
	struct VertexType
	{
		XMFLOAT3 position;
		XMFLOAT4 color;
	};

public:
	ModelClass();
	ModelClass(const ModelClass&);
	~ModelClass();

	bool Initialize(ID3D11Device*);
	void Shutdown();
	void Render(ID3D11DeviceContext*);

	int GetIndexCount();

private:
	bool InitializeBuffers(ID3D11Device*);
	void ShutdownBuffers();
	void RenderBuffers(ID3D11DeviceContext*);

private:
	ID3D11Buffer* m_vertexBuffer, * m_indexBuffer;
	int m_vertexCount, m_indexCount;
};

#endif

3D 모델의 캡슐화를 담당하는 클래스. 모델에 대한 데이터들(위치, 색)을 설정하고, 렌더링 할 수 있도록 정점과 인덱스 버퍼를 만듦.

 

1-1. Initialize

InitializeBuffers를 호출하여 정점과 인덱스 버퍼를 초기화한다.

 

1-2. Shutdown

ShutdownBuffers를 호출하여 정점과 인덱스 버퍼를 해제한다.

 

1-3. Render

RenderBuffers를 호출하여 색 셰이더가 렌더링 될 수 있도록 설정한다.

 

1-4. InitializeBuffers

VertexType 구조체의 Vertex 배열과 unsigned long 타입의 indices 배열을 선언하고, m_vertexCount와 m_indexCount로 정점과 인덱스의 갯수를 정해준다. Vertices와 indeces배열에 데이터들을 저장하고 여러 속성들을 정한 후에 CreateBuffer를 호출하여 렌더링 하는 함수이다.

 

1-5. ShutdownBuffers

InitializeBuffers에서 선언했던 정점과 인덱스 버퍼를 해제한다.

 

1-6. RenderBuffers

정점 버퍼와 인덱스 버퍼를 활성하여 렌더링을 한다. 삼각형, 선 등을 그릴 수 있으며 이를 GPU에 지시하는 역할을 한다.

D3D11_PRIMITIVE_TOPOLOGY_POINTLIST // 독립적인 정점을 찍는다.
D3D11_PRIMITIVE_TOPOLOGY_LINELIST // 두개의 정점이 하나의 선을 만든다. (끊어진 선)
D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP // 두개의 정점이 하나의 선을 만들고 이 선들이 이어진다.
D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP // 두번째, 세번째 정점이 다음 정점과 이어져 붙여진 삼각형이 그려진다.

※ Vertex : 모델을 그리기 위한 그래픽 모델의 한 점

※ Index : Vertex의 집합에서 특정 정점을 식별하는데 쓰이는 값

 

2. CameraClass.h

#pragma once
#ifndef _CAMERACLASS_H_
#define _CAMERACLASS_H_

#include <directxmath.h>

using namespace DirectX;

class CameraClass
{
public:
	CameraClass();
	CameraClass(const CameraClass&);
	~CameraClass();

	void SetPosition(float, float, float);
	void SetRotation(float, float, float);

	XMFLOAT3 GetPosition();
	XMFLOAT3 GetRotation();

	void Render();
	void GetViewMatrix(XMMATRIX&);

private:
	float m_positionX, m_positionY, m_positionZ;
	float m_rotationX, m_rotationY, m_rotationZ;
	XMMATRIX m_viewMatrix;
};

#endif

렌더링 한 픽셀들을 보기 위해서는 카메라 클래스가 필요하다. 카메라의 위치와 회전을 추적하기 위한 View Matrix를 생성.

 

2-1. SetPosition / SetRotation

카메라의 위치와 회전을 설정한다.

 

2-2. GetPosition / GetRotation

카메라의 위치와 회전을 가져온다. 

 

2-3. Render

카메라의 위치와 회전을 사용하여 View Matrix를 생성한다.

카메라 좌표계는 오브젝트와 달리 바라보는 방향을 z축으로 설정한다. 또한 카메라의 윗 방향을 y축으로 설정한다.

이를 적용하기 위해 회전 각을 라디안으로 적용하여 카메라의 pitch, yaw, roll을 정한다.

// Set the yaw (Y axis), pitch (X axis), and roll (Z axis) rotations in radians.
pitch = m_rotationX * 0.0174532925f;
yaw   = m_rotationY * 0.0174532925f;
roll  = m_rotationZ * 0.0174532925f;

이를 활용하여 Rotation Matrix를 만들고 up, position, LookAt 3개의 벡터를 통해 m_viewMatrix를 만든다. 

 

2-4. GetViewMatirx

ViewMatrix를 가져온다. 

 

3. Application -> Render

bool ApplicationClass::Render()
{
	XMMATRIX worldMatrix, viewMatrix, projectionMatrix;
	bool result;


	// Clear the buffers to begin the scene.
	m_Direct3D->BeginScene(0.0f, 0.0f, 0.0f, 1.0f);

	// Generate the view matrix based on the camera's position.
	m_Camera->Render();

	// Get the world, view, and projection matrices from the camera and d3d objects.
	m_Direct3D->GetWorldMatrix(worldMatrix);
	m_Camera->GetViewMatrix(viewMatrix);
	m_Direct3D->GetProjectionMatrix(projectionMatrix);

	// Put the model vertex and index buffers on the graphics pipeline to prepare them for drawing.
	m_Model->Render(m_Direct3D->GetDeviceContext());

	// Render the model using the color shader.
	result = m_ColorShader->Render(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix);
	if (!result)
	{
		return false;
	}

	// Present the rendered scene to the screen.
	m_Direct3D->EndScene();

	return true;
}

장면을 지우는 것과 카메라를 먼저 렌더링하며 시작한다. 각 클래스에서 World, View, Projection Matrix를 가져오고 ModelClass에서 Render함수를 호출하여 렌더링 할 오브젝트를 그래픽 파이트라인에 올려두고 렌더링을 시작한다.

끝으로 EndScene()을 통해 화면에 오브젝트가 표시된다.

728x90
반응형

'DirectX' 카테고리의 다른 글

DirectX11. 6) Light  (0) 2023.06.14
DirectX11. 5) Texture  (0) 2023.06.13
DirectX11. 3) Buffer, Shader I  (0) 2023.06.11
DirectX11. 2) D3D11 Initialize  (1) 2023.06.09
DirectX11. 1) 창 띄우기  (0) 2023.06.07
Comments