scene.cpp 46.6 KB
Newer Older
TheNumbat's avatar
TheNumbat committed
1
2

#include <assimp/Exporter.hpp>
TheNumbat's avatar
TheNumbat committed
3
#include <assimp/Importer.hpp>
TheNumbat's avatar
TheNumbat committed
4
5
#include <assimp/postprocess.h>
#include <assimp/scene.h>
TheNumbat's avatar
TheNumbat committed
6
#include <sstream>
TheNumbat's avatar
TheNumbat committed
7
8
9

#include "../gui/manager.h"
#include "../gui/render.h"
TheNumbat's avatar
TheNumbat committed
10
#include "../lib/log.h"
TheNumbat's avatar
TheNumbat committed
11
12

#include "renderer.h"
TheNumbat's avatar
TheNumbat committed
13
#include "scene.h"
TheNumbat's avatar
TheNumbat committed
14
15
16
#include "undo.h"

namespace std {
TheNumbat's avatar
TheNumbat committed
17
18
19
20
21
22
template <typename T1, typename T2> struct hash<pair<T1, T2>> {
    uint64_t operator()(const pair<T1, T2> &p) const {
        static const hash<T1> h1;
        static const hash<T2> h2;
        return h1(p.first) ^ h2(p.second);
    }
TheNumbat's avatar
TheNumbat committed
23
};
TheNumbat's avatar
TheNumbat committed
24
}; // namespace std
TheNumbat's avatar
TheNumbat committed
25

TheNumbat's avatar
TheNumbat committed
26
Scene_Item::Scene_Item(Scene_Object &&obj) : data(std::move(obj)) {}
TheNumbat's avatar
TheNumbat committed
27

TheNumbat's avatar
TheNumbat committed
28
Scene_Item::Scene_Item(Scene_Light &&light) : data(std::move(light)) {}
TheNumbat's avatar
TheNumbat committed
29

TheNumbat's avatar
TheNumbat committed
30
Scene_Item::Scene_Item(Scene_Item &&src) : data(std::move(src.data)) {}
TheNumbat's avatar
TheNumbat committed
31

TheNumbat's avatar
TheNumbat committed
32
33
34
Scene_Item &Scene_Item::operator=(Scene_Item &&src) {
    data = std::move(src.data);
    return *this;
TheNumbat's avatar
TheNumbat committed
35
36
37
}

void Scene_Item::set_time(float time) {
TheNumbat's avatar
TheNumbat committed
38
39
40
    return std::visit(overloaded{[time](Scene_Object &obj) { obj.set_time(time); },
                                 [time](Scene_Light &light) { light.set_time(time); }},
                      data);
TheNumbat's avatar
TheNumbat committed
41
42
43
}

BBox Scene_Item::bbox() {
TheNumbat's avatar
TheNumbat committed
44
45
46
    return std::visit(overloaded{[](Scene_Object &obj) { return obj.bbox(); },
                                 [](Scene_Light &light) { return light.bbox(); }},
                      data);
TheNumbat's avatar
TheNumbat committed
47
48
}

TheNumbat's avatar
TheNumbat committed
49
50
51
52
void Scene_Item::render(const Mat4 &view, bool solid, bool depth_only, bool posed) {
    std::visit(overloaded{[&](Scene_Object &obj) { obj.render(view, solid, depth_only, posed); },
                          [&](Scene_Light &light) { light.render(view, depth_only, posed); }},
               data);
TheNumbat's avatar
TheNumbat committed
53
54
55
}

Scene_ID Scene_Item::id() const {
TheNumbat's avatar
TheNumbat committed
56
57
58
    return std::visit(overloaded{[](const Scene_Object &obj) { return obj.id(); },
                                 [](const Scene_Light &light) { return light.id(); }},
                      data);
TheNumbat's avatar
TheNumbat committed
59
60
}

TheNumbat's avatar
TheNumbat committed
61
Anim_Pose &Scene_Item::animation() {
TheNumbat's avatar
TheNumbat committed
62

TheNumbat's avatar
TheNumbat committed
63
64
65
66
67
68
    Scene_Object *o = std::get_if<Scene_Object>(&data);
    if (o)
        return o->anim;
    Scene_Light *l = std::get_if<Scene_Light>(&data);
    if (l)
        return l->anim;
TheNumbat's avatar
TheNumbat committed
69

TheNumbat's avatar
TheNumbat committed
70
71
    assert(false);
    return l->anim;
TheNumbat's avatar
TheNumbat committed
72
73
}

TheNumbat's avatar
TheNumbat committed
74
const Anim_Pose &Scene_Item::animation() const {
TheNumbat's avatar
TheNumbat committed
75

TheNumbat's avatar
TheNumbat committed
76
77
78
79
80
81
    const Scene_Object *o = std::get_if<Scene_Object>(&data);
    if (o)
        return o->anim;
    const Scene_Light *l = std::get_if<Scene_Light>(&data);
    if (l)
        return l->anim;
TheNumbat's avatar
TheNumbat committed
82

TheNumbat's avatar
TheNumbat committed
83
84
    assert(false);
    return l->anim;
TheNumbat's avatar
TheNumbat committed
85
86
}

TheNumbat's avatar
TheNumbat committed
87
Pose &Scene_Item::pose() {
TheNumbat's avatar
TheNumbat committed
88

TheNumbat's avatar
TheNumbat committed
89
90
91
92
93
94
    Scene_Object *o = std::get_if<Scene_Object>(&data);
    if (o)
        return o->pose;
    Scene_Light *l = std::get_if<Scene_Light>(&data);
    if (l)
        return l->pose;
TheNumbat's avatar
TheNumbat committed
95

TheNumbat's avatar
TheNumbat committed
96
97
    assert(false);
    return l->pose;
TheNumbat's avatar
TheNumbat committed
98
99
}

TheNumbat's avatar
TheNumbat committed
100
const Pose &Scene_Item::pose() const {
TheNumbat's avatar
TheNumbat committed
101

TheNumbat's avatar
TheNumbat committed
102
103
104
105
106
107
    const Scene_Object *o = std::get_if<Scene_Object>(&data);
    if (o)
        return o->pose;
    const Scene_Light *l = std::get_if<Scene_Light>(&data);
    if (l)
        return l->pose;
TheNumbat's avatar
TheNumbat committed
108

TheNumbat's avatar
TheNumbat committed
109
110
    assert(false);
    return l->pose;
TheNumbat's avatar
TheNumbat committed
111
112
}

TheNumbat's avatar
TheNumbat committed
113
114
115
116
117
118
119
120
std::pair<char *, int> Scene_Item::name() {

    Scene_Object *o = std::get_if<Scene_Object>(&data);
    if (o)
        return {o->opt.name, Scene_Object::max_name_len};
    Scene_Light *l = std::get_if<Scene_Light>(&data);
    if (l)
        return {l->opt.name, Scene_Light::max_name_len};
TheNumbat's avatar
TheNumbat committed
121

TheNumbat's avatar
TheNumbat committed
122
123
    assert(false);
    return {l->opt.name, Scene_Object::max_name_len};
TheNumbat's avatar
TheNumbat committed
124
125
126
127
}

