X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Finstancearray.cpp;fp=source%2Finstancearray.cpp;h=0000000000000000000000000000000000000000;hb=7aaec9a70b8d7733429bec043f8e33e02956f266;hp=abd9fcd0bd870d14def0192392490b09c702fa2d;hpb=bec07999d95b76f4b47cffcc564d0cd0afc0435e;p=libs%2Fgl.git diff --git a/source/instancearray.cpp b/source/instancearray.cpp deleted file mode 100644 index abd9fcd0..00000000 --- a/source/instancearray.cpp +++ /dev/null @@ -1,152 +0,0 @@ -#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 "renderer.h" -#include "technique.h" -#include "vertexsetup.h" - -using namespace std; - -namespace Msp { -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) - { - const Program *shprog = i->second.get_shader_program(); - if(!shprog) - throw invalid_argument("InstanceArray::InstanceArray"); - - int loc = shprog->get_attribute_location("instance_transform"); - if(matrix_location<0) - matrix_location = loc; - else if(loc!=matrix_location) - throw invalid_argument("InstanceArray::InstanceArray"); - } - - if(ARB_vertex_array_object && ARB_instanced_arrays && ARB_draw_instanced) - { - instance_data = new VertexArray((ATTRIB4,matrix_location, ATTRIB4,matrix_location+1, ATTRIB4,matrix_location+2)); - const VertexFormat &fmt = instance_data->get_format(); - matrix_offset = fmt.offset(make_indexed_component(ATTRIB4, matrix_location)); - - instance_buffer = new Buffer(ARRAY_BUFFER); - instance_data->use_buffer(instance_buffer); - - vtx_setup = new VertexSetup; - vtx_setup->set_vertex_array(object.get_mesh()->get_vertices()); - vtx_setup->set_index_buffer(*object.get_mesh()->get_index_buffer()); - vtx_setup->set_instance_array(instance_data); - } - else - static Require req(ARB_vertex_shader); -} - -InstanceArray::~InstanceArray() -{ - for(std::vector::iterator i=instances.begin(); i!=instances.end(); ++i) - delete *i; - delete vtx_setup; - delete instance_data; - delete instance_buffer; -} - -void InstanceArray::append(ObjectInstance *inst) -{ - instances.push_back(inst); - if(instance_data) - { - if(instance_data->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); - } - } - update_instance_matrix(instances.size()-1); - } -} - -void InstanceArray::remove(ObjectInstance &inst) -{ - vector::iterator i = find(instances, &inst); - if(i==instances.end()) - throw key_error(&inst); - - delete *i; - *i = instances.back(); - instances.pop_back(); -} - -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); - for(unsigned i=0; i<12; ++i) - d[matrix_offset+i] = m(i/4, i%4); -} - -void InstanceArray::render(Renderer &renderer, const 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); - } - } -} - -} // namespace GL -} // namespace Msp