This repository has been archived by the owner on Aug 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Scene.cpp
377 lines (302 loc) · 11.7 KB
/
Scene.cpp
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include "Scene.hpp"
#include "gl_errors.hpp"
#include "read_write_chunk.hpp"
#include <glm/gtc/type_ptr.hpp>
#include <fstream>
//-------------------------
glm::mat4x3 Scene::Transform::make_local_to_parent() const {
//compute:
// translate * rotate * scale
// [ 1 0 0 p.x ] [ 0 ] [ s.x 0 0 0 ]
// [ 0 1 0 p.y ] * [ rot 0 ] * [ 0 s.y 0 0 ]
// [ 0 0 1 p.z ] [ 0 ] [ 0 0 s.z 0 ]
// [ 0 0 0 1 ] [ 0 0 0 1 ]
glm::mat3 rot = glm::mat3_cast(rotation);
return glm::mat4x3(
rot[0] * scale.x, //scaling the columns here means that scale happens before rotation
rot[1] * scale.y,
rot[2] * scale.z,
position
);
}
glm::mat4x3 Scene::Transform::make_parent_to_local() const {
//compute:
// 1/scale * rot^-1 * translate^-1
// [ 1/s.x 0 0 0 ] [ 0 ] [ 0 0 0 -p.x ]
// [ 0 1/s.y 0 0 ] * [rot^-1 0 ] * [ 0 0 0 -p.y ]
// [ 0 0 1/s.z 0 ] [ 0 ] [ 0 0 0 -p.z ]
// [ 0 0 0 1 ] [ 0 0 0 1 ]
glm::vec3 inv_scale;
//taking some care so that we don't end up with NaN's , just a degenerate matrix, if scale is zero:
inv_scale.x = (scale.x == 0.0f ? 0.0f : 1.0f / scale.x);
inv_scale.y = (scale.y == 0.0f ? 0.0f : 1.0f / scale.y);
inv_scale.z = (scale.z == 0.0f ? 0.0f : 1.0f / scale.z);
//compute inverse of rotation:
glm::mat3 inv_rot = glm::mat3_cast(glm::inverse(rotation));
//scale the rows of rot:
inv_rot[0] *= inv_scale;
inv_rot[1] *= inv_scale;
inv_rot[2] *= inv_scale;
return glm::mat4x3(
inv_rot[0],
inv_rot[1],
inv_rot[2],
inv_rot * -position
);
}
glm::mat4x3 Scene::Transform::make_local_to_world() const {
if (!parent) {
return make_local_to_parent();
} else {
return parent->make_local_to_world() * glm::mat4(make_local_to_parent()); //note: glm::mat4(glm::mat4x3) pads with a (0,0,0,1) row
}
}
glm::mat4x3 Scene::Transform::make_world_to_local() const {
if (!parent) {
return make_parent_to_local();
} else {
return make_parent_to_local() * glm::mat4(parent->make_world_to_local()); //note: glm::mat4(glm::mat4x3) pads with a (0,0,0,1) row
}
}
//-------------------------
glm::mat4 Scene::Camera::make_projection() const {
return glm::infinitePerspective( fovy, aspect, near );
}
//-------------------------
void Scene::draw(Camera const &camera) const {
assert(camera.transform);
glm::mat4 world_to_clip = camera.make_projection() * glm::mat4(camera.transform->make_world_to_local());
glm::mat4x3 world_to_light = glm::mat4x3(1.0f);
draw(world_to_clip, world_to_light);
}
void Scene::draw(glm::mat4 const &world_to_clip, glm::mat4x3 const &world_to_light) const {
//Iterate through all drawables, sending each one to OpenGL:
for (auto const &drawable : drawables) {
//Reference to drawable's pipeline for convenience:
Scene::Drawable::Pipeline const &pipeline = drawable.pipeline;
//skip any drawables without a shader program set:
if (pipeline.program == 0) continue;
//skip any drawables that don't reference any vertex array:
if (pipeline.vao == 0) continue;
//skip any drawables that don't contain any vertices:
if (pipeline.count == 0) continue;
//Set shader program:
glUseProgram(pipeline.program);
//Set attribute sources:
glBindVertexArray(pipeline.vao);
//Configure program uniforms:
//the object-to-world matrix is used in all three of these uniforms:
assert(drawable.transform); //drawables *must* have a transform
glm::mat4x3 object_to_world = drawable.transform->make_local_to_world();
//OBJECT_TO_CLIP takes vertices from object space to clip space:
if (pipeline.OBJECT_TO_CLIP_mat4 != -1U) {
glm::mat4 object_to_clip = world_to_clip * glm::mat4(object_to_world);
glUniformMatrix4fv(pipeline.OBJECT_TO_CLIP_mat4, 1, GL_FALSE, glm::value_ptr(object_to_clip));
}
//the object-to-light matrix is used in the next two uniforms:
glm::mat4x3 object_to_light = world_to_light * glm::mat4(object_to_world);
//OBJECT_TO_CLIP takes vertices from object space to light space:
if (pipeline.OBJECT_TO_LIGHT_mat4x3 != -1U) {
glUniformMatrix4x3fv(pipeline.OBJECT_TO_LIGHT_mat4x3, 1, GL_FALSE, glm::value_ptr(object_to_light));
}
//NORMAL_TO_CLIP takes normals from object space to light space:
if (pipeline.NORMAL_TO_LIGHT_mat3 != -1U) {
glm::mat3 normal_to_light = glm::inverse(glm::transpose(glm::mat3(object_to_light)));
glUniformMatrix3fv(pipeline.NORMAL_TO_LIGHT_mat3, 1, GL_FALSE, glm::value_ptr(normal_to_light));
}
//set any requested custom uniforms:
if (pipeline.set_uniforms) pipeline.set_uniforms();
//set up textures:
for (uint32_t i = 0; i < Drawable::Pipeline::TextureCount; ++i) {
if (pipeline.textures[i].texture != 0) {
glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(pipeline.textures[i].target, pipeline.textures[i].texture);
}
}
//draw the object:
glDrawArrays(pipeline.type, pipeline.start, pipeline.count);
//un-bind textures:
for (uint32_t i = 0; i < Drawable::Pipeline::TextureCount; ++i) {
if (pipeline.textures[i].texture != 0) {
glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(pipeline.textures[i].target, 0);
}
}
glActiveTexture(GL_TEXTURE0);
}
glUseProgram(0);
glBindVertexArray(0);
GL_ERRORS();
}
void Scene::load(std::string const &filename,
std::function< void(Scene &, Transform *, std::string const &) > const &on_drawable) {
std::ifstream file(filename, std::ios::binary);
std::vector< char > names;
read_chunk(file, "str0", &names);
struct HierarchyEntry {
uint32_t parent;
uint32_t name_begin;
uint32_t name_end;
glm::vec3 position;
glm::quat rotation;
glm::vec3 scale;
};
static_assert(sizeof(HierarchyEntry) == 4 + 4 + 4 + 4*3 + 4*4 + 4*3, "HierarchyEntry is packed.");
std::vector< HierarchyEntry > hierarchy;
read_chunk(file, "xfh0", &hierarchy);
struct MeshEntry {
uint32_t transform;
uint32_t name_begin;
uint32_t name_end;
};
static_assert(sizeof(MeshEntry) == 4 + 4 + 4, "MeshEntry is packed.");
std::vector< MeshEntry > meshes;
read_chunk(file, "msh0", &meshes);
struct CameraEntry {
uint32_t transform;
char type[4]; //"pers" or "orth"
float data; //fov in degrees for 'pers', scale for 'orth'
float clip_near, clip_far;
};
static_assert(sizeof(CameraEntry) == 4 + 4 + 4 + 4 + 4, "CameraEntry is packed.");
std::vector< CameraEntry > loaded_cameras;
read_chunk(file, "cam0", &loaded_cameras);
struct LightEntry {
uint32_t transform;
char type;
glm::u8vec3 color;
float energy;
float distance;
float fov;
};
static_assert(sizeof(LightEntry) == 4 + 1 + 3 + 4 + 4 + 4, "LightEntry is packed.");
std::vector< LightEntry > loaded_lights;
read_chunk(file, "lmp0", &loaded_lights);
//--------------------------------
//Now that file is loaded, create transforms for hierarchy entries:
std::vector< Transform * > hierarchy_transforms;
hierarchy_transforms.reserve(hierarchy.size());
for (auto const &h : hierarchy) {
transforms.emplace_back();
Transform *t = &transforms.back();
if (h.parent != -1U) {
if (h.parent >= hierarchy_transforms.size()) {
throw std::runtime_error("scene file '" + filename + "' did not contain transforms in topological-sort order.");
}
t->parent = hierarchy_transforms[h.parent];
}
if (h.name_begin <= h.name_end && h.name_end <= names.size()) {
t->name = std::string(names.begin() + h.name_begin, names.begin() + h.name_end);
} else {
throw std::runtime_error("scene file '" + filename + "' contains hierarchy entry with invalid name indices");
}
t->position = h.position;
t->rotation = h.rotation;
t->scale = h.scale;
hierarchy_transforms.emplace_back(t);
}
assert(hierarchy_transforms.size() == hierarchy.size());
for (auto const &m : meshes) {
if (m.transform >= hierarchy_transforms.size()) {
throw std::runtime_error("scene file '" + filename + "' contains mesh entry with invalid transform index (" + std::to_string(m.transform) + ")");
}
if (!(m.name_begin <= m.name_end && m.name_end <= names.size())) {
throw std::runtime_error("scene file '" + filename + "' contains mesh entry with invalid name indices");
}
std::string name = std::string(names.begin() + m.name_begin, names.begin() + m.name_end);
if (on_drawable) {
on_drawable(*this, hierarchy_transforms[m.transform], name);
}
}
for (auto const &c : loaded_cameras) {
if (c.transform >= hierarchy_transforms.size()) {
throw std::runtime_error("scene file '" + filename + "' contains camera entry with invalid transform index (" + std::to_string(c.transform) + ")");
}
if (std::string(c.type, 4) != "pers") {
std::cout << "Ignoring non-perspective camera (" + std::string(c.type, 4) + ") stored in file." << std::endl;
continue;
}
cameras.emplace_back(hierarchy_transforms[c.transform]);
Camera *camera = &cameras.back();
camera->fovy = c.data / 180.0f * 3.1415926f; //FOV is stored in degrees; convert to radians.
camera->near = c.clip_near;
//N.b. far plane is ignored because cameras use infinite perspective matrices.
}
for (auto const &l : loaded_lights) {
if (l.transform >= hierarchy_transforms.size()) {
throw std::runtime_error("scene file '" + filename + "' contains lamp entry with invalid transform index (" + std::to_string(l.transform) + ")");
}
if (l.type == 'p') {
//good
} else if (l.type == 'h') {
//fine
} else if (l.type == 's') {
//okay
} else if (l.type == 'd') {
//sure
} else {
std::cout << "Ignoring unrecognized lamp type (" + std::string(&l.type, 1) + ") stored in file." << std::endl;
continue;
}
lights.emplace_back(hierarchy_transforms[l.transform]);
Light *light = &lights.back();
light->type = static_cast<Light::Type>(l.type);
light->energy = glm::vec3(l.color) / 255.0f * l.energy;
light->spot_fov = l.fov / 180.0f * 3.1415926f; //FOV is stored in degrees; convert to radians.
}
//load any extra that a subclass wants:
load_extra(file, names, hierarchy_transforms);
if (file.peek() != EOF) {
std::cerr << "WARNING: trailing data in scene file '" << filename << "'" << std::endl;
}
}
//-------------------------
Scene::Scene(std::string const &filename, std::function< void(Scene &, Transform *, std::string const &) > const &on_drawable) {
load(filename, on_drawable);
}
Scene::Scene(Scene const &other) {
set(other);
}
Scene &Scene::operator=(Scene const &other) {
set(other);
return *this;
}
void Scene::set(Scene const &other, std::unordered_map< Transform const *, Transform * > *transform_map_) {
std::unordered_map< Transform const *, Transform * > t2t_temp;
std::unordered_map< Transform const *, Transform * > &transform_to_transform = *(transform_map_ ? transform_map_ : &t2t_temp);
transform_to_transform.clear();
//null transform maps to itself:
transform_to_transform.insert(std::make_pair(nullptr, nullptr));
//Copy transforms and store mapping:
transforms.clear();
for (auto const &t : other.transforms) {
transforms.emplace_back();
transforms.back().name = t.name;
transforms.back().position = t.position;
transforms.back().rotation = t.rotation;
transforms.back().scale = t.scale;
transforms.back().parent = t.parent; //will update later
//store mapping between transforms old and new:
auto ret = transform_to_transform.insert(std::make_pair(&t, &transforms.back()));
assert(ret.second);
}
//update transform parents:
for (auto &t : transforms) {
t.parent = transform_to_transform.at(t.parent);
}
//copy other's drawables, updating transform pointers:
drawables = other.drawables;
for (auto &d : drawables) {
d.transform = transform_to_transform.at(d.transform);
}
//copy other's cameras, updating transform pointers:
cameras = other.cameras;
for (auto &c : cameras) {
c.transform = transform_to_transform.at(c.transform);
}
//copy other's lights, updating transform pointers:
lights = other.lights;
for (auto &l : lights) {
l.transform = transform_to_transform.at(l.transform);
}
}