~turminal/raytracing

0b25dcbe5801ef13d5075ea99b5c21367426ee85 — Bor Grošelj Simić 2 years ago 8b7388d
all: drop some workarounds that are not needed anymore
3 files changed, 16 insertions(+), 15 deletions(-)

M color.ha
M main.ha
M util.ha
M color.ha => color.ha +7 -7
@@ 1,4 1,4 @@
use math::{sqrtf64};
use math::{sqrtf64, nextafterf64};

export type color = struct {
	r: f64,


@@ 19,16 19,16 @@ export type rgb = struct {
};

export fn torgb(c: color, scale: f64) rgb = rgb {
	r = (clamp(sqrtf64(c.r * scale), 0.0, 0.9999) * 256f64): int: u8, // TODO drop the int cast
	g = (clamp(sqrtf64(c.g * scale), 0.0, 0.9999) * 256f64): int: u8, // TODO nextafter()
	b = (clamp(sqrtf64(c.b * scale), 0.0, 0.9999) * 256f64): int: u8,
	r = (clamp(sqrtf64(c.r * scale), 0.0, nextafterf64(1.0, 0.0)) * 256.0): u8,
	g = (clamp(sqrtf64(c.g * scale), 0.0, nextafterf64(1.0, 0.0)) * 256.0): u8,
	b = (clamp(sqrtf64(c.b * scale), 0.0, nextafterf64(1.0, 0.0)) * 256.0): u8,
};

export fn fromrgb(c: rgb) color = {
	let c = color {
		r = c.r: i32: f64 / 256f64,
		g = c.g: i32: f64 / 256f64,
		b = c.b: i32: f64 / 256f64,
		r = c.r: f64 / 256f64,
		g = c.g: f64 / 256f64,
		b = c.b: f64 / 256f64,
	};
	return color {
		r = c.r * c.r,

M main.ha => main.ha +6 -6
@@ 147,7 147,7 @@ export fn main() void = {
	let from = V(13.0, 2.0, 3.0);
	let at = V(0.0, 0.0, 0.0);
	let c = make_camera(from, at, V(0.0, 1.0, 0.0),
		20.0, width: i64: f64 / height: i64: f64, 0.1, 10.0);
		20.0, width: f64 / height: f64, 0.1, 10.0);

	let scene = init_scene();



@@ 156,16 156,16 @@ export fn main() void = {
	for (let j = begin; j < end; j += 1) {
		fmt::errorf("\rProgress: begin: {}  current: {}  end: {} | {}%",
			begin, j, end,
			((j - begin): i64: f64 / (end - begin): i64: f64 * 100.0): int)!;
			((j - begin): f64 / (end - begin): f64 * 100.0): int)!;
		for (let i = 0z; i < width; i += 1) {
			for (let k = 0u; k < samples; k += 1) {
				let u = i: int: f64;
				let u = i: f64;
				u += random(0.0, 1.0);
				u /= (width: int: f64 - 1.0);
				u /= (width: f64 - 1.0);

				let v = j: int: f64;
				let v = j: f64;
				v += random(0.0, 1.0);
				v /= (height: int: f64 - 1.0);
				v /= (height: f64 - 1.0);

				let r = get_ray_for(&c, u, v);
				let c = compute_color(scene, &r, max_bounce);

M util.ha => util.ha +3 -2
@@ 1,12 1,13 @@
use math;
use math::random;
use types;

let rng: math::random::random = 0u64;

export fn random(min: f64, max: f64) f64 = {
	// TODO: drop the signed cast
	let n = math::random::next(&rng);
	return min + (max - min) * (n: i64: f64 / types::I64_MAX: f64);
	return min + (max - min) *
		math::nextafterf64(n: f64 / types::I64_MAX: f64, 0.0);
};

export fn clamp(x: f64, min: f64, max: f64) f64 =