Geometry
Geometry
- 정점/인덱스를 관리하는 템플릿 컨테이너
- "정점 배열 + 인덱스 배열"을 들고 있는 CPU 쪽 지오메트리 컨테이너
#pragma once
template <typename T>
class Geometry
{
public:
Geometry() {}
~Geometry() {}
uint32 GetVertexCount() { return static_cast<uint32>(_vertices.size()); }
void* GetVertexData() { return _vertices.date(); }
const vector<T>& GetVertices() { return _vertices; }
uint32 GetIndexCount() { return static_cast<uint32>(_indices.size()); }
void* GetIndexData() { return _indices.data(); }
const vector<uint32>& GetIndices() { return _indices; }
void AddVertex(const T& vertex) { _vertices.push_back(vertex); }
void AddVertices(const vector<T>& vertices) {_vertices.insert(_vertices.end(), vertices.begin(), vertices.end());}
void SetVertices(const vector<T>& vertices) { _vertices = vertices; }
void AddIndex(uint32 index) { _indices.push_back(index); }
void AddIndices(const vector<uint32>& indices) { _indices.insert(_indices.end(), indices.begin(), indices.end()); }
void SetIndices(const vector<uint32>& indices) { _indices = indices; }
private:
vector<T> _vertices;
vector<uint32> _indices;
};
Geometry<T>는 정점 타입에 상관없이 재사용할 수 있도록 템플릿으로 설계
GeometryHelpers
- 사각형 지오메트리 생성기
- 자주 쓰는 도형을 간단한 함수 호출로 만들어주는 헬퍼
#include "pch.h"
#include "GeometryHelper.h"
void GeometryHelper::CreateRectangle(shared_ptr<Geometry<VertexTextureData>> geometry)
{
vector<VertexTextureData> vertices;
vertices.resize(4);
vertices[0].position = Vec3(-0.5f, -0.5f, 0.f);
vertices[0].uv = Vec2(0.f, 1.f);
vertices[1].position = Vec3(-0.5f, 0.5f, 0.f);
vertices[1].uv = Vec2(0.f, 0.f);
vertices[2].position = Vec3(0.5f, -0.5f, 0.f);
vertices[2].uv = Vec2(1.f, 1.f);
vertices[3].position = Vec3(0.5f, 0.5f, 0.f);
vertices[3].uv = Vec2(1.f, 0.f);
geometry->SetVertices(vertices);
vector<uint32> indices = { 0, 1, 2, 2, 1, 3 };
geometry->SetIndices(indices);
}
void GeometryHelper::CreateRectangle(shared_ptr<Geometry<VertexColorData>> geometry, Color color)
{
vector<VertexColorData> vertices;
vertices.resize(4);
vertices[0].position = Vec3(-0.5f, -0.5f, 0.f);
vertices[0].color = color;
vertices[1].position = Vec3(-0.5f, 0.5f, 0.f);
vertices[1].color = color;
vertices[2].position = Vec3(0.5f, -0.5f, 0.f);
vertices[2].color = color;
vertices[3].position = Vec3(0.5f, 0.5f, 0.f);
vertices[3].color = color;
geometry->SetVertices(vertices);
vector<uint32> indices = { 0, 1, 2, 2, 1, 3 };
geometry->SetIndices(indices);
}
VertexData
#pragma once
struct VertexTextureData
{
Vec3 position = { 0, 0, 0 };
Vec2 uv = { 0, 0 };
static vector<D3D11_INPUT_ELEMENT_DESC> descs;
};
struct VertexColorData
{
Vec3 position = { 0, 0, 0 };
Color color = { 0, 0, 0, 0 };
static vector<D3D11_INPUT_ELEMENT_DESC> descs;
};
#include "pch.h"
#include "VertexData.h"
vector<D3D11_INPUT_ELEMENT_DESC> VertexTextureData::descs =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
vector<D3D11_INPUT_ELEMENT_DESC> VertexColorData::descs =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};