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()
38 vbuf=new VertexBuffer();
43 void VertexArray::use_vertex_buffer(VertexBuffer *b)
51 void VertexArray::reserve(unsigned n)
53 data.reserve(n*stride);
56 void VertexArray::clear()
61 void VertexArray::reset(VertexFormat f)
65 for(uint fmt=f; fmt; fmt>>=4)
66 format=(format, static_cast<VertexFormat>(fmt&15));
67 stride=get_stride(format);
70 RefPtr<VertexArrayBuilder> VertexArray::modify()
72 return new VertexArrayBuilder(*this);
75 void VertexArray::apply() const
78 throw InvalidState("Trying to apply a vertex apply of format NODATA");
83 const float *base=vbuf?0:&data[0];
86 uint bpv=stride*sizeof(float);
87 for(uint fmt=format; fmt; fmt>>=4)
93 glVertexPointer(sz, GL_FLOAT, bpv, base+offset);
96 glNormalPointer(GL_FLOAT, bpv, base+offset);
99 glTexCoordPointer(sz, GL_FLOAT, bpv, base+offset);
103 glColorPointer(4, GL_UNSIGNED_BYTE, bpv, base+offset);
105 glColorPointer(sz, GL_FLOAT, bpv, base+offset);
108 found|=1<<((fmt&12)>>2);
112 set_array(GL_VERTEX_ARRAY, found&1, 1);
113 set_array(GL_NORMAL_ARRAY, found&2, 2);
114 set_array(GL_TEXTURE_COORD_ARRAY, found&4, 4);
115 set_array(GL_COLOR_ARRAY, found&8, 8);
117 VertexBuffer::unbind();
121 Updates the VertexArray data to the VertexBuffer tied to the array, if any.
123 void VertexArray::update_data()
126 vbuf->data(data.size()*sizeof(float), &data[0]);
129 void VertexArray::set_array(unsigned array, unsigned bit, unsigned mask) const
131 if((enabled_arrays&mask) && !bit)
133 glDisableClientState(array);
134 enabled_arrays&=~mask;
136 else if(!(enabled_arrays&mask) && bit)
138 glEnableClientState(array);
139 enabled_arrays|=mask;
143 unsigned VertexArray::enabled_arrays=0;
146 VertexArray::Loader::Loader(VertexArray &a):
147 VertexArrayBuilder(a)
149 add("vertex2", static_cast<void (Loader::*)(float, float)>(&Loader::vertex));
150 add("vertex3", static_cast<void (Loader::*)(float, float, float)>(&Loader::vertex));
151 add("vertex4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::vertex));
152 add("normal3", static_cast<void (Loader::*)(float, float, float)>(&Loader::normal));
153 add("texcoord1", static_cast<void (Loader::*)(float)>(&Loader::texcoord));
154 add("texcoord2", static_cast<void (Loader::*)(float, float)>(&Loader::texcoord));
155 add("texcoord3", static_cast<void (Loader::*)(float, float, float)>(&Loader::texcoord));
156 add("texcoord4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::texcoord));
157 add("color3", static_cast<void (Loader::*)(float, float, float)>(&Loader::color));
158 add("color4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::color));