X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Frenderer.cpp;h=bac7b6b854bcc2ea1c51d684b2ff700589e2d9a8;hb=f1d6343bc6e08878500672db952e9e7f9c7b8ada;hp=c751b0e3629d77b8bd93fee9aa4049f3a75cc57f;hpb=0c731643d6363eb4c492e836ffb919cb7c0a3035;p=libs%2Fgl.git diff --git a/source/renderer.cpp b/source/renderer.cpp index c751b0e3..bac7b6b8 100644 --- a/source/renderer.cpp +++ b/source/renderer.cpp @@ -2,12 +2,16 @@ #include "buffer.h" #include "camera.h" #include "error.h" +#include "lighting.h" #include "material.h" +#include "mesh.h" #include "program.h" #include "programdata.h" +#include "renderable.h" #include "renderer.h" #include "texture.h" #include "texturing.h" +#include "texunit.h" #include "vertexarray.h" #include "windingtest.h" @@ -18,37 +22,32 @@ namespace GL { Renderer::Renderer(const Camera *c): mtx_stack(*this), - mtx_changed(false), + mtx_changed(true), + matrices_loaded(false), camera(c), state_stack(1), - state(&state_stack.back()), - vertex_array(0), - vertex_array_changed(false), + lighting_changed(false), element_buffer(0) { - MatrixStack::modelview().push(); + state_stack.reserve(16); + shdata_stack.reserve(32); + state = &state_stack.back(); + if(camera) { - MatrixStack::projection().push(); - camera->apply(); mtx_stack.load(camera->get_matrix()); + standard_shdata.uniform("projection_matrix", camera->get_projection_matrix()); } else + { + standard_shdata.uniform("projection_matrix", MatrixStack::projection().top()); mtx_stack.load(MatrixStack::modelview().top()); + } } Renderer::~Renderer() { - if(camera) - MatrixStack::projection().pop(); - MatrixStack::modelview().pop(); - - Texturing::unbind(); - Texture::unbind_from(0); - Material::unbind(); - Program::unbind(); - Buffer::unbind_from(ELEMENT_ARRAY_BUFFER); - WindingTest::unbind(); + end(); } void Renderer::set_texture(const Texture *t) @@ -63,29 +62,46 @@ 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; } +void Renderer::set_lighting(const Lighting *l) +{ + state->lighting = l; + state->lighting_matrix = mtx_stack.top(); + if(l) + l->update_shader_data(standard_shdata, mtx_stack.top()); + lighting_changed = true; +} + void Renderer::set_shader_program(const Program *p, const ProgramData *d) { state->shprog = p; if(p && d) add_shader_data(*d); + + /* Even if we have no new shdata, the existing ones need to be re-applied + to the new program */ shdata_changed = true; } void Renderer::add_shader_data(const ProgramData &d) { - state->shdata.push_back(&d); + shdata_stack.push_back(&d); + state->shdata_count = shdata_stack.size(); shdata_changed = true; } -void Renderer::set_vertex_array(const VertexArray *a) +void Renderer::set_mesh(const Mesh *m) { - vertex_array_changed = (a!=vertex_array); - vertex_array = a; + state->mesh = m; } void Renderer::set_element_buffer(const Buffer *b) @@ -98,6 +114,11 @@ void Renderer::set_winding_test(const WindingTest *w) state->winding_test = w; } +void Renderer::set_reverse_winding(bool r) +{ + state->reverse_winding = r; +} + void Renderer::push_state() { state_stack.push_back(state_stack.back()); @@ -112,6 +133,8 @@ void Renderer::pop_state() state_stack.pop_back(); state = &state_stack.back(); + if(shdata_stack.size()>state->shdata_count) + shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end()); mtx_stack.pop(); mtx_changed = true; shdata_changed = true; @@ -121,33 +144,72 @@ void Renderer::escape() { apply_state(); Buffer::unbind_from(ELEMENT_ARRAY_BUFFER); + matrices_loaded = false; } -void Renderer::draw(const Batch &batch) +void Renderer::end() +{ + if(state_stack.size()>1) + throw invalid_operation("Renderer::end"); + + if(matrices_loaded) + { + if(camera) + MatrixStack::projection().pop(); + MatrixStack::modelview().pop(); + matrices_loaded = false; + } + + Mesh::unbind(); + Texturing::unbind(); + Texture::unbind_from(0); + Material::unbind(); + Lighting::unbind(); + Program::unbind(); + Buffer::unbind_from(ELEMENT_ARRAY_BUFFER); + WindingTest::unbind(); + + *state = State(); +} + +void Renderer::exclude(const Renderable &renderable) +{ + excluded.insert(&renderable); +} + +void Renderer::include(const Renderable &renderable) +{ + excluded.erase(&renderable); +} + +void Renderer::render(const Renderable &renderable, const Tag &tag) { - if(!vertex_array) - throw invalid_operation("Renderer::draw"); + if(!excluded.count(&renderable)) + renderable.render(*this, tag); +} +void Renderer::draw(const Batch &batch) +{ apply_state(); - // Until VertexArray acquires VAO support and becomes Bindable - if(vertex_array_changed) + bool legacy_bindings = (!state->shprog || state->shprog->uses_legacy_variables()); + if(legacy_bindings) { - vertex_array->apply(); - vertex_array_changed = false; + if(element_buffer) + element_buffer->bind_to(ELEMENT_ARRAY_BUFFER); + else + Buffer::unbind_from(ELEMENT_ARRAY_BUFFER); } - if(element_buffer) - element_buffer->bind_to(ELEMENT_ARRAY_BUFFER); - else - Buffer::unbind_from(ELEMENT_ARRAY_BUFFER); - batch.draw(); } void Renderer::apply_state() { - // We let the objects themselves figure out if the binding has changed + /* We (mostly) let the objects themselves figure out if the binding has + changed */ + + bool legacy_bindings = (!state->shprog || state->shprog->uses_legacy_variables()); if(state->texturing) state->texturing->bind(); @@ -160,17 +222,49 @@ void Renderer::apply_state() Texture::unbind_from(0); } - if(state->material) - state->material->bind(); - else - Material::unbind(); + if(legacy_bindings) + { + if(state->material) + state->material->bind(); + else + Material::unbind(); + + if(lighting_changed) + { + if(state->lighting) + { + MatrixStack::modelview() = state->lighting_matrix; + state->lighting->bind(); + mtx_changed = true; + lighting_changed = false; + } + else + Lighting::unbind(); + } + } if(state->shprog) { state->shprog->bind(); + + if(!legacy_bindings) + { + const Matrix &m = mtx_stack.top(); + standard_shdata.uniform("eye_obj_matrix", mtx_stack.top()); + LinAl::SquareMatrix nm; + for(unsigned i=0; i<3; ++i) + for(unsigned j=0; j<3; ++j) + nm(i, j) = m(i, j); + nm = transpose(invert(nm)); + standard_shdata.uniform_matrix3("eye_obj_normal_matrix", &nm(0, 0)); + if(state->material) + state->material->get_shader_data().apply(); + standard_shdata.apply(); + } + if(shdata_changed) { - for(vector::const_iterator i=state->shdata.begin(); i!=state->shdata.end(); ++i) + for(vector::const_iterator i=shdata_stack.begin(); i!=shdata_stack.end(); ++i) (*i)->apply(); shdata_changed = false; } @@ -178,15 +272,47 @@ void Renderer::apply_state() else Program::unbind(); + if(state->mesh) + { + if(legacy_bindings) + { + Mesh::unbind(); + state->mesh->get_vertices().apply(); + } + else + state->mesh->bind(); + } + else + Mesh::unbind(); + if(state->winding_test) - state->winding_test->bind(); + { + if(state->reverse_winding) + state->winding_test->get_reverse().bind(); + else + state->winding_test->bind(); + } else WindingTest::unbind(); - if(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; + } } } @@ -194,9 +320,14 @@ void Renderer::apply_state() Renderer::State::State(): texture(0), texturing(0), + lowest_effect_texunit(TexUnit::get_n_units()), material(0), + lighting(0), shprog(0), - winding_test(0) + shdata_count(0), + mesh(0), + winding_test(0), + reverse_winding(false) { }