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);
122 VertexBuffer::unbind();
126 Updates the VertexArray data to the VertexBuffer tied to the array, if any.
128 void VertexArray::update_data()
132 vbuf->data(data.size()*sizeof(float), &data[0]);
133 VertexBuffer::unbind();
137 void VertexArray::set_array(unsigned array, unsigned bit, unsigned mask) const
139 if((enabled_arrays&mask) && !bit)
141 glDisableClientState(array);
142 enabled_arrays&=~mask;
144 else if(!(enabled_arrays&mask) && bit)
146 glEnableClientState(array);
147 enabled_arrays|=mask;
151 unsigned VertexArray::enabled_arrays=0;
154 VertexArray::Loader::Loader(VertexArray &a):
155 VertexArrayBuilder(a)
157 add("vertex2", static_cast<void (Loader::*)(float, float)>(&Loader::vertex));
158 add("vertex3", static_cast<void (Loader::*)(float, float, float)>(&Loader::vertex));
159 add("vertex4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::vertex));
160 add("normal3", static_cast<void (Loader::*)(float, float, float)>(&Loader::normal));
161 add("texcoord1", static_cast<void (Loader::*)(float)>(&Loader::texcoord));
162 add("texcoord2", static_cast<void (Loader::*)(float, float)>(&Loader::texcoord));
163 add("texcoord3", static_cast<void (Loader::*)(float, float, float)>(&Loader::texcoord));
164 add("texcoord4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::texcoord));
165 add("color3", static_cast<void (Loader::*)(float, float, float)>(&Loader::color));
166 add("color4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::color));