]> git.tdb.fi Git - libs/gl.git/commitdiff
Add begin() method to make Renderer reusable
authorMikko Rasa <tdb@tdb.fi>
Sat, 1 Nov 2014 00:20:27 +0000 (02:20 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sat, 1 Nov 2014 00:20:27 +0000 (02:20 +0200)
source/renderer.cpp
source/renderer.h

index 2f253e2ab8c4f96471d3bf8e1491e450910f4bef..2d262e73bef495501afa333f6e5601a93468a9ac 100644 (file)
@@ -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),
index dd9464127d025ee0ae4ba9612084f91617f8ad67..1f198dae7007b00db30d4c699f0832ce06d0cca1 100644 (file)
@@ -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