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"
12 #include <glm/gtx/quaternion.hpp>
13 #include <glm/gtx/io.hpp>
23 , hsl_mod(0.0f, 1.0f, 1.0f)
28 Instance::~Instance() {
32 Instance::Instance(const Instance &other)
36 , tex_map(other.tex_map)
37 , hsl_mod(other.hsl_mod)
38 , rgb_mod(other.rgb_mod) {
42 Instance &Instance::operator =(const Instance &other) {
46 tex_map = other.tex_map;
47 hsl_mod = other.hsl_mod;
48 rgb_mod = other.rgb_mod;
52 void Instance::Render(const glm::mat4 &M, DirectionalLighting &prog) {
54 std::cout << "building meshes for instance" << std::endl;
55 mesh.resize(state.size());
56 model->RootPart().LoadMeshes(*this);
58 model->RootPart().Render(M, *this, prog);
61 void Instance::SetTextures(const std::vector<float> &t) {
66 void Instance::SetHSLModifier(const glm::vec3 &m) {
71 void Instance::SetRGBModifier(const glm::vec3 &m) {
84 void Model::Enumerate() {
86 part.resize(root.Enumerate(0), nullptr);
90 void Model::Instantiate(Instance &inst) const {
94 inst.state.resize(part.size());
100 , bounds{ glm::vec3(0.0f), glm::vec3(0.0f) }
112 Part &Part::AddChild() {
113 children.emplace_back();
114 children.back().parent = this;
115 return children.back();
118 std::uint16_t Part::Enumerate(std::uint16_t counter) noexcept {
120 for (Part &part : children) {
121 counter = part.Enumerate(counter);
126 void Part::Index(std::vector<Part *> &index) noexcept {
128 for (Part &part : children) {
133 glm::mat4 Part::LocalTransform(const Instance &inst) const noexcept {
134 glm::mat4 transform(toMat4(initial.orientation * inst.state[id].orientation));
135 transform[3] = glm::vec4(initial.position + inst.state[id].position, 1.0f);
139 glm::mat4 Part::GlobalTransform(const Instance &inst) const noexcept {
141 return parent->GlobalTransform(inst) * LocalTransform(inst);
143 return LocalTransform(inst);
149 EntityMesh::Buffer buf;
153 void Part::LoadMeshes(Instance &inst) const {
154 if (shape && shape->IndexCount() > 0) {
156 buf.hsl_mods.resize(shape->VertexCount(), inst.hsl_mod);
157 buf.rgb_mods.resize(shape->VertexCount(), inst.rgb_mod);
158 shape->Fill(buf, inst.tex_map);
159 inst.mesh[id].reset(new EntityMesh());
160 inst.mesh[id]->Update(buf);
162 inst.mesh[id].reset();
164 for (const Part &part : children) {
165 part.LoadMeshes(inst);
171 const Instance &inst,
172 DirectionalLighting &prog
174 glm::mat4 transform = M * LocalTransform(inst);
176 prog.SetM(transform);
177 inst.mesh[id]->Draw();
179 for (const Part &part : children) {
180 part.Render(transform, inst, prog);
185 Skeletons::Skeletons()
190 Skeletons::~Skeletons() {
194 void Skeletons::Load(const ShapeRegistry &shapes) {
196 skeletons.reserve(4);
197 AABB bounds{{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f }};
198 const Shape *shape = &shapes.Get("player_head_block");
200 skeletons.emplace_back(new Model);
202 skeletons[0]->RootPart().bounds = bounds;
203 skeletons[0]->RootPart().shape = shape;
204 skeletons[0]->Enumerate();
207 skeletons.emplace_back(new Model);
209 skeletons[1]->RootPart().bounds = bounds;
210 skeletons[1]->RootPart().shape = shape;
211 skeletons[1]->Enumerate();
214 skeletons.emplace_back(new Model);
216 skeletons[2]->RootPart().bounds = bounds;
217 skeletons[2]->RootPart().shape = shape;
218 skeletons[2]->Enumerate();
221 skeletons.emplace_back(new Model);
223 skeletons[3]->RootPart().bounds = bounds;
224 skeletons[3]->RootPart().shape = shape;
225 skeletons[3]->Enumerate();
229 Model *Skeletons::ByID(std::uint16_t id) noexcept {
230 if (id == 0 || id > skeletons.size()) {
233 return skeletons[id - 1].get();
237 const Model *Skeletons::ByID(std::uint16_t id) const noexcept {
238 if (id == 0 || id > skeletons.size()) {
241 return skeletons[id - 1].get();