図0009a
void CubeObject::CreateBuffers() {
float HelfSize = 0.5f;
vector<VertexPositionColor> vertices = {
{ VertexPositionColor(Vec3(-HelfSize, HelfSize, -HelfSize), Col4(1.0f, 0.0f, 0.0f, 1.0f)) },
{ VertexPositionColor(Vec3(HelfSize, HelfSize, -HelfSize), Col4(0.0f, 1.0f, 0.0f, 1.0f)) },
{ VertexPositionColor(Vec3(-HelfSize, -HelfSize, -HelfSize), Col4(0.0f, 0.0f, 1.0f, 1.0f)) },
{ VertexPositionColor(Vec3(HelfSize, -HelfSize, -HelfSize), Col4(1.0f, 0.0f, 1.0f, 1.0f)) },
{ VertexPositionColor(Vec3(HelfSize, HelfSize, HelfSize), Col4(1.0f, 0.0f, 0.0f, 1.0f)) },
{ VertexPositionColor(Vec3(-HelfSize, HelfSize, HelfSize), Col4(0.0f, 1.0f, 0.0f, 1.0f)) },
{ VertexPositionColor(Vec3(HelfSize, -HelfSize, HelfSize), Col4(0.0f, 0.0f, 1.0f, 1.0f)) },
{ VertexPositionColor(Vec3(-HelfSize, -HelfSize, HelfSize), Col4(1.0f, 0.0f, 1.0f, 1.0f)) }
};
vector<uint16_t> indices = {
0, 1, 2,
1, 3, 2,
1, 4, 3,
4, 6, 3,
4, 5, 6,
5, 7, 6,
5, 0, 7,
0, 2, 7,
5, 4, 0,
4, 1, 0,
6, 7, 3,
7, 2, 3
};
//メッシュの作成(変更できない)
m_CubeMesh = MeshResource::CreateMeshResource(vertices, indices, false);
}
struct StaticConstantBuffer
{
Mat4x4 World;
Mat4x4 View;
Mat4x4 Projection;
Col4 Emissive;
StaticConstantBuffer() {
memset(this, 0, sizeof(StaticConstantBuffer));
};
};
PSPCInput main(VSPCInput input)
{
PSPCInput result;
//頂点の位置を変換
float4 pos = float4(input.position.xyz, 1.0f);
//ワールド変換
pos = mul(pos, World);
//ビュー変換
pos = mul(pos, View);
//射影変換
pos = mul(pos, Projection);
//ピクセルシェーダに渡す変数に設定
result.position = pos;
result.color = input.color;
return result;
}
void CubeObject::CreatePipelineState() {
D3D12_GRAPHICS_PIPELINE_STATE_DESC PineLineDesc;
m_PipelineState = PipelineState::CreateDefault3D<VertexPositionColor,
VSPCStatic, PSPCStatic>(m_RootSignature, PineLineDesc);
}
template<typename Vertex, typename VS, typename PS>
static inline ComPtr<ID3D12PipelineState>
CreateDefault3D(const ComPtr<ID3D12RootSignature>& rootSignature,
D3D12_GRAPHICS_PIPELINE_STATE_DESC& RetDesc) {
CD3DX12_RASTERIZER_DESC rasterizerStateDesc(D3D12_DEFAULT);
//裏面カリング
rasterizerStateDesc.CullMode = D3D12_CULL_MODE_NONE;
ZeroMemory(&RetDesc, sizeof(RetDesc));
RetDesc.InputLayout = { Vertex::GetVertexElement(), Vertex::GetNumElements() };
RetDesc.pRootSignature = rootSignature.Get();
RetDesc.VS =
{
reinterpret_cast<UINT8*>(VS::GetPtr()->GetShaderComPtr()->GetBufferPointer()),
VS::GetPtr()->GetShaderComPtr()->GetBufferSize()
};
RetDesc.PS =
{
reinterpret_cast<UINT8*>(PS::GetPtr()->GetShaderComPtr()->GetBufferPointer()),
PS::GetPtr()->GetShaderComPtr()->GetBufferSize()
};
RetDesc.RasterizerState = rasterizerStateDesc;
RetDesc.BlendState = CD3DX12_BLEND_DESC(D3D12_DEFAULT);
RetDesc.DepthStencilState = CD3DX12_DEPTH_STENCIL_DESC(D3D12_DEFAULT);
RetDesc.SampleMask = UINT_MAX;
RetDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
RetDesc.NumRenderTargets = 1;
RetDesc.RTVFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM;
RetDesc.DSVFormat = DXGI_FORMAT_D32_FLOAT;
RetDesc.SampleDesc.Count = 1;
return CreateDirect(RetDesc);
}
explicit CD3DX12_DEPTH_STENCIL_DESC( CD3DX12_DEFAULT )
{
DepthEnable = TRUE;
DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL;
DepthFunc = D3D12_COMPARISON_FUNC_LESS;
StencilEnable = FALSE;
StencilReadMask = D3D12_DEFAULT_STENCIL_READ_MASK;
StencilWriteMask = D3D12_DEFAULT_STENCIL_WRITE_MASK;
const D3D12_DEPTH_STENCILOP_DESC defaultStencilOp =
{ D3D12_STENCIL_OP_KEEP, D3D12_STENCIL_OP_KEEP, D3D12_STENCIL_OP_KEEP,
D3D12_COMPARISON_FUNC_ALWAYS };
FrontFace = defaultStencilOp;
BackFace = defaultStencilOp;
}
void CubeObject::UpdateConstantBuffer() {
//行列の定義
Mat4x4 World, View, Proj;
//ワールド行列の決定
World.affineTransformation(
m_Scale, //スケーリング
Vec3(0, 0, 0), //回転の中心(重心)
m_Qt, //回転角度
m_Pos //位置
);
//転置する
World.transpose();
//ビュー行列の決定
View = XMMatrixLookAtLH(Vec3(0, 2.0, -5.0f), Vec3(0, 0, 0), Vec3(0, 1.0f, 0));
//転置する
View.transpose();
//射影行列の決定
float w = static_cast<float>(App::GetApp()->GetGameWidth());
float h = static_cast<float>(App::GetApp()->GetGameHeight());
Proj = XMMatrixPerspectiveFovLH(XM_PIDIV4, w / h, 1.0f, 100.0f);
//転置する
Proj.transpose();
m_StaticConstantBuffer.World = World;
m_StaticConstantBuffer.View = View;
m_StaticConstantBuffer.Projection = Proj;
m_StaticConstantBuffer.Emissive = Col4(0, 0, 0, 0);
//更新
memcpy(m_pConstantBuffer, reinterpret_cast<void**>(&m_StaticConstantBuffer),
sizeof(m_StaticConstantBuffer));
}