X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Frenderer.cpp;h=0207f9378324aaed15a22725fbaa5897bf666bff;hb=922fac753e31d97fc88daa166e93e4c5572bd2ba;hp=b6ece6ded24efbf6f45c6d8dde8a27f0e8d51b09;hpb=dbf357bf41561932a8b983bd96ece01ff9fce9f0;p=libs%2Fgl.git diff --git a/source/renderer.cpp b/source/renderer.cpp index b6ece6de..0207f937 100644 --- a/source/renderer.cpp +++ b/source/renderer.cpp @@ -11,6 +11,7 @@ #include "renderer.h" #include "texture.h" #include "texturing.h" +#include "texunit.h" #include "vertexarray.h" #include "windingtest.h" @@ -21,7 +22,8 @@ namespace GL { Renderer::Renderer(const Camera *c): mtx_stack(*this), - mtx_changed(false), + mtx_changed(true), + matrices_loaded(false), camera(c), state_stack(1), lighting_changed(false), @@ -31,11 +33,8 @@ Renderer::Renderer(const Camera *c): shdata_stack.reserve(32); state = &state_stack.back(); - MatrixStack::modelview().push(); if(camera) { - MatrixStack::projection().push(); - camera->apply(); mtx_stack.load(camera->get_matrix()); standard_shdata.uniform("projection_matrix", camera->get_projection_matrix()); } @@ -48,9 +47,12 @@ Renderer::Renderer(const Camera *c): Renderer::~Renderer() { - if(camera) - MatrixStack::projection().pop(); - MatrixStack::modelview().pop(); + if(matrices_loaded) + { + if(camera) + MatrixStack::projection().pop(); + MatrixStack::modelview().pop(); + } Mesh::unbind(); Texturing::unbind(); @@ -74,6 +76,11 @@ void Renderer::set_texturing(const Texturing *t) state->texture = 0; } +unsigned Renderer::allocate_effect_texunit() +{ + return --state->lowest_effect_texunit; +} + void Renderer::set_material(const Material *m) { state->material = m; @@ -84,9 +91,7 @@ void Renderer::set_lighting(const Lighting *l) state->lighting = l; state->lighting_matrix = mtx_stack.top(); if(l) - /* XXX I'm not happy with this, but can't come up with anything better - right now. */ - const_cast(l)->update_shader_data(mtx_stack.top()); + l->update_shader_data(standard_shdata, mtx_stack.top()); lighting_changed = true; } @@ -153,6 +158,7 @@ void Renderer::escape() { apply_state(); Buffer::unbind_from(ELEMENT_ARRAY_BUFFER); + matrices_loaded = false; } void Renderer::exclude(const Renderable &renderable) @@ -240,8 +246,6 @@ void Renderer::apply_state() nm(i, j) = m(i, j); nm = transpose(invert(nm)); standard_shdata.uniform_matrix3("eye_obj_normal_matrix", &nm(0, 0)); - if(state->lighting) - state->lighting->get_shader_data().apply(); if(state->material) state->material->get_shader_data().apply(); standard_shdata.apply(); @@ -280,10 +284,24 @@ void Renderer::apply_state() else WindingTest::unbind(); - if(legacy_bindings && mtx_changed) + if(legacy_bindings) { - MatrixStack::modelview() = mtx_stack.top(); - mtx_changed = false; + if(!matrices_loaded) + { + MatrixStack::modelview().push(); + if(camera) + { + MatrixStack::projection().push(); + camera->apply(); + } + matrices_loaded = true; + } + + if(mtx_changed) + { + MatrixStack::modelview() = mtx_stack.top(); + mtx_changed = false; + } } } @@ -291,6 +309,7 @@ void Renderer::apply_state() Renderer::State::State(): texture(0), texturing(0), + lowest_effect_texunit(TexUnit::get_n_units()), material(0), lighting(0), shprog(0),