Render surface normals
This commit is contained in:
parent
e1173e5bd3
commit
647abd385d
17
main.c
17
main.c
@ -1,3 +1,4 @@
|
|||||||
|
#include <math.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
@ -6,20 +7,26 @@
|
|||||||
#include "ray.h"
|
#include "ray.h"
|
||||||
#include "vec3.h"
|
#include "vec3.h"
|
||||||
|
|
||||||
bool hit_sphere(point3 center, double radius, ray r) {
|
double hit_sphere(point3 center, double radius, ray r) {
|
||||||
vec3 oc = point3_sub(r.origin, center);
|
vec3 oc = point3_sub(r.origin, center);
|
||||||
double a = vec3_length2(r.direction);
|
double a = vec3_length2(r.direction);
|
||||||
double b = 2 * vec3_dot(oc, r.direction);
|
double b = 2 * vec3_dot(oc, r.direction);
|
||||||
double c = vec3_length2(oc) - radius * radius;
|
double c = vec3_length2(oc) - radius * radius;
|
||||||
double discriminant = b * b - 4 * a * c;
|
double discriminant = b * b - 4 * a * c;
|
||||||
return discriminant > 0;
|
if (discriminant < 0)
|
||||||
|
return -1.0;
|
||||||
|
else
|
||||||
|
return (-b - sqrt(discriminant)) / (2.0 * a);
|
||||||
}
|
}
|
||||||
|
|
||||||
color ray_color(ray r) {
|
color ray_color(ray r) {
|
||||||
if (hit_sphere((point3){0, 0, -1}, 0.5, r))
|
double t = hit_sphere((point3){0, 0, -1}, 0.5, r);
|
||||||
return (color){1, 0, 0};
|
if (t > 0.0) {
|
||||||
|
vec3 n = vec3_normalize(point3_sub(ray_at(r, t), (point3){0, 0, -1}));
|
||||||
|
return (color){0.5 * (n.x + 1), 0.5 * (n.y + 1), 0.5 * (n.z + 1)};
|
||||||
|
}
|
||||||
vec3 unit_direction = vec3_normalize(r.direction);
|
vec3 unit_direction = vec3_normalize(r.direction);
|
||||||
double t = 0.5 * (unit_direction.y + 1.0);
|
t = 0.5 * (unit_direction.y + 1.0);
|
||||||
color gradient1 = {1.0, 1.0, 1.0};
|
color gradient1 = {1.0, 1.0, 1.0};
|
||||||
color gradient2 = {0.5, 0.7, 1.0};
|
color gradient2 = {0.5, 0.7, 1.0};
|
||||||
return color_lerp(gradient1, gradient2, t);
|
return color_lerp(gradient1, gradient2, t);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user