Post by simianarmy on May 5, 2019 3:48:22 GMT
I have made the modifications to the previous project (where the red sphere was correctly rendered), but cannot get the sphere to render like the one in the reference picture.
Here are the initial values I am using for the scene:
and here is the ray casting code:
Basically intersect never returns a non-empty array, so nothing is drawn.
Do you see any logic errors here?
Here are the initial values I am using for the scene:
const light = PointLight(point(-10, 10, -10), Color(1, 1, 1));
const wallZ = 10;
const wallSize = 7;
const canvasPixels = 100;
const pixelSize = wallSize / canvasPixels;
const sphere = Sphere(); // unit sphere at origin
sphere.material.color = Color(1, 0.2, 1);
and here is the ray casting code:
const half = wallSize / 2;
for (let y = 0; y < canvasPixels; y++) {
// compute the world y coordinate (top = +half, bottom = -half)
let worldY = half - pixelSize * y;
for (let x = 0; x < canvasPixels; x++) {
let worldX = -half + pixelSize * x;
// cast ray from lightsource to pixel on wall
const wallPoint = point(worldX, worldY, wallZ);
const lightToPixel = normalize(sub(wallPoint, light.position));
const ray = Ray(light.position, lightToPixel);
const xs = intersect(sphere, ray);
// if intersections, color the pixel
if (xs.length > 0) {
// find normal at hit
const closest = hit(xs);
const hitPos = position(ray, closest.t);
const normal = normalAt(closest.object, hitPos);
// calculate eye vector?
const eye = negate(ray.direction);
// calculate color at hit point
const surfaceColor = lighting(closest.object.material, light, hitPos, eye, normal);
canvas.writePixel(x, y, surfaceColor);
}
}
}
Basically intersect never returns a non-empty array, so nothing is drawn.
Do you see any logic errors here?