]> git.tdb.fi Git - libs/gl.git/blobdiff - source/render/instancearray.cpp
Split reflection data from Program to a separate struct
[libs/gl.git] / source / render / instancearray.cpp
index 5854906fe9229de3a59e46f13f27e8f3521c2d20..be3f17d4bc24cd6e2cd3d612321448a7302caf02 100644 (file)
@@ -10,6 +10,7 @@
 #include "mesh.h"
 #include "object.h"
 #include "objectinstance.h"
+#include "program.h"
 #include "renderer.h"
 #include "technique.h"
 #include "vertexsetup.h"
@@ -28,10 +29,9 @@ InstanceArray::InstanceArray(const Object &o):
        matrix_offset(0)
 {
        const Technique *tech = object.get_technique();
-       const Technique::PassMap &passes = tech->get_passes();
-       for(Technique::PassMap::const_iterator i=passes.begin(); i!=passes.end(); ++i)
+       for(const auto &kvp: tech->get_passes())
        {
-               const Program *shprog = i->second.get_shader_program();
+               const Program *shprog = kvp.second.get_shader_program();
                if(!shprog)
                        throw invalid_argument("InstanceArray::InstanceArray");
 
@@ -46,15 +46,17 @@ InstanceArray::InstanceArray(const Object &o):
        {
                instance_data = new VertexArray((RAW_ATTRIB4,matrix_location, RAW_ATTRIB4,matrix_location+1, RAW_ATTRIB4,matrix_location+2));
                const VertexFormat &fmt = instance_data->get_format();
-               matrix_offset = fmt.offset(make_indexed_attribute(RAW_ATTRIB4, matrix_location));
+               matrix_offset = fmt.offset((RAW_ATTRIB4,matrix_location));
 
-               instance_buffer = new Buffer(ARRAY_BUFFER);
+               instance_buffer = new Buffer;
                instance_data->use_buffer(instance_buffer);
 
+               const Mesh *mesh = object.get_mesh();
+
                vtx_setup = new VertexSetup;
-               vtx_setup->set_format_instanced(object.get_mesh()->get_vertices().get_format(), fmt);
-               vtx_setup->set_vertex_array(object.get_mesh()->get_vertices());
-               vtx_setup->set_index_buffer(*object.get_mesh()->get_index_buffer());
+               vtx_setup->set_format_instanced(mesh->get_vertices().get_format(), fmt);
+               vtx_setup->set_vertex_array(mesh->get_vertices());
+               vtx_setup->set_index_buffer(*mesh->get_index_buffer(), mesh->get_batches().front().get_index_type());
                vtx_setup->set_instance_array(*instance_data);
        }
        else
@@ -63,8 +65,8 @@ InstanceArray::InstanceArray(const Object &o):
 
 InstanceArray::~InstanceArray()
 {
-       for(vector<ObjectInstance *>::iterator i=instances.begin(); i!=instances.end(); ++i)
-               delete *i;
+       for(ObjectInstance *i: instances)
+               delete i;
        delete vtx_setup;
        delete instance_data;
        delete instance_buffer;
@@ -82,7 +84,7 @@ void InstanceArray::append(ObjectInstance *inst)
                        if(instance_buffer->get_size()>0 && instance_buffer->get_size()<req_size)
                        {
                                delete instance_buffer;
-                               instance_buffer = new Buffer(ARRAY_BUFFER);
+                               instance_buffer = new Buffer;
                                instance_data->use_buffer(instance_buffer);
                        }
                }
@@ -92,7 +94,7 @@ void InstanceArray::append(ObjectInstance *inst)
 
 void InstanceArray::remove(ObjectInstance &inst)
 {
-       vector<ObjectInstance *>::iterator i = find(instances, &inst);
+       auto i = find(instances, &inst);
        if(i==instances.end())
                throw key_error(&inst);
 
@@ -108,9 +110,9 @@ void InstanceArray::update_instance_matrix(unsigned index)
 
        const Matrix &m = *instances[index]->get_matrix();
 
-       float *d = instance_data->modify(instances.size()-1);
+       float *d = reinterpret_cast<float *>(instance_data->modify(instances.size()-1)+matrix_offset);
        for(unsigned i=0; i<12; ++i)
-               d[matrix_offset+i] = m(i/4, i%4);
+               d[i] = m(i/4, i%4);
 }
 
 void InstanceArray::render(Renderer &renderer, Tag tag) const
@@ -139,12 +141,12 @@ void InstanceArray::render(Renderer &renderer, Tag tag) const
        }
        else
        {
-               for(vector<ObjectInstance *>::const_iterator i=instances.begin(); i!=instances.end(); ++i)
+               for(ObjectInstance *i: instances)
                {
-                       const Matrix &m = *(*i)->get_matrix();
+                       const Matrix &m = *i->get_matrix();
                        for(unsigned j=0; j<3; ++j)
                                glVertexAttrib4f(matrix_location+j, m(j, 0), m(j, 1), m(j, 2), m(j, 3));
-                       (*i)->render(renderer, tag);
+                       i->render(renderer, tag);
                }
        }
 }