diff --git a/src/graphics/core/Model.cpp b/src/graphics/core/Model.cpp index 3da5d897..3595a8e2 100644 --- a/src/graphics/core/Model.cpp +++ b/src/graphics/core/Model.cpp @@ -8,7 +8,12 @@ inline constexpr glm::vec3 X(1, 0, 0); inline constexpr glm::vec3 Y(0, 1, 0); inline constexpr glm::vec3 Z(0, 0, 1); -void Mesh::addPlane(glm::vec3 pos, glm::vec3 right, glm::vec3 up, glm::vec3 norm) { +void Mesh::addPlane( + const glm::vec3& pos, + const glm::vec3& right, + const glm::vec3& up, + const glm::vec3& norm +) { vertices.push_back({pos-right-up, {0,0}, norm}); vertices.push_back({pos+right-up, {1,0}, norm}); vertices.push_back({pos+right+up, {1,1}, norm}); @@ -18,7 +23,23 @@ void Mesh::addPlane(glm::vec3 pos, glm::vec3 right, glm::vec3 up, glm::vec3 norm vertices.push_back({pos-right+up, {0,1}, norm}); } -void Mesh::addBox(glm::vec3 pos, glm::vec3 size) { +void Mesh::addPlane( + const glm::vec3& pos, + const glm::vec3& right, + const glm::vec3& up, + const glm::vec3& norm, + const UVRegion& uv +) { + vertices.push_back({pos-right-up, {uv.u1, uv.v1}, norm}); + vertices.push_back({pos+right-up, {uv.u2, uv.v1}, norm}); + vertices.push_back({pos+right+up, {uv.u2, uv.v2}, norm}); + + vertices.push_back({pos-right-up, {uv.u1, uv.v1}, norm}); + vertices.push_back({pos+right+up, {uv.u2, uv.v2}, norm}); + vertices.push_back({pos-right+up, {uv.u1, uv.u2}, norm}); +} + +void Mesh::addBox(const glm::vec3& pos, const glm::vec3& size) { addPlane(pos+Z*size, X*size, Y*size, Z); addPlane(pos-Z*size, -X*size, Y*size, -Z); @@ -29,6 +50,19 @@ void Mesh::addBox(glm::vec3 pos, glm::vec3 size) { addPlane(pos-X*size, Z*size, Y*size, -X); } +void Mesh::addBox( + const glm::vec3& pos, const glm::vec3& size, const UVRegion (&uvs)[6] +) { + addPlane(pos+Z*size, X*size, Y*size, Z, uvs[0]); + addPlane(pos-Z*size, -X*size, Y*size, -Z, uvs[1]); + + addPlane(pos+Y*size, X*size, -Z*size, Y, uvs[2]); + addPlane(pos-Y*size, X*size, Z*size, -Y, uvs[3]); + + addPlane(pos+X*size, -Z*size, Y*size, X, uvs[4]); + addPlane(pos-X*size, Z*size, Y*size, -X, uvs[5]); +} + void Mesh::scale(const glm::vec3& size) { for (auto& vertex : vertices) { vertex.coord *= size; diff --git a/src/graphics/core/Model.hpp b/src/graphics/core/Model.hpp index b92fb030..da295cbb 100644 --- a/src/graphics/core/Model.hpp +++ b/src/graphics/core/Model.hpp @@ -4,6 +4,8 @@ #include #include +#include "maths/UVRegion.hpp" + namespace model { struct Vertex { glm::vec3 coord; @@ -14,9 +16,27 @@ namespace model { struct Mesh { std::string texture; std::vector vertices; - - void addPlane(glm::vec3 pos, glm::vec3 right, glm::vec3 up, glm::vec3 norm); - void addBox(glm::vec3 pos, glm::vec3 size); + bool lighting = true; + + void addPlane( + const glm::vec3& pos, + const glm::vec3& right, + const glm::vec3& up, + const glm::vec3& norm + ); + void addPlane( + const glm::vec3& pos, + const glm::vec3& right, + const glm::vec3& up, + const glm::vec3& norm, + const UVRegion& region + ); + void addBox(const glm::vec3& pos, const glm::vec3& size); + void addBox( + const glm::vec3& pos, + const glm::vec3& size, + const UVRegion (&texfaces)[6] + ); void scale(const glm::vec3& size); }; diff --git a/src/graphics/render/MainBatch.cpp b/src/graphics/render/MainBatch.cpp index c07a798a..1cbb6c42 100644 --- a/src/graphics/render/MainBatch.cpp +++ b/src/graphics/render/MainBatch.cpp @@ -80,3 +80,56 @@ glm::vec4 MainBatch::sampleLight( glm::max(Lightmap::extract(light, 3), minIntensity) / 15.0f ); } + +inline glm::vec4 do_tint(float value) { + return glm::vec4(value, value, value, 1.0f); +} + +void MainBatch::cube( + const glm::vec3& coord, + const glm::vec3& size, + const UVRegion(&texfaces)[6], + const glm::vec4& tint, + bool shading +) { + const glm::vec3 X(1.0f, 0.0f, 0.0f); + const glm::vec3 Y(0.0f, 1.0f, 0.0f); + const glm::vec3 Z(0.0f, 0.0f, 1.0f); + + quad( + coord + glm::vec3(0.0f, 0.0f, 0.0f), + X, Y, glm::vec2(size.x, size.y), + (shading ? do_tint(0.8) * tint : tint), + glm::vec3(1.0f), texfaces[5] + ); + quad( + coord + glm::vec3(size.x, 0.0f, -size.z), + -X, Y, glm::vec2(size.x, size.y), + (shading ? do_tint(0.8) * tint : tint), + glm::vec3(1.0f), texfaces[4] + ); + quad( + coord + glm::vec3(0.0f, size.y, 0.0f), + X, -Z, glm::vec2(size.x, size.z), + (shading ? do_tint(1.0f) * tint : tint), + glm::vec3(1.0f), texfaces[3] + ); + quad( + coord + glm::vec3(0.0f, 0.0f, -size.z), + X, Z, glm::vec2(size.x, size.z), + (shading ? do_tint(0.7f) * tint : tint), + glm::vec3(1.0f), texfaces[2] + ); + quad( + coord + glm::vec3(0.0f, 0.0f, -size.z), + Z, Y, glm::vec2(size.z, size.y), + (shading ? do_tint(0.9f) * tint : tint), + glm::vec3(1.0f), texfaces[0] + ); + quad( + coord + glm::vec3(size.x, 0.0f, 0.0f), + -Z, Y, glm::vec2(size.z, size.y), + (shading ? do_tint(0.9f) * tint : tint), + glm::vec3(1.0f), texfaces[1] + ); +} diff --git a/src/graphics/render/MainBatch.hpp b/src/graphics/render/MainBatch.hpp index bb25e83b..36bf7a8e 100644 --- a/src/graphics/render/MainBatch.hpp +++ b/src/graphics/render/MainBatch.hpp @@ -118,4 +118,12 @@ public: tint ); } + + void cube( + const glm::vec3& coord, + const glm::vec3& size, + const UVRegion(&texfaces)[6], + const glm::vec4& tint, + bool shading + ); }; diff --git a/src/graphics/render/ModelsGenerator.cpp b/src/graphics/render/ModelsGenerator.cpp index edec061c..07bf7b0d 100644 --- a/src/graphics/render/ModelsGenerator.cpp +++ b/src/graphics/render/ModelsGenerator.cpp @@ -1,6 +1,7 @@ #include "ModelsGenerator.hpp" #include "assets/Assets.hpp" +#include "assets/assets_util.hpp" #include "items/ItemDef.hpp" #include "voxels/Block.hpp" #include "content/Content.hpp" @@ -40,6 +41,13 @@ static model::Model create_flat_model( return model; } +static inline UVRegion get_region_for( + const std::string& texture, const Assets& assets +) { + auto texreg = util::get_texture_region(assets, "blocks:" + texture, ""); + return texreg.region; +} + model::Model ModelsGenerator::generate( const ItemDef& def, const Content& content, const Assets& assets ) { @@ -50,6 +58,56 @@ model::Model ModelsGenerator::generate( return create_flat_model( "blocks:" + blockDef.textureFaces.at(0), assets ); + } else if (blockDef.model == BlockModel::custom) { + model = model::Model(); + for (size_t i = 0; i < blockDef.modelBoxes.size(); i++) { + auto& mesh = + model.addMesh("blocks:" + blockDef.modelTextures[i * 6]); + mesh.lighting = !def.rt.emissive; + const UVRegion (&boxtexfaces)[6] = { + get_region_for(blockDef.modelTextures[i * 6], assets), + get_region_for(blockDef.modelTextures[i * 6 + 1], assets), + get_region_for(blockDef.modelTextures[i * 6 + 2], assets), + get_region_for(blockDef.modelTextures[i * 6 + 3], assets), + get_region_for(blockDef.modelTextures[i * 6 + 4], assets), + get_region_for(blockDef.modelTextures[i * 6 + 5], assets) + }; + mesh.addBox( + blockDef.modelBoxes[i].center(), + blockDef.modelBoxes[i].size()*0.5f, boxtexfaces + ); + } + const auto& points = blockDef.modelExtraPoints; + glm::vec3 poff = glm::vec3(0.0f, 0.0f, 1.0f); + glm::vec3 norm {0, 1, 0}; + for (size_t i = 0; i < blockDef.modelExtraPoints.size() / 4; i++) { + auto texture = + "blocks:" + + blockDef.modelTextures[blockDef.modelBoxes.size() * 6 + i]; + + auto& mesh = model.addMesh(texture); + mesh.lighting = !def.rt.emissive; + + auto reg = get_region_for(texture, assets); + mesh.vertices.push_back( + {points[i * 4 + 0] - poff, glm::vec2(reg.u1, reg.v1), norm} + ); + mesh.vertices.push_back( + {points[i * 4 + 1] - poff, glm::vec2(reg.u2, reg.v1), norm} + ); + mesh.vertices.push_back( + {points[i * 4 + 2] - poff, glm::vec2(reg.u2, reg.v2), norm} + ); + mesh.vertices.push_back( + {points[i * 4 + 3] - poff, glm::vec2(reg.u1, reg.v1), norm} + ); + mesh.vertices.push_back( + {points[i * 4 + 4] - poff, glm::vec2(reg.u2, reg.v2), norm} + ); + mesh.vertices.push_back( + {points[i * 4 + 0] - poff, glm::vec2(reg.u1, reg.v2), norm} + ); + } } for (auto& mesh : model.meshes) { switch (blockDef.model) { diff --git a/src/voxels/Block.hpp b/src/voxels/Block.hpp index 2d2cdf90..c2ff6ce2 100644 --- a/src/voxels/Block.hpp +++ b/src/voxels/Block.hpp @@ -116,8 +116,8 @@ public: std::vector modelTextures = {}; std::vector modelBoxes = {}; - std::vector modelExtraPoints = - {}; // initially made for tetragons + // initially made for tetragons + std::vector modelExtraPoints = {}; std::vector modelUVs = {}; // boxes' tex-UVs also there /// @brief id of used BlockMaterial, may specify non-existing material