X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Finstancearray.cpp;h=29b59f09d9648e8f2474397852f6643910bef1c1;hp=4156c6f37101fec121244444370af8ff55202faf;hb=c041ea998569f24b2af9c5af47dffd337ff78146;hpb=5089933d82e69c4485843781aa4c762fc0c49be7 diff --git a/source/instancearray.cpp b/source/instancearray.cpp index 4156c6f3..29b59f09 100644 --- a/source/instancearray.cpp +++ b/source/instancearray.cpp @@ -24,13 +24,29 @@ InstanceArray::InstanceArray(const 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,12, ATTRIB4,13, ATTRIB4,14)); + 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, 12)); + matrix_offset = fmt.offset(make_indexed_component(ATTRIB4, matrix_location)); instance_buffer = new Buffer(ARRAY_BUFFER); instance_data->use_buffer(instance_buffer); @@ -112,9 +128,8 @@ void InstanceArray::render(Renderer &renderer, const Tag &tag) const for(vector::const_iterator i=instances.begin(); i!=instances.end(); ++i) { const Matrix &m = *(*i)->get_matrix(); - glVertexAttrib4f(12, m(0, 0), m(0, 1), m(0, 2), m(0, 3)); - glVertexAttrib4f(13, m(1, 0), m(1, 1), m(1, 2), m(1, 3)); - glVertexAttrib4f(14, m(2, 0), m(2, 1), m(2, 2), m(2, 3)); + 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); } }