]> git.tdb.fi Git - libs/gl.git/blob - source/mesh.cpp
Style update: add spaces around assignment operators
[libs/gl.git] / source / mesh.cpp
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2007  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include "buffer.h"
9 #include "mesh.h"
10
11 using namespace std;
12
13 namespace Msp {
14 namespace GL {
15
16 Mesh::Mesh():
17         vertices(VERTEX3),
18         ibuf(0)
19 { }
20
21 Mesh::Mesh(const VertexFormat &f):
22         vertices(f),
23         ibuf(0)
24 { }
25
26 void Mesh::use_vertex_buffer(bool b)
27 {
28         if(b)
29         {
30                 vertices.use_vertex_buffer();
31                 if(!ibuf)
32                         ibuf = new Buffer(ELEMENT_ARRAY_BUFFER);
33                 update_index_buffer();
34         }
35         else
36         {
37                 vertices.use_vertex_buffer(0);
38                 delete ibuf;
39                 ibuf = 0;
40         }
41 }
42
43 float *Mesh::get_vertex(unsigned i)
44 {
45         return vertices[i];
46 }
47
48 void Mesh::add_batch(const Batch &b)
49 {
50         batches.push_back(b);
51         update_index_buffer();
52 }
53
54 void Mesh::clear()
55 {
56         vertices.clear();
57         batches.clear();
58 }
59
60 void Mesh::draw() const
61 {
62         vertices.apply();
63         if(ibuf)
64         {
65                 ibuf->bind();
66                 unsigned offset = 0;
67                 for(list<Batch>::const_iterator i=batches.begin(); i!=batches.end(); ++i)
68                 {
69                         i->draw_with_buffer(offset);
70                         offset += i->size();
71                 }
72                 ibuf->unbind();
73         }
74         else
75         {
76                 for(list<Batch>::const_iterator i=batches.begin(); i!=batches.end(); ++i)
77                         i->draw();
78         }
79 }
80
81 void Mesh::update_index_buffer()
82 {
83         if(!ibuf)
84                 return;
85
86         unsigned total = 0;
87         for(list<Batch>::const_iterator i=batches.begin(); i!=batches.end(); ++i)
88                 total += i->size();
89
90         ibuf->data(total*sizeof(unsigned), 0);
91         unsigned offset = 0;
92         for(list<Batch>::const_iterator i=batches.begin(); i!=batches.end(); ++i)
93         {
94                 ibuf->sub_data(offset*sizeof(unsigned), i->size()*sizeof(unsigned), &i->get_indices()[0]);
95                 offset += i->size();
96         }
97         ibuf->unbind();
98 }
99
100
101 Mesh::Loader::Loader(Mesh &m):
102         DataFile::ObjectLoader<Mesh>(m)
103 {
104         add("vertices", &Loader::vertices);
105         add("batch",    &Loader::batch);
106 }
107
108 void Mesh::Loader::vertices(VertexFormat f)
109 {
110         obj.vertices.reset(f);
111         load_sub(obj.vertices);
112 }
113
114 void Mesh::Loader::batch(PrimitiveType p)
115 {
116         obj.batches.push_back(Batch(p));
117         load_sub(obj.batches.back());
118 }
119
120 } // namespace GL
121 } // namespace Msp