X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Frenderer.cpp;h=0207f9378324aaed15a22725fbaa5897bf666bff;hb=f1034ec02beee2a497644fd91e680fd51ee1b5ac;hp=36685b4742ce699d8d7a4ab5d99254443464cc48;hpb=c0f0ea613b79a61bf83ef46a5fac6b22cbb242b7;p=libs%2Fgl.git diff --git a/source/renderer.cpp b/source/renderer.cpp index 36685b47..0207f937 100644 --- a/source/renderer.cpp +++ b/source/renderer.cpp @@ -4,12 +4,14 @@ #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" @@ -20,37 +22,43 @@ 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), - vertex_array(0), element_buffer(0) { state_stack.reserve(16); 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()); } 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(); + if(matrices_loaded) + { + if(camera) + MatrixStack::projection().pop(); + MatrixStack::modelview().pop(); + } + Mesh::unbind(); Texturing::unbind(); Texture::unbind_from(0); Material::unbind(); + Lighting::unbind(); Program::unbind(); Buffer::unbind_from(ELEMENT_ARRAY_BUFFER); WindingTest::unbind(); @@ -68,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; @@ -77,6 +90,8 @@ 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; } @@ -98,9 +113,9 @@ void Renderer::add_shader_data(const ProgramData &d) shdata_changed = true; } -void Renderer::set_vertex_array(const VertexArray *a) +void Renderer::set_mesh(const Mesh *m) { - vertex_array = a; + state->mesh = m; } void Renderer::set_element_buffer(const Buffer *b) @@ -143,6 +158,7 @@ void Renderer::escape() { apply_state(); Buffer::unbind_from(ELEMENT_ARRAY_BUFFER); + matrices_loaded = false; } void Renderer::exclude(const Renderable &renderable) @@ -163,17 +179,16 @@ void Renderer::render(const Renderable &renderable, const Tag &tag) void Renderer::draw(const Batch &batch) { - if(!vertex_array) - throw invalid_operation("Renderer::draw"); - apply_state(); - vertex_array->apply(); - - if(element_buffer) - element_buffer->bind_to(ELEMENT_ARRAY_BUFFER); - else - Buffer::unbind_from(ELEMENT_ARRAY_BUFFER); + bool legacy_bindings = (!state->shprog || state->shprog->uses_legacy_variables()); + if(legacy_bindings) + { + if(element_buffer) + element_buffer->bind_to(ELEMENT_ARRAY_BUFFER); + else + Buffer::unbind_from(ELEMENT_ARRAY_BUFFER); + } batch.draw(); } @@ -183,6 +198,8 @@ void Renderer::apply_state() /* 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(); else @@ -194,27 +211,46 @@ void Renderer::apply_state() Texture::unbind_from(0); } - if(state->material) - state->material->bind(); - else - Material::unbind(); - - if(lighting_changed) + if(legacy_bindings) { - if(state->lighting) + if(state->material) + state->material->bind(); + else + Material::unbind(); + + if(lighting_changed) { - MatrixStack::modelview() = state->lighting_matrix; - state->lighting->bind(); - mtx_changed = true; - lighting_changed = false; + if(state->lighting) + { + MatrixStack::modelview() = state->lighting_matrix; + state->lighting->bind(); + mtx_changed = true; + lighting_changed = false; + } + else + Lighting::unbind(); } - 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=shdata_stack.begin(); i!=shdata_stack.end(); ++i) @@ -225,6 +261,19 @@ 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) { if(state->reverse_winding) @@ -235,10 +284,24 @@ void Renderer::apply_state() 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; + } } } @@ -246,11 +309,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), shdata_count(0), - winding_test(0) + mesh(0), + winding_test(0), + reverse_winding(false) { }