From cdfdcecd046c494470bfb4cc1de66f6cfca5efec Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Fri, 12 Aug 2016 23:38:50 +0300 Subject: [PATCH] Handle clipping in Pipeline and Renderer --- source/pipeline.cpp | 9 ++++++++- source/pipeline.h | 4 ++++ source/renderer.cpp | 35 +++++++++++++++++++++++++++++++++++ source/renderer.h | 7 ++++++- 4 files changed, 53 insertions(+), 2 deletions(-) diff --git a/source/pipeline.cpp b/source/pipeline.cpp index bcbbb05f..1833001f 100644 --- a/source/pipeline.cpp +++ b/source/pipeline.cpp @@ -178,6 +178,7 @@ void Pipeline::render(Renderer &renderer, const Tag &tag) const Bind bind_depth_test(i->get_depth_test()); Bind bind_blend(i->get_blend()); renderer.set_lighting(i->get_lighting()); + renderer.set_clipping(i->get_clipping()); for(vector::const_iterator j=renderables.begin(); j!=renderables.end(); ++j) if(j->passes.empty() || j->passes.count(i->get_tag())) @@ -241,7 +242,8 @@ Pipeline::Pass::Pass(const Tag &t): tag(t), lighting(0), depth_test(0), - blend(0) + blend(0), + clipping(0) { } void Pipeline::Pass::set_lighting(const Lighting *l) @@ -259,6 +261,11 @@ void Pipeline::Pass::set_blend(const Blend *b) blend = b; } +void Pipeline::Pass::set_clipping(const Clipping *c) +{ + clipping =c; +} + Pipeline::Slot::Slot(const Renderable *r): renderable(r) diff --git a/source/pipeline.h b/source/pipeline.h index 515ca1a4..2c61a537 100644 --- a/source/pipeline.h +++ b/source/pipeline.h @@ -13,6 +13,7 @@ namespace GL { class Blend; class Camera; +class Clipping; class DepthTest; class Lighting; class PostProcessor; @@ -49,6 +50,7 @@ public: const Lighting *lighting; const DepthTest *depth_test; const Blend *blend; + const Clipping *clipping; public: Pass(const Tag &); @@ -58,9 +60,11 @@ public: void set_lighting(const Lighting *); void set_depth_test(const DepthTest *); void set_blend(const Blend *); + void set_clipping(const Clipping *); const Lighting *get_lighting() const { return lighting; } const DepthTest *get_depth_test() const { return depth_test; } const Blend *get_blend() const { return blend; } + const Clipping *get_clipping() const { return clipping; } }; private: diff --git a/source/renderer.cpp b/source/renderer.cpp index c558757f..6a4c488f 100644 --- a/source/renderer.cpp +++ b/source/renderer.cpp @@ -1,6 +1,7 @@ #include "batch.h" #include "buffer.h" #include "camera.h" +#include "clipping.h" #include "error.h" #include "lighting.h" #include "material.h" @@ -102,6 +103,15 @@ void Renderer::set_lighting(const Lighting *l) changed |= LIGHTING; } +void Renderer::set_clipping(const Clipping *c) +{ + state->clipping = c; + state->clipping_matrix = mtx_stack.top(); + if(c) + c->update_shader_data(standard_shdata, mtx_stack.top()); + changed |= CLIPPING; +} + void Renderer::set_shader_program(const Program *p, const ProgramData *d) { state->shprog = p; @@ -144,6 +154,7 @@ void Renderer::pop_state() throw stack_underflow("Renderer::pop_state"); const Lighting *old_lighting = state->lighting; + const Clipping *old_clipping = state->clipping; state_stack.pop_back(); state = &state_stack.back(); if(shdata_stack.size()>state->shdata_count) @@ -160,6 +171,12 @@ void Renderer::pop_state() state->lighting->update_shader_data(standard_shdata, state->lighting_matrix); changed |= LIGHTING; } + if(state->clipping!=old_clipping) + { + if(state->clipping) + state->clipping->update_shader_data(standard_shdata, state->clipping_matrix); + changed |= CLIPPING; + } } void Renderer::escape() @@ -256,6 +273,23 @@ void Renderer::apply_state() } } + if(changed&CLIPPING) + { + if(state->clipping) + { + if(legacy_bindings) + { + MatrixStack::modelview() = state->clipping_matrix; + state->clipping->bind(true); + changed = (changed&~CLIPPING)|LEGACY_MATRIX; + } + else + state->clipping->bind(false); + } + else + Clipping::unbind(); + } + if(state->shprog) { bool shprog_changed = (state->shprog!=Program::current()); @@ -357,6 +391,7 @@ Renderer::State::State(): lowest_effect_texunit(TexUnit::get_n_units()), material(0), lighting(0), + clipping(0), shprog(0), shdata_count(0), mesh(0), diff --git a/source/renderer.h b/source/renderer.h index 18d3a41a..3d561b4f 100644 --- a/source/renderer.h +++ b/source/renderer.h @@ -13,6 +13,7 @@ namespace GL { class Batch; class Buffer; class Camera; +class Clipping; class Material; class Mesh; class Lighting; @@ -69,6 +70,8 @@ private: const Material *material; const Lighting *lighting; Matrix lighting_matrix; + const Clipping *clipping; + Matrix clipping_matrix; const Program *shprog; unsigned shdata_count; const Mesh *mesh; @@ -95,7 +98,8 @@ private: MODERN_MATRIX = 2, MATRIX = LEGACY_MATRIX|MODERN_MATRIX, LIGHTING = 4, - SHADER_DATA = 8 + CLIPPING = 8, + SHADER_DATA = 16 }; MtxStack mtx_stack; @@ -138,6 +142,7 @@ public: void set_material(const Material *); void set_lighting(const Lighting *); + void set_clipping(const Clipping *); /** Sets the shader program to use. An initial set of data can be set as well, with the same semantics as add_shader_data. */ -- 2.45.2