Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions imgui.ini
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
[Window][InvisibleWindow]
Pos=0,0
Size=1400,900
Size=2560,1351
Collapsed=0

[Window][Console]
Pos=230,715
Size=828,185
Pos=230,1166
Size=1988,185
Collapsed=0
DockId=0x00000002,0

[Window][Scene]
Pos=230,0
Size=828,713
Size=1988,1164
Collapsed=0
DockId=0x00000001,0

Expand All @@ -22,18 +22,18 @@ Collapsed=0

[Window][Entities]
Pos=0,0
Size=228,900
Size=228,1351
Collapsed=0
DockId=0x00000003,0

[Window][Inspector]
Pos=1060,0
Size=340,900
Pos=2220,0
Size=340,1351
Collapsed=0
DockId=0x00000006,0

[Docking][Data]
DockSpace ID=0xF442860A Window=0xD8117908 Pos=329,358 Size=1400,900 Split=X Selected=0xE192E354
DockSpace ID=0xF442860A Window=0xD8117908 Pos=0,29 Size=2560,1351 Split=X Selected=0xE192E354
DockNode ID=0x00000005 Parent=0xF442860A SizeRef=1058,900 Split=X
DockNode ID=0x00000003 Parent=0x00000005 SizeRef=228,900 Selected=0xA628E342
DockNode ID=0x00000004 Parent=0x00000005 SizeRef=1988,900 Split=Y
Expand Down
17 changes: 14 additions & 3 deletions shaders/model.ps.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
struct PSInput
{
float4 Position : SV_Position;
float3 FragPosition : POSITION;
float3 FragPosition : POSITION0;
float4 FragPositionLightSpace : POSITION1;
float3 Normal : NORMAL;
float2 TexCoords : TEXCOORD;
};
Expand All @@ -16,8 +17,18 @@ struct PSOutput

Texture2D Texture : register(t0);
TextureCube CubeMapTexture : register(t1);
Texture2D ShadowMap : register(t2);
SamplerState TextureSampler : register(s0);

float CalculateShadows(PSInput input)
{
float3 projCoords = input.FragPositionLightSpace.xyz / input.FragPositionLightSpace.w;
projCoords = projCoords * 0.5f + 0.5f;
float closestDepth = ShadowMap.Sample(TextureSampler, projCoords.xy).r;
float currentDepth = projCoords.z;
return currentDepth > closestDepth ? 1.0 : 0.0f;
}

float4 CalculateDirectionalLight(PSInput input)
{
// ambient
Expand All @@ -35,9 +46,9 @@ float4 CalculateDirectionalLight(PSInput input)
float spec = pow(max(dot(normal, halfWayDirection), 0.0f), 32.0f); // material shininess
float3 specular = DirectionalLightSpecular * spec;

// TODO: calculate shadows here.
float shadow = CalculateShadows(input);

float3 result = ambient * (diffuse + specular);
float3 result = ambient + (1.0 - shadow) * (diffuse + specular);
return float4(result, 1.0f);
}

Expand Down
5 changes: 4 additions & 1 deletion shaders/model.vs.hlsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "util.hlsl"
#include "camera_matrix.hlsl"
#include "model_matrix.hlsl"
#include "shadow_map.hlsl"

