본문 바로가기

개인공부/DirectX

DirectX 4일차

내용:

 

행렬

더보기

행렬의덧셈 == 중요하지않음

 

행렬의곱셈

 

즉 첫번쨰 행렬의 행과 두번째행렬의 열의 갯수가 같아야 연산이 가능하다.

단위행렬

회전행렬(Rotation Matrix)

전치행렬(행과열의 바꾸는 행렬)

역행렬

 

 

 

 

상수버퍼

더보기

cpu에 특정값을 넘겨주기위한 버퍼

gpu에서 상수란 매프레임마다 렌더파이프라인이 호출되는 동안 쓰이는 것

상수버퍼 -헤더파일

더보기
#pragma once
class ConstBuffer
{
public:
	ConstBuffer(void* data,UINT dataSize);
	~ConstBuffer();

	void SetVS(UINT slot);
	void SetPS(UINT slot);

private:
	ID3D11Buffer* buffer;

	void* data;
	UINT dataSize;
};

상수버퍼-클래스파일

더보기
#include "Framework.h"

ConstBuffer::ConstBuffer(void* data, UINT dataSize)
    : data(data),dataSize(dataSize)
{
    D3D11_BUFFER_DESC bufferDesc = {};
    bufferDesc.Usage = D3D11_USAGE_DEFAULT;
    bufferDesc.ByteWidth = dataSize;
    bufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;

    D3D11_SUBRESOURCE_DATA initData = {};
    initData.pSysMem = data;

    DEVICE->CreateBuffer(&bufferDesc, nullptr, &buffer);
}

ConstBuffer::~ConstBuffer()
{
    buffer->Release();
}

void ConstBuffer::SetVS(UINT slot)
{
    DC->UpdateSubresource(buffer, 0, nullptr, data, 0, 0);
    DC->VSSetConstantBuffers(slot, 1, &buffer);
}

void ConstBuffer::SetPS(UINT slot)
{
    DC->UpdateSubresource(buffer, 0, nullptr, data, 0, 0);
    DC->PSSetConstantBuffers(slot, 1, &buffer);
}

 

글로벌버퍼

더보기
#pragma once


class ColorBuffer : public ConstBuffer
{
public:
	ColorBuffer() : ConstBuffer(&color, sizeof(Float4))
	{
	}

	void SetColor(float r, float g, float b, float a = 1.0f)
	{
		color = { r,g,b,a };
	}

	Float4 GetColor() { return color; }
private:
	Float4 color = { 1,1,1,1 };
};

정점을 뭉친 오브젝트를 == world변환이라하고 

가상의 카메라를 비춰서보는것 view변환

절두체를 스크린안으로 들어오게 하는것 projection변환

행렬에는 교환법칙이 성립하지않기떄문에

world -> view-> projection과정을 거쳐야한다.

더보기
cbuffer WorldBuffer : register(b0)
{
    matrix world;
}

cbuffer ViewBuffer : register(b1)
{
    matrix view;
}

cbuffer ProjectionBuffer : register(b2)
{
    matrix projection;
}

struct Input
{
    float4 pos : POSITION;
    float4 color : COLOR;
};

struct Output
{
    float4 pos : SV_POSITION;
    float4 color : COLOR;
};

Output VS(Input input)
{
    Output output;
    
    output.pos = mul(input.pos, world);
    output.pos = mul(output.pos, view);
    output.pos = mul(output.pos, projection);
    
    output.color = input.color;
    
    return output;
}

 

projection변환에는 두가지가있는데

 

1) Orthographic: 절두체를 직육면체로 설정해서 원근감이 없게끔 설정하는방법 == 2D || Ui에 자주쓰이는 방법

 

2) Perspective : 절두체가 사각뿔형태, 원근감이 있다. ==3D에 사용

 

 

RS단계 가 하는일

더보기

1. View 포트 변환

2. 폴리곤안을 채우는 일(선형보간을 이용)

 

정보를 매트릭스로 담는버퍼

더보기
// 글로벌버퍼내부의 함수 

class MatrixBuffer : public ConstBuffer
{
public:
	MatrixBuffer() : ConstBuffer(&matrix, sizeof(Float4))
	{
		matrix = XMMatrixIdentity();
	}

	void Set(Matrix value)
	{
		matrix = XMMatrixTranspose(value);
	}

private:
	Matrix matrix;
};

 

 

world->view->projection과정

더보기
 worldBuffer = new MatrixBuffer();
 viewBuffer = new MatrixBuffer();
 projectionBuffer = new MatrixBuffer();

 Matrix orthographic = XMMatrixOrthographicOffCenterLH(0.0f,SCREEN_WIDTH,0.0f,SCREEN_HEIGHT,-1.0f,1.0f);

 projectionBuffer->Set(orthographic);

 

행렬에서 4행,4열의 존재이유

더보기

위치 관할하는 행렬번호

41번 = x

42번 = y

43번 = z

 

14

24

34

44

 

Scale을 관할하는 행렬번호

11 x 

22 y

33 z

회전을 할때 관여하는 원소

더보기

X축회전할때는 1행,2열이 들어가는 원소를 제외한 원소들(초록색)

전체 코드

 

더보기
#include "Framework.h"
#include "TutorialScene.h"

