From bfc7f104c642590fd85577a6545c6bc6259ccaa4 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 26 Nov 2016 06:56:54 +0200 Subject: [PATCH] Make the camera part of changeable Renderer state 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 | 42 ++++++++++++++++++++++++------------------ source/renderer.h | 13 +++++++++---- 2 files changed, 33 insertions(+), 22 deletions(-) diff --git a/source/renderer.cpp b/source/renderer.cpp index a0dc4151..45cf241e 100644 --- a/source/renderer.cpp +++ b/source/renderer.cpp @@ -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(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()), diff --git a/source/renderer.h b/source/renderer.h index 157282a5..1f4624ce 100644 --- a/source/renderer.h +++ b/source/renderer.h @@ -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_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(); -- 2.45.2