From 51e043f07d2c3068a1f19586f281f66aeb046d77 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Wed, 9 Jul 2025 00:45:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E7=9A=84PrimitiveComponent::GetWorldO?= =?UTF-8?q?BBMatrix=E5=87=BD=E6=95=B0=EF=BC=8C=E8=BF=9B=E4=B8=80=E6=AD=A5?= =?UTF-8?q?=E5=87=8F=E5=B0=91=E4=B8=AD=E9=97=B4=E8=AE=A1=E7=AE=97=E6=AD=A5?= =?UTF-8?q?=E9=AA=A4=EF=BC=8C=E7=9B=B4=E6=8E=A5=E5=87=BAOBBMATRIX?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMSceneGraph | 2 +- example/Basic/RenderBoundBox.cpp | 12 +++++------ inc/hgl/component/PrimitiveComponent.h | 28 ++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/CMSceneGraph b/CMSceneGraph index 50f5514a..a4d3950a 160000 --- a/CMSceneGraph +++ b/CMSceneGraph @@ -1 +1 @@ -Subproject commit 50f5514aee947cc8cb400cd9bf98b58803fc58a6 +Subproject commit a4d3950af690d892e259865be4559053e007c979 diff --git a/example/Basic/RenderBoundBox.cpp b/example/Basic/RenderBoundBox.cpp index 70bbfe45..b6cf9a84 100644 --- a/example/Basic/RenderBoundBox.cpp +++ b/example/Basic/RenderBoundBox.cpp @@ -240,7 +240,8 @@ private: { for(int i=0;i<6;i++) { - cci.mat=AxisYRotate(deg2rad(i*30.0f)); + cci.mat=AxisYRotate(deg2rad(i*30.0f))*ScaleMatrix((i+1)*1.0f); + rm_torus->component[i]=CreateComponent(&cci,rm_torus->cdp); rm_torus->component[i]->SetOverrideMaterial(solid.mi[i]); } @@ -257,13 +258,12 @@ private: for(int i=0;i<6;i++) { - PrimitiveComponent *component=rm_torus->component[i]; + MeshComponent *component=rm_torus->component[i]; - component->GetWorldOBB(obb); + if(!component->GetWorldOBBMatrix(cci.mat)) + continue; - cci.mat=obb.GetMatrix(); - - CreateComponent(&cci,rm_box->cdp); + auto *box_component=CreateComponent(&cci,rm_box->cdp); } return(true); diff --git a/inc/hgl/component/PrimitiveComponent.h b/inc/hgl/component/PrimitiveComponent.h index e3f5d3e5..da7e22dc 100644 --- a/inc/hgl/component/PrimitiveComponent.h +++ b/inc/hgl/component/PrimitiveComponent.h @@ -31,6 +31,34 @@ public: return true; } + + const bool GetWorldOBBMatrix(Matrix4f &obb_matrix,const float cube_size=1.0f) + { + AABB aabb; + + if(!GetLocalAABB(aabb)) + return false; + + //这两行也是对的 + //OBB obb(GetLocalToWorldMatrix(),aabb); + //obb_matrix=obb.GetMatrix(cube_size); + + // 1. 计算最终的缩放向量 + const Vector3f scale_vector = aabb.GetLength() * cube_size; + + // 2. 直接构建局部空间的变换矩阵 (Translate * Scale) + Matrix4f local_aabb_matrix; + + local_aabb_matrix[0] = Vector4f(scale_vector.x, 0.0f, 0.0f, 0.0f); + local_aabb_matrix[1] = Vector4f(0.0f, scale_vector.y, 0.0f, 0.0f); + local_aabb_matrix[2] = Vector4f(0.0f, 0.0f, scale_vector.z, 0.0f); + local_aabb_matrix[3] = Vector4f(aabb.GetCenter(), 1.0f); + + // 3. 将局部 AABB 矩阵与组件的 LocalToWorld 矩阵相乘,得到最终的世界 OBB 矩阵 + obb_matrix = GetLocalToWorldMatrix() * local_aabb_matrix; + + return(true); + } };//class PrimitiveComponent COMPONENT_NAMESPACE_END