1 #include "CompositeModel.hpp"
2 #include "CompositeInstance.hpp"
3 #include "Skeletons.hpp"
5 #include "EntityModel.hpp"
7 #include "../graphics/DirectionalLighting.hpp"
9 #include <glm/gtx/quaternion.hpp>
14 CompositeModel::CompositeModel()
18 , bounds{{ 0.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 0.0f }}
20 , orientation(1.0f, 0.0f, 0.0f, 0.0f)
26 CompositeModel &CompositeModel::AddPart() {
28 parts.back().parent = this;
33 glm::mat4 CompositeModel::LocalTransform() const noexcept {
34 glm::mat4 transform(toMat4(orientation));
35 transform[3].x = position.x;
36 transform[3].y = position.y;
37 transform[3].z = position.z;
41 glm::mat4 CompositeModel::GlobalTransform() const noexcept {
43 return Parent().GlobalTransform() * LocalTransform();
45 return LocalTransform();
50 void CompositeModel::Instantiate(CompositeInstance &inst) const {
51 inst.part_model = this;
52 inst.position = position;
53 inst.orientation = orientation;
55 inst.parts.reserve(parts.size());
56 for (const CompositeModel &part : parts) {
57 part.Instantiate(inst.AddPart());
62 CompositeInstance::CompositeInstance()
66 , orientation(1.0f, 0.0f, 0.0f, 0.0f)
72 CompositeInstance &CompositeInstance::AddPart() {
74 parts.back().parent = this;
79 glm::mat4 CompositeInstance::LocalTransform() const noexcept {
80 glm::mat4 transform(toMat4(orientation));
81 transform[3].x = position.x;
82 transform[3].y = position.y;
83 transform[3].z = position.z;
87 glm::mat4 CompositeInstance::GlobalTransform() const noexcept {
89 return Parent().GlobalTransform() * LocalTransform();
91 return LocalTransform();
96 void CompositeInstance::Render(const glm::mat4 &M, DirectionalLighting &prog) const {
97 glm::mat4 transform(M * LocalTransform());
98 if (part_model->HasNodeModel()) {
100 part_model->NodeModel().Draw();
102 for (const CompositeInstance &part : parts) {
103 part.Render(transform, prog);
108 Skeletons::Skeletons()
114 Skeletons::~Skeletons() {
118 void Skeletons::LoadHeadless() {
120 skeletons.reserve(4);
122 AABB bounds{{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f }};
123 skeletons.emplace_back(new CompositeModel);
125 skeletons[0]->Bounds(bounds);
128 AABB bounds{{ -0.5f, -0.25f, -0.5f }, { 0.5f, 0.25f, 0.5f }};
129 skeletons.emplace_back(new CompositeModel);
131 skeletons[1]->Bounds(bounds);
134 AABB bounds{{ -0.25f, -0.5f, -0.25f }, { 0.25f, 0.5f, 0.25f }};
135 skeletons.emplace_back(new CompositeModel);
137 skeletons[2]->Bounds(bounds);
140 AABB bounds{{ -0.25f, -0.5f, -0.35f }, { 0.25f, 0.5f, 0.35f }};
141 skeletons.emplace_back(new CompositeModel);
143 skeletons[3]->Bounds(bounds);
147 void Skeletons::Load() {
150 EntityModel::Buffer buf;
152 CuboidShape shape(skeletons[0]->Bounds());
153 shape.Vertices(buf, 3.0f);
154 buf.colors.resize(shape.VertexCount(), { 1.0f, 1.0f, 0.0f });
155 models[0].Update(buf);
156 skeletons[0]->SetNodeModel(&models[0]);
159 CuboidShape shape(skeletons[1]->Bounds());
161 shape.Vertices(buf, 0.0f);
162 buf.colors.resize(shape.VertexCount(), { 0.0f, 1.0f, 1.0f });
163 models[1].Update(buf);
164 skeletons[1]->SetNodeModel(&models[1]);
167 StairShape shape(skeletons[2]->Bounds(), { 0.4f, 0.4f });
169 shape.Vertices(buf, 1.0f);
170 buf.colors.resize(shape.VertexCount(), { 1.0f, 0.0f, 1.0f });
171 models[2].Update(buf);
172 skeletons[2]->SetNodeModel(&models[2]);
175 CuboidShape shape(skeletons[3]->Bounds());
177 shape.Vertices(buf, 2.0f);
178 buf.colors.resize(shape.VertexCount(), { 1.0f, 0.25f, 0.5f });
179 models[3].Update(buf);
180 skeletons[3]->SetNodeModel(&models[3]);
184 CompositeModel *Skeletons::ByID(std::uint16_t id) noexcept {
185 if (id == 0 || id > skeletons.size()) {
188 return skeletons[id - 1].get();
192 const CompositeModel *Skeletons::ByID(std::uint16_t id) const noexcept {
193 if (id == 0 || id > skeletons.size()) {
196 return skeletons[id - 1].get();