TutorialScene::TutorialScene()
{
	vertexShader = Shader::AddVS(L"Vertex.hlsl");
	pixelShader = Shader::AddPS(L"Pixel.hlsl");
        
   /* UINT count = 100;
   // Polygon : 정점 3개로 이루어진 3차원 상의 평면
   // 정점순서의 시계방향을 앞면으로 하며 앞면만 출력
    float stepAngle = XM_2PI / count;

    float radius = 100.0f;
    VertexColor center(0, 0, 1, 0, 0);

    vertices.push_back(center);

    FOR(count)
    {
        float angle = stepAngle * i;        
        vertices.emplace_back(cos(angle)* radius, sin(angle)* radius, 0, 0, 1);
    }
    vertices.emplace_back(cos(0)* radius, sin(0)* radius, 0, 0, 1);
*/

    vertices.emplace_back(0, 100, 1, 0, 0);
    vertices.emplace_back(100, -100, 1, 0, 0);
    vertices.emplace_back(-100, -100, 1, 0, 0);
    vertexBuffer = new VertexBuffer(vertices.data(), sizeof(VertexColor), vertices.size());

    indices = { 0, 1, 2 };
   /* FOR(count)
    {
        indices.push_back(0);        
        if (i + 2 > count)
        {
            indices.push_back(1);
        }
        else
        {
            indices.push_back(i + 2);
        }
        indices.push_back(i + 1);

    }*/

    indexBuffer = new IndexBuffer(indices.data(), indices.size());

    colorBuffer = new ColorBuffer();

    worldBuffer = new MatrixBuffer();
    viewBuffer = new MatrixBuffer();
    projectionBuffer = new MatrixBuffer();

    Matrix orthographic = XMMatrixOrthographicOffCenterLH(0.0f,SCREEN_WIDTH,0.0f,SCREEN_HEIGHT,-1.0f,1.0f);

    projectionBuffer->Set(orthographic);
    worldMatrix._11 = 1;
    worldMatrix._22 = 1;
    worldMatrix._33 = 1;
    worldMatrix._44 = 1;

    worldMatrix._41 = CENTER_X;
    worldMatrix._42 = CENTER_Y;
}

TutorialScene::~TutorialScene()
{
    delete vertexBuffer;
    delete indexBuffer;
    delete colorBuffer;

    delete worldBuffer;
    delete viewBuffer;
    delete projectionBuffer;
}

void TutorialScene::Update()
{
    pos = mousePos;

    /*if (KEY->Press('W'))
    {
        worldMatrix._42 += 100.0f * DELTA;
    } 
    if (KEY->Press('S'))
    {
        worldMatrix._42 -= 100.0f * DELTA;
    } 
    if (KEY->Press('A'))
    {
        worldMatrix._41 -= 100.0f * DELTA;
    }
    if (KEY->Press('D'))
    {
        worldMatrix._41 += 100.0f * DELTA;
    }*/
    if (KEY->Press('W'))
    {
        pos.y+= 100.0f * DELTA;
    }
    if (KEY->Press('S'))
    {
        pos.y -= 100.0f * DELTA;
    }
    if (KEY->Press('A'))
    {
        pos.x -= 100.0f * DELTA;
    }
    if (KEY->Press('D'))
    {
        pos.x += 100.0f * DELTA;
    }

    Matrix T = XMMatrixTranslation(pos.x, pos.y, 0.0f);


    if (KEY->Press('T'))
    {
        scale.y += 1.0f * DELTA;
    }
    if (KEY->Press('G'))
    {
        scale.y -= 1.0f * DELTA;
    }
    if (KEY->Press('F'))
    {
        scale.x -= 1.0f * DELTA;
    }
    if (KEY->Press('H'))
    {
        scale.x += 1.0f * DELTA;
    }
    Matrix S = XMMatrixScaling(scale.x, scale.y, 1.0f);
    /*static float angle = 0.0f;
    
    worldMatrix._11 = cos(angle);
    worldMatrix._12 = sin(angle);
    worldMatrix._21 = -sin(angle);
    worldMatrix._22 = cos(angle);*/



    if (KEY->Press(VK_UP))
    {
        angle += DELTA;
    }
    if (KEY->Press(VK_DOWN))
    {
        angle -= DELTA;
    }

    Matrix R = XMMatrixRotationZ(angle);
    //// 색을 바꾸는 함수
    //float r = Random(0.0f, 1.0f);
    //float g = Random(0.0f, 1.0f);
    //float b = Random(0.0f, 1.0f);

    //colorBuffer->SetColor(r, g, b);

    //worldBuffer->Set(XMLoadFloat4x4(&worldMatrix));

    worldBuffer->Set(S * R * T);
}

void TutorialScene::Render()
{
    worldBuffer->SetVS(0);
    viewBuffer->SetVS(1);
    projectionBuffer->SetVS(2);
    vertexBuffer->Set(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
    indexBuffer->Set();

    colorBuffer->SetPS(0);

    vertexShader->Set();
    pixelShader->Set();

    DC->DrawIndexed(indices.size(), 0, 0);
    //DC->Draw(vertices.size(), 0);
}

void TutorialScene::PostRender()
{
}

 

 

과제

더보기

비행기 출력 (객체따로빼기)

 

총알구현

 

마우스위치바라보게 회전

 

 

비행기를타고훨훨날아가겠죠.zip
14.17MB

'개인공부 > DirectX' 카테고리의 다른 글

DirectX8일차_RectCollision  (0) 2024.03.07
DirectX 7일차 _Collision  (0) 2024.03.06
DirectX 6일차  (0) 2024.03.05
DirectX 5일차  (0) 2024.03.04
DirectX 1일  (0) 2024.02.26