Skip to content

Commit

Permalink
Merge pull request #236 from carboncopies/216-implement-directional-l…
Browse files Browse the repository at this point in the history
…ight-shadows

216 implement directional light shadows
  • Loading branch information
datacrystals authored Jun 25, 2022
2 parents f9dfcd1 + 48df658 commit f6f17a9
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ void ERS_CLASS_ProjectManager::LoadProject(long AssetID) {

// Load Default Scene
SystemUtils_->Logger_->Log(std::string(std::string("Loading Project Default Scene With ID ") + std::to_string(Project_.SceneIDs[Project_.DefaultScene])).c_str(), 5);
SceneManager_->AddScene(SceneLoader_->ProcessScene(Project_.SceneIDs[Project_.DefaultScene]));
for (unsigned int i = 0; i < Project_.SceneIDs.size(); i++) {
SceneManager_->AddScene(SceneLoader_->ProcessScene(Project_.SceneIDs[i]));
}
SceneManager_->SetActiveScene(Project_.DefaultScene);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,15 @@ void ERS_CLASS_VisualRenderer::UpdateViewport(int Index, ERS_CLASS_SceneManager*


// Update Cursor If Selection Changed
if (SceneManager->Scenes_[SceneManager->ActiveScene_]->HasSelectionChanged && DrawCursor) {
ERS_STRUCT_Scene* ActiveScene = SceneManager->Scenes_[SceneManager->ActiveScene_].get();
if (ActiveScene->HasSelectionChanged && DrawCursor) {

// Get Selected Model
int SelectedObject = SceneManager->Scenes_[SceneManager->ActiveScene_]->SelectedObject;
int SelectedObject = ActiveScene->SelectedObject;
if (SelectedObject >= ActiveScene->SceneObjects_.size()) {
SelectedObject = 0;
ActiveScene->SceneObjects_ = 0;
}

// Get LocRotScale
glm::vec3 Position;
Expand All @@ -383,33 +388,33 @@ void ERS_CLASS_VisualRenderer::UpdateViewport(int Index, ERS_CLASS_SceneManager*
bool HasRotation = false;
bool HasScale = false;

if (SceneManager->Scenes_[SceneManager->ActiveScene_]->SceneObjects_[SelectedObject].Type_ == std::string("Model")) {
unsigned long Index = SceneManager->Scenes_[SceneManager->ActiveScene_]->SceneObjects_[SelectedObject].Index_;
Position = SceneManager->Scenes_[SceneManager->ActiveScene_]->Models[Index]->ModelPosition;
Rotation = SceneManager->Scenes_[SceneManager->ActiveScene_]->Models[Index]->ModelRotation;
Scale = SceneManager->Scenes_[SceneManager->ActiveScene_]->Models[Index]->ModelScale;
if (ActiveScene->SceneObjects_[SelectedObject].Type_ == std::string("Model")) {
unsigned long Index = ActiveScene->SceneObjects_[SelectedObject].Index_;
Position = ActiveScene->Models[Index]->ModelPosition;
Rotation = ActiveScene->Models[Index]->ModelRotation;
Scale = ActiveScene->Models[Index]->ModelScale;
HasRotation = true;
HasScale = true;
} else if (SceneManager->Scenes_[SceneManager->ActiveScene_]->SceneObjects_[SelectedObject].Type_ == std::string("PointLight")) {
unsigned long Index = SceneManager->Scenes_[SceneManager->ActiveScene_]->SceneObjects_[SelectedObject].Index_;
Position = SceneManager->Scenes_[SceneManager->ActiveScene_]->PointLights[Index]->Pos;
} else if (SceneManager->Scenes_[SceneManager->ActiveScene_]->SceneObjects_[SelectedObject].Type_ == std::string("DirectionalLight")) {
unsigned long Index = SceneManager->Scenes_[SceneManager->ActiveScene_]->SceneObjects_[SelectedObject].Index_;
Position = SceneManager->Scenes_[SceneManager->ActiveScene_]->DirectionalLights[Index]->Pos;
Rotation = SceneManager->Scenes_[SceneManager->ActiveScene_]->DirectionalLights[Index]->Rot;
} else if (ActiveScene->SceneObjects_[SelectedObject].Type_ == std::string("PointLight")) {
unsigned long Index = ActiveScene->SceneObjects_[SelectedObject].Index_;
Position = ActiveScene->PointLights[Index]->Pos;
} else if (ActiveScene->SceneObjects_[SelectedObject].Type_ == std::string("DirectionalLight")) {
unsigned long Index = ActiveScene->SceneObjects_[SelectedObject].Index_;
Position = ActiveScene->DirectionalLights[Index]->Pos;
Rotation = ActiveScene->DirectionalLights[Index]->Rot;
HasRotation = true;
} else if (SceneManager->Scenes_[SceneManager->ActiveScene_]->SceneObjects_[SelectedObject].Type_ == std::string("SpotLight")) {
unsigned long Index = SceneManager->Scenes_[SceneManager->ActiveScene_]->SceneObjects_[SelectedObject].Index_;
Position = SceneManager->Scenes_[SceneManager->ActiveScene_]->SpotLights[Index]->Pos;
Rotation = SceneManager->Scenes_[SceneManager->ActiveScene_]->SpotLights[Index]->Rot;
} else if (ActiveScene->SceneObjects_[SelectedObject].Type_ == std::string("SpotLight")) {
unsigned long Index = ActiveScene->SceneObjects_[SelectedObject].Index_;
Position = ActiveScene->SpotLights[Index]->Pos;
Rotation = ActiveScene->SpotLights[Index]->Rot;
HasRotation = true;
}

// Set Cursor Position
Cursors3D_->SetLocRotScale(Position, Rotation, Scale, HasRotation, HasScale);

// Indicate Selection Hasn't Changed
SceneManager->Scenes_[SceneManager->ActiveScene_]->HasSelectionChanged = false;
ActiveScene->HasSelectionChanged = false;
}


Expand All @@ -425,7 +430,7 @@ void ERS_CLASS_VisualRenderer::UpdateViewport(int Index, ERS_CLASS_SceneManager*

// Render

//MeshRenderer_->RenderSceneNoTextures(SceneManager->Scenes_[SceneManager->ActiveScene_].get(), Shaders_[ShaderIndex].get());
//MeshRenderer_->RenderSceneNoTextures(ActiveScene.get(), Shaders_[ShaderIndex].get());
MeshRenderer_->RenderScene(SceneManager->Scenes_[SceneManager->ActiveScene_].get(), OpenGLDefaults_, Shaders_[ShaderIndex].get());

if (Viewports_[Index]->GridEnabled) {
Expand Down
4 changes: 3 additions & 1 deletion Source/EditorAssets/Projects/DefaultProject/1.ERS
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ProjectName: Back Alley Test Scene
ProjectName: ERS Test Project
ProjectDescription: Test development scene for EGSC.
ProjectVersion: 1.0.0
ProjectCreationDate: 0000-00-00 00-00-00
Expand All @@ -8,6 +8,8 @@ ProjectLicense: AGPL-V3.0 Or Higher
IsLicenseProprietary: false
SceneIDs:
0: 3
1: 12
2: 13
DefaultScene: 0
EditorLayouts:
0: 2
Expand Down
22 changes: 13 additions & 9 deletions Source/EditorAssets/Projects/DefaultProject/10041.ERS
Original file line number Diff line number Diff line change
Expand Up @@ -379,32 +379,35 @@ float ShadowCalculation(STRUCT_PointLight Light)
return 1.0f - Shadow;
}

float ShadowCalculation(mat4 LightSpaceMatrix, vec3 LightPos, int Index)
float ShadowCalculation(STRUCT_DirectionalLight Light)
{

// ., Light.Direction, Light.DepthMapIndex

// If Shadows Are Not Enabled, Exit Early
if (!ReceiveShadows_) {
return 1.0f;
if (!ReceiveShadows_ || !Light.CastsShadows) {
return 1.0f;
}


vec4 fragPosLightSpace = LightSpaceMatrix * vec4(Object.FragPos, 1.0f);
vec4 fragPosLightSpace = Light.LightSpaceMatrix * vec4(Object.FragPos, 1.0f);

// perform perspective divide
vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w;
// transform to [0,1] range
projCoords = projCoords * 0.5f + 0.5;

// get closest depth value from light's perspective (using [0,1] range fragPosLight as coords)
float closestDepth = texture(DepthMapArray, vec3(projCoords.xy, Index)).r;
float closestDepth = texture(DepthMapArray, vec3(projCoords.xy, Light.DepthMapIndex)).r;
// get depth of current fragment from light's perspective
float currentDepth = projCoords.z;
// check whether current frag pos is in shadow
vec3 LightDir = normalize(LightPos - Object.FragPos);
float Bias = max(0.00 * (1.0 - dot(Object.Normal, LightDir)), 0.0025);
//vec3 LightDir = normalize(L - Object.FragPos);

float Bias = max(0.00 * (1.0 - dot(Object.Normal, Light.Direction)), 0.0025);

//float Shadow = currentDepth - Bias > closestDepth ? 1.0 : 0.0;
float Shadow = PCFSampler(Bias, projCoords, Index, currentDepth, 2);
float Shadow = PCFSampler(Bias, projCoords, Light.DepthMapIndex, currentDepth, 2);
//float Shadow = ShadowPCFRandom(Bias, projCoords, Index, currentDepth, 1);

if (projCoords.z > 1.0) {
Expand Down Expand Up @@ -774,7 +777,7 @@ vec3 PBRDirectionalLight(STRUCT_DirectionalLight Light, vec3 ViewDir, vec3 Refle
float NdotL = max(dot(Model.Normal, L), 0.0);

// add to outgoing radiance Lo
return ((kD * Model.Albedo.rgb / PI + specular) * radiance * NdotL) * ShadowCalculation(Light.LightSpaceMatrix, Light.Direction, Light.DepthMapIndex); // note that we already multiplied the BRDF by the Fresnel (kS) so we won't multiply by kS again
return ((kD * Model.Albedo.rgb / PI + specular) * radiance * NdotL) * ShadowCalculation(Light); // note that we already multiplied the BRDF by the Fresnel (kS) so we won't multiply by kS again


}
Expand Down Expand Up @@ -863,5 +866,6 @@ void main() {






22 changes: 22 additions & 0 deletions Source/EditorAssets/Projects/DefaultProject/12.ERS
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
SceneName: Skybox Test Scene
SceneFormatVersion: 0
SceneData:
0:
AssetName: StreetTile01
AssetType: Model
AssetID: 1254897313
AssetPositionX: 1
AssetPositionY: -0.00100000005
AssetPositionZ: 4
AssetRotationX: 0
AssetRotationY: 0
AssetRotationZ: 0
AssetScaleX: 0.0199999996
AssetScaleY: 0.0199999996
AssetScaleZ: 0.0199999996
FlipTextures: true
CastDynamicShadows: true
CastStaticShadows: true
ReceiveShadows: true
AttachedScripts:
{}
22 changes: 22 additions & 0 deletions Source/EditorAssets/Projects/DefaultProject/13.ERS
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
SceneName: EGSC Model Testing
SceneFormatVersion: 0
SceneData:
0:
AssetName: StreetTile01
AssetType: Model
AssetID: 1254897313
AssetPositionX: 1
AssetPositionY: -0.00100000005
AssetPositionZ: 4
AssetRotationX: 0
AssetRotationY: 0
AssetRotationZ: 0
AssetScaleX: 0.0199999996
AssetScaleY: 0.0199999996
AssetScaleZ: 0.0199999996
FlipTextures: true
CastDynamicShadows: true
CastStaticShadows: true
ReceiveShadows: true
AttachedScripts:
{}
4 changes: 2 additions & 2 deletions Source/EditorAssets/Projects/DefaultProject/3.ERS
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
SceneName: FightingMechanicScene
SceneName: Alleyway Test Scene
SceneFormatVersion: 0
SceneData:
0:
Expand Down Expand Up @@ -1252,4 +1252,4 @@ SceneData:
RollOff: 9
CastShadows: true
AttachedScripts:
{}
{}

0 comments on commit f6f17a9

Please sign in to comment.