From 0ca11f9bee1cd918acf6ce8247a495442009fec9 Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Sun, 21 Nov 2021 17:49:23 +0500 Subject: Moved face lighting to lighting pass --- cwd/assets/altcraft/shaders/frag/face.fs | 19 +++++++++++-------- cwd/assets/altcraft/shaders/frag/light.fs | 16 ++++++++++++++-- cwd/assets/altcraft/shaders/frag/sky.fs | 14 ++++++++++---- cwd/assets/altcraft/shaders/vert/face.vs | 24 +++++++++++------------- src/GalOgl.cpp | 5 +++++ src/Render.cpp | 4 +++- src/RenderConfigs.cpp | 27 +++++++++++++++++++++++++-- src/RenderConfigs.hpp | 10 ++++++++-- src/RendererWorld.cpp | 1 + src/RendererWorld.hpp | 2 ++ 10 files changed, 90 insertions(+), 32 deletions(-) diff --git a/cwd/assets/altcraft/shaders/frag/face.fs b/cwd/assets/altcraft/shaders/frag/face.fs index 2120697..57ea0ec 100644 --- a/cwd/assets/altcraft/shaders/frag/face.fs +++ b/cwd/assets/altcraft/shaders/frag/face.fs @@ -1,21 +1,24 @@ #version 330 core -in VS_OUT { - vec3 Texture; - vec3 Color; - vec3 faceNormal; -} fs_in; +in vec3 faceTexture; +in vec3 faceAddColor; +in vec3 faceNormal; +in vec2 faceLight; layout (location = 0) out vec4 color; layout (location = 1) out vec4 normal; +layout (location = 2) out vec4 addColor; +layout (location = 3) out vec4 light; uniform sampler2DArray textureAtlas; void main() { - vec4 col = texture(textureAtlas, fs_in.Texture); + vec4 col = texture(textureAtlas, faceTexture); if (col.a < 0.3) discard; - color = vec4(col.rgb * fs_in.Color, 1.0f); - normal = vec4(fs_in.faceNormal, 1.0f); + color = vec4(col.rgb, 1.0f); + normal = vec4(faceNormal, 1.0f); + addColor = vec4(faceAddColor, 1.0f); + light = vec4(faceLight / 15.0f, 0.0f, 1.0f); } diff --git a/cwd/assets/altcraft/shaders/frag/light.fs b/cwd/assets/altcraft/shaders/frag/light.fs index 73686cc..12ab841 100644 --- a/cwd/assets/altcraft/shaders/frag/light.fs +++ b/cwd/assets/altcraft/shaders/frag/light.fs @@ -6,11 +6,23 @@ in vec2 uv; uniform sampler2D color; uniform sampler2D normal; +uniform sampler2D addColor; +uniform sampler2D light; uniform sampler2D depthStencil; +uniform float dayTime; + void main() { vec4 c = texture(color, uv); vec4 n = texture(normal, uv); - float d = texture(depthStencil, uv).r; - fragColor = n; + vec4 ac = texture(addColor, uv); + vec4 an = texture(light, uv); + float d = 1.0f - texture(depthStencil, uv).r; + + float faceLight = an.r; + float skyLight = an.g; + float lightLevel = clamp(faceLight + skyLight * dayTime, 0.2f, 1.0f); + vec3 faceColor = mix(ac.rgb * lightLevel, vec3(1,1,1) * lightLevel, float(ac.rgb == vec3(0,0,0))); + + fragColor = vec4(c.rgb * faceColor, 1.0f); } diff --git a/cwd/assets/altcraft/shaders/frag/sky.fs b/cwd/assets/altcraft/shaders/frag/sky.fs index 53e0cf4..3763bdb 100644 --- a/cwd/assets/altcraft/shaders/frag/sky.fs +++ b/cwd/assets/altcraft/shaders/frag/sky.fs @@ -2,7 +2,10 @@ in vec3 pos; -out vec4 fragColor; +layout (location = 0) out vec4 color; +layout (location = 1) out vec4 normal; +layout (location = 2) out vec4 addColor; +layout (location = 3) out vec4 light; uniform sampler2DArray textureAtlas; uniform float DayTime; @@ -46,7 +49,10 @@ vec4 Moon() { void main() { vec4 starColor = vec4(0.0f, 0.04f, 0.06f, 1.0f); - fragColor = mix(starColor, DaySkyColor, DayTime); - fragColor += Sun(); - fragColor += Moon(); + color = vec4(mix(starColor, DaySkyColor, DayTime).rgb, 1.0f); + color += vec4(Sun().rgb, 1.0f); + color += vec4(Moon().rgb, 1.0f); + normal = vec4(0.0f, 0.0f, 0.0f, 1.0f); + addColor = vec4(0.0f, 0.0f, 0.0f, 1.0f); + light = vec4(1.0f, 1.0f, 0.0f, 1.0f); } diff --git a/cwd/assets/altcraft/shaders/vert/face.vs b/cwd/assets/altcraft/shaders/vert/face.vs index 21f7110..ac92831 100644 --- a/cwd/assets/altcraft/shaders/vert/face.vs +++ b/cwd/assets/altcraft/shaders/vert/face.vs @@ -8,23 +8,21 @@ in float animation; in vec3 color; in vec2 light; -out VS_OUT { - vec3 Texture; - vec3 Color; - vec3 faceNormal; -} vs_out; +out vec3 faceTexture; +out vec3 faceNormal; +out vec3 faceAddColor; +out vec2 faceLight; -uniform float GlobalTime; uniform mat4 projView; -uniform float DayTime; -uniform float MinLightLevel; +uniform float GlobalTime; void main() { gl_Position = projView * vec4(position[gl_VertexID], 1.0f); - vs_out.Texture = vec3(uv[gl_VertexID], uvLayer); - vs_out.Texture.y -= (uv[2].y - uv[0].y) * trunc(mod(GlobalTime * 4.0f, animation)); - float faceLight = clamp(light.x / 15.0 + (light.y / 15.0) * DayTime, MinLightLevel, 1.0); - vs_out.Color = mix(color.rgb * faceLight, vec3(1,1,1) * faceLight, float(color == vec3(0,0,0))); - vs_out.faceNormal = normal; + faceTexture = vec3(uv[gl_VertexID], uvLayer); + faceTexture.y -= (uv[2].y - uv[0].y) * trunc(mod(GlobalTime * 4.0f, animation)); + + faceNormal = normal; + faceAddColor = color; + faceLight = light; } diff --git a/src/GalOgl.cpp b/src/GalOgl.cpp index c250539..75369ab 100644 --- a/src/GalOgl.cpp +++ b/src/GalOgl.cpp @@ -1256,6 +1256,11 @@ struct ImplOgl : public Impl { size_t attribSize = GalTypeGetSize(type); for (size_t i = 0; i < count; i++) { + if (location < 0) { + vertexSize += attribSize; + continue; + } + pipeline->vertexBindCmds.push_back({ bufferId, static_cast(location + i), diff --git a/src/Render.cpp b/src/Render.cpp index 96c1ed8..48114b0 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -209,8 +209,10 @@ void Render::RenderFrame() { if (isWireframe) Gal::GetImplementation()->SetWireframe(true); - if (renderWorld) + if (renderWorld) { world->Render(static_cast(windowWidth) / static_cast(windowHeight)); + gbuffer->SetDayTime(world->shaderDayTime); + } if (isWireframe) Gal::GetImplementation()->SetWireframe(false); diff --git a/src/RenderConfigs.cpp b/src/RenderConfigs.cpp index ebdbaef..86418c2 100644 --- a/src/RenderConfigs.cpp +++ b/src/RenderConfigs.cpp @@ -5,24 +5,44 @@ Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH) { auto gal = Gal::GetImplementation(); - auto colorConf = gal->CreateTexture2DConfig(geomW, geomH, Gal::Format::R8G8B8A8); + auto colorConf = gal->CreateTexture2DConfig(geomW, geomH, Gal::Format::R8G8B8); + colorConf->SetMinFilter(Gal::Filtering::Bilinear); + colorConf->SetMaxFilter(Gal::Filtering::Bilinear); color = gal->BuildTexture(colorConf); - auto normalConf = gal->CreateTexture2DConfig(geomW, geomH, Gal::Format::R8G8B8A8); + auto normalConf = gal->CreateTexture2DConfig(geomW, geomH, Gal::Format::R8G8B8); + normalConf->SetMinFilter(Gal::Filtering::Bilinear); + normalConf->SetMaxFilter(Gal::Filtering::Bilinear); normal = gal->BuildTexture(normalConf); + auto addColorConf = gal->CreateTexture2DConfig(geomW, geomH, Gal::Format::R8G8B8); + addColorConf->SetMinFilter(Gal::Filtering::Bilinear); + addColorConf->SetMaxFilter(Gal::Filtering::Bilinear); + addColor = gal->BuildTexture(addColorConf); + + auto lightConf = gal->CreateTexture2DConfig(geomW, geomH, Gal::Format::R8G8B8); + lightConf->SetMinFilter(Gal::Filtering::Bilinear); + lightConf->SetMaxFilter(Gal::Filtering::Bilinear); + light = gal->BuildTexture(lightConf); + auto dsConf = gal->CreateTexture2DConfig(geomW, geomH, Gal::Format::D24S8); + dsConf->SetMinFilter(Gal::Filtering::Bilinear); + dsConf->SetMaxFilter(Gal::Filtering::Bilinear); depthStencil = gal->BuildTexture(dsConf); auto geomFbConf = gal->CreateFramebufferConfig(); geomFbConf->SetTexture(0, color); geomFbConf->SetTexture(1, normal); + geomFbConf->SetTexture(2, addColor); + geomFbConf->SetTexture(3, light); geomFbConf->SetDepthStencil(depthStencil); geomFramebuffer = gal->BuildFramebuffer(geomFbConf); geomFramebuffer->SetViewport(0, 0, geomW, geomH); auto finalColorConf = gal->CreateTexture2DConfig(lightW, lightH, Gal::Format::R8G8B8A8); + finalColorConf->SetMinFilter(Gal::Filtering::Bilinear); + finalColorConf->SetMaxFilter(Gal::Filtering::Bilinear); finalColor = gal->BuildTexture(finalColorConf); auto lightFbConf = gal->CreateFramebufferConfig(); @@ -43,8 +63,11 @@ Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH) { auto lightPPC = gal->CreatePipelineConfig(); lightPPC->SetTarget(lightFramebuffer); lightPPC->AddStaticTexture("color", color); + lightPPC->AddStaticTexture("addColor", addColor); lightPPC->AddStaticTexture("normal", normal); + lightPPC->AddStaticTexture("light", light); lightPPC->AddStaticTexture("depthStencil", depthStencil); + lightPPC->AddShaderParameter("dayTime", Gal::Type::Float); lightPPC->SetVertexShader(gal->LoadVertexShader(vertexSource)); lightPPC->SetPixelShader(gal->LoadPixelShader(pixelSource)); diff --git a/src/RenderConfigs.hpp b/src/RenderConfigs.hpp index 6279169..94e96bb 100644 --- a/src/RenderConfigs.hpp +++ b/src/RenderConfigs.hpp @@ -7,8 +7,10 @@ class Gbuffer { std::shared_ptr lightBuffer; std::shared_ptr lightPipeline; std::shared_ptr lightPipelineInstance; - std::shared_ptr color; - std::shared_ptr normal; + std::shared_ptr color; //RGB - color + std::shared_ptr normal; //RGB - normal + std::shared_ptr addColor; //RGB - addColor + std::shared_ptr light; //R - faceLight, G - skyLight, B - unused std::shared_ptr depthStencil; std::shared_ptr geomFramebuffer; @@ -35,4 +37,8 @@ public: geomFramebuffer->Clear(); lightFramebuffer->Clear(); } + + void SetDayTime(float dayTime) { + lightPipeline->SetShaderParameter("dayTime", dayTime); + } }; diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp index 9055729..d2c69fd 100644 --- a/src/RendererWorld.cpp +++ b/src/RendererWorld.cpp @@ -366,6 +366,7 @@ void RendererWorld::Render(float screenRatio) { float timePassed = (dayTime - moonriseMin); mixLevel = 1.0 - (timePassed / moonriseLength); } + shaderDayTime = mixLevel; skyPipeline->Activate(); skyPipeline->SetShaderParameter("projView", projView); diff --git a/src/RendererWorld.hpp b/src/RendererWorld.hpp index e645b30..a6fca06 100644 --- a/src/RendererWorld.hpp +++ b/src/RendererWorld.hpp @@ -63,6 +63,8 @@ public: double MaxRenderingDistance; + float shaderDayTime; + void Update(double timeToUpdate); }; -- cgit v1.2.3