]> git.localhorst.tv Git - blank.git/blobdiff - src/model/model.cpp
store shapes in models rather than meshes
[blank.git] / src / model / model.cpp
index 632a5b64289533c343c18fd1a18ef35b28cf0ba2..730a34a330c4bd08c1336e2750039781f064c8ac 100644 (file)
@@ -8,19 +8,69 @@
 #include "../graphics/DirectionalLighting.hpp"
 #include "../graphics/EntityMesh.hpp"
 
+#include <iostream>
 #include <glm/gtx/quaternion.hpp>
+#include <glm/gtx/io.hpp>
 
 
 namespace blank {
 
 Instance::Instance()
 : model(nullptr)
-, state() {
+, state()
+, mesh()
+, tex_map()
+, hsl_mod(0.0f, 1.0f, 1.0f)
+, rgb_mod(1.0f) {
 
 }
 
-void Instance::Render(const glm::mat4 &M, DirectionalLighting &prog) const {
-       model->RootPart().Render(M, state, prog);
+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;
+}
+
+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);
+}
+
+void Instance::SetTextures(const std::vector<float> &t) {
+       tex_map = t;
+       mesh.clear();
+}
+
+void Instance::SetHSLModifier(const glm::vec3 &m) {
+       hsl_mod = m;
+       mesh.clear();
+}
+
+void Instance::SetRGBModifier(const glm::vec3 &m) {
+       rgb_mod = m;
+       mesh.clear();
 }
 
 
@@ -40,6 +90,7 @@ void Model::Enumerate() {
 void Model::Instantiate(Instance &inst) const {
        inst.model = this;
        inst.state.clear();
+       inst.mesh.clear();
        inst.state.resize(part.size());
 }
 
@@ -48,7 +99,7 @@ Part::Part()
 : id(0)
 , bounds{ glm::vec3(0.0f), glm::vec3(0.0f) }
 , initial()
-, mesh(nullptr)
+, shape(nullptr)
 , parent(nullptr)
 , children() {
 
@@ -79,43 +130,60 @@ void Part::Index(std::vector<Part *> &index) noexcept {
        }
 }
 
-glm::mat4 Part::LocalTransform(
-       const std::vector<State> &state
-) const noexcept {
-       glm::mat4 transform(toMat4(initial.orientation * state[id].orientation));
-       transform[3] = glm::vec4(initial.position + state[id].position, 1.0f);
+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 std::vector<State> &state
-) const noexcept {
+glm::mat4 Part::GlobalTransform(const Instance &inst) const noexcept {
        if (parent) {
-               return parent->GlobalTransform(state) * LocalTransform(state);
+               return parent->GlobalTransform(inst) * LocalTransform(inst);
        } else {
-               return LocalTransform(state);
+               return LocalTransform(inst);
+       }
+}
+
+namespace {
+
+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 std::vector<State> &state,
+       const Instance &inst,
        DirectionalLighting &prog
 ) const {
-       glm::mat4 transform = M * LocalTransform(state);
-       if (mesh) {
+       glm::mat4 transform = M * LocalTransform(inst);
+       if (inst.mesh[id]) {
                prog.SetM(transform);
-               mesh->Draw();
+               inst.mesh[id]->Draw();
        }
        for (const Part &part : children) {
-               part.Render(transform, state, prog);
+               part.Render(transform, inst, prog);
        }
 }
 
 
 Skeletons::Skeletons()
-: skeletons()
-, meshes() {
+: skeletons() {
 
 }
 
@@ -123,78 +191,41 @@ Skeletons::~Skeletons() {
 
 }
 
-void Skeletons::LoadHeadless() {
+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();
        }
 }
 
-void Skeletons::Load(const ShapeRegistry &shapes, TextureIndex &tex_index) {
-       LoadHeadless();
-       meshes.resize(4);
-       const Shape &shape = shapes.Get("player_head_block");
-       EntityMesh::Buffer buf;
-       std::vector<float> tex_map;
-       tex_map.push_back(tex_index.GetID("rock-1"));
-       tex_map.push_back(tex_index.GetID("rock-face"));
-       buf.Reserve(shape.VertexCount(), shape.IndexCount());
-       {
-               shape.Fill(buf, tex_map);
-               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]->RootPart().mesh = &meshes[0];
-       }
-       {
-               buf.Clear();
-               shape.Fill(buf, tex_map);
-               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]->RootPart().mesh = &meshes[1];
-       }
-       {
-               buf.Clear();
-               shape.Fill(buf, tex_map);
-               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]->RootPart().mesh = &meshes[2];
-       }
-       {
-               buf.Clear();
-               shape.Fill(buf, tex_map);
-               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]->RootPart().mesh = &meshes[3];
-       }
-}
-
 Model *Skeletons::ByID(std::uint16_t id) noexcept {
        if (id == 0 || id > skeletons.size()) {
                return nullptr;