From 5df9e64424dd157da1410b4498f53a4ac21e6aee Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 1 Nov 2014 02:20:27 +0200 Subject: [PATCH] Add begin() method to make Renderer reusable --- source/renderer.cpp | 52 ++++++++++++++++++++++++++++++--------------- source/renderer.h | 11 ++++++++-- 2 files changed, 44 insertions(+), 19 deletions(-) diff --git a/source/renderer.cpp b/source/renderer.cpp index 2f253e2a..2d262e73 100644 --- a/source/renderer.cpp +++ b/source/renderer.cpp @@ -22,10 +22,10 @@ namespace GL { Renderer::Renderer(const Camera *c): mtx_stack(*this), - changed(MATRIX), + changed(0), matrices_loaded(false), shdata_applied(0), - camera(c), + camera(0), state_stack(1), element_buffer(0) { @@ -33,6 +33,23 @@ Renderer::Renderer(const Camera *c): shdata_stack.reserve(32); state = &state_stack.back(); + begin(c); +} + +Renderer::~Renderer() +{ + end(); +} + +void Renderer::begin(const Camera *c) +{ + if(state_stack.size()>1) + throw invalid_operation("Renderer::begin"); + + reset_state(); + excluded.clear(); + camera = c; + if(camera) { mtx_stack.load(camera->get_matrix()); @@ -40,16 +57,11 @@ Renderer::Renderer(const Camera *c): } else { - standard_shdata.uniform("projection_matrix", MatrixStack::projection().top()); mtx_stack.load(MatrixStack::modelview().top()); + standard_shdata.uniform("projection_matrix", MatrixStack::projection().top()); } } -Renderer::~Renderer() -{ - end(); -} - void Renderer::set_texture(const Texture *t) { state->texture = t; @@ -158,13 +170,7 @@ void Renderer::end() if(state_stack.size()>1) throw invalid_operation("Renderer::end"); - if(matrices_loaded) - { - if(camera) - MatrixStack::projection().pop(); - MatrixStack::modelview().pop(); - matrices_loaded = false; - } + reset_state(); Mesh::unbind(); Texturing::unbind(); @@ -174,8 +180,6 @@ void Renderer::end() Program::unbind(); Buffer::unbind_from(ELEMENT_ARRAY_BUFFER); WindingTest::unbind(); - - *state = State(); } void Renderer::exclude(const Renderable &renderable) @@ -331,6 +335,20 @@ void Renderer::apply_state() } } +void Renderer::reset_state() +{ + if(!matrices_loaded) + return; + + if(camera) + MatrixStack::projection().pop(); + MatrixStack::modelview().pop(); + matrices_loaded = false; + changed |= MATRIX; + + *state = State(); +} + Renderer::State::State(): texture(0), diff --git a/source/renderer.h b/source/renderer.h index dd946412..1f198dae 100644 --- a/source/renderer.h +++ b/source/renderer.h @@ -114,6 +114,11 @@ public: Renderer(const Camera *); ~Renderer(); + /** Resets all internal state and restarts rendering. There must be no + unpopped state in the stack. It is permissible to call begin() multiple + times without an intervening end(). */ + void begin(const Camera *); + MatrixStack &matrix_stack() { return mtx_stack; } const Camera *get_camera() const { return camera; } @@ -151,8 +156,9 @@ public: Renderer. DEPRECATED. */ void escape(); - /** Ends rendering, unbinding all objects and resetting state. There must - be no unpopped state in the stack. */ + /** Unbinds all objects and resets related state. There must be no unpopped + state in the stack. Rendering with the same camera can be restarted without + an explicit begin() call. */ void end(); void exclude(const Renderable &); @@ -163,6 +169,7 @@ public: private: void apply_state(); + void reset_state(); }; } // namespace GL -- 2.45.2