[D3D] Constant Buffer

vector·2025년 11월 17일

"ConstantBuffer를 통해 방향키를 사용해서 사각형을 이동시키기"

ConstBuffer.fx

fx에서 World, View, Projection Matrix를 선언해주고 곱해주는 방식으로 진행

/* 변경 부분*/
matrix world;
matrix view;
matrix projection;
/* 변경 부분*/

struct VertexInput
{
    float4 position : POSITION;
    float4 color : COLOR;
};

struct VertexOutput
{
    float4 position : SV_POSITION; //SV : System Value
    float4 color : COLOR;
};

VertexOutput VS(VertexInput input)
{
    VertexOutput output;
    /* 변경 부분*/
    output.position = mul(input.position, world);
    output.position = mul(output.position, view);
    output.position = mul(output.position, projection);
    /* 변경 부분*/
    
    output.color = input.color;
    
    
    return output;
}

float4 PS(VertexOutput input) : SV_TARGET
{
    return input.color;
}

RasterizerState FillModeWireframe
{
    FillMode = Wireframe;
    CullMode = None;
};


technique11 T0
{
    pass P0
    {
        SetVertexShader( CompileShader( vs_5_0, VS() ) );
        SetPixelShader( CompileShader( ps_5_0, PS() ) );
    }
    pass P1
    {
        SetRasterizerState(FillModeWireframe);
        SetVertexShader( CompileShader( vs_5_0, VS() ) );
        SetPixelShader( CompileShader( ps_5_0, PS() ) );
    }
}

ConstBufferDemo

  • 변수로 world view projection Matrix와 위치값 translation을 설정
  • Update()에서 Key값을 받아서 translation을 수정하며, worldtranslation을 적용

constBufferDemo.h

#pragma once
#include "IExecute.h"
#include "Geometry.h"

class ConstBufferDemo : public IExecute
{
public:
	void Init() override;
	void Update() override;
	void Render() override;


	shared_ptr<Shader> _shader;
	shared_ptr<Geometry<VertexColorData>> _geometry;
	shared_ptr<VertexBuffer> _vertexBuffer;
	shared_ptr<IndexBuffer> _indexBuffer;


	Vec3 _translation = Vec3(0, 0, 0);

	Matrix _world = Matrix::Identity;
	Matrix _view = Matrix::Identity;
	Matrix _projection = Matrix::Identity;

};

ConstBuffer.cpp

#include "pch.h"
#include "03. ConstBufferDemo.h"
#include "GeometryHelper.h"


void ConstBufferDemo::Init()
{
	_shader = make_shared<Shader>(L"03.ConstBuffer.fx");
	
	_geometry = make_shared<Geometry<VertexColorData>>();
	GeometryHelper::CreateQuad(_geometry, Color(0.f, 1.f, 0.f, 1.f));


	_vertexBuffer = make_shared<VertexBuffer>();
	_vertexBuffer->Create(_geometry->GetVertices());

	_indexBuffer = make_shared<IndexBuffer>();
	_indexBuffer->Create(_geometry->GetIndices());
}

void ConstBufferDemo::Update()
{
	float dt = TIME->GetDeltaTime();

	if (INPUT->GetButton(KEY_TYPE::A))
	{
		_translation.x -= dt;
	}
	else if (INPUT->GetButton(KEY_TYPE::D))
	{
		_translation.x += dt;
	}
	if (INPUT->GetButton(KEY_TYPE::W))
	{
		_translation.y += dt;
	}
	else if (INPUT->GetButton(KEY_TYPE::S))
	{
		_translation.y -= dt;
	}

	//_translation.Normalize();


	// SRT
	_world = Matrix::CreateTranslation(_translation);
}

void ConstBufferDemo::Render()
{
	// 
	_shader->GetMatrix("world")->SetMatrix((float*)&_world);
	_shader->GetMatrix("view")->SetMatrix((float*)&_view);
	_shader->GetMatrix("projection")->SetMatrix((float*)&_projection);




	uint32 stride = _vertexBuffer->GetStride();
	uint32 offset = _vertexBuffer->GetOffset();

	DC->IASetVertexBuffers(0, 1, _vertexBuffer->GetComPtr().GetAddressOf(), &stride, &offset);

	DC->IASetIndexBuffer(_indexBuffer->GetComPtr().Get(), DXGI_FORMAT_R32_UINT, 0);


	//_shader->Draw(0, 0, 3);
	_shader->DrawIndexed(0, 0, _indexBuffer->GetCount(), 0, 0);
}

참고 강의

https://www.inflearn.com/courses/lecture?courseId=329791&type=LECTURE&unitId=161119&tab=curriculum&subtitleLanguage=ko

profile
게임 클라이언트 프로그래머 준비중 (공부 및 기록용)

0개의 댓글