Matrix를 이용하여 실시간 위치 변경 영상
// Game.h
private:
/* Constant Buffer */
TransformData _transformData;
ComPtr<ID3D11Buffer> _constantBuffer;
/* SRT */
Vec3 _localPosition = { 0.f, 0.f, 0.f };
Vec3 _localRotation = { 0.f, 0.f, 0.f };
Vec3 _localScale = { 1.f, 1.f, 1.f };
// Game.cpp
void Game::Update()
{
_localPosition.x += 0.001f;
// Create SRT
Matrix scaleMatrix = Matrix::CreateScale(_localScale / 3);
Matrix rotationMatrix = Matrix::CreateRotationX(_localRotation.x);
rotationMatrix *= Matrix::CreateRotationY(_localRotation.y);
rotationMatrix *= Matrix::CreateRotationZ(_localRotation.z);
Matrix translationMatrix = Matrix::CreateTranslation(_localPosition);
// Create WorldMatrix
Matrix worldMatrix = scaleMatrix * rotationMatrix * translationMatrix;
_transformData.worldMatrix = worldMatrix;
D3D11_MAPPED_SUBRESOURCE subResource;
ZeroMemory(&subResource, sizeof(subResource));
// GPU의 Constant Buffer를 CPU에서 쓰기 위한 접근 요청
_deviceContext->Map(
_constantBuffer.Get(), // 업데이트할 Constant Buffer
0, // 서브리소스 인덱스 (일반적으로 0)
D3D11_MAP_WRITE_DISCARD, // 이전 내용은 버리고 새로 쓰기 (가장 일반적인 방식)
0, // Reserved (항상 0)
&subResource // 매핑 결과를 받을 구조체 (CPU가 접근 가능한 포인터 제공됨)
);
// _transformData값을 GPU메모리로 복사한 후, GPU의 Constant Buffer에 업로드
::memcpy(subResource.pData, &_transformData, sizeof(_transformData));
// 맵핑 해제 → GPU에서 읽을 수 있도록 다시 연결
_deviceContext->Unmap(_constantBuffer.Get(), 0);
}
// DefaultVertexShader.hlsl
cbuffer TransformData : register(b0)
{
row_major matrix worldMatrix;
row_major matrix viewMatrix;
row_major matrix projectionMatrix;
}
// IA - VS - RS(VS_main에서 자동 처리) - PS - OM
// VS_main은 정점 단위로 실행, 위치 관련 처리
VS_OUTPUT VS_main(VS_INPUT input)
{
VS_OUTPUT output;
// World, View, Projection Matrix
float4 position = mul(input.position, worldMatrix);
position = mul(position, viewMatrix);
position = mul(position, projectionMatrix);
output.position = position;
output.uv = input.uv;
return output;
}
// Struct.h
/* Constant Buffer, 16Byte 정렬 필요 */
struct TransformData
{
Matrix worldMatrix = Matrix::Identity; // offset 0, size = 16 * sizeof(float) = 64Byte;
Matrix viewMatrix = Matrix::Identity; // offset 64, size = 16 * sizeof(float) = 64Byte;
Matrix projectrionMatrix = Matrix::Identity; // offset 128, size = 16 * sizeof(float) = 64Byte
};
'게임개발 > Graphics' 카테고리의 다른 글
[Graphics] GameObject, Transform, Component (0) | 2025.04.30 |
---|---|
[Graphics] SimpleMath (0) | 2025.04.21 |
[Graphics] 행렬 (0) | 2025.04.21 |
[Graphics] RasterizerState, SampleState, BlendState (0) | 2025.04.10 |
[Graphics] Constant Buffer (0) | 2025.04.07 |