summaryrefslogtreecommitdiff
path: root/assetFactory/use.cpp
blob: 898b736266feab5e7e4814b6e382f144aef47ca4 (plain)
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
#include "use.h"
#include "assetFactory.h"
#include <glm/ext/matrix_transform.hpp>
#include <glm/matrix.hpp>
#include <maths.h>
#include <set>
#include <stream_support.hpp>

Shape::CreatedFaces
Use::createMesh(ModelFactoryMesh & mesh, float levelOfDetailFactor) const
{
	auto apply = [&mesh](const auto & faces, const Mutation::Matrix & m) {
		std::set<ModelFactoryMesh::VertexHandle> vs;
		for (const auto & f : faces) {
			const auto fvr = mesh.fv_range(f.second);
			for (const auto & v : fvr) {
				if (!vs.contains(v)) {
					glm::vec3 p = mesh.point(v);
					p = p % m;
					mesh.set_point(v, ModelFactoryMesh::Point(p.x, p.y, p.z));
					vs.insert(v);
				}
			}
		}
	};

	auto faces = type->createMesh(mesh, levelOfDetailFactor * relativeLevelOfDetail());
	applyStyle(mesh, {this}, faces);
	apply(faces, getDeformationMatrix());
	for (const auto & [name, faceController] : faceControllers) {
		faceController->apply(mesh, {this}, name, faces);
	}
	apply(faces, getLocationMatrix());
	return faces;
}

struct Lookup : public Persistence::SelectionV<Shape::CPtr> {
	using Persistence::SelectionV<Shape::CPtr>::SelectionV;
	using Persistence::SelectionV<Shape::CPtr>::setValue;
	void
	setValue(std::string && str) override
	{
		if (auto mf = Persistence::ParseBase::getShared<const AssetFactory>("assetFactory")) {
			v = mf->shapes.at(str);
		}
	}
};

bool
Use::persist(Persistence::PersistenceStore & store)
{
	return STORE_TYPE && STORE_HELPER(type, Lookup) && STORE_MEMBER(position) && STORE_MEMBER(scale)
			&& STORE_MEMBER(rotation) && Style::persist(store)
			&& STORE_NAME_HELPER("face", faceControllers, Persistence::MapByMember<FaceControllers>);
}