1 #include <msp/gl/extensions/arb_vertex_array_object.h>
2 #include <msp/gl/extensions/arb_vertex_shader.h>
22 Mesh::Mesh(const VertexFormat &f):
37 glDeleteVertexArrays(1, &vao_id);
46 void Mesh::use_buffers(bool b)
48 defer_buffers = false;
53 vertices.use_buffer(0);
61 void Mesh::create_buffers()
63 defer_buffers = false;
66 vbuf = new Buffer(ARRAY_BUFFER);
67 vertices.use_buffer(vbuf);
70 ibuf = new Buffer(ELEMENT_ARRAY_BUFFER);
72 if(ARB_vertex_array_object && !vao_id)
73 glGenVertexArrays(1, &vao_id);
76 void Mesh::refresh() const
84 for(list<Batch>::const_iterator i=batches.begin(); i!=batches.end(); ++i)
90 glBindVertexArray(vao_id);
91 BufferAlias<ARRAY_BUFFER> vbuf_alias(*vbuf);
92 Bind bind_vbuf(vbuf_alias);
94 const VertexFormat &fmt = vertices.get_format();
95 unsigned stride = get_stride(fmt)*sizeof(float);
97 for(const unsigned char *c=fmt.begin(); c!=fmt.end(); ++c)
99 unsigned t = get_component_type(*c);
100 if(t>=get_component_type(ATTRIB1))
101 t -= get_component_type(ATTRIB1);
102 unsigned sz = get_component_size(*c);
104 glVertexAttribPointer(t, 4, GL_UNSIGNED_BYTE, true, stride, ptr);
106 glVertexAttribPointer(t, sz, GL_FLOAT, false, stride, ptr);
107 glEnableVertexAttribArray(t);
110 glBindBuffer(ELEMENT_ARRAY_BUFFER, ibuf->get_id());
111 glBindVertexArray(0);
118 unsigned Mesh::get_n_vertices() const
120 return vertices.size();
123 float *Mesh::modify_vertex(unsigned i)
125 return vertices.modify(i);
128 void Mesh::add_batch(const Batch &b)
134 if(!batches.empty() && batches.back().can_append(b.get_type()))
135 batches.back().append(b);
138 Batch *prev = (batches.empty() ? 0 : &batches.back());
139 batches.push_back(b);
141 batches.back().use_buffer(ibuf, prev);
145 void Mesh::set_winding(const WindingTest *w)
150 void Mesh::draw() const
158 ibuf->bind_to(ELEMENT_ARRAY_BUFFER);
161 Bind bind_winding(winding);
163 for(list<Batch>::const_iterator i=batches.begin(); i!=batches.end(); ++i)
166 if(!current() && ibuf)
167 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
170 void Mesh::draw(Renderer &renderer) const
173 renderer.set_element_buffer(ibuf);
174 renderer.set_winding_test(winding);
176 for(list<Batch>::const_iterator i=batches.begin(); i!=batches.end(); ++i)
180 void Mesh::bind() const
182 /* If VAOs are not supported, vao_id is zero and set_current won't get
183 called. Thus unbind won't try to call a null function either. */
188 if(Buffer::current(ELEMENT_ARRAY_BUFFER))
191 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
193 if(set_current(this))
194 glBindVertexArray(vao_id);
201 glBindVertexArray(0);
205 Mesh::Loader::Loader(Mesh &m):
206 DataFile::ObjectLoader<Mesh>(m)
208 add("batch", &Loader::batch);
209 add("vertices", &Loader::vertices);
210 add("winding", &Loader::winding);
213 void Mesh::Loader::vertices(const vector<VertexComponent> &c)
216 throw invalid_argument("No vertex components");
219 for(vector<VertexComponent>::const_iterator i=c.begin(); i!=c.end(); ++i)
221 obj.vertices.reset(fmt);
223 load_sub(obj.vertices);
226 void Mesh::Loader::batch(PrimitiveType p)
233 void Mesh::Loader::winding(FaceWinding w)
236 obj.winding = &WindingTest::clockwise();
237 else if(w==COUNTERCLOCKWISE)
238 obj.winding = &WindingTest::counterclockwise();