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(3);
122 AABB bounds{{ -0.25f, -0.5f, -0.25f }, { 0.25f, 0.5f, 0.25f }};
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.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f }};
135 skeletons.emplace_back(new CompositeModel);
137 skeletons[2]->Bounds(bounds);
141 void Skeletons::Load() {
144 EntityModel::Buffer buf;
146 CuboidShape shape(skeletons[0]->Bounds());
147 shape.Vertices(buf, 1.0f);
148 buf.colors.resize(shape.VertexCount(), { 1.0f, 1.0f, 0.0f });
149 models[0].Update(buf);
150 skeletons[0]->SetNodeModel(&models[0]);
153 CuboidShape shape(skeletons[1]->Bounds());
155 shape.Vertices(buf, 2.0f);
156 buf.colors.resize(shape.VertexCount(), { 0.0f, 1.0f, 1.0f });
157 models[1].Update(buf);
158 skeletons[1]->SetNodeModel(&models[1]);
161 StairShape shape(skeletons[2]->Bounds(), { 0.4f, 0.4f });
163 shape.Vertices(buf, 3.0f);
164 buf.colors.resize(shape.VertexCount(), { 1.0f, 0.0f, 1.0f });
165 models[2].Update(buf);
166 skeletons[2]->SetNodeModel(&models[2]);
170 CompositeModel *Skeletons::ByID(std::uint16_t id) noexcept {
171 if (id == 0 || id > skeletons.size()) {
174 return skeletons[id - 1].get();
178 const CompositeModel *Skeletons::ByID(std::uint16_t id) const noexcept {
179 if (id == 0 || id > skeletons.size()) {
182 return skeletons[id - 1].get();