Add shadows

This commit is contained in:
2025-01-17 18:16:29 +01:00
parent f413bf886b
commit 1ee5dd4ad0
13 changed files with 3898 additions and 3801 deletions

View File

@@ -1,2 +1,42 @@
# Blender 4.3.2 MTL File: 'None' # Blender 4.3.2 MTL File: 'None'
# www.blender.org # www.blender.org
newmtl Material.001
Ns 250.000000
Ka 1.000000 1.000000 1.000000
Kd 0.800119 0.001607 0.000000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.500000
d 1.000000
illum 2
newmtl Material.002
Ns 250.000000
Ka 1.000000 1.000000 1.000000
Kd 0.800149 0.000000 0.387990
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.500000
d 1.000000
illum 2
newmtl Material.003
Ns 250.000000
Ka 1.000000 1.000000 1.000000
Kd 0.003822 0.800111 0.000000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.500000
d 1.000000
illum 2
newmtl Material.004
Ns 250.000000
Ka 1.000000 1.000000 1.000000
Kd 0.800000 0.800000 0.800000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.500000
d 1.000000
illum 2

File diff suppressed because it is too large Load Diff

View File

@@ -77,26 +77,37 @@ float ShadowCalculation(float4 shadowCoord, float3 normal) {
// Normalize the depth by dividing by the w component // Normalize the depth by dividing by the w component
float3 projCoord = shadowCoord.xyz / shadowCoord.w; float3 projCoord = shadowCoord.xyz / shadowCoord.w;
float shadow = 0.0;
uint width, height;
gShadowMap.GetDimensions(width, height);
float2 texelSize = float2(1.f / width, 1.f / height);
// Convert from [-1, 1] to [0, 1] // Convert from [-1, 1] to [0, 1]
float2 UVCoords; float2 UVCoords;
UVCoords.x = 0.5f * projCoord.x + 0.5f; UVCoords.x = 0.5f * projCoord.x + 0.5f;
UVCoords.y = -0.5f * projCoord.y + 0.5f; UVCoords.y = -0.5f * projCoord.y + 0.5f;
// float z = 0.5 * projCoord.z + 0.5;
float z = projCoord.z; float z = projCoord.z;
// Sample the shadow map
float shadowDepth = gShadowMap.Sample(gShadowSampler2, UVCoords.xy).r;
// float bias = 0.0025f; float bias = max(0.05 * (1.0 - dot(normal, -gLightDirection)), 0.005);
float bias = max(0.05 * (1.0 - dot(normal, gLightDirection)), 0.005); for(int x = -1; x <= 1; ++x)
{
for(int y = -1; y <= 1; ++y)
{
float pcfDepth = gShadowMap.Sample(gShadowSampler2, UVCoords.xy + float2(x, y) * texelSize).r;
shadow += z - bias > pcfDepth ? 1.0 : 0.0;
}
}
// return gShadowMap.Sample(gShadowSampler2, UVCoords.xy).r; shadow /= 9.0f;
// Check if the current fragment is in shadow
if (shadowDepth + bias < z)
return 0.5f;
else
return 1.0f;
// float shadowDepth = gShadowMap.Sample(gShadowSampler2, UVCoords.xy).r;
if(projCoord.z > 1.0)
shadow = 0.0;
return shadow;
} }
// Pixel shader // Pixel shader
float4 PS(VS_OUTPUT input) : SV_TARGET { float4 PS(VS_OUTPUT input) : SV_TARGET {
@@ -104,7 +115,7 @@ float4 PS(VS_OUTPUT input) : SV_TARGET {
float3 diffuseColor = gDiffuseMap.Sample(gSampleState, input.TexCoord).rgb; float3 diffuseColor = gDiffuseMap.Sample(gSampleState, input.TexCoord).rgb;
float3 finalColor = float3(1.f, 1.f, 1.f) * shadowFactor; float3 finalColor = diffuseColor * (1 - shadowFactor);
finalColor += gAmbient; finalColor += gAmbient;
return float4(finalColor, 1.0f); return float4(finalColor, 1.0f);

Binary file not shown.

After

Width:  |  Height:  |  Size: 133 KiB

View File

@@ -27,9 +27,11 @@ dae::Matrix DirectionalLight::GetViewMatrix() const {
dae::Matrix DirectionalLight::GetProjectionMatrix(float nearPlane, float farPlane, float size) const { dae::Matrix DirectionalLight::GetProjectionMatrix(float nearPlane, float farPlane, float size) const {
return dae::Matrix::CreateOrthographic(size, size, nearPlane, farPlane); return dae::Matrix::CreateOrthographic(size, size, nearPlane, farPlane);
} }
dae::Matrix DirectionalLight::GetViewProjectionMatrix(float nearPlane, float farPlane, float size) const { dae::Matrix DirectionalLight::GetViewProjectionMatrix(float nearPlane, float farPlane, float size) const {
return GetViewMatrix() * GetProjectionMatrix(nearPlane, farPlane, size); return GetViewMatrix() * GetProjectionMatrix(nearPlane, farPlane, size);
} }
void DirectionalLight::Update() { void DirectionalLight::Update() {
// If the light position or target changes dynamically, this is where updates would be managed. // If the light position or target changes dynamically, this is where updates would be managed.
// m_ViewMatrix = dae::Matrix::CreateLookAtLH(m_Position, m_Target, m_Up); // m_ViewMatrix = dae::Matrix::CreateLookAtLH(m_Position, m_Target, m_Up);
@@ -43,3 +45,9 @@ dae::Vector3 DirectionalLight::GetTarget() {
dae::Vector3 DirectionalLight::GetPosition() { dae::Vector3 DirectionalLight::GetPosition() {
return m_Position; return m_Position;
} }
void DirectionalLight::SetColor(const dae::ColorRGB &color) { m_Color = color; }
dae::Vector3 DirectionalLight::GetDirection() const { return m_Target - m_Position; }
dae::ColorRGB DirectionalLight::GetColor() const { return m_Color; }

View File

@@ -4,6 +4,7 @@
#include "Math/Vector3.h" #include "Math/Vector3.h"
#include "Math/Matrix.h" #include "Math/Matrix.h"
#include "ColorRGB.h"
class DirectionalLight { class DirectionalLight {
public: public:
@@ -13,6 +14,7 @@ public:
void SetPosition(const dae::Vector3 &position); void SetPosition(const dae::Vector3 &position);
void SetTarget(const dae::Vector3 &target); void SetTarget(const dae::Vector3 &target);
void SetUp(const dae::Vector3 &up); void SetUp(const dae::Vector3 &up);
void SetColor(const dae::ColorRGB &color);
dae::Matrix GetViewMatrix() const; dae::Matrix GetViewMatrix() const;
dae::Matrix GetProjectionMatrix(float nearPlane = 0.1f, float farPlane = 100.0f, float size = 50.0f) const; dae::Matrix GetProjectionMatrix(float nearPlane = 0.1f, float farPlane = 100.0f, float size = 50.0f) const;
@@ -22,11 +24,15 @@ public:
dae::Vector3 GetTarget(); dae::Vector3 GetTarget();
dae::Vector3 GetPosition(); dae::Vector3 GetPosition();
dae::Vector3 GetDirection() const;
dae::ColorRGB GetColor() const;
private: private:
dae::Vector3 m_Position; dae::Vector3 m_Position;
dae::Vector3 m_Target; dae::Vector3 m_Target;
dae::Vector3 m_Up; dae::Vector3 m_Up;
dae::ColorRGB m_Color{1.f, 1.f, 1.f};
dae::Matrix m_ViewMatrix; dae::Matrix m_ViewMatrix;
dae::Matrix m_ProjectionMatrix; dae::Matrix m_ProjectionMatrix;
}; };

View File

@@ -54,7 +54,6 @@ ID3DX11Effect *BaseEffect::LoadEffect(ID3D11Device *devicePtr, const std::wstrin
ss << errorsPtr[i]; ss << errorsPtr[i];
OutputDebugStringW(ss.str().c_str()); OutputDebugStringW(ss.str().c_str());
errorBlobPtr->Release(); errorBlobPtr->Release();
errorBlobPtr = nullptr; errorBlobPtr = nullptr;
return nullptr; return nullptr;

View File

@@ -8,74 +8,80 @@ ShadowDiffuse::ShadowDiffuse(ID3D11Device *devicePtr, const std::wstring &filePa
: BaseEffect(devicePtr, filePath) { : BaseEffect(devicePtr, filePath) {
m_LightPosVariablePtr = m_EffectPtr->GetVariableByName("gLightDirection")->AsVector(); m_LightPosVariablePtr = m_EffectPtr->GetVariableByName("gLightDirection")->AsVector();
if(!m_LightPosVariablePtr->IsValid()) if (!m_LightPosVariablePtr->IsValid())
std::wcout << L"gLightDirection Vector is not valid" << std::endl; std::wcout << L"gLightDirection Vector is not valid" << std::endl;
m_SamplerVariablePtr = m_EffectPtr->GetVariableByName("gSampleState")->AsSampler(); m_SamplerVariablePtr = m_EffectPtr->GetVariableByName("gSampleState")->AsSampler();
if(!m_SamplerVariablePtr->IsValid()) if (!m_SamplerVariablePtr->IsValid())
std::wcout << L"gSampleState Sampler is not valid" << std::endl; std::wcout << L"gSampleState Sampler is not valid" << std::endl;
m_MatWorldVariablePtr = m_EffectPtr->GetVariableByName("gWorldMatrix")->AsMatrix(); m_MatWorldVariablePtr = m_EffectPtr->GetVariableByName("gWorldMatrix")->AsMatrix();
if(!m_MatWorldVariablePtr->IsValid()) if (!m_MatWorldVariablePtr->IsValid())
std::wcout << L"gWorld Matrix is not valid" << std::endl; std::wcout << L"gWorld Matrix is not valid" << std::endl;
m_DiffuseMapVariablePtr = m_EffectPtr->GetVariableByName("gDiffuseMap")->AsShaderResource(); m_DiffuseMapVariablePtr = m_EffectPtr->GetVariableByName("gDiffuseMap")->AsShaderResource();
if(!m_DiffuseMapVariablePtr->IsValid()) if (!m_DiffuseMapVariablePtr->IsValid())
std::wcout << L"gDiffuseMap ShaderResource is not valid" << std::endl; std::wcout << L"gDiffuseMap ShaderResource is not valid" << std::endl;
m_NormalMapVariablePtr = m_EffectPtr->GetVariableByName("gNormalMap")->AsShaderResource(); m_NormalMapVariablePtr = m_EffectPtr->GetVariableByName("gNormalMap")->AsShaderResource();
if(!m_NormalMapVariablePtr->IsValid()) if (!m_NormalMapVariablePtr->IsValid())
std::wcout << L"gNormalMap ShaderResource is not valid" << std::endl; std::wcout << L"gNormalMap ShaderResource is not valid" << std::endl;
m_SpecularMapVariablePtr = m_EffectPtr->GetVariableByName("gSpecularMap")->AsShaderResource(); m_SpecularMapVariablePtr = m_EffectPtr->GetVariableByName("gSpecularMap")->AsShaderResource();
if(!m_SpecularMapVariablePtr->IsValid()) if (!m_SpecularMapVariablePtr->IsValid())
std::wcout << L"gSpecularMap ShaderResource is not valid" << std::endl; std::wcout << L"gSpecularMap ShaderResource is not valid" << std::endl;
m_GlossMapVariablePtr = m_EffectPtr->GetVariableByName("gGlossMap")->AsShaderResource(); m_GlossMapVariablePtr = m_EffectPtr->GetVariableByName("gGlossMap")->AsShaderResource();
if(!m_GlossMapVariablePtr->IsValid()) if (!m_GlossMapVariablePtr->IsValid())
std::wcout << L"gGlossMap ShaderResource is not valid" << std::endl; std::wcout << L"gGlossMap ShaderResource is not valid" << std::endl;
m_CameraPosVariablePtr = m_EffectPtr->GetVariableByName("gCameraPosition")->AsVector(); m_CameraPosVariablePtr = m_EffectPtr->GetVariableByName("gCameraPosition")->AsVector();
if(!m_CameraPosVariablePtr->IsValid()) if (!m_CameraPosVariablePtr->IsValid())
std::wcout << L"gCameraPos Vector is not valid" << std::endl; std::wcout << L"gCameraPos Vector is not valid" << std::endl;
m_LightColorVariablePtr = m_EffectPtr->GetVariableByName("gLightColor")->AsVector(); m_LightColorVariablePtr = m_EffectPtr->GetVariableByName("gLightColor")->AsVector();
if(!m_LightColorVariablePtr->IsValid()) if (!m_LightColorVariablePtr->IsValid())
std::wcout << L"gLightColor Vector is not valid" << std::endl; std::wcout << L"gLightColor Vector is not valid" << std::endl;
m_UseNormalMapVariablePtr = m_EffectPtr->GetVariableByName("gUseNormal")->AsScalar(); m_UseNormalMapVariablePtr = m_EffectPtr->GetVariableByName("gUseNormal")->AsScalar();
if(!m_UseNormalMapVariablePtr->IsValid()) if (!m_UseNormalMapVariablePtr->IsValid())
std::wcout << L"gUseNormalMap Scalar is not valid" << std::endl; std::wcout << L"gUseNormalMap Scalar is not valid" << std::endl;
m_RasterizerVariablePtr = m_EffectPtr->GetVariableByName("gRasterizerState")->AsRasterizer(); m_RasterizerVariablePtr = m_EffectPtr->GetVariableByName("gRasterizerState")->AsRasterizer();
if(!m_RasterizerVariablePtr->IsValid()) if (!m_RasterizerVariablePtr->IsValid())
std::wcout << L"gRasterizerState Rasterizer is not valid" << std::endl; std::wcout << L"gRasterizerState Rasterizer is not valid" << std::endl;
m_ShadowMapVariablePtr = m_EffectPtr->GetVariableByName("gShadowMap")->AsShaderResource(); m_ShadowMapVariablePtr = m_EffectPtr->GetVariableByName("gShadowMap")->AsShaderResource();
if(!m_ShadowMapVariablePtr->IsValid()) if (!m_ShadowMapVariablePtr->IsValid())
std::wcout << L"gShadowMapSampler Sampler is not valid" << std::endl; std::wcout << L"gShadowMapSampler Sampler is not valid" << std::endl;
m_LightWorldViewProjVariablePtr = m_EffectPtr->GetVariableByName("gLightWorldViewProj")->AsMatrix(); m_LightWorldViewProjVariablePtr = m_EffectPtr->GetVariableByName("gLightWorldViewProj")->AsMatrix();
if(!m_LightWorldViewProjVariablePtr->IsValid()) if (!m_LightWorldViewProjVariablePtr->IsValid())
std::wcout << L"gLightWorldViewProj Matrix is not valid" << std::endl; std::wcout << L"gLightWorldViewProj Matrix is not valid" << std::endl;
m_LightDirectionVariablePtr = m_EffectPtr->GetVariableByName("gLightDirection")->AsVector();
if (!m_LightDirectionVariablePtr->IsValid())
std::wcout << L"gLightDirection Vector is not valid" << std::endl;
m_LightColorVariablePtr = m_EffectPtr->GetVariableByName("gLightColor")->AsVector();
if (!m_LightColorVariablePtr->IsValid())
std::wcout << L"gLightColor Vector is not valid" << std::endl;
m_UseNormalMapVariablePtr->SetBool(m_UseNormalMap); m_UseNormalMapVariablePtr->SetBool(m_UseNormalMap);
constexpr int vectorSize{4};
constexpr int vectorSize{ 4 }; constexpr float lightDirection[vectorSize]{.577f, -.577f, .577f, 0.f};
constexpr float lightDirection[vectorSize]{ .577f, -.577f, .577f, 0.f };
m_LightPosVariablePtr->SetFloatVector(lightDirection); m_LightPosVariablePtr->SetFloatVector(lightDirection);
constexpr float lightColor[vectorSize]{ 1.f, 1.f, 1.f, 1.f }; constexpr float lightColor[vectorSize]{1.f, 1.f, 1.f, 1.f};
m_LightColorVariablePtr->SetFloatVector(lightColor); m_LightColorVariablePtr->SetFloatVector(lightColor);
this->InitSamplers(devicePtr); this->InitSamplers(devicePtr);
this->InitRasterizer(devicePtr); this->InitRasterizer(devicePtr);
m_SamplerVariablePtr->SetSampler(0,m_SamplerStates[static_cast<int>(m_TechniqueType)]); m_SamplerVariablePtr->SetSampler(0, m_SamplerStates[static_cast<int>(m_TechniqueType)]);
m_RasterizerVariablePtr->SetRasterizerState(0,m_RasterizerStates[static_cast<int>(m_CullMode)]); m_RasterizerVariablePtr->SetRasterizerState(0, m_RasterizerStates[static_cast<int>(m_CullMode)]);
} }
ShadowDiffuse::~ShadowDiffuse() { ShadowDiffuse::~ShadowDiffuse() {
@@ -112,21 +118,24 @@ ShadowDiffuse::~ShadowDiffuse() {
m_RasterizerVariablePtr->Release(); m_RasterizerVariablePtr->Release();
m_RasterizerVariablePtr = nullptr; m_RasterizerVariablePtr = nullptr;
if(m_ShadowMapVariablePtr){ m_LightDirectionVariablePtr->Release();
m_LightDirectionVariablePtr = nullptr;
if (m_ShadowMapVariablePtr) {
m_ShadowMapVariablePtr->Release(); m_ShadowMapVariablePtr->Release();
m_ShadowMapVariablePtr = nullptr; m_ShadowMapVariablePtr = nullptr;
} }
if (m_LightWorldViewProjVariablePtr) {
if(m_LightWorldViewProjVariablePtr){
m_LightWorldViewProjVariablePtr->Release(); m_LightWorldViewProjVariablePtr->Release();
m_LightWorldViewProjVariablePtr = nullptr; m_LightWorldViewProjVariablePtr = nullptr;
} }
for(auto sampler : m_SamplerStates){
for (auto sampler: m_SamplerStates) {
sampler->Release(); sampler->Release();
} }
for(auto rasterizer : m_RasterizerStates){ for (auto rasterizer: m_RasterizerStates) {
rasterizer->Release(); rasterizer->Release();
} }
@@ -135,27 +144,26 @@ ShadowDiffuse::~ShadowDiffuse() {
} }
void ShadowDiffuse::SetWorldMatrix(const dae::Matrix &world) const { void ShadowDiffuse::SetWorldMatrix(const dae::Matrix &world) const {
m_MatWorldVariablePtr->SetMatrix(reinterpret_cast<const float*>(&world)); m_MatWorldVariablePtr->SetMatrix(reinterpret_cast<const float *>(&world));
} }
void ShadowDiffuse::SetMaterial(Material* material) { void ShadowDiffuse::SetMaterial(Material *material) {
if(material->diffuseTexturePtr) if (material->diffuseTexturePtr)
m_DiffuseMapVariablePtr->SetResource(material->diffuseTexturePtr->GetSrv()); m_DiffuseMapVariablePtr->SetResource(material->diffuseTexturePtr->GetSrv());
if(material->normalTexturePtr) if (material->normalTexturePtr)
m_NormalMapVariablePtr->SetResource(material->normalTexturePtr->GetSrv()); m_NormalMapVariablePtr->SetResource(material->normalTexturePtr->GetSrv());
if(material->specularTexturePtr) if (material->specularTexturePtr)
m_SpecularMapVariablePtr->SetResource(material->specularTexturePtr->GetSrv()); m_SpecularMapVariablePtr->SetResource(material->specularTexturePtr->GetSrv());
if(material->glossTexturePtr) if (material->glossTexturePtr)
m_GlossMapVariablePtr->SetResource(material->glossTexturePtr->GetSrv()); m_GlossMapVariablePtr->SetResource(material->glossTexturePtr->GetSrv());
} }
void ShadowDiffuse::SetCameraPos(const dae::Vector3 &pos) const { void ShadowDiffuse::SetCameraPos(const dae::Vector3 &pos) const {
m_CameraPosVariablePtr->SetFloatVector(reinterpret_cast<const float*>(&pos)); m_CameraPosVariablePtr->SetFloatVector(reinterpret_cast<const float *>(&pos));
} }
void ShadowDiffuse::NextSamplingState() { void ShadowDiffuse::NextSamplingState() {
switch(m_TechniqueType){ switch (m_TechniqueType) {
case TechniqueType::Point: case TechniqueType::Point:
m_TechniqueType = TechniqueType::Linear; m_TechniqueType = TechniqueType::Linear;
break; break;
@@ -166,7 +174,7 @@ void ShadowDiffuse::NextSamplingState() {
m_TechniqueType = TechniqueType::Point; m_TechniqueType = TechniqueType::Point;
break; break;
} }
m_SamplerVariablePtr->SetSampler(0,m_SamplerStates[static_cast<int>(m_TechniqueType)]); m_SamplerVariablePtr->SetSampler(0, m_SamplerStates[static_cast<int>(m_TechniqueType)]);
} }
void ShadowDiffuse::ToggleNormals() { void ShadowDiffuse::ToggleNormals() {
@@ -190,12 +198,12 @@ void ShadowDiffuse::InitSamplers(ID3D11Device *devicePtr) {
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT; samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
devicePtr->CreateSamplerState(&samplerDesc, &m_SamplerStates[static_cast<int>(TechniqueType::Point)]); devicePtr->CreateSamplerState(&samplerDesc, &m_SamplerStates[static_cast<int>(TechniqueType::Point)]);
m_SamplerVariablePtr->SetSampler(0,m_SamplerStates[static_cast<int>(m_TechniqueType)]); m_SamplerVariablePtr->SetSampler(0, m_SamplerStates[static_cast<int>(m_TechniqueType)]);
} }
void ShadowDiffuse::NextCullMode() { void ShadowDiffuse::NextCullMode() {
m_CullMode = static_cast<CullMode>((static_cast<int>(m_CullMode) + 1) % 3); m_CullMode = static_cast<CullMode>((static_cast<int>(m_CullMode) + 1) % 3);
m_RasterizerVariablePtr->SetRasterizerState(0,m_RasterizerStates[static_cast<int>(m_CullMode)]); m_RasterizerVariablePtr->SetRasterizerState(0, m_RasterizerStates[static_cast<int>(m_CullMode)]);
} }
void ShadowDiffuse::InitRasterizer(ID3D11Device *devicePtr) { void ShadowDiffuse::InitRasterizer(ID3D11Device *devicePtr) {
@@ -227,9 +235,13 @@ void ShadowDiffuse::SetShadowMap(ID3D11ShaderResourceView *shadowMapSRV) {
} }
void ShadowDiffuse::SetLightViewProjMatrix(Matrix matrix) { void ShadowDiffuse::SetLightViewProjMatrix(Matrix matrix) {
m_LightWorldViewProjVariablePtr->SetMatrix(reinterpret_cast<const float*>(&matrix)); m_LightWorldViewProjVariablePtr->SetMatrix(reinterpret_cast<const float *>(&matrix));
} }
void ShadowDiffuse::SetLightDirection(Vector3 direction) { void ShadowDiffuse::SetLightDirection(Vector3 direction) {
m_LightDirectionVariablePtr->SetFloatVector(reinterpret_cast<const float *>(&direction));
}
void ShadowDiffuse::SetLightColor(ColorRGB rgb) {
m_LightColorVariablePtr->SetFloatVector(reinterpret_cast<const float *>(&rgb));
} }

View File

@@ -1,14 +1,9 @@
#pragma once #pragma once
#include "BaseEffect.h" #include "BaseEffect.h"
#include "Effect.h"
#include <array> #include <array>
enum class CullMode {
None = 0,
Front = 1,
Back = 2
};
class ShadowDiffuse final: public BaseEffect { class ShadowDiffuse final: public BaseEffect {
public: public:
ShadowDiffuse(ID3D11Device *devicePtr, const std::wstring &filePath); ShadowDiffuse(ID3D11Device *devicePtr, const std::wstring &filePath);
@@ -33,8 +28,9 @@ public:
void SetLightDirection(Vector3 direction); void SetLightDirection(Vector3 direction);
private: void SetLightColor(ColorRGB rgb);
private:
void InitSamplers(ID3D11Device* devicePtr); void InitSamplers(ID3D11Device* devicePtr);
void InitRasterizer(ID3D11Device* devicePtr); void InitRasterizer(ID3D11Device* devicePtr);
@@ -48,6 +44,7 @@ private:
ID3DX11EffectVectorVariable *m_CameraPosVariablePtr{}; ID3DX11EffectVectorVariable *m_CameraPosVariablePtr{};
ID3DX11EffectVectorVariable *m_LightPosVariablePtr{}; ID3DX11EffectVectorVariable *m_LightPosVariablePtr{};
ID3DX11EffectVectorVariable *m_LightColorVariablePtr{}; ID3DX11EffectVectorVariable *m_LightColorVariablePtr{};
ID3DX11EffectVectorVariable *m_LightDirectionVariablePtr{};
ID3DX11EffectScalarVariable *m_UseNormalMapVariablePtr{}; ID3DX11EffectScalarVariable *m_UseNormalMapVariablePtr{};

View File

@@ -28,6 +28,7 @@ enum class SceneNames{
Diorama, Diorama,
Instanced, Instanced,
Planet, Planet,
ShadowTest,
Count Count
}; };

View File

@@ -16,31 +16,32 @@ void ShadowTestScene::Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *D
assert(true && "Shadow map buffer failed to initialize"); assert(true && "Shadow map buffer failed to initialize");
} }
// Initialize light source // Initialize light source
m_Light.SetPosition({-20.0f, 30.0f, 0.0f}); m_Light.SetPosition({-20.0f, 60.0f, 0.0f});
m_Light.SetTarget({0, -5, 40}); m_Light.SetTarget({0, -5, 40});
m_Light.SetUp({0.0f, 1.0f, 0.0f}); m_Light.SetUp({0.0f, 1.0f, 0.0f});
std::vector<VertexIn> vertices{}; std::vector<VertexIn> vertices{};
std::vector<uint32_t> indices{}; std::vector<uint32_t> indices{};
if(!Utils::ParseOBJNew("resources/ShadingDemo.obj", vertices, indices, true)){ if(!Utils::ParseOBJNew("resources/ShadingDemo.obj", vertices, indices, true)){
assert(true && "Model failed to load"); assert(true && "Model failed to load");
} }
auto material = std::make_shared<Material>(); auto material = std::make_shared<Material>();
// material->diffuseTexturePtr = Texture::LoadFromFile("resources/vehicle_diffuse.png", DevicePtr); material->diffuseTexturePtr = Texture::LoadFromFile("resources/shadowTestTexture.png", DevicePtr);
auto shadowEffect = new ShadowEffect(DevicePtr, L"resources/shadowEffect.fx"); auto shadowEffect = new ShadowEffect(DevicePtr, L"resources/shadowEffect.fx");
auto otherEffect = new Effect(DevicePtr, L"resources/SuperSimpleDiffuse.fx"); auto otherEffect = new ShadowDiffuse(DevicePtr, L"resources/SuperSimpleDiffuse.fx");
auto shadowMesh = new ShadowMesh(DevicePtr, vertices, indices, material, shadowEffect, otherEffect); auto shadowMesh = new ShadowMesh(DevicePtr, vertices, indices, material, shadowEffect, otherEffect);
//
// std::vector<std::unique_ptr<Utils::MaterialMesh>> materialMeshes; // std::vector<std::unique_ptr<Utils::MaterialMesh>> materialMeshes;
// Utils::LoadObjWithMaterials("resources/scene.obj", materialMeshes, true, DevicePtr); // Utils::LoadObjWithMaterials("resources/scene.obj", materialMeshes, true, DevicePtr);
// for (const auto &mesh: materialMeshes) { // for (const auto &mesh: materialMeshes) {
// if (mesh->vertices.size() > 0) { // if (mesh->vertices.size() > 0) {
// std::shared_ptr<Material> material = std::make_shared<Material>(); // std::shared_ptr<Material> material = std::make_shared<Material>();
// BaseEffect *effect = new Effect(DevicePtr, L"resources/SuperSimpleDiffuse.fx"); // auto* effect = new ShadowDiffuse(DevicePtr, L"resources/SuperSimpleDiffuse.fx");
// ShadowEffect* shadowEffect = new ShadowEffect(DevicePtr, L"resources/shadowEffect.fx"); // ShadowEffect* shadowEffect = new ShadowEffect(DevicePtr, L"resources/shadowEffect.fx");
// material->diffuseTexturePtr = Texture::LoadFromFile("./resources/diorama/" + mesh->diffuse_texture, DevicePtr); // material->diffuseTexturePtr = Texture::LoadFromFile("./resources/diorama/" + mesh->diffuse_texture, DevicePtr);
// //
@@ -130,12 +131,12 @@ void ShadowTestScene::Render(ID3D11DeviceContext *devicePtr, ID3D11RenderTargetV
auto* effect = dynamic_cast<ShadowMesh*>(mesh)->GetColorEffectPtr() ; auto* effect = dynamic_cast<ShadowMesh*>(mesh)->GetColorEffectPtr() ;
dynamic_cast<Effect*>(effect)->SetWorldViewProjMatrix(worldViewProj); effect->SetWorldViewProjMatrix(worldViewProj);
dynamic_cast<Effect*>(effect)->SetWorldMatrix(mesh->GetWorldMatrix()); effect->SetWorldMatrix(mesh->GetWorldMatrix());
dynamic_cast<Effect*>(effect)->SetShadowMap(shadowMap); effect->SetShadowMap(shadowMap);
dynamic_cast<Effect*>(effect)->SetLightViewProjMatrix(lightWorldViewProj); effect->SetLightViewProjMatrix(lightWorldViewProj);
effect->SetLightDirection(m_Light.GetDirection());
effect->SetLightColor(m_Light.GetColor());
dynamic_cast<ShadowMesh*>(mesh)->RenderFinal(devicePtr, worldViewProj); dynamic_cast<ShadowMesh*>(mesh)->RenderFinal(devicePtr, worldViewProj);
} }
} }

View File

@@ -8,7 +8,7 @@ ShadowMesh::ShadowMesh(ID3D11Device* devicePtr,
const std::vector<Uint32>& indices, const std::vector<Uint32>& indices,
std::shared_ptr<Material> material, std::shared_ptr<Material> material,
ShadowEffect* shadowEffectPtr, ShadowEffect* shadowEffectPtr,
BaseEffect* colorEffectPtr) ShadowDiffuse* colorEffectPtr)
: m_ShadowEffectPtr(shadowEffectPtr), : m_ShadowEffectPtr(shadowEffectPtr),
m_ColorEffectPtr(colorEffectPtr), m_ColorEffectPtr(colorEffectPtr),
m_VerticesIn(verticesIn), m_VerticesIn(verticesIn),

View File

@@ -7,6 +7,7 @@
#include "Effects/ShadowEffect.h" #include "Effects/ShadowEffect.h"
#include "Material.h" #include "Material.h"
#include "Mesh.h" #include "Mesh.h"
#include "Effects/ShadowDiffuse.h"
class ShadowMesh { class ShadowMesh {
public: public:
@@ -15,7 +16,7 @@ public:
const std::vector<Uint32>& indices, const std::vector<Uint32>& indices,
std::shared_ptr<Material> material, std::shared_ptr<Material> material,
ShadowEffect* shadowEffectPtr, ShadowEffect* shadowEffectPtr,
BaseEffect* colorEffectPtr); ShadowDiffuse* colorEffectPtr);
~ShadowMesh(); ~ShadowMesh();
@@ -26,7 +27,7 @@ public:
Matrix GetWorldMatrix() const; Matrix GetWorldMatrix() const;
ShadowEffect* GetShadowEffectPtr(){ return m_ShadowEffectPtr; } ShadowEffect* GetShadowEffectPtr(){ return m_ShadowEffectPtr; }
BaseEffect* GetColorEffectPtr(){ return m_ColorEffectPtr; } ShadowDiffuse* GetColorEffectPtr(){ return m_ColorEffectPtr; }
private: private:
@@ -34,7 +35,7 @@ private:
ShadowEffect* m_ShadowEffectPtr; ShadowEffect* m_ShadowEffectPtr;
// Color effect for final render // Color effect for final render
BaseEffect* m_ColorEffectPtr; ShadowDiffuse* m_ColorEffectPtr;
// Buffers // Buffers
ID3D11InputLayout* m_InputLayoutPtr; ID3D11InputLayout* m_InputLayoutPtr;