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
72 if(vbuf) vbuf->bind();
74 const float *base=vbuf?0:&data[0];
77 for(uint fmt=format; fmt; fmt>>=4)
83 glVertexPointer(size, GL_FLOAT, stride, base+offset);
86 glNormalPointer(GL_FLOAT, stride, base+offset);
89 glTexCoordPointer(size, GL_FLOAT, stride, base+offset);
93 glColorPointer(4, GL_UNSIGNED_BYTE, stride, base+offset);
95 glColorPointer(size, GL_FLOAT, stride, base+offset);
98 found|=1<<((fmt&12)>>2);
102 set_array(GL_VERTEX_ARRAY, found&1, 1);
103 set_array(GL_NORMAL_ARRAY, found&2, 2);
104 set_array(GL_TEXTURE_COORD_ARRAY, found&4, 4);
105 set_array(GL_COLOR_ARRAY, found&8, 8);
107 VertexBuffer::unbind();
111 Updates the VertexArray data to the VertexBuffer tied to the array, if any.
113 void VertexArray::update_data()
116 vbuf->data(data.size()*sizeof(float), &data[0]);
119 void VertexArray::set_array(unsigned array, unsigned bit, unsigned mask) const
121 if((enabled_arrays&mask) && !bit)
123 glDisableClientState(array);
124 enabled_arrays&=~mask;
126 else if(!(enabled_arrays&mask) && bit)
128 glEnableClientState(array);
129 enabled_arrays|=mask;
133 unsigned VertexArray::enabled_arrays=0;
136 VertexArray::Loader::Loader(VertexArray &a):
137 VertexArrayBuilder(a, a.data)
139 add("vertex2", static_cast<void (Loader::*)(float, float)>(&Loader::vertex));
140 add("vertex3", static_cast<void (Loader::*)(float, float, float)>(&Loader::vertex));
141 add("vertex4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::vertex));
142 add("normal3", static_cast<void (Loader::*)(float, float, float)>(&Loader::normal));
143 add("texcoord1", static_cast<void (Loader::*)(float)>(&Loader::texcoord));
144 add("texcoord2", static_cast<void (Loader::*)(float, float)>(&Loader::texcoord));
145 add("texcoord3", static_cast<void (Loader::*)(float, float, float)>(&Loader::texcoord));
146 add("texcoord4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::texcoord));
147 add("color3", static_cast<void (Loader::*)(float, float, float)>(&Loader::color));
148 add("color4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::color));