GameOjbect
- DirectX11 렌더링의 모든 구성 요소를 캡슐화한 클래스
#pragma once
class GameObject
{
public:
GameObject(ComPtr<ID3D11Device> device, ComPtr<ID3D11DeviceContext> deviceContext);
~GameObject();
void Update();
void Render(shared_ptr<Pipeline> pipeline);
private:
ComPtr<ID3D11Device> _device;
shared_ptr<Geometry<VertexTextureData>> _geometry;
shared_ptr<VertexBuffer> _vertexBuffer;
shared_ptr<IndexBuffer> _indexBuffer;
shared_ptr<InputLayout> _inputLayout;
shared_ptr<VertexShader> _vertexShader;
shared_ptr<RasterizerState> _rasterizerState;
shared_ptr<PixelShader> _pixelShader;
shared_ptr<Texture> _texture1;
shared_ptr<SamplerState> _samplerState;
shared_ptr<BlendState> _blendState;
private:
TransformData _transformData;
shared_ptr<ConstantBuffer<TransformData>> _constantBuffer;
Vec3 _localPosition = Vec3(0.f, 0.f, 0.f);
Vec3 _localRotation = Vec3(0.f, 0.f, 0.f);
Vec3 _localScale = Vec3(1.f, 1.f, 1.f);
};
#include "pch.h"
#include "GameObject.h"
GameObject::GameObject(ComPtr<ID3D11Device> device, ComPtr<ID3D11DeviceContext> deviceContext)
: _device(device)
{
_geometry = make_shared<Geometry<VertexTextureData>>();
GeometryHelper::CreateRectangle(_geometry);
_vertexBuffer = make_shared<VertexBuffer>(_device);
_vertexBuffer->Create(_geometry->GetVertices());
_indexBuffer = make_shared<IndexBuffer>(_device);
_indexBuffer->Create(_geometry->GetIndices());
_vertexShader = make_shared<VertexShader>(_device);
_vertexShader->Create(L"Default.hlsl", "VS", "vs_5_0");
_inputLayout = make_shared<InputLayout>(_device);
_inputLayout->Create(VertexTextureData::descs, _vertexShader->GetBlob());
_pixelShader = make_shared<PixelShader>(_device);
_pixelShader->Create(L"Default.hlsl", "PS", "ps_5_0");
_rasterizerState = make_shared<RasterizerState>(_device);
_rasterizerState->Create();
_blendState = make_shared<BlendState>(_device);
_blendState->Create();
_constantBuffer = make_shared<ConstantBuffer<TransformData>>(_device, deviceContext);
_constantBuffer->Create();
_texture1 = make_shared<Texture>(_device);
_texture1->Create(L"Skeleton.png");
_samplerState = make_shared<SamplerState>(_device);
_samplerState->Create();
}
GameObject::~GameObject()
{
}
void GameObject::Update()
{
_localPosition.x += 0.0005f;
Matrix matScale = Matrix::CreateScale(_localScale / 3);
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;
_transformData.matWorld = matWorld;
_constantBuffer->CopyData(_transformData);
}
void GameObject::Render(shared_ptr<Pipeline> pipeline)
{
PipelineInfo info;
info.inputLayout = _inputLayout;
info.vertexShader = _vertexShader;
info.pixelShader = _pixelShader;
info.rasterizerState = _rasterizerState;
info.blendState = _blendState;
pipeline->UpdatePipeline(info);
pipeline->SetVertexBuffer(_vertexBuffer);
pipeline->SetIndexBuffer(_indexBuffer);
pipeline->SetConstantBuffer(0, SS_VertexShader, _constantBuffer);
pipeline->SetTexture(0, SS_PixelShader, _texture1);
pipeline->SetSamplerState(0, SS_PixelShader, _samplerState);
pipeline->DrawIndexed(_geometry->GetIndexCount(), 0, 0);
}