std::string Scene_Item::name() const {

TheNumbat's avatar
TheNumbat committed
128
129
130
131
132
133
    const Scene_Object *o = std::get_if<Scene_Object>(&data);
    if (o)
        return std::string(o->opt.name);
    const Scene_Light *l = std::get_if<Scene_Light>(&data);
    if (l)
        return std::string(l->opt.name);
TheNumbat's avatar
TheNumbat committed
134

TheNumbat's avatar
TheNumbat committed
135
136
    assert(false);
    return std::string(l->opt.name);
TheNumbat's avatar
TheNumbat committed
137
138
}

TheNumbat's avatar
TheNumbat committed
139
Scene::Scene(Scene_ID start) : next_id(start), first_id(start) {}
TheNumbat's avatar
TheNumbat committed
140

TheNumbat's avatar
TheNumbat committed
141
Scene_ID Scene::used_ids() { return next_id; }
TheNumbat's avatar
TheNumbat committed
142

TheNumbat's avatar
TheNumbat committed
143
Scene_ID Scene::reserve_id() { return next_id++; }
TheNumbat's avatar
TheNumbat committed
144

TheNumbat's avatar
TheNumbat committed
145
146
147
148
149
150
Scene_ID Scene::add(Pose pose, Halfedge_Mesh &&mesh, std::string n, Scene_ID id) {
    if (!id)
        id = next_id++;
    assert(objs.find(id) == objs.end());
    objs.emplace(std::make_pair(id, Scene_Object(id, pose, std::move(mesh), n)));
    return id;
TheNumbat's avatar
TheNumbat committed
151
152
}

TheNumbat's avatar
TheNumbat committed
153
154
155
156
157
158
Scene_ID Scene::add(Pose pose, GL::Mesh &&mesh, std::string n, Scene_ID id) {
    if (!id)
        id = next_id++;
    assert(objs.find(id) == objs.end());
    objs.emplace(std::make_pair(id, Scene_Object(id, pose, std::move(mesh), n)));
    return id;
TheNumbat's avatar
TheNumbat committed
159
160
161
}

std::string Scene::set_env_map(std::string file) {
TheNumbat's avatar
TheNumbat committed
162
163
164
165
166
167
168
169
170
171
172
173
174
    Scene_ID id = 0;
    for_items([&id](const Scene_Item &item) {
        if (item.is<Scene_Light>() && item.get<Scene_Light>().is_env())
            id = item.id();
    });
    Scene_Light l(Light_Type::sphere, reserve_id(), {}, "env_map");
    std::string err = l.emissive_load(file);
    if (err.empty()) {
        if (id)
            erase(id);
        add(std::move(l));
    }
    return err;
TheNumbat's avatar
TheNumbat committed
175
176
177
}

bool Scene::has_env_light() const {
TheNumbat's avatar
TheNumbat committed
178
179
180
181
182
    bool ret = false;
    for_items([&ret](const Scene_Item &item) {
        ret = ret || (item.is<Scene_Light>() && item.get<Scene_Light>().is_env());
    });
    return ret;
TheNumbat's avatar
TheNumbat committed
183
184
}

TheNumbat's avatar
TheNumbat committed
185
186
187
188
Scene_ID Scene::add(Scene_Light &&obj) {
    assert(objs.find(obj.id()) == objs.end());
    objs.emplace(std::make_pair(obj.id(), std::move(obj)));
    return obj.id();
TheNumbat's avatar
TheNumbat committed
189
190
}

TheNumbat's avatar
TheNumbat committed
191
192
193
194
Scene_ID Scene::add(Scene_Object &&obj) {
    assert(objs.find(obj.id()) == objs.end());
    objs.emplace(std::make_pair(obj.id(), std::move(obj)));
    return obj.id();
TheNumbat's avatar
TheNumbat committed
195
196
197
}

void Scene::restore(Scene_ID id) {
TheNumbat's avatar
TheNumbat committed
198
199
200
201
202
    if (objs.find(id) != objs.end())
        return;
    assert(erased.find(id) != erased.end());
    objs.insert({id, std::move(erased[id])});
    erased.erase(id);
TheNumbat's avatar
TheNumbat committed
203
204
205
}

void Scene::erase(Scene_ID id) {
TheNumbat's avatar
TheNumbat committed
206
207
    assert(erased.find(id) == erased.end());
    assert(objs.find(id) != objs.end());
TheNumbat's avatar
TheNumbat committed
208

TheNumbat's avatar
TheNumbat committed
209
210
    erased.insert({id, std::move(objs[id])});
    objs.erase(id);
TheNumbat's avatar
TheNumbat committed
211
212
}

TheNumbat's avatar
TheNumbat committed
213
214
215
216
void Scene::for_items(std::function<void(const Scene_Item &)> func) const {
    for (const auto &obj : objs) {
        func(obj.second);
    }
TheNumbat's avatar
TheNumbat committed
217
218
}

TheNumbat's avatar
TheNumbat committed
219
220
221
222
void Scene::for_items(std::function<void(Scene_Item &)> func) {
    for (auto &obj : objs) {
        func(obj.second);
    }
TheNumbat's avatar
TheNumbat committed
223
224
}

TheNumbat's avatar
TheNumbat committed
225
size_t Scene::size() { return objs.size(); }
TheNumbat's avatar
TheNumbat committed
226

TheNumbat's avatar
TheNumbat committed
227
bool Scene::empty() { return objs.size() == 0; }
TheNumbat's avatar
TheNumbat committed
228
229

Scene_Maybe Scene::get(Scene_ID id) {
TheNumbat's avatar
TheNumbat committed
230
231
232
233
    auto entry = objs.find(id);
    if (entry == objs.end())
        return std::nullopt;
    return entry->second;
TheNumbat's avatar
TheNumbat committed
234
235
}

TheNumbat's avatar
TheNumbat committed
236
237
238
239
240
Scene_Light &Scene::get_light(Scene_ID id) {
    auto entry = objs.find(id);
    assert(entry != objs.end());
    assert(entry->second.is<Scene_Light>());
    return entry->second.get<Scene_Light>();
TheNumbat's avatar
TheNumbat committed
241
242
}

TheNumbat's avatar
TheNumbat committed
243
244
245
246
247
Scene_Object &Scene::get_obj(Scene_ID id) {
    auto entry = objs.find(id);
    assert(entry != objs.end());
    assert(entry->second.is<Scene_Object>());
    return entry->second.get<Scene_Object>();
TheNumbat's avatar
TheNumbat committed
248
249
}

TheNumbat's avatar
TheNumbat committed
250
251
252
253
254
void Scene::clear(Undo &undo) {
    next_id = first_id;
    objs.clear();
    erased.clear();
    undo.reset();
TheNumbat's avatar
TheNumbat committed
255
256
257
258
}

static const std::string FAKE_NAME = "FAKE-S3D-FAKE-MESH";

TheNumbat's avatar
TheNumbat committed
259
static aiVector3D vecVec(Vec3 v) { return aiVector3D(v.x, v.y, v.z); }
TheNumbat's avatar
TheNumbat committed
260

TheNumbat's avatar
TheNumbat committed
261
static Quat aiQuat(aiQuaternion aiv) { return Quat(aiv.x, aiv.y, aiv.z, aiv.w); }
TheNumbat's avatar
TheNumbat committed
262

