X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fmodel%2Fmodel.cpp;h=9a5801b2a10af3ce527cfae51f7aa66ed9c7c6c5;hb=HEAD;hp=632a5b64289533c343c18fd1a18ef35b28cf0ba2;hpb=bc2806164f55b7ac48dbb6d224b7d4b55683decf;p=blank.git diff --git a/src/model/model.cpp b/src/model/model.cpp index 632a5b6..9a5801b 100644 --- a/src/model/model.cpp +++ b/src/model/model.cpp @@ -1,14 +1,18 @@ #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 #include +#include namespace blank { @@ -19,15 +23,37 @@ Instance::Instance() } -void Instance::Render(const glm::mat4 &M, DirectionalLighting &prog) const { - model->RootPart().Render(M, state, prog); +Instance::~Instance() { + +} + +Part::State &Instance::BodyState() noexcept { + return state[model->GetBodyPart().ID()]; +} + +glm::mat4 Instance::BodyTransform() const noexcept { + return model->GetBodyPart().GlobalTransform(*this); +} + +Part::State &Instance::EyesState() noexcept { + return state[model->GetEyesPart().ID()]; +} + +glm::mat4 Instance::EyesTransform() const noexcept { + return model->GetEyesPart().GlobalTransform(*this); +} + +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) { } @@ -44,13 +70,48 @@ void Model::Instantiate(Instance &inst) const { } +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) } +: parent(nullptr) +, shape(nullptr) +, children() +, tex_map() +, mesh() , initial() -, mesh(nullptr) -, parent(nullptr) -, children() { +, hsl_mod(0, 255, 255) +, rgb_mod(255, 255, 255) +, id(0) { } @@ -58,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 = EntityMesh::ColorMod(color_conv * 255.0f); + } else if (name == "rgb_mod") { + in.ReadVec(color_conv); + rgb_mod = EntityMesh::ColorMod(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; @@ -79,135 +192,46 @@ void Part::Index(std::vector &index) noexcept { } } -glm::mat4 Part::LocalTransform( - const std::vector &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(glm::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 -) 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::Render( const glm::mat4 &M, - const std::vector &state, + const Instance &inst, DirectionalLighting &prog ) const { - glm::mat4 transform = M * LocalTransform(state); - if (mesh) { + 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, state, prog); - } -} - - -Skeletons::Skeletons() -: skeletons() -, meshes() { - -} - -Skeletons::~Skeletons() { - -} - -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]->RootPart().bounds = bounds; - skeletons[0]->Enumerate(); - } - { - skeletons.emplace_back(new Model); - skeletons[1]->ID(2); - skeletons[1]->RootPart().bounds = bounds; - skeletons[1]->Enumerate(); - } - { - skeletons.emplace_back(new Model); - skeletons[2]->ID(3); - skeletons[2]->RootPart().bounds = bounds; - skeletons[2]->Enumerate(); - } - { - skeletons.emplace_back(new Model); - skeletons[3]->ID(4); - skeletons[3]->RootPart().bounds = bounds; - 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 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; - } 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(); + part.Render(transform, inst, prog); } }