X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Frender%2Finstancearray.h;fp=source%2Frender%2Finstancearray.h;h=627051f75b123a973ef878546fddd762708afe59;hb=7aaec9a70b8d7733429bec043f8e33e02956f266;hp=0000000000000000000000000000000000000000;hpb=bec07999d95b76f4b47cffcc564d0cd0afc0435e;p=libs%2Fgl.git diff --git a/source/render/instancearray.h b/source/render/instancearray.h new file mode 100644 index 00000000..627051f7 --- /dev/null +++ b/source/render/instancearray.h @@ -0,0 +1,83 @@ +#ifndef MSP_GL_INSTANCEARRAY_H_ +#define MSP_GL_INSTANCEARRAY_H_ + +#include +#include "programdata.h" +#include "renderable.h" + +namespace Msp { +namespace GL { + +class Buffer; +class Object; +class ObjectInstance; +class VertexArray; +class VertexSetup; + +/** +Renders multiple instances of an Object in an efficient manner. If instanced +rendering is supported, only one draw call per Batch needs to be issued. + +Changing the Mesh of the Object while an InstanceArray exists is not supported. +*/ +class InstanceArray: public Renderable +{ +public: + template + 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 &); + }; + +private: + const Object &object; + std::vector instances; + VertexArray *instance_data; + Buffer *instance_buffer; + VertexSetup *vtx_setup; + int matrix_location; + unsigned matrix_offset; + +public: + InstanceArray(const Object &); + ~InstanceArray(); + + void set_matrix_attribute(const std::string &); + + template + T &append(); +private: + void append(ObjectInstance *); + void update_instance_matrix(unsigned); +public: + void remove(ObjectInstance &); + + virtual void render(Renderer &, const Tag &) const; +}; + +template +T &InstanceArray::append() +{ + Instance *inst = new Instance(object, *this, instances.size()); + append(inst); + return *inst; +} + +template +void InstanceArray::Instance::set_matrix(const Matrix &m) +{ + T::set_matrix(m); + array.update_instance_matrix(index); +} + +} // namespace GL +} // namespace Msp + +#endif