]> git.tdb.fi Git - libs/gl.git/blobdiff - source/renderer.cpp
Check the flat qualifier from the correct member
[libs/gl.git] / source / renderer.cpp
diff --git a/source/renderer.cpp b/source/renderer.cpp
deleted file mode 100644 (file)
index cbec16e..0000000
+++ /dev/null
@@ -1,413 +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 "windingtest.h"
-
-using namespace std;
-
-namespace Msp {
-namespace GL {
-
-Renderer::Renderer(const Camera *c):
-       mtx_stack(*this),
-       changed(0),
-       matrices_loaded(false),
-       shdata_applied(0),
-       camera(0),
-       state_stack(1)
-{
-       state_stack.reserve(16);
-       shdata_stack.reserve(32);
-       state = &state_stack.back();
-
-       begin(c);
-}
-
-Renderer::~Renderer()
-{
-       end();
-}
-
-void Renderer::begin(const Camera *c)
-{
-       if(state_stack.size()>1)
-               throw invalid_operation("Renderer::begin");
-
-       reset_state();
-       excluded.clear();
-       camera = c;
-
-       if(camera)
-       {
-               mtx_stack.load(camera->get_matrix());
-               standard_shdata.uniform("projection_matrix", camera->get_projection_matrix());
-       }
-       else
-       {
-               mtx_stack.load(MatrixStack::modelview().top());
-               standard_shdata.uniform("projection_matrix", MatrixStack::projection().top());
-       }
-}
-
-void Renderer::set_matrix(const Matrix &matrix)
-{
-       mtx_stack.load(matrix);
-}
-
-void Renderer::transform(const Matrix &matrix)
-{
-       mtx_stack *= 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;
-}
-
-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());
-       changed |= LIGHTING;
-}
-
-void Renderer::set_clipping(const Clipping *c)
-{
-       state->clipping = c;
-       state->clipping_matrix = mtx_stack.top();
-       if(c)
-               c->update_shader_data(standard_shdata, mtx_stack.top());
-       changed |= 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)
-{
-       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_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());
-       state = &state_stack.back();
-       mtx_stack.push();
-}
-
-void Renderer::pop_state()
-{
-       if(state_stack.size()==1)
-               throw stack_underflow("Renderer::pop_state");
-
-       const Lighting *old_lighting = state->lighting;
-       const Clipping *old_clipping = state->clipping;
-       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());
-               changed |= SHADER_DATA;
-       }
-       shdata_applied = min<unsigned>(shdata_applied, shdata_stack.size());
-       mtx_stack.pop();
-       changed |= MATRIX;
-       if(state->lighting!=old_lighting)
-       {
-               if(state->lighting)
-                       state->lighting->update_shader_data(standard_shdata, state->lighting_matrix);
-               changed |= LIGHTING;
-       }
-       if(state->clipping!=old_clipping)
-       {
-               if(state->clipping)
-                       state->clipping->update_shader_data(standard_shdata, state->clipping_matrix);
-               changed |= CLIPPING;
-       }
-}
-
-void Renderer::escape()
-{
-       apply_state();
-       Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
-       matrices_loaded = false;
-}
-
-void Renderer::end()
-{
-       if(state_stack.size()>1)
-               throw invalid_operation("Renderer::end");
-
-       reset_state();
-
-       Mesh::unbind();
-       Texturing::unbind();
-       Texture::unbind_from(0);
-       Material::unbind();
-       Lighting::unbind();
-       Program::unbind();
-       Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
-       WindingTest::unbind();
-}
-
-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();
-
-       bool legacy_bindings = (!state->shprog || state->shprog->uses_legacy_variables());
-       if(state->mesh && legacy_bindings)
-       {
-               if(const Buffer *ibuf = state->mesh->get_index_buffer())
-                       ibuf->bind_to(ELEMENT_ARRAY_BUFFER);
-               else
-                       Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
-       }
-
-       batch.draw();
-}
-
-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
-       {
-               Texturing::unbind();
-               if(state->texture)
-                       state->texture->bind_to(0);
-               else
-                       Texture::unbind_from(0);
-       }
-
-       if(legacy_bindings)
-       {
-               if(state->material)
-                       state->material->bind();
-               else
-                       Material::unbind();
-
-               if(changed&LIGHTING)
-               {
-                       if(state->lighting)
-                       {
-                               MatrixStack::modelview() = state->lighting_matrix;
-                               state->lighting->bind();
-                               changed = (changed&~LIGHTING)|LEGACY_MATRIX;
-                       }
-                       else
-                               Lighting::unbind();
-               }
-       }
-
-       if(state->clipping)
-       {
-               if(legacy_bindings)
-               {
-                       if(changed&CLIPPING)
-                       {
-                               MatrixStack::modelview() = state->clipping_matrix;
-                               state->clipping->bind(true);
-                               changed = (changed&~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)
-                       {
-                               const Matrix &m = mtx_stack.top();
-                               standard_shdata.uniform("eye_obj_matrix", mtx_stack.top());
-                               LinAl::SquareMatrix<float, 3> nm = m.block<3, 3>(0, 0);
-                               nm = transpose(invert(nm));
-                               standard_shdata.uniform_matrix3("eye_obj_normal_matrix", &nm(0, 0));
-                               changed &= ~MODERN_MATRIX;
-                       }
-
-                       if(state->material)
-                               state->material->get_shader_data().apply();
-                       standard_shdata.apply();
-               }
-
-               if((changed&SHADER_DATA) || shprog_changed)
-               {
-                       vector<const ProgramData *>::const_iterator i = shdata_stack.begin();
-                       if(!shprog_changed)
-                               i += shdata_applied;
-                       for(; i!=shdata_stack.end(); ++i)
-                               (*i)->apply();
-                       changed &= ~SHADER_DATA;
-                       shdata_applied = shdata_stack.size();
-               }
-       }
-       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)
-                       state->winding_test->get_reverse().bind();
-               else
-                       state->winding_test->bind();
-       }
-       else
-               WindingTest::unbind();
-
-       if(legacy_bindings)
-       {
-               if(!matrices_loaded)
-               {
-                       MatrixStack::modelview().push();
-                       if(camera)
-                       {
-                               MatrixStack::projection().push();
-                               camera->apply();
-                       }
-                       matrices_loaded = true;
-               }
-
-               if(changed&LEGACY_MATRIX)
-               {
-                       MatrixStack::modelview() = mtx_stack.top();
-                       changed &= ~LEGACY_MATRIX;
-               }
-       }
-}
-
-void Renderer::reset_state()
-{
-       if(!matrices_loaded)
-               return;
-
-       if(camera)
-               MatrixStack::projection().pop();
-       MatrixStack::modelview().pop();
-       matrices_loaded = false;
-       changed |= MATRIX;
-
-       *state = State();
-}
-
-
-Renderer::State::State():
-       texture(0),
-       texturing(0),
-       lowest_effect_texunit(TexUnit::get_n_units()),
-       material(0),
-       lighting(0),
-       clipping(0),
-       shprog(0),
-       shdata_count(0),
-       mesh(0),
-       winding_test(0),
-       reverse_winding(false)
-{ }
-
-
-Renderer::MtxStack::MtxStack(Renderer &r):
-       renderer(r)
-{ }
-
-void Renderer::MtxStack::update()
-{
-       renderer.changed |= MATRIX;
-}
-
-} // namespace GL
-} // namespace Msp