]> git.tdb.fi Git - libs/gl.git/blobdiff - source/vertexarray.h
Unbind things if they are deleted while current
[libs/gl.git] / source / vertexarray.h
index f0baf2d4a84fa8896b21c351149d9cd28612ee04..9fa55b7117c32fdc2901c74215223b6616f9fa53 100644 (file)
@@ -1,30 +1,24 @@
-/* $Id$
-
-This file is part of libmspgl
-Copyright © 2007  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
 #ifndef MSP_GL_VERTEXARRAY_H_
 #define MSP_GL_VERTEXARRAY_H_
 
+#include <climits>
 #include <vector>
 #include <msp/core/refptr.h>
 #include <msp/datafile/loader.h>
+#include "bindable.h"
+#include "bufferable.h"
+#include "datatype.h"
 #include "primitivetype.h"
-#include "types.h"
 #include "vertexarraybuilder.h"
 #include "vertexformat.h"
 
 namespace Msp {
 namespace GL {
 
-class VertexBuffer;
+class Buffer;
 
-class VertexArray
+class VertexArray: public Bindable<VertexArray>, public Bufferable
 {
-       friend class VertexArrayBuilder;
-
 public:
        class Loader: public DataFile::Loader, public VertexArrayBuilder
        {
@@ -33,33 +27,53 @@ public:
        };
 
 private:
+       struct Array
+       {
+               unsigned char component;
+               unsigned char offset;
+
+               Array();
+       };
+
        VertexFormat format;
        std::vector<float> data;
-       uint         stride;
-       VertexBuffer *vbuf;
-       bool         own_vbuf;
+       unsigned stride;
+       std::vector<Array> arrays;
 
        VertexArray(const VertexArray &);
        VertexArray &operator=(const VertexArray &);
 public:
-       VertexArray(VertexFormat);
+       VertexArray(const VertexFormat &);
        ~VertexArray();
 
-       VertexFormat get_format() const { return format; }
-       const std::vector<float> &get_data() const { return data; }
-       void         use_vertex_buffer();
-       void         use_vertex_buffer(VertexBuffer *);
-       void         reserve(unsigned);
-       unsigned     size() const { return data.size()/stride; }
-       void         clear();
-       void         reset(VertexFormat);
-       RefPtr<VertexArrayBuilder> modify();
-       void         apply() const;
-       void         update_data();
+       void reset(const VertexFormat &);
+       const VertexFormat &get_format() const { return format; }
 private:
-       void set_array(unsigned, unsigned, unsigned) const;
+       static unsigned get_array_slot(unsigned char);
 
-       static unsigned enabled_arrays;
+public:
+       /// Deprecated alias for use_buffer
+       void use_vertex_buffer(Buffer *b) { use_buffer(b); }
+
+       void clear();
+       void reserve(unsigned);
+       float *append();
+       float *modify(unsigned);
+private:
+       virtual unsigned get_data_size() const;
+       virtual const void *get_data_pointer() const { return &data[0]; }
+
+public:
+       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; }
+
+       void bind() const;
+       void apply() const { bind(); }
+private:
+       static void apply_arrays(const std::vector<Array> *, const std::vector<Array> *, const float *, unsigned);
+public:
+       static void unbind();
 };
 
 } // namespace GL