TheNumbat's avatar
TheNumbat committed
263
static Vec3 aiVec(aiVector3D aiv) { return Vec3(aiv.x, aiv.y, aiv.z); }
TheNumbat's avatar
TheNumbat committed
264

TheNumbat's avatar
TheNumbat committed
265
static Spectrum aiSpec(aiColor3D aiv) { return Spectrum(aiv.r, aiv.g, aiv.b); }
TheNumbat's avatar
TheNumbat committed
266

TheNumbat's avatar
TheNumbat committed
267
268
269
static aiMatrix4x4 matMat(const Mat4 &T) {
    return {T[0][0], T[1][0], T[2][0], T[3][0], T[0][1], T[1][1], T[2][1], T[3][1],
            T[0][2], T[1][2], T[2][2], T[3][2], T[0][3], T[1][3], T[2][3], T[3][3]};
TheNumbat's avatar
TheNumbat committed
270
271
272
}

static Mat4 aiMat(aiMatrix4x4 T) {
TheNumbat's avatar
TheNumbat committed
273
274
    return Mat4{Vec4{T[0][0], T[1][0], T[2][0], T[3][0]}, Vec4{T[0][1], T[1][1], T[2][1], T[3][1]},
                Vec4{T[0][2], T[1][2], T[2][2], T[3][2]}, Vec4{T[0][3], T[1][3], T[2][3], T[3][3]}};
TheNumbat's avatar
TheNumbat committed
275
276
}

TheNumbat's avatar
TheNumbat committed
277
278
279
280
281
282
283
static aiMatrix4x4 node_transform(const aiNode *node) {
    aiMatrix4x4 T;
    while (node) {
        T = T * node->mTransformation;
        node = node->mParent;
    }
    return T;
TheNumbat's avatar
TheNumbat committed
284
285
}

TheNumbat's avatar
TheNumbat committed
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
static void load_node(Scene &scobj, std::vector<std::string> &errors,
                      std::unordered_map<aiNode *, Scene_ID> &node_to_obj,
                      std::unordered_map<aiNode *, Joint *> &node_to_bone, const aiScene *scene,
                      aiNode *node, aiMatrix4x4 transform) {

    transform = transform * node->mTransformation;

    for (unsigned int i = 0; i < node->mNumMeshes; i++) {

        const aiMesh *mesh = scene->mMeshes[node->mMeshes[i]];

        std::string name;
        bool do_flip = false, do_smooth = false;

        if (mesh->mName.length) {
            name = std::string(mesh->mName.C_Str());

            if (name.find(FAKE_NAME) != std::string::npos)
                continue;

            size_t special = name.find("-S3D-");
            if (special != std::string::npos) {
                if (name.find("FLIPPED") != std::string::npos)
                    do_flip = true;
                if (name.find("SMOOTHED") != std::string::npos)
                    do_smooth = true;
                name = name.substr(0, special);
                std::replace(name.begin(), name.end(), '_', ' ');
            }
        }

        std::vector<Vec3> verts;

        for (unsigned int j = 0; j < mesh->mNumVertices; j++) {
            const aiVector3D &pos = mesh->mVertices[j];
            verts.push_back(Vec3(pos.x, pos.y, pos.z));
        }

        std::vector<std::vector<Halfedge_Mesh::Index>> polys;
        for (unsigned int j = 0; j < mesh->mNumFaces; j++) {
            const aiFace &face = mesh->mFaces[j];
            std::vector<Halfedge_Mesh::Index> poly;
            for (unsigned int k = 0; k < face.mNumIndices; k++) {
                poly.push_back(face.mIndices[k]);
            }
            polys.push_back(poly);
        }

        aiVector3D ascale, arot, apos;
        transform.Decompose(ascale, arot, apos);
        Vec3 pos = aiVec(apos);
        Vec3 rot = aiVec(arot);
        Vec3 scale = aiVec(ascale);
        Pose p = {pos, Degrees(rot).range(0.0f, 360.0f), scale};

        Material::Options material;
        const aiMaterial &ai_mat = *scene->mMaterials[mesh->mMaterialIndex];

        aiColor3D albedo;
        ai_mat.Get(AI_MATKEY_COLOR_DIFFUSE, albedo);
        material.albedo = aiSpec(albedo);

        aiColor3D emissive;
        ai_mat.Get(AI_MATKEY_COLOR_EMISSIVE, emissive);
        material.emissive = aiSpec(emissive);

        aiColor3D reflectance;
        ai_mat.Get(AI_MATKEY_COLOR_REFLECTIVE, reflectance);
        material.reflectance = aiSpec(reflectance);

        aiColor3D transmittance;
        ai_mat.Get(AI_MATKEY_COLOR_TRANSPARENT, transmittance);
        material.transmittance = aiSpec(transmittance);

        ai_mat.Get(AI_MATKEY_REFRACTI, material.ior);
        ai_mat.Get(AI_MATKEY_SHININESS, material.intensity);

        aiString ai_type;
        ai_mat.Get(AI_MATKEY_NAME, ai_type);
        std::string type(ai_type.C_Str());

        if (type.find("lambertian") != std::string::npos) {
            material.type = Material_Type::lambertian;
        } else if (type.find("mirror") != std::string::npos) {
            material.type = Material_Type::mirror;
        } else if (type.find("refract") != std::string::npos) {
            material.type = Material_Type::refract;
        } else if (type.find("glass") != std::string::npos) {
            material.type = Material_Type::glass;
        } else if (type.find("diffuse_light") != std::string::npos) {
            material.type = Material_Type::diffuse_light;
        } else {
            material = Material::Options();
        }
        material.emissive *= 1.0f / material.intensity;

        Scene_Object new_obj;

        // Horrible hack
        if (type.find("SPHERESHAPE") != std::string::npos) {

            aiColor3D specular;
            ai_mat.Get(AI_MATKEY_COLOR_SPECULAR, specular);
            Scene_Object obj(scobj.reserve_id(), p, GL::Mesh());
            obj.material.opt = material;
            obj.opt.shape_type = PT::Shape_Type::sphere;
            obj.opt.shape = PT::Shape(PT::Sphere(specular.r));
            new_obj = std::move(obj);

        } else {

            Halfedge_Mesh hemesh;
            std::string err = hemesh.from_poly(polys, verts);
            if (!err.empty()) {

                std::vector<GL::Mesh::Vert> mesh_verts;
                std::vector<GL::Mesh::Index> mesh_inds;

                for (unsigned int j = 0; j < mesh->mNumVertices; j++) {
                    const aiVector3D &vpos = mesh->mVertices[j];
TheNumbat's avatar
TheNumbat committed
406
407
408
409
                    aiVector3D vnorm;
                    if(mesh->HasNormals()) {
                        vnorm = mesh->mNormals[j];
                    }
TheNumbat's avatar
TheNumbat committed
410
411
412
413
414
                    mesh_verts.push_back({aiVec(vpos), aiVec(vnorm), 0});
                }

                for (unsigned int j = 0; j < mesh->mNumFaces; j++) {
                    const aiFace &face = mesh->mFaces[j];
TheNumbat's avatar
TheNumbat committed
415
                    if(face.mNumIndices < 3) continue;
TheNumbat's avatar
TheNumbat committed
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
                    unsigned int start = face.mIndices[0];
                    for (size_t k = 1; k <= face.mNumIndices - 2; k++) {
                        mesh_inds.push_back(start);
                        mesh_inds.push_back(face.mIndices[k]);
                        mesh_inds.push_back(face.mIndices[k + 1]);
                    }
                }

                errors.push_back(err);
                Scene_Object obj(scobj.reserve_id(), p,
                                 GL::Mesh(std::move(mesh_verts), std::move(mesh_inds)), name);
                obj.material.opt = material;
                new_obj = std::move(obj);

            } else {

                if (do_flip)
                    hemesh.flip();
                Scene_Object obj(scobj.reserve_id(), p, std::move(hemesh), name);
                obj.material.opt = material;
                obj.opt.smooth_normals = do_smooth;
                new_obj = std::move(obj);
            }
        }

        if (mesh->mNumBones) {

            Skeleton &skeleton = new_obj.armature;
            aiNode *arm_node = mesh->mBones[0]->mArmature;
            {
                aiVector3D t, r, s;
                arm_node->mTransformation.Decompose(s, r, t);
                skeleton.base() = aiVec(t);
            }

            std::unordered_map<aiNode *, aiBone *> node_to_aibone;
            for (unsigned int j = 0; j < mesh->mNumBones; j++) {
                node_to_aibone[mesh->mBones[j]->mNode] = mesh->mBones[j];
            }

            std::function<void(Joint *, aiNode *)> build_tree;
            build_tree = [&](Joint *p, aiNode *node) {
                aiBone *bone = node_to_aibone[node];
                aiVector3D t, r, s;
                bone->mOffsetMatrix.Decompose(s, r, t);
                Joint *c = skeleton.add_child(p, aiVec(t));
                node_to_bone[node] = c;
                c->pose = aiVec(r);
                c->radius = bone->mWeights[0].mWeight;
                for (unsigned int j = 0; j < node->mNumChildren; j++)
                    build_tree(c, node->mChildren[j]);
            };
            for (unsigned int j = 0; j < arm_node->mNumChildren; j++) {
                aiNode *root_node = arm_node->mChildren[j];
                aiBone *root_bone = node_to_aibone[root_node];
                aiVector3D t, r, s;
                root_bone->mOffsetMatrix.Decompose(s, r, t);
                Joint *root = skeleton.add_root(aiVec(t));
                node_to_bone[root_node] = root;
                root->pose = aiVec(r);
                root->radius = root_bone->mWeights[0].mWeight;
                for (unsigned int k = 0; k < root_node->mNumChildren; k++)
                    build_tree(root, root_node->mChildren[k]);
            }
        }

        node_to_obj[node] = new_obj.id();
        scobj.add(std::move(new_obj));
    }

    for (unsigned int i = 0; i < node->mNumChildren; i++) {
        load_node(scobj, errors, node_to_obj, node_to_bone, scene, node->mChildren[i], transform);
    }
TheNumbat's avatar
TheNumbat committed
489
490
}

