]> git.tdb.fi Git - libs/gl.git/commitdiff
Eliminate the separate matrix stack in Renderer
authorMikko Rasa <tdb@tdb.fi>
Sat, 19 Nov 2016 14:20:06 +0000 (16:20 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sat, 19 Nov 2016 14:20:06 +0000 (16:20 +0200)
Instead incorporate the modelview matrix into the main render state.

source/renderer.cpp
source/renderer.h

index 01c17a26ef8cc3ce5019bea833a3224e7c54e19c..3bef74b34039aa1db1db907eb648eff17ae69771 100644 (file)
@@ -22,7 +22,6 @@ namespace Msp {
 namespace GL {
 
 Renderer::Renderer(const Camera *c):
-       mtx_stack(*this),
        changed(0),
        matrices_loaded(false),
        shdata_applied(0),
@@ -52,24 +51,26 @@ void Renderer::begin(const Camera *c)
 
        if(camera)
        {
-               mtx_stack.load(camera->get_matrix());
+               state->modelview_matrix = camera->get_view_matrix();
                standard_shdata.uniform("projection_matrix", camera->get_projection_matrix());
        }
        else
        {
-               mtx_stack.load(MatrixStack::modelview().top());
+               state->modelview_matrix = MatrixStack::modelview().top();
                standard_shdata.uniform("projection_matrix", MatrixStack::projection().top());
        }
 }
 
 void Renderer::set_matrix(const Matrix &matrix)
 {
-       mtx_stack.load(matrix);
+       state->modelview_matrix = matrix;
+       changed |= MATRIX;
 }
 
 void Renderer::transform(const Matrix &matrix)
 {
-       mtx_stack *= matrix;
+       state->modelview_matrix *= matrix;
+       changed |= MATRIX;
 }
 
 void Renderer::set_texture(const Texture *t)
@@ -98,10 +99,10 @@ void Renderer::set_material(const Material *m)
 void Renderer::set_lighting(const Lighting *l)
 {
        state->lighting = l;
-       state->lighting_matrix = mtx_stack.top();
+       state->lighting_matrix = state->modelview_matrix;
        if(l)
        {
-               l->update_shader_data(standard_shdata, mtx_stack.top());
+               l->update_shader_data(standard_shdata, state->lighting_matrix);
                changed |= STANDARD_SHDATA;
        }
        changed |= LEGACY_LIGHTING;
@@ -110,9 +111,9 @@ void Renderer::set_lighting(const Lighting *l)
 void Renderer::set_clipping(const Clipping *c)
 {
        state->clipping = c;
-       state->clipping_matrix = mtx_stack.top();
+       state->clipping_matrix = state->modelview_matrix;
        if(c)
-               c->update_shader_data(standard_shdata, mtx_stack.top());
+               c->update_shader_data(standard_shdata, state->clipping_matrix);
        changed |= LEGACY_CLIPPING;
 }
 
@@ -149,7 +150,6 @@ void Renderer::push_state()
 {
        state_stack.push_back(state_stack.back());
        state = &state_stack.back();
-       mtx_stack.push();
 }
 
 void Renderer::pop_state()
@@ -167,7 +167,6 @@ void Renderer::pop_state()
                changed |= SHADER_DATA;
        }
        shdata_applied = min<unsigned>(shdata_applied, shdata_stack.size());
-       mtx_stack.pop();
        changed |= MATRIX;
        if(state->lighting!=old_lighting)
        {
@@ -304,9 +303,8 @@ void Renderer::apply_state()
                {
                        if(changed&MODERN_MATRIX)
                        {
-                               const Matrix &m = mtx_stack.top();
-                               standard_shdata.uniform("eye_obj_matrix", mtx_stack.top());
-                               LinAl::SquareMatrix<float, 3> nm = m.block<3, 3>(0, 0);
+                               standard_shdata.uniform("eye_obj_matrix", state->modelview_matrix);
+                               LinAl::SquareMatrix<float, 3> nm = state->modelview_matrix.block<3, 3>(0, 0);
                                nm = transpose(invert(nm));
                                standard_shdata.uniform_matrix3("eye_obj_normal_matrix", &nm(0, 0));
                                changed = (changed&~MODERN_MATRIX)|STANDARD_SHDATA;
@@ -377,7 +375,7 @@ void Renderer::apply_state()
 
                if(changed&LEGACY_MATRIX)
                {
-                       MatrixStack::modelview() = mtx_stack.top();
+                       MatrixStack::modelview() = state->modelview_matrix;
                        changed &= ~LEGACY_MATRIX;
                }
        }
@@ -412,15 +410,5 @@ Renderer::State::State():
        reverse_winding(false)
 { }
 
-
-Renderer::MtxStack::MtxStack(Renderer &r):
-       renderer(r)
-{ }
-
-void Renderer::MtxStack::update()
-{
-       renderer.changed |= MATRIX;
-}
-
 } // namespace GL
 } // namespace Msp
index b114f6873648574032e8566bc3897d9e581fa348..157282a5398f6a18a23f0f80684a9765a16e7a3e 100644 (file)
@@ -64,6 +64,7 @@ public:
 private:
        struct State
        {
+               Matrix modelview_matrix;
                const Texture *texture;
                const Texturing *texturing;
                unsigned lowest_effect_texunit;
@@ -81,17 +82,6 @@ private:
                State();
        };
 
-       class MtxStack: public MatrixStack
-       {
-       private:
-               Renderer &renderer;
-
-       public:
-               MtxStack(Renderer &);
-       private:
-               virtual void update();
-       };
-
        enum ChangeMask
        {
                LEGACY_MATRIX = 1,
@@ -104,7 +94,6 @@ private:
                STANDARD_SHDATA = 64
        };
 
-       MtxStack mtx_stack;
        unsigned char changed;
        bool matrices_loaded;
        unsigned shdata_applied;
@@ -124,9 +113,6 @@ public:
        times without an intervening end(). */
        void begin(const Camera *);
 
-       /** Deprecated as unsafe.  Use set_matrix() or transform() instead. */
-       MatrixStack &matrix_stack() { return mtx_stack; }
-
        /** Replaces the Renderer's modelview matrix. */
        void set_matrix(const Matrix &);
 
@@ -134,7 +120,7 @@ public:
        void transform(const Matrix &);
 
        /** Returns the current modelview matrix. */
-       const Matrix &get_matrix() const { return mtx_stack.top(); }
+       const Matrix &get_matrix() const { return state->modelview_matrix; }
 
        const Camera *get_camera() const { return camera; }