Added follow cam

Added "Controlled" mesh classes
Added Global Lighting Class
Added Gamepad controls
Split terrain nodes into Height and Perlin classes
Fixed Splitmesh node stuff
This commit is contained in:
iDunnoDev
2022-05-09 17:50:22 +01:00
committed by iDunnoDev
parent bc906064e5
commit f6bba67897
58 changed files with 1743 additions and 351 deletions

View File

@ -1,8 +1,8 @@
#include "SubMeshRenderer.h"
void SubMeshRenderer::SetSubMesh(shared_ptr<SubMesh> subMesh)
void SubMeshRenderer::SetRootChildren(list<SceneNodePointer>& rootChildren)
{
_subMesh = subMesh;
_rootGraph = &rootChildren;
}
void SubMeshRenderer::SetWorldTransformation(FXMMATRIX worldTransformation)
@ -21,11 +21,6 @@ void SubMeshRenderer::SetDirectionalLight(FXMVECTOR lightVector, XMFLOAT4 lightC
XMStoreFloat4(&_directionalLightVector, lightVector);
}
void SubMeshRenderer::SetCameraPosition(XMFLOAT4 cameraPosition)
{
_cameraPosition = cameraPosition;
}
bool SubMeshRenderer::Initialise()
{
_device = DirectXFramework::GetDXFramework()->GetDevice();
@ -38,6 +33,60 @@ bool SubMeshRenderer::Initialise()
return true;
}
void SubMeshRenderer::RenderChild(SceneNodePointer node, bool renderTransparent)
{
shared_ptr<SubMeshNode> currentMeshNode = dynamic_pointer_cast<SubMeshNode>(node);
if (currentMeshNode != nullptr)
{
XMMATRIX projectionTransformation = DirectXFramework::GetDXFramework()->GetProjectionTransformation();
XMMATRIX viewTransformation = DirectXFramework::GetDXFramework()->GetCamera()->GetViewMatrix();
XMMATRIX completeTransformation = XMLoadFloat4x4(&currentMeshNode->GetCurrentTransform()) * viewTransformation * projectionTransformation;
shared_ptr<SubMesh> subMesh = currentMeshNode->GetSubMesh();
shared_ptr<Material> material = subMesh->GetMaterial();
float opacity = material->GetOpacity();
if ((renderTransparent && opacity < 1.0f) ||
(!renderTransparent && opacity == 1.0f))
{
_cBuffer.completeTransformation = completeTransformation;
_cBuffer.worldTransformation = XMLoadFloat4x4(&currentMeshNode->GetCurrentTransform());
UINT stride = sizeof(VERTEX);
UINT offset = 0;
_vertexBuffer = subMesh->GetVertexBuffer();
_deviceContext->IASetVertexBuffers(0, 1, _vertexBuffer.GetAddressOf(), &stride, &offset);
_indexBuffer = subMesh->GetIndexBuffer();
_deviceContext->IASetIndexBuffer(_indexBuffer.Get(), DXGI_FORMAT_R32_UINT, 0);
_cBuffer.diffuseCoefficient = material->GetDiffuseColour();
_cBuffer.specularCoefficient = material->GetSpecularColour();
_cBuffer.shininess = material->GetShininess();
_cBuffer.opacity = opacity;
// Update the constant buffer
_deviceContext->VSSetConstantBuffers(0, 1, _constantBuffer.GetAddressOf());
_deviceContext->UpdateSubresource(_constantBuffer.Get(), 0, 0, &_cBuffer, 0, 0);
_texture = material->GetTexture();
_deviceContext->PSSetShaderResources(0, 1, _texture.GetAddressOf());
_deviceContext->PSSetConstantBuffers(0, 1, _constantBuffer.GetAddressOf());
_deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
_deviceContext->DrawIndexed(static_cast<UINT>(subMesh->GetIndexCount()), 0, 0);
}
}
shared_ptr<SceneGraph> currentGraphNode = dynamic_pointer_cast<SceneGraph>(node);
if (currentGraphNode != nullptr)
{
// Render the children
// Loop through all submeshes in the mesh, rendering them
for (SceneNodePointer& child : currentGraphNode->GetChildren())
{
RenderChild(child, renderTransparent);
}
}
}
void SubMeshRenderer::Render()
{
// Turn off back face culling while we render a mesh.
@ -46,20 +95,12 @@ void SubMeshRenderer::Render()
// back face culling, some materials do not render correctly.
_deviceContext->RSSetState(_noCullRasteriserState.Get());
XMMATRIX projectionTransformation = DirectXFramework::GetDXFramework()->GetProjectionTransformation();
XMMATRIX viewTransformation = DirectXFramework::GetDXFramework()->GetCamera()->GetViewMatrix();
XMMATRIX completeTransformation = XMLoadFloat4x4(&_worldTransformation) * viewTransformation * projectionTransformation;
// Draw the first cube
CBUFFER cBuffer;
cBuffer.completeTransformation = completeTransformation;
cBuffer.worldTransformation = XMLoadFloat4x4(&_worldTransformation);
cBuffer.ambientColor = _ambientLight;
cBuffer.lightVector = XMVector4Normalize(XMLoadFloat4(&_directionalLightVector));
cBuffer.lightColor = _directionalLightColour;
cBuffer.cameraPosition = _cameraPosition;
// Reset the CBUFFER since its now a class member.
_cBuffer = CBUFFER();
_cBuffer.ambientColor = _ambientLight;
_cBuffer.lightVector = XMVector4Normalize(XMLoadFloat4(&_directionalLightVector));
_cBuffer.lightColor = _directionalLightColour;
XMStoreFloat4(&_cBuffer.cameraPosition, DirectXFramework::GetDXFramework()->GetCamera()->GetCameraPosition());
_deviceContext->VSSetShader(_vertexShader.Get(), 0, 0);
_deviceContext->PSSetShader(_pixelShader.Get(), 0, 0);
@ -69,27 +110,20 @@ void SubMeshRenderer::Render()
float blendFactors[] = { 0.0f, 0.0f, 0.0f, 0.0f };
_deviceContext->OMSetBlendState(_transparentBlendState.Get(), blendFactors, 0xffffffff);
shared_ptr<Material> material = _subMesh->GetMaterial();
float opacity = material->GetOpacity();
UINT stride = sizeof(VERTEX);
UINT offset = 0;
_vertexBuffer = _subMesh->GetVertexBuffer();
_deviceContext->IASetVertexBuffers(0, 1, _vertexBuffer.GetAddressOf(), &stride, &offset);
_indexBuffer = _subMesh->GetIndexBuffer();
_deviceContext->IASetIndexBuffer(_indexBuffer.Get(), DXGI_FORMAT_R32_UINT, 0);
cBuffer.diffuseCoefficient = material->GetDiffuseColour();
cBuffer.specularCoefficient = material->GetSpecularColour();
cBuffer.shininess = material->GetShininess();
cBuffer.opacity = opacity;
// Update the constant buffer
_deviceContext->VSSetConstantBuffers(0, 1, _constantBuffer.GetAddressOf());
_deviceContext->UpdateSubresource(_constantBuffer.Get(), 0, 0, &cBuffer, 0, 0);
_texture = material->GetTexture();
_deviceContext->PSSetShaderResources(0, 1, _texture.GetAddressOf());
_deviceContext->PSSetConstantBuffers(0, 1, _constantBuffer.GetAddressOf());
_deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
_deviceContext->DrawIndexed(static_cast<UINT>(_subMesh->GetIndexCount()), 0, 0);
// We do two passes through the nodes. The first time we render nodes
// that are not transparent (i.e. their opacity == 1.0f).
for (SceneNodePointer& child : *_rootGraph)
{
RenderChild(child, false);
}
// Now we render any transparent nodes
// We have to do this since blending always blends the submesh with
// whatever is in the render target. If we render a transparent node
// first, it will be opaque.
for (SceneNodePointer& child : *_rootGraph)
{
RenderChild(child, true);
}
// Turn back face culling back on in case another renderer
// relies on it
@ -202,3 +236,4 @@ void SubMeshRenderer::BuildRendererState()
rasteriserDesc.CullMode = D3D11_CULL_NONE;
ThrowIfFailed(_device->CreateRasterizerState(&rasteriserDesc, _noCullRasteriserState.GetAddressOf()));
}