Exemplo n.º 1
0
//-----------------------------------------------------------------------------
// Given an inverse projection matrix, take the extremes of the space in transformed into world space and
// get a bounding sphere.
//-----------------------------------------------------------------------------
void CalculateSphereFromProjectionMatrixInverse( const VMatrix &volumeToWorld, Vector *pCenter, float *pflRadius )
{
	// FIXME: Could maybe do better than the compile with all of these multiplies by 0 and 1.

	// Need 3 points: the endpoint of the line through the center of the near + far planes,
	// and one point on the far plane. From that, we can derive a point somewhere on the center	line
	// which would produce the smallest bounding sphere.
	Vector vecCenterNear, vecCenterFar, vecNearEdge, vecFarEdge;
	Vector3DMultiplyPositionProjective( volumeToWorld, Vector( 0.5f, 0.5f, 0.0f ), vecCenterNear );
	Vector3DMultiplyPositionProjective( volumeToWorld, Vector( 0.5f, 0.5f, 1.0f ), vecCenterFar );
	Vector3DMultiplyPositionProjective( volumeToWorld, Vector( 0.0f, 0.0f, 0.0f ), vecNearEdge );
	Vector3DMultiplyPositionProjective( volumeToWorld, Vector( 0.0f, 0.0f, 1.0f ), vecFarEdge );

	// Let the distance between the near + far center points = l
	// Let the distance between the near center point + near edge point = h1
	// Let the distance between the far center point + far edge point = h2
	// Let the distance along the center line from the near point to the sphere center point = x
	// Then let the distance between the sphere center point + near edge point == 
	//	the distance between the sphere center point + far edge point == r == radius of sphere
	// Then h1^2 + x^2 == r^2 == (l-x)^2 + h2^2
	// h1^x + x^2 = l^2 - 2 * l * x + x^2 + h2^2
	// 2 * l * x = l^2 + h2^2 - h1^2
	// x = (l^2 + h2^2 - h1^2) / (2 * l)
	// r = sqrt( hl^1 + x^2 )
	Vector vecDelta;
	VectorSubtract( vecCenterFar, vecCenterNear, vecDelta );
	float l = vecDelta.Length();
	float h1Sqr = vecCenterNear.DistToSqr( vecNearEdge );
	float h2Sqr = vecCenterFar.DistToSqr( vecFarEdge );
	float x = (l*l + h2Sqr - h1Sqr) / (2.0f * l);
	VectorMA( vecCenterNear, (x / l), vecDelta, *pCenter );
	*pflRadius = sqrt( h1Sqr + x*x );
}
Exemplo n.º 2
0
static inline void FrustumPlanesFromMatrixHelper( const VMatrix &shadowToWorld, const Vector &p1, const Vector &p2, const Vector &p3, 
												 Vector &normal, float &dist )
{
	Vector world1, world2, world3;
	Vector3DMultiplyPositionProjective( shadowToWorld, p1, world1 );
	Vector3DMultiplyPositionProjective( shadowToWorld, p2, world2 );
	Vector3DMultiplyPositionProjective( shadowToWorld, p3, world3 );

	Vector v1, v2;
	VectorSubtract( world2, world1, v1 );
	VectorSubtract( world3, world1, v2 );

	CrossProduct( v1, v2, normal );
	VectorNormalize( normal );
	dist = DotProduct( normal, world1 );	
}
Exemplo n.º 3
0
void CCamera::ViewToWorld( const Vector2D &vView, Vector& vWorld)
{	
	Vector vView3D;

	vView3D.x = 2.0 * vView.x / m_nViewWidth - 1;
	vView3D.y = -2.0 * vView.y / m_nViewHeight + 1;
	vView3D.z = 0;

	Vector3DMultiplyPositionProjective( m_InvViewProjMatrix, vView3D, vWorld );
}
Exemplo n.º 4
0
void CCamera::WorldToView( const Vector& vWorld, Vector2D &vView )
{
	Vector vView3D;

	Vector3DMultiplyPositionProjective( m_ViewProjMatrix, vWorld, vView3D );

	// NOTE: The negative sign on y is because wc wants to think about screen
	// coordinates in a different way than the material system
	vView.x = 0.5 * (vView3D.x + 1.0) * m_nViewWidth;
	vView.y = 0.5 * (-vView3D.y + 1.0) * m_nViewHeight;
}
Exemplo n.º 5
0
static inline void CalculateAABBForNormalizedFrustum_Helper( float x, float y, float z, const VMatrix &volumeToWorld, Vector &mins, Vector &maxs )
{
	Vector volumeSpacePos( x, y, z );

	// Make sure it's been clipped
	Assert( volumeSpacePos[0] >= -1e-3f );
	Assert( volumeSpacePos[0] - 1.0f <= 1e-3f );
	Assert( volumeSpacePos[1] >= -1e-3f );
	Assert( volumeSpacePos[1] - 1.0f <= 1e-3f );
	Assert( volumeSpacePos[2] >= -1e-3f );
	Assert( volumeSpacePos[2] - 1.0f <= 1e-3f );

	Vector worldPos;
	Vector3DMultiplyPositionProjective( volumeToWorld, volumeSpacePos, worldPos );
	AddPointToBounds( worldPos, mins, maxs );
}
Exemplo n.º 6
0
static int vmatrix_Vector3DMultiplyPositionProjective (lua_State *L) {
  Vector3DMultiplyPositionProjective(luaL_checkvmatrix(L, 1), luaL_checkvector(L, 2), luaL_checkvector(L, 3));
  return 0;
}