]> git.tdb.fi Git - libs/gl.git/blobdiff - source/vertexarray.cpp
Support specifying elements in PrimitiveBuilder
[libs/gl.git] / source / vertexarray.cpp
index e7d7386509d6208b2b5130be5bb780f3bc757521..c0ca8dca80f22b96f1efdfe8f9de6ccd9dc04ffd 100644 (file)
@@ -5,7 +5,7 @@ Copyright © 2007  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 
-#include <GL/gl.h>
+#include "gl.h"
 #include "vertexarray.h"
 #include "vertexbuffer.h"
 
@@ -48,6 +48,11 @@ void VertexArray::use_vertex_buffer(VertexBuffer *b)
        update_data();
 }
 
+void VertexArray::reserve(unsigned n)
+{
+       data.reserve(n*stride);
+}
+
 void VertexArray::clear()
 {
        data.clear();
@@ -64,39 +69,44 @@ void VertexArray::reset(VertexFormat f)
 
 RefPtr<VertexArrayBuilder> VertexArray::modify()
 {
-       return new VertexArrayBuilder(*this, data);
+       return new VertexArrayBuilder(*this);
 }
 
 void VertexArray::apply() const
 {
-       if(vbuf) vbuf->bind();
+       if(format==NODATA)
+               throw InvalidState("Trying to apply a vertex apply of format NODATA");
+
+       if(vbuf)
+               vbuf->bind();
 
        const float *base=vbuf?0:&data[0];
        uint offset=0;
        uint found=0;
+       uint bpv=stride*sizeof(float);
        for(uint fmt=format; fmt; fmt>>=4)
        {
-               uint size=(fmt&3)+1;
+               uint sz=(fmt&3)+1;
                switch(fmt&12)
                {
                case 0:
-                       glVertexPointer(size, GL_FLOAT, stride, base+offset);
+                       glVertexPointer(sz, GL_FLOAT, bpv, base+offset);
                        break;
                case 4:
-                       glNormalPointer(GL_FLOAT, stride, base+offset);
+                       glNormalPointer(GL_FLOAT, bpv, base+offset);
                        break;
                case 8:
-                       glTexCoordPointer(size, GL_FLOAT, stride, base+offset);
+                       glTexCoordPointer(sz, GL_FLOAT, bpv, base+offset);
                        break;
                case 12:
-                       if(size==1)
-                               glColorPointer(4, GL_UNSIGNED_BYTE, stride, base+offset);
+                       if(sz==1)
+                               glColorPointer(4, GL_UNSIGNED_BYTE, bpv, base+offset);
                        else
-                               glColorPointer(size, GL_FLOAT, stride, base+offset);
+                               glColorPointer(sz, GL_FLOAT, bpv, base+offset);
                        break;
                }
                found|=1<<((fmt&12)>>2);
-               offset+=size;
+               offset+=sz;
        }
 
        set_array(GL_VERTEX_ARRAY, found&1, 1);
@@ -134,7 +144,7 @@ unsigned VertexArray::enabled_arrays=0;
 
 
 VertexArray::Loader::Loader(VertexArray &a):
-       VertexArrayBuilder(a, a.data)
+       VertexArrayBuilder(a)
 {
        add("vertex2",   static_cast<void (Loader::*)(float, float)>(&Loader::vertex));
        add("vertex3",   static_cast<void (Loader::*)(float, float, float)>(&Loader::vertex));