๐Ÿงญ ์ฃผ์ œ

DirectX ๊ธฐ๋ฐ˜ ๊ฒŒ์ž„ ์—”์ง„ ์„ค๊ณ„์—์„œ์˜ GameObject ํด๋ž˜์Šค ์„ค๊ณ„ ๋ฐ ๊ตฌํ˜„ ๋ฐฉ๋ฒ•

  • ๊ฒŒ์ž„ ๋‚ด ๋ Œ๋”๋ง ๊ฐ€๋Šฅํ•œ ์˜ค๋ธŒ์ ํŠธ๋ฅผ GameObject ํด๋ž˜์Šค๋กœ ์ถ”์ƒํ™”ํ•˜๊ณ , ๊ฐ ์˜ค๋ธŒ์ ํŠธ๊ฐ€ ๋…๋ฆฝ์ ์ธ ๋ Œ๋”๋ง ์ž์›๊ณผ ์ƒํƒœ๋ฅผ ๊ฐ€์ง€๋„๋ก ๊ตฌ์„ฑํ•œ๋‹ค.
  • ๊ธฐ์กด Game ํด๋ž˜์Šค์—์„œ ์ฒ˜๋ฆฌํ•˜๋˜ ๋ Œ๋”๋ง ๋กœ์ง์„ ์˜ค๋ธŒ์ ํŠธ๋ณ„๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ ์œ ์ง€๋ณด์ˆ˜์„ฑ๊ณผ ํ™•์žฅ์„ฑ์„ ํ™•๋ณดํ•œ๋‹ค.
  • VertexShader, PixelShader, Buffer, Texture, TransformData ๋“ฑ์„ ๊ฐœ๋ณ„ ์˜ค๋ธŒ์ ํŠธ ๋‹จ์œ„๋กœ ์ฒ˜๋ฆฌํ•˜๋„๋ก ๊ตฌ์กฐํ™”ํ•œ๋‹ค.

๐Ÿ“š ๊ฐœ๋…

ํ•ต์‹ฌ ๊ฐœ๋… ์š”์•ฝ

๊ฐœ๋…์„ค๋ช…
GameObject๊ฒŒ์ž„ ๋‚ด์˜ ํ•˜๋‚˜์˜ ๋ Œ๋”๋ง ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด. ์ž์ฒด ์…ฐ์ด๋”, ๋ฒ„ํผ, ํ…์Šค์ฒ˜, ์›”๋“œ ๋ณ€ํ™˜ ์ •๋ณด๋ฅผ ๊ฐ€์ง.
๋ Œ๋”๋ง ํŒŒ์ดํ”„๋ผ์ธGPU๊ฐ€ ํ™”๋ฉด์— ์ด๋ฏธ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด ์ฒ˜๋ฆฌํ•˜๋Š” ์ผ๋ จ์˜ ๋‹จ๊ณ„.
Device/DeviceContextGPU ๋ฆฌ์†Œ์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ๊ทธ์— ๋Œ€ํ•œ ๋ช…๋ น์„ ๋ณด๋‚ด๋Š” DirectX11 ๊ฐ์ฒด.
TransformData์˜ค๋ธŒ์ ํŠธ์˜ ์œ„์น˜, ํšŒ์ „, ํฌ๊ธฐ๋ฅผ ๋‹ด์€ ๊ตฌ์กฐ์ฒด๋กœ, ์…ฐ์ด๋”์— ์ „๋‹ฌ๋จ.
ConstantBufferCPU์—์„œ GPU๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ด๊ธฐ ์œ„ํ•œ ์ „์šฉ ๋ฒ„ํผ.
PipelineInfo๋ Œ๋”๋ง ํŒŒ์ดํ”„๋ผ์ธ์˜ ์„ค์ • ๋ฌถ์Œ. ์…ฐ์ด๋”, ์ƒํƒœ ๊ฐ์ฒด ๋“ฑ์„ ํฌํ•จ.
Geometry์ •์  ๋ฐ ์ธ๋ฑ์Šค๋ฅผ ๋ณด๊ด€ํ•˜๋Š” ๋„ํ˜• ์ •๋ณด ํด๋ž˜์Šค.

๐Ÿงพ ์šฉ์–ด ์ •๋ฆฌ

