Finish Week 1 (Could be a bug)

This commit is contained in:
2024-09-25 16:29:49 +02:00
parent 834a6f7cff
commit 254a796fac
6 changed files with 91 additions and 29 deletions

View File

@@ -11,9 +11,34 @@ namespace dae
//SPHERE HIT-TESTS
inline bool HitTest_Sphere(const Sphere& sphere, const Ray& ray, HitRecord& hitRecord, bool ignoreHitRecord = false)
{
//todo W1
throw std::runtime_error("Not Implemented Yet");
return false;
const float radius2 = sphere.radius * sphere.radius;
const Vector3 CameraToOrigin = sphere.origin - ray.origin;
const float tInSphere = Vector3::Dot(CameraToOrigin, ray.direction);
if(tInSphere < 0) return false; //Looking away from sphere
const float od2 = std::pow(Vector3::Reject(CameraToOrigin, ray.direction).Magnitude(),2);
const float thc = std::sqrt(radius2 - od2);
float t0 = tInSphere - thc;
float t1 = tInSphere + thc;
if(t0 < 0 && t1 < 0) return false; //Both intersections are behind the camera
if(t0 < 0) t0 = t1; //If t0 is behind the camera, use t1
if (!ignoreHitRecord && t0 < hitRecord.t){
hitRecord.t = t0;
hitRecord.origin = ray.origin + ray.direction * t0;
hitRecord.normal = (hitRecord.origin - sphere.origin).Normalized();
hitRecord.materialIndex = sphere.materialIndex;
hitRecord.didHit = true;
return true;
}
return false;
}
inline bool HitTest_Sphere(const Sphere& sphere, const Ray& ray)
@@ -26,9 +51,24 @@ namespace dae
//PLANE HIT-TESTS
inline bool HitTest_Plane(const Plane& plane, const Ray& ray, HitRecord& hitRecord, bool ignoreHitRecord = false)
{
//todo W1
throw std::runtime_error("Not Implemented Yet");
return false;
float dot = Vector3::Dot(plane.normal, ray.direction);
if(dot < 0) {
float t = Vector3::Dot(plane.origin - ray.origin, plane.normal) / dot;
bool hit = t >= 0;
if(hit && !ignoreHitRecord){
hitRecord.t = t;
hitRecord.origin = ray.origin + ray.direction * t;
hitRecord.normal = plane.normal;
hitRecord.materialIndex = plane.materialIndex;
hitRecord.didHit = true;
}
return hit;
}
return false;
}
inline bool HitTest_Plane(const Plane& plane, const Ray& ray)