display normal on sphere
This commit is contained in:
parent
14c91ed8e5
commit
6a0e48a006
|
@ -16,21 +16,27 @@ const SDL_WINDOWPOS_UNDEFINED = @bitCast(c_int, sdl.c.SDL_WINDOWPOS_UNDEFINED_MA
|
||||||
|
|
||||||
const fps = 60;
|
const fps = 60;
|
||||||
|
|
||||||
fn hitSphere(center: Point3, radius: f32, ray: Ray) bool {
|
fn hitSphere(center: Point3, radius: f32, ray: Ray) f32 {
|
||||||
const ssr = ray.origin.sub(center); // Sphere-space ray, (A - C) in book
|
const ssr = ray.origin.sub(center); // Sphere-space ray, (A - C) in book
|
||||||
const a = Vec3.dot(ray.direction, ray.direction);
|
const a = Vec3.dot(ray.direction, ray.direction);
|
||||||
const b = 2.0 * Vec3.dot(ssr, ray.direction);
|
const b = 2.0 * Vec3.dot(ssr, ray.direction);
|
||||||
const c = Vec3.dot(ssr, ssr) - (radius * radius);
|
const c = Vec3.dot(ssr, ssr) - (radius * radius);
|
||||||
const discriminant = b * b - 4 * a * c;
|
const discriminant = b * b - 4 * a * c;
|
||||||
return discriminant > 0;
|
if (discriminant < 0) {
|
||||||
|
return -1;
|
||||||
|
} else {
|
||||||
|
return (-b - std.math.sqrt(discriminant)) / (2 * a);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rayColor(ray: Ray) Color {
|
fn rayColor(ray: Ray) Color {
|
||||||
if (hitSphere(Point3{ .x = 0, .y = 0, .z = -1 }, 0.5, ray)) {
|
var t = hitSphere(Point3{ .x = 0, .y = 0, .z = -1 }, 0.5, ray);
|
||||||
return Color{ .x = 1, .y = 0, .z = 0 };
|
if (t > 0) {
|
||||||
|
const normal = vec3.unitVector(ray.at(t).sub(Vec3{ .z = -1 }));
|
||||||
|
return normal.add(Vec3{ .x = 1, .y = 1, .z = 1}).div(2);
|
||||||
}
|
}
|
||||||
const unitDirection = vec3.unitVector(ray.direction);
|
const unitDirection = vec3.unitVector(ray.direction);
|
||||||
const t = 0.5 * (unitDirection.y + 1.0);
|
t = 0.5 * (unitDirection.y + 1.0);
|
||||||
const white = Color{ .x = 1.0, .y = 1.0, .z = 1.0 };
|
const white = Color{ .x = 1.0, .y = 1.0, .z = 1.0 };
|
||||||
const blue = Color{ .x = 0.5, .y = 0.7, .z = 1.0 };
|
const blue = Color{ .x = 0.5, .y = 0.7, .z = 1.0 };
|
||||||
return white.mul(1.0 - t).add(blue.mul(t));
|
return white.mul(1.0 - t).add(blue.mul(t));
|
||||||
|
@ -45,7 +51,7 @@ pub fn main() anyerror!void {
|
||||||
|
|
||||||
// Image
|
// Image
|
||||||
const aspectRatio: f32 = 16.0 / 9.0;
|
const aspectRatio: f32 = 16.0 / 9.0;
|
||||||
const imageWidth = 400;
|
const imageWidth = 600;
|
||||||
const imageHeight = @floatToInt(usize, imageWidth / aspectRatio);
|
const imageHeight = @floatToInt(usize, imageWidth / aspectRatio);
|
||||||
|
|
||||||
// Camera
|
// Camera
|
||||||
|
@ -87,7 +93,7 @@ pub fn main() anyerror!void {
|
||||||
const v = @intToFloat(f32, (imageHeight - 1) - j) / @intToFloat(f32, (imageHeight - 1));
|
const v = @intToFloat(f32, (imageHeight - 1) - j) / @intToFloat(f32, (imageHeight - 1));
|
||||||
const r = Ray{
|
const r = Ray{
|
||||||
.origin = origin,
|
.origin = origin,
|
||||||
.direction = lowerLeftCorner.add(horizontal.mul(u).add(vertical.mul(v).sub(origin))),
|
.direction = lowerLeftCorner.add(horizontal.mul(u)).add(vertical.mul(v)).sub(origin),
|
||||||
};
|
};
|
||||||
const pixelColor = rayColor(r);
|
const pixelColor = rayColor(r);
|
||||||
sdl.setSurfacePixel(surface, i, j, pixelColor);
|
sdl.setSurfacePixel(surface, i, j, pixelColor);
|
||||||
|
|
|
@ -5,7 +5,7 @@ pub const Ray = struct {
|
||||||
origin: Point3,
|
origin: Point3,
|
||||||
direction: Vec3,
|
direction: Vec3,
|
||||||
|
|
||||||
pub fn at(t: f32) Point3 {
|
pub fn at(self: Ray, t: f32) Point3 {
|
||||||
return origin.add(direction.mul(t));
|
return self.origin.add(self.direction.mul(t));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue