]> git.localhorst.tv Git - blank.git/blobdiff - src/model/model.cpp
save a little bandwidth
[blank.git] / src / model / model.cpp
index 22d2bedfc54211cbc68202ac04a0074a768829a6..ed443138e23e0d5746a551c25fd7e1f6956cf198 100644 (file)
 #include "Model.hpp"
+#include "ModelRegistry.hpp"
 #include "Instance.hpp"
-#include "Skeletons.hpp"
+#include "Part.hpp"
 
-#include "bounds.hpp"
+#include "Shape.hpp"
+#include "ShapeRegistry.hpp"
+#include "../io/TokenStreamReader.hpp"
 #include "../graphics/DirectionalLighting.hpp"
 #include "../graphics/EntityMesh.hpp"
+#include "../shared/ResourceIndex.hpp"
 
+#include <iostream>
 #include <glm/gtx/quaternion.hpp>
+#include <glm/gtx/io.hpp>
 
 
 namespace blank {
 
-Model::Model()
-: parent(nullptr)
-, node_mesh(nullptr)
-, id(0)
-, bounds{{ 0.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 0.0f }}
-, position(0.0f)
-, orientation(1.0f, 0.0f, 0.0f, 0.0f)
-, parts() {
+Instance::Instance()
+: model(nullptr)
+, state() {
 
 }
 
+Instance::~Instance() {
 
-Model &Model::AddPart() {
-       parts.emplace_back();
-       parts.back().parent = this;
-       return parts.back();
 }
 
+Part::State &Instance::BodyState() noexcept {
+       return state[model->GetBodyPart().ID()];
+}
 
-glm::mat4 Model::LocalTransform() const noexcept {
-       glm::mat4 transform(toMat4(orientation));
-       transform[3].x = position.x;
-       transform[3].y = position.y;
-       transform[3].z = position.z;
-       return transform;
+glm::mat4 Instance::BodyTransform() const noexcept {
+       return model->GetBodyPart().GlobalTransform(*this);
 }
 
-glm::mat4 Model::GlobalTransform() const noexcept {
-       if (HasParent()) {
-               return Parent().GlobalTransform() * LocalTransform();
-       } else {
-               return LocalTransform();
-       }
+Part::State &Instance::EyesState() noexcept {
+       return state[model->GetEyesPart().ID()];
 }
 
+glm::mat4 Instance::EyesTransform() const noexcept {
+       return model->GetEyesPart().GlobalTransform(*this);
+}
 
-void Model::Instantiate(Instance &inst) const {
-       inst.part_model = this;
-       inst.position = position;
-       inst.orientation = orientation;
-       inst.parts.clear();
-       inst.parts.reserve(parts.size());
-       for (const Model &part : parts) {
-               part.Instantiate(inst.AddPart());
-       }
+void Instance::Render(const glm::mat4 &M, DirectionalLighting &prog) {
+       model->RootPart().Render(M, *this, prog);
 }
 
 
-Instance::Instance()
-: part_model(nullptr)
-, parent(nullptr)
-, position(0.0f)
-, orientation(1.0f, 0.0f, 0.0f, 0.0f)
-, parts() {
+Model::Model()
+: id(0)
+, root()
+, part()
+, body_id(0)
+, eyes_id(0) {
 
 }
 
+void Model::Enumerate() {
+       part.clear();
+       part.resize(root.Enumerate(0), nullptr);
+       root.Index(part);
+}
 
-Instance &Instance::AddPart() {
-       parts.emplace_back();
-       parts.back().parent = this;
-       return parts.back();
+void Model::Instantiate(Instance &inst) const {
+       inst.model = this;
+       inst.state.clear();
+       inst.state.resize(part.size());
 }
 
 
-glm::mat4 Instance::LocalTransform() const noexcept {
-       glm::mat4 transform(toMat4(orientation));
-       transform[3].x = position.x;
-       transform[3].y = position.y;
-       transform[3].z = position.z;
-       return transform;
+ModelRegistry::ModelRegistry()
+: models()
+, name_index() {
+
 }
 
-glm::mat4 Instance::GlobalTransform() const noexcept {
-       if (HasParent()) {
-               return Parent().GlobalTransform() * LocalTransform();
+Model &ModelRegistry::Add(const std::string &name) {
+       models.emplace_back(new Model());
+       models.back()->ID(models.size());
+       name_index[name] = &*models.back();
+       return *models.back();
+}
+
+Model &ModelRegistry::Get(const std::string &name) {
+       auto entry = name_index.find(name);
+       if (entry != name_index.end()) {
+               return *entry->second;
        } else {
-               return LocalTransform();
+               throw std::runtime_error("unknown model " + name);
        }
 }
 
-
-void Instance::Render(const glm::mat4 &M, DirectionalLighting &prog) const {
-       glm::mat4 transform(M * LocalTransform());
-       if (part_model->HasNodeMesh()) {
-               prog.SetM(transform);
-               part_model->NodeMesh().Draw();
-       }
-       for (const Instance &part : parts) {
-               part.Render(transform, prog);
+const Model &ModelRegistry::Get(const std::string &name) const {
+       auto entry = name_index.find(name);
+       if (entry != name_index.end()) {
+               return *entry->second;
+       } else {
+               throw std::runtime_error("unknown model " + name);
        }
 }
 
 
-Skeletons::Skeletons()
-: skeletons()
-, meshes() {
-
+Part::Part()
+: parent(nullptr)
+, shape(nullptr)
+, children()
+, tex_map()
+, mesh()
+, initial()
+, hsl_mod(0, 255, 255)
+, rgb_mod(255, 255, 255)
+, id(0) {
+
+}
+
+Part::~Part() {
+
+}
+
+void Part::Read(TokenStreamReader &in, ResourceIndex &tex_index, const ShapeRegistry &shapes) {
+       std::string name;
+       std::string shape_name;
+       std::string tex_name;
+       glm::vec3 color_conv;
+       in.Skip(Token::ANGLE_BRACKET_OPEN);
+       while (in.HasMore() && in.Peek().type != Token::ANGLE_BRACKET_CLOSE) {
+               in.ReadIdentifier(name);
+               in.Skip(Token::EQUALS);
+               if (name == "shape") {
+                       in.ReadIdentifier(shape_name);
+                       shape = &shapes.Get(shape_name);
+               } else if (name == "position") {
+                       in.ReadVec(initial.position);
+               } else if (name == "orientation") {
+                       in.ReadQuat(initial.orientation);
+               } else if (name == "hsl_mod") {
+                       in.ReadVec(color_conv);
+                       hsl_mod = glm::tvec3<unsigned char>(color_conv * 255.0f);
+               } else if (name == "rgb_mod") {
+                       in.ReadVec(color_conv);
+                       rgb_mod = glm::tvec3<unsigned char>(color_conv * 255.0f);
+               } else if (name == "textures") {
+                       in.Skip(Token::BRACKET_OPEN);
+                       while (in.HasMore() && in.Peek().type != Token::BRACKET_CLOSE) {
+                               in.ReadString(tex_name);
+                               tex_map.push_back(tex_index.GetID(tex_name));
+                               if (in.Peek().type == Token::COMMA) {
+                                       in.Skip(Token::COMMA);
+                               }
+                       }
+                       in.Skip(Token::BRACKET_CLOSE);
+               } else if (name == "children") {
+                       in.Skip(Token::BRACKET_OPEN);
+                       while (in.HasMore() && in.Peek().type != Token::BRACKET_CLOSE) {
+                               Part &child = AddChild();
+                               child.Read(in, tex_index, shapes);
+                               if (in.Peek().type == Token::COMMA) {
+                                       in.Skip(Token::COMMA);
+                               }
+                       }
+                       in.Skip(Token::BRACKET_CLOSE);
+               } else {
+                       while (in.HasMore() && in.Peek().type != Token::SEMICOLON) {
+                               in.Next();
+                       }
+               }
+               in.Skip(Token::SEMICOLON);
+       }
+       in.Skip(Token::ANGLE_BRACKET_CLOSE);
 }
 
-Skeletons::~Skeletons() {
-
+Part &Part::AddChild() {
+       children.emplace_back();
+       children.back().parent = this;
+       return children.back();
 }
 
-void Skeletons::LoadHeadless() {
-       skeletons.clear();
-       skeletons.reserve(4);
-       {
-               AABB bounds{{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f }};
-               skeletons.emplace_back(new Model);
-               skeletons[0]->ID(1);
-               skeletons[0]->Bounds(bounds);
-       }
-       {
-               AABB bounds{{ -0.5f, -0.25f, -0.5f }, { 0.5f, 0.25f, 0.5f }};
-               skeletons.emplace_back(new Model);
-               skeletons[1]->ID(2);
-               skeletons[1]->Bounds(bounds);
-       }
-       {
-               AABB bounds{{ -0.25f, -0.5f, -0.25f }, { 0.25f, 0.5f, 0.25f }};
-               skeletons.emplace_back(new Model);
-               skeletons[2]->ID(3);
-               skeletons[2]->Bounds(bounds);
-       }
-       {
-               AABB bounds{{ -0.25f, -0.5f, -0.35f }, { 0.25f, 0.5f, 0.35f }};
-               skeletons.emplace_back(new Model);
-               skeletons[3]->ID(4);
-               skeletons[3]->Bounds(bounds);
+std::uint16_t Part::Enumerate(std::uint16_t counter) noexcept {
+       id = counter++;
+       for (Part &part : children) {
+               counter = part.Enumerate(counter);
        }
+       return counter;
 }
 
-void Skeletons::Load() {
-       LoadHeadless();
-       meshes.resize(4);
-       EntityMesh::Buffer buf;
-       {
-               CuboidBounds shape(skeletons[0]->Bounds());
-               shape.Vertices(buf, 3.0f);
-               buf.hsl_mods.resize(shape.VertexCount(), { 0.0f, 1.0f, 1.0f });
-               buf.rgb_mods.resize(shape.VertexCount(), { 1.0f, 1.0f, 0.0f });
-               meshes[0].Update(buf);
-               skeletons[0]->SetNodeMesh(&meshes[0]);
-       }
-       {
-               CuboidBounds shape(skeletons[1]->Bounds());
-               buf.Clear();
-               shape.Vertices(buf, 0.0f);
-               buf.hsl_mods.resize(shape.VertexCount(), { 0.0f, 1.0f, 1.0f });
-               buf.rgb_mods.resize(shape.VertexCount(), { 0.0f, 1.0f, 1.0f });
-               meshes[1].Update(buf);
-               skeletons[1]->SetNodeMesh(&meshes[1]);
-       }
-       {
-               StairBounds shape(skeletons[2]->Bounds(), { 0.4f, 0.4f });
-               buf.Clear();
-               shape.Vertices(buf, 1.0f);
-               buf.hsl_mods.resize(shape.VertexCount(), { 0.0f, 1.0f, 1.0f });
-               buf.rgb_mods.resize(shape.VertexCount(), { 1.0f, 0.0f, 1.0f });
-               meshes[2].Update(buf);
-               skeletons[2]->SetNodeMesh(&meshes[2]);
-       }
-       {
-               CuboidBounds shape(skeletons[3]->Bounds());
-               buf.Clear();
-               shape.Vertices(buf, 2.0f);
-               buf.hsl_mods.resize(shape.VertexCount(), { 0.0f, 1.0f, 1.0f });
-               buf.rgb_mods.resize(shape.VertexCount(), { 1.0f, 0.25f, 0.5f });
-               meshes[3].Update(buf);
-               skeletons[3]->SetNodeMesh(&meshes[3]);
+void Part::Index(std::vector<Part *> &index) noexcept {
+       index[id] = this;
+       for (Part &part : children) {
+               part.Index(index);
        }
 }
 
-Model *Skeletons::ByID(std::uint16_t id) noexcept {
-       if (id == 0 || id > skeletons.size()) {
-               return nullptr;
+glm::mat4 Part::LocalTransform(const Instance &inst) const noexcept {
+       glm::mat4 transform(toMat4(initial.orientation * inst.state[id].orientation));
+       transform[3] = glm::vec4(initial.position + inst.state[id].position, 1.0f);
+       return transform;
+}
+
+glm::mat4 Part::GlobalTransform(const Instance &inst) const noexcept {
+       if (parent) {
+               return parent->GlobalTransform(inst) * LocalTransform(inst);
        } else {
-               return skeletons[id - 1].get();
+               return LocalTransform(inst);
        }
 }
 
-const Model *Skeletons::ByID(std::uint16_t id) const noexcept {
-       if (id == 0 || id > skeletons.size()) {
-               return nullptr;
-       } else {
-               return skeletons[id - 1].get();
+namespace {
+
+EntityMesh::Buffer buf;
+
+}
+
+void Part::Render(
+       const glm::mat4 &M,
+       const Instance &inst,
+       DirectionalLighting &prog
+) const {
+       glm::mat4 transform = M * LocalTransform(inst);
+       if (shape && shape->IndexCount() > 0) {
+               if (!mesh) {
+                       buf.Clear();
+                       buf.hsl_mods.resize(shape->VertexCount(), hsl_mod);
+                       buf.rgb_mods.resize(shape->VertexCount(), rgb_mod);
+                       shape->Fill(buf, tex_map);
+                       mesh.reset(new EntityMesh());
+                       mesh->Update(buf);
+               }
+               prog.SetM(transform);
+               mesh->Draw();
+       }
+       for (const Part &part : children) {
+               part.Render(transform, inst, prog);
        }
 }