From b0fd716af9e906dcc6ba8c429758e8ead1808e84 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Wed, 9 Jul 2025 21:18:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E7=94=A8=E9=81=8D=E5=8E=86Component?= =?UTF-8?q?=E6=B3=95=E5=88=9B=E5=BB=BABoundingBox?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/Basic/RenderBoundBox.cpp | 46 ++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/example/Basic/RenderBoundBox.cpp b/example/Basic/RenderBoundBox.cpp index b6cf9a84..6536b380 100644 --- a/example/Basic/RenderBoundBox.cpp +++ b/example/Basic/RenderBoundBox.cpp @@ -55,7 +55,7 @@ private: MeshComponentData *data; ComponentDataPtr cdp; - MeshComponent *component[7]; + MeshComponent *component; public: @@ -204,8 +204,8 @@ private: tci.innerRadius=0.9; tci.outerRadius=1.1; - tci.numberSlices=64; - tci.numberStacks=8; + tci.numberSlices=128; + tci.numberStacks=16; rm_torus=CreateRenderMesh(CreateTorus(prim_creater,&tci),&solid,4); } @@ -234,17 +234,14 @@ private: { cci.mat=ScaleMatrix(10,10,1); - //rm_plane->component=CreateComponent(&cci,rm_plane->cdp); + rm_plane->component=CreateComponent(&cci,rm_plane->cdp); } { - for(int i=0;i<6;i++) - { - cci.mat=AxisYRotate(deg2rad(i*30.0f))*ScaleMatrix((i+1)*1.0f); + cci.mat=AxisYRotate(deg2rad(90.0f)); - rm_torus->component[i]=CreateComponent(&cci,rm_torus->cdp); - rm_torus->component[i]->SetOverrideMaterial(solid.mi[i]); - } + rm_torus->component=CreateComponent(&cci,rm_torus->cdp); + rm_torus->component->SetOverrideMaterial(solid.mi[1]); } return(true); @@ -252,18 +249,33 @@ private: bool InitBoundingBoxScene() { - CreateComponentInfo cci(GetSceneRoot()); + SceneNode *root=GetSceneRoot(); - OBB obb; + CreateComponentInfo cci(root); - for(int i=0;i<6;i++) + ArrayList box_matrices; + + for(Component *c:root->GetComponents()) { - MeshComponent *component=rm_torus->component[i]; - - if(!component->GetWorldOBBMatrix(cci.mat)) + if(c->GetTypeHash()!=MeshComponent::StaticTypeHash()) continue; - auto *box_component=CreateComponent(&cci,rm_box->cdp); + MeshComponent *component=(MeshComponent *)c; + + Matrix4f mat; + + if(component->GetWorldOBBMatrix(mat)) + box_matrices.Add(mat); + } + + //不可以直接在上面的循环中创建新的Component,因为循环本身就要读取Component列表 + + for(const Matrix4f &mat:box_matrices) + { + cci.mat=mat; + + if(!CreateComponent(&cci,rm_box->cdp)) + return(false); } return(true);