์šฉ์–ด์„ค๋ช…
shared_ptr<T>C++ ์Šค๋งˆํŠธ ํฌ์ธํ„ฐ. ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ฐธ์กฐ ์นด์šดํŠธ ๊ธฐ๋ฐ˜์œผ๋กœ ์•ˆ์ „ํ•˜๊ฒŒ ๊ด€๋ฆฌ.
ComPtr<T>COM ๊ฐ์ฒด๋ฅผ ์œ„ํ•œ ์Šค๋งˆํŠธ ํฌ์ธํ„ฐ. DirectX์—์„œ ์ž์ฃผ ์‚ฌ์šฉ๋จ.
VertexBuffer, IndexBuffer์ •์  ๋ฐ ์ •์ ์˜ ๊ทธ๋ฆด ์ˆœ์„œ๋ฅผ GPU์— ๋„˜๊ธฐ๊ธฐ ์œ„ํ•œ ๋ฒ„ํผ.
InputLayout์ •์  ๋ฒ„ํผ์™€ ์…ฐ์ด๋” ์‚ฌ์ด์˜ ์—ฐ๊ฒฐ ๋ฐฉ์‹ ์ •์˜.
SamplerState, BlendState, RasterizerState๋ Œ๋”๋ง์— ์˜ํ–ฅ์„ ์ฃผ๋Š” ์ƒํƒœ ์„ค์ • ๊ฐ์ฒด.
Textureํ…์Šค์ฒ˜ ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ๋ฅผ GPU์— ์—…๋กœ๋“œํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ํด๋ž˜์Šค.

๐Ÿ“ฆ GameObject ํด๋ž˜์Šค ์„ค๊ณ„ (GameObject.h)

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.cpp)

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์—์„œ ์ด๋ฅผ ์‚ฌ์šฉํ•จ.


๐Ÿ”„ Update ํ•จ์ˆ˜: ๋ณ€ํ™˜ ํ–‰๋ ฌ ๊ตฌ์„ฑ ๋ฐ ์ „์†ก

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์— ๋ณ€ํ™˜ ํ–‰๋ ฌ์„ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.


๐ŸŽจ Render ํ•จ์ˆ˜: ํŒŒ์ดํ”„๋ผ์ธ ๊ตฌ์„ฑ ๋ฐ ๊ทธ๋ฆฌ๊ธฐ

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๋กœ ์ „์†ก
RenderPipeline์— ๋ Œ๋”๋ง ์ž์› ์„ค์ • ํ›„ Draw ๋ช…๋ น ์ˆ˜ํ–‰
์„ค๊ณ„ ์žฅ์ ์œ ์ง€๋ณด์ˆ˜ ์šฉ์ด, ํ™•์žฅ์„ฑ ํ™•๋ณด, ๋ฆฌ์†Œ์Šค ๋ถ„๋ฆฌ ๋ฐ ๊ด€๋ฆฌ ๊ฐ€๋Šฅ

๐Ÿ”ง ๊ตฌ์กฐ ํ๋ฆ„ ์š”์•ฝ

VertexColorData ๊ธฐ๋ฐ˜์œผ๋กœ ๋ฐ”๋€ ์ „์ฒด ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐ์ดํ„ฐ โ†’ ์…ฐ์ด๋” โ†’ ๋ Œ๋”๋ง ํ˜ธ์ถœ ํ๋ฆ„์œผ๋กœ ์—ฐ๊ฒฐ๋ฉ๋‹ˆ๋‹ค:

  1. VertexColorData ๊ตฌ์กฐ์ฒด ์„ ์–ธ
  2. GeometryHelper::CreateRectangle()๋ฅผ ํ†ตํ•ด ์ƒ‰์ƒ ํฌํ•จ ์ •์  ์ƒ์„ฑ
  3. ์ •์ /์ธ๋ฑ์Šค ๋ฒ„ํผ ์ƒ์„ฑ ๋ฐ ์—…๋กœ๋“œ
  4. ์…ฐ์ด๋” ํŒŒ์ผ(Color.hlsl) ์ƒ์„ฑ ๋ฐ ์ปดํŒŒ์ผ
  5. InputLayout ์—ฐ๊ฒฐ (VertexColorData์— ๋งž์ถฐ ์ˆ˜์ •)
  6. Render์—์„œ PipelineInfo๋กœ ํŒŒ์ดํ”„๋ผ์ธ ์„ค์ •
  7. GPU์— ๋ช…๋ น ์ „๋‹ฌ โ†’ ์ƒ‰์ƒ ๊ธฐ๋ฐ˜ ๋„ํ˜• ์ถœ๋ ฅ

๐Ÿงฑ ๊ตฌ์กฐ ์‹œ๊ฐํ™” (ํ…์ŠคํŠธ ํ”Œ๋กœ์šฐ)

