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::clear()
56 void VertexArray::reset(VertexFormat f)
60 for(uint fmt=f; fmt; fmt>>=4)
61 format=(format, static_cast<VertexFormat>(fmt&15));
62 stride=get_stride(format);
65 RefPtr<VertexArrayBuilder> VertexArray::modify()
67 return new VertexArrayBuilder(*this, data);
70 void VertexArray::apply() const
73 throw InvalidState("Trying to apply a vertex apply of format NODATA");
78 const float *base=vbuf?0:&data[0];
81 for(uint fmt=format; fmt; fmt>>=4)
87 glVertexPointer(size, GL_FLOAT, stride, base+offset);
90 glNormalPointer(GL_FLOAT, stride, base+offset);
93 glTexCoordPointer(size, GL_FLOAT, stride, base+offset);
97 glColorPointer(4, GL_UNSIGNED_BYTE, stride, base+offset);
99 glColorPointer(size, GL_FLOAT, stride, base+offset);
102 found|=1<<((fmt&12)>>2);
106 set_array(GL_VERTEX_ARRAY, found&1, 1);
107 set_array(GL_NORMAL_ARRAY, found&2, 2);
108 set_array(GL_TEXTURE_COORD_ARRAY, found&4, 4);
109 set_array(GL_COLOR_ARRAY, found&8, 8);
111 VertexBuffer::unbind();
115 Updates the VertexArray data to the VertexBuffer tied to the array, if any.
117 void VertexArray::update_data()
120 vbuf->data(data.size()*sizeof(float), &data[0]);
123 void VertexArray::set_array(unsigned array, unsigned bit, unsigned mask) const
125 if((enabled_arrays&mask) && !bit)
127 glDisableClientState(array);
128 enabled_arrays&=~mask;
130 else if(!(enabled_arrays&mask) && bit)
132 glEnableClientState(array);
133 enabled_arrays|=mask;
137 unsigned VertexArray::enabled_arrays=0;
140 VertexArray::Loader::Loader(VertexArray &a):
141 VertexArrayBuilder(a, a.data)
143 add("vertex2", static_cast<void (Loader::*)(float, float)>(&Loader::vertex));
144 add("vertex3", static_cast<void (Loader::*)(float, float, float)>(&Loader::vertex));
145 add("vertex4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::vertex));
146 add("normal3", static_cast<void (Loader::*)(float, float, float)>(&Loader::normal));
147 add("texcoord1", static_cast<void (Loader::*)(float)>(&Loader::texcoord));
148 add("texcoord2", static_cast<void (Loader::*)(float, float)>(&Loader::texcoord));
149 add("texcoord3", static_cast<void (Loader::*)(float, float, float)>(&Loader::texcoord));
150 add("texcoord4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::texcoord));
151 add("color3", static_cast<void (Loader::*)(float, float, float)>(&Loader::color));
152 add("color4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::color));