Cogs.Core
MeshGenerator.cpp
1#include "MeshGenerator.h"
2
3#include "MeshGeneratorCube.h"
4#include "MeshGeneratorSphere.h"
5#include "MeshGeneratorCylinder.h"
6#include "MeshGeneratorCone.h"
7#include "MeshGeneratorCircularArc.h"
8
9#include "Resources/MeshManager.h"
10
11#include <unordered_map>
12
13namespace Cogs
14{
15 namespace Core
16 {
18 {
19 std::unordered_map<size_t, MeshHandle> shapes;
20 };
21 }
22}
23
24Cogs::Core::MeshGenerator::MeshGenerator(Context * context) :
25 context(context),
26 cache(std::make_unique<ShapeCache>())
27{
28}
29
30Cogs::Core::MeshGenerator::~MeshGenerator()
31{
32}
33
34void Cogs::Core::MeshGenerator::cleanup()
35{
36 cache.reset();
37}
38
39Cogs::Core::MeshHandle Cogs::Core::MeshGenerator::getMesh(ShapeType shape, int samples, glm::vec3 size, float arcStart, float arcEnd)
40{
41 ShapeDefinition definition = {
42 shape,
43 refinementLevel(samples, kRefinementLevels, kMaxSamples),
44 size,
45 arcStart,
46 arcEnd
47 };
48
49 size_t hashValue = hash(definition);
50
51 auto found = cache->shapes.find(hashValue);
52
53 if (found != cache->shapes.end()) {
54 return found->second;
55 }
56
57 auto meshHandle = context->meshManager->create();
58 auto mesh = meshHandle.resolve();
59
60 getMesh(mesh, definition);
61
62 cache->shapes[hashValue] = MeshHandle(mesh);
63
64 return MeshHandle(mesh);
65}
66
67void Cogs::Core::MeshGenerator::purgeCache()
68{
69 cache->shapes.clear();
70}
71
72
73void Cogs::Core::MeshGenerator::getMesh(Mesh * mesh, const ShapeDefinition & definition)
74{
75 switch (definition.type) {
77 planeFactory(mesh, definition);
78 break;
81 sphereGeoTexSphFactory<ShapeType::SphereGeoTexSph>(mesh, definition, kRefinementLevels, kMaxSamples);
82 break;
84 sphereGeoTexSphFactory<ShapeType::SphereGeoTexCyl>(mesh, definition, kRefinementLevels, kMaxSamples);
85 break;
87 cylinderFactory(mesh, definition, kRefinementLevels, kMaxSamples);
88 break;
89 case ShapeType::Cone:
90 coneFactory(mesh, definition, kRefinementLevels, kMaxSamples);
91 break;
92 case ShapeType::Cube:
94 cubeTex2DFactory(mesh, definition);
95 break;
96 case ShapeType::WireCube:
97 wireCubeFactory(mesh, definition);
98 break;
100 coordSysFactory(mesh, definition);
101 break;
103 circularArcFactory(mesh, definition);
104 break;
105 default:
106 break;
107 }
108}
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
ShapeType
Shape types generated by mesh generator.
Definition: ShapeType.h:19
@ CubeTex2D
Cube shape with unit square texture coordinates over each face.
@ CircularArc
Flat circular arc with inner (size.x) and outer (size.y) radius on the XY plane. Can be extended (arc...
@ Plane
Plane shape.
@ Cylinder
Generic cylinder shape.
@ SphereGeoTexCyl
Sphere shape with geodesic tessellation and cylindrical texture coordinates.
@ SphereGeoTexSph
Sphere shape with geodesic tessellation and spherical texture coordinates.
@ Cube
Generic cube shape.
@ Sphere
Generic sphere shape.
@ Cone
Generic cone shape.
@ CoordSys
Simple coordinate system where X axis is red, Y axis is green and Z axis is blue.
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
constexpr size_t hash() noexcept
Simple getter function that returns the initial value for fnv1a hashing.
Definition: HashFunctions.h:62
STL namespace.
Meshes contain streams of vertex data in addition to index data and options defining geometry used fo...
Definition: Mesh.h:265
Defines creation values for a unique shape.
Definition: ShapeType.h:74