X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fmodel%2Fmodel.cpp;fp=src%2Fmodel%2Fmodel.cpp;h=63187b863033569af1da8f1e446ac3ec54607d69;hb=7e782291e0ce39eb2d4e8c1df28f682c313e6f8d;hp=730a34a330c4bd08c1336e2750039781f064c8ac;hpb=a26ca06878d45d3ce77cbc28b574f2553e121944;p=blank.git diff --git a/src/model/model.cpp b/src/model/model.cpp index 730a34a..63187b8 100644 --- a/src/model/model.cpp +++ b/src/model/model.cpp @@ -1,10 +1,12 @@ #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" @@ -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,50 +27,10 @@ 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 &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(); -} - Model::Model() : id(0) @@ -90,18 +48,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.0f, 1.0f, 1.0f) +, rgb_mod(1.0f, 1.0f, 1.0f) +, id(0) { } @@ -109,6 +101,55 @@ Part::~Part() { } +void Part::Read(TokenStreamReader &in, TextureIndex &tex_index, const ShapeRegistry &shapes) { + std::string name; + std::string shape_name; + std::string tex_name; + 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(hsl_mod); + } else if (name == "rgb_mod") { + in.ReadVec(rgb_mod); + } 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 +191,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(); - } -} - }