diff --git a/src/coders/vcm.cpp b/src/coders/vcm.cpp index 337d721f..b7d93b57 100644 --- a/src/coders/vcm.cpp +++ b/src/coders/vcm.cpp @@ -1,6 +1,8 @@ #include "vcm.hpp" -#include +#include +#include +#include #include "xml.hpp" #include "util/stringutil.hpp" @@ -73,6 +75,29 @@ static void perform_box(const xmlelement& root, model::Model& model) { auto from = root.attr("from").asVec3(); auto to = root.attr("to").asVec3(); + glm::vec3 origin = (from + to) * 0.5f; + if (root.has("origin")) { + origin = root.attr("origin").asVec3(); + } + + glm::mat4 tsf(1.0f); + from -= origin; + to -= origin; + tsf = glm::translate(tsf, origin); + + if (root.has("rotate")) { + auto text = root.attr("rotate").getText(); + if (std::count(text.begin(), text.end(), ',') == 3) { + auto quat = root.attr("rotate").asVec4(); + tsf *= glm::mat4_cast(glm::quat(quat.w, quat.x, quat.y, quat.z)); + } else { + auto rot = root.attr("rotate").asVec3(); + tsf = glm::rotate(tsf, glm::radians(rot.x), glm::vec3(1, 0, 0)); + tsf = glm::rotate(tsf, glm::radians(rot.y), glm::vec3(0, 1, 0)); + tsf = glm::rotate(tsf, glm::radians(rot.z), glm::vec3(0, 0, 1)); + } + } + UVRegion regions[6] {}; regions[0].scale(to.x - from.x, to.y - from.y); regions[1].scale(from.x - to.x, to.y - from.y); @@ -142,7 +167,7 @@ static void perform_box(const xmlelement& root, model::Model& model) { bool enabled[6] {}; enabled[i] = true; auto& mesh = model.addMesh(texfaces[i], shading); - mesh.addBox(center, halfsize, regions, enabled); + mesh.addBox(center, halfsize, regions, enabled, tsf); } } diff --git a/src/graphics/commons/Model.cpp b/src/graphics/commons/Model.cpp index 20d90eb4..386ae1cc 100644 --- a/src/graphics/commons/Model.cpp +++ b/src/graphics/commons/Model.cpp @@ -8,21 +8,6 @@ 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( - 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}); - - vertices.push_back({pos-right-up, {0,0}, norm}); - vertices.push_back({pos+right+up, {1,1}, norm}); - vertices.push_back({pos-right+up, {0,1}, norm}); -} - void Mesh::addPlane( const glm::vec3& pos, const glm::vec3& right, @@ -39,6 +24,23 @@ void Mesh::addPlane( vertices.push_back({pos-right+up, {uv.u1, uv.v2}, norm}); } +void Mesh::addPlane( + const glm::vec3& pos, + const glm::vec3& right, + const glm::vec3& up, + const glm::vec3& norm, + const UVRegion& region, + const glm::mat4& transform +) { + addPlane( + glm::vec3(transform * glm::vec4(pos, 1.0f)), + glm::vec3(transform * glm::vec4(right, 0.0f)), + glm::vec3(transform * glm::vec4(up, 0.0f)), + glm::normalize(glm::vec3(transform * glm::vec4(norm, 0.0f))), + region + ); +} + void Mesh::addRect( const glm::vec3& pos, const glm::vec3& right, @@ -56,14 +58,15 @@ void Mesh::addRect( } 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); + UVRegion fullRegion (0, 0, 1, 1); + addPlane(pos+Z*size, X*size, Y*size, Z, fullRegion); + addPlane(pos-Z*size, -X*size, Y*size, -Z, fullRegion); - addPlane(pos+Y*size, X*size, -Z*size, Y); - addPlane(pos-Y*size, X*size, Z*size, -Y); + addPlane(pos+Y*size, X*size, -Z*size, Y, fullRegion); + addPlane(pos-Y*size, X*size, Z*size, -Y, fullRegion); - addPlane(pos+X*size, -Z*size, Y*size, X); - addPlane(pos-X*size, Z*size, Y*size, -X); + addPlane(pos+X*size, -Z*size, Y*size, X, fullRegion); + addPlane(pos-X*size, Z*size, Y*size, -X, fullRegion); } void Mesh::addBox( @@ -71,19 +74,29 @@ void Mesh::addBox( const glm::vec3& size, const UVRegion (&uvs)[6], const bool enabledSides[6] +) { + addBox(pos, size, uvs, enabledSides, glm::mat4(1.0f)); +} + +void Mesh::addBox( + const glm::vec3& pos, + const glm::vec3& size, + const UVRegion (&uvs)[6], + const bool enabledSides[6], + const glm::mat4& transform ) { if (enabledSides[0]) // north - addPlane(pos+Z*size, X*size, Y*size, Z, uvs[0]); + addPlane(pos+Z*size, X*size, Y*size, Z, uvs[0], transform); if (enabledSides[1]) // south - addPlane(pos-Z*size, -X*size, Y*size, -Z, uvs[1]); + addPlane(pos-Z*size, -X*size, Y*size, -Z, uvs[1], transform); if (enabledSides[2]) // top - addPlane(pos+Y*size, X*size, -Z*size, Y, uvs[2] * glm::vec2(-1)); + addPlane(pos+Y*size, X*size, -Z*size, Y, uvs[2] * glm::vec2(-1), transform); if (enabledSides[3]) // bottom - addPlane(pos-Y*size, X*size, Z*size, -Y, uvs[3] * glm::vec2(-1, 1)); + addPlane(pos-Y*size, X*size, Z*size, -Y, uvs[3] * glm::vec2(-1, 1), transform); if (enabledSides[4]) // west - addPlane(pos+X*size, -Z*size, Y*size, X, uvs[4]); + addPlane(pos+X*size, -Z*size, Y*size, X, uvs[4], transform); if (enabledSides[5]) // east - addPlane(pos-X*size, Z*size, Y*size, -X, uvs[5] * glm::vec2(-1, 1)); + addPlane(pos-X*size, Z*size, Y*size, -X, uvs[5] * glm::vec2(-1, 1), transform); } void Mesh::scale(const glm::vec3& size) { diff --git a/src/graphics/commons/Model.hpp b/src/graphics/commons/Model.hpp index 73bee359..bbb0d398 100644 --- a/src/graphics/commons/Model.hpp +++ b/src/graphics/commons/Model.hpp @@ -22,14 +22,16 @@ namespace model { const glm::vec3& pos, const glm::vec3& right, const glm::vec3& up, - const glm::vec3& norm + const glm::vec3& norm, + const UVRegion& region ); void addPlane( const glm::vec3& pos, const glm::vec3& right, const glm::vec3& up, const glm::vec3& norm, - const UVRegion& region + const UVRegion& region, + const glm::mat4& transform ); void addRect( const glm::vec3& pos, @@ -45,6 +47,13 @@ namespace model { const UVRegion (&texfaces)[6], const bool enabledSides[6] ); + void addBox( + const glm::vec3& pos, + const glm::vec3& size, + const UVRegion (&texfaces)[6], + const bool enabledSides[6], + const glm::mat4& transform + ); void scale(const glm::vec3& size); };