X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Frender%2Finstancearray.cpp;h=f6a7bb634bd450c0f6f4d4011655331ca72e93ea;hp=427976129d65b1a4e5f0c82caf725822111a44b7;hb=38712d8ecc57d043a2419ffbaeeb57f7a6586f14;hpb=a04e4054b3001d24a540a0e7b088d270f3e07c07 diff --git a/source/render/instancearray.cpp b/source/render/instancearray.cpp index 42797612..f6a7bb63 100644 --- a/source/render/instancearray.cpp +++ b/source/render/instancearray.cpp @@ -1,18 +1,13 @@ #include #include -#include -#include -#include -#include #include "buffer.h" -#include "camera.h" #include "instancearray.h" #include "mesh.h" #include "object.h" #include "objectinstance.h" +#include "program.h" #include "renderer.h" #include "technique.h" -#include "vertexsetup.h" using namespace std; @@ -21,17 +16,14 @@ namespace GL { InstanceArray::InstanceArray(const Object &o): object(o), - instance_data(0), instance_buffer(0), - vtx_setup(0), matrix_location(-1), 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_methods()) { - const Program *shprog = i->second.get_shader_program(); + const Program *shprog = kvp.second.get_shader_program(); if(!shprog) throw invalid_argument("InstanceArray::InstanceArray"); @@ -42,59 +34,48 @@ InstanceArray::InstanceArray(const Object &o): throw invalid_argument("InstanceArray::InstanceArray"); } - if(ARB_vertex_array_object && ARB_instanced_arrays && ARB_draw_instanced) - { - 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((RAW_ATTRIB4,matrix_location)); + instance_data.set_format((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((RAW_ATTRIB4,matrix_location)); - instance_buffer = new Buffer; - instance_data->use_buffer(instance_buffer); + instance_buffer = new Buffer; + instance_data.use_buffer(instance_buffer); - const Mesh *mesh = object.get_mesh(); + const Mesh *mesh = object.get_mesh(); - vtx_setup = new VertexSetup; - 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 - static Require req(ARB_vertex_shader); + 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); } InstanceArray::~InstanceArray() { - for(vector::iterator i=instances.begin(); i!=instances.end(); ++i) - delete *i; - delete vtx_setup; - delete instance_data; + for(ObjectInstance *i: instances) + delete i; delete instance_buffer; } void InstanceArray::append(ObjectInstance *inst) { instances.push_back(inst); - if(instance_data) + if(instance_data.size()size()get_size()>0 && instance_buffer->get_size()append(); - unsigned req_size = instance_data->get_required_buffer_size(); - if(instance_buffer->get_size()>0 && instance_buffer->get_size()use_buffer(instance_buffer); - } + delete instance_buffer; + instance_buffer = new Buffer; + instance_data.use_buffer(instance_buffer); } - update_instance_matrix(instances.size()-1); } + update_instance_matrix(instances.size()-1); } void InstanceArray::remove(ObjectInstance &inst) { - vector::iterator i = find(instances, &inst); + auto i = find(instances, &inst); if(i==instances.end()) throw key_error(&inst); @@ -105,14 +86,11 @@ void InstanceArray::remove(ObjectInstance &inst) void InstanceArray::update_instance_matrix(unsigned index) { - if(!instance_data) - return; - const Matrix &m = *instances[index]->get_matrix(); - float *d = instance_data->modify(instances.size()-1); + float *d = reinterpret_cast(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 @@ -120,35 +98,22 @@ void InstanceArray::render(Renderer &renderer, Tag tag) const if(instances.empty()) return; - if(instance_data) - { - const Technique *tech = object.get_technique(); - if(!tech) - throw logic_error("no technique"); - const RenderPass *pass = tech->find_pass(tag); - if(!pass) - return; - - const Mesh *mesh = object.get_mesh(); - mesh->get_vertices().refresh(); - if(instance_buffer->get_size()==0) - instance_buffer->storage(instance_data->get_required_buffer_size()); - instance_data->refresh(); - - Renderer::Push push(renderer); - pass->apply(renderer); - mesh->draw_instanced(renderer, *vtx_setup, instances.size()); - } - else - { - for(vector::const_iterator i=instances.begin(); i!=instances.end(); ++i) - { - 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); - } - } + const Technique *tech = object.get_technique(); + if(!tech) + throw logic_error("no technique"); + const RenderMethod *method = tech->find_method(tag); + if(!method) + return; + + const Mesh *mesh = object.get_mesh(); + mesh->get_vertices().refresh(); + if(instance_buffer->get_size()==0) + instance_buffer->storage(instance_data.get_required_buffer_size()); + instance_data.refresh(); + + Renderer::Push push(renderer); + method->apply(renderer); + mesh->draw_instanced(renderer, vtx_setup, instances.size()); } } // namespace GL