]> git.tdb.fi Git - libs/gl.git/commitdiff
Integrate modern shaders and VAOs with Renderer
authorMikko Rasa <tdb@tdb.fi>
Sun, 15 Dec 2013 23:26:55 +0000 (01:26 +0200)
committerMikko Rasa <tdb@tdb.fi>
Mon, 16 Dec 2013 18:28:48 +0000 (20:28 +0200)
source/mesh.cpp
source/renderer.cpp
source/renderer.h

index d474a27f116cc0c3e7175f7a64488120d4e83805..26d660446b6c8c264c4fba19781069343282bdb6 100644 (file)
@@ -169,7 +169,9 @@ void Mesh::draw() const
 
 void Mesh::draw(Renderer &renderer) const
 {
-       vertices.apply();
+       refresh();
+
+       renderer.set_mesh(this);
        renderer.set_element_buffer(ibuf);
        renderer.set_winding_test(winding);
 
index 8199d3a64dc4056c3e621a3cc46b909c5d1d0f66..5108ff4c8a48b26ba1283bb66027d4cc17899a56 100644 (file)
@@ -4,6 +4,7 @@
 #include "error.h"
 #include "lighting.h"
 #include "material.h"
+#include "mesh.h"
 #include "program.h"
 #include "programdata.h"
 #include "renderable.h"
@@ -36,9 +37,13 @@ Renderer::Renderer(const Camera *c):
                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()
@@ -47,6 +52,7 @@ Renderer::~Renderer()
                MatrixStack::projection().pop();
        MatrixStack::modelview().pop();
 
+       Mesh::unbind();
        Texturing::unbind();
        Texture::unbind_from(0);
        Material::unbind();
@@ -77,6 +83,10 @@ void Renderer::set_lighting(const Lighting *l)
 {
        state->lighting = l;
        state->lighting_matrix = mtx_stack.top();
+       if(l)
+               /* XXX I'm not happy with this, but can't come up with anything better
+               right now. */
+               const_cast<Lighting *>(l)->update_shader_data(mtx_stack.top());
        lighting_changed = true;
 }
 
@@ -98,6 +108,11 @@ void Renderer::add_shader_data(const ProgramData &d)
        shdata_changed = true;
 }
 
+void Renderer::set_mesh(const Mesh *m)
+{
+       state->mesh = m;
+}
+
 void Renderer::set_element_buffer(const Buffer *b)
 {
        element_buffer = b;
@@ -160,10 +175,14 @@ void Renderer::draw(const Batch &batch)
 {
        apply_state();
 
-       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();
 }
@@ -173,6 +192,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
@@ -184,27 +205,48 @@ 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<float, 3> 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->lighting)
+                               state->lighting->get_shader_data().apply();
+                       if(state->material)
+                               state->material->get_shader_data().apply();
+                       standard_shdata.apply();
+               }
+
                if(shdata_changed)
                {
                        for(vector<const ProgramData *>::const_iterator i=shdata_stack.begin(); i!=shdata_stack.end(); ++i)
@@ -215,6 +257,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)
@@ -225,7 +280,7 @@ void Renderer::apply_state()
        else
                WindingTest::unbind();
 
-       if(mtx_changed)
+       if(legacy_bindings && mtx_changed)
        {
                MatrixStack::modelview() = mtx_stack.top();
                mtx_changed = false;
@@ -240,6 +295,7 @@ Renderer::State::State():
        lighting(0),
        shprog(0),
        shdata_count(0),
+       mesh(0),
        winding_test(0)
 { }
 
index a45b2943004d0beb96c654ad0f6796de4b673e3a..f736447e12fce15c7bf9555cd9be7333cd01b2ae 100644 (file)
@@ -4,6 +4,7 @@
 #include <set>
 #include <vector>
 #include "matrix.h"
+#include "programdata.h"
 #include "tag.h"
 
 namespace Msp {
@@ -13,9 +14,9 @@ class Batch;
 class Buffer;
 class Camera;
 class Material;
+class Mesh;
 class Lighting;
 class Program;
-class ProgramData;
 class Renderable;
 class Texture;
 class Texturing;
@@ -69,6 +70,7 @@ private:
                Matrix lighting_matrix;
                const Program *shprog;
                unsigned shdata_count;
+               const Mesh *mesh;
                const WindingTest *winding_test;
                bool reverse_winding;
 
@@ -92,6 +94,7 @@ private:
        std::vector<State> state_stack;
        State *state;
        bool lighting_changed;
+       ProgramData standard_shdata;
        std::vector<const ProgramData *> shdata_stack;
        bool shdata_changed;
        const Buffer *element_buffer;
@@ -120,6 +123,7 @@ public:
        Renderer state is popped. */
        void add_shader_data(const ProgramData &data);
 
+       void set_mesh(const Mesh *);
        void set_element_buffer(const Buffer *);
        void set_winding_test(const WindingTest *);
        void set_reverse_winding(bool);