+inline void InstanceArrayBase::destroy_all()
+{
+ for(unsigned i=0; i<slots.size(); ++i)
+ if(slots[i].used)
+ reinterpret_cast<T *>(get_address(i))->~T();
+}
+
+
+/**
+Stores and renders multiple instances of an Object in an efficient manner.
+
+The instance specific transform is passed to the shader in an attribute with
+the name instance_transform. The attribute should have the type vec4[3]. Each
+elements of the array corresponds to a row of the transform matrix.
+
+If the Mesh or Technique of the Object is changed during the lifetime of the
+InstanceArray, behaviour is undefined.
+
+The instance type must have a constructor accepting a const Object &. If it
+has a virtual function with the signature void set_matrix(const Matrix &), it
+will be used to update the instance matrix. The original function is also
+called.
+
+Instance created by the array have stable addresses. However after an instance
+is removed, its address may later be reused for another instance.
+*/
+template<typename T = ObjectInstance>
+class InstanceArray: public InstanceArrayBase
+{
+public:
+ class Loader: public DataFile::DerivedObjectLoader<InstanceArray, InstanceArrayBase::Loader>
+ {
+ public:
+ Loader(InstanceArray &a): DataFile::DerivedObjectLoader<InstanceArray, InstanceArrayBase::Loader>(a) { }
+
+ protected:
+ virtual void instance();
+ };
+
+private:
+ class Instance: public T
+ {
+ private:
+ InstanceArray &array;
+ unsigned index;
+
+ public:
+ Instance(const Object &o, InstanceArray &a, unsigned i): T(o), array(a), index(i) { }
+
+ virtual void set_matrix(const Matrix &);
+ };
+
+public:
+ InstanceArray(const Object &o): InstanceArrayBase(o, sizeof(Instance)) { }
+ ~InstanceArray() { destroy_all<T>(); }
+
+ T &append() { return *create<Instance>(*this); }
+ void remove(T &obj) { destroy(&obj); }
+};
+
+
+template<typename T>
+inline void InstanceArray<T>::Instance::set_matrix(const Matrix &m)