#pragma once #include "matrices.h" #include "materials.h" struct sphere { struct mat4x4 transform; struct material material; }; inline static struct sphere sphere() { return (struct sphere){ mat4x4_identity(), material() }; } inline static struct tuple sphere_normal_at(struct sphere const * const s, struct tuple world_point) { struct mat4x4 inv = mat4x4_inverse(&s->transform); struct tuple object_point = mat4x4_mul_t(&inv, &world_point); struct tuple object_normal = tuple_sub(object_point, point(0.0f, 0.0f, 0.0f)); struct mat4x4 inv_t = mat4x4_transpose(&inv); struct tuple world_normal = mat4x4_mul_t(&inv_t, &object_normal); world_normal.w = 0.0f; return tuple_normalize(world_normal); }