]> git.tdb.fi Git - libs/gl.git/commitdiff
Make the camera part of changeable Renderer state
authorMikko Rasa <tdb@tdb.fi>
Sat, 26 Nov 2016 04:56:54 +0000 (06:56 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sat, 26 Nov 2016 04:56:54 +0000 (06:56 +0200)
I'd like to move to a single, persistent renderer at some point and this
is a necessary step towards that.  Some effects want to use their own
camera to render into an FBO.

source/renderer.cpp
source/renderer.h

index a0dc41516c0f90ba36ffa84070a8a7276809224a..45cf241e8ec3798ab0a0368c389b8794fa11f5d3 100644 (file)
@@ -25,7 +25,6 @@ Renderer::Renderer(const Camera *c):
        changed(0),
        matrices_loaded(false),
        shdata_applied(0),
-       camera(0),
        state_stack(1)
 {
        state_stack.reserve(16);
@@ -47,19 +46,17 @@ void Renderer::begin(const Camera *c)
 
        reset_state();
        excluded.clear();
-       camera = c;
 
-       if(camera)
-       {
-               state->modelview_matrix = camera->get_view_matrix();
-               standard_shdata.uniform("projection_matrix", camera->get_projection_matrix());
-       }
-       else
-       {
-               state->modelview_matrix = MatrixStack::modelview().top();
-               standard_shdata.uniform("projection_matrix", MatrixStack::projection().top());
-       }
-       changed |= MATRIX|STANDARD_SHDATA;
+       if(c)
+               set_camera(*c);
+}
+
+void Renderer::set_camera(const Camera &c)
+{
+       state->camera = &c;
+       standard_shdata.uniform("projection_matrix", state->camera->get_projection_matrix());
+       changed |= STANDARD_SHDATA|LEGACY_PROJECTION;
+       set_matrix(state->camera->get_view_matrix());
 }
 
 void Renderer::set_matrix(const Matrix &matrix)
@@ -161,6 +158,7 @@ void Renderer::pop_state()
        if(state_stack.size()==1)
                throw stack_underflow("Renderer::pop_state");
 
+       const Camera *old_camera = state->camera;
        const Lighting *old_lighting = state->lighting;
        const Clipping *old_clipping = state->clipping;
        state_stack.pop_back();
@@ -172,6 +170,11 @@ void Renderer::pop_state()
        }
        shdata_applied = min<unsigned>(shdata_applied, shdata_stack.size());
        changed |= MATRIX;
+       if(state->camera!=old_camera)
+       {
+               standard_shdata.uniform("projection_matrix", state->camera->get_projection_matrix());
+               changed |= STANDARD_SHDATA|LEGACY_PROJECTION;
+       }
        if(state->lighting!=old_lighting)
        {
                if(state->lighting)
@@ -375,14 +378,16 @@ void Renderer::apply_state()
                if(!matrices_loaded)
                {
                        MatrixStack::modelview().push();
-                       if(camera)
-                       {
-                               MatrixStack::projection().push();
-                               camera->apply();
-                       }
+                       MatrixStack::projection().push();
                        matrices_loaded = true;
                }
 
+               if(changed&LEGACY_PROJECTION)
+               {
+                       MatrixStack::projection() = state->camera->get_projection_matrix();
+                       changed &= ~LEGACY_PROJECTION;
+               }
+
                if(changed&LEGACY_MATRIX)
                {
                        MatrixStack::modelview() = state->modelview_matrix;
@@ -407,6 +412,7 @@ void Renderer::reset_state()
 
 
 Renderer::State::State():
+       camera(0),
        texture(0),
        texturing(0),
        lowest_effect_texunit(TexUnit::get_n_units()),
index 157282a5398f6a18a23f0f80684a9765a16e7a3e..1f4624ce889476370c25fef2e0077db723c8ded1 100644 (file)
@@ -64,6 +64,7 @@ public:
 private:
        struct State
        {
+               const Camera *camera;
                Matrix modelview_matrix;
                const Texture *texture;
                const Texturing *texturing;
@@ -91,13 +92,13 @@ private:
                LEGACY_CLIPPING = 8,
                SHADER_DATA = 16,
                MATERIAL_SHDATA = 32,
-               STANDARD_SHDATA = 64
+               STANDARD_SHDATA = 64,
+               LEGACY_PROJECTION = 128
        };
 
        unsigned char changed;
        bool matrices_loaded;
        unsigned shdata_applied;
-       const Camera *camera;
        std::vector<State> state_stack;
        State *state;
        ProgramData standard_shdata;
@@ -113,6 +114,12 @@ public:
        times without an intervening end(). */
        void begin(const Camera *);
 
+       /** Sets the camera to render from.  The modelview matrix is reset to the
+       camera's view matrix. */
+       void set_camera(const Camera &);
+
+       const Camera *get_camera() const { return state->camera; }
+
        /** Replaces the Renderer's modelview matrix. */
        void set_matrix(const Matrix &);
 
@@ -122,8 +129,6 @@ public:
        /** Returns the current modelview matrix. */
        const Matrix &get_matrix() const { return state->modelview_matrix; }
 
-       const Camera *get_camera() const { return camera; }
-
        void set_texture(const Texture *);
        void set_texturing(const Texturing *);
        unsigned allocate_effect_texunit();