이번에 새롭게 만든 DirectX11 3D프로젝트에서 기존에 했던 사각형 띄위기
shader만 사용하는 것이 아닌 Technique과 pass사용기존과 동일하게 GeometryHelper를 통해서 CreateQuad()함수를 생성해서 사각형 그릴때 사용할 Vertex와 Index를 생성
#pragma once
#include "Geometry.h"
#include "VertexData.h"
class GeometryHelper
{
public:
static void CreateQuad(shared_ptr<Geometry<VertexColorData>> geometry, Color color);
};
#include "pch.h"
#include "GeometryHelper.h"
void GeometryHelper::CreateQuad(shared_ptr<Geometry<VertexColorData>> geometry, Color color)
{
vector<VertexColorData> vtx;
vtx.resize(4);
// 1 3
// 0 2
vtx[0].position = Vec3(-0.5f, -0.5f, 0.f);
vtx[0].color = color;
vtx[1].position = Vec3(-0.5f, 0.5f, 0.f);
vtx[1].color = color;
vtx[2].position = Vec3(0.5f, -0.5f, 0.f);
vtx[2].color = color;
vtx[3].position = Vec3(0.5f, 0.5f, 0.f);
vtx[3].color = color;
geometry->SetVertices(vtx);
vector<uint32> idx = { 0, 1, 2, 2, 1, 3 };
geometry->SetIndices(idx);
}
technique11와 pass를 활용하여 여러개의 효과를 볼수 있도록 설정
struct VertexInput
{
float4 position : POSITION;
float4 color : COLOR;
};
struct VertexOutput
{
float4 position : SV_POSITION; //SV : System Value
float4 color : COLOR;
};
VertexOutput VS(VertexInput input)
{
VertexOutput output;
output.position = input.position;
output.color = input.color;
return output;
}
float4 PS(VertexOutput input) : SV_TARGET
{
return input.color;
}
RasterizerState FillModeWireframe
{
FillMode = Wireframe;
CullMode = None;
};
technique11 T0
{
pass P0
{
SetVertexShader( CompileShader( vs_5_0, VS() ) );
SetPixelShader( CompileShader( ps_5_0, PS() ) );
}
pass P1
{
SetRasterizerState(FillModeWireframe);
SetVertexShader( CompileShader( vs_5_0, VS() ) );
SetPixelShader( CompileShader( ps_5_0, PS() ) );
}
}
Init, Update, Render를 가지고 있음shader, geometry, vertexBuffer, indexBuffer를 가지고 있음Init함수 : 각 변수를 생성해주고, 버퍼를 생성Render함수 : 각 vertexBuffer와 indexBuffer를 DeviceContext에 설정해주고, DrawIndexed를 통해서 화면에 사각형을 그리기#pragma once
#include "IExecute.h"
#include "Geometry.h"
class QuadDemo : public IExecute
{
public:
void Init() override;
void Update() override;
void Render() override;
shared_ptr<Shader> _shader;
shared_ptr<Geometry<VertexColorData>> _geometry;
shared_ptr<VertexBuffer> _vertexBuffer;
shared_ptr<IndexBuffer> _indexBuffer;
};
#include "pch.h"
#include "02. QuadDemo.h"
#include "GeometryHelper.h"
void QuadDemo::Init()
{
_shader = make_shared<Shader>(L"02.Quad.fx");
_geometry = make_shared<Geometry<VertexColorData>>();
GeometryHelper::CreateQuad(_geometry, Color(0.f, 1.f, 0.f, 1.f));
_vertexBuffer = make_shared<VertexBuffer>();
_vertexBuffer->Create(_geometry->GetVertices());
_indexBuffer = make_shared<IndexBuffer>();
_indexBuffer->Create(_geometry->GetIndices());
}
void QuadDemo::Update()
{
}
void QuadDemo::Render()
{
uint32 stride = _vertexBuffer->GetStride();
uint32 offset = _vertexBuffer->GetOffset();
DC->IASetVertexBuffers(0, 1, _vertexBuffer->GetComPtr().GetAddressOf(), &stride, &offset);
DC->IASetIndexBuffer(_indexBuffer->GetComPtr().Get(), DXGI_FORMAT_R32_UINT, 0);
//_shader->Draw(0, 0, 3);
_shader->DrawIndexed(0, 1, _indexBuffer->GetCount(), 0, 0);
}
desc만 수정해서 GAME->RUN(desc)을 활용 (3d에서 사용하는 Engine부분은 따로 정리 예정)#include "pch.h"
#include "Main.h"
#include "Engine/Game.h"
#include "02. QuadDemo.h"
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
GameDesc desc;
desc.appName = L"GameCoding";
desc.hInstance = hInstance;
desc.vsync = false;
desc.hWnd = NULL;
desc.width = 800;
desc.height = 600;
desc.clearColor = Color(0.5f, 0.5f, 0.5f, 0.5f);
desc.app = make_shared<QuadDemo>(); //테스트할 클래스를 생성
GAME->Run(desc);
return 0;
}
Main함수가 동작하는 원리 간단히 보기
WinAPI가 시작하는 함수 =
Game::RUN
- Game 헤더에서 GameDesc의 구조체를 생성
- Game::Run()에서 desc를 매개변수로 받아서 메인루프 시작
- Client 프로젝트의 Main에서 GameDesc 구조체를 설정하고 Game::Run에 넘기는 방식
// Game.h struct GameDesc { shared_ptr<class IExecute> app = nullptr; wstring appName = L"GameCoding"; HINSTANCE hInstance = 0; HWND hWnd = 0; float width = 800; float height = 600; bool vsync = false; bool windowed = true; Color clearColor = Color(0.5f, 0.5f, 0.5f, 0.5f); };