From: Mikko Rasa Date: Fri, 21 Jun 2019 08:32:46 +0000 (+0300) Subject: Obtain the instance transform location from shader program X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=c041ea998569f24b2af9c5af47dffd337ff78146;p=libs%2Fgl.git Obtain the instance transform location from shader program --- 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); } } diff --git a/source/instancearray.h b/source/instancearray.h index ffba39e0..627051f7 100644 --- a/source/instancearray.h +++ b/source/instancearray.h @@ -42,6 +42,7 @@ private: VertexArray *instance_data; Buffer *instance_buffer; VertexSetup *vtx_setup; + int matrix_location; unsigned matrix_offset; public: