]> git.tdb.fi Git - libs/gl.git/blobdiff - source/renderer.cpp
Add functions for setting arrays of 2x2 and 3x3 matrix uniforms
[libs/gl.git] / source / renderer.cpp
index 50acd8e728c60e9531fd7b23dccc8a28cfb4a7c1..cc00ac8e3affda145ec8e2f8afcc5c1d7bb4af40 100644 (file)
@@ -14,6 +14,7 @@
 #include "texturing.h"
 #include "texunit.h"
 #include "vertexarray.h"
+#include "vertexsetup.h"
 #include "windingtest.h"
 
 using namespace std;
@@ -22,6 +23,7 @@ namespace Msp {
 namespace GL {
 
 Renderer::Renderer(const Camera *c):
+       default_camera(c),
        changed(0),
        matrices_loaded(false),
        state_stack(1)
@@ -33,7 +35,10 @@ Renderer::Renderer(const Camera *c):
        if(c)
                set_camera(*c);
        else
+       {
                standard_shdata.uniform("projection_matrix", Matrix());
+               standard_shdata.uniform("eye_world_matrix", Matrix());
+       }
 }
 
 Renderer::~Renderer()
@@ -52,6 +57,7 @@ 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());
 }
@@ -141,6 +147,11 @@ 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;
@@ -172,9 +183,15 @@ void Renderer::pop_state()
        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
@@ -206,6 +223,10 @@ void Renderer::end()
                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();
 
@@ -216,6 +237,7 @@ void Renderer::end()
        Lighting::unbind();
        Clipping::unbind();
        Program::unbind();
+       VertexSetup::unbind();
        Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
        WindingTest::unbind();
 
@@ -247,18 +269,16 @@ 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::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
@@ -364,13 +384,24 @@ void Renderer::apply_state()
                {
                        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)
@@ -416,6 +447,7 @@ Renderer::State::State():
        shprog(0),
        shdata_count(0),
        mesh(0),
+       vertex_setup(0),
        winding_test(0),
        reverse_winding(false)
 { }