From 4092f6b4d4ee2cd530f5e76751c7e72d74e6635b Mon Sep 17 00:00:00 2001 From: Tleety Date: Mon, 18 Jan 2016 14:20:28 +0100 Subject: [PATCH 01/15] New Lightstruct working. --- assets | 2 +- include/Engine/Rendering/LightCullingPass.h | 7 ++--- resources/Shaders/CullLights.comp.glsl | 29 ++++++++++++--------- resources/Shaders/ForwardPlus.frag.glsl | 17 ++++++------ src/Engine/Rendering/LightCullingPass.cpp | 17 ++++++------ 5 files changed, 39 insertions(+), 33 deletions(-) diff --git a/assets b/assets index 6cbf2365..2fca9181 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 6cbf2365d49e6280750ea3bcd0f9c271779e6f15 +Subproject commit 2fca918162535c859c6adbe433e69fad8ea931b6 diff --git a/include/Engine/Rendering/LightCullingPass.h b/include/Engine/Rendering/LightCullingPass.h index 2fc1cbfc..954444da 100644 --- a/include/Engine/Rendering/LightCullingPass.h +++ b/include/Engine/Rendering/LightCullingPass.h @@ -56,15 +56,16 @@ private: Frustum* m_Frustums; //This should be a component - struct PointLight { + struct LightSource { glm::vec4 Position = glm::vec4(0.f); + glm::vec4 Direction = glm::vec4(0.f); glm::vec4 Color = glm::vec4(1.f); float Radius = 5.f; float Intensity = 0.8f; float Falloff = 0.3f; - float Padding = 1337; + enum Type_t { Point, Directional, Spot } Type; }; - std::vector m_PointLights; + std::vector m_LightSources; struct LightGrid { float Start; diff --git a/resources/Shaders/CullLights.comp.glsl b/resources/Shaders/CullLights.comp.glsl index c2675f20..f2908ecc 100644 --- a/resources/Shaders/CullLights.comp.glsl +++ b/resources/Shaders/CullLights.comp.glsl @@ -27,19 +27,20 @@ layout (std430, binding = 0) buffer FrustumBuffer Frustum Data[]; } Frustums; -struct PointLight { +struct LightSource { vec4 Position; + vec4 Direction; vec4 Color; float Radius; float Intensity; float Falloff; - float Padding; + int Type; }; layout (std430, binding = 1) buffer LightBuffer { - PointLight List[]; -} PointLights; + LightSource List[]; +} LightSources; struct LightGrid { float Start; @@ -106,8 +107,7 @@ layout (local_size_x = 16, local_size_y = 16, local_size_z = 1) in; void main () { GroupIndex = int(gl_WorkGroupID.x + (gl_WorkGroupID.y * int(ScreenDimensions.x/TILE_SIZE))); - if(gl_LocalInvocationIndex == 0) - { + if(gl_LocalInvocationIndex == 0) { GroupLightCount = 0; GroupFrustum = Frustums.Data[GroupIndex]; } @@ -115,22 +115,25 @@ void main () barrier(); memoryBarrierShared(); - for(int i = int(gl_LocalInvocationIndex); i < PointLights.List.length(); i += TILE_SIZE*TILE_SIZE) - { - PointLight light = PointLights.List[i]; + for(int i = int(gl_LocalInvocationIndex); i < LightSources.List.length(); i += TILE_SIZE*TILE_SIZE) { + LightSource light = LightSources.List[i]; //if pointlight //Pos i view antagligen - if(SphereInsideFrustrum( vec3(V * light.Position), light.Radius, GroupFrustum)) - { - //TODO: Fix transparent and opaque list, and depth test. - AppendLight( i ); + if(light.Type == 0) { + if(SphereInsideFrustrum( vec3(V * light.Position), light.Radius, GroupFrustum)) { + //TODO: Fix transparent and opaque list, and depth test. + AppendLight( i ); + } } //if conelight //if directional + if(light.Type == 1) { + AppendLight( i ); + } } diff --git a/resources/Shaders/ForwardPlus.frag.glsl b/resources/Shaders/ForwardPlus.frag.glsl index 3d5b9bfd..713e3621 100644 --- a/resources/Shaders/ForwardPlus.frag.glsl +++ b/resources/Shaders/ForwardPlus.frag.glsl @@ -9,19 +9,20 @@ uniform sampler2D texture0; #define TILE_SIZE 16 -struct PointLight { +struct LightSource { vec4 Position; + vec4 Direction; vec4 Color; float Radius; float Intensity; float Falloff; - float Padding; + int Type; }; layout (std430, binding = 1) buffer LightBuffer { - PointLight List[]; -} PointLights; + LightSource List[]; +} LightSources; struct LightGrid { float Start; @@ -71,7 +72,7 @@ vec4 CalcDiffuse(vec4 lightColor, vec4 lightVec, vec4 normal) { return lightColor * power; } -LightResult CalcPointLight(vec4 lightPos, float lightRadius, vec4 lightColor, float intensity, vec4 viewVec, vec4 position, vec4 normal, float falloff) +LightResult CalcLightSource(vec4 lightPos, float lightRadius, vec4 lightColor, float intensity, vec4 viewVec, vec4 position, vec4 normal, float falloff) { vec4 L = lightPos - position; float dist = length(L); @@ -108,15 +109,15 @@ void main() { int l = int(LightIndex[i]); - LightResult result = CalcPointLight(V * PointLights.List[l].Position, PointLights.List[l].Radius, PointLights.List[l].Color, PointLights.List[l].Intensity, viewVec, position, normal, PointLights.List[i].Falloff); + LightResult result = CalcLightSource(V * LightSources.List[l].Position, LightSources.List[l].Radius, LightSources.List[l].Color, LightSources.List[l].Intensity, viewVec, position, normal, LightSources.List[i].Falloff); totalLighting.Diffuse += result.Diffuse; totalLighting.Specular += result.Specular; } - fragmentColor += Input.DiffuseColor * (totalLighting.Diffuse + totalLighting.Specular) * texel * Color; + //fragmentColor += Input.DiffuseColor * (totalLighting.Diffuse + totalLighting.Specular) * texel * Color; //fragmentColor += Input.DiffuseColor * (totalLighting.Diffuse) * texel * Color; - //fragmentColor += vec4(0.0, LightGrids.Data[currentTile].Amount/3.0, 0, 1); + fragmentColor += Input.DiffuseColor + vec4(0.0, LightGrids.Data[currentTile].Amount/3.0, 0, 1); //fragmentColor = texel * Input.DiffuseColor * Color; if(int(gl_FragCoord.x)%16 == 0 || int(gl_FragCoord.y)%16 == 0 ) { diff --git a/src/Engine/Rendering/LightCullingPass.cpp b/src/Engine/Rendering/LightCullingPass.cpp index e59a50c0..d9cc8249 100644 --- a/src/Engine/Rendering/LightCullingPass.cpp +++ b/src/Engine/Rendering/LightCullingPass.cpp @@ -59,8 +59,8 @@ void LightCullingPass::CullLights(RenderScene& scene) glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_LightOffsetSSBO); glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(m_LightOffset), &m_LightOffset, GL_DYNAMIC_COPY); glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_LightSSBO); - if (m_PointLights.size() > 0) { - glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(PointLight) * m_PointLights.size(), &(m_PointLights[0]), GL_DYNAMIC_COPY); + if (m_LightSources.size() > 0) { + glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(LightSource) * m_LightSources.size(), &(m_LightSources[0]), GL_DYNAMIC_COPY); } else { GLfloat zero = 0.f; glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(GLfloat), &zero , GL_DYNAMIC_COPY); @@ -82,19 +82,20 @@ void LightCullingPass::CullLights(RenderScene& scene) void LightCullingPass::FillLightList(RenderScene& scene) { - m_PointLights.clear(); + m_LightSources.clear(); for(auto &job : scene.PointLightJobs) { auto pointLightjob = std::dynamic_pointer_cast(job); if (pointLightjob) { - PointLight p; + LightSource p; p.Color = pointLightjob->Color; p.Falloff = pointLightjob->Falloff; p.Intensity = pointLightjob->Intensity; p.Position = glm::vec4(glm::vec3(pointLightjob->Position), 1.f); p.Radius = pointLightjob->Radius; - p.Padding = 123.f; - m_PointLights.push_back(p); + //p.Padding = 123.f; + p.Type = LightSource::Point; + m_LightSources.push_back(p); continue; } } @@ -110,8 +111,8 @@ void LightCullingPass::InitializeSSBOs() glGenBuffers(1, &m_LightSSBO); glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_LightSSBO); - if(m_PointLights.size() > 0) { - glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(PointLight) * m_PointLights.size(), &(m_PointLights[0]), GL_DYNAMIC_COPY); + if(m_LightSources.size() > 0) { + glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(LightSource) * m_LightSources.size(), &(m_LightSources[0]), GL_DYNAMIC_COPY); } glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0); GLERROR("m_LightSSBO"); From 6680f6863f079dcbf95934a65a897e40b814777e Mon Sep 17 00:00:00 2001 From: Tleety Date: Mon, 18 Jan 2016 14:30:22 +0100 Subject: [PATCH 02/15] DirectionalLight component added. --- .../Schema/Components/DirectionalLight.xml | 5 +++++ .../Schema/Components/DirectionalLight.xsd | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 resources/Schema/Components/DirectionalLight.xml create mode 100644 resources/Schema/Components/DirectionalLight.xsd diff --git a/resources/Schema/Components/DirectionalLight.xml b/resources/Schema/Components/DirectionalLight.xml new file mode 100644 index 00000000..a14697c8 --- /dev/null +++ b/resources/Schema/Components/DirectionalLight.xml @@ -0,0 +1,5 @@ + + + 0.8 + true + \ No newline at end of file diff --git a/resources/Schema/Components/DirectionalLight.xsd b/resources/Schema/Components/DirectionalLight.xsd new file mode 100644 index 00000000..b4d96d18 --- /dev/null +++ b/resources/Schema/Components/DirectionalLight.xsd @@ -0,0 +1,19 @@ + + + + + + + + A directional light that shines bright like the future. + + + + + + + + + + + \ No newline at end of file From 6f1fd19ac70859bb26ff0dcd51d21abab7a91179 Mon Sep 17 00:00:00 2001 From: Tleety Date: Mon, 18 Jan 2016 14:30:28 +0100 Subject: [PATCH 03/15] Directional lights now being filled into m_LightSources --- src/Engine/Rendering/LightCullingPass.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Engine/Rendering/LightCullingPass.cpp b/src/Engine/Rendering/LightCullingPass.cpp index d9cc8249..76f2045f 100644 --- a/src/Engine/Rendering/LightCullingPass.cpp +++ b/src/Engine/Rendering/LightCullingPass.cpp @@ -99,6 +99,18 @@ void LightCullingPass::FillLightList(RenderScene& scene) continue; } } + for(auto &job : scene.DirectionalLightJobs) { + auto directionalLightJob = std::dynamic_pointer_cast(job); + if(directionalLightJob) { + LightSource p; + p.Color = directionalLightJob->Color; + p.Intensity = directionalLightJob->Intensity; + p.Position = glm::vec4(glm::vec3(directionalLightJob->Position), 1.f); + p.Type = LightSource::Directional; + m_LightSources.push_back(p); + continue; + } + } } void LightCullingPass::InitializeSSBOs() From 0a3b6f4da140cdb775f8c8896088bfc6e54f6ca8 Mon Sep 17 00:00:00 2001 From: Tleety Date: Mon, 18 Jan 2016 14:57:55 +0100 Subject: [PATCH 04/15] Added DirectionalLightJob --- .../Engine/Rendering/DirectionalLightJob.h | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 include/Engine/Rendering/DirectionalLightJob.h diff --git a/include/Engine/Rendering/DirectionalLightJob.h b/include/Engine/Rendering/DirectionalLightJob.h new file mode 100644 index 00000000..2686eb41 --- /dev/null +++ b/include/Engine/Rendering/DirectionalLightJob.h @@ -0,0 +1,34 @@ +#ifndef DirectionalLightJob_h__ +#define DirectionalLightJob_h__ + +#include + +#include "../Common.h" +#include "../GLM.h" +#include "../Core/ComponentWrapper.h" +#include "RenderJob.h" +#include "../Core/Transform.h" +#include "../Core/World.h" + +struct DirectionalLightJob : RenderJob +{ + DirectionalLightJob(ComponentWrapper transformComponent, ComponentWrapper directionalLightCompoentn, World* m_World) + : RenderJob() + { + Direction = glm::vec4((glm::vec3)directionalLightCompoentn["Direction"], 1.f); + Color = (glm::vec4)directionalLightCompoentn["Color"]; + Intensity = (double)directionalLightCompoentn["Intensity"]; + }; + + glm::vec4 Direction; + glm::vec4 Color; + float Intensity; + glm::vec3 padding = glm::vec3(1.f, 2.f, 3.f); + + void CalculateHash() override + { + Hash = 0; + } +}; + +#endif \ No newline at end of file From 794635b6a1c114e8746a61db0d3d0d25d6b407e2 Mon Sep 17 00:00:00 2001 From: Tleety Date: Mon, 18 Jan 2016 14:58:19 +0100 Subject: [PATCH 05/15] Added DirectionalLights to renderqueue jobs --- include/Engine/Rendering/RenderQueue.h | 3 ++ .../Schema/Components/DirectionalLight.xml | 1 + src/Engine/Rendering/RenderSystem.cpp | 43 +++++++++++++------ 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/include/Engine/Rendering/RenderQueue.h b/include/Engine/Rendering/RenderQueue.h index 119ea57f..8df967ac 100644 --- a/include/Engine/Rendering/RenderQueue.h +++ b/include/Engine/Rendering/RenderQueue.h @@ -12,6 +12,7 @@ #include "RenderJob.h" #include "ModelJob.h" #include "PointLightJob.h" +#include "DirectionalLightJob.h" /* @@ -53,12 +54,14 @@ struct RenderScene ::Camera* Camera; std::list> ForwardJobs; std::list> PointLightJobs; + std::list> DirectionalLightJobs; Rectangle Viewport; void Clear() { ForwardJobs.clear(); PointLightJobs.clear(); + DirectionalLightJobs.clear(); } }; diff --git a/resources/Schema/Components/DirectionalLight.xml b/resources/Schema/Components/DirectionalLight.xml index a14697c8..c854696a 100644 --- a/resources/Schema/Components/DirectionalLight.xml +++ b/resources/Schema/Components/DirectionalLight.xml @@ -1,4 +1,5 @@ + 0.8 true diff --git a/src/Engine/Rendering/RenderSystem.cpp b/src/Engine/Rendering/RenderSystem.cpp index 11eca86b..eecf6786 100644 --- a/src/Engine/Rendering/RenderSystem.cpp +++ b/src/Engine/Rendering/RenderSystem.cpp @@ -102,22 +102,39 @@ void RenderSystem::fillModels(std::list>& jobs, World void RenderSystem::fillLight(std::list>& jobs, World* world) { auto pointLights = world->GetComponents("PointLight"); - if (pointLights == nullptr) { - return; + if (pointLights != nullptr) { + for (auto& pointlightC : *pointLights) { + bool visible = pointlightC["Visible"]; + if (!visible) { + continue; + } + auto transformC = world->GetComponent(pointlightC.EntityID, "Transform"); + if (&transformC == nullptr) { + continue; + } + + std::shared_ptr pointLightJob = std::shared_ptr(new PointLightJob(transformC, pointlightC, m_World)); + jobs.push_back(pointLightJob); + } } - for (auto& pointlightC : *pointLights) { - bool visible = pointlightC["Visible"]; - if (!visible) { - continue; - } - auto transformC = world->GetComponent(pointlightC.EntityID, "Transform"); - if (&transformC == nullptr) { - return; - } - std::shared_ptr pointLightJob = std::shared_ptr(new PointLightJob(transformC, pointlightC, m_World)); - jobs.push_back(pointLightJob); + auto directionalLights = world->GetComponents("DirectionalLight"); + if (directionalLights != nullptr) { + for (auto& directionalLightC : *directionalLights) { + bool visable = directionalLightC["Visible"]; + if (!visable) { + continue; + } + + auto transformC = world->GetComponent(directionalLightC.EntityID, "Transform"); + if(&transformC == nullptr) { + continue; + } + + std::shared_ptr directionalLightJob = std::shared_ptr(new DirectionalLightJob(transformC, directionalLightC, m_World)); + jobs.push_back(directionalLightJob); + } } } From 01a164847c41713deb9ef908921917188cada510 Mon Sep 17 00:00:00 2001 From: Tleety Date: Mon, 18 Jan 2016 15:15:33 +0100 Subject: [PATCH 06/15] Directional light now fully added to all lists. --- include/Engine/Rendering/DirectionalLightJob.h | 8 ++++---- include/Engine/Rendering/RenderSystem.h | 3 ++- resources/Schema/Components.xsd | 1 + resources/Schema/Components/DirectionalLight.xml | 4 ++-- src/Engine/Rendering/LightCullingPass.cpp | 8 ++++---- src/Engine/Rendering/RenderSystem.cpp | 10 +++++++--- 6 files changed, 20 insertions(+), 14 deletions(-) diff --git a/include/Engine/Rendering/DirectionalLightJob.h b/include/Engine/Rendering/DirectionalLightJob.h index 2686eb41..e8cd5b17 100644 --- a/include/Engine/Rendering/DirectionalLightJob.h +++ b/include/Engine/Rendering/DirectionalLightJob.h @@ -12,12 +12,12 @@ struct DirectionalLightJob : RenderJob { - DirectionalLightJob(ComponentWrapper transformComponent, ComponentWrapper directionalLightCompoentn, World* m_World) + DirectionalLightJob(ComponentWrapper transformComponent, ComponentWrapper directionalLightComponent, World* m_World) : RenderJob() { - Direction = glm::vec4((glm::vec3)directionalLightCompoentn["Direction"], 1.f); - Color = (glm::vec4)directionalLightCompoentn["Color"]; - Intensity = (double)directionalLightCompoentn["Intensity"]; + Direction = glm::vec4((glm::vec3)directionalLightComponent["Direction"], 1.f); + Color = (glm::vec4)directionalLightComponent["Color"]; + Intensity = (double)directionalLightComponent["Intensity"]; }; glm::vec4 Direction; diff --git a/include/Engine/Rendering/RenderSystem.h b/include/Engine/Rendering/RenderSystem.h index fe110276..966593db 100644 --- a/include/Engine/Rendering/RenderSystem.h +++ b/include/Engine/Rendering/RenderSystem.h @@ -46,7 +46,8 @@ private: void updateProjectionMatrix(ComponentWrapper& cameraComponent); void fillModels(std::list>& jobs, World* world); - void fillLight(std::list>& jobs, World* world); + void fillPointLights(std::list>& jobs, World* world); + void fillDirectionalLights(std::list>& jobs, World* world); EventRelay m_EInputCommand; bool OnInputCommand(const Events::InputCommand& e); diff --git a/resources/Schema/Components.xsd b/resources/Schema/Components.xsd index 9181daf2..a90d51ec 100644 --- a/resources/Schema/Components.xsd +++ b/resources/Schema/Components.xsd @@ -9,6 +9,7 @@ + \ No newline at end of file diff --git a/resources/Schema/Components/DirectionalLight.xml b/resources/Schema/Components/DirectionalLight.xml index c854696a..79d3ac2b 100644 --- a/resources/Schema/Components/DirectionalLight.xml +++ b/resources/Schema/Components/DirectionalLight.xml @@ -1,6 +1,6 @@ - + 0.8 true - \ No newline at end of file + \ No newline at end of file diff --git a/src/Engine/Rendering/LightCullingPass.cpp b/src/Engine/Rendering/LightCullingPass.cpp index 76f2045f..f49156dc 100644 --- a/src/Engine/Rendering/LightCullingPass.cpp +++ b/src/Engine/Rendering/LightCullingPass.cpp @@ -96,19 +96,19 @@ void LightCullingPass::FillLightList(RenderScene& scene) //p.Padding = 123.f; p.Type = LightSource::Point; m_LightSources.push_back(p); - continue; + } } for(auto &job : scene.DirectionalLightJobs) { - auto directionalLightJob = std::dynamic_pointer_cast(job); + auto directionalLightJob = std::dynamic_pointer_cast(job); if(directionalLightJob) { LightSource p; + p.Direction = directionalLightJob->Direction; p.Color = directionalLightJob->Color; p.Intensity = directionalLightJob->Intensity; - p.Position = glm::vec4(glm::vec3(directionalLightJob->Position), 1.f); p.Type = LightSource::Directional; m_LightSources.push_back(p); - continue; + } } } diff --git a/src/Engine/Rendering/RenderSystem.cpp b/src/Engine/Rendering/RenderSystem.cpp index eecf6786..9a75a615 100644 --- a/src/Engine/Rendering/RenderSystem.cpp +++ b/src/Engine/Rendering/RenderSystem.cpp @@ -99,7 +99,7 @@ void RenderSystem::fillModels(std::list>& jobs, World } -void RenderSystem::fillLight(std::list>& jobs, World* world) +void RenderSystem::fillPointLights(std::list>& jobs, World* world) { auto pointLights = world->GetComponents("PointLight"); if (pointLights != nullptr) { @@ -117,8 +117,11 @@ void RenderSystem::fillLight(std::list>& jobs, World* jobs.push_back(pointLightJob); } } +} +void RenderSystem::fillDirectionalLights(std::list>& jobs, World* world) +{ auto directionalLights = world->GetComponents("DirectionalLight"); if (directionalLights != nullptr) { for (auto& directionalLightC : *directionalLights) { @@ -128,7 +131,7 @@ void RenderSystem::fillLight(std::list>& jobs, World* } auto transformC = world->GetComponent(directionalLightC.EntityID, "Transform"); - if(&transformC == nullptr) { + if (&transformC == nullptr) { continue; } @@ -162,7 +165,8 @@ void RenderSystem::Update(World* world, double dt) scene.Camera = m_Camera; scene.Viewport = Rectangle(1280, 720); fillModels(scene.ForwardJobs, world); - fillLight(scene.PointLightJobs, world); + fillPointLights(scene.PointLightJobs, world); + fillDirectionalLights(scene.DirectionalLightJobs, world); m_RenderFrame->Add(scene); } From 601ce85c05da303cbda4d44b4cf55d164cf422fd Mon Sep 17 00:00:00 2001 From: Tleety Date: Mon, 18 Jan 2016 16:50:41 +0100 Subject: [PATCH 07/15] WIP --- .../Engine/Rendering/DirectionalLightJob.h | 1 - include/Engine/Rendering/LightCullingPass.h | 2 +- .../Schema/Components/DirectionalLight.xml | 2 +- resources/Shaders/ForwardPlus.frag.glsl | 39 +++++++++++++++---- 4 files changed, 33 insertions(+), 11 deletions(-) diff --git a/include/Engine/Rendering/DirectionalLightJob.h b/include/Engine/Rendering/DirectionalLightJob.h index e8cd5b17..83787833 100644 --- a/include/Engine/Rendering/DirectionalLightJob.h +++ b/include/Engine/Rendering/DirectionalLightJob.h @@ -23,7 +23,6 @@ struct DirectionalLightJob : RenderJob glm::vec4 Direction; glm::vec4 Color; float Intensity; - glm::vec3 padding = glm::vec3(1.f, 2.f, 3.f); void CalculateHash() override { diff --git a/include/Engine/Rendering/LightCullingPass.h b/include/Engine/Rendering/LightCullingPass.h index 954444da..ab50f650 100644 --- a/include/Engine/Rendering/LightCullingPass.h +++ b/include/Engine/Rendering/LightCullingPass.h @@ -58,7 +58,7 @@ private: //This should be a component struct LightSource { glm::vec4 Position = glm::vec4(0.f); - glm::vec4 Direction = glm::vec4(0.f); + glm::vec4 Direction = glm::vec4(10.f); glm::vec4 Color = glm::vec4(1.f); float Radius = 5.f; float Intensity = 0.8f; diff --git a/resources/Schema/Components/DirectionalLight.xml b/resources/Schema/Components/DirectionalLight.xml index 79d3ac2b..b14b7934 100644 --- a/resources/Schema/Components/DirectionalLight.xml +++ b/resources/Schema/Components/DirectionalLight.xml @@ -1,5 +1,5 @@ - + 0.8 true diff --git a/resources/Shaders/ForwardPlus.frag.glsl b/resources/Shaders/ForwardPlus.frag.glsl index 713e3621..087c594c 100644 --- a/resources/Shaders/ForwardPlus.frag.glsl +++ b/resources/Shaders/ForwardPlus.frag.glsl @@ -72,7 +72,7 @@ vec4 CalcDiffuse(vec4 lightColor, vec4 lightVec, vec4 normal) { return lightColor * power; } -LightResult CalcLightSource(vec4 lightPos, float lightRadius, vec4 lightColor, float intensity, vec4 viewVec, vec4 position, vec4 normal, float falloff) +LightResult CalcPointLightSource(vec4 lightPos, float lightRadius, vec4 lightColor, float intensity, vec4 viewVec, vec4 position, vec4 normal, float falloff) { vec4 L = lightPos - position; float dist = length(L); @@ -86,6 +86,16 @@ LightResult CalcLightSource(vec4 lightPos, float lightRadius, vec4 lightColor, f return result; } +LightResult CalcDirectionalLightSource(vec4 direction, vec4 color, float intensity, vec4 viewVec, vec4 vertPosition, vec4 vertNormal) +{ + vec4 L = normalize( -direction ); + + LightResult result; + result.Diffuse = CalcDiffuse(color, L, vertNormal) * intensity; + result.Specular = CalcSpecular(color, viewVec, L, vertNormal) * intensity; + return result; +} + void main() { @@ -105,22 +115,35 @@ void main() int start = int(LightGrids.Data[currentTile].Start); int amount = int(LightGrids.Data[currentTile].Amount); //for(int i = 0; i < 3; i++) - for(int i = start; i < start + amount; i++) - { + for(int i = start; i < start + amount; i++) { int l = int(LightIndex[i]); + LightResult result; - LightResult result = CalcLightSource(V * LightSources.List[l].Position, LightSources.List[l].Radius, LightSources.List[l].Color, LightSources.List[l].Intensity, viewVec, position, normal, LightSources.List[i].Falloff); - + if(LightSources.List[i].Type == 0) { // point + //result = CalcPointLightSource(V * LightSources.List[l].Position, LightSources.List[l].Radius, LightSources.List[l].Color, LightSources.List[l].Intensity, viewVec, position, normal, LightSources.List[i].Falloff); + } else if (LightSources.List[i].Type == 1) { //Directional + result = CalcDirectionalLightSource(V * LightSources.List[l].Direction, LightSources.List[i].Color, LightSources.List[i].Intensity, viewVec, position, normal); + } totalLighting.Diffuse += result.Diffuse; totalLighting.Specular += result.Specular; } + + fragmentColor += Input.DiffuseColor; //fragmentColor += Input.DiffuseColor * (totalLighting.Diffuse + totalLighting.Specular) * texel * Color; //fragmentColor += Input.DiffuseColor * (totalLighting.Diffuse) * texel * Color; - fragmentColor += Input.DiffuseColor + vec4(0.0, LightGrids.Data[currentTile].Amount/3.0, 0, 1); + //fragmentColor += Input.DiffuseColor + vec4(0.0, LightGrids.Data[currentTile].Amount/3.0, 0, 1); + if(LightSources.List[0].Type == 0) { + fragmentColor += vec4(1,0,0,1); + } + if(LightSources.List[0].Type == 1) { + fragmentColor += vec4(0,1,0,1); + } + if(LightSources.List[0].Type == 2) { + fragmentColor += vec4(0,0,1,1); + } //fragmentColor = texel * Input.DiffuseColor * Color; - if(int(gl_FragCoord.x)%16 == 0 || int(gl_FragCoord.y)%16 == 0 ) - { + if(int(gl_FragCoord.x)%16 == 0 || int(gl_FragCoord.y)%16 == 0 ) { //fragmentColor += vec4(0.5, 0, 0, 0); } else { //fragmentColor += vec4(LightGrids.Data[int(tilePos.x + tilePos.y*80)].Amount/3.0, 0, 0, 1); From 4b29d48f27ecfef71d8f34510e3789b0132f568c Mon Sep 17 00:00:00 2001 From: Tleety Date: Mon, 18 Jan 2016 17:57:45 +0100 Subject: [PATCH 08/15] WIP --- .../Engine/Rendering/DirectionalLightJob.h | 2 +- include/Engine/Rendering/LightCullingPass.h | 2 +- resources/Shaders/CullLights.comp.glsl | 4 +-- resources/Shaders/ForwardPlus.frag.glsl | 25 ++++++------------- 4 files changed, 11 insertions(+), 22 deletions(-) diff --git a/include/Engine/Rendering/DirectionalLightJob.h b/include/Engine/Rendering/DirectionalLightJob.h index 83787833..92cd195a 100644 --- a/include/Engine/Rendering/DirectionalLightJob.h +++ b/include/Engine/Rendering/DirectionalLightJob.h @@ -15,7 +15,7 @@ struct DirectionalLightJob : RenderJob DirectionalLightJob(ComponentWrapper transformComponent, ComponentWrapper directionalLightComponent, World* m_World) : RenderJob() { - Direction = glm::vec4((glm::vec3)directionalLightComponent["Direction"], 1.f); + Direction = glm::vec4((glm::vec3)directionalLightComponent["Direction"], 0.f); Color = (glm::vec4)directionalLightComponent["Color"]; Intensity = (double)directionalLightComponent["Intensity"]; }; diff --git a/include/Engine/Rendering/LightCullingPass.h b/include/Engine/Rendering/LightCullingPass.h index ab50f650..7eb2c62d 100644 --- a/include/Engine/Rendering/LightCullingPass.h +++ b/include/Engine/Rendering/LightCullingPass.h @@ -63,7 +63,7 @@ private: float Radius = 5.f; float Intensity = 0.8f; float Falloff = 0.3f; - enum Type_t { Point, Directional, Spot } Type; + enum Type_t { Zero, Point, Directional, Spot } Type; }; std::vector m_LightSources; diff --git a/resources/Shaders/CullLights.comp.glsl b/resources/Shaders/CullLights.comp.glsl index f2908ecc..c32c6479 100644 --- a/resources/Shaders/CullLights.comp.glsl +++ b/resources/Shaders/CullLights.comp.glsl @@ -120,7 +120,7 @@ void main () //if pointlight //Pos i view antagligen - if(light.Type == 0) { + if(light.Type == 1) { if(SphereInsideFrustrum( vec3(V * light.Position), light.Radius, GroupFrustum)) { //TODO: Fix transparent and opaque list, and depth test. AppendLight( i ); @@ -131,7 +131,7 @@ void main () //if conelight //if directional - if(light.Type == 1) { + if(light.Type == 2) { AppendLight( i ); } diff --git a/resources/Shaders/ForwardPlus.frag.glsl b/resources/Shaders/ForwardPlus.frag.glsl index 087c594c..9f9140fb 100644 --- a/resources/Shaders/ForwardPlus.frag.glsl +++ b/resources/Shaders/ForwardPlus.frag.glsl @@ -88,7 +88,7 @@ LightResult CalcPointLightSource(vec4 lightPos, float lightRadius, vec4 lightCol LightResult CalcDirectionalLightSource(vec4 direction, vec4 color, float intensity, vec4 viewVec, vec4 vertPosition, vec4 vertNormal) { - vec4 L = normalize( -direction ); + vec4 L = normalize( vec4(direction.xyz, 1) ); LightResult result; result.Diffuse = CalcDiffuse(color, L, vertNormal) * intensity; @@ -119,9 +119,9 @@ void main() int l = int(LightIndex[i]); LightResult result; - if(LightSources.List[i].Type == 0) { // point - //result = CalcPointLightSource(V * LightSources.List[l].Position, LightSources.List[l].Radius, LightSources.List[l].Color, LightSources.List[l].Intensity, viewVec, position, normal, LightSources.List[i].Falloff); - } else if (LightSources.List[i].Type == 1) { //Directional + if(LightSources.List[i].Type == 1) { // point + result = CalcPointLightSource(V * LightSources.List[l].Position, LightSources.List[l].Radius, LightSources.List[l].Color, LightSources.List[l].Intensity, viewVec, position, normal, LightSources.List[i].Falloff); + } else if (LightSources.List[i].Type == 2) { //Directional result = CalcDirectionalLightSource(V * LightSources.List[l].Direction, LightSources.List[i].Color, LightSources.List[i].Intensity, viewVec, position, normal); } totalLighting.Diffuse += result.Diffuse; @@ -129,26 +129,15 @@ void main() } - fragmentColor += Input.DiffuseColor; - //fragmentColor += Input.DiffuseColor * (totalLighting.Diffuse + totalLighting.Specular) * texel * Color; + //fragmentColor += Input.DiffuseColor; + fragmentColor += Input.DiffuseColor * (totalLighting.Diffuse + totalLighting.Specular) * texel * Color; //fragmentColor += Input.DiffuseColor * (totalLighting.Diffuse) * texel * Color; - //fragmentColor += Input.DiffuseColor + vec4(0.0, LightGrids.Data[currentTile].Amount/3.0, 0, 1); - if(LightSources.List[0].Type == 0) { - fragmentColor += vec4(1,0,0,1); - } - if(LightSources.List[0].Type == 1) { - fragmentColor += vec4(0,1,0,1); - } - if(LightSources.List[0].Type == 2) { - fragmentColor += vec4(0,0,1,1); - } + //fragmentColor += Input.DiffuseColor + vec4(0.0, LightGrids.Data[currentTile].Start/.0, 0, 1); //fragmentColor = texel * Input.DiffuseColor * Color; if(int(gl_FragCoord.x)%16 == 0 || int(gl_FragCoord.y)%16 == 0 ) { //fragmentColor += vec4(0.5, 0, 0, 0); } else { //fragmentColor += vec4(LightGrids.Data[int(tilePos.x + tilePos.y*80)].Amount/3.0, 0, 0, 1); - - } } From 46e17b7e0da3a6c31b6dbaeaf4d32b46a2bd13b2 Mon Sep 17 00:00:00 2001 From: Tleety Date: Tue, 19 Jan 2016 12:32:34 +0100 Subject: [PATCH 09/15] Working directional light --- .../Engine/Rendering/DirectionalLightJob.h | 4 +++- include/Engine/Rendering/LightCullingPass.h | 10 +++++----- .../Schema/Components/DirectionalLight.xml | 1 - .../Schema/Components/DirectionalLight.xsd | 1 - resources/Shaders/ForwardPlus.frag.glsl | 20 ++++++++++--------- src/Engine/Rendering/LightCullingPass.cpp | 20 ++++++++----------- 6 files changed, 27 insertions(+), 29 deletions(-) diff --git a/include/Engine/Rendering/DirectionalLightJob.h b/include/Engine/Rendering/DirectionalLightJob.h index 92cd195a..f6942112 100644 --- a/include/Engine/Rendering/DirectionalLightJob.h +++ b/include/Engine/Rendering/DirectionalLightJob.h @@ -15,7 +15,9 @@ struct DirectionalLightJob : RenderJob DirectionalLightJob(ComponentWrapper transformComponent, ComponentWrapper directionalLightComponent, World* m_World) : RenderJob() { - Direction = glm::vec4((glm::vec3)directionalLightComponent["Direction"], 0.f); + + Direction = glm::vec4(0,0,-1,0) * Transform::AbsoluteOrientation(m_World, transformComponent.EntityID); + //Direction = glm::vec4((glm::vec3)directionalLightComponent["Direction"], 0.f); Color = (glm::vec4)directionalLightComponent["Color"]; Intensity = (double)directionalLightComponent["Intensity"]; }; diff --git a/include/Engine/Rendering/LightCullingPass.h b/include/Engine/Rendering/LightCullingPass.h index 7eb2c62d..d8852df0 100644 --- a/include/Engine/Rendering/LightCullingPass.h +++ b/include/Engine/Rendering/LightCullingPass.h @@ -46,8 +46,8 @@ private: int m_NumberOfTiles = 0; struct Plane { - glm::vec3 Normal; - float d; + glm::vec3 Normal = glm::vec3(0.f); + float d = 0; }; struct Frustum { @@ -68,9 +68,9 @@ private: std::vector m_LightSources; struct LightGrid { - float Start; - float Amount; - glm::vec2 Padding; + float Start = 0; + float Amount = 0; + glm::vec2 Padding = glm::vec2(1.f, 2.f); }; LightGrid* m_LightGrid; diff --git a/resources/Schema/Components/DirectionalLight.xml b/resources/Schema/Components/DirectionalLight.xml index b14b7934..f66605c0 100644 --- a/resources/Schema/Components/DirectionalLight.xml +++ b/resources/Schema/Components/DirectionalLight.xml @@ -1,5 +1,4 @@ - 0.8 true diff --git a/resources/Schema/Components/DirectionalLight.xsd b/resources/Schema/Components/DirectionalLight.xsd index b4d96d18..dbfee38c 100644 --- a/resources/Schema/Components/DirectionalLight.xsd +++ b/resources/Schema/Components/DirectionalLight.xsd @@ -9,7 +9,6 @@ - diff --git a/resources/Shaders/ForwardPlus.frag.glsl b/resources/Shaders/ForwardPlus.frag.glsl index 9f9140fb..0298c969 100644 --- a/resources/Shaders/ForwardPlus.frag.glsl +++ b/resources/Shaders/ForwardPlus.frag.glsl @@ -86,9 +86,9 @@ LightResult CalcPointLightSource(vec4 lightPos, float lightRadius, vec4 lightCol return result; } -LightResult CalcDirectionalLightSource(vec4 direction, vec4 color, float intensity, vec4 viewVec, vec4 vertPosition, vec4 vertNormal) +LightResult CalcDirectionalLightSource(vec4 direction, vec4 color, float intensity, vec4 viewVec, vec4 vertNormal) { - vec4 L = normalize( vec4(direction.xyz, 1) ); + vec4 L = normalize( -direction ); LightResult result; result.Diffuse = CalcDiffuse(color, L, vertNormal) * intensity; @@ -105,8 +105,8 @@ void main() vec4 viewVec = normalize(-position); vec2 tilePos; - tilePos.x = int(gl_FragCoord.x/16); - tilePos.y = int(gl_FragCoord.y/16); + tilePos.x = int(gl_FragCoord.x/TILE_SIZE); + tilePos.y = int(gl_FragCoord.y/TILE_SIZE); LightResult totalLighting; totalLighting.Diffuse = scene_ambient; @@ -117,12 +117,13 @@ void main() //for(int i = 0; i < 3; i++) for(int i = start; i < start + amount; i++) { int l = int(LightIndex[i]); + LightSource light = LightSources.List[l]; LightResult result; - if(LightSources.List[i].Type == 1) { // point - result = CalcPointLightSource(V * LightSources.List[l].Position, LightSources.List[l].Radius, LightSources.List[l].Color, LightSources.List[l].Intensity, viewVec, position, normal, LightSources.List[i].Falloff); - } else if (LightSources.List[i].Type == 2) { //Directional - result = CalcDirectionalLightSource(V * LightSources.List[l].Direction, LightSources.List[i].Color, LightSources.List[i].Intensity, viewVec, position, normal); + if(light.Type == 1) { // point + result = CalcPointLightSource(V * light.Position, light.Radius, light.Color, light.Intensity, viewVec, position, normal, light.Falloff); + } else if (light.Type == 2) { //Directional + result = CalcDirectionalLightSource(V * light.Direction, light.Color, light.Intensity, viewVec, normal); } totalLighting.Diffuse += result.Diffuse; totalLighting.Specular += result.Specular; @@ -132,8 +133,9 @@ void main() //fragmentColor += Input.DiffuseColor; fragmentColor += Input.DiffuseColor * (totalLighting.Diffuse + totalLighting.Specular) * texel * Color; //fragmentColor += Input.DiffuseColor * (totalLighting.Diffuse) * texel * Color; - //fragmentColor += Input.DiffuseColor + vec4(0.0, LightGrids.Data[currentTile].Start/.0, 0, 1); + //fragmentColor += Input.DiffuseColor + vec4(0.0, LightGrids.Data[currentTile].Amount/3, 0, 1); //fragmentColor = texel * Input.DiffuseColor * Color; + //fragmentColor += vec4(currentTile/3600.f, 0, 0, 1); if(int(gl_FragCoord.x)%16 == 0 || int(gl_FragCoord.y)%16 == 0 ) { //fragmentColor += vec4(0.5, 0, 0, 0); } else { diff --git a/src/Engine/Rendering/LightCullingPass.cpp b/src/Engine/Rendering/LightCullingPass.cpp index f49156dc..2a2e2383 100644 --- a/src/Engine/Rendering/LightCullingPass.cpp +++ b/src/Engine/Rendering/LightCullingPass.cpp @@ -40,15 +40,14 @@ void LightCullingPass::OnResolutionChange() void LightCullingPass::SetSSBOSizes() { - m_NumberOfTiles = (int)(m_Renderer->Resolution().Width*m_Renderer->Resolution().Height)/TILE_SIZE; - - //m_Frustums = new Frustum[s]; - //m_LightGrid = new LightGrid[s]; - //m_LightIndex = new float[s*200]; + m_NumberOfTiles = (int)(m_Renderer->Resolution().Width/TILE_SIZE) * (int)(m_Renderer->Resolution().Height/TILE_SIZE); m_Frustums = new Frustum[m_NumberOfTiles]; m_LightGrid = new LightGrid[m_NumberOfTiles]; m_LightIndex = new float[m_NumberOfTiles*MAX_LIGHTS_PER_TILE]; + for (int i = 0; i < m_NumberOfTiles*MAX_LIGHTS_PER_TILE; i++) { + m_LightIndex[i] = -1; + } } void LightCullingPass::CullLights(RenderScene& scene) @@ -75,7 +74,7 @@ void LightCullingPass::CullLights(RenderScene& scene) glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, m_LightGridSSBO); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, m_LightOffsetSSBO); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, m_LightIndexSSBO); - glDispatchCompute(m_Renderer->Resolution().Width / TILE_SIZE, m_Renderer->Resolution().Height / TILE_SIZE, 1); + glDispatchCompute(glm::ceil(m_Renderer->Resolution().Width / TILE_SIZE), glm::ceil(m_Renderer->Resolution().Height / TILE_SIZE), 1); GLERROR("CullLights Error: End"); } @@ -117,25 +116,22 @@ void LightCullingPass::InitializeSSBOs() { glGenBuffers(1, &m_FrustumSSBO); glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_FrustumSSBO); - glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(Frustum)*m_NumberOfTiles, m_Frustums, GL_DYNAMIC_COPY); + glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(Frustum)*m_NumberOfTiles, nullptr, GL_DYNAMIC_COPY); glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0); GLERROR("m_FrustumSSBO"); glGenBuffers(1, &m_LightSSBO); glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_LightSSBO); - if(m_LightSources.size() > 0) { - glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(LightSource) * m_LightSources.size(), &(m_LightSources[0]), GL_DYNAMIC_COPY); - } + glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(LightSource) * 200, nullptr, GL_DYNAMIC_COPY); glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0); GLERROR("m_LightSSBO"); glGenBuffers(1, &m_LightGridSSBO); glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_LightGridSSBO); - glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(LightGrid)*m_NumberOfTiles, m_LightGrid, GL_DYNAMIC_COPY); + glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(LightGrid)*m_NumberOfTiles, nullptr, GL_DYNAMIC_COPY); glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0); GLERROR("m_LightGridSSBO"); - glGenBuffers(1, &m_LightOffsetSSBO); glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_LightOffsetSSBO); glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(m_LightOffset), &m_LightOffset, GL_DYNAMIC_COPY); From 982837052a2816eda655a55db3b9272ed0cd5d4b Mon Sep 17 00:00:00 2001 From: Tleety Date: Tue, 19 Jan 2016 14:44:17 +0100 Subject: [PATCH 10/15] DirectionalLight now correctly getting directions --- .../Engine/Rendering/DirectionalLightJob.h | 2 +- .../Schema/Components/DirectionalLight.xml | 5 +- .../Schema/Components/DirectionalLight.xsd | 2 - resources/Schema/Entities/EditorTestWorld.xml | 54 ++++++++++++++++++- resources/Schema/Types/Entity.xsd | 1 + resources/Shaders/ForwardPlus.frag.glsl | 2 +- resources/Shaders/ForwardPlus.vert.glsl | 2 +- 7 files changed, 59 insertions(+), 9 deletions(-) diff --git a/include/Engine/Rendering/DirectionalLightJob.h b/include/Engine/Rendering/DirectionalLightJob.h index f6942112..5f104ca5 100644 --- a/include/Engine/Rendering/DirectionalLightJob.h +++ b/include/Engine/Rendering/DirectionalLightJob.h @@ -16,7 +16,7 @@ struct DirectionalLightJob : RenderJob : RenderJob() { - Direction = glm::vec4(0,0,-1,0) * Transform::AbsoluteOrientation(m_World, transformComponent.EntityID); + Direction = glm::vec4(0,0,-1,0) * glm::inverse(Transform::AbsoluteOrientation(m_World, transformComponent.EntityID)); //Direction = glm::vec4((glm::vec3)directionalLightComponent["Direction"], 0.f); Color = (glm::vec4)directionalLightComponent["Color"]; Intensity = (double)directionalLightComponent["Intensity"]; diff --git a/resources/Schema/Components/DirectionalLight.xml b/resources/Schema/Components/DirectionalLight.xml index f66605c0..7f777ef8 100644 --- a/resources/Schema/Components/DirectionalLight.xml +++ b/resources/Schema/Components/DirectionalLight.xml @@ -1,5 +1,6 @@ - + + 0.8 true - \ No newline at end of file + \ No newline at end of file diff --git a/resources/Schema/Components/DirectionalLight.xsd b/resources/Schema/Components/DirectionalLight.xsd index dbfee38c..a14248a8 100644 --- a/resources/Schema/Components/DirectionalLight.xsd +++ b/resources/Schema/Components/DirectionalLight.xsd @@ -1,8 +1,6 @@ - - A directional light that shines bright like the future. diff --git a/resources/Schema/Entities/EditorTestWorld.xml b/resources/Schema/Entities/EditorTestWorld.xml index e44d238e..a482cf1b 100755 --- a/resources/Schema/Entities/EditorTestWorld.xml +++ b/resources/Schema/Entities/EditorTestWorld.xml @@ -13,6 +13,7 @@ + @@ -45,9 +46,58 @@ + - - + + + + + + + + + + + Models/DirectionalLightWidget.obj + + + + + + + + + + + + Models/Assault.obj + + + + + + + + + + Models/SecondaryWeapon4.fbx + + + + + + + + + + + + + + Models/Core/UnitSphere.obj + + + diff --git a/resources/Schema/Types/Entity.xsd b/resources/Schema/Types/Entity.xsd index 1aaa8497..14e3c5a4 100644 --- a/resources/Schema/Types/Entity.xsd +++ b/resources/Schema/Types/Entity.xsd @@ -27,6 +27,7 @@ + diff --git a/resources/Shaders/ForwardPlus.frag.glsl b/resources/Shaders/ForwardPlus.frag.glsl index 0298c969..e67a4d5d 100644 --- a/resources/Shaders/ForwardPlus.frag.glsl +++ b/resources/Shaders/ForwardPlus.frag.glsl @@ -88,7 +88,7 @@ LightResult CalcPointLightSource(vec4 lightPos, float lightRadius, vec4 lightCol LightResult CalcDirectionalLightSource(vec4 direction, vec4 color, float intensity, vec4 viewVec, vec4 vertNormal) { - vec4 L = normalize( -direction ); + vec4 L = normalize( -vec4(direction.xyz, 0) ); LightResult result; result.Diffuse = CalcDiffuse(color, L, vertNormal) * intensity; diff --git a/resources/Shaders/ForwardPlus.vert.glsl b/resources/Shaders/ForwardPlus.vert.glsl index 20ab9051..d3b8ba16 100644 --- a/resources/Shaders/ForwardPlus.vert.glsl +++ b/resources/Shaders/ForwardPlus.vert.glsl @@ -29,6 +29,6 @@ void main() Output.Position = Position; Output.TextureCoordinate = TextureCoords; - Output.Normal = Normal; + Output.Normal = vec3(M * vec4(Normal, 0.0)); Output.DiffuseColor = DiffuseVertexColor; } \ No newline at end of file From 893ef38237b0fde65d423ab15e4a3c68f62e201c Mon Sep 17 00:00:00 2001 From: Tleety Date: Tue, 19 Jan 2016 15:41:17 +0100 Subject: [PATCH 11/15] Removed world from renderer and moved depth-calculation code to modeljob --- include/Engine/Rendering/ModelJob.h | 3 +++ include/Engine/Rendering/Renderer.h | 6 ++---- src/Engine/Rendering/Renderer.cpp | 27 ++++++++------------------- src/Game/Game.cpp | 3 +-- 4 files changed, 14 insertions(+), 25 deletions(-) diff --git a/include/Engine/Rendering/ModelJob.h b/include/Engine/Rendering/ModelJob.h index a35a1721..62235b33 100644 --- a/include/Engine/Rendering/ModelJob.h +++ b/include/Engine/Rendering/ModelJob.h @@ -28,6 +28,9 @@ struct ModelJob : RenderJob Matrix = matrix; Color = modelComponent["Color"]; Entity = modelComponent.EntityID; + glm::vec3 abspos = Transform::AbsolutePosition(world, modelComponent.EntityID); + glm::vec3 worldpos = glm::vec3(camera->ViewMatrix() * glm::vec4(abspos, 1)); + Depth = worldpos.z; World = world; }; diff --git a/include/Engine/Rendering/Renderer.h b/include/Engine/Rendering/Renderer.h index 0bfe76f7..0006cca1 100644 --- a/include/Engine/Rendering/Renderer.h +++ b/include/Engine/Rendering/Renderer.h @@ -22,9 +22,8 @@ class Renderer : public IRenderer { public: - Renderer(EventBroker* eventBroker, World* world) + Renderer(EventBroker* eventBroker) : m_EventBroker(eventBroker) - , m_World(world) { } virtual void Initialize() override; @@ -36,7 +35,6 @@ public: private: //----------------------Variables----------------------// EventBroker* m_EventBroker; - World* m_World; Texture* m_ErrorTexture; Texture* m_WhiteTexture; @@ -62,7 +60,7 @@ private: void DrawScreenQuad(GLuint textureToDraw); static bool DepthSort(const std::shared_ptr &i, const std::shared_ptr &j) { return (i->Depth < j->Depth); } - void FillDepth(RenderScene& scene); + void SortRenderJobsByDepth(RenderScene &scene); void GenerateTexture(GLuint* texture, GLenum wrapping, GLenum filtering, glm::vec2 dimensions, GLint internalFormat, GLint format, GLenum type); //--------------------ShaderPrograms-------------------// ShaderProgram* m_BasicForwardProgram; diff --git a/src/Engine/Rendering/Renderer.cpp b/src/Engine/Rendering/Renderer.cpp index 9ae317ca..a642e667 100644 --- a/src/Engine/Rendering/Renderer.cpp +++ b/src/Engine/Rendering/Renderer.cpp @@ -98,9 +98,8 @@ void Renderer::Draw(RenderFrame& frame) m_PickingPass->ClearPicking(); for (auto scene : frame.RenderScenes){ - m_Camera = scene->Camera; // remove renderer camera when Editor uses the render scene cameras. - FillDepth(*scene); + SortRenderJobsByDepth(*scene); m_PickingPass->Draw(*scene); m_LightCullingPass->GenerateNewFrustum(*scene); m_LightCullingPass->FillLightList(*scene); @@ -147,6 +146,13 @@ void Renderer::InitializeTextures() m_WhiteTexture = ResourceManager::Load("Textures/Core/Blank.png"); } + +void Renderer::SortRenderJobsByDepth(RenderScene *scene) +{ + //Sort all forward jobs so transparency is good. + scene->ForwardJobs.sort(Renderer::DepthSort); +} + void Renderer::GenerateTexture(GLuint* texture, GLenum wrapping, GLenum filtering, glm::vec2 dimensions, GLint internalFormat, GLint format, GLenum type) { glGenTextures(1, texture); @@ -165,21 +171,4 @@ void Renderer::InitializeRenderPasses() m_PickingPass = new PickingPass(this, m_EventBroker); m_LightCullingPass = new LightCullingPass(this); m_DrawFinalPass = new DrawFinalPass(this, m_LightCullingPass); -} - -//Temp func -void Renderer::FillDepth(RenderScene& scene) -{ - for (auto job : scene.ForwardJobs) { - auto modelJob = std::dynamic_pointer_cast(job); - if(! modelJob) { - return; - } - - - glm::vec3 abspos = Transform::AbsolutePosition(modelJob->World, modelJob->Entity); - glm::vec3 worldpos = glm::vec3(scene.Camera->ViewMatrix() * glm::vec4(abspos, 1)); - modelJob->Depth = worldpos.z; - } - scene.ForwardJobs.sort(Renderer::DepthSort); } \ No newline at end of file diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index 54f7b69d..eb0e6b3f 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -64,8 +64,7 @@ Game::Game(int argc, char* argv[]) EntityFileParser fp(file); fp.MergeEntities(m_World); } - //SO MUCH TEMP PLEASE REMOVE ME OMFG VIKTOR HELP - m_Renderer->m_World = m_World; + // Create Octrees m_OctreeCollision = new Octree(AABB(glm::vec3(-100), glm::vec3(100)), 4); From a8ffb240404615b51127a0f647cf8130665bf41f Mon Sep 17 00:00:00 2001 From: Tleety Date: Tue, 19 Jan 2016 16:08:09 +0100 Subject: [PATCH 12/15] latest commit fix --- include/Engine/Rendering/ModelJob.h | 1 + resources/Schema/Entities/EditorTestWorld.xml | 10 +++++----- src/Engine/Rendering/Renderer.cpp | 4 ++-- src/Game/Game.cpp | 2 +- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/include/Engine/Rendering/ModelJob.h b/include/Engine/Rendering/ModelJob.h index 62235b33..a88530e7 100644 --- a/include/Engine/Rendering/ModelJob.h +++ b/include/Engine/Rendering/ModelJob.h @@ -12,6 +12,7 @@ #include "../Core/ResourceManager.h" #include "Camera.h" #include "../Core/World.h" +#include "../Core/Transform.h" struct ModelJob : RenderJob { diff --git a/resources/Schema/Entities/EditorTestWorld.xml b/resources/Schema/Entities/EditorTestWorld.xml index a482cf1b..733e643b 100755 --- a/resources/Schema/Entities/EditorTestWorld.xml +++ b/resources/Schema/Entities/EditorTestWorld.xml @@ -48,8 +48,8 @@ - - + + @@ -61,8 +61,8 @@ Models/DirectionalLightWidget.obj - - + + @@ -83,7 +83,7 @@ Models/SecondaryWeapon4.fbx - + diff --git a/src/Engine/Rendering/Renderer.cpp b/src/Engine/Rendering/Renderer.cpp index a642e667..8c30adfd 100644 --- a/src/Engine/Rendering/Renderer.cpp +++ b/src/Engine/Rendering/Renderer.cpp @@ -147,10 +147,10 @@ void Renderer::InitializeTextures() } -void Renderer::SortRenderJobsByDepth(RenderScene *scene) +void Renderer::SortRenderJobsByDepth(RenderScene &scene) { //Sort all forward jobs so transparency is good. - scene->ForwardJobs.sort(Renderer::DepthSort); + scene.ForwardJobs.sort(Renderer::DepthSort); } void Renderer::GenerateTexture(GLuint* texture, GLenum wrapping, GLenum filtering, glm::vec2 dimensions, GLint internalFormat, GLint format, GLenum type) diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index eb0e6b3f..1985d9dc 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -29,7 +29,7 @@ Game::Game(int argc, char* argv[]) // Create the renderer - m_Renderer = new Renderer(m_EventBroker, m_World); + m_Renderer = new Renderer(m_EventBroker); m_Renderer->SetFullscreen(m_Config->Get("Video.Fullscreen", false)); m_Renderer->SetVSYNC(m_Config->Get("Video.VSYNC", false)); m_Renderer->SetResolution(Rectangle::Rectangle( From d2263b0c90ada29134abea3c5f204e1a68f6b75b Mon Sep 17 00:00:00 2001 From: Tleety Date: Tue, 19 Jan 2016 16:09:30 +0100 Subject: [PATCH 13/15] Assets added --- assets | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets b/assets index 2fca9181..187bc629 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 2fca918162535c859c6adbe433e69fad8ea931b6 +Subproject commit 187bc62969fd753216441a12b179f520ce5c6b05 From a124ab24004cc3f9a2891c561f9617f04e9723f8 Mon Sep 17 00:00:00 2001 From: Tleety Date: Tue, 19 Jan 2016 16:11:56 +0100 Subject: [PATCH 14/15] Some asset shit. Fixed testworld to use the right model. --- assets | 2 +- resources/Schema/Entities/EditorTestWorld.xml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/assets b/assets index 187bc629..6ffb46e1 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 187bc62969fd753216441a12b179f520ce5c6b05 +Subproject commit 6ffb46e155c8f013241cd1507098c94900ec2448 diff --git a/resources/Schema/Entities/EditorTestWorld.xml b/resources/Schema/Entities/EditorTestWorld.xml index 733e643b..2e95d626 100755 --- a/resources/Schema/Entities/EditorTestWorld.xml +++ b/resources/Schema/Entities/EditorTestWorld.xml @@ -49,7 +49,7 @@ - + @@ -80,7 +80,7 @@ - Models/SecondaryWeapon4.fbx + Models/SecondaryWeapon.fbx From 5d6d0f82b9e733c86bdb0bca7721734fcf6471fa Mon Sep 17 00:00:00 2001 From: Tleety Date: Tue, 19 Jan 2016 16:48:59 +0100 Subject: [PATCH 15/15] Removed some debug code --- resources/Shaders/ForwardPlus.frag.glsl | 18 +++++++++++------- src/Engine/Rendering/LightCullingPass.cpp | 2 -- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/resources/Shaders/ForwardPlus.frag.glsl b/resources/Shaders/ForwardPlus.frag.glsl index e67a4d5d..78a2125e 100644 --- a/resources/Shaders/ForwardPlus.frag.glsl +++ b/resources/Shaders/ForwardPlus.frag.glsl @@ -114,12 +114,13 @@ void main() int start = int(LightGrids.Data[currentTile].Start); int amount = int(LightGrids.Data[currentTile].Amount); - //for(int i = 0; i < 3; i++) + for(int i = start; i < start + amount; i++) { + int l = int(LightIndex[i]); LightSource light = LightSources.List[l]; - LightResult result; + LightResult result; if(light.Type == 1) { // point result = CalcPointLightSource(V * light.Position, light.Radius, light.Color, light.Intensity, viewVec, position, normal, light.Falloff); } else if (light.Type == 2) { //Directional @@ -136,12 +137,15 @@ void main() //fragmentColor += Input.DiffuseColor + vec4(0.0, LightGrids.Data[currentTile].Amount/3, 0, 1); //fragmentColor = texel * Input.DiffuseColor * Color; //fragmentColor += vec4(currentTile/3600.f, 0, 0, 1); - if(int(gl_FragCoord.x)%16 == 0 || int(gl_FragCoord.y)%16 == 0 ) { - //fragmentColor += vec4(0.5, 0, 0, 0); - } else { - //fragmentColor += vec4(LightGrids.Data[int(tilePos.x + tilePos.y*80)].Amount/3.0, 0, 0, 1); - } + //Tiled Debug Code + /* + if(int(gl_FragCoord.x)%16 == 0 || int(gl_FragCoord.y)%16 == 0 ) { + fragmentColor += vec4(0.5, 0, 0, 0); + } else { + fragmentColor += vec4(LightGrids.Data[int(tilePos.x + tilePos.y*80)].Amount/LightSources.List.length(), 0, 0, 1); + } + */ } diff --git a/src/Engine/Rendering/LightCullingPass.cpp b/src/Engine/Rendering/LightCullingPass.cpp index 2a2e2383..a68fe4d1 100644 --- a/src/Engine/Rendering/LightCullingPass.cpp +++ b/src/Engine/Rendering/LightCullingPass.cpp @@ -95,7 +95,6 @@ void LightCullingPass::FillLightList(RenderScene& scene) //p.Padding = 123.f; p.Type = LightSource::Point; m_LightSources.push_back(p); - } } for(auto &job : scene.DirectionalLightJobs) { @@ -107,7 +106,6 @@ void LightCullingPass::FillLightList(RenderScene& scene) p.Intensity = directionalLightJob->Intensity; p.Type = LightSource::Directional; m_LightSources.push_back(p); - } } }