]> git.tdb.fi Git - libs/gl.git/blob - source/mesh.cpp
Use renderer for lighting in the shaders demo
[libs/gl.git] / source / mesh.cpp
1 #include "buffer.h"
2 #include "mesh.h"
3 #include "renderer.h"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace GL {
9
10 Mesh::Mesh():
11         vertices(VERTEX3),
12         vbuf(0),
13         ibuf(0),
14         defer_buffers(true),
15         winding(0)
16 { }
17
18 Mesh::Mesh(const VertexFormat &f):
19         vertices(f),
20         vbuf(0),
21         ibuf(0),
22         defer_buffers(true),
23         winding(0)
24 { }
25
26 Mesh::~Mesh()
27 {
28         delete vbuf;
29         delete ibuf;
30 }
31
32 void Mesh::clear()
33 {
34         vertices.clear();
35         batches.clear();
36 }
37
38 void Mesh::use_buffers(bool b)
39 {
40         defer_buffers = false;
41         if(b)
42                 create_buffers();
43         else
44         {
45                 vertices.use_vertex_buffer(0);
46                 delete vbuf;
47                 vbuf = 0;
48                 delete ibuf;
49                 ibuf = 0;
50         }
51 }
52
53 void Mesh::create_buffers()
54 {
55         defer_buffers = false;
56
57         if(!vbuf)
58                 vbuf = new Buffer(ARRAY_BUFFER);
59         vertices.use_vertex_buffer(vbuf);
60
61         if(!ibuf)
62                 ibuf = new Buffer(ELEMENT_ARRAY_BUFFER);
63 }
64
65 unsigned Mesh::get_n_vertices() const
66 {
67         return vertices.size();
68 }
69
70 float *Mesh::modify_vertex(unsigned i)
71 {
72         return vertices.modify(i);
73 }
74
75 void Mesh::add_batch(const Batch &b)
76 {
77         if(defer_buffers)
78                 create_buffers();
79
80         if(!batches.empty() && batches.back().can_append(b.get_type()))
81                 batches.back().append(b);
82         else
83         {
84                 Batch *prev = (batches.empty() ? 0 : &batches.back());
85                 batches.push_back(b);
86                 if(ibuf)
87                         batches.back().use_buffer(ibuf, prev);
88         }
89 }
90
91 void Mesh::set_winding(const WindingTest *w)
92 {
93         winding = w;
94 }
95
96 void Mesh::draw() const
97 {
98         vertices.apply();
99
100         if(ibuf)
101                 ibuf->bind_to(ELEMENT_ARRAY_BUFFER);
102         Bind bind_winding(winding);
103
104         for(list<Batch>::const_iterator i=batches.begin(); i!=batches.end(); ++i)
105                 i->draw();
106
107         if(ibuf)
108                 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
109 }
110
111 void Mesh::draw(Renderer &renderer) const
112 {
113         vertices.apply();
114         renderer.set_element_buffer(ibuf);
115         renderer.set_winding_test(winding);
116
117         for(list<Batch>::const_iterator i=batches.begin(); i!=batches.end(); ++i)
118                 renderer.draw(*i);
119 }
120
121
122 Mesh::Loader::Loader(Mesh &m):
123         DataFile::ObjectLoader<Mesh>(m)
124 {
125         add("batch",    &Loader::batch);
126         add("vertices", &Loader::vertices);
127         add("winding",  &Loader::winding);
128 }
129
130 void Mesh::Loader::vertices(const vector<VertexComponent> &c)
131 {
132         if(c.empty())
133                 throw invalid_argument("No vertex components");
134
135         VertexFormat fmt;
136         for(vector<VertexComponent>::const_iterator i=c.begin(); i!=c.end(); ++i)
137                 fmt = (fmt, *i);
138         obj.vertices.reset(fmt);
139         load_sub(obj.vertices);
140 }
141
142 void Mesh::Loader::batch(PrimitiveType p)
143 {
144         Batch btc(p);
145         load_sub(btc);
146         obj.add_batch(btc);
147 }
148
149 void Mesh::Loader::winding(FaceWinding w)
150 {
151         if(w==CLOCKWISE)
152                 obj.winding = &WindingTest::clockwise();
153         else if(w==COUNTERCLOCKWISE)
154                 obj.winding = &WindingTest::counterclockwise();
155 }
156
157 } // namespace GL
158 } // namespace Msp