• Create Account

When to calculate the bounding sphere

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

#1george7378  Members   -  Reputation: 1132

Like
0Likes
Like

Posted 10 February 2013 - 07:42 AM

Hi everyone,

If I'm calculating the bounding sphere of my mesh (using D3DXComputeBoundingSphere), should I do it before or after I have transformed the mesh using the world matrix? I'm guessing after, but I just want to check!

Thanks!

#2mhagain  Crossbones+   -  Reputation: 7448

Like
1Likes
Like

Posted 10 February 2013 - 08:13 AM

You can do it any time you wish, including once-only at load time.  The great thing about a bounding sphere is that you only need to transform it's midpoint to get it to a different space.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.

#3cozzie  Members   -  Reputation: 1473

Like
0Likes
Like

Posted 10 February 2013 - 08:43 AM

Hi George,

This is a bit tricky, when you do it is not important, it's important that you transform/ rotate etc. at the right time.

I found out by playing around quite I while. Here's what I do now, in pseudo code:

- lock the vertexbuffer of the mesh

- applying scaling and rotations on the vertices, for this mesh instance

- compute boundingsphere for complete meshinstance (0,0,0 being in local space, mesh center)

- do the same for each submesh (attr of the d3dxmesh)

- translate the worldpostion for both the full mesh instance as all submeshes

Keep in mind future plans, if you're gonna use mesh instances, then you should not limit yourself by calculation it on mesh base, but do it on mesh instance base. For example I do this within my mesh instance class (using a specific mesh). Which also has the rotation, translation, etc. matrices for the mesh instance.

It's important to calculate it in local space and keep track of scaling, to prevent weird results.

I also calculate it for the submeshes (attributes of the d3dxmesh).

#4L. Spiro  Crossbones+   -  Reputation: 12290

Like
0Likes
Like

Posted 10 February 2013 - 08:53 AM

Create any bounding boxes/spheres for a given mesh once at load-time (or embed them into your custom file format) and apply the same transformations on them as those that are applied to your mesh.

For spheres, that means:
	/**
* Creates a new sphere that represents the given sphere modified by the given matrix.
*
* \param _sSphere The sphere to modify.
* \param _mMat The matrix by which to modify the given sphere.
* \return Returns a sphere that has been scaled and moved by the given matrix.
*/
CSphere LSE_FCALL CSphere::CreateSphereFromSphereAndMatrix( const CSphere &_sSphere, const CMatrix4x4 &_mMat ) {
CSphere sRet;
CVector3 vRow;
_mMat.GetRow( 0UL, vRow );
sRet.r = vRow.LenSq();
_mMat.GetRow( 1UL, vRow );
sRet.r = CStd::Max( sRet.r, vRow.LenSq() );
_mMat.GetRow( 2UL, vRow );
sRet.r = CStd::Max( sRet.r, vRow.LenSq() );
sRet.r = CMathLib::Sqrt( sRet.r ) * _sSphere.r;
sRet.c = _mMat * _sSphere.c;
return sRet;
}
The midpoint of the sphere is simply fed into the matrix and returned as a new point in space.
The radius of the sphere is modified by the largest scaling factor in the matrix. This can be omitted for matrices with no scaling factor.

L. Spiro

Edited by L. Spiro, 10 February 2013 - 08:54 AM.

It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#5swiftcoder  Senior Moderators   -  Reputation: 9635

Like
0Likes
Like

Posted 10 February 2013 - 09:51 AM

The great thing about a bounding sphere is that you only need to transform it's midpoint to get it to a different space.

Be careful there, things get really hairy when dealing with non-uniform scaling.

I'd generally suggest that you avoid non-uniform scaling everywhere, for simplicity, but if you can't, bounding boxes start to look a whole lot better.

Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]

#6mhagain  Crossbones+   -  Reputation: 7448

Like
1Likes
Like

Posted 10 February 2013 - 12:44 PM

The great thing about a bounding sphere is that you only need to transform it's midpoint to get it to a different space.

Be careful there, things get really hairy when dealing with non-uniform scaling.

I'd generally suggest that you avoid non-uniform scaling everywhere, for simplicity, but if you can't, bounding boxes start to look a whole lot better.

This is true.

Since the OP talked about transforming the mesh using the world matrix, and since you generally don't see scaling in a world matrix, it can probably be discounted as a specific problem for this question, but it is still correct to highlight it.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.

#7L. Spiro  Crossbones+   -  Reputation: 12290

Like
0Likes
Like

Posted 10 February 2013 - 06:38 PM

The code I posted handles this problem.  To transform a bounding sphere to handle any scaling, uniform or not, simply look for the longest row vector (this is Direct3D after all) in the upper-left 3×3 area and multiply the radius by the length of that vector.

since you generally don't see scaling in a world matrix

I am not sure what you mean by this. Scale, rotation, and position are all part of the world matrix.

L. Spiro

Edited by L. Spiro, 10 February 2013 - 06:41 PM.

It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS