@@ -14,6 +14,7 @@ Mesh::Mesh(std::string name, Transform& parentTransform) : Component::Component(
1414 this ->m_bMeshLoaded = false ;
1515 this ->m_nTotalVertices = 0 ;
1616 this ->m_nSamplerIndex = -1 ; // I put -1 cuz 0 can actually be occupied.
17+ this ->m_nWvpIndex = -1 ;
1718 this ->m_nTotalVertices = 0 ;
1819 this ->m_shader = nullptr ;
1920
@@ -27,8 +28,8 @@ Mesh::Mesh(std::string name, Transform& parentTransform) : Component::Component(
2728 this ->m_wvp .World *= XMMatrixTranspose (XMMatrixRotationY (XMConvertToRadians (this ->m_transform .rotation .y )));
2829 this ->m_wvp .World *= XMMatrixTranspose (XMMatrixRotationZ (XMConvertToRadians (this ->m_transform .rotation .z )));
2930
30- this ->m_wvp .View = XMMatrixTranspose (XMMatrixIdentity ());
31- this ->m_wvp .Projection = XMMatrixTranspose (XMMatrixPerspectiveLH (XMConvertToRadians (90 .f ), static_cast <float >(nWidth) / static_cast <float >(nHeight), 0 .01f , 3000 .f ));
31+ this ->m_wvp .View = XMMatrixTranspose (XMMatrixIdentity () * XMMatrixTranslation ( 0 . f , 0 . f , 2 . f ) );
32+ this ->m_wvp .Projection = XMMatrixTranspose (XMMatrixPerspectiveFovLH (XMConvertToRadians (90 .f ), static_cast <float >(nWidth) / static_cast <float >(nHeight), 0 .01f , 3000 .f ));
3233}
3334
3435void Mesh::Init () {
@@ -80,17 +81,58 @@ void Mesh::UploadVertices() {
8081 }
8182}
8283
84+ void Mesh::InitConstantBuffer () {
85+ D3D12* d3d12 = dynamic_cast <D3D12*>(this ->m_renderer );
86+
87+ UINT nWVPSize = (sizeof (this ->m_wvp ) + 255 ) & ~255 ;
88+
89+ d3d12->CreateBuffer (&this ->m_wvp , nWVPSize, this ->m_wvpRes );
90+ this ->m_wvpRes ->SetName (L" Mesh WVP Constant buffer" );
91+
92+ D3D12_CONSTANT_BUFFER_VIEW_DESC cbvDesc = { };
93+ cbvDesc.BufferLocation = this ->m_wvpRes ->GetGPUVirtualAddress ();
94+ cbvDesc.SizeInBytes = nWVPSize;
95+
96+ this ->m_cbv_srvHeap ->Allocate (1 );
97+ this ->m_nWvpIndex = this ->m_cbv_srvHeap ->GetLastDescriptorIndex ();
98+ Descriptor wvpDesc = this ->m_cbv_srvHeap ->GetDescriptor (this ->m_nWvpIndex );
99+
100+ this ->m_dev ->CreateConstantBufferView (&cbvDesc, wvpDesc.cpuHandle );
101+ }
102+
103+ void Mesh::UpdateConstantBuffer () {
104+ UINT nWVPSize = (sizeof (this ->m_wvp ) + 255 ) & ~255 ;
105+ this ->m_wvp .World = XMMatrixTranspose (XMMatrixIdentity () * XMMatrixTranslation (this ->m_transform .location .x , this ->m_transform .location .y , this ->m_transform .location .z ));
106+ this ->m_wvp .World *= XMMatrixTranspose (XMMatrixRotationX (XMConvertToRadians (this ->m_transform .rotation .x )));
107+ this ->m_wvp .World *= XMMatrixTranspose (XMMatrixRotationY (XMConvertToRadians (this ->m_transform .rotation .y )));
108+ this ->m_wvp .World *= XMMatrixTranspose (XMMatrixRotationZ (XMConvertToRadians (this ->m_transform .rotation .z )));
109+
110+ this ->m_wvp .View = XMMatrixTranspose (XMMatrixIdentity () * XMMatrixTranslation (0 .f , 0 .f , 2 .f ));
111+ // this->m_wvp.Projection = XMMatrixTranspose(XMMatrixPerspectiveLH(XMConvertToRadians(90.f), static_cast<float>(nWidth) / static_cast<float>(nHeight), 0.01f, 3000.f));
112+
113+ PVOID pData;
114+ ThrowIfFailed (this ->m_wvpRes ->Map (0 , nullptr , &pData));
115+ memcpy (pData, &this ->m_wvp , nWVPSize);
116+ this ->m_wvpRes ->Unmap (0 , nullptr );
117+
118+ return ;
119+ }
120+
83121void Mesh::Update () {
84122 Component::Update ();
85123
86124}
87125
88126void Mesh::Render () {
127+ this ->UpdateConstantBuffer ();
89128 this ->m_list ->IASetPrimitiveTopology (D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
90129 this ->m_list ->SetPipelineState (this ->m_plState .Get ());
91130 this ->m_list ->SetGraphicsRootSignature (this ->m_rootSig .Get ());
92131
132+ Descriptor wvpDesc = this ->m_cbv_srvHeap ->GetDescriptor (this ->m_nWvpIndex );
133+
93134 this ->m_list ->SetGraphicsRootDescriptorTable (0 , this ->m_samplerDescriptor .gpuHandle );
135+ this ->m_list ->SetGraphicsRootDescriptorTable (2 , wvpDesc.gpuHandle );
94136
95137 int i = 0 ;
96138 for (D3D12_VERTEX_BUFFER_VIEW vbv : this ->m_VBVs ) {
@@ -114,17 +156,22 @@ void Mesh::InitPipeline() {
114156
115157 CD3DX12_DESCRIPTOR_RANGE albedoRange;
116158 CD3DX12_DESCRIPTOR_RANGE samplerRange;
159+ CD3DX12_DESCRIPTOR_RANGE wvpRange;
117160 albedoRange.Init (D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 1 , 0 );
118161 samplerRange.Init (D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER, 1 , 0 );
162+ wvpRange.Init (D3D12_DESCRIPTOR_RANGE_TYPE_CBV, 1 , 0 );
119163
120164 CD3DX12_ROOT_PARAMETER albedoParam;
121165 CD3DX12_ROOT_PARAMETER samplerParam;
166+ CD3DX12_ROOT_PARAMETER wvpParam;
122167 albedoParam.InitAsDescriptorTable (1 , &albedoRange, D3D12_SHADER_VISIBILITY_PIXEL);
123168 samplerParam.InitAsDescriptorTable (1 , &samplerRange, D3D12_SHADER_VISIBILITY_PIXEL);
124-
169+ wvpParam.InitAsDescriptorTable (1 , &wvpRange, D3D12_SHADER_VISIBILITY_VERTEX);
170+
125171 D3D12_ROOT_PARAMETER rootParams[] = {
126172 samplerParam,
127- albedoParam
173+ albedoParam,
174+ wvpParam
128175 };
129176
130177 D3D12_ROOT_SIGNATURE_DESC rootDesc = { };
@@ -169,7 +216,7 @@ void Mesh::InitPipeline() {
169216 plDesc.DSVFormat = DXGI_FORMAT_D24_UNORM_S8_UINT;
170217 plDesc.RasterizerState = CD3DX12_RASTERIZER_DESC (D3D12_DEFAULT);
171218 plDesc.RasterizerState .CullMode = D3D12_CULL_MODE_BACK;
172- plDesc.RasterizerState .FrontCounterClockwise = TRUE ;
219+ plDesc.RasterizerState .FrontCounterClockwise = FALSE ;
173220 plDesc.RTVFormats [0 ] = DXGI_FORMAT_B8G8R8A8_UNORM;
174221 plDesc.RTVFormats [1 ] = DXGI_FORMAT_B8G8R8A8_UNORM;
175222 plDesc.RTVFormats [2 ] = DXGI_FORMAT_B8G8R8A8_UNORM;
@@ -194,6 +241,7 @@ void Mesh::D3D12Init(D3D12* renderer) {
194241 this ->UploadVertices ();
195242 this ->InitPipeline ();
196243 this ->InitSampler (renderer);
244+ this ->InitConstantBuffer ();
197245}
198246
199247void Mesh::InitSampler (D3D12* renderer) {
0 commit comments