Пример #1
0
	bool bounding_box::intersects(const ray & ray, intersection_data & out) const
	{
		float distance_max = std::numeric_limits<float>::max();

		const box & aabb = bounding_box::get_box();
		const point & min = aabb.get_min();
		const point & max = aabb.get_max();

		const float epsilon = 0.01f;

		for (int i = 0; i < 3; i++)
		{
			if (std::abs(ray.get_direction()[i]) < epsilon)
			{
				if (ray.get_origin()[i] < min[i] || ray.get_origin()[i] > max[i])
					return false;
			}
			else
			{
				float ood = 1.0f / ray.get_direction()[i];
				float t1 = (min[i] - ray.get_origin()[i]) * ood;
				float t2 = (max[i] - ray.get_origin()[i]) * ood;

				if (t1 > t2) std::swap(t1, t2);

				if (t1 > out.distance) out.distance = t1;
				if (t2 > distance_max) distance_max = t2;

				if (out.distance > distance_max)
					return false;
			}
		}

		out.coordinates = ray.get_origin() + float3(ray.get_direction());
		out.coordinates = out.coordinates * out.distance;

		return true;
	}
Пример #2
0
	bool bounding_sphere::intersects(const ray & ray, intersection_data & out) const
	{
		point m = ray.get_origin() - origin;
		point b = point(XMVector3Dot(m, ray.get_direction()));
		point c = point(XMVector3Dot(m, m)) - std::pow(get_radius(), 2);

		if (c > 0.f && b > 0.f)
			return false;

		float disc = std::pow(b[axis::x], 2) - c[axis::x];

		if (disc < 0.f)
			return false;

		out.distance = -b[axis::x] - std::sqrt(disc);
		
		if (out.distance < 0.f)
			out.distance = 0.f;

		out.coordinates = ray.get_origin() + point(out.distance) * float3(ray.get_direction());

		return true;
	}
Пример #3
0
	bool bounding_sphere::intersects(const ray & ray) const
	{
		point m = ray.get_origin() - origin;
		point c = point(XMVector3Dot(m, m)) - std::pow(get_radius(), 2);

		if (c <= 0.f)
			return true;

		point b = XMVector3Dot(m, ray.get_direction());

		if (b > 0.f)
			return false;

		float disc = std::pow(b[axis::x], 2) - c[axis::x];

		return disc >= 0.f;
	}