DirectX ๊ธฐ๋ฐ ๊ฒ์ ์์ง ์ค๊ณ์์์ GameObject ํด๋์ค ์ค๊ณ ๋ฐ ๊ตฌํ ๋ฐฉ๋ฒ
GameObject ํด๋์ค๋ก ์ถ์ํํ๊ณ , ๊ฐ ์ค๋ธ์ ํธ๊ฐ ๋
๋ฆฝ์ ์ธ ๋ ๋๋ง ์์๊ณผ ์ํ๋ฅผ ๊ฐ์ง๋๋ก ๊ตฌ์ฑํ๋ค.Game ํด๋์ค์์ ์ฒ๋ฆฌํ๋ ๋ ๋๋ง ๋ก์ง์ ์ค๋ธ์ ํธ๋ณ๋ก ๋ถ๋ฆฌํ์ฌ ์ ์ง๋ณด์์ฑ๊ณผ ํ์ฅ์ฑ์ ํ๋ณดํ๋ค.VertexShader, PixelShader, Buffer, Texture, TransformData ๋ฑ์ ๊ฐ๋ณ ์ค๋ธ์ ํธ ๋จ์๋ก ์ฒ๋ฆฌํ๋๋ก ๊ตฌ์กฐํํ๋ค.| ๊ฐ๋ | ์ค๋ช |
|---|---|
| GameObject | ๊ฒ์ ๋ด์ ํ๋์ ๋ ๋๋ง ๊ฐ๋ฅํ ๊ฐ์ฒด. ์์ฒด ์ ฐ์ด๋, ๋ฒํผ, ํ ์ค์ฒ, ์๋ ๋ณํ ์ ๋ณด๋ฅผ ๊ฐ์ง. |
| ๋ ๋๋ง ํ์ดํ๋ผ์ธ | GPU๊ฐ ํ๋ฉด์ ์ด๋ฏธ์ง๋ฅผ ์ถ๋ ฅํ๊ธฐ ์ํด ์ฒ๋ฆฌํ๋ ์ผ๋ จ์ ๋จ๊ณ. |
| Device/DeviceContext | GPU ๋ฆฌ์์ค๋ฅผ ์์ฑํ๊ณ , ๊ทธ์ ๋ํ ๋ช ๋ น์ ๋ณด๋ด๋ DirectX11 ๊ฐ์ฒด. |
| TransformData | ์ค๋ธ์ ํธ์ ์์น, ํ์ , ํฌ๊ธฐ๋ฅผ ๋ด์ ๊ตฌ์กฐ์ฒด๋ก, ์ ฐ์ด๋์ ์ ๋ฌ๋จ. |
| ConstantBuffer | CPU์์ GPU๋ก ๋ฐ์ดํฐ๋ฅผ ๋ณด๋ด๊ธฐ ์ํ ์ ์ฉ ๋ฒํผ. |
| PipelineInfo | ๋ ๋๋ง ํ์ดํ๋ผ์ธ์ ์ค์ ๋ฌถ์. ์ ฐ์ด๋, ์ํ ๊ฐ์ฒด ๋ฑ์ ํฌํจ. |
| Geometry | ์ ์ ๋ฐ ์ธ๋ฑ์ค๋ฅผ ๋ณด๊ดํ๋ ๋ํ ์ ๋ณด ํด๋์ค. |
| ์ฉ์ด | ์ค๋ช |
|---|---|
shared_ptr<T> | C++ ์ค๋งํธ ํฌ์ธํฐ. ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฐธ์กฐ ์นด์ดํธ ๊ธฐ๋ฐ์ผ๋ก ์์ ํ๊ฒ ๊ด๋ฆฌ. |
ComPtr<T> | COM ๊ฐ์ฒด๋ฅผ ์ํ ์ค๋งํธ ํฌ์ธํฐ. DirectX์์ ์์ฃผ ์ฌ์ฉ๋จ. |
VertexBuffer, IndexBuffer | ์ ์ ๋ฐ ์ ์ ์ ๊ทธ๋ฆด ์์๋ฅผ GPU์ ๋๊ธฐ๊ธฐ ์ํ ๋ฒํผ. |
InputLayout | ์ ์ ๋ฒํผ์ ์ ฐ์ด๋ ์ฌ์ด์ ์ฐ๊ฒฐ ๋ฐฉ์ ์ ์. |
SamplerState, BlendState, RasterizerState | ๋ ๋๋ง์ ์ํฅ์ ์ฃผ๋ ์ํ ์ค์ ๊ฐ์ฒด. |
Texture | ํ ์ค์ฒ ์ด๋ฏธ์ง ๋ฐ์ดํฐ๋ฅผ GPU์ ์ ๋ก๋ํด ์ฌ์ฉํ ์ ์๋๋ก ํ๋ ํด๋์ค. |
class GameObject
{
public:
GameObject(ComPtr<ID3D11Device> device, ComPtr<ID3D11DeviceContext> deviceContext);
~GameObject();
void Update();
void Render(shared_ptr<Pipeline> pipeline);
private:
// DirectX ์ฅ์น ํธ๋ค
ComPtr<ID3D11Device> _device;
// ๊ธฐํ ๊ตฌ์กฐ ๋ฐ ๋ฒํผ
shared_ptr<Geometry<VertexTextureData>> _geometry;
shared_ptr<VertexBuffer> _vertexBuffer;
shared_ptr<IndexBuffer> _indexBuffer;
shared_ptr<InputLayout> _inputLayout;
// ์
ฐ์ด๋
shared_ptr<VertexShader> _vertexShader;
shared_ptr<PixelShader> _pixelShader;
// ๋ ๋๋ง ์ํ
shared_ptr<RasterizerState> _rasterizerState;
shared_ptr<BlendState> _blendState;
shared_ptr<SamplerState> _samplerState;
// ํ
์ค์ฒ
shared_ptr<Texture> _texture1;
// ํธ๋์คํผ ์ ๋ณด
TransformData _transformData;
shared_ptr<ConstantBuffer<TransformData>> _constantBuffer;
// ๋ก์ปฌ ์ขํ ์ ๋ณด
Vec3 _localPosition = { 0.f, 0.f, 0.f };
Vec3 _localRotation = { 0.f, 0.f, 0.f };
Vec3 _localScale = { 1.f, 1.f, 1.f };
};
GameObject::GameObject(ComPtr<ID3D11Device> device, ComPtr<ID3D11DeviceContext> deviceContext)
: _device(device)
{
// 1. Geometry ์ค์
_geometry = make_shared<Geometry<VertexTextureData>>();
GeometryHelper::CreateRectangle(_geometry); // ์ ์ ๊ณผ ์ธ๋ฑ์ค ์๋ ์์ฑ
// 2. Vertex/Index Buffer ์์ฑ
_vertexBuffer = make_shared<VertexBuffer>(device);
_vertexBuffer->Create(_geometry->GetVertices());
_indexBuffer = make_shared<IndexBuffer>(device);
_indexBuffer->Create(_geometry->GetIndices());
// 3. ์
ฐ์ด๋ ์์ฑ ๋ฐ ์ปดํ์ผ
_vertexShader = make_shared<VertexShader>(device);
_vertexShader->Create(L"Default.hlsl", "VS", "vs_5_0");
_inputLayout = make_shared<InputLayout>(device);
_inputLayout->Create(VertexTextureData::descs, _vertexShader->GetBlob());
_pixelShader = make_shared<PixelShader>(device);
_pixelShader->Create(L"Default.hlsl", "PS", "ps_5_0");
// 4. ์ํ ๊ฐ์ฒด๋ค ์์ฑ
_rasterizerState = make_shared<RasterizerState>(device);
_rasterizerState->Create();
_blendState = make_shared<BlendState>(device);
_blendState->Create();
_samplerState = make_shared<SamplerState>(device);
_samplerState->Create();
// 5. ์์ ๋ฒํผ ์์ฑ
_constantBuffer = make_shared<ConstantBuffer<TransformData>>(device, deviceContext);
_constantBuffer->Create();
// 6. ํ
์ค์ฒ ์์ฑ
_texture1 = make_shared<Texture>(device);
_texture1->Create(L"cat.png");
}
โ ์์ฑ์๋ ์ค๋ธ์ ํธ๊ฐ ๋ ๋๋ง์ ํ์ํ ์์๊ณผ ์ํ๋ฅผ ๋ชจ๋ ์์ฑํ๋ฉฐ, ์ดํ Update/Render์์ ์ด๋ฅผ ์ฌ์ฉํจ.
void GameObject::Update()
{
_localPosition.x += 0.001f; // ์ง์์ ์ธ ์ด๋
Matrix matScale = Matrix::CreateScale(_localScale / 3); // ํฌ๊ธฐ
Matrix matRotation = Matrix::CreateRotationX(_localRotation.x);
matRotation *= Matrix::CreateRotationY(_localRotation.y);
matRotation *= Matrix::CreateRotationZ(_localRotation.z); // ํ์
Matrix matTranslation = Matrix::CreateTranslation(_localPosition); // ์์น
Matrix matWorld = matScale * matRotation * matTranslation; // SRT ์์๋ก ์๋ ํ๋ ฌ ๊ตฌ์ฑ
_transformData.matWorld = matWorld;
// GPU์ ์ ์ก
_constantBuffer->CopyData(_transformData);
}
โ ์ด ํจ์๋ ๋งค ํ๋ ์ ํธ์ถ๋๋ฉฐ, ์ค๋ธ์ ํธ์ ์์น๋ฅผ ๋ณ๊ฒฝํ๊ณ GPU์ ๋ณํ ํ๋ ฌ์ ์ ๋ฌํฉ๋๋ค.
void GameObject::Render(shared_ptr<Pipeline> pipeline)
{
PipelineInfo info;
info.inputLayout = _inputLayout;
info.vertexShader = _vertexShader;
info.pixelShader = _pixelShader;
info.rasterizerState = _rasterizerState;
info.blendState = _blendState;
pipeline->UpdatePipeline(info); // ํ์ดํ๋ผ์ธ ์ํ ์
๋ฐ์ดํธ
pipeline->SetVertexBuffer(_vertexBuffer);
pipeline->SetIndexBuffer(_indexBuffer);
pipeline->SetConstantBuffer(0, SS_VertexShader, _constantBuffer);
pipeline->SetTexture(0, SS_PixelShader, _texture1);
pipeline->SetSamplerState(0, SS_PixelShader, _samplerState);
pipeline->DrawIndexed(_geometry->GetIndexCount(), 0, 0);
}
โ ๋ ๋๋ง ๋ช ๋ น์ ์ค๋ธ์ ํธ๊ฐ ์ค์ค๋ก ๋ด๋ฆฌ๋ฉฐ, Pipeline ๊ฐ์ฒด๋ฅผ ํตํด GPU์ ์ค์ ๋ฐ ์ ๋ฌ๋ฉ๋๋ค.
| ํญ๋ชฉ | ์ค๋ช |
|---|---|
| GameObject ํด๋์ค ๋ชฉ์ | ์ค๋ธ์ ํธ ๋จ์ ๋ ๋๋ง ์์ ๋ฐ ์ํ๋ฅผ ์บก์ํ |
| ์์ฑ์ ์ญํ | ์ ์ , ์ ฐ์ด๋, ์ํ, ํ ์ค์ฒ ๋ฑ์ ์ด๊ธฐํ |
| Update | ์๋ ํ๋ ฌ์ ๊ตฌ์ฑํ๊ณ ConstantBuffer๋ฅผ ํตํด GPU๋ก ์ ์ก |
| Render | Pipeline์ ๋ ๋๋ง ์์ ์ค์ ํ Draw ๋ช ๋ น ์ํ |
| ์ค๊ณ ์ฅ์ | ์ ์ง๋ณด์ ์ฉ์ด, ํ์ฅ์ฑ ํ๋ณด, ๋ฆฌ์์ค ๋ถ๋ฆฌ ๋ฐ ๊ด๋ฆฌ ๊ฐ๋ฅ |
VertexColorData ๊ธฐ๋ฐ์ผ๋ก ๋ฐ๋ ์ ์ฒด ๊ตฌ์กฐ๋ ๋ค์๊ณผ ๊ฐ์ด ๋ฐ์ดํฐ โ ์ ฐ์ด๋ โ ๋ ๋๋ง ํธ์ถ ํ๋ฆ์ผ๋ก ์ฐ๊ฒฐ๋ฉ๋๋ค:
GeometryHelper::CreateRectangle()๋ฅผ ํตํด ์์ ํฌํจ ์ ์ ์์ฑPipelineInfo๋ก ํ์ดํ๋ผ์ธ ์ค์ [GameObject]
โโโ Geometry<VertexColorData>
โโโ ์ ์ (Vertex): ์์น + ์์
โโโ ์ธ๋ฑ์ค(Index): ์ ์ ์์
โโโ VertexBuffer/IndexBuffer
โโโ GPU ์ ์ก (Create ํธ์ถ)
โโโ VertexShader("Color.hlsl")
โโโ VS_INPUT: position, color
โโโ VS_OUTPUT: color ์ ๋ฌ
โโโ PixelShader("Color.hlsl")
โโโ color ๊ฐ ๊ทธ๋๋ก ํฝ์
์ ์ถ๋ ฅ
โโโ InputLayout: VertexColorData::descs
โโโ ConstantBuffer<TransformData>
โโโ ์๋ ํ๋ ฌ (Update โ GPU ์
๋ก๋)
โโโ Pipeline
โโโ ํ์ดํ๋ผ์ธ ์ค์ ํ DrawIndexed ํธ์ถ
| ๊ตฌ๋ถ | VertexTextureData | VertexColorData |
|---|---|---|
| ์ ์ ๊ตฌ์ฑ | ์์น + ํ ์ค์ฒ ์ขํ | ์์น + ์์ |
| ์ ฐ์ด๋ ์ ๋ ฅ | POSITION, TEXCOORD | POSITION, COLOR |
| ์ถ๋ ฅ ๋ฐฉ์ | ํ ์ค์ฒ ์์ | ์ ์ ์์ ์ง์ ์ถ๋ ฅ |
| ์ ฐ์ด๋ ํ์ผ | Default.hlsl | Color.hlsl |
| ์๊ฐ์ ์ถ๋ ฅ | ์ด๋ฏธ์ง ๋ ๋๋ง | ๋จ์ ๋ํ (ex. ๋นจ๊ฐ ์ฌ๊ฐํ) |
| ํ์ฉ | ์ค์ ๊ฒ์ ๋ชจ๋ธ, ๋ฐฐ๊ฒฝ ๋ฑ | ๋๋ฒ๊น ์ฉ, UI ์์, ๋จ์ ํจ๊ณผ ๋ฑ |
InputLayout๊ณผ ์ ฐ์ด๋ ์ ๋ ฅ ๊ตฌ์กฐ ๋ถ์ผ์น
VertexColorData::descs๋ก InputLayout์ ๊ตฌ์ฑํ๋์ง ๊ผญ ํ์ธfloat4 color : COLOR;๋ก ์ผ์นํด์ผ ํจShader ํ์ผ์ด ๋ฐ๋์๋๋ฐ๋ ์ด์ ํ์ผ์ ์ฐธ์กฐํ๋ ๊ฒฝ์ฐ
"Default.hlsl"์ "Color.hlsl"๋ก ํ์คํ ๊ต์ฒดํ๋์ง ์ฒดํฌSamplerState, Texture๊ฐ ์๋ฏธ ์๋๋ฐ๋ ๋จ์์๋ ๊ฒฝ์ฐ
| ์ํฉ | VertexColorData์ ์ฅ์ |
|---|---|
| ๋๋ฒ๊ทธ์ฉ ๋ฐ์ค ์ถ๋ ฅ | ์ถฉ๋ ํ์ ๋ฐ์ค๋ ์์ญ ํ์ ์ ์ ์ฉ |
| UI ์์ ๊ตฌ์ฑ | ๋ฒํผ, ๋ฐฐ๊ฒฝ ๋ฐ์ค ๋ฑ ํ ์ค์ฒ ์์ด ๋น ๋ฅด๊ฒ ๋ ๋๋ง ๊ฐ๋ฅ |
| ์ ฐ์ด๋ ํ ์คํธ | ์กฐ๋ช , ์์ ๋ธ๋ ๋ฉ ํ ์คํธ ์ ๋จ์ ์์ ์ฌ์ฉ์ด ํธํจ |