TheNumbat's avatar
TheNumbat committed
491
492
493
494
495
496
497
498
499
500
std::string Scene::load(bool new_scene, Undo &undo, Gui::Manager &gui, std::string file) {

    if (new_scene) {
        clear(undo);
        gui.get_animate().clear();
        gui.get_rig().clear();
    }

    Assimp::Importer importer;
    const aiScene *scene = importer.ReadFile(
TheNumbat's avatar
TheNumbat committed
501
502
        file.c_str(), aiProcess_PopulateArmatureData | aiProcess_OptimizeMeshes |
                      aiProcess_ValidateDataStructure | aiProcess_FindInvalidData |
TheNumbat's avatar
TheNumbat committed
503
                      aiProcess_FindInstances | aiProcess_FindDegenerates |
TheNumbat's avatar
TheNumbat committed
504
                      aiProcess_DropNormals | aiProcess_JoinIdenticalVertices);
TheNumbat's avatar
TheNumbat committed
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729

    if (!scene) {
        return "Parsing scene " + file + ": " + std::string(importer.GetErrorString());
    }

    std::vector<std::string> errors;
    std::unordered_map<aiNode *, Scene_ID> node_to_obj;
    std::unordered_map<aiNode *, Joint *> node_to_bone;
    scene->mRootNode->mTransformation = aiMatrix4x4();

    // Load objects
    load_node(*this, errors, node_to_obj, node_to_bone, scene, scene->mRootNode, aiMatrix4x4());

    // Load camera
    if (new_scene && scene->mNumCameras) {
        const aiCamera &aiCam = *scene->mCameras[0];
        Mat4 cam_transform = aiMat(node_transform(scene->mRootNode->FindNode(aiCam.mName)));
        Vec3 pos = cam_transform * aiVec(aiCam.mPosition);
        Vec3 center = cam_transform * aiVec(aiCam.mLookAt);
        gui.get_render().load_cam(pos, center, aiCam.mAspect, aiCam.mHorizontalFOV);
    }

    // Load lights
    for (unsigned int i = 0; i < scene->mNumLights; i++) {

        const aiLight &ailight = *scene->mLights[i];
        const aiNode *node = scene->mRootNode->FindNode(ailight.mName);

        Mat4 trans = aiMat(node_transform(node));
        Vec3 pos = trans * aiVec(ailight.mPosition);
        Vec3 dir = trans.rotate(Vec3{0.0f, 1.0f, 0.0f});

        Pose p;
        p.pos = pos;
        p.euler = Mat4::rotate_to(dir).to_euler();

        bool was_exported_from_s3d = false;
        float export_power = 1.0f;
        bool is_sphere = false, is_hemisphere = false, is_area = false;

        std::string name = std::string(node->mName.C_Str());

        size_t special = name.find("-S3D-");
        if (special != std::string::npos) {

            was_exported_from_s3d = true;
            export_power = ailight.mAttenuationQuadratic;

            std::string left = name.substr(special + 4);
            name = name.substr(0, special);
            std::replace(name.begin(), name.end(), '_', ' ');

            if (left.find("HEMISPHERE") != std::string::npos)
                is_hemisphere = true;
            else if (left.find("SPHERE") != std::string::npos)
                is_sphere = true;
            else if (left.find("AREA") != std::string::npos)
                is_area = true;
        }

        aiColor3D color;
        Scene_Light light(Light_Type::point, reserve_id(), p, name);

        switch (ailight.mType) {
        case aiLightSource_DIRECTIONAL: {
            light.opt.type = Light_Type::directional;
            color = ailight.mColorDiffuse;
        } break;
        case aiLightSource_POINT: {
            light.opt.type = Light_Type::point;
            color = ailight.mColorDiffuse;
        } break;
        case aiLightSource_SPOT: {
            light.opt.type = Light_Type::spot;
            light.opt.angle_bounds.x = ailight.mAngleInnerCone;
            light.opt.angle_bounds.y = ailight.mAngleOuterCone;
            color = ailight.mColorDiffuse;
        } break;
        case aiLightSource_AMBIENT: {
            if (is_hemisphere)
                light.opt.type = Light_Type::hemisphere;
            else if (is_sphere) {
                light.opt.type = Light_Type::sphere;
                if (ailight.mEnvMap.length) {
                    light.opt.has_emissive_map = true;
                    light.emissive_load(std::string(ailight.mEnvMap.C_Str()));
                }
            } else if (is_area) {
                light.opt.type = Light_Type::rectangle;
                light.opt.size.x = ailight.mAttenuationConstant;
                light.opt.size.y = ailight.mAttenuationLinear;
            }
            color = ailight.mColorAmbient;
        } break;
        case aiLightSource_AREA: {
            light.opt.type = Light_Type::rectangle;
            light.opt.size.x = ailight.mSize.x;
            light.opt.size.y = ailight.mSize.y;
            color = ailight.mColorDiffuse;
        } break;
        default:
            continue;
        }

        float power = std::max(color.r, std::max(color.g, color.b));
        light.opt.spectrum = Spectrum(color.r, color.g, color.b) * (1.0f / power);
        light.opt.intensity = power;

        if (was_exported_from_s3d) {
            float factor = power / export_power;
            light.opt.spectrum *= factor;
            light.opt.intensity = export_power;
        }

        if (!light.is_env() || !has_env_light()) {

            node_to_obj[(aiNode *)node] = light.id();

            std::string anim_name = std::string(node->mName.C_Str()) + "-LIGHT_ANIM_NODE";
            aiNode *anim_node = scene->mRootNode->FindNode(aiString(anim_name));
            node_to_obj[anim_node] = light.id();

            add(std::move(light));
        }
    }

    // animations
    for (unsigned int i = 0; i < scene->mNumAnimations; i++) {

        aiAnimation &anim = *scene->mAnimations[i];
        for (unsigned int j = 0; j < anim.mNumChannels; j++) {
            aiNodeAnim &node_anim = *anim.mChannels[j];

            if (node_anim.mNodeName == aiString("camera_node")) {

                Gui::Anim_Camera &cam = gui.get_animate().camera();
                unsigned int keys =
                    std::min(node_anim.mNumPositionKeys,
                             std::min(node_anim.mNumRotationKeys, node_anim.mNumScalingKeys));

                for (unsigned int k = 0; k < keys; k++) {
                    float t = (float)node_anim.mPositionKeys[k].mTime;
                    Vec3 pos = aiVec(node_anim.mPositionKeys[k].mValue);
                    Quat rot = aiQuat(node_anim.mRotationKeys[k].mValue);
                    Vec3 v = aiVec(node_anim.mScalingKeys[k].mValue);
                    cam.splines.set(t, pos, rot, v.x, v.y);
                }

            } else if (std::string(node_anim.mNodeName.C_Str()).find("LIGHT_ANIM_NODE") !=
                       std::string::npos) {

                aiNode *node = scene->mRootNode->FindNode(node_anim.mNodeName);
                auto entry = node_to_obj.find(node);
                if (entry != node_to_obj.end()) {

                    Scene_Light &l = get_light(entry->second);
                    Scene_Light::Anim_Light &light = l.lanim;
                    unsigned int keys =
                        std::min(node_anim.mNumPositionKeys,
                                 std::min(node_anim.mNumRotationKeys, node_anim.mNumScalingKeys));

                    for (unsigned int k = 0; k < keys; k++) {
                        float t = (float)node_anim.mPositionKeys[k].mTime;
                        Vec3 spec = aiVec(node_anim.mPositionKeys[k].mValue);
                        Vec3 angle = aiQuat(node_anim.mRotationKeys[k].mValue).to_euler();
                        Vec3 inten_sz = aiVec(node_anim.mScalingKeys[k].mValue);
                        light.splines.set(t, Spectrum{spec.x, spec.y, spec.z}, inten_sz.x - 1.0f,
                                          Vec2{angle.x, angle.z},
                                          Vec2{inten_sz.y - 1.0f, inten_sz.z - 1.0f});
                    }
                }

            } else {

                aiNode *node = scene->mRootNode->FindNode(node_anim.mNodeName);
                auto jentry = node_to_bone.find(node);
                if (jentry != node_to_bone.end()) {

                    Joint *jt = jentry->second;
                    unsigned int keys = node_anim.mNumRotationKeys;

                    for (unsigned int k = 0; k < keys; k++) {
                        float t = (float)node_anim.mRotationKeys[k].mTime;
                        Quat rot = aiQuat(node_anim.mRotationKeys[k].mValue);
                        jt->anim.set(t, rot);
                    }

                } else {
                    auto entry = node_to_obj.find(node);
                    if (entry != node_to_obj.end()) {

                        Scene_Item &item = *get(entry->second);
                        Anim_Pose &pose = item.animation();
                        unsigned int keys = std::min(
                            node_anim.mNumPositionKeys,
                            std::min(node_anim.mNumRotationKeys, node_anim.mNumScalingKeys));

                        for (unsigned int k = 0; k < keys; k++) {
                            float t = (float)node_anim.mPositionKeys[k].mTime;
                            Vec3 pos = aiVec(node_anim.mPositionKeys[k].mValue);
                            Quat rot = aiQuat(node_anim.mRotationKeys[k].mValue);
                            Vec3 scl = aiVec(node_anim.mScalingKeys[k].mValue);
                            pose.splines.set(t, pos, rot, scl);
                        }
                    }
                }
            }
        }

        if (anim.mDuration > 0.0f) {
            gui.get_animate().set((int)std::ceil(anim.mDuration),
                                  (int)std::round(anim.mTicksPerSecond));
        }
        gui.get_animate().refresh(*this);
    }

    std::stringstream stream;
    if (errors.size()) {
        stream << "Meshes with errors may not be edit-able in the model mode." << std::endl
               << std::endl;
    }
    for (size_t i = 0; i < errors.size(); i++) {
        stream << "Loading mesh " << i << ": " << errors[i] << std::endl;
    }
    return stream.str();
TheNumbat's avatar
TheNumbat committed
730
731
}

