From 96013162fa53bd13ae9aea9f924f9dc0e52d12db Mon Sep 17 00:00:00 2001 From: Armin Burgmeier Date: Sun, 27 Nov 2016 19:57:01 -0800 Subject: [PATCH] Instantiate C4ShaderCall within C4FoWDrawStrategy This will allow us to use different shaders for different passes of the draw. --- src/landscape/fow/C4FoW.cpp | 7 +------ src/landscape/fow/C4FoWDrawStrategy.cpp | 16 ++++++++++++---- src/landscape/fow/C4FoWDrawStrategy.h | 9 ++++++--- src/landscape/fow/C4FoWLight.cpp | 4 ++-- src/landscape/fow/C4FoWLight.h | 2 +- 5 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/landscape/fow/C4FoW.cpp b/src/landscape/fow/C4FoW.cpp index 7571635de..587b13785 100644 --- a/src/landscape/fow/C4FoW.cpp +++ b/src/landscape/fow/C4FoW.cpp @@ -249,14 +249,9 @@ void C4FoW::Render(C4FoWRegion *pRegion, const C4TargetFacet *pOnScreen, C4Playe assert(pShader); if (!pShader) return; - C4ShaderCall Call(pShader); - Call.Start(); - Call.SetUniformMatrix4x4(C4FoWRSU_ProjectionMatrix, projectionMatrix); - for (C4FoWLight *pLight = pLights; pLight; pLight = pLight->getNext()) if (pLight->IsVisibleForPlayer(pPlr)) - pLight->Render(pRegion, pOnScreen, Call); + pLight->Render(pRegion, pOnScreen, projectionMatrix, *pShader); - Call.Finish(); #endif } diff --git a/src/landscape/fow/C4FoWDrawStrategy.cpp b/src/landscape/fow/C4FoWDrawStrategy.cpp index 3f48e19bf..6a1d0a407 100644 --- a/src/landscape/fow/C4FoWDrawStrategy.cpp +++ b/src/landscape/fow/C4FoWDrawStrategy.cpp @@ -139,8 +139,12 @@ void C4FoWDrawLightTextureStrategy::Begin(const C4FoWRegion* regionPar) } } -void C4FoWDrawLightTextureStrategy::End(C4ShaderCall& call) +void C4FoWDrawLightTextureStrategy::End(const StdProjectionMatrix& projectionMatrix, const C4Shader& renderShader) { + C4ShaderCall call(&renderShader); + call.Start(); + call.SetUniformMatrix4x4(C4FoWRSU_ProjectionMatrix, projectionMatrix); + // If we have nothing to draw (e.g. directly after initialization), abort early. if (vertices.empty()) return; @@ -254,7 +258,7 @@ void C4FoWDrawLightTextureStrategy::End(C4ShaderCall& call) // Assume the capacity stays the same: vertices.resize(0); - C4FoWDrawStrategy::End(call); + C4FoWDrawStrategy::End(projectionMatrix, renderShader); } void C4FoWDrawLightTextureStrategy::DrawVertex(float x, float y, bool shadow) @@ -359,8 +363,12 @@ void C4FoWDrawWireframeStrategy::Begin(const C4FoWRegion* region) { } -void C4FoWDrawWireframeStrategy::End(C4ShaderCall& call) +void C4FoWDrawWireframeStrategy::End(const StdProjectionMatrix& projectionMatrix, const C4Shader& renderShader) { + C4ShaderCall call(&renderShader); + call.Start(); + call.SetUniformMatrix4x4(C4FoWRSU_ProjectionMatrix, projectionMatrix); + // If we have nothing to draw (e.g. directly after initialization), abort early. if (vertices.empty()) return; @@ -421,7 +429,7 @@ void C4FoWDrawWireframeStrategy::End(C4ShaderCall& call) // Assume the capacity stays the same: vertices.resize(0); - C4FoWDrawStrategy::End(call); + C4FoWDrawStrategy::End(projectionMatrix, renderShader); } void C4FoWDrawWireframeStrategy::DrawVertex(Vertex& vtx) diff --git a/src/landscape/fow/C4FoWDrawStrategy.h b/src/landscape/fow/C4FoWDrawStrategy.h index 1830839d8..099d43e0b 100644 --- a/src/landscape/fow/C4FoWDrawStrategy.h +++ b/src/landscape/fow/C4FoWDrawStrategy.h @@ -84,7 +84,10 @@ public: /** Called before each rendering pass */ virtual void Begin(const C4FoWRegion* region) = 0; /** Called after each rendering pass */ - virtual void End(C4ShaderCall& call) { triangulator.Reset(); } + /* TODO: shaders should be created in drawstrategy, but: make sure + * there is only one shader per class + * (static? but then when to clean up?). */ + virtual void End(const StdProjectionMatrix&, const C4Shader& renderShader) { triangulator.Reset(); } virtual void DrawLightVertex(float x, float y) { triangulator.AddVertex(); } virtual void DrawDarkVertex(float x, float y) { triangulator.AddVertex(); } @@ -124,7 +127,7 @@ public: virtual void DrawLightVertex(float x, float y); virtual void DrawDarkVertex(float x, float y); virtual void Begin(const C4FoWRegion* region); - virtual void End(C4ShaderCall& call); + virtual void End(const StdProjectionMatrix&, const C4Shader& renderShader); private: void DrawVertex(float x, float y, bool shadeLight); @@ -161,7 +164,7 @@ public: virtual void DrawLightVertex(float x, float y); virtual void DrawDarkVertex(float x, float y); virtual void Begin(const C4FoWRegion* region); - virtual void End(C4ShaderCall& call); + virtual void End(const StdProjectionMatrix&, const C4Shader& renderShader); private: struct Vertex { diff --git a/src/landscape/fow/C4FoWLight.cpp b/src/landscape/fow/C4FoWLight.cpp index 6a7ecfec9..fceafd496 100644 --- a/src/landscape/fow/C4FoWLight.cpp +++ b/src/landscape/fow/C4FoWLight.cpp @@ -119,7 +119,7 @@ void C4FoWLight::Update(C4Rect Rec) sections[i]->Update(Rec); } -void C4FoWLight::Render(C4FoWRegion *region, const C4TargetFacet *onScreen, C4ShaderCall& call) +void C4FoWLight::Render(C4FoWRegion *region, const C4TargetFacet *onScreen, const StdProjectionMatrix& projectionMatrix, const C4Shader& renderShader) { TriangleList triangles; @@ -157,7 +157,7 @@ void C4FoWLight::Render(C4FoWRegion *region, const C4TargetFacet *onScreen, C4Sh DrawFade(strategy.get(), triangles); DrawIntermediateFadeTriangles(strategy.get(), triangles); - strategy->End(call); + strategy->End(projectionMatrix, renderShader); } void C4FoWLight::CalculateFanMaxed(TriangleList &triangles) const diff --git a/src/landscape/fow/C4FoWLight.h b/src/landscape/fow/C4FoWLight.h index eb633b58b..25cf9eaa0 100644 --- a/src/landscape/fow/C4FoWLight.h +++ b/src/landscape/fow/C4FoWLight.h @@ -85,7 +85,7 @@ public: /** Update all light beams within the given rectangle for this light */ void Update(C4Rect r); /** Render this light*/ - void Render(class C4FoWRegion *pRegion, const C4TargetFacet *pOnScreen, C4ShaderCall& call); + void Render(class C4FoWRegion *pRegion, const C4TargetFacet *pOnScreen, const StdProjectionMatrix& projectionMatrix, const C4Shader& renderShader); bool IsVisibleForPlayer(C4Player *player) const; // check if attached to an object that is not hostile to the given player