[Graphics] SimpleMath 실습

2025. 4. 26. 17:51·게임개발/Graphics

 

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
'게임개발/Graphics' 카테고리의 다른 글
  • [Graphics] GameObject, Transform, Component
  • [Graphics] SimpleMath
  • [Graphics] 행렬
  • [Graphics] RasterizerState, SampleState, BlendState
Mingurii
Mingurii
minkyeong's devlog
  • Mingurii
    Mingurii
    Mingurii
  • 전체
    오늘
    어제
    • 분류 전체보기 (53)
      • 알고리즘 - 기초 (8)
        • 재귀 (0)
        • 누적합 (8)
        • 최소신장트리 (0)
      • 알고리즘 (32)
        • 구현 (0)
        • DP (30)
        • DFS, BFS (0)
        • 백트래킹 (0)
        • 투포인터 (2)
        • 이진탐색 (0)
        • 그리디 (0)
        • 다익스트라 (0)
        • 크루스칼, 프림 (0)
        • 위상정렬 (0)
        • 플로이드-워셜 (0)
        • 유니온 파인드 (0)
        • 세그먼트 트리 (0)
      • 게임개발 (11)
        • Gameplay Ability System (0)
        • Graphics (11)
        • Lyra 분석 (0)
        • 다크 앤 다커 클론 코딩 (0)
        • After the Farmer Sleeps (0)
      • 서버 (2)
        • 공부 (2)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    c++
    행렬
    1749
    DX11
    DirectX11
    2294
    20438
    2293
    누적합
    9655
    그래픽스
    코테
    2167
    알고리즘
    게임수학
    백준
    코딩테스트
    DP
    서버
    투포인터
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.0
Mingurii
[Graphics] SimpleMath 실습
상단으로

티스토리툴바