#include <msp/gl/extensions/arb_map_buffer_range.h>
#include "buffer.h"
#include "error.h"
-#include "mesh.h"
#include "misc.h"
+#include "vertexsetup.h"
using namespace std;
if(t!=type)
require_buffer_type(t);
if(t==ELEMENT_ARRAY_BUFFER)
- if(const Mesh *m = Mesh::current())
+ if(const VertexSetup *vs = VertexSetup::current())
{
- // Don't change the binding in a mesh's vertex array object
- if(this==m->get_index_buffer())
+ // Don't change the binding in a vertex array object
+ if(this==vs->get_index_buffer())
return;
throw invalid_operation("Buffer::bind_to(ELEMENT_ARRAY_BUFFER)");
}
const Buffer *Buffer::current(BufferType t)
{
if(t==ELEMENT_ARRAY_BUFFER)
- if(const Mesh *m = Mesh::current())
- return m->get_index_buffer();
+ if(const VertexSetup *vs = VertexSetup::current())
+ return vs->get_index_buffer();
return binding(t);
}
void Buffer::unbind_from(BufferType type)
{
- if(type==ELEMENT_ARRAY_BUFFER && Mesh::current())
+ if(type==ELEMENT_ARRAY_BUFFER && VertexSetup::current())
throw invalid_operation("Buffer::unbind_from(ELEMENT_ARRAY_BUFFER)");
if(set_current(type, 0))
glBindBuffer(type, 0);
#include "mesh.h"
#include "renderer.h"
#include "resourcemanager.h"
+#include "vertexsetup.h"
using namespace std;
{
vbuf = 0;
ibuf = 0;
- vao_id = 0;
+ vtx_setup = 0;
defer_buffers = ARB_vertex_buffer_object;
- dirty = true;
disallow_rendering = false;
winding = 0;
Mesh::~Mesh()
{
set_manager(0);
+ delete vtx_setup;
delete vbuf;
delete ibuf;
- if(vao_id)
- glDeleteVertexArrays(1, &vao_id);
}
void Mesh::clear()
if(!ibuf)
ibuf = new Buffer(ELEMENT_ARRAY_BUFFER);
- if(ARB_vertex_array_object && !vao_id)
- glGenVertexArrays(1, &vao_id);
-}
-
-void Mesh::setup_vao() const
-{
- Bind bind_vbuf(vbuf, ARRAY_BUFFER);
-
- const VertexFormat &fmt = vertices.get_format();
- unsigned stride = get_stride(fmt)*sizeof(float);
- float *ptr = 0;
- for(const unsigned char *c=fmt.begin(); c!=fmt.end(); ++c)
+ if(ARB_vertex_array_object && !vtx_setup)
{
- unsigned t = get_component_type(*c);
- if(t>=get_component_type(ATTRIB1))
- t -= get_component_type(ATTRIB1);
- unsigned sz = get_component_size(*c);
- if(*c==COLOR4_UBYTE)
- glVertexAttribPointer(t, 4, GL_UNSIGNED_BYTE, true, stride, ptr);
- else
- glVertexAttribPointer(t, sz, GL_FLOAT, false, stride, ptr);
- glEnableVertexAttribArray(t);
- ptr += sz;
+ vtx_setup = new VertexSetup;
+ vtx_setup->set_vertex_array(vertices);
+ vtx_setup->set_index_buffer(*ibuf);
}
- glBindBuffer(ELEMENT_ARRAY_BUFFER, ibuf->get_id());
-
- dirty = false;
}
unsigned Mesh::get_n_vertices() const
void Mesh::bind() const
{
- /* If VAOs are not supported, vao_id is zero and set_current won't get
+ /* If VAOs are not supported, vtx_setup is zero and set_current won't get
called. Thus unbind won't try to call a null function either. */
- if(!vao_id)
+ if(!vtx_setup)
{
unbind();
vertices.apply(false);
}
else if(set_current(this))
{
- glBindVertexArray(vao_id);
+ vtx_setup->bind();
vertices.refresh();
- if(dirty)
- setup_vao();
}
}
void Mesh::unbind()
{
if(set_current(0))
- glBindVertexArray(0);
+ VertexSetup::unbind();
}
Resource::AsyncLoader *Mesh::load(IO::Seekable &io, const Resources *)
for(vector<VertexComponent>::const_iterator i=c.begin(); i!=c.end(); ++i)
fmt = (fmt, *i);
obj.vertices.reset(fmt);
- obj.dirty = true;
+ if(obj.vtx_setup)
+ // Set it again to force the vertex setup to update
+ obj.vtx_setup->set_vertex_array(obj.vertices);
load_sub(obj.vertices);
}
class Buffer;
class Renderer;
+class VertexSetup;
/**
Raw mesh data, consisting of a VertexArray and one or more Batches. Though a
std::vector<Batch> batches;
Buffer *vbuf;
Buffer *ibuf;
- unsigned vao_id;
+ VertexSetup *vtx_setup;
bool defer_buffers;
mutable bool dirty;
bool disallow_rendering;
void use_buffers(bool);
private:
void create_buffers();
- void setup_vao() const;
public:
const VertexArray &get_vertices() const { return vertices; }
--- /dev/null
+#include <msp/gl/extensions/arb_vertex_array_object.h>
+#include <msp/gl/extensions/arb_vertex_buffer_object.h>
+#include <msp/gl/extensions/arb_vertex_shader.h>
+#include "buffer.h"
+#include "gl.h"
+#include "vertexarray.h"
+#include "vertexsetup.h"
+
+namespace Msp {
+namespace GL {
+
+VertexSetup::VertexSetup():
+ dirty(false),
+ array(0),
+ index_buffer(0)
+{
+ static Require req(ARB_vertex_array_object);
+ glGenVertexArrays(1, &id);
+}
+
+VertexSetup::~VertexSetup()
+{
+ if(current()==this)
+ unbind();
+ glDeleteVertexArrays(1, &id);
+}
+
+void VertexSetup::set_vertex_array(const VertexArray &a)
+{
+ array = &a;
+ update();
+}
+
+void VertexSetup::set_index_buffer(const Buffer &ibuf)
+{
+ index_buffer = &ibuf;
+ update();
+}
+
+void VertexSetup::update() const
+{
+ if(current()!=this)
+ {
+ dirty = true;
+ return;
+ }
+
+ Bind bind_vbuf(array->get_buffer(), ARRAY_BUFFER);
+
+ const VertexFormat &fmt = array->get_format();
+ unsigned stride = get_stride(fmt)*sizeof(float);
+ float *ptr = 0;
+ for(const unsigned char *c=fmt.begin(); c!=fmt.end(); ++c)
+ {
+ unsigned t = get_component_type(*c);
+ if(t>=get_component_type(ATTRIB1))
+ t -= get_component_type(ATTRIB1);
+ unsigned sz = get_component_size(*c);
+ if(*c==COLOR4_UBYTE)
+ glVertexAttribPointer(t, 4, GL_UNSIGNED_BYTE, true, stride, ptr);
+ else
+ glVertexAttribPointer(t, sz, GL_FLOAT, false, stride, ptr);
+ glEnableVertexAttribArray(t);
+ ptr += sz;
+ }
+
+ glBindBuffer(ELEMENT_ARRAY_BUFFER, index_buffer->get_id());
+}
+
+void VertexSetup::bind() const
+{
+ if(set_current(this))
+ {
+ glBindVertexArray(id);
+ if(dirty)
+ {
+ update();
+ dirty = false;
+ }
+ }
+}
+
+void VertexSetup::unbind()
+{
+ if(set_current(0))
+ glBindVertexArray(0);
+}
+
+} // namespace GL
+} // namespace Msp
--- /dev/null
+#ifndef MSP_GL_VERTEXSETUP_H_
+#define MSP_GL_VERTEXSETUP_H_
+
+#include "bindable.h"
+
+namespace Msp {
+namespace GL {
+
+class VertexArray;
+
+/**
+Combines a VertexArray with an index buffer. This wraps OpenGL's vertex array
+objects. Intended for internal use.
+*/
+class VertexSetup: public Bindable<VertexSetup>
+{
+private:
+ unsigned id;
+ mutable bool dirty;
+ const VertexArray *array;
+ const Buffer *index_buffer;
+
+public:
+ VertexSetup();
+ ~VertexSetup();
+
+ void set_vertex_array(const VertexArray &);
+ void set_index_buffer(const Buffer &);
+ const Buffer *get_index_buffer() const { return index_buffer; }
+
+private:
+ void update() const;
+
+public:
+ void bind() const;
+ static void unbind();
+};
+
+} // namespace GL
+} // namespace Msp
+
+#endif