1 #ifndef MSP_GL_INSTANCEARRAY_H_
2 #define MSP_GL_INSTANCEARRAY_H_
5 #include "programdata.h"
6 #include "renderable.h"
7 #include "vertexarray.h"
8 #include "vertexsetup.h"
18 Renders multiple instances of an Object in an efficient manner.
20 The instance specific transform is passed to the shader in an attribute with
21 the name instance_transform. The attribute should have the type vec4[3]. Each
22 elements of the array corresponds to a row of the transform matrix.
24 If the Mesh or Technique of the Object is changed during the lifetime of the
25 InstanceArray, behaviour is undefined.
27 class InstanceArray: public Renderable
31 class Instance: public T
38 Instance(const Object &o, InstanceArray &a, unsigned i): T(o), array(a), index(i) { }
40 virtual void set_matrix(const Matrix &);
45 std::vector<ObjectInstance *> instances;
46 VertexArray instance_data;
47 Buffer *instance_buffer = 0;
48 VertexSetup vtx_setup;
49 int matrix_location = -1;
50 unsigned matrix_offset = 0;
53 InstanceArray(const Object &);
56 void set_matrix_attribute(const std::string &);
58 /** Adds a new instance to the array. The instance class must have a
59 constructor taking a const reference to Object as its sole parameter. */
60 template<typename T = ObjectInstance>
63 void append(ObjectInstance *);
64 void update_instance_matrix(unsigned);
66 void remove(ObjectInstance &);
68 virtual void render(Renderer &, Tag) const;
72 T &InstanceArray::append()
74 Instance<T> *inst = new Instance<T>(object, *this, instances.size());
80 void InstanceArray::Instance<T>::set_matrix(const Matrix &m)
83 array.update_instance_matrix(index);