Add camera and shadows (week2)

This commit is contained in:
2024-10-08 21:37:45 +02:00
parent de2e9704c5
commit b7106c2af8
4 changed files with 114 additions and 30 deletions

View File

@@ -17,27 +17,26 @@ namespace dae
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 originToInsideDist2 = CameraToOrigin.SqrMagnitude() - tInSphere * tInSphere;
if(originToInsideDist2 > radius2)
return false; // 'inside' point is outside of sphere
float t0 = tInSphere - thc;
float t1 = tInSphere + thc;
float tDiff = std::sqrt(radius2 - originToInsideDist2);
float t = tInSphere - tDiff;
if(t0 < 0 && t1 < 0) return false; //Both intersections are behind the camera
const bool hit = t > ray.min && t < ray.max;
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;
if (!ignoreHitRecord && hit){
hitRecord.t = t;
hitRecord.origin = ray.origin + ray.direction * t;
hitRecord.normal = (hitRecord.origin - sphere.origin).Normalized();
hitRecord.materialIndex = sphere.materialIndex;
hitRecord.didHit = true;
return true;
}
return false;
return hit;
}
@@ -54,8 +53,10 @@ namespace dae
float dot = Vector3::Dot(plane.normal, ray.direction);
if(dot < 0) {
// float t = Vector3::Dot(plane.origin - ray.origin, plane.normal) / dot;
float t = Vector3::Dot(plane.origin - ray.origin, plane.normal) / Vector3::Dot(ray.direction, plane.normal);
bool hit = t >= 0;
float t = Vector3::Dot(plane.origin - ray.origin, plane.normal);
t /= dot;
const bool hit = t > ray.min && t < ray.max;
if(hit && !ignoreHitRecord){
hitRecord.t = t;
@@ -113,9 +114,7 @@ namespace dae
//Direction from target to light
inline Vector3 GetDirectionToLight(const Light& light, const Vector3 origin)
{
//todo W3
throw std::runtime_error("Not Implemented Yet");
return {};
return { light.origin - origin };
}
inline ColorRGB GetRadiance(const Light& light, const Vector3& target)