]> git.localhorst.tv Git - blank.git/blobdiff - src/model/model.cpp
save a little bandwidth
[blank.git] / src / model / model.cpp
index cc6e20e788d2ca6e721c943f871b209920dfdac5..ed443138e23e0d5746a551c25fd7e1f6956cf198 100644 (file)
-#include "BlockModel.hpp"
-#include "EntityModel.hpp"
-#include "OutlineModel.hpp"
-#include "SkyBoxModel.hpp"
-#include "SpriteModel.hpp"
+#include "Model.hpp"
+#include "ModelRegistry.hpp"
+#include "Instance.hpp"
+#include "Part.hpp"
+
+#include "Shape.hpp"
+#include "ShapeRegistry.hpp"
+#include "../io/TokenStreamReader.hpp"
+#include "../graphics/DirectionalLighting.hpp"
+#include "../graphics/EntityMesh.hpp"
+#include "../shared/ResourceIndex.hpp"
 
-#include "shapes.hpp"
-
-#include <algorithm>
 #include <iostream>
+#include <glm/gtx/quaternion.hpp>
+#include <glm/gtx/io.hpp>
 
 
 namespace blank {
 
-void EntityModel::Update(const Buffer &buf) noexcept {
-#ifndef NDEBUG
-       if (buf.tex_coords.size() < buf.vertices.size()) {
-               std::cerr << "EntityModel: not enough tex coords!" << std::endl;
-       }
-       if (buf.hsl_mods.size() < buf.vertices.size()) {
-               std::cerr << "BlockModel: not enough HSL modifiers!" << std::endl;
-       }
-       if (buf.rgb_mods.size() < buf.vertices.size()) {
-               std::cerr << "BlockModel: not enough RGB modifiers!" << std::endl;
-       }
-       if (buf.normals.size() < buf.vertices.size()) {
-               std::cerr << "EntityModel: not enough normals!" << std::endl;
-       }
-#endif
+Instance::Instance()
+: model(nullptr)
+, state() {
 
-       vao.Bind();
-       vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
-       vao.PushAttribute(ATTRIB_TEXCOORD, buf.tex_coords);
-       vao.PushAttribute(ATTRIB_HSL, buf.hsl_mods);
-       vao.PushAttribute(ATTRIB_RGB, buf.rgb_mods);
-       vao.PushAttribute(ATTRIB_NORMAL, buf.normals);
-       vao.PushIndices(ATTRIB_INDEX, buf.indices);
 }
 
+Instance::~Instance() {
 
-void EntityModel::Draw() const noexcept {
-       vao.DrawTriangleElements();
 }
 
+Part::State &Instance::BodyState() noexcept {
+       return state[model->GetBodyPart().ID()];
+}
 
-void BlockModel::Update(const Buffer &buf) noexcept {
-#ifndef NDEBUG
-       if (buf.tex_coords.size() < buf.vertices.size()) {
-               std::cerr << "BlockModel: not enough tex coords!" << std::endl;
-       }
-       if (buf.hsl_mods.size() < buf.vertices.size()) {
-               std::cerr << "BlockModel: not enough HSL modifiers!" << std::endl;
-       }
-       if (buf.rgb_mods.size() < buf.vertices.size()) {
-               std::cerr << "BlockModel: not enough RGB modifiers!" << std::endl;
-       }
-       if (buf.lights.size() < buf.vertices.size()) {
-               std::cerr << "BlockModel: not enough lights!" << std::endl;
-       }
-#endif
+glm::mat4 Instance::BodyTransform() const noexcept {
+       return model->GetBodyPart().GlobalTransform(*this);
+}
 
-       vao.Bind();
-       vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
-       vao.PushAttribute(ATTRIB_TEXCOORD, buf.tex_coords);
-       vao.PushAttribute(ATTRIB_HSL, buf.hsl_mods);
-       vao.PushAttribute(ATTRIB_RGB, buf.rgb_mods);
-       vao.PushAttribute(ATTRIB_LIGHT, buf.lights);
-       vao.PushIndices(ATTRIB_INDEX, buf.indices);
+Part::State &Instance::EyesState() noexcept {
+       return state[model->GetEyesPart().ID()];
 }
 
+glm::mat4 Instance::EyesTransform() const noexcept {
+       return model->GetEyesPart().GlobalTransform(*this);
+}
 
-void BlockModel::Draw() const noexcept {
-       vao.DrawTriangleElements();
+void Instance::Render(const glm::mat4 &M, DirectionalLighting &prog) {
+       model->RootPart().Render(M, *this, prog);
 }
 
 
-void OutlineModel::Update(const Buffer &buf) noexcept {
-#ifndef NDEBUG
-       if (buf.colors.size() < buf.vertices.size()) {
-               std::cerr << "OutlineModel: not enough colors!" << std::endl;
-       }
-#endif
+Model::Model()
+: id(0)
+, root()
+, part()
+, body_id(0)
+, eyes_id(0) {
 
-       vao.Bind();
-       vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
-       vao.PushAttribute(ATTRIB_COLOR, buf.colors);
-       vao.PushIndices(ATTRIB_INDEX, buf.indices);
 }
 
+void Model::Enumerate() {
+       part.clear();
+       part.resize(root.Enumerate(0), nullptr);
+       root.Index(part);
+}
 
-void OutlineModel::Draw() noexcept {
-       glEnable(GL_LINE_SMOOTH);
-       glLineWidth(2.0f);
-       vao.DrawLineElements();
+void Model::Instantiate(Instance &inst) const {
+       inst.model = this;
+       inst.state.clear();
+       inst.state.resize(part.size());
 }
 
 
-void SkyBoxModel::LoadUnitBox() {
-       Buffer buffer;
-       CuboidShape shape({{ -1, -1, -1 }, { 1, 1, 1 }});
-       shape.Vertices(buffer);
-       Update(buffer);
+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();
 }
 
-void SkyBoxModel::Update(const Buffer &buf) noexcept {
-       vao.Bind();
-       vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
-       vao.PushIndices(ATTRIB_INDEX, buf.indices);
+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);
+       }
 }
 
-void SkyBoxModel::Draw() const noexcept {
-       vao.DrawTriangleElements();
+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);
+       }
 }
 
 
-void SpriteModel::Buffer::LoadRect(
-       float w, float h,
-       const glm::vec2 &pivot,
-       const glm::vec2 &tex_begin,
-       const glm::vec2 &tex_end
-) {
-       Clear();
-       Reserve(4, 6);
+Part::Part()
+: parent(nullptr)
+, shape(nullptr)
+, children()
+, tex_map()
+, mesh()
+, initial()
+, hsl_mod(0, 255, 255)
+, rgb_mod(255, 255, 255)
+, id(0) {
 
-       vertices.emplace_back( -pivot.x,  -pivot.y, 0.0f);
-       vertices.emplace_back(w-pivot.x,  -pivot.y, 0.0f);
-       vertices.emplace_back( -pivot.x, h-pivot.y, 0.0f);
-       vertices.emplace_back(w-pivot.x, h-pivot.y, 0.0f);
+}
 
-       coords.emplace_back(tex_begin.x, tex_begin.y);
-       coords.emplace_back(tex_end.x,   tex_begin.y);
-       coords.emplace_back(tex_begin.x, tex_end.y);
-       coords.emplace_back(tex_end.x,   tex_end.y);
+Part::~Part() {
 
-       indices.assign({ 0, 2, 1, 1, 2, 3 });
 }
 
+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;
+       return children.back();
+}
+
+std::uint16_t Part::Enumerate(std::uint16_t counter) noexcept {
+       id = counter++;
+       for (Part &part : children) {
+               counter = part.Enumerate(counter);
+       }
+       return counter;
+}
 
-void SpriteModel::Update(const Buffer &buf) noexcept {
-#ifndef NDEBUG
-       if (buf.coords.size() < buf.vertices.size()) {
-               std::cerr << "SpriteModel: not enough coords!" << std::endl;
+void Part::Index(std::vector<Part *> &index) noexcept {
+       index[id] = this;
+       for (Part &part : children) {
+               part.Index(index);
        }
-#endif
+}
 
-       vao.Bind();
-       vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
-       vao.PushAttribute(ATTRIB_TEXCOORD, buf.coords);
-       vao.PushIndices(ATTRIB_INDEX, buf.indices);
+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 Instance &inst) const noexcept {
+       if (parent) {
+               return parent->GlobalTransform(inst) * LocalTransform(inst);
+       } else {
+               return LocalTransform(inst);
+       }
+}
+
+namespace {
+
+EntityMesh::Buffer buf;
 
-void SpriteModel::Draw() noexcept {
-       vao.DrawTriangleElements();
+}
+
+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);
+       }
 }
 
 }