3 This file is part of libmspgl
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
9 #include "vertexarray.h"
10 #include "vertexbuffer.h"
17 VertexArray::VertexArray(VertexFormat f):
19 stride(get_stride(f)),
23 // Reverse the format so the first item is in lowest bits. This makes handling in bind() easier.
24 for(uint fmt=f; fmt; fmt>>=4)
25 format=(format, static_cast<VertexFormat>(fmt&15));
28 VertexArray::~VertexArray()
34 void VertexArray::use_vertex_buffer()
39 vbuf=new VertexBuffer();
45 void VertexArray::use_vertex_buffer(VertexBuffer *b)
55 void VertexArray::reserve(unsigned n)
57 data.reserve(n*stride);
60 void VertexArray::clear()
65 void VertexArray::reset(VertexFormat f)
69 for(uint fmt=f; fmt; fmt>>=4)
70 format=(format, static_cast<VertexFormat>(fmt&15));
71 stride=get_stride(format);
74 RefPtr<VertexArrayBuilder> VertexArray::modify()
76 return new VertexArrayBuilder(*this);
79 void VertexArray::apply() const
82 throw InvalidState("Trying to apply a vertex apply of format NODATA");
87 const float *base=vbuf?0:&data[0];
90 uint bpv=stride*sizeof(float);
91 for(uint fmt=format; fmt; fmt>>=4)
97 glVertexPointer(sz, GL_FLOAT, bpv, base+offset);
100 glNormalPointer(GL_FLOAT, bpv, base+offset);
103 glTexCoordPointer(sz, GL_FLOAT, bpv, base+offset);
107 glColorPointer(4, GL_UNSIGNED_BYTE, bpv, base+offset);
109 glColorPointer(sz, GL_FLOAT, bpv, base+offset);
112 found|=1<<((fmt&12)>>2);
116 set_array(GL_VERTEX_ARRAY, found&1, 1);
117 set_array(GL_NORMAL_ARRAY, found&2, 2);
118 set_array(GL_TEXTURE_COORD_ARRAY, found&4, 4);
119 set_array(GL_COLOR_ARRAY, found&8, 8);
121 VertexBuffer::unbind();
125 Updates the VertexArray data to the VertexBuffer tied to the array, if any.
127 void VertexArray::update_data()
130 vbuf->data(data.size()*sizeof(float), &data[0]);
133 void VertexArray::set_array(unsigned array, unsigned bit, unsigned mask) const
135 if((enabled_arrays&mask) && !bit)
137 glDisableClientState(array);
138 enabled_arrays&=~mask;
140 else if(!(enabled_arrays&mask) && bit)
142 glEnableClientState(array);
143 enabled_arrays|=mask;
147 unsigned VertexArray::enabled_arrays=0;
150 VertexArray::Loader::Loader(VertexArray &a):
151 VertexArrayBuilder(a)
153 add("vertex2", static_cast<void (Loader::*)(float, float)>(&Loader::vertex));
154 add("vertex3", static_cast<void (Loader::*)(float, float, float)>(&Loader::vertex));
155 add("vertex4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::vertex));
156 add("normal3", static_cast<void (Loader::*)(float, float, float)>(&Loader::normal));
157 add("texcoord1", static_cast<void (Loader::*)(float)>(&Loader::texcoord));
158 add("texcoord2", static_cast<void (Loader::*)(float, float)>(&Loader::texcoord));
159 add("texcoord3", static_cast<void (Loader::*)(float, float, float)>(&Loader::texcoord));
160 add("texcoord4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::texcoord));
161 add("color3", static_cast<void (Loader::*)(float, float, float)>(&Loader::color));
162 add("color4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::color));