2 #include "Instance.hpp"
3 #include "Skeletons.hpp"
6 #include "ShapeRegistry.hpp"
7 #include "../app/TextureIndex.hpp"
8 #include "../graphics/DirectionalLighting.hpp"
9 #include "../graphics/EntityMesh.hpp"
11 #include <glm/gtx/quaternion.hpp>
22 void Instance::Render(const glm::mat4 &M, DirectionalLighting &prog) const {
23 model->RootPart().Render(M, state, prog);
34 void Model::Enumerate() {
36 part.resize(root.Enumerate(0), nullptr);
40 void Model::Instantiate(Instance &inst) const {
43 inst.state.resize(part.size());
49 , bounds{ glm::vec3(0.0f), glm::vec3(0.0f) }
61 Part &Part::AddChild() {
62 children.emplace_back();
63 children.back().parent = this;
64 return children.back();
67 std::uint16_t Part::Enumerate(std::uint16_t counter) noexcept {
69 for (Part &part : children) {
70 counter = part.Enumerate(counter);
75 void Part::Index(std::vector<Part *> &index) noexcept {
77 for (Part &part : children) {
82 glm::mat4 Part::LocalTransform(
83 const std::vector<State> &state
85 glm::mat4 transform(toMat4(initial.orientation * state[id].orientation));
86 transform[3] = glm::vec4(initial.position + state[id].position, 1.0f);
90 glm::mat4 Part::GlobalTransform(
91 const std::vector<State> &state
94 return parent->GlobalTransform(state) * LocalTransform(state);
96 return LocalTransform(state);
102 const std::vector<State> &state,
103 DirectionalLighting &prog
105 glm::mat4 transform = M * LocalTransform(state);
107 prog.SetM(transform);
110 for (const Part &part : children) {
111 part.Render(transform, state, prog);
116 Skeletons::Skeletons()
122 Skeletons::~Skeletons() {
126 void Skeletons::LoadHeadless() {
128 skeletons.reserve(4);
129 AABB bounds{{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f }};
131 skeletons.emplace_back(new Model);
133 skeletons[0]->RootPart().bounds = bounds;
134 skeletons[0]->Enumerate();
137 skeletons.emplace_back(new Model);
139 skeletons[1]->RootPart().bounds = bounds;
140 skeletons[1]->Enumerate();
143 skeletons.emplace_back(new Model);
145 skeletons[2]->RootPart().bounds = bounds;
146 skeletons[2]->Enumerate();
149 skeletons.emplace_back(new Model);
151 skeletons[3]->RootPart().bounds = bounds;
152 skeletons[3]->Enumerate();
156 void Skeletons::Load(const ShapeRegistry &shapes, TextureIndex &tex_index) {
159 const Shape &shape = shapes.Get("player_head_block");
160 EntityMesh::Buffer buf;
161 std::vector<float> tex_map;
162 tex_map.push_back(tex_index.GetID("rock-1"));
163 tex_map.push_back(tex_index.GetID("rock-face"));
164 buf.Reserve(shape.VertexCount(), shape.IndexCount());
166 shape.Fill(buf, tex_map);
167 buf.hsl_mods.resize(shape.VertexCount(), { 0.0f, 1.0f, 1.0f });
168 buf.rgb_mods.resize(shape.VertexCount(), { 1.0f, 1.0f, 0.0f });
169 meshes[0].Update(buf);
170 skeletons[0]->RootPart().mesh = &meshes[0];
174 shape.Fill(buf, tex_map);
175 buf.hsl_mods.resize(shape.VertexCount(), { 0.0f, 1.0f, 1.0f });
176 buf.rgb_mods.resize(shape.VertexCount(), { 0.0f, 1.0f, 1.0f });
177 meshes[1].Update(buf);
178 skeletons[1]->RootPart().mesh = &meshes[1];
182 shape.Fill(buf, tex_map);
183 buf.hsl_mods.resize(shape.VertexCount(), { 0.0f, 1.0f, 1.0f });
184 buf.rgb_mods.resize(shape.VertexCount(), { 1.0f, 0.0f, 1.0f });
185 meshes[2].Update(buf);
186 skeletons[2]->RootPart().mesh = &meshes[2];
190 shape.Fill(buf, tex_map);
191 buf.hsl_mods.resize(shape.VertexCount(), { 0.0f, 1.0f, 1.0f });
192 buf.rgb_mods.resize(shape.VertexCount(), { 1.0f, 0.25f, 0.5f });
193 meshes[3].Update(buf);
194 skeletons[3]->RootPart().mesh = &meshes[3];
198 Model *Skeletons::ByID(std::uint16_t id) noexcept {
199 if (id == 0 || id > skeletons.size()) {
202 return skeletons[id - 1].get();
206 const Model *Skeletons::ByID(std::uint16_t id) const noexcept {
207 if (id == 0 || id > skeletons.size()) {
210 return skeletons[id - 1].get();