Mesh can draw itself, it's usually used as part of Renderables rather than on
its own.
*/
-class Mesh: public Bindable<Mesh>, public Resource
+class Mesh: public Resource
{
friend class MeshBuilder;
void draw(Renderer &) const;
void draw_instanced(Renderer &, const VertexSetup &, unsigned) const;
- /** Binds the mesh for rendering. The vertex array is applied using generic
- attributes only. Uses vertex array object if possible. */
- void bind() const;
-
- static void unbind();
-
virtual int get_load_priority() const { return 1; }
virtual Resource::AsyncLoader *load(IO::Seekable &, const Resources * = 0);
virtual UInt64 get_data_size() const;
reset(f);
}
-VertexArray::~VertexArray()
-{
- /* Unbind accesses the current VertexArray, so a call from ~Bindable would
- try to access destroyed data. */
- if(current()==this)
- unbind();
-}
-
void VertexArray::reset(const VertexFormat &f)
{
clear();
format = f;
stride = get_stride(format);
-
- arrays.clear();
-
- unsigned offs = 0;
- for(const unsigned char *c=format.begin(); c!=format.end(); ++c)
- {
- unsigned slot = get_array_slot(*c);
- if(slot>=arrays.size())
- arrays.resize(slot+1);
-
- Array &arr = arrays[slot];
- arr.component = *c;
- arr.offset = offs;
-
- offs += get_component_size(*c);
- }
-}
-
-unsigned VertexArray::get_array_slot(unsigned char comp)
-{
- unsigned t = get_component_type(comp);
- if(t==get_component_type(VERTEX3))
- return 0;
- else if(t==get_component_type(NORMAL3))
- return 1;
- else if(t==get_component_type(COLOR4_FLOAT))
- return 2;
- else if(comp>=TEXCOORD1 && comp<=TEXCOORD4+12)
- {
- t -= get_component_type(TEXCOORD1);
- if(t>0)
- static Require _req(ARB_multitexture);
- return 3+t;
- }
- else
- {
- static Require _req(ARB_vertex_shader);
- if(comp>=ATTRIB1)
- t -= get_component_type(ATTRIB1);
- return 7+t;
- }
}
void VertexArray::clear()
return data.size()*sizeof(float);
}
-void VertexArray::apply() const
-{
- if(format.empty())
- throw invalid_operation("VertexArray::apply");
- // Don't mess up the vertex array object of a mesh
- if(Mesh::current())
- throw invalid_operation("VertexArray::apply");
-
- static Require _req(ARB_vertex_shader);
-
- const VertexArray *old = current();
- /* If the array has been modified, apply it even if it was the last one to
- be applied. This is necessary to get the data updated to vertex buffer, and
- to resync things after a format change. Radeon drivers also have some
- problems with modifying vertex arrays without re-setting the pointers. */
- if(!set_current(this) && !dirty)
- return;
-
- const Buffer *vbuf = get_buffer();
- Bind _bind_vbuf(vbuf, ARRAY_BUFFER);
- if(vbuf && dirty)
- update_buffer();
-
- const float *base = (vbuf ? reinterpret_cast<float *>(get_offset()) : &data[0]);
- unsigned stride_bytes = stride*sizeof(float);
- apply_arrays(&arrays, (old ? &old->arrays : 0), base, stride_bytes);
-}
-
-void VertexArray::apply_arrays(const vector<Array> *arrays, const vector<Array> *old_arrays, const float *base, unsigned stride_bytes)
-{
- unsigned n_arrays = arrays ? arrays->size() : 0;
- if(old_arrays)
- n_arrays = max<unsigned>(n_arrays, old_arrays->size());
- for(unsigned i=0; i<n_arrays; ++i)
- {
- const Array *arr = ((arrays && i<arrays->size() && (*arrays)[i].component) ? &(*arrays)[i] : 0);
- const Array *old_arr = ((old_arrays && i<old_arrays->size() && (*old_arrays)[i].component) ? &(*old_arrays)[i] : 0);
- if(!arr && !old_arr)
- continue;
-
- unsigned char comp = (arr ? arr->component : old_arr->component);
- unsigned sz = get_component_size(comp);
- unsigned t = get_component_type(comp);
-
- if(t>=get_component_type(ATTRIB1))
- t -= get_component_type(ATTRIB1);
- if(arr)
- {
- if(arr->component==COLOR4_UBYTE)
- glVertexAttribPointer(t, 4, GL_UNSIGNED_BYTE, true, stride_bytes, base+arr->offset);
- else
- glVertexAttribPointer(t, sz, GL_FLOAT, false, stride_bytes, base+arr->offset);
- }
-
- // Only change enable state if needed
- if(arr && !old_arr)
- glEnableVertexAttribArray(t);
- else if(old_arr && !arr)
- glDisableVertexAttribArray(t);
- }
-}
-
-void VertexArray::unbind()
-{
- const VertexArray *old = current();
- if(set_current(0))
- apply_arrays(0, &old->arrays, 0, 0);
-}
-
-
-VertexArray::Array::Array():
- component(0),
- offset(0)
-{ }
-
VertexArray::Loader::Loader(VertexArray &a):
VertexArrayBuilder(a)
#include <vector>
#include <msp/core/refptr.h>
#include <msp/datafile/loader.h>
-#include "bindable.h"
#include "bufferable.h"
#include "datatype.h"
#include "primitivetype.h"
A higher-level interface for filling in vertex data is available in the
VertexArrayBuilder class.
*/
-class VertexArray: public Bindable<VertexArray>, public Bufferable
+class VertexArray: public Bufferable
{
public:
class Loader: public DataFile::Loader, public VertexArrayBuilder
};
private:
- struct Array
- {
- unsigned char component;
- unsigned char offset;
-
- Array();
- };
-
VertexFormat format;
std::vector<float> data;
unsigned stride;
- std::vector<Array> arrays;
VertexArray(const VertexArray &);
VertexArray &operator=(const VertexArray &);
public:
VertexArray(const VertexFormat &);
- ~VertexArray();
/// Resets the VertexArray to a different format. All data is cleared.
void reset(const VertexFormat &);
const VertexFormat &get_format() const { return format; }
-private:
- static unsigned get_array_slot(unsigned char);
-public:
/// Clears all vertices from the array.
void clear();
unsigned size() const { return data.size()/stride; }
const std::vector<float> &get_data() const { return data; }
const float *operator[](unsigned i) const { return &data[0]+i*stride; }
-
- /// Equivalent to apply(). For compatibility with the Bindable interface.
- void bind() const { apply(); }
-
- /// Applies component arrays to the GL.
- void apply() const;
-
-private:
- static void apply_arrays(const std::vector<Array> *, const std::vector<Array> *, const float *, unsigned);
-public:
- static void unbind();
};
} // namespace GL