TheNumbat's avatar
TheNumbat committed
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
std::string Scene::write(std::string file, const Camera &render_cam,
                         const Gui::Animate &animation) {

    bool no_real_meshes = false;

    size_t n_meshes = 0, n_lights = 0, n_anims = 0;
    size_t n_armatures = 0;
    for_items([&](Scene_Item &item) {
        if (item.is<Scene_Object>()) {
            Scene_Object &obj = item.get<Scene_Object>();
            n_meshes++;
            if (obj.anim.splines.any())
                n_anims++;
            if (obj.armature.has_bones())
                n_armatures++;
            obj.armature.for_joints([&](Joint *j) {
                if (j->anim.any())
                    n_anims++;
            });
        } else if (item.is<Scene_Light>()) {
            if (item.animation().splines.any())
                n_anims++;
            if (item.get<Scene_Light>().lanim.splines.any())
                n_anims++;
            n_lights++;
        }
    });

    size_t camera_anim = n_anims;
    if (animation.camera().splines.any())
        n_anims++;

    if (!n_meshes) {
        no_real_meshes = true;
        n_meshes = 1;
    }

    aiScene scene;
    scene.mRootNode = new aiNode();

    // materials
    scene.mMaterials = new aiMaterial *[n_meshes]();
    scene.mNumMaterials = (unsigned int)n_meshes;

    // camera
    const Gui::Anim_Camera &anim_cam = animation.camera();
    Camera cam = render_cam;
    if (anim_cam.splines.any()) {
        cam = animation.camera().at(0.0f);
    }
    scene.mCameras = new aiCamera *[1]();
    scene.mCameras[0] = new aiCamera();
    scene.mCameras[0]->mAspect = cam.get_ar();
    scene.mCameras[0]->mClipPlaneNear = cam.get_near();
    scene.mCameras[0]->mClipPlaneFar = 100.0f;
    scene.mCameras[0]->mLookAt = aiVector3D(0.0f, 0.0f, -1.0f);
    scene.mCameras[0]->mHorizontalFOV = Radians(cam.get_h_fov());
    scene.mCameras[0]->mName = aiString("camera_node");
    scene.mNumCameras = 1;

    // nodes
    size_t cam_idx = n_meshes + n_armatures + n_lights * 2;
    size_t n_nodes = cam_idx + 1;

    scene.mRootNode->mNumMeshes = 0;
    scene.mRootNode->mNumChildren = (unsigned int)(n_nodes);
    scene.mRootNode->mChildren = new aiNode *[n_nodes]();

    // camera node
    Mat4 view = cam.get_view().inverse();
    scene.mRootNode->mChildren[cam_idx] = new aiNode();
    scene.mRootNode->mChildren[cam_idx]->mNumMeshes = 0;
    scene.mRootNode->mChildren[cam_idx]->mName = aiString("camera_node");
    scene.mRootNode->mChildren[cam_idx]->mTransformation = matMat(view);

    // meshes
    scene.mMeshes = new aiMesh *[n_meshes]();
    scene.mNumMeshes = (unsigned int)n_meshes;

    // lights
    scene.mLights = new aiLight *[n_lights]();
    scene.mNumLights = (unsigned int)n_lights;

    size_t mesh_idx = 0, light_idx = 0, node_idx = 0;

    if (no_real_meshes) {
        aiMesh *ai_mesh = new aiMesh();
        aiNode *ai_node = new aiNode();
        scene.mMeshes[mesh_idx++] = ai_mesh;
        scene.mRootNode->mChildren[node_idx++] = ai_node;
        ai_node->mNumMeshes = 1;
        ai_node->mMeshes = new unsigned int((unsigned int)0);

        ai_mesh->mVertices = new aiVector3D[3];
        ai_mesh->mNormals = new aiVector3D[3];
        ai_mesh->mNumVertices = (unsigned int)3;
        ai_mesh->mVertices[0] = vecVec(Vec3{1.0f, 0.0f, 0.0f});
        ai_mesh->mVertices[1] = vecVec(Vec3{0.0f, 1.0f, 0.0f});
        ai_mesh->mVertices[2] = vecVec(Vec3{0.0f, 0.0f, 1.0f});
        ai_mesh->mNormals[0] = vecVec(Vec3{0.0f, 1.0f, 0.0f});
        ai_mesh->mNormals[1] = vecVec(Vec3{0.0f, 1.0f, 0.0f});
        ai_mesh->mNormals[2] = vecVec(Vec3{0.0f, 1.0f, 0.0f});

        ai_mesh->mFaces = new aiFace[1];
        ai_mesh->mNumFaces = 1u;

        ai_mesh->mFaces[0].mIndices = new unsigned int[3];
        ai_mesh->mFaces[0].mNumIndices = 3;
        ai_mesh->mFaces[0].mIndices[0] = 0;
        ai_mesh->mFaces[0].mIndices[1] = 1;
        ai_mesh->mFaces[0].mIndices[2] = 2;

        ai_mesh->mName = aiString(FAKE_NAME);
        ai_node->mName = aiString(FAKE_NAME);
        scene.mMaterials[0] = new aiMaterial();
    }

    std::unordered_map<Scene_ID, aiNode *> item_nodes;
    std::unordered_map<std::pair<Scene_ID, Scene_ID>, aiNode *> bone_nodes;

    for (auto &entry : objs) {

        if (entry.second.is<Scene_Object>()) {

            Scene_Object &obj = entry.second.get<Scene_Object>();

            if (obj.is_shape()) {
                obj.try_make_editable(obj.opt.shape_type);
            }

            aiMesh *ai_mesh = new aiMesh();
            aiNode *ai_node = new aiNode();
            aiMaterial *ai_mat = new aiMaterial();

            size_t idx = mesh_idx++;
            scene.mMaterials[idx] = ai_mat;
            scene.mMeshes[idx] = ai_mesh;
            scene.mRootNode->mChildren[node_idx++] = ai_node;

            ai_mesh->mMaterialIndex = (unsigned int)idx;
            ai_node->mNumMeshes = 1;
            ai_node->mMeshes = new unsigned int((unsigned int)idx);

            if (obj.is_editable()) {

                Halfedge_Mesh &mesh = obj.get_mesh();
                size_t n_verts = mesh.n_vertices();
                size_t n_faces = mesh.n_faces();

                ai_mesh->mVertices = new aiVector3D[n_verts];
                ai_mesh->mNormals = new aiVector3D[n_verts];
                ai_mesh->mNumVertices = (unsigned int)n_verts;

                ai_mesh->mFaces = new aiFace[n_faces];
                ai_mesh->mNumFaces = (unsigned int)(n_faces);

                std::unordered_map<size_t, size_t> id_to_idx;

                size_t vert_idx = 0;
                for (auto v = mesh.vertices_begin(); v != mesh.vertices_end(); v++) {
                    id_to_idx[v->id()] = vert_idx;
                    Vec3 n = mesh.flipped() ? -v->normal() : v->normal();
                    ai_mesh->mVertices[vert_idx] = vecVec(v->pos);
                    ai_mesh->mNormals[vert_idx] = vecVec(n);
                    vert_idx++;
                }

                size_t face_idx = 0;
                for (auto f = mesh.faces_begin(); f != mesh.faces_end(); f++) {

                    aiFace &face = ai_mesh->mFaces[face_idx];
                    face.mIndices = new unsigned int[f->degree()];
                    face.mNumIndices = f->degree();

                    size_t i_idx = 0;
                    auto h = f->halfedge();
                    do {
                        face.mIndices[i_idx] = (unsigned int)id_to_idx[h->vertex()->id()];
                        h = h->next();
                        i_idx++;
                    } while (h != f->halfedge());

                    face_idx++;
                }

            } else {

                const auto &verts = obj.mesh().verts();
                const auto &elems = obj.mesh().indices();

                ai_mesh->mVertices = new aiVector3D[verts.size()];
                ai_mesh->mNormals = new aiVector3D[verts.size()];
                ai_mesh->mNumVertices = (unsigned int)verts.size();

                int j = 0;
                for (GL::Mesh::Vert v : verts) {
                    ai_mesh->mVertices[j] = vecVec(v.pos);
                    ai_mesh->mNormals[j] = vecVec(v.norm);
                    j++;
                }

                ai_mesh->mFaces = new aiFace[elems.size() / 3];
                ai_mesh->mNumFaces = (unsigned int)(elems.size() / 3);

                for (size_t i = 0; i < elems.size(); i += 3) {
                    aiFace &face = ai_mesh->mFaces[i / 3];
                    face.mIndices = new unsigned int[3];
                    face.mNumIndices = 3;
                    face.mIndices[0] = elems[i];
                    face.mIndices[1] = elems[i + 1];
                    face.mIndices[2] = elems[i + 2];
                }
            }

            std::string name(obj.opt.name);
            std::replace(name.begin(), name.end(), ' ', '_');
            name += "-S3D-" + std::to_string(obj.id());

            if (obj.get_mesh().flipped())
                name += "-FLIPPED";
            if (obj.opt.smooth_normals)
                name += "-SMOOTHED";

            ai_mesh->mName = aiString(name);

            Mat4 trans = obj.pose.transform();

            item_nodes[obj.id()] = ai_node;
            ai_node->mName = aiString(name);
            ai_node->mTransformation = matMat(trans);

            const Material::Options &opt = obj.material.opt;
            std::string mat_name;

            switch (opt.type) {
            case Material_Type::lambertian: {
                mat_name = "lambertian";
            } break;
            case Material_Type::mirror: {
                mat_name = "mirror";
            } break;
            case Material_Type::refract: {
                mat_name = "refract";
            } break;
            case Material_Type::glass: {
                mat_name = "glass";
            } break;
            case Material_Type::diffuse_light: {
                mat_name = "diffuse_light";
            } break;
            default:
                break;
            }

            // Horrible hack
            if (obj.opt.shape_type == PT::Shape_Type::sphere) {
                mat_name += "-SPHERESHAPE";
                ai_mat->AddProperty(new aiColor3D(obj.opt.shape.get<PT::Sphere>().radius), 1,
                                    AI_MATKEY_COLOR_SPECULAR);
            }

            Spectrum emissive = obj.material.emissive();

            // diffuse -> albedo
            // reflective -> reflectance
            // transparent -> transmittance
            // emissive -> emissive
            // refracti -> index of refraction
            // shininess -> intensity
            ai_mat->AddProperty(new aiString(mat_name), AI_MATKEY_NAME);
            ai_mat->AddProperty(new aiColor3D(opt.albedo.r, opt.albedo.g, opt.albedo.b), 1,
                                AI_MATKEY_COLOR_DIFFUSE);
            ai_mat->AddProperty(
                new aiColor3D(opt.reflectance.r, opt.reflectance.g, opt.reflectance.b), 1,
                AI_MATKEY_COLOR_REFLECTIVE);
            ai_mat->AddProperty(
                new aiColor3D(opt.transmittance.r, opt.transmittance.g, opt.transmittance.b), 1,
                AI_MATKEY_COLOR_TRANSPARENT);
            ai_mat->AddProperty(new aiColor3D(emissive.r, emissive.g, emissive.b), 1,
                                AI_MATKEY_COLOR_EMISSIVE);
            ai_mat->AddProperty(new float(opt.ior), 1, AI_MATKEY_REFRACTI);
            ai_mat->AddProperty(new float(opt.intensity), 1, AI_MATKEY_SHININESS);

            if (obj.armature.has_bones()) {
                ai_mesh->mNumBones = obj.armature.n_bones();
                ai_mesh->mBones = new aiBone *[ai_mesh->mNumBones];

                size_t bone_idx = 0;
                std::string prefix = "S3D-joint-" + std::to_string(obj.id()) + "-";

                aiNode *arm_node = new aiNode();
                scene.mRootNode->mChildren[node_idx++] = arm_node;
                arm_node->mName = aiString(prefix + "armature");
                arm_node->mTransformation = matMat(Mat4::translate(obj.armature.base_pos));
                arm_node->mNumChildren = (unsigned int)obj.armature.roots.size();
                arm_node->mChildren = new aiNode *[obj.armature.roots.size()];

                std::unordered_map<Joint *, aiNode *> j_to_node;

                std::function<void(std::string, aiNode *, Joint *)> joint_tree;
                joint_tree = [&joint_tree, &j_to_node](std::string n, aiNode *node, Joint *j) {
                    std::string name = n + std::to_string(j->_id);
                    node->mName = aiString(name);
                    j_to_node[j] = node;
                    if (j->children.size()) {
                        node->mNumChildren = (unsigned int)j->children.size();
                        node->mChildren = new aiNode *[j->children.size()];
                        size_t i = 0;
                        for (Joint *c : j->children) {
                            node->mChildren[i] = new aiNode();
                            joint_tree(n, node->mChildren[i], c);
                            i++;
                        }
                    }
                };

                size_t i = 0;
                for (Joint *j : obj.armature.roots) {
                    aiNode *root_node = new aiNode();
                    arm_node->mChildren[i] = root_node;
                    j_to_node[j] = root_node;
                    joint_tree(prefix, root_node, j);
                    i++;
                }

                for (auto [j, n] : j_to_node) {
                    bone_nodes.insert({{obj.id(), j->_id}, n});
                }

                obj.armature.for_joints([&](Joint *j) {
                    aiBone *bone = new aiBone();
                    ai_mesh->mBones[bone_idx++] = bone;
                    bone->mOffsetMatrix = matMat(Mat4::translate(j->extent) * Mat4::euler(j->pose));
                    bone->mNode = j_to_node[j];
                    std::string name = prefix + std::to_string(j->_id);
                    bone->mName = aiString(name);
                    bone->mNumWeights = 1;
                    bone->mWeights = new aiVertexWeight[1];
                    bone->mWeights[0].mWeight = j->radius;
                });
            }

        } else if (entry.second.is<Scene_Light>()) {

            const Scene_Light &light = entry.second.get<Scene_Light>();

            std::string name(light.opt.name);
            std::replace(name.begin(), name.end(), ' ', '_');

            aiLight *ai_light = new aiLight();
            aiNode *ai_node = new aiNode();
            aiNode *ai_node_light = new aiNode();

            scene.mLights[light_idx++] = ai_light;
            scene.mRootNode->mChildren[node_idx++] = ai_node;
            scene.mRootNode->mChildren[node_idx++] = ai_node_light;

            switch (light.opt.type) {
            case Light_Type::directional: {
                ai_light->mType = aiLightSource_DIRECTIONAL;
                name += "-S3D-" + std::to_string(light.id());
            } break;
            case Light_Type::sphere: {
                ai_light->mType = aiLightSource_AMBIENT;
                name += "-S3D-SPHERE-" + std::to_string(light.id());
                if (light.opt.has_emissive_map) {
                    ai_light->mEnvMap = aiString(light.emissive_loaded());
                }
            } break;
            case Light_Type::hemisphere: {
                ai_light->mType = aiLightSource_AMBIENT;
                name += "-S3D-HEMISPHERE-" + std::to_string(light.id());
            } break;
            case Light_Type::point: {
                ai_light->mType = aiLightSource_POINT;
                name += "-S3D-" + std::to_string(light.id());
            } break;
            case Light_Type::spot: {
                ai_light->mType = aiLightSource_SPOT;
                name += "-S3D-" + std::to_string(light.id());
            } break;
            case Light_Type::rectangle: {
                // the collada exporter literally just ignores area lights ????????
                ai_light->mType = aiLightSource_AMBIENT;
                name += "-S3D-AREA-" + std::to_string(light.id());
                ai_light->mAttenuationConstant = light.opt.size.x;
                ai_light->mAttenuationLinear = light.opt.size.y;
            } break;
            default:
                break;
            }

            Spectrum r = light.radiance();

            ai_light->mName = aiString(name);
            ai_light->mPosition = aiVector3D(0.0f, 0.0f, 0.0f);
            ai_light->mDirection = aiVector3D(0.0f, 1.0f, 0.0f);
            ai_light->mUp = aiVector3D(0.0f, 1.0f, 0.0f);
            ai_light->mSize = aiVector2D(light.opt.size.x, light.opt.size.y);
            ai_light->mAngleInnerCone = light.opt.angle_bounds.x;
            ai_light->mAngleOuterCone = light.opt.angle_bounds.y;
            ai_light->mColorDiffuse = aiColor3D(r.r, r.g, r.b);
            ai_light->mColorAmbient = aiColor3D(r.r, r.g, r.b);
            ai_light->mColorDiffuse = aiColor3D(r.r, r.g, r.b);
            ai_light->mAttenuationQuadratic = light.opt.intensity;

            Mat4 T = light.pose.transform();
            item_nodes[light.id()] = ai_node;
            ai_node->mName = aiString(name);
            ai_node->mNumMeshes = 0;
            ai_node->mMeshes = nullptr;
            ai_node->mTransformation = matMat(T);

            ai_node_light->mName = aiString(name + "-LIGHT_ANIM_NODE");
            ai_node_light->mNumMeshes = 0;
            ai_node_light->mMeshes = nullptr;
            ai_node_light->mTransformation = aiMatrix4x4();
        }
    }
    {
        scene.mNumAnimations = 1;
        scene.mAnimations = new aiAnimation *[1];
        scene.mAnimations[0] = new aiAnimation();
        aiAnimation &ai_anim = *scene.mAnimations[0];

        ai_anim.mName = aiString("Scotty3D-animate");
        ai_anim.mDuration = (double)animation.n_frames();
        ai_anim.mTicksPerSecond = (double)animation.fps();
        ai_anim.mNumChannels = (unsigned int)n_anims;
        ai_anim.mChannels = new aiNodeAnim *[n_anims];

        auto write_keyframes = [](aiNodeAnim *node, std::string name, auto pt, auto rot, auto scl) {
            node->mNodeName = aiString(name);
            node->mNumPositionKeys = (unsigned int)pt.size();
            node->mNumRotationKeys = (unsigned int)rot.size();
            node->mNumScalingKeys = (unsigned int)scl.size();
            node->mPositionKeys = new aiVectorKey[pt.size()];
            node->mRotationKeys = new aiQuatKey[rot.size()];
            node->mScalingKeys = new aiVectorKey[scl.size()];
            size_t i = 0;
            for (auto &e : pt) {
                node->mPositionKeys[i] = aiVectorKey(e.first, vecVec(e.second));
                i++;
            }
            i = 0;
            for (auto &e : rot) {
                node->mRotationKeys[i] = aiQuatKey(
                    e.first, aiQuaternion(e.second.w, e.second.x, e.second.y, e.second.z));
                i++;
            }
            i = 0;
            for (auto &e : scl) {
                node->mScalingKeys[i] = aiVectorKey(e.first, vecVec(e.second));
                i++;
            }
        };

        if (anim_cam.splines.any()) {
            ai_anim.mChannels[camera_anim] = new aiNodeAnim();

            std::set<float> keys = anim_cam.splines.keys();
            std::unordered_map<float, Vec3> points, scales;
            std::unordered_map<float, Quat> rotations;
            for (float k : keys) {
                auto [p, r, fov, ar] = anim_cam.splines.at(k);
                points[k] = p;
                rotations[k] = r;
                scales[k] = Vec3{fov, ar, 1.0f};
            }
            write_keyframes(ai_anim.mChannels[camera_anim], "camera_node", points, rotations,
                            scales);
        }

        size_t anim_idx = 0;
        for (auto &entry : objs) {

            Scene_Item &item = entry.second;
            const Anim_Pose &pose = item.animation();

            if (pose.splines.any()) {
                std::set<float> keys = pose.splines.keys();
                std::unordered_map<float, Vec3> points, scales;
                std::unordered_map<float, Quat> rotations;
                for (float k : keys) {
                    auto [p, r, s] = pose.splines.at(k);
                    points[k] = p;
                    rotations[k] = r;
                    scales[k] = s;
                }

                aiNode *node = item_nodes[item.id()];
                ai_anim.mChannels[anim_idx] = new aiNodeAnim();
                write_keyframes(ai_anim.mChannels[anim_idx], std::string(node->mName.C_Str()),
                                points, rotations, scales);
                anim_idx++;
            }

            if (item.is<Scene_Object>()) {

                Skeleton &skel = item.get<Scene_Object>().armature;
                if (skel.has_keyframes()) {

                    skel.for_joints([&](Joint *j) {
                        std::set<float> keys = j->anim.keys();
                        std::unordered_map<float, Vec3> points, scales;
                        std::unordered_map<float, Quat> rotations;

                        for (float k : keys) {
                            points[k] = Vec3{0.0f};
                            rotations[k] = j->anim.at(k);
                            scales[k] = Vec3{1.0f};
                        }

                        aiNode *node = bone_nodes[{item.id(), j->_id}];
                        ai_anim.mChannels[anim_idx] = new aiNodeAnim();
                        write_keyframes(ai_anim.mChannels[anim_idx],
                                        std::string(node->mName.C_Str()), points, rotations,
                                        scales);
                        anim_idx++;
                    });
                }

            } else if (item.is<Scene_Light>()) {

                aiNode *node = item_nodes[item.id()];
                std::string name(node->mName.C_Str());
                name += "-LIGHT_ANIM_NODE";

                const Scene_Light::Anim_Light &light = item.get<Scene_Light>().lanim;

                if (light.splines.any()) {
                    std::unordered_map<float, Vec3> points, scales;
                    std::unordered_map<float, Quat> rotations;
                    std::set<float> keys = light.splines.keys();
                    for (float k : keys) {
                        auto [spec, inten, angle, size] = light.splines.at(k);
                        points[k] = Vec3{spec.r, spec.g, spec.b};
                        rotations[k] = Quat::euler(Vec3{angle.x, 0.0f, angle.y});
                        scales[k] = Vec3{inten + 1.0f, size.x + 1.0f, size.y + 1.0f};
                    }

                    ai_anim.mChannels[anim_idx] = new aiNodeAnim();
                    write_keyframes(ai_anim.mChannels[anim_idx], name, points, rotations, scales);
                    anim_idx++;
                }
            }
        }
    }

    // Note: exporter/scene destructor should free everything

    Assimp::Exporter exporter;
    if (exporter.Export(&scene, "collada", file.c_str())) {
        return std::string(exporter.GetErrorString());
    }
    return {};
TheNumbat's avatar
TheNumbat committed
1288
}