]> git.localhorst.tv Git - blank.git/blobdiff - src/model/model.cpp
save a little bandwidth
[blank.git] / src / model / model.cpp
index 730a34a330c4bd08c1336e2750039781f064c8ac..ed443138e23e0d5746a551c25fd7e1f6956cf198 100644 (file)
@@ -1,12 +1,14 @@
 #include "Model.hpp"
+#include "ModelRegistry.hpp"
 #include "Instance.hpp"
-#include "Skeletons.hpp"
+#include "Part.hpp"
 
 #include "Shape.hpp"
 #include "ShapeRegistry.hpp"
-#include "../app/TextureIndex.hpp"
+#include "../io/TokenStreamReader.hpp"
 #include "../graphics/DirectionalLighting.hpp"
 #include "../graphics/EntityMesh.hpp"
+#include "../shared/ResourceIndex.hpp"
 
 #include <iostream>
 #include <glm/gtx/quaternion.hpp>
@@ -17,11 +19,7 @@ namespace blank {
 
 Instance::Instance()
 : model(nullptr)
-, state()
-, mesh()
-, tex_map()
-, hsl_mod(0.0f, 1.0f, 1.0f)
-, rgb_mod(1.0f) {
+, state() {
 
 }
 
@@ -29,55 +27,33 @@ Instance::~Instance() {
 
 }
 
-Instance::Instance(const Instance &other)
-: model(other.model)
-, state(other.state)
-, mesh()
-, tex_map(other.tex_map)
-, hsl_mod(other.hsl_mod)
-, rgb_mod(other.rgb_mod) {
-
-}
-
-Instance &Instance::operator =(const Instance &other) {
-       model = other.model;
-       state = other.state;
-       mesh.clear();
-       tex_map = other.tex_map;
-       hsl_mod = other.hsl_mod;
-       rgb_mod = other.rgb_mod;
-       return *this;
+Part::State &Instance::BodyState() noexcept {
+       return state[model->GetBodyPart().ID()];
 }
 
-void Instance::Render(const glm::mat4 &M, DirectionalLighting &prog) {
-       if (mesh.empty()) {
-               std::cout << "building meshes for instance" << std::endl;
-               mesh.resize(state.size());
-               model->RootPart().LoadMeshes(*this);
-       }
-       model->RootPart().Render(M, *this, prog);
+glm::mat4 Instance::BodyTransform() const noexcept {
+       return model->GetBodyPart().GlobalTransform(*this);
 }
 
-void Instance::SetTextures(const std::vector<float> &t) {
-       tex_map = t;
-       mesh.clear();
+Part::State &Instance::EyesState() noexcept {
+       return state[model->GetEyesPart().ID()];
 }
 
-void Instance::SetHSLModifier(const glm::vec3 &m) {
-       hsl_mod = m;
-       mesh.clear();
+glm::mat4 Instance::EyesTransform() const noexcept {
+       return model->GetEyesPart().GlobalTransform(*this);
 }
 
-void Instance::SetRGBModifier(const glm::vec3 &m) {
-       rgb_mod = m;
-       mesh.clear();
+void Instance::Render(const glm::mat4 &M, DirectionalLighting &prog) {
+       model->RootPart().Render(M, *this, prog);
 }
 
 
 Model::Model()
 : id(0)
 , root()
-, part() {
+, part()
+, body_id(0)
+, eyes_id(0) {
 
 }
 
@@ -90,18 +66,52 @@ void Model::Enumerate() {
 void Model::Instantiate(Instance &inst) const {
        inst.model = this;
        inst.state.clear();
-       inst.mesh.clear();
        inst.state.resize(part.size());
 }
 
 
+ModelRegistry::ModelRegistry()
+: models()
+, name_index() {
+
+}
+
+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 {
+               throw std::runtime_error("unknown model " + name);
+       }
+}
+
+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);
+       }
+}
+
+
 Part::Part()
-: id(0)
-, bounds{ glm::vec3(0.0f), glm::vec3(0.0f) }
-, initial()
+: parent(nullptr)
 , shape(nullptr)
-, parent(nullptr)
-, children() {
+, children()
+, tex_map()
+, mesh()
+, initial()
+, hsl_mod(0, 255, 255)
+, rgb_mod(255, 255, 255)
+, id(0) {
 
 }
 
@@ -109,6 +119,58 @@ 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);
+}
+
 Part &Part::AddChild() {
        children.emplace_back();
        children.back().parent = this;
@@ -150,96 +212,27 @@ EntityMesh::Buffer buf;
 
 }
 
-void Part::LoadMeshes(Instance &inst) const {
-       if (shape && shape->IndexCount() > 0) {
-               buf.Clear();
-               buf.hsl_mods.resize(shape->VertexCount(), inst.hsl_mod);
-               buf.rgb_mods.resize(shape->VertexCount(), inst.rgb_mod);
-               shape->Fill(buf, inst.tex_map);
-               inst.mesh[id].reset(new EntityMesh());
-               inst.mesh[id]->Update(buf);
-       } else {
-               inst.mesh[id].reset();
-       }
-       for (const Part &part : children) {
-               part.LoadMeshes(inst);
-       }
-}
-
 void Part::Render(
        const glm::mat4 &M,
        const Instance &inst,
        DirectionalLighting &prog
 ) const {
        glm::mat4 transform = M * LocalTransform(inst);
-       if (inst.mesh[id]) {
+       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);
-               inst.mesh[id]->Draw();
+               mesh->Draw();
        }
        for (const Part &part : children) {
                part.Render(transform, inst, prog);
        }
 }
 
-
-Skeletons::Skeletons()
-: skeletons() {
-
-}
-
-Skeletons::~Skeletons() {
-
-}
-
-void Skeletons::Load(const ShapeRegistry &shapes) {
-       skeletons.clear();
-       skeletons.reserve(4);
-       AABB bounds{{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f }};
-       const Shape *shape = &shapes.Get("player_head_block");
-       {
-               skeletons.emplace_back(new Model);
-               skeletons[0]->ID(1);
-               skeletons[0]->RootPart().bounds = bounds;
-               skeletons[0]->RootPart().shape = shape;
-               skeletons[0]->Enumerate();
-       }
-       {
-               skeletons.emplace_back(new Model);
-               skeletons[1]->ID(2);
-               skeletons[1]->RootPart().bounds = bounds;
-               skeletons[1]->RootPart().shape = shape;
-               skeletons[1]->Enumerate();
-       }
-       {
-               skeletons.emplace_back(new Model);
-               skeletons[2]->ID(3);
-               skeletons[2]->RootPart().bounds = bounds;
-               skeletons[2]->RootPart().shape = shape;
-               skeletons[2]->Enumerate();
-       }
-       {
-               skeletons.emplace_back(new Model);
-               skeletons[3]->ID(4);
-               skeletons[3]->RootPart().bounds = bounds;
-               skeletons[3]->RootPart().shape = shape;
-               skeletons[3]->Enumerate();
-       }
-}
-
-Model *Skeletons::ByID(std::uint16_t id) noexcept {
-       if (id == 0 || id > skeletons.size()) {
-               return nullptr;
-       } else {
-               return skeletons[id - 1].get();
-       }
-}
-
-const Model *Skeletons::ByID(std::uint16_t id) const noexcept {
-       if (id == 0 || id > skeletons.size()) {
-               return nullptr;
-       } else {
-               return skeletons[id - 1].get();
-       }
-}
-
 }