Add shadows
This commit is contained in:
@@ -1,2 +1,42 @@
|
||||
# Blender 4.3.2 MTL File: 'None'
|
||||
# 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
@@ -77,26 +77,37 @@ float ShadowCalculation(float4 shadowCoord, float3 normal) {
|
||||
// Normalize the depth by dividing by the w component
|
||||
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]
|
||||
float2 UVCoords;
|
||||
UVCoords.x = 0.5f * projCoord.x + 0.5f;
|
||||
UVCoords.y = -0.5f * projCoord.y + 0.5f;
|
||||
// float z = 0.5 * projCoord.z + 0.5;
|
||||
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;
|
||||
// Check if the current fragment is in shadow
|
||||
if (shadowDepth + bias < z)
|
||||
return 0.5f;
|
||||
else
|
||||
return 1.0f;
|
||||
shadow /= 9.0f;
|
||||
|
||||
// float shadowDepth = gShadowMap.Sample(gShadowSampler2, UVCoords.xy).r;
|
||||
|
||||
|
||||
if(projCoord.z > 1.0)
|
||||
shadow = 0.0;
|
||||
|
||||
return shadow;
|
||||
}
|
||||
// Pixel shader
|
||||
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 finalColor = float3(1.f, 1.f, 1.f) * shadowFactor;
|
||||
float3 finalColor = diffuseColor * (1 - shadowFactor);
|
||||
finalColor += gAmbient;
|
||||
|
||||
return float4(finalColor, 1.0f);
|
||||
|
||||
BIN
project/resources/shadowTestTexture.png
Normal file
BIN
project/resources/shadowTestTexture.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 133 KiB |
@@ -27,9 +27,11 @@ dae::Matrix DirectionalLight::GetViewMatrix() const {
|
||||
dae::Matrix DirectionalLight::GetProjectionMatrix(float nearPlane, float farPlane, float size) const {
|
||||
return dae::Matrix::CreateOrthographic(size, size, nearPlane, farPlane);
|
||||
}
|
||||
|
||||
dae::Matrix DirectionalLight::GetViewProjectionMatrix(float nearPlane, float farPlane, float size) const {
|
||||
return GetViewMatrix() * GetProjectionMatrix(nearPlane, farPlane, size);
|
||||
}
|
||||
|
||||
void DirectionalLight::Update() {
|
||||
// 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);
|
||||
@@ -43,3 +45,9 @@ dae::Vector3 DirectionalLight::GetTarget() {
|
||||
dae::Vector3 DirectionalLight::GetPosition() {
|
||||
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; }
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#include "Math/Vector3.h"
|
||||
#include "Math/Matrix.h"
|
||||
#include "ColorRGB.h"
|
||||
|
||||
class DirectionalLight {
|
||||
public:
|
||||
@@ -13,6 +14,7 @@ public:
|
||||
void SetPosition(const dae::Vector3 &position);
|
||||
void SetTarget(const dae::Vector3 &target);
|
||||
void SetUp(const dae::Vector3 &up);
|
||||
void SetColor(const dae::ColorRGB &color);
|
||||
|
||||
dae::Matrix GetViewMatrix() 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 GetPosition();
|
||||
dae::Vector3 GetDirection() const;
|
||||
dae::ColorRGB GetColor() const;
|
||||
private:
|
||||
dae::Vector3 m_Position;
|
||||
dae::Vector3 m_Target;
|
||||
dae::Vector3 m_Up;
|
||||
|
||||
dae::ColorRGB m_Color{1.f, 1.f, 1.f};
|
||||
|
||||
dae::Matrix m_ViewMatrix;
|
||||
dae::Matrix m_ProjectionMatrix;
|
||||
};
|
||||
|
||||
@@ -54,7 +54,6 @@ ID3DX11Effect *BaseEffect::LoadEffect(ID3D11Device *devicePtr, const std::wstrin
|
||||
ss << errorsPtr[i];
|
||||
OutputDebugStringW(ss.str().c_str());
|
||||
|
||||
|
||||
errorBlobPtr->Release();
|
||||
errorBlobPtr = nullptr;
|
||||
return nullptr;
|
||||
|
||||
@@ -60,10 +60,16 @@ ShadowDiffuse::ShadowDiffuse(ID3D11Device *devicePtr, const std::wstring &filePa
|
||||
if (!m_LightWorldViewProjVariablePtr->IsValid())
|
||||
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);
|
||||
|
||||
|
||||
constexpr int vectorSize{4};
|
||||
constexpr float lightDirection[vectorSize]{.577f, -.577f, .577f, 0.f};
|
||||
m_LightPosVariablePtr->SetFloatVector(lightDirection);
|
||||
@@ -112,16 +118,19 @@ ShadowDiffuse::~ShadowDiffuse() {
|
||||
m_RasterizerVariablePtr->Release();
|
||||
m_RasterizerVariablePtr = nullptr;
|
||||
|
||||
m_LightDirectionVariablePtr->Release();
|
||||
m_LightDirectionVariablePtr = nullptr;
|
||||
|
||||
if (m_ShadowMapVariablePtr) {
|
||||
m_ShadowMapVariablePtr->Release();
|
||||
m_ShadowMapVariablePtr = nullptr;
|
||||
}
|
||||
|
||||
if (m_LightWorldViewProjVariablePtr) {
|
||||
m_LightWorldViewProjVariablePtr->Release();
|
||||
m_LightWorldViewProjVariablePtr = nullptr;
|
||||
}
|
||||
|
||||
|
||||
for (auto sampler: m_SamplerStates) {
|
||||
sampler->Release();
|
||||
}
|
||||
@@ -149,7 +158,6 @@ void ShadowDiffuse::SetMaterial(Material* material) {
|
||||
m_GlossMapVariablePtr->SetResource(material->glossTexturePtr->GetSrv());
|
||||
}
|
||||
|
||||
|
||||
void ShadowDiffuse::SetCameraPos(const dae::Vector3 &pos) const {
|
||||
m_CameraPosVariablePtr->SetFloatVector(reinterpret_cast<const float *>(&pos));
|
||||
}
|
||||
@@ -231,5 +239,9 @@ void ShadowDiffuse::SetLightViewProjMatrix(Matrix matrix) {
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
@@ -1,14 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include "BaseEffect.h"
|
||||
#include "Effect.h"
|
||||
#include <array>
|
||||
|
||||
enum class CullMode {
|
||||
None = 0,
|
||||
Front = 1,
|
||||
Back = 2
|
||||
};
|
||||
|
||||
class ShadowDiffuse final: public BaseEffect {
|
||||
public:
|
||||
ShadowDiffuse(ID3D11Device *devicePtr, const std::wstring &filePath);
|
||||
@@ -33,8 +28,9 @@ public:
|
||||
|
||||
void SetLightDirection(Vector3 direction);
|
||||
|
||||
private:
|
||||
void SetLightColor(ColorRGB rgb);
|
||||
|
||||
private:
|
||||
void InitSamplers(ID3D11Device* devicePtr);
|
||||
void InitRasterizer(ID3D11Device* devicePtr);
|
||||
|
||||
@@ -48,6 +44,7 @@ private:
|
||||
ID3DX11EffectVectorVariable *m_CameraPosVariablePtr{};
|
||||
ID3DX11EffectVectorVariable *m_LightPosVariablePtr{};
|
||||
ID3DX11EffectVectorVariable *m_LightColorVariablePtr{};
|
||||
ID3DX11EffectVectorVariable *m_LightDirectionVariablePtr{};
|
||||
|
||||
ID3DX11EffectScalarVariable *m_UseNormalMapVariablePtr{};
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@ enum class SceneNames{
|
||||
Diorama,
|
||||
Instanced,
|
||||
Planet,
|
||||
ShadowTest,
|
||||
|
||||
Count
|
||||
};
|
||||
|
||||
@@ -16,31 +16,32 @@ void ShadowTestScene::Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *D
|
||||
assert(true && "Shadow map buffer failed to initialize");
|
||||
}
|
||||
// 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.SetUp({0.0f, 1.0f, 0.0f});
|
||||
|
||||
std::vector<VertexIn> vertices{};
|
||||
std::vector<uint32_t> indices{};
|
||||
|
||||
if(!Utils::ParseOBJNew("resources/ShadingDemo.obj", vertices, indices, true)){
|
||||
assert(true && "Model failed to load");
|
||||
}
|
||||
|
||||
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 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);
|
||||
|
||||
//
|
||||
|
||||
// std::vector<std::unique_ptr<Utils::MaterialMesh>> materialMeshes;
|
||||
// Utils::LoadObjWithMaterials("resources/scene.obj", materialMeshes, true, DevicePtr);
|
||||
// for (const auto &mesh: materialMeshes) {
|
||||
// if (mesh->vertices.size() > 0) {
|
||||
// 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");
|
||||
// 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() ;
|
||||
|
||||
dynamic_cast<Effect*>(effect)->SetWorldViewProjMatrix(worldViewProj);
|
||||
dynamic_cast<Effect*>(effect)->SetWorldMatrix(mesh->GetWorldMatrix());
|
||||
dynamic_cast<Effect*>(effect)->SetShadowMap(shadowMap);
|
||||
dynamic_cast<Effect*>(effect)->SetLightViewProjMatrix(lightWorldViewProj);
|
||||
|
||||
|
||||
effect->SetWorldViewProjMatrix(worldViewProj);
|
||||
effect->SetWorldMatrix(mesh->GetWorldMatrix());
|
||||
effect->SetShadowMap(shadowMap);
|
||||
effect->SetLightViewProjMatrix(lightWorldViewProj);
|
||||
effect->SetLightDirection(m_Light.GetDirection());
|
||||
effect->SetLightColor(m_Light.GetColor());
|
||||
dynamic_cast<ShadowMesh*>(mesh)->RenderFinal(devicePtr, worldViewProj);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ ShadowMesh::ShadowMesh(ID3D11Device* devicePtr,
|
||||
const std::vector<Uint32>& indices,
|
||||
std::shared_ptr<Material> material,
|
||||
ShadowEffect* shadowEffectPtr,
|
||||
BaseEffect* colorEffectPtr)
|
||||
ShadowDiffuse* colorEffectPtr)
|
||||
: m_ShadowEffectPtr(shadowEffectPtr),
|
||||
m_ColorEffectPtr(colorEffectPtr),
|
||||
m_VerticesIn(verticesIn),
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include "Effects/ShadowEffect.h"
|
||||
#include "Material.h"
|
||||
#include "Mesh.h"
|
||||
#include "Effects/ShadowDiffuse.h"
|
||||
|
||||
class ShadowMesh {
|
||||
public:
|
||||
@@ -15,7 +16,7 @@ public:
|
||||
const std::vector<Uint32>& indices,
|
||||
std::shared_ptr<Material> material,
|
||||
ShadowEffect* shadowEffectPtr,
|
||||
BaseEffect* colorEffectPtr);
|
||||
ShadowDiffuse* colorEffectPtr);
|
||||
|
||||
~ShadowMesh();
|
||||
|
||||
@@ -26,7 +27,7 @@ public:
|
||||
Matrix GetWorldMatrix() const;
|
||||
|
||||
ShadowEffect* GetShadowEffectPtr(){ return m_ShadowEffectPtr; }
|
||||
BaseEffect* GetColorEffectPtr(){ return m_ColorEffectPtr; }
|
||||
ShadowDiffuse* GetColorEffectPtr(){ return m_ColorEffectPtr; }
|
||||
|
||||
|
||||
private:
|
||||
@@ -34,7 +35,7 @@ private:
|
||||
ShadowEffect* m_ShadowEffectPtr;
|
||||
|
||||
// Color effect for final render
|
||||
BaseEffect* m_ColorEffectPtr;
|
||||
ShadowDiffuse* m_ColorEffectPtr;
|
||||
|
||||
// Buffers
|
||||
ID3D11InputLayout* m_InputLayoutPtr;
|
||||
|
||||
Reference in New Issue
Block a user