tangled
alpha
login
or
join now
altagos.dev
/
rayray
this repo has no description
0
fork
atom
overview
issues
pulls
pipelines
colorfull sphere
altagos.dev
2 years ago
044c78d3
ed1bc978
+20
-9
1 changed file
expand all
collapse all
unified
split
src
ray.zig
+20
-9
src/ray.zig
···
12
12
};
13
13
}
14
14
15
15
-
pub fn color(self: *Ray) zm.Vec {
16
16
-
if (hit_sphere(zm.f32x4(0, 0, -1, 0), 0.5, self)) {
17
17
-
return zm.f32x4(1, 0, 0, 1);
15
15
+
pub fn at(self: *Ray, t: f32) zm.Vec {
16
16
+
return self.orig + zm.f32x4s(t) * self.dir;
17
17
+
}
18
18
+
19
19
+
pub fn color(r: *Ray) zm.Vec {
20
20
+
const t = hitSphere(zm.f32x4(0, 0, -1, 0), 0.5, r);
21
21
+
if (t > 0.0) {
22
22
+
const N = zm.normalize3(r.at(t) - zm.f32x4(0, 0, -1, 0));
23
23
+
return zm.f32x4s(0.5) * zm.f32x4(N[0] + 1, N[1] + 1, N[2] + 1, 1);
18
24
}
19
25
20
20
-
const unit_direction = zm.normalize3(self.dir);
26
26
+
const unit_direction = zm.normalize3(r.dir);
21
27
const a = 0.5 * (unit_direction[1] + 1.0);
22
28
return zm.f32x4s(1.0 - a) * zm.f32x4s(1.0) + zm.f32x4s(a) * zm.f32x4(0.5, 0.7, 1.0, 1.0);
23
29
}
24
30
25
25
-
fn hit_sphere(center: zm.Vec, radius: f32, r: *Ray) bool {
31
31
+
fn hitSphere(center: zm.Vec, radius: f32, r: *Ray) f32 {
26
32
const oc = r.orig - center;
27
27
-
const a = zm.dot3(r.dir, r.dir)[0];
28
28
-
const b = 2.0 * zm.dot3(oc, r.dir)[0];
33
33
+
const a = zm.lengthSq3(r.dir)[0];
34
34
+
const half_b = zm.dot3(oc, r.dir)[0];
29
35
const c = zm.dot3(oc, oc)[0] - radius * radius;
30
30
-
const discriminant = b * b - 4 * a * c;
31
31
-
return discriminant >= 0;
36
36
+
const discriminant = half_b * half_b - a * c;
37
37
+
38
38
+
if (discriminant < 0) {
39
39
+
return -1.0;
40
40
+
} else {
41
41
+
return (-half_b - @sqrt(discriminant)) / a;
42
42
+
}
32
43
}