From 21458277fcebef496315ecbca1d11044c4e0102b Mon Sep 17 00:00:00 2001 From: fdai7303 Date: Thu, 29 Jun 2023 07:58:13 +0200 Subject: [PATCH] sphere almost finished --- src/05/ray.ts | 16 ++++++++++------ src/05/sphere.ts | 19 ++++++++----------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/05/ray.ts b/src/05/ray.ts index 3bebd27..ad9c521 100644 --- a/src/05/ray.ts +++ b/src/05/ray.ts @@ -33,13 +33,17 @@ export default class Ray { // TODO: on the image plane. In addition to the coordinates (x, y), you will need the // TODO: width and height of the camera (i.e. the width and height of the camera's // TODO: image plane), and the angle alpha specifying the camera's field of view. - var temp = new Vector(0,0,0,0); - temp.x = x - ((camera.width-1) / 2); - temp.y = ((camera.height-1) / 2) - y; - temp.z = - ((camera.width/2) / (Math.tan(camera.alpha/2))); + var a = x - ((camera.width-1) / 2); + var b = ((camera.height-1) / 2) - y; + var c = - ((camera.width/2) / (Math.tan(camera.alpha/2))); - temp = temp.normalize(); + //temp = temp.normalize(); - return new Ray(camera.origin, temp); + var nom = Math.sqrt(a*a + b*b + c*c); + let direction = new Vector((x-(camera.width-1)/2)/nom, + ((camera.height-1)/2-y)/nom, + (-((camera.width/2)/Math.tan(camera.alpha/2)))/nom,0); + + return new Ray(camera.origin, direction); } } diff --git a/src/05/sphere.ts b/src/05/sphere.ts index 2024b7a..2142f6a 100644 --- a/src/05/sphere.ts +++ b/src/05/sphere.ts @@ -45,23 +45,20 @@ export default class Sphere { // TODO: of two hits, return the one closer to the start point of // TODO: the ray. - let c = Math.pow(ray.origin.dot(ray.direction),2) - ray.origin.dot(ray.origin) + Math.pow(this.radius,2); - // hier oben noch nicht berechenenm da c negativ sein kann und dann wurzel nicht funktioniert + let newCenter = ray.origin.sub(this.center); + // hier oben noch nicht t berechenenm da c negativ sein kann und dann wurzel nicht funktioniert + let c = Math.pow(newCenter.dot(ray.direction),2) - newCenter.dot(newCenter) + Math.pow(this.radius,2); if(c < 0) { return null; } else if(c == 0) { - return new Intersection(t1, ); //schnittpunkt ist origin + direction * t + //PQ Formel + let t1 = - newCenter.dot(ray.direction); + return new Intersection(t1, newCenter.add(ray.direction.mul(t1)), (newCenter.add(ray.direction.mul(t1)).sub(this.center)).normalize() ); //schnittpunkt ist origin + direction * t } else { - let t1 = - ray.origin.dot(ray.direction) + Math.sqrt( Math.pow(ray.origin.dot(ray.direction),2) - ray.origin.dot(ray.origin) + Math.pow(this.radius,2)); - let t2 = - ray.origin.dot(ray.direction) - Math.sqrt( Math.pow(ray.origin.dot(ray.direction),2) - ray.origin.dot(ray.origin) + Math.pow(this.radius,2)); - - if(t1 < t2) { - return null; - } else { - return null; - } + let t2 = - newCenter.dot(ray.direction) - Math.sqrt(c); + return new Intersection(t2, newCenter.add(ray.direction.mul(t2)), (newCenter.add(ray.direction.mul(t2)).sub(this.center)).normalize() ); //schnittpunkt ist origin + direction * t } } }