X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Frenderer.cpp;h=c5fe3c64a8baca0d39b7966d43edc61c3efa9be4;hp=90ac455107ffc078a6db1dbe8e452ea06ed44f7a;hb=HEAD;hpb=2341b3575c874b1960814264fa759ad584bf3134 diff --git a/source/renderer.cpp b/source/renderer.cpp deleted file mode 100644 index 90ac4551..00000000 --- a/source/renderer.cpp +++ /dev/null @@ -1,462 +0,0 @@ -#include "batch.h" -#include "buffer.h" -#include "camera.h" -#include "clipping.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 "vertexsetup.h" -#include "windingtest.h" - -using namespace std; - -namespace Msp { -namespace GL { - -Renderer::Renderer(const Camera *c): - default_camera(c), - changed(0), - matrices_loaded(false), - state_stack(1) -{ - state_stack.reserve(16); - shdata_stack.reserve(32); - state = &state_stack.back(); - - if(c) - set_camera(*c); - else - { - standard_shdata.uniform("projection_matrix", Matrix()); - standard_shdata.uniform("eye_world_matrix", Matrix()); - } -} - -Renderer::~Renderer() -{ - end(); -} - -void Renderer::begin(const Camera *c) -{ - end(); - 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()); - standard_shdata.uniform("eye_world_matrix", state->camera->get_view_matrix()); - changed |= STANDARD_SHDATA|LEGACY_PROJECTION; - set_matrix(state->camera->get_view_matrix()); -} - -void Renderer::set_matrix(const Matrix &matrix) -{ - state->modelview_matrix = matrix; - changed |= MATRIX; -} - -void Renderer::transform(const Matrix &matrix) -{ - state->modelview_matrix *= matrix; - changed |= MATRIX; -} - -void Renderer::set_texture(const Texture *t) -{ - state->texture = t; - state->texturing = 0; -} - -void Renderer::set_texturing(const Texturing *t) -{ - state->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; - changed |= MATERIAL_SHDATA; -} - -void Renderer::set_lighting(const Lighting *l) -{ - state->lighting = l; - state->lighting_matrix = state->modelview_matrix; - if(l) - { - l->update_shader_data(standard_shdata, state->lighting_matrix); - changed |= STANDARD_SHDATA; - } - changed |= LEGACY_LIGHTING; -} - -void Renderer::set_clipping(const Clipping *c) -{ - state->clipping = c; - state->clipping_matrix = state->modelview_matrix; - if(c) - { - c->update_shader_data(standard_shdata, state->clipping_matrix); - changed |= STANDARD_SHDATA; - } - changed |= LEGACY_CLIPPING; -} - -void Renderer::set_shader_program(const Program *p, const ProgramData *d) -{ - state->shprog = p; - if(p && d) - add_shader_data(*d); -} - -void Renderer::add_shader_data(const ProgramData &d) -{ - if(state->shdata_countshdata_count]==&d) - ++state->shdata_count; - else - { - if(shdata_stack.size()>state->shdata_count) - shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end()); - shdata_stack.push_back(&d); - state->shdata_count = shdata_stack.size(); - changed |= SHADER_DATA; - } -} - -void Renderer::set_mesh(const Mesh *m) -{ - state->mesh = m; -} - -void Renderer::set_vertex_setup(const VertexSetup *vs) -{ - state->vertex_setup = vs; -} - -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::set_object_lod_bias(unsigned b) -{ - state->object_lod_bias = b; -} - -void Renderer::push_state() -{ - state_stack.push_back(state_stack.back()); - state = &state_stack.back(); -} - -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(); - state = &state_stack.back(); - changed |= MATRIX; - bool camera_changed = (state->camera!=old_camera); - if(camera_changed) - { - if(state->camera) - { - standard_shdata.uniform("projection_matrix", state->camera->get_projection_matrix()); - standard_shdata.uniform("eye_world_matrix", state->camera->get_view_matrix()); - } - else - { - standard_shdata.uniform("projection_matrix", Matrix()); - standard_shdata.uniform("eye_world_matrix", Matrix()); - } - changed |= STANDARD_SHDATA|LEGACY_PROJECTION; - } - /* This actually should compare the relevant matrices rather than check for - camera, but in practice lighting and clipping is set right after the camera - and a boolean check is much faster than matrix comparison. */ - if(state->lighting!=old_lighting || camera_changed) - { - if(state->lighting) - { - state->lighting->update_shader_data(standard_shdata, state->lighting_matrix); - changed |= STANDARD_SHDATA; - } - changed |= LEGACY_LIGHTING; - } - if(state->clipping!=old_clipping || camera_changed) - { - if(state->clipping) - { - state->clipping->update_shader_data(standard_shdata, state->clipping_matrix); - changed |= STANDARD_SHDATA; - } - changed |= LEGACY_CLIPPING; - } -} - -void Renderer::end() -{ - if(state_stack.size()>1) - throw invalid_operation("Renderer::end"); - - *state = State(); - if(default_camera) - set_camera(*default_camera); - else - standard_shdata.uniform("projection_matrix", Matrix()); - shdata_stack.clear(); - excluded.clear(); - - Mesh::unbind(); - Texturing::unbind(); - Texture::unbind_from(0); - Material::unbind(); - Lighting::unbind(); - Clipping::unbind(); - Program::unbind(); - VertexSetup::unbind(); - Buffer::unbind_from(ELEMENT_ARRAY_BUFFER); - WindingTest::unbind(); - - if(matrices_loaded) - { - MatrixStack::projection().pop(); - MatrixStack::modelview().pop(); - matrices_loaded = false; - } -} - -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(!excluded.count(&renderable)) - renderable.render(*this, tag); -} - -void Renderer::draw(const Batch &batch) -{ - apply_state(); - - batch.draw(); -} - -void Renderer::draw_instanced(const Batch &batch, unsigned count) -{ - apply_state(); - - batch.draw_instanced(count); -} - -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()); - bool legacy_textures = !state->shprog; - - if(state->texturing) - state->texturing->bind(legacy_textures); - else - { - Texturing::unbind(); - if(state->texture) - state->texture->bind_to(0, legacy_textures); - else - Texture::unbind_from(0); - } - - if(legacy_bindings) - { - if(state->material) - state->material->bind(); - else - Material::unbind(); - - if(changed&LEGACY_LIGHTING) - { - if(state->lighting) - { - MatrixStack::modelview() = state->lighting_matrix; - state->lighting->bind(); - changed = (changed&~LEGACY_LIGHTING)|LEGACY_MATRIX; - } - else - Lighting::unbind(); - } - } - - if(state->clipping) - { - if(legacy_bindings) - { - if(changed&LEGACY_CLIPPING) - { - MatrixStack::modelview() = state->clipping_matrix; - state->clipping->bind(true); - changed = (changed&~LEGACY_CLIPPING)|LEGACY_MATRIX; - } - } - else - state->clipping->bind(false); - } - else - Clipping::unbind(); - - if(state->shprog) - { - bool shprog_changed = (state->shprog!=Program::current()); - state->shprog->bind(); - - if(!legacy_bindings) - { - if(changed&MODERN_MATRIX) - { - standard_shdata.uniform("eye_obj_matrix", state->modelview_matrix); - LinAl::SquareMatrix nm = state->modelview_matrix.block<3, 3>(0, 0); - nm = transpose(invert(nm)); - standard_shdata.uniform_matrix3("eye_obj_normal_matrix", &nm(0, 0)); - changed = (changed&~MODERN_MATRIX)|STANDARD_SHDATA; - } - - if(state->material && ((changed&MATERIAL_SHDATA) || shprog_changed)) - { - state->material->get_shader_data().apply(); - changed &= ~MATERIAL_SHDATA; - } - - if((changed&STANDARD_SHDATA) || shprog_changed) - { - standard_shdata.apply(); - changed &= ~STANDARD_SHDATA; - } - } - - bool extra_shdata = (shdata_stack.size()>state->shdata_count); - - if((changed&SHADER_DATA) || shprog_changed || extra_shdata) - { - if(extra_shdata) - shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end()); - for(vector::const_iterator i=shdata_stack.begin(); i!=shdata_stack.end(); ++i) - (*i)->apply(); - changed &= ~SHADER_DATA; - } - } - else - Program::unbind(); - - if(state->mesh) - { - if(legacy_bindings) - { - Mesh::unbind(); - state->mesh->get_vertices().apply(); - if(const Buffer *ibuf = state->mesh->get_index_buffer()) - ibuf->bind_to(ELEMENT_ARRAY_BUFFER); - else - Buffer::unbind_from(ELEMENT_ARRAY_BUFFER); - } - else - state->mesh->bind(); - } - else - { - Mesh::unbind(); - - if(state->vertex_setup) - state->vertex_setup->bind(); - else - VertexSetup::unbind(); - } - - if(state->winding_test) - { - if(state->reverse_winding) - state->winding_test->get_reverse().bind(); - else - state->winding_test->bind(); - } - else - WindingTest::unbind(); - - if(legacy_bindings) - { - if(!matrices_loaded) - { - MatrixStack::modelview().push(); - 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; - changed &= ~LEGACY_MATRIX; - } - } -} - - -Renderer::State::State(): - camera(0), - texture(0), - texturing(0), - lowest_effect_texunit(TexUnit::get_n_units()), - material(0), - lighting(0), - clipping(0), - shprog(0), - shdata_count(0), - mesh(0), - vertex_setup(0), - winding_test(0), - reverse_winding(false), - object_lod_bias(0) -{ } - -} // namespace GL -} // namespace Msp