Cogs.Core
Model.h
1#pragma once
2
3#include "Services/PropertiesManager.h"
4
5#include "ResourceBase.h"
6
7#include "Skeleton.h"
8
9#include "Foundation/Geometry/BoundingBox.hpp"
10
11namespace Cogs::Core
12{
13 struct Mesh;
14
15 const size_t NoParentPart = uint32_t(-1);
16 const uint32_t NoIndex = uint32_t(-1);
17
18 const glm::mat4 ModelPartIdentity = glm::mat4(1.0f);
19
20 struct ModelPart
21 {
22 ModelPart() = default;
23 ModelPart(ModelPart & modelPart) = delete;
24 ModelPart(ModelPart && modelPart) = default;
25
26 uint32_t parentIndex = NoParentPart;
27 uint32_t boundsIndex = NoIndex;
28 uint32_t meshIndex = NoIndex;
29 uint32_t materialIndex = NoIndex;
30
31 uint32_t startIndex = 0;
32 uint32_t vertexCount = static_cast<uint32_t>(-1);
33
34 uint32_t nameIndex = NoIndex;
35 uint32_t transformIndex = NoIndex;
36
37 uint32_t firstProperty = 0;
38 uint32_t numProperties = 0;
39
40 uint32_t primitiveType = static_cast<uint32_t>(-1);
41 uint32_t pad;
42 };
43
55 struct Model : public ResourceBase
56 {
58 Model() = default;
59
61 Model(const Model & other) = delete;
62
64 Model(Model && other) noexcept = default;
65
67 Model & operator=(Model && other) noexcept = default;
68
69 void setPartName(ModelPart & part, const StringView & name)
70 {
71 part.nameIndex = properties.addProperty("n", name);
72 }
73
74 StringView getPartName(const ModelPart & part) const
75 {
76 if (part.nameIndex == NoIndex) return {};
77 return properties.getString(part.nameIndex);
78 }
79
80 void setPartTransform(ModelPart & part, const glm::mat4 & t)
81 {
82 glm::mat4 tt = t;
83 auto values = std::span(glm::value_ptr(tt), 16);
84 part.transformIndex = properties.addProperty("t", values);
85 }
86
87 const glm::mat4 & getPartTransform(const ModelPart & part) const
88 {
89 if (part.transformIndex != NoIndex) {
90 auto values = properties.getFloatArray(part.transformIndex);
91 return *reinterpret_cast<const glm::mat4 *>(values.data());
92 }
93
94 return ModelPartIdentity;
95 }
96
97 std::vector<MeshHandle> meshes;
98 std::vector<MaterialInstanceHandle> materials;
99
100 std::vector<Geometry::BoundingBox> bounds;
101 std::vector<ModelPart> parts;
102
103 PropertyStore properties;
104
105 // 2019-01-24 chrisdy: This skeleton appears not to be used outside importers/exporters,
106 // not even by the model instantiation code. The animation & rendering code uses the one
107 // stored inside the animation.
108 Skeleton skeleton;
109
110 AnimationHandle animation;
111 };
112}
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
Model resources define a template for a set of connected entities, with resources such as meshes,...
Definition: Model.h:56
Model & operator=(Model &&other) noexcept=default
Move assign a Model from other.
Model(Model &&other) noexcept=default
Move construct a Model from other.
Model(const Model &other)=delete
Disabled copy construction.
Model()=default
Default constructs a Model resource.
Base class for engine resources.
Definition: ResourceBase.h:107