]> git.tdb.fi Git - libs/gl.git/commitdiff
Obtain the instance transform location from shader program
authorMikko Rasa <tdb@tdb.fi>
Fri, 21 Jun 2019 08:32:46 +0000 (11:32 +0300)
committerMikko Rasa <tdb@tdb.fi>
Fri, 21 Jun 2019 11:54:28 +0000 (14:54 +0300)
source/instancearray.cpp
source/instancearray.h

index 4156c6f37101fec121244444370af8ff55202faf..29b59f09d9648e8f2474397852f6643910bef1c1 100644 (file)
@@ -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<ObjectInstance *>::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);
                }
        }
index ffba39e072c7f35c3ed5d2c3c89cc7ed41b22dcb..627051f75b123a973ef878546fddd762708afe59 100644 (file)
@@ -42,6 +42,7 @@ private:
        VertexArray *instance_data;
        Buffer *instance_buffer;
        VertexSetup *vtx_setup;
+       int matrix_location;
        unsigned matrix_offset;
 
 public: