]> git.tdb.fi Git - libs/gl.git/blobdiff - source/renderer.cpp
Make RenderPass easier to use
[libs/gl.git] / source / renderer.cpp
index 6734edc4bd20458be56563bdddfd57a2589ab203..3a44dcd3c0b21bd5afd1fac151405d6e0936f5d1 100644 (file)
@@ -2,6 +2,7 @@
 #include "buffer.h"
 #include "camera.h"
 #include "error.h"
+#include "lighting.h"
 #include "material.h"
 #include "program.h"
 #include "programdata.h"
@@ -23,7 +24,6 @@ Renderer::Renderer(const Camera *c):
        camera(c),
        state_stack(1),
        vertex_array(0),
-       vertex_array_changed(false),
        element_buffer(0)
 {
        state_stack.reserve(16);
@@ -72,6 +72,13 @@ 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();
+       lighting_changed = true;
+}
+
 void Renderer::set_shader_program(const Program *p, const ProgramData *d)
 {
        state->shprog = p;
@@ -92,7 +99,6 @@ void Renderer::add_shader_data(const ProgramData &d)
 
 void Renderer::set_vertex_array(const VertexArray *a)
 {
-       vertex_array_changed = (a!=vertex_array);
        vertex_array = a;
 }
 
@@ -156,12 +162,7 @@ void Renderer::draw(const Batch &batch)
 
        apply_state();
 
-       // Until VertexArray acquires VAO support and becomes Bindable
-       if(vertex_array_changed)
-       {
-               vertex_array->apply();
-               vertex_array_changed = false;
-       }
+       vertex_array->apply();
 
        if(element_buffer)
                element_buffer->bind_to(ELEMENT_ARRAY_BUFFER);
@@ -173,7 +174,8 @@ void Renderer::draw(const Batch &batch)
 
 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 */
 
        if(state->texturing)
                state->texturing->bind();
@@ -191,6 +193,19 @@ void Renderer::apply_state()
        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();
@@ -221,6 +236,7 @@ Renderer::State::State():
        texture(0),
        texturing(0),
        material(0),
+       lighting(0),
        shprog(0),
        shdata_count(0),
        winding_test(0)