-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscene.cpp
More file actions
353 lines (291 loc) · 8.08 KB
/
scene.cpp
File metadata and controls
353 lines (291 loc) · 8.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include <node.hpp>
#include <scene.hpp>
#include <mesh/object.hpp>
#include <mesh/cube.hpp>
#include <mesh/sphere.hpp>
#include <mesh/cylinder.hpp>
#include <mesh/mesh.hpp>
#include <shader.hpp>
#include <camera.hpp>
#include <texture.hpp>
#include <cstdio>
#include <light.hpp>
#include <map>
#include <string>
#include <deque>
#include <set>
class ContourShader : public Shader {
public:
ContourShader() : Shader() {}
ContourShader(std::string pfx) : Shader(pfx) {}
ContourShader(std::string vert, std::string frag) : Shader(vert,frag) {}
virtual void draw(Object *obj) {
use();
// save context and set our stencil on the diffuse pass
glPushAttrib(GL_ALL_ATTRIB_BITS);
glClearStencil(0);
glClear(GL_STENCIL_BUFFER_BIT);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS, 1, -1);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
obj->draw(this);
// use our stencil on the contour pass and restore context
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glStencilFunc(GL_NOTEQUAL, 1, -1);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
glLineWidth(2);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glEnable(GL_LINE_SMOOTH);
glColor3f(.75,.75,.75);
obj->draw(this);
glPopAttrib();
}
};
Scene::Scene(const Config& conf) {
nodes["null"] = new Node("null");
geoms["cube"] = new Cube();
geoms["sphere"] = new Sphere();
geoms["null"] = NULL;
geoms["cylinder"] = new Cylinder();
shaders["diffuse"] = new Shader("shaders/diff");
shaders["contour"] = new ContourShader("shaders/cntr");
textures["null"] = NULL;
materials["default"] = new MatConf();
camera = new Camera(conf.camera);
for(unsigned int i = 0; i < conf.shaders.size(); i++)
addShader(conf.shaders[i]);
for(unsigned int i = 0; i < conf.textures.size(); i++)
addTexture(conf.textures[i].name, conf.textures[i].fName);
for(unsigned int i = 0; i < conf.materials.size(); i++)
addMaterial(conf.materials[i]);
for(unsigned int i = 0; i < conf.nodes.size(); i++)
addNode(conf.nodes[i]);
for(unsigned int i = 0; i < conf.lights.size(); i++)
lights.push_back(new PointLight(conf.lights[i]));
buildPreOrder();
}
Scene::~Scene() {
std::map<std::string,Object*>::iterator objItr;
for(objItr = geoms.begin(); objItr != geoms.end(); objItr++) {
if(objItr->second)
delete objItr->second;
}
std::map<std::string,Shader*>::iterator sItr;
for(sItr = shaders.begin(); sItr != shaders.end(); sItr++) {
if(sItr->second)
delete sItr->second;
}
std::map<std::string,MatConf*>::iterator matItr;
for(matItr = materials.begin(); matItr != materials.end(); matItr++) {
if(matItr->second)
delete matItr->second;
}
delete camera;
delete nodes["null"];
}
void Scene::resize(int width, int height) {
camera->resize(width,height);
}
void Scene::addMesh(std::string name, std::string objFile) {
if(geoms[name])
delete geoms[name];
geoms[name] = new Mesh(objFile);
}
void Scene::addNode(const NodeConf& conf) {
std::string name = conf.name;
std::string parent = conf.parentName;
std::string shape = conf.shape;
Node *node;
if(shape != "mesh") {
node = new Node(conf, geoms[shape]);
} else {
geoms[name] = new Mesh(conf.objFile);
node = new Node(conf, geoms[name]);
}
node->addShader(shaders["diffuse"]);
node->bindTexture(textures[conf.texName]);
node->bindMaterial(materials[conf.matName]);
if(materials[conf.matName] && materials[conf.matName]->lEmit > 0.001)
lights.push_back(new SolidLight(node));
if(nodes[name])
delete nodes[name];
nodes[name] = node;
nodes[parent]->addChild(node);
}
void Scene::delNode(std::string name) {
if(nodes[name]) {
delete nodes[name];
nodes.erase(name);
}
buildPreOrder();
}
void Scene::addMaterial(const MatConf& m) {
std::string name = m.name;
if(materials[name])
delete materials[name];
materials[name] = new MatConf();
*materials[name] = m;
}
void Scene::addShader(std::string name, std::string vSrc, std::string fSrc) {
if(shaders[name])
delete shaders[name];
shaders[name] = new Shader(vSrc, fSrc);
}
void Scene::addShader(std::string name, std::string pfx) {
if(shaders[name])
delete shaders[name];
shaders[name] = new Shader(pfx);
}
void Scene::addShader(const ShaderConf& conf) {
if(shaders[conf.name])
delete shaders[conf.name];
shaders[conf.name] = new Shader(conf);
}
void Scene::delShader(std::string name) {
if(shaders[name]) {
delete shaders[name];
shaders.erase(name);
}
}
std::deque<std::string> Scene::nodeList() {
return preOrder;
}
void Scene::draw(std::string sName) {
Shader *shader = shaders[sName];
if(!shader) {
std::cerr << "error: invalid shader " << sName << "\n";
return;
}
shader->bind(camera);
for(unsigned int i = 0; i < lights.size(); i++) {
shader->setUniform("u_LightPos", lights[i]->pos());
shader->setUniform("u_LightColor", lights[i]->color());
}
nodes["null"]->draw(shader);
}
void Scene::buildPreOrder() {
preOrder.clear();
nodes["null"]->buildPreOrder(preOrder);
}
void Scene::bindShader(std::string nName, std::string sName) {
Node *n = nodes[nName];
Shader *s = shaders[sName];
if(!n) {
std::cout << "error: no such node " << nName << std::endl;
return;
}
if(!s) {
std::cout << "error: no such shader " << sName << std::endl;
return;
}
n->addShader(s);
}
void Scene::unbindShader(std::string nName, std::string sName) {
Node *n = nodes[nName];
Shader *s = shaders[sName];
if(!n) {
std::cout << "error: no such node " << nName << std::endl;
return;
}
if(!s) {
std::cout << "error: no such shader " << sName << std::endl;
return;
}
n->delShader(s);
}
void Scene::translate(std::string name, const glm::vec3& dv) {
Node *n = nodes[name];
if(!n) {
std::cout << "error: no such node " << name << std::endl;
return;
}
n->translate(dv);
}
void Scene::rotate(std::string name, const glm::vec3& dv) {
Node *n = nodes[name];
if(!n) {
std::cout << "error: no such node " << name << std::endl;
return;
}
n->rotate(dv);
}
void Scene::scale(std::string name, const glm::vec3& dv) {
Node *n = nodes[name];
if(!n) {
std::cout << "error: no such node " << name << std::endl;
return;
}
n->scale(dv);
}
void Scene::center(std::string name, const glm::vec3& v) {
Node *n = nodes[name];
if(!n) {
std::cout << "error: no such node " << name << std::endl;
return;
}
n->center(v);
}
void Scene::addTexture(std::string name, std::string img) {
if(!textures[name])
delete textures[name];
textures[name] = new Texture(img);
}
void Scene::delTexture(std::string name) {
if(textures[name]) {
delete textures[name];
textures.erase(name);
}
}
void Scene::bindMaterial(std::string nName, std::string mName) {
Node *n = nodes[nName];
MatConf *m = materials[mName];
if(!n) {
std::cout << "error: no such node " << nName << std::endl;
return;
}
if(!m) {
std::cout << "error: no such material " << mName << std::endl;
return;
}
n->bindMaterial(m);
}
void Scene::bindTexture(std::string nName, std::string tName) {
Node *n = nodes[nName];
Texture *t = textures[tName];
if(!n) {
std::cout << "error: no such node " << nName << std::endl;
return;
}
if(!t) {
std::cout << "error: no such texture " << tName << std::endl;
return;
}
n->bindTexture(t);
}
void Scene::unbindTexture(std::string nName, std::string tName) {
Node *n = nodes[nName];
Texture *t = textures[tName];
if(!n) {
std::cout << "error: no such node " << nName << std::endl;
return;
}
if(!t) {
std::cout << "error: no such texture " << tName << std::endl;
return;
}
n->unbindTexture();
}
void Scene::moveLight(glm::vec3 dv) {
for(unsigned int i = 0; i < lights.size(); i++)
lights[i]->pos() += dv;
}
void Scene::moveMouse(float xpos, float ypos) {
camera->rotate(xpos, ypos, .1);
}
void Scene::raytrace() {
camera->raytrace(nodes["null"],lights);
}
void Scene::zoom(float dy) {
camera->zoom(dy);
}