blob: 22a24d15e8eaadc8c89a8b856f7e152858652db9 [file] [log] [blame]
// Copyright 2017 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "garnet/bin/ui/scene_manager/resources/shapes/planar_shape.h"
namespace scene_manager {
PlanarShape::PlanarShape(Session* session,
scenic::ResourceId id,
const ResourceTypeInfo& type_info)
: Shape(session, id, type_info) {}
bool PlanarShape::GetIntersection(const escher::ray4& ray,
float* out_distance) const {
// Reject if the ray origin is behind the Z=0 plane.
if (ray.origin.z < 0.f)
return false;
// Reject if the ray is not pointing down towards the Z=0 plane.
float delta_z = -ray.direction.z;
if (delta_z < std::numeric_limits<float>::epsilon())
return false;
// Compute the distance to the plane in multiples of the ray's direction
// vector and the point of intersection.
float distance = ray.origin.z / delta_z;
escher::vec2 point =
(escher::vec2(ray.origin) + distance * escher::vec2(ray.direction)) /
ray.origin.w;
// Reject if the shape does not contain the point of intersection.
if (!ContainsPoint(point))
return false;
*out_distance = distance;
return true;
}
} // namespace scene_manager