Fixed raycast sphere.
This commit is contained in:
@ -14,20 +14,23 @@ bool_t Dawn::raytestSphere(
|
||||
glm::vec3 *normal,
|
||||
float_t *distance
|
||||
) {
|
||||
assertNotNull(hit);
|
||||
assertNotNull(normal);
|
||||
float_t a = glm::dot(ray.direction, ray.direction);
|
||||
float_t b = 2.0f * glm::dot(ray.direction, ray.origin - sphere.center);
|
||||
float_t c = glm::dot(ray.origin - sphere.center, ray.origin - sphere.center);
|
||||
c -= sphere.radius * sphere.radius;
|
||||
|
||||
glm::vec3 h, n;
|
||||
auto result = glm::intersectRaySphere(
|
||||
ray.origin, ray.direction,
|
||||
sphere.center, sphere.radius,
|
||||
h, n
|
||||
);
|
||||
if(!result) return result;
|
||||
*hit = h;
|
||||
*normal = n;
|
||||
*distance = glm::distance(ray.origin, h);
|
||||
return result;
|
||||
float_t dt = b * b - 4.0f * a * c;
|
||||
if(dt < 0.0f) return false;
|
||||
|
||||
float_t t0 = (-b - sqrtf(dt)) / (a * 2.0f);
|
||||
if(t0 < 0.0f) return false;
|
||||
|
||||
*hit = ray.origin + t0 * ray.direction;
|
||||
*normal = glm::normalize(*hit - sphere.center);
|
||||
*distance = t0;
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool_t Dawn::raytestTriangle(
|
||||
|
Reference in New Issue
Block a user