]> git.tdb.fi Git - libs/gl.git/blobdiff - source/renderer.cpp
Make the camera part of changeable Renderer state
[libs/gl.git] / source / renderer.cpp
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()),