struct VSInput
{
Expand All @@ -12,7 +13,8 @@ struct VSInput
struct VSOutput
{
float4 Position : SV_Position;
float3 FragPosition : POSITION;
float3 FragPosition : POSITION0;
float4 FragPositionLightSpace : POSITION1;
float3 Normal : NORMAL;
float2 TexCoords : TEXCOORD;
};
Expand All @@ -25,6 +27,7 @@ VSOutput Main(VSInput input)
VSOutput output;
output.Position = mul(viewProjectionMatrix, worldPosition);
output.FragPosition = worldPosition.xyz;
output.FragPositionLightSpace = mul(LightSpaceMatrix, worldPosition);
output.Normal = mul(transpose(inverse(ModelMatrix)), input.Normal);
output.TexCoords = input.TexCoords;
return output;
Expand Down
9 changes: 9 additions & 0 deletions shaders/shadow.ps.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// We only use the depth map for shadow mapping.
struct PSInput
{
float4 Position : SV_Position;
};

void Main(PSInput input)
{
}
25 changes: 25 additions & 0 deletions shaders/shadow.vs.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "util.hlsl"
#include "camera_matrix.hlsl"
#include "model_matrix.hlsl"
#include "shadow_map.hlsl"

struct VSInput
{
float3 Position : POSITION;
float3 Normal : NORMAL;
float2 TexCoords : TEXCOORD;
};

struct VSOutput
{
float4 Position : SV_Position;
};

VSOutput Main(VSInput input)
{
float4 worldPosition = mul(ModelMatrix, float4(input.Position, 1.0f));

VSOutput output;
output.Position = mul(LightSpaceMatrix, worldPosition);
return output;
}
4 changes: 4 additions & 0 deletions shaders/shadow_map.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cbuffer ShadowMap : register(b3)
{
matrix LightSpaceMatrix;
};
3 changes: 2 additions & 1 deletion src/components/model/texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
enum class TextureType
{
DIFFUSE,
SKYBOX
SKYBOX,
SHADOW_MAP
};

class Texture
Expand Down
7 changes: 4 additions & 3 deletions src/render/constant_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

enum class ConstantType
{
CAMERA_MATRIX = 0,
MODEL_MATRIX = 1,
DIRECTIONAL_LIGHT = 2,
CAMERA_MATRIX,
MODEL_MATRIX,
DIRECTIONAL_LIGHT,
SHADOW_MAP,
};

template <typename T>
Expand Down
56 changes: 56 additions & 0 deletions src/render/shadow_map.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include "shadow_map.hpp"

#include "components/model/texture.hpp"
#include "renderer.hpp"
#include "log.hpp"

ShadowMap::ShadowMap() : _constantBuffer(ConstantType::SHADOW_MAP, ShadowMapBuffer{})
{
Microsoft::WRL::ComPtr<ID3D11Texture2D> depthStencilTexture;

D3D11_TEXTURE2D_DESC shadowMapDesc = {};
shadowMapDesc.Width = 4096;
shadowMapDesc.Height = 4096;
shadowMapDesc.MipLevels = 1;
shadowMapDesc.ArraySize = 1;
shadowMapDesc.Format = DXGI_FORMAT_R24G8_TYPELESS;
shadowMapDesc.SampleDesc.Count = 1;
shadowMapDesc.Usage = D3D11_USAGE_DEFAULT;
shadowMapDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE;

if (FAILED(Renderer::GetDevice()->CreateTexture2D(&shadowMapDesc, nullptr, &depthStencilTexture)))
LOG(ERROR) << "Failed to create depth stencil texture";

D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc = {};
shaderResourceViewDesc.Format = DXGI_FORMAT_R24_UNORM_X8_TYPELESS;
shaderResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
shaderResourceViewDesc.Texture2D.MostDetailedMip = 0;
shaderResourceViewDesc.Texture2D.MipLevels = 1;

if (FAILED(Renderer::GetDevice()->CreateShaderResourceView(depthStencilTexture.Get(), &shaderResourceViewDesc, &_shaderResourceView)))
LOG(ERROR) << "Failed to create shader resource view description";

D3D11_DEPTH_STENCIL_VIEW_DESC dsvDesc = {};
dsvDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
dsvDesc.Texture2D.MipSlice = 0;
dsvDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;

if (FAILED(Renderer::GetDevice()->CreateDepthStencilView(depthStencilTexture.Get(), &dsvDesc, &_depthStencilView)))
LOG(ERROR) << "Failed to create depth stencil resource view";
}

void ShadowMap::Bind()
{
Renderer::GetDeviceContext()->OMSetRenderTargets(0, nullptr, _depthStencilView.Get());
}

void ShadowMap::AssignMap()
{
Renderer::GetDeviceContext()->PSSetShaderResources((int)TextureType::SHADOW_MAP, 1, _shaderResourceView.GetAddressOf());
}

void ShadowMap::SetLightSpaceMatrix(const glm::mat4 &matrix)
{
_constantBuffer.Update(ShadowMapBuffer{matrix});
_constantBuffer.Bind();
}
29 changes: 29 additions & 0 deletions src/render/shadow_map.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include "constant_buffer.hpp"

#include <glm/glm.hpp>

#include <d3d11.h>
#include <dxgi1_3.h>
#include <wrl.h>

class ShadowMap
{
public:
struct ShadowMapBuffer
{
glm::mat4 LightSpaceMatrix;
};

ShadowMap();

void Bind();
void AssignMap();
void SetLightSpaceMatrix(const glm::mat4 &matrix);

private:
ConstantBuffer<ShadowMapBuffer> _constantBuffer;
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> _shaderResourceView;
Microsoft::WRL::ComPtr<ID3D11DepthStencilView> _depthStencilView;
};
59 changes: 48 additions & 11 deletions src/scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
#include "components/model/model_component.hpp"

#include <GLFW/glfw3.h>
#include <glm/gtc/matrix_transform.hpp>

Scene::Scene() : _renderTarget(RenderTarget()),
_shadowMap(ShadowMap()),
_camera(Camera()),
_skybox(SkyBox()),
_modelVertexShader(L"shaders/model.vs.hlsl"),
_modelPixelShader(L"shaders/model.ps.hlsl")
_modelPixelShader(L"shaders/model.ps.hlsl"),
_shadowVertexShader(L"shaders/shadow.vs.hlsl"),
_shadowPixelShader(L"shaders/shadow.ps.hlsl")
{
ActiveScene = this;

Expand Down Expand Up @@ -78,17 +82,42 @@ void Scene::Render()
_camera.Update();
CalculateDeltaTime();

ShadowPass();
NormalPass();

_pxScene->simulate(1.0f / 60.0f);
_pxScene->fetchResults(true);
}

void Scene::ShadowPass()
{
_shadowMap.Bind();

auto matrix = glm::mat4();

const auto lightGroup = Scene::ActiveScene->Registry.view<TransformComponent, DirectionalLightComponent>(entt::exclude<IgnoreComponent>);
for (const auto &entity : lightGroup)
{
auto &transform = lightGroup.get<TransformComponent>(entity);

const auto lightProjection = glm::ortho(-10.0f, 10.0f, -10.0f, 10.0f, 0.1f, 10.0f);
const auto lightView = glm::lookAt(transform.GetPosition(), transform.GetPosition() + transform.GetDirection(), glm::vec3(0.0, 1.0, 0.0));
_shadowMap.SetLightSpaceMatrix(lightProjection * lightView);
}

RenderModels(_shadowVertexShader, _shadowPixelShader);
}

void Scene::NormalPass()
{
_renderTarget.Bind();

RenderModels();
RenderModels(_modelVertexShader, _modelPixelShader);
RenderSkyBox();
RenderLight();

_pxScene->simulate(1.0f / 60.0f);
_pxScene->fetchResults(true);
}

void Scene::RenderModels()
void Scene::RenderModels(const VertexShader &vs, const PixelShader &ps)
{
_renderTarget.SetMode(RenderTarget::Mode::Default);

Expand All @@ -98,10 +127,12 @@ void Scene::RenderModels()
auto &transform = modelGroup.get<TransformComponent>(entity);
const auto &model = modelGroup.get<ModelComponent>(entity);

_modelVertexShader.Bind();
_modelPixelShader.Bind();
transform.Bind();
vs.Bind();
ps.Bind();

_shadowMap.AssignMap();

transform.Bind();
model.Render();
}
}
Expand Down Expand Up @@ -169,10 +200,16 @@ void Scene::InitializeDefaultScene()
Registry.get<TransformComponent>(floor).SetPosition(glm::vec3(0.0f, -1.0f, 0.0f));
Registry.emplace<ModelComponent>(floor, floor).LoadModel("models\\plane\\plane.obj");