[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 ํ˜ธ์ถœ

๐Ÿ” ์ด์ „ ๋ฐฉ์‹๊ณผ์˜ ๋น„๊ต ์š”์•ฝ

๊ตฌ๋ถ„VertexTextureDataVertexColorData
์ •์  ๊ตฌ์„ฑ์œ„์น˜ + ํ…์Šค์ฒ˜ ์ขŒํ‘œ์œ„์น˜ + ์ƒ‰์ƒ
์…ฐ์ด๋” ์ž…๋ ฅPOSITION, TEXCOORDPOSITION, COLOR
์ถœ๋ ฅ ๋ฐฉ์‹ํ…์Šค์ฒ˜ ์ƒ‰์ƒ์ •์  ์ƒ‰์ƒ ์ง์ ‘ ์ถœ๋ ฅ
์…ฐ์ด๋” ํŒŒ์ผDefault.hlslColor.hlsl
์‹œ๊ฐ์  ์ถœ๋ ฅ์ด๋ฏธ์ง€ ๋ Œ๋”๋ง๋‹จ์ƒ‰ ๋„ํ˜• (ex. ๋นจ๊ฐ• ์‚ฌ๊ฐํ˜•)
ํ™œ์šฉ์‹ค์ œ ๊ฒŒ์ž„ ๋ชจ๋ธ, ๋ฐฐ๊ฒฝ ๋“ฑ๋””๋ฒ„๊น…์šฉ, UI ์š”์†Œ, ๋‹จ์ˆœ ํšจ๊ณผ ๋“ฑ

๐Ÿง  ํ•™์Šต์ž๊ฐ€ ์ž์ฃผ ์‹ค์ˆ˜ํ•˜๋Š” ํฌ์ธํŠธ

  1. InputLayout๊ณผ ์…ฐ์ด๋” ์ž…๋ ฅ ๊ตฌ์กฐ ๋ถˆ์ผ์น˜

    • VertexColorData::descs๋กœ InputLayout์„ ๊ตฌ์„ฑํ–ˆ๋Š”์ง€ ๊ผญ ํ™•์ธ
    • ์…ฐ์ด๋” ๋‚ด VS_INPUT๋„ float4 color : COLOR;๋กœ ์ผ์น˜ํ•ด์•ผ ํ•จ
  2. Shader ํŒŒ์ผ์ด ๋ฐ”๋€Œ์—ˆ๋Š”๋ฐ๋„ ์ด์ „ ํŒŒ์ผ์„ ์ฐธ์กฐํ•˜๋Š” ๊ฒฝ์šฐ

    • "Default.hlsl"์„ "Color.hlsl"๋กœ ํ™•์‹คํžˆ ๊ต์ฒดํ–ˆ๋Š”์ง€ ์ฒดํฌ
  3. SamplerState, Texture๊ฐ€ ์˜๋ฏธ ์—†๋Š”๋ฐ๋„ ๋‚จ์•„์žˆ๋Š” ๊ฒฝ์šฐ

    • ์ƒ‰์ƒ ๊ธฐ๋ฐ˜ ๋ Œ๋”๋ง์—์„œ๋Š” ํ…์Šค์ฒ˜๊ฐ€ ๋ถˆํ•„์š”. ๋ Œ๋”๋ง์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š์ง€๋งŒ ์ฝ”๋“œ๋ฅผ ์ •๋ฆฌํ•˜๋ฉด ์ข‹์Œ

๐ŸŽฏ ์‹ค์ „ ์ ์šฉ ์‹œ๋‚˜๋ฆฌ์˜ค

์ƒํ™ฉVertexColorData์˜ ์žฅ์ 
๋””๋ฒ„๊ทธ์šฉ ๋ฐ•์Šค ์ถœ๋ ฅ์ถฉ๋Œ ํŒ์ • ๋ฐ•์Šค๋‚˜ ์˜์—ญ ํ‘œ์‹œ ์‹œ ์œ ์šฉ
UI ์š”์†Œ ๊ตฌ์„ฑ๋ฒ„ํŠผ, ๋ฐฐ๊ฒฝ ๋ฐ•์Šค ๋“ฑ ํ…์Šค์ฒ˜ ์—†์ด ๋น ๋ฅด๊ฒŒ ๋ Œ๋”๋ง ๊ฐ€๋Šฅ
์…ฐ์ด๋” ํ…Œ์ŠคํŠธ์กฐ๋ช…, ์ƒ‰์ƒ ๋ธ”๋ Œ๋”ฉ ํ…Œ์ŠคํŠธ ์‹œ ๋‹จ์ˆœ ์ƒ‰์ƒ ์‚ฌ์šฉ์ด ํŽธํ•จ

profile
๏งกๅฎถ๋„ค_๊ณต๋ถ€๋ฐฉ

0๊ฐœ์˜ ๋Œ“๊ธ€