-/* $Id$
-
-This file is part of libmspgl
-Copyright © 2007 Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
+#include <msp/gl/extensions/arb_multitexture.h>
+#include <msp/gl/extensions/arb_vertex_shader.h>
+#include "buffer.h"
+#include "error.h"
#include "gl.h"
+#include "mesh.h"
#include "vertexarray.h"
-#include "vertexbuffer.h"
using namespace std;
namespace Msp {
namespace GL {
-VertexArray::VertexArray(VertexFormat f):
- format(NODATA),
- stride(get_stride(f)),
- vbuf(0),
- own_vbuf(false)
-{
- // Reverse the format so the first item is in lowest bits. This makes handling in bind() easier.
- for(uint fmt=f; fmt; fmt>>=4)
- format=(format, static_cast<VertexFormat>(fmt&15));
-}
-
-VertexArray::~VertexArray()
+VertexArray::VertexArray(const VertexFormat &f)
{
- if(own_vbuf)
- delete vbuf;
+ reset(f);
}
-void VertexArray::use_vertex_buffer()
-{
- if(vbuf && own_vbuf)
- return;
-
- vbuf=new VertexBuffer();
- own_vbuf=true;
-
- update_data();
-}
-
-void VertexArray::use_vertex_buffer(VertexBuffer *b)
-{
- if(own_vbuf)
- delete vbuf;
- vbuf=b;
- own_vbuf=false;
-
- update_data();
-}
-
-void VertexArray::reserve(unsigned n)
+void VertexArray::reset(const VertexFormat &f)
{
- data.reserve(n*stride);
+ clear();
+ format = f;
+ stride = get_stride(format);
}
void VertexArray::clear()
data.clear();
}
-void VertexArray::reset(VertexFormat f)
-{
- clear();
- format=NODATA;
- for(uint fmt=f; fmt; fmt>>=4)
- format=(format, static_cast<VertexFormat>(fmt&15));
- stride=get_stride(format);
-}
-
-RefPtr<VertexArrayBuilder> VertexArray::modify()
+void VertexArray::reserve(unsigned n)
{
- return new VertexArrayBuilder(*this);
+ data.reserve(n*stride);
}
-void VertexArray::apply() const
+float *VertexArray::append()
{
- 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 sz=(fmt&3)+1;
- switch(fmt&12)
- {
- case 0:
- glVertexPointer(sz, GL_FLOAT, bpv, base+offset);
- break;
- case 4:
- glNormalPointer(GL_FLOAT, bpv, base+offset);
- break;
- case 8:
- glTexCoordPointer(sz, GL_FLOAT, bpv, base+offset);
- break;
- case 12:
- if(sz==1)
- glColorPointer(4, GL_UNSIGNED_BYTE, bpv, base+offset);
- else
- glColorPointer(sz, GL_FLOAT, bpv, base+offset);
- break;
- }
- found|=1<<((fmt&12)>>2);
- offset+=sz;
- }
-
- set_array(GL_VERTEX_ARRAY, found&1, 1);
- set_array(GL_NORMAL_ARRAY, found&2, 2);
- set_array(GL_TEXTURE_COORD_ARRAY, found&4, 4);
- set_array(GL_COLOR_ARRAY, found&8, 8);
-
- if(vbuf)
- VertexBuffer::unbind();
+ data.insert(data.end(), stride, 0.0f);
+ update_offset();
+ dirty = true;
+ return &*(data.end()-stride);
}
-/**
-Updates the VertexArray data to the VertexBuffer tied to the array, if any.
-*/
-void VertexArray::update_data()
+float *VertexArray::modify(unsigned i)
{
- if(vbuf)
- {
- vbuf->data(data.size()*sizeof(float), &data[0]);
- VertexBuffer::unbind();
- }
+ dirty = true;
+ return &data[0]+i*stride;
}
-void VertexArray::set_array(unsigned array, unsigned bit, unsigned mask) const
+unsigned VertexArray::get_data_size() const
{
- if((enabled_arrays&mask) && !bit)
- {
- glDisableClientState(array);
- enabled_arrays&=~mask;
- }
- else if(!(enabled_arrays&mask) && bit)
- {
- glEnableClientState(array);
- enabled_arrays|=mask;
- }
+ return data.size()*sizeof(float);
}
-unsigned VertexArray::enabled_arrays=0;
-
VertexArray::Loader::Loader(VertexArray &a):
VertexArrayBuilder(a)
{
+ add("vertex", static_cast<void (Loader::*)(float, float)>(&Loader::vertex));
+ add("vertex", static_cast<void (Loader::*)(float, float, float)>(&Loader::vertex));
+ add("vertex", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::vertex));
+ add("normal", static_cast<void (Loader::*)(float, float, float)>(&Loader::normal));
+ add("texcoord", static_cast<void (Loader::*)(float)>(&Loader::texcoord));
+ add("texcoord", static_cast<void (Loader::*)(float, float)>(&Loader::texcoord));
+ add("texcoord", static_cast<void (Loader::*)(float, float, float)>(&Loader::texcoord));
+ add("texcoord", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::texcoord));
+ add("multitexcoord", static_cast<void (Loader::*)(unsigned, float)>(&Loader::multitexcoord));
+ add("multitexcoord", static_cast<void (Loader::*)(unsigned, float, float)>(&Loader::multitexcoord));
+ add("multitexcoord", static_cast<void (Loader::*)(unsigned, float, float, float)>(&Loader::multitexcoord));
+ add("multitexcoord", static_cast<void (Loader::*)(unsigned, float, float, float, float)>(&Loader::multitexcoord));
+ add("color", static_cast<void (Loader::*)(float, float, float)>(&Loader::color));
+ add("color", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::color));
+ add("attrib", static_cast<void (Loader::*)(unsigned, float)>(&Loader::attrib));
+ add("attrib", static_cast<void (Loader::*)(unsigned, float, float)>(&Loader::attrib));
+ add("attrib", static_cast<void (Loader::*)(unsigned, float, float, float)>(&Loader::attrib));
+ add("attrib", static_cast<void (Loader::*)(unsigned, float, float, float, float)>(&Loader::attrib));
+ add("tangent", static_cast<void (Loader::*)(float, float, float)>(&Loader::tangent));
+ add("binormal", static_cast<void (Loader::*)(float, float, float)>(&Loader::binormal));
+
add("vertex2", static_cast<void (Loader::*)(float, float)>(&Loader::vertex));
add("vertex3", static_cast<void (Loader::*)(float, float, float)>(&Loader::vertex));
add("vertex4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::vertex));
add("texcoord2", static_cast<void (Loader::*)(float, float)>(&Loader::texcoord));
add("texcoord3", static_cast<void (Loader::*)(float, float, float)>(&Loader::texcoord));
add("texcoord4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::texcoord));
+ add("multitexcoord1", static_cast<void (Loader::*)(unsigned, float)>(&Loader::multitexcoord));
+ add("multitexcoord2", static_cast<void (Loader::*)(unsigned, float, float)>(&Loader::multitexcoord));
+ add("multitexcoord3", static_cast<void (Loader::*)(unsigned, float, float, float)>(&Loader::multitexcoord));
+ add("multitexcoord4", static_cast<void (Loader::*)(unsigned, float, float, float, float)>(&Loader::multitexcoord));
add("color3", static_cast<void (Loader::*)(float, float, float)>(&Loader::color));
add("color4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::color));
+ add("attrib1", static_cast<void (Loader::*)(unsigned, float)>(&Loader::attrib));
+ add("attrib2", static_cast<void (Loader::*)(unsigned, float, float)>(&Loader::attrib));
+ add("attrib3", static_cast<void (Loader::*)(unsigned, float, float, float)>(&Loader::attrib));
+ add("attrib4", static_cast<void (Loader::*)(unsigned, float, float, float, float)>(&Loader::attrib));
+ add("tangent3", static_cast<void (Loader::*)(float, float, float)>(&Loader::tangent));
+ add("binormal3", static_cast<void (Loader::*)(float, float, float)>(&Loader::binormal));
}
} // namespace GL