// Temp cube
auto cube = CreateNewEntity();
Registry.get<InfoComponent>(cube).Name = "Cube";
Registry.get<TransformComponent>(cube).SetPosition(glm::vec3(0.0f, -0.5f, 3.2f));
Registry.emplace<ModelComponent>(cube, cube).LoadModel("models\\dev_orange_cube\\dev_orange_cube.obj");

// Sun
auto sun = CreateNewEntity();
Registry.get<InfoComponent>(sun).Name = "Sun";
Registry.get<TransformComponent>(sun).SetPosition(glm::vec3(-10.0f, 0.0f, 0.0f));
Registry.get<TransformComponent>(sun).SetRotation(glm::vec3(2.0f, 0.0f, 0.4f));
Registry.get<TransformComponent>(sun).SetPosition(glm::vec3(0.0f, -20.0f, 30.0f));
Registry.get<TransformComponent>(sun).SetRotation(glm::vec3(0.7f, 0.0f, 0.0f));
Registry.emplace<DirectionalLightComponent>(sun, sun);
}
10 changes: 9 additions & 1 deletion src/scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "render/vertex_shader.hpp"
#include "render/pixel_shader.hpp"
#include "render/render_target.hpp"
#include "render/shadow_map.hpp"
#include "render/skybox.hpp"

#include <PxPhysicsAPI.h>
Expand Down Expand Up @@ -41,9 +42,13 @@ class Scene
VertexShader _modelVertexShader;
PixelShader _modelPixelShader;

VertexShader _shadowVertexShader;
PixelShader _shadowPixelShader;

SkyBox _skybox;

RenderTarget _renderTarget;
ShadowMap _shadowMap;

physx::PxDefaultAllocator _pxAllocator;
physx::PxDefaultErrorCallback _pxErrorCallback;
Expand All @@ -56,7 +61,10 @@ class Scene
float _previousFrameTime;
float _deltaTime;

void RenderModels();
void ShadowPass();
void NormalPass();

void RenderModels(const VertexShader &vs, const PixelShader &ps);
void RenderSkyBox();
void RenderLight();

Expand Down