Skip to content

Commit 56a66c1

Browse files
committed
Fixed bounding sphere transform bug
1 parent 8fa0f2b commit 56a66c1

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

MiniEngine/Model/Model.cpp

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void Model::Destroy()
3232
void Model::Render(
3333
MeshSorter& sorter,
3434
const GpuBuffer& meshConstants,
35-
const ScaleAndTranslation sphereTransforms[],
35+
const AffineTransform sphereTransforms[],
3636
const Joint* skeleton ) const
3737
{
3838
// Pointer to current mesh
@@ -45,9 +45,14 @@ void Model::Render(
4545
{
4646
const Mesh& mesh = *(const Mesh*)pMesh;
4747

48-
const ScaleAndTranslation& sphereXform = sphereTransforms[mesh.meshCBV];
49-
BoundingSphere sphereLS((const XMFLOAT4*)mesh.bounds);
50-
BoundingSphere sphereWS = sphereXform * sphereLS;
48+
const AffineTransform& sphereXform = sphereTransforms[mesh.meshCBV];
49+
Scalar scaleXSqr = LengthSquare((Vector3)sphereXform.GetX());
50+
Scalar scaleYSqr = LengthSquare((Vector3)sphereXform.GetY());
51+
Scalar scaleZSqr = LengthSquare((Vector3)sphereXform.GetZ());
52+
Scalar sphereScale = Sqrt(Max(Max(scaleXSqr, scaleYSqr), scaleZSqr));
53+
54+
BoundingSphere sphereLS((const XMFLOAT4*)mesh.bounds);
55+
BoundingSphere sphereWS = BoundingSphere(sphereXform * sphereLS.GetCenter(), sphereScale * sphereLS.GetRadius());
5156
BoundingSphere sphereVS = BoundingSphere(viewMat * sphereWS.GetCenter(), sphereWS.GetRadius());
5257

5358
if (frustum.IntersectSphere(sphereVS))
@@ -68,7 +73,7 @@ void ModelInstance::Render(MeshSorter& sorter) const
6873
if (m_Model != nullptr)
6974
{
7075
//const Frustum& frustum = sorter.GetWorldFrustum();
71-
m_Model->Render(sorter, m_MeshConstantsGPU, (const ScaleAndTranslation*)m_BoundingSphereTransforms.get(),
76+
m_Model->Render(sorter, m_MeshConstantsGPU, m_BoundingSphereTransforms.get(),
7277
m_Skeleton.get());
7378
}
7479
}
@@ -90,7 +95,8 @@ ModelInstance::ModelInstance( std::shared_ptr<const Model> sourceModel )
9095
{
9196
m_MeshConstantsCPU.Create(L"Mesh Constant Upload Buffer", sourceModel->m_NumNodes * sizeof(MeshConstants));
9297
m_MeshConstantsGPU.Create(L"Mesh Constant GPU Buffer", sourceModel->m_NumNodes, sizeof(MeshConstants));
93-
m_BoundingSphereTransforms.reset(new __m128[sourceModel->m_NumNodes]);
98+
99+
m_BoundingSphereTransforms.reset(new AffineTransform[sourceModel->m_NumNodes]);
94100
m_Skeleton.reset(new Joint[sourceModel->m_NumJoints]);
95101

96102
if (sourceModel->m_NumAnimations > 0)
@@ -129,7 +135,8 @@ ModelInstance& ModelInstance::operator=( std::shared_ptr<const Model> sourceMode
129135
{
130136
m_MeshConstantsCPU.Create(L"Mesh Constant Upload Buffer", sourceModel->m_NumNodes * sizeof(MeshConstants));
131137
m_MeshConstantsGPU.Create(L"Mesh Constant GPU Buffer", sourceModel->m_NumNodes, sizeof(MeshConstants));
132-
m_BoundingSphereTransforms.reset(new __m128[sourceModel->m_NumNodes]);
138+
139+
m_BoundingSphereTransforms.reset(new AffineTransform[sourceModel->m_NumNodes]);
133140
m_Skeleton.reset(new Joint[sourceModel->m_NumJoints]);
134141

135142
if (sourceModel->m_NumAnimations > 0)
@@ -158,7 +165,6 @@ void ModelInstance::Update(GraphicsContext& gfxContext, float deltaTime)
158165
Matrix4 matrixStack[kMaxStackDepth];
159166
Matrix4 ParentMatrix = Matrix4((AffineTransform)m_Locator);
160167

161-
ScaleAndTranslation* boundingSphereTransforms = (ScaleAndTranslation*)m_BoundingSphereTransforms.get();
162168
MeshConstants* cb = (MeshConstants*)m_MeshConstantsCPU.Map();
163169

164170
if (m_AnimGraph)
@@ -196,12 +202,12 @@ void ModelInstance::Update(GraphicsContext& gfxContext, float deltaTime)
196202
cbv.World = xform;
197203
cbv.WorldIT = InverseTranspose(xform.Get3x3());
198204

199-
Scalar scaleXSqr = LengthSquare((Vector3)xform.GetX());
200-
Scalar scaleYSqr = LengthSquare((Vector3)xform.GetY());
201-
Scalar scaleZSqr = LengthSquare((Vector3)xform.GetZ());
202-
Scalar sphereScale = Sqrt(Max(Max(scaleXSqr, scaleYSqr), scaleZSqr));
203-
boundingSphereTransforms[Node->matrixIdx] = ScaleAndTranslation((Vector3)xform.GetW(), sphereScale);
204-
}
205+
m_BoundingSphereTransforms[Node->matrixIdx] = AffineTransform(
206+
(Vector3)xform.GetX(),
207+
(Vector3)xform.GetY(),
208+
(Vector3)xform.GetZ(),
209+
(Vector3)xform.GetW());
210+
}
205211

206212
// If the next node will be a descendent, replace the parent matrix with our new matrix
207213
if (Node->hasChildren)

MiniEngine/Model/Model.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class Model
106106

107107
void Render(Renderer::MeshSorter& sorter,
108108
const GpuBuffer& meshConstants,
109-
const Math::ScaleAndTranslation sphereTransforms[],
109+
const Math::AffineTransform sphereTransforms[],
110110
const Joint* skeleton) const;
111111

112112
Math::BoundingSphere m_BoundingSphere; // Object-space bounding sphere
@@ -166,7 +166,7 @@ class ModelInstance
166166
std::shared_ptr<const Model> m_Model;
167167
UploadBuffer m_MeshConstantsCPU;
168168
ByteAddressBuffer m_MeshConstantsGPU;
169-
std::unique_ptr<__m128[]> m_BoundingSphereTransforms;
169+
std::unique_ptr<Math::AffineTransform[]> m_BoundingSphereTransforms;
170170
Math::UniformTransform m_Locator;
171171

172172
std::unique_ptr<GraphNode[]> m_AnimGraph; // A copy of the scene graph when instancing animation

0 commit comments

Comments
 (0)