1 #include <msp/gl/extensions/arb_vertex_array_object.h>
2 #include <msp/gl/extensions/arb_vertex_shader.h>
23 Mesh::Mesh(const VertexFormat &f):
38 glDeleteVertexArrays(1, &vao_id);
47 void Mesh::use_buffers(bool b)
49 defer_buffers = false;
54 vertices.use_buffer(0);
62 void Mesh::create_buffers()
64 defer_buffers = false;
67 vbuf = new Buffer(ARRAY_BUFFER);
68 vertices.use_buffer(vbuf);
71 ibuf = new Buffer(ELEMENT_ARRAY_BUFFER);
73 if(ARB_vertex_array_object && !vao_id)
74 glGenVertexArrays(1, &vao_id);
77 void Mesh::refresh() const
84 for(list<Batch>::const_iterator i=batches.begin(); i!=batches.end(); ++i)
90 Bind bind_vbuf(vbuf, ARRAY_BUFFER);
92 bool bind_here = !current();
94 glBindVertexArray(vao_id);
96 const VertexFormat &fmt = vertices.get_format();
97 unsigned stride = get_stride(fmt)*sizeof(float);
99 for(const unsigned char *c=fmt.begin(); c!=fmt.end(); ++c)
101 unsigned t = get_component_type(*c);
102 if(t>=get_component_type(ATTRIB1))
103 t -= get_component_type(ATTRIB1);
104 unsigned sz = get_component_size(*c);
106 glVertexAttribPointer(t, 4, GL_UNSIGNED_BYTE, true, stride, ptr);
108 glVertexAttribPointer(t, sz, GL_FLOAT, false, stride, ptr);
109 glEnableVertexAttribArray(t);
112 glBindBuffer(ELEMENT_ARRAY_BUFFER, ibuf->get_id());
115 glBindVertexArray(0);
122 unsigned Mesh::get_n_vertices() const
124 return vertices.size();
127 float *Mesh::modify_vertex(unsigned i)
129 return vertices.modify(i);
132 void Mesh::add_batch(const Batch &b)
138 if(!batches.empty() && batches.back().can_append(b.get_type()))
139 batches.back().append(b);
142 Batch *prev = (batches.empty() ? 0 : &batches.back());
143 batches.push_back(b);
145 batches.back().use_buffer(ibuf, prev);
149 void Mesh::set_winding(const WindingTest *w)
154 void Mesh::draw() const
156 const Mesh *cur = current();
158 throw invalid_operation("Mesh::draw");
164 Bind bind_ibuf(ibuf, ELEMENT_ARRAY_BUFFER);
165 Bind bind_winding(winding);
167 for(list<Batch>::const_iterator i=batches.begin(); i!=batches.end(); ++i)
171 void Mesh::draw(Renderer &renderer) const
175 renderer.set_mesh(this);
176 renderer.set_element_buffer(ibuf);
177 renderer.set_winding_test(winding);
179 for(list<Batch>::const_iterator i=batches.begin(); i!=batches.end(); ++i)
183 void Mesh::bind() const
185 /* If VAOs are not supported, vao_id is zero and set_current won't get
186 called. Thus unbind won't try to call a null function either. */
189 else if(set_current(this))
190 glBindVertexArray(vao_id);
196 glBindVertexArray(0);
200 Mesh::Loader::Loader(Mesh &m):
201 DataFile::ObjectLoader<Mesh>(m)
203 add("batch", &Loader::batch);
204 add("vertices", &Loader::vertices);
205 add("winding", &Loader::winding);
208 void Mesh::Loader::vertices(const vector<VertexComponent> &c)
211 throw invalid_argument("No vertex components");
214 for(vector<VertexComponent>::const_iterator i=c.begin(); i!=c.end(); ++i)
216 obj.vertices.reset(fmt);
218 load_sub(obj.vertices);
221 void Mesh::Loader::batch(PrimitiveType p)
228 void Mesh::Loader::winding(FaceWinding w)
231 obj.winding = &WindingTest::clockwise();
232 else if(w==COUNTERCLOCKWISE)
233 obj.winding = &WindingTest::counterclockwise();