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