Esempio n. 1
0
ShapePosition
Frustum::testBoundingBox(math::Box::Ptr box)
{
	int result = 0;
	
	// bottom left front
	float xblf = box->bottomLeft()->x();
	float yblf = box->bottomLeft()->y();
	float zblf = box->bottomLeft()->z();

	// top right back
	float xtrb = box->topRight()->x();
	float ytrb = box->topRight()->y();
	float ztrb = box->topRight()->z();

	// bottom right front
	float xbrf = xtrb;
	float ybrf = yblf;
	float zbrf = zblf;

	// bottom left back
	float xblb = xblf;
	float yblb = yblf;
	float zblb = ztrb;

	// bottom right back
	float xbrb = xtrb;
	float ybrb = yblf;
	float zbrb = ztrb;

	// top left back
	float xtlb = xblf;
	float ytlb = ytrb;
	float ztlb = ztrb;

	// top left front
	float xtlf = xtrb;
	float ytlf = ytrb;
	float ztlf = zblf;

	// top right front
	float xtrf = xblf;
	float ytrf = ytrb;
	float ztrf = zblf;

	for (uint planeId = 0; planeId < _planes.size(); ++planeId)
	{
		float pa = _planes[planeId]->x();
		float pb = _planes[planeId]->y();
		float pc = _planes[planeId]->z();
		float pd = _planes[planeId]->w();

		
		_blfResult[planeId] = pa * xblf + pb * yblf + pc * zblf + pd < 0.;
		_brfResult[planeId] = pa * xbrf + pb * ybrf + pc * zbrf + pd < 0.;
		_blbResult[planeId] = pa * xblb + pb * yblb + pc * zblb + pd < 0.;
		_brbResult[planeId] = pa * xbrb + pb * ybrb + pc * zbrb + pd < 0.;
		
		_tlfResult[planeId] = pa * xtlf + pb * ytlf + pc * ztlf + pd < 0.;
		_trfResult[planeId] = pa * xtrf + pb * ytrf + pc * ztrf + pd < 0.;
		_tlbResult[planeId] = pa * xtlb + pb * ytlb + pc * ztlb + pd < 0.;
		_trbResult[planeId] = pa * xtrb + pb * ytrb + pc * ztrb + pd < 0.;
	}

	if ((_blfResult[(int)PlanePosition::LEFT]	&& _trbResult[(int)PlanePosition::RIGHT]) ||
		(_blfResult[(int)PlanePosition::RIGHT]	&& _trbResult[(int)PlanePosition::LEFT]) ||
		(_blfResult[(int)PlanePosition::TOP]	&& _trbResult[(int)PlanePosition::BOTTOM]) ||
		(_blfResult[(int)PlanePosition::BOTTOM] && _trbResult[(int)PlanePosition::TOP]))
		return ShapePosition::AROUND;

	for (uint planeId = 0; planeId < _planes.size(); ++planeId)
	{
		if (_blfResult[planeId] &&
			_brfResult[planeId] &&
			_blbResult[planeId] &&
			_brbResult[planeId] &&
			_tlfResult[planeId] &&
			_trfResult[planeId] &&
			_tlbResult[planeId] &&
			_trbResult[planeId]
			)
			return static_cast<ShapePosition>(planeId);
	}

	return ShapePosition::INSIDE;
}
Esempio n. 2
0
std::pair<math::ShapePosition, unsigned int>
math::Frustum::testBoundingBox(math::Box::Ptr box, unsigned int basePlaneId)
{
	int result = 0;
	
	// bottom left front
	float xblf = box->bottomLeft().x;
	float yblf = box->bottomLeft().y;
	float zblf = box->bottomLeft().z;

	// top right back
	float xtrb = box->topRight().x;
	float ytrb = box->topRight().y;
	float ztrb = box->topRight().z;

	// bottom right front
	float xbrf = xtrb;
	float ybrf = yblf;
	float zbrf = zblf;

	// bottom left back
	float xblb = xblf;
	float yblb = yblf;
	float zblb = ztrb;

	// bottom right back
	float xbrb = xtrb;
	float ybrb = yblf;
	float zbrb = ztrb;

	// top left back
	float xtlb = xblf;
	float ytlb = ytrb;
	float ztlb = ztrb;

	// top left front
	float xtlf = xtlb;
	float ytlf = ytrb;
	float ztlf = zblf;

	// top right front
	float xtrf = xbrf;
	float ytrf = ytrb;
	float ztrf = zblf;

	for (auto i = 0u; i < _planes.size(); ++i)
	{
		const auto planeId = (basePlaneId + i) % _planes.size();

		float pa = _planes[planeId].x;
		float pb = _planes[planeId].y;
		float pc = _planes[planeId].z;
		float pd = _planes[planeId].w;

		_blfResult[planeId] = pa * xblf + pb * yblf + pc * zblf + pd < 0.f;
		_brfResult[planeId] = pa * xbrf + pb * ybrf + pc * zbrf + pd < 0.f;
		_blbResult[planeId] = pa * xblb + pb * yblb + pc * zblb + pd < 0.f;
		_brbResult[planeId] = pa * xbrb + pb * ybrb + pc * zbrb + pd < 0.f;
		
		_tlfResult[planeId] = pa * xtlf + pb * ytlf + pc * ztlf + pd < 0.f;
		_trfResult[planeId] = pa * xtrf + pb * ytrf + pc * ztrf + pd < 0.f;
		_tlbResult[planeId] = pa * xtlb + pb * ytlb + pc * ztlb + pd < 0.f;
		_trbResult[planeId] = pa * xtrb + pb * ytrb + pc * ztrb + pd < 0.f;

		if (_blfResult[planeId] &&
			_brfResult[planeId] &&
			_blbResult[planeId] &&
			_brbResult[planeId] &&
			_tlfResult[planeId] &&
			_trfResult[planeId] &&
			_tlbResult[planeId] &&
			_trbResult[planeId])
		{
			return std::make_pair(static_cast<ShapePosition>(planeId), planeId);
		}
	}

	if (((_blfResult[(int)PlanePosition::LEFT]	&& _trbResult[(int)PlanePosition::RIGHT]) ||
		(_blfResult[(int)PlanePosition::RIGHT]	&& _trbResult[(int)PlanePosition::LEFT])) &&
		((_blfResult[(int)PlanePosition::TOP]	&& _trbResult[(int)PlanePosition::BOTTOM]) ||
		(_blfResult[(int)PlanePosition::BOTTOM] && _trbResult[(int)PlanePosition::TOP])) &&
        ((_blfResult[(int)PlanePosition::NEAR]	&& _trbResult[(int)PlanePosition::FAR]) ||
		(_blfResult[(int)PlanePosition::FAR]	&& _trbResult[(int)PlanePosition::NEAR])))
		return std::make_pair(ShapePosition::AROUND, 0u);

	return std::make_pair(ShapePosition::INSIDE, 0u);
}