From 3641509462a81530dfa5497069561265ec2e679b Mon Sep 17 00:00:00 2001 From: Nicolas Hake Date: Sun, 3 Apr 2016 12:22:11 +0200 Subject: [PATCH] Mesh rotation: Keep rendered mesh inside shape --- src/lib/StdMesh.h | 5 +++++ src/object/C4Object.cpp | 3 +++ 2 files changed, 8 insertions(+) diff --git a/src/lib/StdMesh.h b/src/lib/StdMesh.h index 9b0836d6e..c7c9f4e5c 100644 --- a/src/lib/StdMesh.h +++ b/src/lib/StdMesh.h @@ -145,6 +145,11 @@ struct StdMeshBox { float x1, y1, z1; float x2, y2, z2; + + StdMeshVector GetCenter() const + { + return StdMeshVector{ (x2 + x1) / 2.0f, (y2 + y1) / 2.0f, (z2 + z1) / 2.0f }; + } }; class StdSubMesh diff --git a/src/object/C4Object.cpp b/src/object/C4Object.cpp index 2f23a04a9..924411340 100644 --- a/src/object/C4Object.cpp +++ b/src/object/C4Object.cpp @@ -584,7 +584,10 @@ void C4Object::DrawFaceImpl(C4TargetFacet &cgo, bool action, float fx, float fy, if (fix_r != Fix0) { + const auto mesh_center = pMeshInstance->GetMesh().GetBoundingBox().GetCenter(); + matrix = StdMeshMatrix::Translate(-mesh_center.x, -mesh_center.y, -mesh_center.z) * matrix; matrix = StdMeshMatrix::Rotate(fixtof(fix_r) * (M_PI / 180.0f), 0.0f, 0.0f, 1.0f) * matrix; + matrix = StdMeshMatrix::Translate(mesh_center.x, mesh_center.y, mesh_center.z) * matrix; } if(twdt != fwdt || thgt != fhgt)