Browse Source

finished exercise 05b

master
fdai7303 1 year ago
parent
commit
8ebdd5fb3d
  1. 18
      src/05/manyspheres.ts
  2. 19
      src/05/raytracing.ts
  3. 4
      src/05/sphere.ts

18
src/05/manyspheres.ts

@ -25,4 +25,22 @@ export function raytrace(data: Uint8ClampedArray,
// TODO: Generate ray and perform intersection with every sphere. // TODO: Generate ray and perform intersection with every sphere.
// TODO: On intersection set pixel color to color of the sphere // TODO: On intersection set pixel color to color of the sphere
// TODO: containing the closest intersection point. // TODO: containing the closest intersection point.
var distance = 90000;
var light = Ray.makeRay(x, y, camera);
for(let i = 0; i < spheres.length; i++) {
let intersection = spheres[i].intersect(light);
if(intersection != null) {
if(distance > intersection.t) {
distance = intersection.t;
var posInArrayX = x * 4;
var posInArrayY = y * 4 * width;
data[0 + posInArrayX + posInArrayY] = spheres[i].color.r*255;
data[1 + posInArrayX + posInArrayY] = spheres[i].color.g*255;
data[2 + posInArrayX + posInArrayY] = spheres[i].color.b*255;
data[3 + posInArrayX + posInArrayY] = spheres[i].color.a*255;
}
}
}
} }

19
src/05/raytracing.ts

@ -1,6 +1,7 @@
import Camera from './camera'; import Camera from './camera';
import Sphere from './sphere'; import Sphere from './sphere';
import Ray from './ray'; import Ray from './ray';
import Vector from './vector';
/** /**
* Compute the color of the pixel (x, y) by raytracing * Compute the color of the pixel (x, y) by raytracing
@ -25,4 +26,22 @@ export function raytrace(data: Uint8ClampedArray,
// TODO: (x, y) in the camera's image plane, and perform intersection // TODO: (x, y) in the camera's image plane, and perform intersection
// TODO: with the given sphere. Set color of pixel (x, y) in the data // TODO: with the given sphere. Set color of pixel (x, y) in the data
// TODO: array to black, if the ray hits the sphere. // TODO: array to black, if the ray hits the sphere.
var light = Ray.makeRay(x, y, camera);
if(sphere.intersect(light) != null) {
var posInArrayX = x * 4;
var posInArrayY = y * 4 * width;
data[0 + posInArrayX + posInArrayY] = 0;
data[1 + posInArrayX + posInArrayY] = 0;
data[2 + posInArrayX + posInArrayY] = 0;
data[3 + posInArrayX + posInArrayY] = 255;
} else {
var posInArrayX = x * 4;
var posInArrayY = y * 4 * width;
data[0 + posInArrayX + posInArrayY] = 255;
data[1 + posInArrayX + posInArrayY] = 255;
data[2 + posInArrayX + posInArrayY] = 255;
data[3 + posInArrayX + posInArrayY] = 255;
}
} }

4
src/05/sphere.ts

@ -54,11 +54,11 @@ export default class Sphere {
} else if(c == 0) { } else if(c == 0) {
//PQ Formel //PQ Formel
let t1 = - newCenter.dot(ray.direction); 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
return new Intersection(t1, ray.origin.add(ray.direction.mul(t1)), (ray.origin.add(ray.direction.mul(t1)).sub(this.center)).normalize() ); //schnittpunkt ist origin + direction * t
} else { } else {
let t2 = - newCenter.dot(ray.direction) - Math.sqrt(c); 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
return new Intersection(t2, ray.origin.add(ray.direction.mul(t2)), (ray.origin.add(ray.direction.mul(t2)).sub(this.center)).normalize() ); //schnittpunkt ist origin + direction * t
} }
} }
} }
Loading…
Cancel
Save