렌더링 파이프라인의 VS 단계에서는 World, View, Projection이 이루어진다
4x4 행렬로 이루어져있으며
D3DXMatrixIdentity(&world);
Identity를 통해 정방행렬로 초기화된다
World
이전에 정점을 옮기는 방법으로 GetDC() -> UpdateSubresource() 함수를 사용했다
이 함수는 사실 빠르지 않은 함수라 사용하지 않고
World를 옮기는 방법을 사용한다
World를 옮기는 것이 정점을 하나하나 움직이는 것보다 훨씬 빠르고 관리도 편하다
여기서 사각형이 Local, 사각형을 담고있는 통이 World를 나타낸다
World를 Transform(이동), Scale(크기), Rotation(회전) 하면 World안의 Local이 전부 동시에 움직인다
//x의 위치 조정( 행렬의 4행 1열)
if (Keyboard::Get()->Press(VK_RIGHT))
{
world._41 += 1.0f * Time::Delta();
}
else if (Keyboard::Get()->Press(VK_LEFT))
{
world._41 -= 1.0f * Time::Delta();
}
// x,y의 크기 조정 (행렬의 1행1열, 2행2열)
if (Keyboard::Get()->Press(VK_RIGHT))
{
world._11 += 1.0f * Time::Delta();
world._22 += 1.0f * Time::Delta();
}
else if (Keyboard::Get()->Press(VK_LEFT))
{
world._11 -= 1.0f * Time::Delta();
world._22 -= 1.0f * Time::Delta();
}
키 입력을 받아 사각형의 x크기(world._11)와 y크기(world._22)를 움직인다
View와 Projection도 간단하게 살펴보면
View는 바라볼 위치와 방향을 나타낸다
Projection은 얼만큼 보여줄 것인지를 의미한다
Projection의 과정에서 절두체가 만들어진다
shader->AsMatrix("World")->SetMatrix(world);
shader->AsMatrix("View")->SetMatrix(Context::Get()->View());
shader->AsMatrix("Projection")->SetMatrix(Context::Get()->Projection());
셰이더의 World변수에 world matrix를 세팅
동일하게 View와 Projection도 수행
matrix World;
matrix View;
matrix Projection;
struct VertexInput
{
float4 Position: Position;
};
struct VertexOutput
{
float4 Position : SV_Position;
};
VertexOutput VS(VertexInput input)
{
VertexOutput output;
output.Position = mul(input.Position, World);
output.Position = mul(output.Position, View);
output.Position = mul(output.Position, Projection);
return output;
}
inputposition을 world매트릭스에 곱(mul)해서 리턴해준다 => World가 input position만큼 이동하게 된다
차례대로 View와 Projection도 곱해준다
행렬의 곱으로 위치를 세팅하기
Matrix world;
Vector3 position = Vector3(0, 0, 0);
Vector3 scale = Vector3(1, 1, 1);
Matrix world2;
Vector3 position2 = Vector3(0,0,0);
Vector3 scale2 = Vector3(1,1,1);
Matrix S, T;
D3DXMatrixTranslation(&T, position.x, position.y, position.z);
D3DXMatrixScaling(&S, scale.x, scale.y, scale.z);
world = S * T;
Translation matrix와 Scale matrix를 곱해주면 움직이게 된다
이 때 Scale과 Translation의 곱하는 순서를 주의해주어야한다
서로 위치가 바뀌면 다른 결과가 나오게 된다
shader->AsMatrix("View")->SetMatrix(Context::Get()->View());
shader->AsMatrix("Projection")->SetMatrix(Context::Get()->Projection());
UINT stride = sizeof(Vertex);
UINT offset = 0;
D3D::GetDC()->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
shader->AsScalar("Index")->SetInt(0);
shader->AsMatrix("World")->SetMatrix(world);
D3D::GetDC()->IASetVertexBuffers(0, 1, &vertexBuffer, &stride, &offset);
shader->Draw(0,0,6);
shader->AsScalar("Index")->SetInt(1);
shader->AsMatrix("World")->SetMatrix(world2);
D3D::GetDC()->IASetVertexBuffers(0, 1, &vertexBuffer2, &stride, &offset);
shader->Draw(0, 0, 6);
world가 여러개일 때는 World Matrix와 VertexBuffer를 각각 설정해주어야한다
'> Study > DirectX' 카테고리의 다른 글
[DirectX] Texture (0) | 2024.01.23 |
---|---|
[DirectX] Grid, Camera (0) | 2024.01.04 |
[DirectX] Triangle, Rectangle (0) | 2023.12.21 |
[DirectX] Vertex (1) | 2023.12.20 |
[DirectX] (1) | 2023.12.20 |