Example #1
0
bool triangle::intersect(ray const& Ray, intersection& Intersection) const
{
    glm::vec3 w = Ray.get_position() - A;
    glm::vec3 u = -(B - A);
    glm::vec3 v = -(C - A);
    float D = glm::dot(glm::cross(u, v), Ray.get_direction());
    float a = -glm::dot(glm::cross(w, v), Ray.get_direction()) / D;
    float b = -glm::dot(glm::cross(u, w), Ray.get_direction()) / D;
    float t = glm::dot(glm::cross(u, v), w) / D;

    if(a > 0.0f && b > 0.0f && a + b < 1.0)
        return true;
    return false;
}
Example #2
0
bool plane::intersect(ray const& Ray, intersection& Intersection) const
{
	bool hit = false;

	if(glm::abs(Ray.get_direction().z) > 0.0f)
	{
		float t = -Ray.get_position().z / Ray.get_direction().z;
		if(t > glm::epsilon<float>() * 100.f)
		{
			Intersection.set_local_position(Ray.get_position() + Ray.get_direction() * t);
			hit = true;
		}
	}
	return hit;
}
Example #3
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;
	}
Example #4
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;
	}
Example #5
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;
	}