From b768473b44cd8a16b474ab008f1e39d267df1c2d Mon Sep 17 00:00:00 2001 From: parax0 Date: Sun, 10 Jan 2016 06:17:12 -0700 Subject: [PATCH] Added lightmap multiplier to allow applying basic lighting on world geometry --- src/Core/OpenGL/CShaderGenerator.cpp | 8 +++++++- src/Core/Render/CGraphics.cpp | 4 +++- src/Core/Render/CGraphics.h | 1 + src/Core/Resource/Factory/CMaterialLoader.cpp | 9 +++++---- src/Core/Scene/CModelNode.cpp | 1 + src/Core/Scene/CSceneNode.cpp | 5 +++-- src/Core/Scene/CStaticNode.cpp | 19 ++++++++++++++++--- 7 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/Core/OpenGL/CShaderGenerator.cpp b/src/Core/OpenGL/CShaderGenerator.cpp index 67842d12..4b1a4949 100644 --- a/src/Core/OpenGL/CShaderGenerator.cpp +++ b/src/Core/OpenGL/CShaderGenerator.cpp @@ -329,6 +329,7 @@ bool CShaderGenerator::CreatePixelShader(const CMaterial& Mat) << " vec4 KonstColors[4];\n" << " vec4 TevColor;\n" << " vec4 TintColor;\n" + << " float LightmapMultiplier;\n" << "};\n\n"; for (u32 iPass = 0; iPass < PassCount; iPass++) @@ -374,6 +375,11 @@ bool CShaderGenerator::CreatePixelShader(const CMaterial& Mat) else if (PassType == "BLOL") ShaderCode << ".rgbg"; + // Apply lightmap multiplier + if ( (PassType == "DIFF") || + (PassType == "CUST" && (Mat.Options() & CMaterial::eLightmap) && iPass == 0) ) + ShaderCode << " * LightmapMultiplier"; + ShaderCode << ";\n"; ShaderCode << " Konst = vec4(" << gkKonstColor[pPass->KColorSel()] << ", " << gkKonstAlpha[pPass->KAlphaSel()] << ");\n"; @@ -398,7 +404,7 @@ bool CShaderGenerator::CreatePixelShader(const CMaterial& Mat) << ".rgb = "; ShaderCode << "clamp(vec3(TevInD.rgb + ((1.0 - TevInC.rgb) * TevInA.rgb + TevInC.rgb * TevInB.rgb))"; - if ((PassType == "CLR ") && (Lightmap)) ShaderCode << "* 2.0"; // Apply tevscale 2.0 on the color pass if lightmap is present + if ((PassType == "CLR ") && (Lightmap)) ShaderCode << "* (2.0 - (1.0 - LightmapMultiplier))"; // Apply tevscale 2.0 on the color pass if lightmap is present. Scale so we don't apply if lightmaps are off. ShaderCode << ", vec3(0, 0, 0), vec3(1.0, 1.0, 1.0));\n"; ShaderCode << " // Alpha Combine\n" diff --git a/src/Core/Render/CGraphics.cpp b/src/Core/Render/CGraphics.cpp index 239d290c..8cf219b4 100644 --- a/src/Core/Render/CGraphics.cpp +++ b/src/Core/Render/CGraphics.cpp @@ -166,8 +166,10 @@ void CGraphics::SetupAmbientColor() { if (sLightMode == eWorldLighting) sVertexBlock.COLOR0_Amb = sAreaAmbientColor * sWorldLightMultiplier; - else + else if (sLightMode == eBasicLighting) sVertexBlock.COLOR0_Amb = skDefaultAmbientColor; + else + sVertexBlock.COLOR0_Amb = CColor::skWhite; } void CGraphics::SetIdentityMVP() diff --git a/src/Core/Render/CGraphics.h b/src/Core/Render/CGraphics.h index 09ab9b0c..67014e9a 100644 --- a/src/Core/Render/CGraphics.h +++ b/src/Core/Render/CGraphics.h @@ -56,6 +56,7 @@ public: CColor Konst[4]; CColor TevColor; CColor TintColor; + float LightmapMultiplier; }; static SPixelBlock sPixelBlock; diff --git a/src/Core/Resource/Factory/CMaterialLoader.cpp b/src/Core/Resource/Factory/CMaterialLoader.cpp index b4751c28..4d977d09 100644 --- a/src/Core/Resource/Factory/CMaterialLoader.cpp +++ b/src/Core/Resource/Factory/CMaterialLoader.cpp @@ -405,15 +405,16 @@ void CMaterialLoader::CreateCorruptionPasses(CMaterial *pMat) // Color Map (Diffuse) if (Type == "CLR ") { + pPass->SetRasSel(eRasColor0A0); + if (Lightmap) { - pPass->SetColorInputs(eZeroRGB, eColor0RGB, eTextureRGB, ePrevRGB); + pPass->SetColorInputs(eZeroRGB, eColor0RGB, eTextureRGB, eZeroRGB); } else { - pPass->SetColorInputs(eZeroRGB, eRasRGB, eTextureRGB, ePrevRGB); - pPass->SetRasSel(eRasColor0A0); + pPass->SetColorInputs(eZeroRGB, eRasRGB, eTextureRGB, eZeroRGB); } @@ -439,7 +440,7 @@ void CMaterialLoader::CreateCorruptionPasses(CMaterial *pMat) // Lightmap else if (Type == "DIFF") { - pPass->SetColorInputs(eZeroRGB, eKonstRGB, eTextureRGB, eZeroRGB); + pPass->SetColorInputs(eZeroRGB, eKonstRGB, eTextureRGB, eRasRGB); pPass->SetAlphaInputs(eZeroAlpha, eZeroAlpha, eZeroAlpha, eKonstAlpha); pPass->SetColorOutput(eColor0Reg); pPass->SetAlphaOutput(eColor0Reg); diff --git a/src/Core/Scene/CModelNode.cpp b/src/Core/Scene/CModelNode.cpp index 14019e4d..dc1c91f2 100644 --- a/src/Core/Scene/CModelNode.cpp +++ b/src/Core/Scene/CModelNode.cpp @@ -51,6 +51,7 @@ void CModelNode::Draw(FRenderOptions Options, int ComponentIndex, const SViewInf CGraphics::sPixelBlock.TevColor = CColor::skWhite; CGraphics::sPixelBlock.TintColor = TintColor(ViewInfo); + CGraphics::sPixelBlock.LightmapMultiplier = 1.f; LoadModelMatrix(); if (ComponentIndex < 0) diff --git a/src/Core/Scene/CSceneNode.cpp b/src/Core/Scene/CSceneNode.cpp index 03efd7b6..5d1e5326 100644 --- a/src/Core/Scene/CSceneNode.cpp +++ b/src/Core/Scene/CSceneNode.cpp @@ -187,8 +187,8 @@ void CSceneNode::LoadLights(const SViewInfo& ViewInfo) switch (Mode) { case CGraphics::eNoLighting: - // No lighting: default ambient color, no dynamic lights - CGraphics::sVertexBlock.COLOR0_Amb = CGraphics::skDefaultAmbientColor; + // No lighting: full white ambient, no dynamic lights + CGraphics::sVertexBlock.COLOR0_Amb = CColor::skWhite; break; case CGraphics::eBasicLighting: @@ -206,6 +206,7 @@ void CSceneNode::LoadLights(const SViewInfo& ViewInfo) break; } + CGraphics::sPixelBlock.LightmapMultiplier = (Mode == CGraphics::eWorldLighting ? 1.f : 0.f); CGraphics::UpdateLightBlock(); } diff --git a/src/Core/Scene/CStaticNode.cpp b/src/Core/Scene/CStaticNode.cpp index 6cf99234..99e5f9cf 100644 --- a/src/Core/Scene/CStaticNode.cpp +++ b/src/Core/Scene/CStaticNode.cpp @@ -49,12 +49,25 @@ void CStaticNode::Draw(FRenderOptions Options, int ComponentIndex, const SViewIn if (!mpModel) return; bool IsLightingEnabled = CGraphics::sLightMode == CGraphics::eWorldLighting || ViewInfo.GameMode; - CGraphics::sVertexBlock.COLOR0_Amb = (IsLightingEnabled ? CColor::skBlack : CColor::skWhite); + + if (IsLightingEnabled) + { + CGraphics::sNumLights = 0; + CGraphics::sVertexBlock.COLOR0_Amb = CColor::skBlack; + CGraphics::sPixelBlock.LightmapMultiplier = 1.0f; + CGraphics::UpdateLightBlock(); + } + + else + { + LoadLights(ViewInfo); + if (CGraphics::sLightMode == CGraphics::eNoLighting) + CGraphics::sVertexBlock.COLOR0_Amb = CColor::skWhite; + } + float Mul = CGraphics::sWorldLightMultiplier; CGraphics::sPixelBlock.TevColor = CColor(Mul,Mul,Mul); CGraphics::sPixelBlock.TintColor = TintColor(ViewInfo); - CGraphics::sNumLights = 0; - CGraphics::UpdateLightBlock(); LoadModelMatrix(); if (ComponentIndex < 0)