From: Daniel Karbach Date: Mon, 12 Oct 2015 08:22:17 +0000 (+0200) Subject: model -> mesh X-Git-Url: http://git.localhorst.tv/?a=commitdiff_plain;h=3542823a1af7f5063d7cc8da84efa248eb889b8a;p=blank.git model -> mesh --- diff --git a/src/graphics/BlockMesh.hpp b/src/graphics/BlockMesh.hpp new file mode 100644 index 0000000..e62b601 --- /dev/null +++ b/src/graphics/BlockMesh.hpp @@ -0,0 +1,81 @@ +#ifndef BLANK_GRAPHICS_BLOCKMESH_HPP_ +#define BLANK_GRAPHICS_BLOCKMESH_HPP_ + +#include "VertexArray.hpp" + +#include +#include +#include + + +namespace blank { + +class BlockMesh { + +public: + using Position = glm::vec3; + using TexCoord = glm::vec3; + using ColorMod = glm::vec3; + using Light = float; + using Index = unsigned int; + + using Positions = std::vector; + using TexCoords = std::vector; + using ColorMods = std::vector; + using Lights = std::vector; + using Indices = std::vector; + + enum Attribute { + ATTRIB_VERTEX, + ATTRIB_TEXCOORD, + ATTRIB_HSL, + ATTRIB_RGB, + ATTRIB_LIGHT, + ATTRIB_INDEX, + ATTRIB_COUNT, + }; + + struct Buffer { + + Positions vertices; + TexCoords tex_coords; + ColorMods hsl_mods; + ColorMods rgb_mods; + Lights lights; + Indices indices; + + void Clear() noexcept { + vertices.clear(); + tex_coords.clear(); + hsl_mods.clear(); + rgb_mods.clear(); + lights.clear(); + indices.clear(); + } + + void Reserve(size_t p, size_t i) { + vertices.reserve(p); + tex_coords.reserve(p); + hsl_mods.reserve(p); + rgb_mods.reserve(p); + lights.reserve(p); + indices.reserve(i); + } + + }; + + using VAO = VertexArray; + +public: + void Update(const Buffer &) noexcept; + + void Draw() const noexcept; + +private: + VAO vao; + +}; + +} + +#endif diff --git a/src/graphics/EntityMesh.hpp b/src/graphics/EntityMesh.hpp new file mode 100644 index 0000000..6b894f9 --- /dev/null +++ b/src/graphics/EntityMesh.hpp @@ -0,0 +1,81 @@ +#ifndef BLANK_GRAPHICS_ENTITYMESH_HPP_ +#define BLANK_GRAPHICS_ENTITYMESH_HPP_ + +#include "VertexArray.hpp" + +#include +#include +#include + + +namespace blank { + +class EntityMesh { + +public: + using Position = glm::vec3; + using TexCoord = glm::vec3; + using ColorMod = glm::vec3; + using Normal = glm::vec3; + using Index = unsigned int; + + using Positions = std::vector; + using TexCoords = std::vector; + using ColorMods = std::vector; + using Normals = std::vector; + using Indices = std::vector; + + enum Attribute { + ATTRIB_VERTEX, + ATTRIB_TEXCOORD, + ATTRIB_HSL, + ATTRIB_RGB, + ATTRIB_NORMAL, + ATTRIB_INDEX, + ATTRIB_COUNT, + }; + + struct Buffer { + + Positions vertices; + TexCoords tex_coords; + ColorMods hsl_mods; + ColorMods rgb_mods; + Normals normals; + Indices indices; + + void Clear() noexcept { + vertices.clear(); + tex_coords.clear(); + hsl_mods.clear(); + rgb_mods.clear(); + normals.clear(); + indices.clear(); + } + + void Reserve(size_t p, size_t i) { + vertices.reserve(p); + tex_coords.reserve(p); + hsl_mods.reserve(p); + rgb_mods.reserve(p); + normals.reserve(p); + indices.reserve(i); + } + + }; + + using VAO = VertexArray; + +public: + void Update(const Buffer &) noexcept; + + void Draw() const noexcept; + +private: + VAO vao; + +}; + +} + +#endif diff --git a/src/graphics/OutlineMesh.hpp b/src/graphics/OutlineMesh.hpp new file mode 100644 index 0000000..77e0436 --- /dev/null +++ b/src/graphics/OutlineMesh.hpp @@ -0,0 +1,65 @@ +#ifndef BLANK_GRAPHICS_OUTLINEMESH_HPP_ +#define BLANK_GRAPHICS_OUTLINEMESH_HPP_ + +#include "VertexArray.hpp" + +#include +#include +#include + + +namespace blank { + +class OutlineMesh { + +public: + using Position = glm::vec3; + using Color = glm::vec3; + using Index = unsigned short; + + using Positions = std::vector; + using Colors = std::vector; + using Indices = std::vector; + + enum Attribute { + ATTRIB_VERTEX, + ATTRIB_COLOR, + ATTRIB_INDEX, + ATTRIB_COUNT, + }; + + struct Buffer { + + Positions vertices; + Colors colors; + Indices indices; + + void Clear() noexcept { + vertices.clear(); + colors.clear(); + indices.clear(); + } + + void Reserve(size_t p, size_t i) { + vertices.reserve(p); + colors.reserve(p); + indices.reserve(i); + } + + }; + + using VAO = VertexArray; + +public: + void Update(const Buffer &) noexcept; + + void Draw() noexcept; + +private: + VAO vao; + +}; + +} + +#endif diff --git a/src/graphics/SkyBox.hpp b/src/graphics/SkyBox.hpp index 1254fe2..c2bb6f8 100644 --- a/src/graphics/SkyBox.hpp +++ b/src/graphics/SkyBox.hpp @@ -2,7 +2,7 @@ #define BLANK_GRAPHICS_SKYBOX_HPP_ #include "CubeMap.hpp" -#include "../model/SkyBoxModel.hpp" +#include "SkyBoxMesh.hpp" namespace blank { @@ -18,7 +18,7 @@ public: private: CubeMap texture; - SkyBoxModel model; + SkyBoxMesh mesh; }; diff --git a/src/graphics/SkyBoxMesh.hpp b/src/graphics/SkyBoxMesh.hpp new file mode 100644 index 0000000..ab1da8d --- /dev/null +++ b/src/graphics/SkyBoxMesh.hpp @@ -0,0 +1,59 @@ +#ifndef BLANK_GRAPHICS_SKYBOXMESH_HPP_ +#define BLANK_GRAPHICS_SKYBOXMESH_HPP_ + +#include "VertexArray.hpp" + +#include +#include + + +namespace blank { + +class SkyBoxMesh { + +public: + using Position = glm::vec3; + using Index = unsigned int; + + using Positions = std::vector; + using Indices = std::vector; + + enum Attribute { + ATTRIB_VERTEX, + ATTRIB_INDEX, + ATTRIB_COUNT, + }; + + struct Buffer { + + Positions vertices; + Indices indices; + + void Clear() noexcept { + vertices.clear(); + indices.clear(); + } + + void Reserve(size_t p, size_t i) { + vertices.reserve(p); + indices.reserve(i); + } + + }; + + using VAO = VertexArray; + +public: + void LoadUnitBox(); + void Update(const Buffer &) noexcept; + + void Draw() const noexcept; + +private: + VAO vao; + +}; + +} + +#endif diff --git a/src/graphics/SpriteMesh.hpp b/src/graphics/SpriteMesh.hpp new file mode 100644 index 0000000..a7cfd64 --- /dev/null +++ b/src/graphics/SpriteMesh.hpp @@ -0,0 +1,72 @@ +#ifndef BLANK_GRPAHICS_SPRITEMESH_HPP_ +#define BLANK_GRPAHICS_SPRITEMESH_HPP_ + +#include "VertexArray.hpp" + +#include +#include +#include + + +namespace blank { + +class SpriteMesh { + +public: + using Position = glm::vec3; + using TexCoord = glm::vec2; + using Index = unsigned short; + + using Positions = std::vector; + using TexCoords = std::vector; + using Indices = std::vector; + + enum Attribute { + ATTRIB_VERTEX, + ATTRIB_TEXCOORD, + ATTRIB_INDEX, + ATTRIB_COUNT, + }; + + struct Buffer { + + Positions vertices; + TexCoords coords; + Indices indices; + + void Clear() noexcept { + vertices.clear(); + coords.clear(); + indices.clear(); + } + + void Reserve(size_t p, size_t i) { + vertices.reserve(p); + coords.reserve(p); + indices.reserve(i); + } + + void LoadRect( + float w, float h, + const glm::vec2 &pivot = glm::vec2(0.0f), + const glm::vec2 &tex_begin = glm::vec2(0.0f), + const glm::vec2 &tex_end = glm::vec2(1.0f, 1.0f) + ); + + }; + + using VAO = VertexArray; + +public: + void Update(const Buffer &) noexcept; + + void Draw() noexcept; + +private: + VAO vao; + +}; + +} + +#endif diff --git a/src/graphics/mesh.cpp b/src/graphics/mesh.cpp new file mode 100644 index 0000000..4b0f582 --- /dev/null +++ b/src/graphics/mesh.cpp @@ -0,0 +1,171 @@ +#include "BlockMesh.hpp" +#include "EntityMesh.hpp" +#include "OutlineMesh.hpp" +#include "SkyBoxMesh.hpp" +#include "SpriteMesh.hpp" + +#include +#include + + +namespace blank { + +void EntityMesh::Update(const Buffer &buf) noexcept { +#ifndef NDEBUG + if (buf.tex_coords.size() < buf.vertices.size()) { + std::cerr << "EntityMesh: not enough tex coords!" << std::endl; + } + if (buf.hsl_mods.size() < buf.vertices.size()) { + std::cerr << "BlockMesh: not enough HSL modifiers!" << std::endl; + } + if (buf.rgb_mods.size() < buf.vertices.size()) { + std::cerr << "BlockMesh: not enough RGB modifiers!" << std::endl; + } + if (buf.normals.size() < buf.vertices.size()) { + std::cerr << "EntityMesh: not enough normals!" << std::endl; + } +#endif + + 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); +} + + +void EntityMesh::Draw() const noexcept { + vao.DrawTriangleElements(); +} + + +void BlockMesh::Update(const Buffer &buf) noexcept { +#ifndef NDEBUG + if (buf.tex_coords.size() < buf.vertices.size()) { + std::cerr << "BlockMesh: not enough tex coords!" << std::endl; + } + if (buf.hsl_mods.size() < buf.vertices.size()) { + std::cerr << "BlockMesh: not enough HSL modifiers!" << std::endl; + } + if (buf.rgb_mods.size() < buf.vertices.size()) { + std::cerr << "BlockMesh: not enough RGB modifiers!" << std::endl; + } + if (buf.lights.size() < buf.vertices.size()) { + std::cerr << "BlockMesh: not enough lights!" << std::endl; + } +#endif + + 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); +} + + +void BlockMesh::Draw() const noexcept { + vao.DrawTriangleElements(); +} + + +void OutlineMesh::Update(const Buffer &buf) noexcept { +#ifndef NDEBUG + if (buf.colors.size() < buf.vertices.size()) { + std::cerr << "OutlineMesh: not enough colors!" << std::endl; + } +#endif + + vao.Bind(); + vao.PushAttribute(ATTRIB_VERTEX, buf.vertices); + vao.PushAttribute(ATTRIB_COLOR, buf.colors); + vao.PushIndices(ATTRIB_INDEX, buf.indices); +} + + +void OutlineMesh::Draw() noexcept { + glEnable(GL_LINE_SMOOTH); + glLineWidth(2.0f); + vao.DrawLineElements(); +} + + +void SkyBoxMesh::LoadUnitBox() { + Buffer buffer; + buffer.vertices = { + { 1.0f, 1.0f, 1.0f }, + { 1.0f, 1.0f, -1.0f }, + { 1.0f, -1.0f, 1.0f }, + { 1.0f, -1.0f, -1.0f }, + { -1.0f, 1.0f, 1.0f }, + { -1.0f, 1.0f, -1.0f }, + { -1.0f, -1.0f, 1.0f }, + { -1.0f, -1.0f, -1.0f }, + }; + buffer.indices = { + 5, 7, 3, 3, 1, 5, + 6, 7, 5, 5, 4, 6, + 3, 2, 0, 0, 1, 3, + 6, 4, 0, 0, 2, 6, + 5, 1, 0, 0, 4, 5, + 7, 6, 3, 3, 6, 2, + }; + Update(buffer); +} + +void SkyBoxMesh::Update(const Buffer &buf) noexcept { + vao.Bind(); + vao.PushAttribute(ATTRIB_VERTEX, buf.vertices); + vao.PushIndices(ATTRIB_INDEX, buf.indices); +} + +void SkyBoxMesh::Draw() const noexcept { + vao.DrawTriangleElements(); +} + + +void SpriteMesh::Buffer::LoadRect( + float w, float h, + const glm::vec2 &pivot, + const glm::vec2 &tex_begin, + const glm::vec2 &tex_end +) { + Clear(); + Reserve(4, 6); + + 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); + + indices.assign({ 0, 2, 1, 1, 2, 3 }); +} + + +void SpriteMesh::Update(const Buffer &buf) noexcept { +#ifndef NDEBUG + if (buf.coords.size() < buf.vertices.size()) { + std::cerr << "SpriteMesh: not enough coords!" << std::endl; + } +#endif + + vao.Bind(); + vao.PushAttribute(ATTRIB_VERTEX, buf.vertices); + vao.PushAttribute(ATTRIB_TEXCOORD, buf.coords); + vao.PushIndices(ATTRIB_INDEX, buf.indices); +} + + +void SpriteMesh::Draw() noexcept { + vao.DrawTriangleElements(); +} + +} diff --git a/src/graphics/viewport.cpp b/src/graphics/viewport.cpp index ddc609b..ad58b58 100644 --- a/src/graphics/viewport.cpp +++ b/src/graphics/viewport.cpp @@ -76,14 +76,14 @@ void Canvas::UpdateProjection() noexcept { SkyBox::SkyBox(CubeMap &&tex) : texture(std::move(tex)) -, model() { - model.LoadUnitBox(); +, mesh() { + mesh.LoadUnitBox(); } void SkyBox::Render(Viewport &viewport) noexcept { SkyBoxShader &prog = viewport.SkyBoxProgram(); prog.SetTexture(texture); - model.Draw(); + mesh.Draw(); } diff --git a/src/io/WorldSave.cpp b/src/io/WorldSave.cpp index f45410b..6414dd0 100644 --- a/src/io/WorldSave.cpp +++ b/src/io/WorldSave.cpp @@ -177,7 +177,7 @@ void WorldSave::Read(Chunk &chunk) const { if (gzclose(file) != Z_OK) { throw runtime_error("failed to read chunk file"); } - chunk.InvalidateModel(); + chunk.InvalidateMesh(); chunk.ClearSave(); } diff --git a/src/model/BlockModel.hpp b/src/model/BlockModel.hpp deleted file mode 100644 index 0c1543b..0000000 --- a/src/model/BlockModel.hpp +++ /dev/null @@ -1,81 +0,0 @@ -#ifndef BLANK_MODEL_BLOCKMODEL_HPP_ -#define BLANK_MODEL_BLOCKMODEL_HPP_ - -#include "../graphics/VertexArray.hpp" - -#include -#include -#include - - -namespace blank { - -class BlockModel { - -public: - using Position = glm::vec3; - using TexCoord = glm::vec3; - using ColorMod = glm::vec3; - using Light = float; - using Index = unsigned int; - - using Positions = std::vector; - using TexCoords = std::vector; - using ColorMods = std::vector; - using Lights = std::vector; - using Indices = std::vector; - - enum Attribute { - ATTRIB_VERTEX, - ATTRIB_TEXCOORD, - ATTRIB_HSL, - ATTRIB_RGB, - ATTRIB_LIGHT, - ATTRIB_INDEX, - ATTRIB_COUNT, - }; - - struct Buffer { - - Positions vertices; - TexCoords tex_coords; - ColorMods hsl_mods; - ColorMods rgb_mods; - Lights lights; - Indices indices; - - void Clear() noexcept { - vertices.clear(); - tex_coords.clear(); - hsl_mods.clear(); - rgb_mods.clear(); - lights.clear(); - indices.clear(); - } - - void Reserve(size_t p, size_t i) { - vertices.reserve(p); - tex_coords.reserve(p); - hsl_mods.reserve(p); - rgb_mods.reserve(p); - lights.reserve(p); - indices.reserve(i); - } - - }; - - using VAO = VertexArray; - -public: - void Update(const Buffer &) noexcept; - - void Draw() const noexcept; - -private: - VAO vao; - -}; - -} - -#endif diff --git a/src/model/CompositeModel.hpp b/src/model/CompositeModel.hpp index 947db01..4b3d2a9 100644 --- a/src/model/CompositeModel.hpp +++ b/src/model/CompositeModel.hpp @@ -12,7 +12,7 @@ namespace blank { class CompositeInstance; -class EntityModel; +class EntityMesh; class CompositeModel { @@ -34,10 +34,10 @@ public: const glm::quat &Orientation() const noexcept { return orientation; } void Orientation(const glm::quat &o) noexcept { orientation = o; } - bool HasNodeModel() const noexcept { return node_model; } - void SetNodeModel(const EntityModel *m) noexcept { node_model = m; } + bool HasNodeMesh() const noexcept { return node_mesh; } + void SetNodeMesh(const EntityMesh *m) noexcept { node_mesh = m; } - const EntityModel &NodeModel() const noexcept { return *node_model; } + const EntityMesh &NodeMesh() const noexcept { return *node_mesh; } CompositeModel &AddPart(); bool HasParent() const noexcept { return parent; } @@ -51,7 +51,7 @@ public: private: CompositeModel *parent; - const EntityModel *node_model; + const EntityMesh *node_mesh; std::uint32_t id; diff --git a/src/model/EntityModel.hpp b/src/model/EntityModel.hpp deleted file mode 100644 index a89d00b..0000000 --- a/src/model/EntityModel.hpp +++ /dev/null @@ -1,81 +0,0 @@ -#ifndef BLANK_MODEL_ENTITYMODEL_HPP_ -#define BLANK_MODEL_ENTITYMODEL_HPP_ - -#include "../graphics/VertexArray.hpp" - -#include -#include -#include - - -namespace blank { - -class EntityModel { - -public: - using Position = glm::vec3; - using TexCoord = glm::vec3; - using ColorMod = glm::vec3; - using Normal = glm::vec3; - using Index = unsigned int; - - using Positions = std::vector; - using TexCoords = std::vector; - using ColorMods = std::vector; - using Normals = std::vector; - using Indices = std::vector; - - enum Attribute { - ATTRIB_VERTEX, - ATTRIB_TEXCOORD, - ATTRIB_HSL, - ATTRIB_RGB, - ATTRIB_NORMAL, - ATTRIB_INDEX, - ATTRIB_COUNT, - }; - - struct Buffer { - - Positions vertices; - TexCoords tex_coords; - ColorMods hsl_mods; - ColorMods rgb_mods; - Normals normals; - Indices indices; - - void Clear() noexcept { - vertices.clear(); - tex_coords.clear(); - hsl_mods.clear(); - rgb_mods.clear(); - normals.clear(); - indices.clear(); - } - - void Reserve(size_t p, size_t i) { - vertices.reserve(p); - tex_coords.reserve(p); - hsl_mods.reserve(p); - rgb_mods.reserve(p); - normals.reserve(p); - indices.reserve(i); - } - - }; - - using VAO = VertexArray; - -public: - void Update(const Buffer &) noexcept; - - void Draw() const noexcept; - -private: - VAO vao; - -}; - -} - -#endif diff --git a/src/model/OutlineModel.hpp b/src/model/OutlineModel.hpp deleted file mode 100644 index ba85fc8..0000000 --- a/src/model/OutlineModel.hpp +++ /dev/null @@ -1,65 +0,0 @@ -#ifndef BLANK_MODEL_OUTLINEMODEL_HPP_ -#define BLANK_MODEL_OUTLINEMODEL_HPP_ - -#include "../graphics/VertexArray.hpp" - -#include -#include -#include - - -namespace blank { - -class OutlineModel { - -public: - using Position = glm::vec3; - using Color = glm::vec3; - using Index = unsigned short; - - using Positions = std::vector; - using Colors = std::vector; - using Indices = std::vector; - - enum Attribute { - ATTRIB_VERTEX, - ATTRIB_COLOR, - ATTRIB_INDEX, - ATTRIB_COUNT, - }; - - struct Buffer { - - Positions vertices; - Colors colors; - Indices indices; - - void Clear() noexcept { - vertices.clear(); - colors.clear(); - indices.clear(); - } - - void Reserve(size_t p, size_t i) { - vertices.reserve(p); - colors.reserve(p); - indices.reserve(i); - } - - }; - - using VAO = VertexArray; - -public: - void Update(const Buffer &) noexcept; - - void Draw() noexcept; - -private: - VAO vao; - -}; - -} - -#endif diff --git a/src/model/Shape.hpp b/src/model/Shape.hpp index 312b1aa..d2e0695 100644 --- a/src/model/Shape.hpp +++ b/src/model/Shape.hpp @@ -1,10 +1,9 @@ #ifndef BLANK_MODEL_SHAPE_HPP_ #define BLANK_MODEL_SHAPE_HPP_ -#include "BlockModel.hpp" -#include "EntityModel.hpp" -#include "OutlineModel.hpp" -#include "SkyBoxModel.hpp" +#include "../graphics/BlockMesh.hpp" +#include "../graphics/EntityMesh.hpp" +#include "../graphics/OutlineMesh.hpp" #include @@ -21,33 +20,30 @@ struct Shape { /// the number of vertex indices this shape has size_t VertexIndexCount() const noexcept { return vtx_idx.size(); } - const EntityModel::Normal &VertexNormal(size_t idx) const noexcept { return vtx_nrm[idx]; } - EntityModel::Normal VertexNormal( + const EntityMesh::Normal &VertexNormal(size_t idx) const noexcept { return vtx_nrm[idx]; } + EntityMesh::Normal VertexNormal( size_t idx, const glm::mat4 &transform ) const noexcept { - return EntityModel::Normal(transform * glm::vec4(vtx_nrm[idx], 0.0f)); + return EntityMesh::Normal(transform * glm::vec4(vtx_nrm[idx], 0.0f)); } /// fill given buffers with this shape's elements with an /// optional transform and offset void Vertices( - EntityModel::Buffer &out, + EntityMesh::Buffer &out, float tex_offset = 0.0f ) const; void Vertices( - EntityModel::Buffer &out, + EntityMesh::Buffer &out, const glm::mat4 &transform, float tex_offset = 0.0f, - EntityModel::Index idx_offset = 0 + EntityMesh::Index idx_offset = 0 ) const; void Vertices( - BlockModel::Buffer &out, + BlockMesh::Buffer &out, const glm::mat4 &transform, float tex_offset = 0.0f, - BlockModel::Index idx_offset = 0 - ) const; - void Vertices( - SkyBoxModel::Buffer &out + BlockMesh::Index idx_offset = 0 ) const; /// the number of vertices this shape's outline has @@ -56,7 +52,7 @@ struct Shape { size_t OutlineIndexCount() const { return out_idx.size(); } /// fill given buffers with this shape's outline's elements - void Outline(OutlineModel::Buffer &out) const; + void Outline(OutlineMesh::Buffer &out) const; /// Check if given ray would pass though this shape if it were /// transformed with given matrix. @@ -81,24 +77,24 @@ struct Shape { protected: void SetShape( - const EntityModel::Positions &pos, - const EntityModel::Normals &nrm, - const EntityModel::Indices &idx); + const EntityMesh::Positions &pos, + const EntityMesh::Normals &nrm, + const EntityMesh::Indices &idx); void SetTexture( - const BlockModel::TexCoords &tex_coords); + const BlockMesh::TexCoords &tex_coords); void SetOutline( - const OutlineModel::Positions &pos, - const OutlineModel::Indices &idx); + const OutlineMesh::Positions &pos, + const OutlineMesh::Indices &idx); private: - EntityModel::Positions vtx_pos; - EntityModel::Normals vtx_nrm; - EntityModel::Indices vtx_idx; + EntityMesh::Positions vtx_pos; + EntityMesh::Normals vtx_nrm; + EntityMesh::Indices vtx_idx; - BlockModel::TexCoords vtx_tex_coords; + BlockMesh::TexCoords vtx_tex_coords; - OutlineModel::Positions out_pos; - OutlineModel::Indices out_idx; + OutlineMesh::Positions out_pos; + OutlineMesh::Indices out_idx; }; diff --git a/src/model/Skeletons.hpp b/src/model/Skeletons.hpp index 131bf0c..250ebce 100644 --- a/src/model/Skeletons.hpp +++ b/src/model/Skeletons.hpp @@ -9,7 +9,7 @@ namespace blank { class CompositeModel; -class EntityModel; +class EntityMesh; class Skeletons { @@ -35,7 +35,7 @@ public: private: std::vector> skeletons; - std::vector models; + std::vector meshes; }; diff --git a/src/model/SkyBoxModel.hpp b/src/model/SkyBoxModel.hpp deleted file mode 100644 index 84555ee..0000000 --- a/src/model/SkyBoxModel.hpp +++ /dev/null @@ -1,59 +0,0 @@ -#ifndef BLANK_MODEL_SKYBOXMODEL_HPP_ -#define BLANK_MODEL_SKYBOXMODEL_HPP_ - -#include "../graphics/VertexArray.hpp" - -#include -#include - - -namespace blank { - -class SkyBoxModel { - -public: - using Position = glm::vec3; - using Index = unsigned int; - - using Positions = std::vector; - using Indices = std::vector; - - enum Attribute { - ATTRIB_VERTEX, - ATTRIB_INDEX, - ATTRIB_COUNT, - }; - - struct Buffer { - - Positions vertices; - Indices indices; - - void Clear() noexcept { - vertices.clear(); - indices.clear(); - } - - void Reserve(size_t p, size_t i) { - vertices.reserve(p); - indices.reserve(i); - } - - }; - - using VAO = VertexArray; - -public: - void LoadUnitBox(); - void Update(const Buffer &) noexcept; - - void Draw() const noexcept; - -private: - VAO vao; - -}; - -} - -#endif diff --git a/src/model/SpriteModel.hpp b/src/model/SpriteModel.hpp deleted file mode 100644 index c5670fc..0000000 --- a/src/model/SpriteModel.hpp +++ /dev/null @@ -1,72 +0,0 @@ -#ifndef BLANK_MODEL_SPRITEMODEL_HPP_ -#define BLANK_MODEL_SPRITEMODEL_HPP_ - -#include "../graphics/VertexArray.hpp" - -#include -#include -#include - - -namespace blank { - -class SpriteModel { - -public: - using Position = glm::vec3; - using TexCoord = glm::vec2; - using Index = unsigned short; - - using Positions = std::vector; - using TexCoords = std::vector; - using Indices = std::vector; - - enum Attribute { - ATTRIB_VERTEX, - ATTRIB_TEXCOORD, - ATTRIB_INDEX, - ATTRIB_COUNT, - }; - - struct Buffer { - - Positions vertices; - TexCoords coords; - Indices indices; - - void Clear() noexcept { - vertices.clear(); - coords.clear(); - indices.clear(); - } - - void Reserve(size_t p, size_t i) { - vertices.reserve(p); - coords.reserve(p); - indices.reserve(i); - } - - void LoadRect( - float w, float h, - const glm::vec2 &pivot = glm::vec2(0.0f), - const glm::vec2 &tex_begin = glm::vec2(0.0f), - const glm::vec2 &tex_end = glm::vec2(1.0f, 1.0f) - ); - - }; - - using VAO = VertexArray; - -public: - void Update(const Buffer &) noexcept; - - void Draw() noexcept; - -private: - VAO vao; - -}; - -} - -#endif diff --git a/src/model/composite.cpp b/src/model/composite.cpp index b2e7022..8507770 100644 --- a/src/model/composite.cpp +++ b/src/model/composite.cpp @@ -2,9 +2,9 @@ #include "CompositeInstance.hpp" #include "Skeletons.hpp" -#include "EntityModel.hpp" #include "shapes.hpp" #include "../graphics/DirectionalLighting.hpp" +#include "../graphics/EntityMesh.hpp" #include @@ -13,7 +13,7 @@ namespace blank { CompositeModel::CompositeModel() : parent(nullptr) -, node_model(nullptr) +, node_mesh(nullptr) , id(0) , bounds{{ 0.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 0.0f }} , position(0.0f) @@ -95,9 +95,9 @@ glm::mat4 CompositeInstance::GlobalTransform() const noexcept { void CompositeInstance::Render(const glm::mat4 &M, DirectionalLighting &prog) const { glm::mat4 transform(M * LocalTransform()); - if (part_model->HasNodeModel()) { + if (part_model->HasNodeMesh()) { prog.SetM(transform); - part_model->NodeModel().Draw(); + part_model->NodeMesh().Draw(); } for (const CompositeInstance &part : parts) { part.Render(transform, prog); @@ -107,7 +107,7 @@ void CompositeInstance::Render(const glm::mat4 &M, DirectionalLighting &prog) co Skeletons::Skeletons() : skeletons() -, models() { +, meshes() { } @@ -146,15 +146,15 @@ void Skeletons::LoadHeadless() { void Skeletons::Load() { LoadHeadless(); - models.resize(4); - EntityModel::Buffer buf; + meshes.resize(4); + EntityMesh::Buffer buf; { CuboidShape 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 }); - models[0].Update(buf); - skeletons[0]->SetNodeModel(&models[0]); + meshes[0].Update(buf); + skeletons[0]->SetNodeMesh(&meshes[0]); } { CuboidShape shape(skeletons[1]->Bounds()); @@ -162,8 +162,8 @@ void Skeletons::Load() { 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 }); - models[1].Update(buf); - skeletons[1]->SetNodeModel(&models[1]); + meshes[1].Update(buf); + skeletons[1]->SetNodeMesh(&meshes[1]); } { StairShape shape(skeletons[2]->Bounds(), { 0.4f, 0.4f }); @@ -171,8 +171,8 @@ void Skeletons::Load() { 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 }); - models[2].Update(buf); - skeletons[2]->SetNodeModel(&models[2]); + meshes[2].Update(buf); + skeletons[2]->SetNodeMesh(&meshes[2]); } { CuboidShape shape(skeletons[3]->Bounds()); @@ -180,8 +180,8 @@ void Skeletons::Load() { 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 }); - models[3].Update(buf); - skeletons[3]->SetNodeModel(&models[3]); + meshes[3].Update(buf); + skeletons[3]->SetNodeMesh(&meshes[3]); } } diff --git a/src/model/model.cpp b/src/model/model.cpp deleted file mode 100644 index cc6e20e..0000000 --- a/src/model/model.cpp +++ /dev/null @@ -1,157 +0,0 @@ -#include "BlockModel.hpp" -#include "EntityModel.hpp" -#include "OutlineModel.hpp" -#include "SkyBoxModel.hpp" -#include "SpriteModel.hpp" - -#include "shapes.hpp" - -#include -#include - - -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 - - 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); -} - - -void EntityModel::Draw() const noexcept { - vao.DrawTriangleElements(); -} - - -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 - - 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); -} - - -void BlockModel::Draw() const noexcept { - vao.DrawTriangleElements(); -} - - -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 - - vao.Bind(); - vao.PushAttribute(ATTRIB_VERTEX, buf.vertices); - vao.PushAttribute(ATTRIB_COLOR, buf.colors); - vao.PushIndices(ATTRIB_INDEX, buf.indices); -} - - -void OutlineModel::Draw() noexcept { - glEnable(GL_LINE_SMOOTH); - glLineWidth(2.0f); - vao.DrawLineElements(); -} - - -void SkyBoxModel::LoadUnitBox() { - Buffer buffer; - CuboidShape shape({{ -1, -1, -1 }, { 1, 1, 1 }}); - shape.Vertices(buffer); - Update(buffer); -} - -void SkyBoxModel::Update(const Buffer &buf) noexcept { - vao.Bind(); - vao.PushAttribute(ATTRIB_VERTEX, buf.vertices); - vao.PushIndices(ATTRIB_INDEX, buf.indices); -} - -void SkyBoxModel::Draw() const noexcept { - vao.DrawTriangleElements(); -} - - -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); - - 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); - - indices.assign({ 0, 2, 1, 1, 2, 3 }); -} - - -void SpriteModel::Update(const Buffer &buf) noexcept { -#ifndef NDEBUG - if (buf.coords.size() < buf.vertices.size()) { - std::cerr << "SpriteModel: not enough coords!" << std::endl; - } -#endif - - vao.Bind(); - vao.PushAttribute(ATTRIB_VERTEX, buf.vertices); - vao.PushAttribute(ATTRIB_TEXCOORD, buf.coords); - vao.PushIndices(ATTRIB_INDEX, buf.indices); -} - - -void SpriteModel::Draw() noexcept { - vao.DrawTriangleElements(); -} - -} diff --git a/src/model/shape.cpp b/src/model/shape.cpp index 7422f89..d69046e 100644 --- a/src/model/shape.cpp +++ b/src/model/shape.cpp @@ -5,7 +5,7 @@ namespace blank { void Shape::Vertices( - EntityModel::Buffer &out, + EntityMesh::Buffer &out, float tex_offset ) const { for (const auto &pos : vtx_pos) { @@ -23,10 +23,10 @@ void Shape::Vertices( } void Shape::Vertices( - EntityModel::Buffer &out, + EntityMesh::Buffer &out, const glm::mat4 &transform, float tex_offset, - EntityModel::Index idx_offset + EntityMesh::Index idx_offset ) const { for (const auto &pos : vtx_pos) { out.vertices.emplace_back(transform * glm::vec4(pos, 1.0f)); @@ -43,10 +43,10 @@ void Shape::Vertices( } void Shape::Vertices( - BlockModel::Buffer &out, + BlockMesh::Buffer &out, const glm::mat4 &transform, float tex_offset, - BlockModel::Index idx_offset + BlockMesh::Index idx_offset ) const { for (const auto &pos : vtx_pos) { out.vertices.emplace_back(transform * glm::vec4(pos, 1.0f)); @@ -59,26 +59,15 @@ void Shape::Vertices( } } -void Shape::Vertices( - SkyBoxModel::Buffer &out -) const { - for (const auto &pos : vtx_pos) { - out.vertices.emplace_back(pos); - } - for (auto idx : vtx_idx) { - out.indices.emplace_back(idx); - } -} - -void Shape::Outline(OutlineModel::Buffer &out) const { +void Shape::Outline(OutlineMesh::Buffer &out) const { out.vertices.insert(out.vertices.end(), out_pos.begin(), out_pos.end()); out.indices.insert(out.indices.end(), out_idx.begin(), out_idx.end()); } void Shape::SetShape( - const EntityModel::Positions &pos, - const EntityModel::Normals &nrm, - const EntityModel::Indices &idx + const EntityMesh::Positions &pos, + const EntityMesh::Normals &nrm, + const EntityMesh::Indices &idx ) { vtx_pos = pos; vtx_nrm = nrm; @@ -86,14 +75,14 @@ void Shape::SetShape( } void Shape::SetTexture( - const BlockModel::TexCoords &tex_coords + const BlockMesh::TexCoords &tex_coords ) { vtx_tex_coords = tex_coords; } void Shape::SetOutline( - const OutlineModel::Positions &pos, - const OutlineModel::Indices &idx + const OutlineMesh::Positions &pos, + const OutlineMesh::Indices &idx ) { out_pos = pos; out_idx = idx; diff --git a/src/ui/HUD.hpp b/src/ui/HUD.hpp index d15643f..32a167b 100644 --- a/src/ui/HUD.hpp +++ b/src/ui/HUD.hpp @@ -3,8 +3,8 @@ #include "FixedText.hpp" #include "MessageBox.hpp" -#include "../model/EntityModel.hpp" -#include "../model/OutlineModel.hpp" +#include "../graphics/EntityMesh.hpp" +#include "../graphics/OutlineMesh.hpp" #include @@ -57,13 +57,13 @@ private: const Player &player; // block focus - OutlineModel outline; + OutlineMesh outline; glm::mat4 outline_transform; bool outline_visible; // "inventory" - EntityModel block; - EntityModel::Buffer block_buf; + EntityMesh block; + EntityMesh::Buffer block_buf; glm::mat4 block_transform; FixedText block_label; bool block_visible; @@ -82,7 +82,7 @@ private: IntervalTimer msg_timer; // crosshair - OutlineModel crosshair; + OutlineMesh crosshair; }; diff --git a/src/ui/Text.hpp b/src/ui/Text.hpp index 6c7c6a5..bdeb304 100644 --- a/src/ui/Text.hpp +++ b/src/ui/Text.hpp @@ -3,7 +3,7 @@ #include "../graphics/align.hpp" #include "../graphics/Texture.hpp" -#include "../model/SpriteModel.hpp" +#include "../graphics/SpriteMesh.hpp" #include #include @@ -36,7 +36,7 @@ private: private: Texture tex; - SpriteModel sprite; + SpriteMesh sprite; glm::vec2 size; Gravity pivot; bool dirty; diff --git a/src/ui/ui.cpp b/src/ui/ui.cpp index 4dd9711..c249ed4 100644 --- a/src/ui/ui.cpp +++ b/src/ui/ui.cpp @@ -250,12 +250,12 @@ HUD::HUD(Environment &env, Config &config, const Player &player) messages.Background(glm::vec4(0.5f)); // crosshair - OutlineModel::Buffer buf; + OutlineMesh::Buffer buf; buf.vertices = std::vector({ { -10.0f, 0.0f, 0.0f }, { 10.0f, 0.0f, 0.0f }, { 0.0f, -10.0f, 0.0f }, { 0.0f, 10.0f, 0.0f }, }); - buf.indices = std::vector({ + buf.indices = std::vector({ 0, 1, 2, 3 }); buf.colors.resize(4, { 10.0f, 10.0f, 10.0f }); @@ -264,7 +264,7 @@ HUD::HUD(Environment &env, Config &config, const Player &player) namespace { -OutlineModel::Buffer outl_buf; +OutlineMesh::Buffer outl_buf; } @@ -272,7 +272,7 @@ void HUD::FocusBlock(const Chunk &chunk, int index) { const Block &block = chunk.BlockAt(index); const BlockType &type = chunk.Type(index); outl_buf.Clear(); - type.FillOutlineModel(outl_buf); + type.FillOutlineMesh(outl_buf); outline.Update(outl_buf); outline_transform = chunk.Transform(player.GetEntity().ChunkCoords()); outline_transform *= chunk.ToTransform(Chunk::ToPos(index), index); @@ -312,7 +312,7 @@ void HUD::DisplayNone() { void HUD::Display(const BlockType &type) { block_buf.Clear(); - type.FillEntityModel(block_buf); + type.FillEntityMesh(block_buf); block.Update(block_buf); block_label.Set(env.assets.small_ui_font, type.label); diff --git a/src/ui/widgets.cpp b/src/ui/widgets.cpp index 852d2f1..b19dbcf 100644 --- a/src/ui/widgets.cpp +++ b/src/ui/widgets.cpp @@ -110,7 +110,7 @@ void Text::Set(const Font &font, const char *text) { namespace { -SpriteModel::Buffer sprite_buf; +SpriteMesh::Buffer sprite_buf; } diff --git a/src/world/BlockType.hpp b/src/world/BlockType.hpp index a27b89b..5fab931 100644 --- a/src/world/BlockType.hpp +++ b/src/world/BlockType.hpp @@ -2,9 +2,9 @@ #define BLANK_WORLD_BLOCKTYPE_HPP_ #include "Block.hpp" -#include "../model/BlockModel.hpp" -#include "../model/EntityModel.hpp" -#include "../model/OutlineModel.hpp" +#include "../graphics/BlockMesh.hpp" +#include "../graphics/EntityMesh.hpp" +#include "../graphics/OutlineMesh.hpp" #include "../model/shapes.hpp" #include @@ -81,17 +81,17 @@ struct BlockType { return fill[block.OrientedFace(face)]; } - void FillEntityModel( - EntityModel::Buffer &m, + void FillEntityMesh( + EntityMesh::Buffer &m, const glm::mat4 &transform = glm::mat4(1.0f), - EntityModel::Index idx_offset = 0 + EntityMesh::Index idx_offset = 0 ) const noexcept; - void FillBlockModel( - BlockModel::Buffer &m, + void FillBlockMesh( + BlockMesh::Buffer &m, const glm::mat4 &transform = glm::mat4(1.0f), - BlockModel::Index idx_offset = 0 + BlockMesh::Index idx_offset = 0 ) const noexcept; - void FillOutlineModel(OutlineModel::Buffer &m) const noexcept; + void FillOutlineMesh(OutlineMesh::Buffer &m) const noexcept; }; diff --git a/src/world/Chunk.hpp b/src/world/Chunk.hpp index cc0ae8e..eaed28a 100644 --- a/src/world/Chunk.hpp +++ b/src/world/Chunk.hpp @@ -127,7 +127,7 @@ public: int GetLight(const Pos &pos) const noexcept { return GetLight(ToIndex(pos)); } int GetLight(const Block::Pos &pos) const noexcept { return GetLight(ToIndex(pos)); } - float GetVertexLight(const Pos &, const BlockModel::Position &, const EntityModel::Normal &) const noexcept; + float GetVertexLight(const Pos &, const BlockMesh::Position &, const EntityMesh::Normal &) const noexcept; bool Intersection( const Ray &ray, @@ -167,14 +167,14 @@ public: void UnRef() noexcept { --ref_count; } bool Referenced() const noexcept { return ref_count > 0; } - void Invalidate() noexcept { dirty_model = dirty_save = true; } - void InvalidateModel() noexcept { dirty_model = true; } - void ClearModel() noexcept { dirty_model = false; } + void Invalidate() noexcept { dirty_mesh = dirty_save = true; } + void InvalidateMesh() noexcept { dirty_mesh = true; } + void ClearMesh() noexcept { dirty_mesh = false; } void ClearSave() noexcept { dirty_save = false; } - bool ShouldUpdateModel() const noexcept { return dirty_model; } + bool ShouldUpdateMesh() const noexcept { return dirty_mesh; } bool ShouldUpdateSave() const noexcept { return dirty_save; } - void Update(BlockModel &) noexcept; + void Update(BlockMesh &) noexcept; private: const BlockTypeRegistry *types; @@ -187,7 +187,7 @@ private: Pos position; int ref_count; - bool dirty_model; + bool dirty_mesh; bool dirty_save; }; diff --git a/src/world/ChunkRenderer.hpp b/src/world/ChunkRenderer.hpp index d45d0fd..714bb84 100644 --- a/src/world/ChunkRenderer.hpp +++ b/src/world/ChunkRenderer.hpp @@ -4,7 +4,7 @@ #include "Block.hpp" #include "Chunk.hpp" #include "../graphics/ArrayTexture.hpp" -#include "../model/BlockModel.hpp" +#include "../graphics/BlockMesh.hpp" #include @@ -12,7 +12,7 @@ namespace blank { class AssetLoader; -class BlockModel; +class BlockMesh; class ChunkIndex; class TextureIndex; class Viewport; @@ -34,7 +34,7 @@ public: private: ChunkIndex &index; - std::vector models; + std::vector models; ArrayTexture block_tex; diff --git a/src/world/block.cpp b/src/world/block.cpp index aa8fc77..d2453cf 100644 --- a/src/world/block.cpp +++ b/src/world/block.cpp @@ -100,27 +100,27 @@ BlockType::BlockType() noexcept } -void BlockType::FillEntityModel( - EntityModel::Buffer &buf, +void BlockType::FillEntityMesh( + EntityMesh::Buffer &buf, const glm::mat4 &transform, - EntityModel::Index idx_offset + EntityMesh::Index idx_offset ) const noexcept { shape->Vertices(buf, transform, texture, idx_offset); buf.hsl_mods.insert(buf.hsl_mods.end(), shape->VertexCount(), hsl_mod); buf.rgb_mods.insert(buf.rgb_mods.end(), shape->VertexCount(), rgb_mod); } -void BlockType::FillBlockModel( - BlockModel::Buffer &buf, +void BlockType::FillBlockMesh( + BlockMesh::Buffer &buf, const glm::mat4 &transform, - BlockModel::Index idx_offset + BlockMesh::Index idx_offset ) const noexcept { shape->Vertices(buf, transform, texture, idx_offset); buf.hsl_mods.insert(buf.hsl_mods.end(), shape->VertexCount(), hsl_mod); buf.rgb_mods.insert(buf.rgb_mods.end(), shape->VertexCount(), rgb_mod); } -void BlockType::FillOutlineModel(OutlineModel::Buffer &buf) const noexcept { +void BlockType::FillOutlineMesh(OutlineMesh::Buffer &buf) const noexcept { shape->Outline(buf); buf.colors.insert(buf.colors.end(), shape->OutlineCount(), outline_color); } diff --git a/src/world/chunk.cpp b/src/world/chunk.cpp index baad446..14c3af5 100644 --- a/src/world/chunk.cpp +++ b/src/world/chunk.cpp @@ -9,9 +9,9 @@ #include "WorldCollision.hpp" #include "../app/Assets.hpp" #include "../graphics/BlockLighting.hpp" +#include "../graphics/BlockMesh.hpp" #include "../graphics/Viewport.hpp" #include "../io/WorldSave.hpp" -#include "../model/BlockModel.hpp" #include #include @@ -36,19 +36,23 @@ Chunk::Chunk(const BlockTypeRegistry &types) noexcept , lighted(false) , position(0, 0, 0) , ref_count(0) -, dirty_model(false) +, dirty_mesh(false) , dirty_save(false) { } Chunk::Chunk(Chunk &&other) noexcept : types(other.types) +, generated(other.generated) +, lighted(other.lighted) , position(other.position) -, dirty_model(other.dirty_model) +, ref_count(other.ref_count) +, dirty_mesh(other.dirty_mesh) , dirty_save(other.dirty_save) { std::copy(other.neighbor, other.neighbor + sizeof(neighbor), neighbor); std::copy(other.blocks, other.blocks + sizeof(blocks), blocks); std::copy(other.light, other.light + sizeof(light), light); + other.ref_count = 0; } Chunk &Chunk::operator =(Chunk &&other) noexcept { @@ -56,8 +60,11 @@ Chunk &Chunk::operator =(Chunk &&other) noexcept { std::copy(other.neighbor, other.neighbor + sizeof(neighbor), neighbor); std::copy(other.blocks, other.blocks + sizeof(blocks), blocks); std::copy(other.light, other.light + sizeof(light), light); + generated = other.generated; + lighted = other.lighted; position = other.position; - dirty_model = other.dirty_save; + std::swap(ref_count, other.ref_count); + dirty_mesh = other.dirty_save; dirty_save = other.dirty_save; return *this; } @@ -246,7 +253,7 @@ int Chunk::GetLight(int index) const noexcept { return light[index]; } -float Chunk::GetVertexLight(const Pos &pos, const BlockModel::Position &vtx, const EntityModel::Normal &norm) const noexcept { +float Chunk::GetVertexLight(const Pos &pos, const BlockMesh::Position &vtx, const EntityMesh::Normal &norm) const noexcept { int index = ToIndex(pos); float light = GetLight(index); @@ -421,11 +428,11 @@ bool Chunk::Intersection( namespace { -BlockModel::Buffer buf; +BlockMesh::Buffer buf; } -void Chunk::Update(BlockModel &model) noexcept { +void Chunk::Update(BlockMesh &model) noexcept { int vtx_count = 0, idx_count = 0; for (const auto &block : blocks) { const Shape *shape = Type(block).shape; @@ -436,7 +443,7 @@ void Chunk::Update(BlockModel &model) noexcept { buf.Reserve(vtx_count, idx_count); int idx = 0; - BlockModel::Index vtx_counter = 0; + BlockMesh::Index vtx_counter = 0; for (size_t z = 0; z < depth; ++z) { for (size_t y = 0; y < height; ++y) { for (size_t x = 0; x < width; ++x, ++idx) { @@ -445,7 +452,7 @@ void Chunk::Update(BlockModel &model) noexcept { if (!type.visible || Obstructed(pos).All()) continue; - type.FillBlockModel(buf, ToTransform(pos, idx), vtx_counter); + type.FillBlockMesh(buf, ToTransform(pos, idx), vtx_counter); size_t vtx_begin = vtx_counter; vtx_counter += type.shape->VertexCount(); @@ -461,7 +468,7 @@ void Chunk::Update(BlockModel &model) noexcept { } model.Update(buf); - ClearModel(); + ClearMesh(); } Block::FaceSet Chunk::Obstructed(const Pos &pos) const noexcept { @@ -672,7 +679,7 @@ void ChunkRenderer::Update(int dt) { if (!index[i]->Lighted() && index.HasAllSurrounding(index[i]->Position())) { index[i]->ScanLights(); } - if (index[i]->ShouldUpdateModel()) { + if (index[i]->ShouldUpdateMesh()) { index[i]->Update(models[i]); ++updates; } @@ -689,7 +696,7 @@ void ChunkRenderer::Render(Viewport &viewport) { glm::mat4 m(index[i]->Transform(index.Base())); glm::mat4 mvp(chunk_prog.GetVP() * m); if (!CullTest(Chunk::Bounds(), mvp)) { - if (index[i]->ShouldUpdateModel()) { + if (index[i]->ShouldUpdateMesh()) { index[i]->Update(models[i]); } chunk_prog.SetM(m); @@ -1023,7 +1030,7 @@ void ChunkStore::Clean() { ++i; free.splice(free.end(), loaded, chunk); chunk->Unlink(); - chunk->InvalidateModel(); + chunk->InvalidateMesh(); } } }