X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fmodel%2Fmodel.cpp;h=9a5801b2a10af3ce527cfae51f7aa66ed9c7c6c5;hb=HEAD;hp=423e00d842150202a6948cc6ec3ad4be879a12b9;hpb=b7d09e1e35ef90282c97509e0020b20db3c7ea9f;p=blank.git diff --git a/src/model/model.cpp b/src/model/model.cpp index 423e00d..9a5801b 100644 --- a/src/model/model.cpp +++ b/src/model/model.cpp @@ -1,290 +1,238 @@ -#include "BlockModel.hpp" #include "Model.hpp" -#include "OutlineModel.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 +#include +#include namespace blank { -Model::Model() noexcept -: va(0) -, handle{} -, count(0) { - glGenVertexArrays(1, &va); - glGenBuffers(ATTRIB_COUNT, handle); +Instance::Instance() +: model(nullptr) +, state() { + } -Model::~Model() noexcept { - glDeleteBuffers(ATTRIB_COUNT, handle); - glDeleteVertexArrays(1, &va); +Instance::~Instance() { + } -Model::Model(Model &&other) noexcept -: va(other.va) -, count(other.count) { - other.va = 0; - for (int i = 0; i < ATTRIB_COUNT; ++i) { - handle[i] = other.handle[i]; - other.handle[i] = 0; - } +Part::State &Instance::BodyState() noexcept { + return state[model->GetBodyPart().ID()]; } -Model &Model::operator =(Model &&other) noexcept { - std::swap(va, other.va); - for (int i = 0; i < ATTRIB_COUNT; ++i) { - std::swap(handle[i], other.handle[i]); - } - count = other.count; - return *this; +glm::mat4 Instance::BodyTransform() const noexcept { + return model->GetBodyPart().GlobalTransform(*this); } -void Model::Update(const Buffer &buf) noexcept { - glBindVertexArray(va); - glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_VERTEX]); - glBufferData(GL_ARRAY_BUFFER, buf.vertices.size() * sizeof(glm::vec3), buf.vertices.data(), GL_STATIC_DRAW); - glEnableVertexAttribArray(ATTRIB_VERTEX); - glVertexAttribPointer( - ATTRIB_VERTEX, // location (for shader) - 3, // size - GL_FLOAT, // type - GL_FALSE, // normalized - 0, // stride - nullptr // offset - ); - -#ifndef NDEBUG - if (buf.colors.size() < buf.vertices.size()) { - std::cerr << "Model: not enough colors!" << std::endl; - } -#endif - glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_COLOR]); - glBufferData(GL_ARRAY_BUFFER, buf.colors.size() * sizeof(glm::vec3), buf.colors.data(), GL_STATIC_DRAW); - glEnableVertexAttribArray(ATTRIB_COLOR); - glVertexAttribPointer( - ATTRIB_COLOR, // location (for shader) - 3, // size - GL_FLOAT, // type - GL_FALSE, // normalized - 0, // stride - nullptr // offset - ); - -#ifndef NDEBUG - if (buf.normals.size() < buf.vertices.size()) { - std::cerr << "Model: not enough normals!" << std::endl; - } -#endif - glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_NORMAL]); - glBufferData(GL_ARRAY_BUFFER, buf.normals.size() * sizeof(glm::vec3), buf.normals.data(), GL_STATIC_DRAW); - glEnableVertexAttribArray(ATTRIB_NORMAL); - glVertexAttribPointer( - ATTRIB_NORMAL, // location (for shader) - 3, // size - GL_FLOAT, // type - GL_FALSE, // normalized - 0, // stride - nullptr // offset - ); - - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, buf.indices.size() * sizeof(Index), buf.indices.data(), GL_STATIC_DRAW); - count = buf.indices.size(); +Part::State &Instance::EyesState() noexcept { + return state[model->GetEyesPart().ID()]; } +glm::mat4 Instance::EyesTransform() const noexcept { + return model->GetEyesPart().GlobalTransform(*this); +} -void Model::Draw() const noexcept { - glBindVertexArray(va); - glDrawElements( - GL_TRIANGLES, // how - count, // count - GL_UNSIGNED_INT, // type - nullptr // offset - ); +void Instance::Render(const glm::mat4 &M, DirectionalLighting &prog) { + model->RootPart().Render(M, *this, prog); } -BlockModel::BlockModel() noexcept -: va(0) -, handle{} -, count(0) { - glGenVertexArrays(1, &va); - glGenBuffers(ATTRIB_COUNT, handle); +Model::Model() +: id(0) +, root() +, part() +, body_id(0) +, eyes_id(0) { + } -BlockModel::~BlockModel() noexcept { - glDeleteBuffers(ATTRIB_COUNT, handle); - glDeleteVertexArrays(1, &va); +void Model::Enumerate() { + part.clear(); + part.resize(root.Enumerate(0), nullptr); + root.Index(part); } -BlockModel::BlockModel(BlockModel &&other) noexcept -: va(other.va) -, count(other.count) { - other.va = 0; - for (int i = 0; i < ATTRIB_COUNT; ++i) { - handle[i] = other.handle[i]; - other.handle[i] = 0; - } +void Model::Instantiate(Instance &inst) const { + inst.model = this; + inst.state.clear(); + inst.state.resize(part.size()); } -BlockModel &BlockModel::operator =(BlockModel &&other) noexcept { - std::swap(va, other.va); - for (int i = 0; i < ATTRIB_COUNT; ++i) { - std::swap(handle[i], other.handle[i]); - } - count = other.count; - return *this; + +ModelRegistry::ModelRegistry() +: models() +, name_index() { + } -void BlockModel::Update(const Buffer &buf) noexcept { - glBindVertexArray(va); - glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_VERTEX]); - glBufferData(GL_ARRAY_BUFFER, buf.vertices.size() * sizeof(glm::vec3), buf.vertices.data(), GL_STATIC_DRAW); - glEnableVertexAttribArray(ATTRIB_VERTEX); - glVertexAttribPointer( - ATTRIB_VERTEX, // location (for shader) - 3, // size - GL_FLOAT, // type - GL_FALSE, // normalized - 0, // stride - nullptr // offset - ); - -#ifndef NDEBUG - if (buf.colors.size() < buf.vertices.size()) { - std::cerr << "BlockModel: not enough colors!" << std::endl; +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); } -#endif - glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_COLOR]); - glBufferData(GL_ARRAY_BUFFER, buf.colors.size() * sizeof(glm::vec3), buf.colors.data(), GL_STATIC_DRAW); - glEnableVertexAttribArray(ATTRIB_COLOR); - glVertexAttribPointer( - ATTRIB_COLOR, // location (for shader) - 3, // size - GL_FLOAT, // type - GL_FALSE, // normalized - 0, // stride - nullptr // offset - ); - -#ifndef NDEBUG - if (buf.lights.size() < buf.vertices.size()) { - std::cerr << "BlockModel: not enough lights!" << std::endl; +} + +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); } -#endif - glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_LIGHT]); - glBufferData(GL_ARRAY_BUFFER, buf.lights.size() * sizeof(float), buf.lights.data(), GL_STATIC_DRAW); - glEnableVertexAttribArray(ATTRIB_LIGHT); - glVertexAttribPointer( - ATTRIB_LIGHT, // location (for shader) - 1, // size - GL_FLOAT, // type - GL_FALSE, // normalized - 0, // stride - nullptr // offset - ); - - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, buf.indices.size() * sizeof(Index), buf.indices.data(), GL_STATIC_DRAW); - count = buf.indices.size(); } -void BlockModel::Draw() const noexcept { - glBindVertexArray(va); - glDrawElements( - GL_TRIANGLES, // how - count, // count - GL_UNSIGNED_INT, // type - nullptr // offset - ); +Part::Part() +: parent(nullptr) +, shape(nullptr) +, children() +, tex_map() +, mesh() +, initial() +, hsl_mod(0, 255, 255) +, rgb_mod(255, 255, 255) +, id(0) { + +} + +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); } -OutlineModel::OutlineModel() noexcept -: vertices() -, colors() -, indices() -, va(0) -, handle{} -, dirty(false) { - glGenVertexArrays(1, &va); - glGenBuffers(ATTRIB_COUNT, handle); +Part &Part::AddChild() { + children.emplace_back(); + children.back().parent = this; + return children.back(); } -OutlineModel::~OutlineModel() noexcept { - glDeleteBuffers(ATTRIB_COUNT, handle); - glDeleteVertexArrays(1, &va); +std::uint16_t Part::Enumerate(std::uint16_t counter) noexcept { + id = counter++; + for (Part &part : children) { + counter = part.Enumerate(counter); + } + return counter; } - -void OutlineModel::Clear() noexcept { - vertices.clear(); - colors.clear(); - indices.clear(); - Invalidate(); +void Part::Index(std::vector &index) noexcept { + index[id] = this; + for (Part &part : children) { + part.Index(index); + } } -void OutlineModel::Reserve(int v, int i) { - vertices.reserve(v); - colors.reserve(v); - indices.reserve(i); +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; } - -void OutlineModel::Update() noexcept { - glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_VERTEX]); - glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), vertices.data(), GL_STATIC_DRAW); - glEnableVertexAttribArray(ATTRIB_VERTEX); - glVertexAttribPointer( - ATTRIB_VERTEX, // location (for shader) - 3, // size - GL_FLOAT, // type - GL_FALSE, // normalized - 0, // stride - nullptr // offset - ); - -#ifndef NDEBUG - if (colors.size() < vertices.size()) { - std::cerr << "OutlineModel: not enough colors!" << std::endl; - colors.resize(vertices.size(), { 1, 0, 1 }); +glm::mat4 Part::GlobalTransform(const Instance &inst) const noexcept { + if (parent) { + return parent->GlobalTransform(inst) * LocalTransform(inst); + } else { + return LocalTransform(inst); } -#endif - glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_COLOR]); - glBufferData(GL_ARRAY_BUFFER, colors.size() * sizeof(glm::vec3), colors.data(), GL_STATIC_DRAW); - glEnableVertexAttribArray(ATTRIB_COLOR); - glVertexAttribPointer( - ATTRIB_COLOR, // location (for shader) - 3, // size - GL_FLOAT, // type - GL_FALSE, // normalized - 0, // stride - nullptr // offset - ); - - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(Index), indices.data(), GL_STATIC_DRAW); - - dirty = false; } +namespace { -void OutlineModel::Draw() noexcept { - glBindVertexArray(va); - - if (dirty) { - Update(); - } +EntityMesh::Buffer buf; - glEnable(GL_LINE_SMOOTH); - glLineWidth(2.0f); +} - glDrawElements( - GL_LINES, // how - indices.size(), // count - GL_UNSIGNED_SHORT, // type - nullptr // offset - ); +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); + } } }