• Home
  • About
    • 게임 개발자 유정룡 photo

      게임 개발자 유정룡

      포트폴리오

    • Learn More
    • Email
    • Github
    • Bitbucket
  • Projects
    • All Projects
    • All Tags

DirectX11 공부 4일차(Wrapping Class - Geometry)

04 Jun 2021

Reading time ~2 minutes

Wrapping Class - Geometry

이젠 Geometry를 뤠핑 할 순서이다.

VertexColor와 VertexTexture 이 둘을 뤠핑 할거다.

Geometry.h

#pragma once
#include "stdafx.h"

template <class T>
class Geomatry
{
public:
	Geomatry() = default;
	virtual ~Geomatry();

	// ========================================================
	// [Vertex]
	// ========================================================
	const uint GetVertexCount() const { return static_cast<uint>(vertices.size()); }
	const uint GetVertexByteWidth() const { return GetVertexCount() * sizeof(T); }
	const T* GetVertexPointer() const { return vertices.data(); }
	const std::vector<T>& GetVertices() const { return vertices; }
	const std::vector<T> GetVertices(const uint& offset, const uint& count);

	void AddVertex(const T& vertex);
	void AddVerticse(const std::vector<T>& vertices);
	void SetVerticse(const std::vector<T>& vertices);
	// ========================================================
	// [Index]
	// ========================================================
	const uint GetIndexCount() const { return static_cast<uint>(indices.size()); }
	const uint GetIndexByteWidth() const { return GetIndexCount() * sizeof(uint); }
	const uint* GetIndexPointer() const { return indices.data(); }
	const std::vector<uint>& GetIndices() const { return indices; }
	const std::vector<uint> GetIndices(const uint& offset, const uint& count);

	void AddIndex(const uint& indices);
	void AddIndices(const std::vector<uint>& indices);
	void SetIndices(const std::vector<uint>& indices);

	// ========================================================
	// [MICS]
	// ========================================================
	void Clear();

protected:
	std::vector<T> vertices;
	std::vector<uint> indices;
};

#include "Geometry.inl"

저 둘중에 하나를 콕 집어서 뤠핑 할 수도 있지만, 템플릿을 사용해서 유동적으로 할 수 있게 하자.

뭐 안에 변수나 getter setter 정도는 다 이해 하겠지만 밑에 .inl은 처음보는 확장자이다.

.inl은 inline 확장자로 inline을 헤더 파일에 쓰면 너무 지저분해지기 때문에 따로 빼서 작성했다.

Geometry.inl

#include "Geometry.h"

template<class T>
inline Geomatry<T>::~Geomatry()
{
	Clear();
}

template<class T>
inline const std::vector<T> Geomatry<T>::GetVertices(const uint& offset, const uint& count)
{
	std::vector<T> sub_verticse;

	typename std::vector<T>::iterator first = vertices.begin() + offset;
	typename std::vector<T>::iterator last = first + count;

	sub_verticse.assign(first, last);

	return sub_verticse;
}

template<class T>
inline void Geomatry<T>::AddVertex(const T& vertex)
{
	vertices.emplace_back(vertex);
}

template<class T>
inline void Geomatry<T>::AddVerticse(const std::vector<T>& vertices)
{
	this->vertices.insert(this->vertices.end(), vertices.begin(), vertices.end());
}

template<class T>
inline void Geomatry<T>::SetVerticse(const std::vector<T>& vertices)
{
	this->vertices.clear();
	this->vertices.shrink_to_fit();
	this->vertices = vertices;
}

template<class T>
inline const std::vector<uint> Geomatry<T>::GetIndices(const uint& offset, const uint& count)
{
	std::vector<uint> sub_indices;

	std::vector<uint>::iterator first = vertices.begin() + offset;
	std::vector<uint>::iterator last = first + count;

	sub_indices.assign(first, last);

	return sub_indices;
}

template<class T>
inline void Geomatry<T>::AddIndex(const uint& index)
{
	indices.emplace_back(index);
}

template<class T>
inline void Geomatry<T>::AddIndices(const std::vector<uint>& indices)
{
	this->indices.insert(this->indices.end(), indices.begin(), indices.end());
}

template<class T>
inline void Geomatry<T>::SetIndices(const std::vector<uint>& indices)
{
	this->indices.clear();
	this->indices.shrink_to_fit();
	this->indices = indices;
}

template<class T>
inline void Geomatry<T>::Clear()
{
	vertices.clear();
	vertices.shrink_to_fit();

	indices.clear();
	indices.shrink_to_fit();
}

Execute.cpp

안에 작성 했고 마지막 벡터의 경우에는 clear이후에 chrink_to_fit을 사용해서 메모리까지 비워주자!

그리고 다시 Execute로 넘어가서 수정해주자

알아서 바꿔주면 되지만, 벡터 메모리 때문에 조금 찾아본 것이 있다.

// Vertex Data
{
	std::vector<VertexTexture> vertices(4);

	vertices[0].position = D3DXVECTOR3(-0.5f, -0.5f, 0.0f);
	vertices[0].uv = D3DXVECTOR2(0.0f, 1.0f);

	vertices[1].position = D3DXVECTOR3(-0.5f, 0.5f, 0.0f);
	vertices[1].uv = D3DXVECTOR2(0.0f, 0.0f);

	vertices[2].position = D3DXVECTOR3(0.5f, -0.5f, 0.0f);
	vertices[2].uv = D3DXVECTOR2(1.0f, 1.0f);

	vertices[3].position = D3DXVECTOR3(0.5f, 0.5f, 0.0f);
	vertices[3].uv = D3DXVECTOR2(1.0f, 0.0f);

	geometry.AddVerticse(vertices);
	vertices.clear();
	vertices.shrink_to_fit();
}

강의에서는 하나하나 넣어줬지만, 귀찮아서 있는걸로 사용했다.

그러다 vector를 사용해서 넣어줬을때, 넣었던 vector값을 변경해줘야 한다.

추가 하는것이기 때문에 메모리 주소까지 갖고 올수 없다. 그래서 꼭 비워주자

Check Start

vertices address
014365F8
0143660C
01436620
01436634

geometry adress
01436678
0143668C
014366A0
014366B4

비교해보면 서로 주소값이 다르다. 따라서 넣었던 vertices를 안비워주면 종료할때 비워지기 대문에 그 동안 메모리가 낭비된다.

꼭 비워주자



DirectX Share Tweet +1