1 #include <msp/core/algorithm.h>
2 #include <msp/core/maputils.h>
3 #include <msp/gl/extensions/arb_draw_instanced.h>
4 #include <msp/gl/extensions/arb_instanced_arrays.h>
5 #include <msp/gl/extensions/arb_vertex_array_object.h>
6 #include <msp/gl/extensions/arb_vertex_shader.h>
9 #include "instancearray.h"
12 #include "objectinstance.h"
14 #include "technique.h"
15 #include "vertexsetup.h"
22 InstanceArray::InstanceArray(const Object &o):
30 const Technique *tech = object.get_technique();
31 const Technique::PassMap &passes = tech->get_passes();
32 for(Technique::PassMap::const_iterator i=passes.begin(); i!=passes.end(); ++i)
34 const Program *shprog = i->second.get_shader_program();
36 throw invalid_argument("InstanceArray::InstanceArray");
38 int loc = shprog->get_attribute_location("instance_transform");
40 matrix_location = loc;
41 else if(loc!=matrix_location)
42 throw invalid_argument("InstanceArray::InstanceArray");
45 if(ARB_vertex_array_object && ARB_instanced_arrays && ARB_draw_instanced)
47 instance_data = new VertexArray((RAW_ATTRIB4,matrix_location, RAW_ATTRIB4,matrix_location+1, RAW_ATTRIB4,matrix_location+2));
48 const VertexFormat &fmt = instance_data->get_format();
49 matrix_offset = fmt.offset(make_indexed_attribute(RAW_ATTRIB4, matrix_location));
51 instance_buffer = new Buffer;
52 instance_data->use_buffer(instance_buffer);
54 vtx_setup = new VertexSetup;
55 vtx_setup->set_format_instanced(object.get_mesh()->get_vertices().get_format(), fmt);
56 vtx_setup->set_vertex_array(object.get_mesh()->get_vertices());
57 vtx_setup->set_index_buffer(*object.get_mesh()->get_index_buffer());
58 vtx_setup->set_instance_array(*instance_data);
61 static Require req(ARB_vertex_shader);
64 InstanceArray::~InstanceArray()
66 for(vector<ObjectInstance *>::iterator i=instances.begin(); i!=instances.end(); ++i)
70 delete instance_buffer;
73 void InstanceArray::append(ObjectInstance *inst)
75 instances.push_back(inst);
78 if(instance_data->size()<instances.size())
80 instance_data->append();
81 unsigned req_size = instance_data->get_required_buffer_size();
82 if(instance_buffer->get_size()>0 && instance_buffer->get_size()<req_size)
84 delete instance_buffer;
85 instance_buffer = new Buffer;
86 instance_data->use_buffer(instance_buffer);
89 update_instance_matrix(instances.size()-1);
93 void InstanceArray::remove(ObjectInstance &inst)
95 vector<ObjectInstance *>::iterator i = find(instances, &inst);
96 if(i==instances.end())
97 throw key_error(&inst);
100 *i = instances.back();
101 instances.pop_back();
104 void InstanceArray::update_instance_matrix(unsigned index)
109 const Matrix &m = *instances[index]->get_matrix();
111 float *d = instance_data->modify(instances.size()-1);
112 for(unsigned i=0; i<12; ++i)
113 d[matrix_offset+i] = m(i/4, i%4);
116 void InstanceArray::render(Renderer &renderer, Tag tag) const
118 if(instances.empty())
123 const Technique *tech = object.get_technique();
125 throw logic_error("no technique");
126 const RenderPass *pass = tech->find_pass(tag);
130 const Mesh *mesh = object.get_mesh();
131 mesh->get_vertices().refresh();
132 if(instance_buffer->get_size()==0)
133 instance_buffer->storage(instance_data->get_required_buffer_size());
134 instance_data->refresh();
136 Renderer::Push push(renderer);
137 pass->apply(renderer);
138 mesh->draw_instanced(renderer, *vtx_setup, instances.size());
142 for(vector<ObjectInstance *>::const_iterator i=instances.begin(); i!=instances.end(); ++i)
144 const Matrix &m = *(*i)->get_matrix();
145 for(unsigned j=0; j<3; ++j)
146 glVertexAttrib4f(matrix_location+j, m(j, 0), m(j, 1), m(j, 2), m(j, 3));
147 (*i)->render(renderer, tag);