오늘은 각종 행렬과 수학을 코드로 구현해볼 것이다.
일단 구조체부분을 매트릭스가 들어갈 수 있게 바꿔주자
Struct.h
#pragma once
#include "Types.h"
struct Vertex
{
Vec3 position; //12바이트 0부터시작
//Color color; //12부터시작
Vec2 uv;
};
struct TransformData
{
Matrix matWorld = Matrix::Identity;
Matrix matView = Matrix::Identity;
Matrix matProjection= Matrix::Identity;
};
그리고 지역좌표계에서 이동、회전、크기조절을 담당하게 될 매트릭스 변수를 선언해주자
Vec3 _localposition = { 0.f,0.f,0.f };
Vec3 _localRotation = { 0.f,0.f,0.f };
Vec3 _localScale = { 1.f,1.f,1.f };
그리고 업데이트문을 수정하여 _transformData에 맞는 값이 들어갈 수 있도 하자。
void Game::Update()
{
//크기 회전 이동
Matrix matScale = Matrix::CreateScale(_localScale);
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;
D3D11_MAPPED_SUBRESOURCE subResouce;
ZeroMemory(&subResouce, sizeof(subResouce));
//맵을 통해 값을 넣어줄 준비를 한다. 이후 값을 넣고(복사해주고) UNMAP
_deviceContext->Map(_constantBuffer.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &subResouce);
::memcpy(subResouce.pData, &_transformData, sizeof(_transformData)); //바로 cpu-> gpu 넘어가게 된다.
_deviceContext->Unmap(_constantBuffer.Get(), 0);
}
이제 이 값을 쉐이더 상에서 처리할 수 있게 hlsl파일 코드도 고쳐주자
Default.hlsl
cbuffer TransformData : register(b0)
{
//hlsl상의 순서와 코드상의 순서가 달라서
row_major matrix matWorld;
row_major matrix matView;
row_major matrix matProjection;
}
//정점 쉐이더- 위치관련 -> 레스터라이저-정점바탕으로 도형만들고 내/외부 판단 및 보간
VS_OUTPUT VS(VS_INPUT input)
{
VS_OUTPUT output;
//World view projection
float4 position = mul(input.position, matWorld);
position = mul(position, matView);
position = mul(position, matProjection);
output.position = position;
output.uv = input.uv;
return output;
}
보면 아직 행렬쪽에서 변화가 없기 때문에 그대로인 결과이미지를 볼 수 있다.
만약 크기를 1/3배 줄인다면
Matrix matScale = Matrix::CreateScale(_localScale/3);
줄어든 이미지를 볼 수 있다.
'게임공부 > Directx11' 카테고리의 다른 글
[Directx11][C++]10. 프레임워크 제작2(Shader,Rasterizer) (0) | 2024.08.08 |
---|---|
[Directx11][C++]9. 프레임워크 제작(Graphics,Input Assembler,Geometry) (0) | 2024.07.31 |
[Directx11]7. RasterizerState, SampleState, BlendState (0) | 2024.07.10 |
[Directx11]6. Constant Buffer(상수 버퍼) (0) | 2024.07.09 |
[Directx11][C++]5. 텍스처와 UV (0) | 2024.07.01 |