#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 "../app/TextureIndex.hpp"
+#include "../io/TokenStreamReader.hpp"
#include "../graphics/DirectionalLighting.hpp"
#include "../graphics/EntityMesh.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();
}
-
-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;
+void Instance::Render(const glm::mat4 &M, DirectionalLighting &prog) {
+ model->RootPart().Render(M, *this, prog);
}
-glm::mat4 Model::GlobalTransform() const noexcept {
- if (HasParent()) {
- return Parent().GlobalTransform() * LocalTransform();
- } else {
- return LocalTransform();
- }
+
+Model::Model()
+: id(0)
+, root()
+, part() {
+
}
+void Model::Enumerate() {
+ part.clear();
+ part.resize(root.Enumerate(0), nullptr);
+ root.Index(part);
+}
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());
- }
+ inst.model = this;
+ inst.state.clear();
+ inst.state.resize(part.size());
}
-Instance::Instance()
-: part_model(nullptr)
-, parent(nullptr)
-, position(0.0f)
-, orientation(1.0f, 0.0f, 0.0f, 0.0f)
-, parts() {
+ModelRegistry::ModelRegistry()
+: models()
+, name_index() {
}
-
-Instance &Instance::AddPart() {
- parts.emplace_back();
- parts.back().parent = this;
- return parts.back();
+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();
}
-
-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;
+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);
+ }
}
-glm::mat4 Instance::GlobalTransform() const noexcept {
- if (HasParent()) {
- return Parent().GlobalTransform() * LocalTransform();
+const Model &ModelRegistry::Get(const std::string &name) const {
+ 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);
+Part::Part()
+: parent(nullptr)
+, shape(nullptr)
+, children()
+, tex_map()
+, mesh()
+, initial()
+, hsl_mod(0.0f, 1.0f, 1.0f)
+, rgb_mod(1.0f, 1.0f, 1.0f)
+, id(0) {
+
+}
+
+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);
}
-
-Skeletons::Skeletons()
-: skeletons()
-, meshes() {
-
+Part &Part::AddChild() {
+ children.emplace_back();
+ children.back().parent = this;
+ return children.back();
}
-Skeletons::~Skeletons() {
-
+std::uint16_t Part::Enumerate(std::uint16_t counter) noexcept {
+ id = counter++;
+ for (Part &part : children) {
+ counter = part.Enumerate(counter);
+ }
+ return counter;
}
-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);
+void Part::Index(std::vector<Part *> &index) noexcept {
+ index[id] = this;
+ for (Part &part : children) {
+ part.Index(index);
}
}
-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]);
- }
+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;
}
-Model *Skeletons::ByID(std::uint16_t id) noexcept {
- if (id == 0 || id > skeletons.size()) {
- return nullptr;
+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);
}
}