Skip to content

Commit

Permalink
Update material implementation (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
gau-nernst authored Nov 29, 2023
1 parent 621fbfa commit 267d6ef
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 70 deletions.
6 changes: 5 additions & 1 deletion include/hittable.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#ifndef HITTABLE_H
#define HITTABLE_H

#include "material.h"
#include "vec3.h"
#include <stdbool.h>
#include <stddef.h>

typedef struct Texture Texture;
typedef struct Material Material;
typedef struct HitRecord HitRecord;

typedef struct Ray {
Vec3 origin;
Expand Down
42 changes: 23 additions & 19 deletions include/material.h
Original file line number Diff line number Diff line change
@@ -1,39 +1,43 @@
#ifndef MATERIAL_H
#define MATERIAL_H

#include "texture.h"
#include "utils.h"
#include "vec3.h"
#include <stdbool.h>

typedef struct Texture Texture;
typedef struct Material Material;
typedef struct HitRecord HitRecord;
typedef struct Ray Ray;

typedef struct MaterialVTable {
bool (*scatter)(const HitRecord *rec, Vec3 r_in, Vec3 *r_out, Vec3 *color, PCG32 *rng);
float (*scatter_pdf)(Vec3 normal, Vec3 r_in, Vec3 r_out);
Vec3 (*emit)(const HitRecord *rec);
} MaterialVTable;
typedef struct HitRecord {
Vec3 p;
Vec3 normal;
Material *material;
float t;
float u;
float v;
bool front_face;
} HitRecord;

typedef enum MaterialType {
SURFACE_NORMAL,
LAMBERTIAN,
METAL,
DIELECTRIC,
DIFFUSE_LIGHT,
ISOTROPIC,
} MaterialType;

struct Material {
MaterialVTable *vtable;
MaterialType tag;
Texture *albedo;
union {
float fuzz; // for metal
float eta; // for dielectric
};
};

struct HitRecord {
Vec3 p;
Vec3 normal;
Material *material;
float t;
float u;
float v;
bool front_face;
};
bool Material_scatter(const HitRecord *rec, Vec3 r_in, Vec3 *r_out, Vec3 *color, bool *skip_pdf, PCG32 *rng);
float Material_scatter_pdf(const Material *mat, Vec3 normal, Vec3 r_in, Vec3 r_out);
Vec3 Material_emit(const HitRecord *rec);

void SurfaceNormal_init(Material *self);
Material *SurfaceNormal_new();
Expand Down
2 changes: 1 addition & 1 deletion src/hittable.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "hittable.h"
#include "material.h"
#include "pcg32.h"
#include "vec3.h"
#include "utils.h"
#include <math.h>

Vec3 ray_at(const Ray *ray, float t) { return vec3vec3_add(ray->origin, vec3float_mul(ray->direction, t)); }
Expand Down
1 change: 1 addition & 0 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "hittable.h"
#include "material.h"
#include "raytracing.h"
#include "texture.h"
#include "tiff.h"
#include "utils.h"
#include <time.h>
Expand Down
102 changes: 61 additions & 41 deletions src/material.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
#include "material.h"
#include "texture.h"
#include "utils.h"
#include "vec3.h"
#include <math.h>
#include <stdlib.h>

static Vec3 _Texture_value(const HitRecord *rec) {
const Texture *texture = rec->material->albedo;
return texture->value(texture, rec->u, rec->v, rec->p);
}
static bool Empty_scatter(const HitRecord *rec, Vec3 r_in, Vec3 *r_out, Vec3 *color, PCG32 *rng) { return false; }
static Vec3 Empty_emit(const HitRecord *rec) { return VEC3_ZERO; }

#define define_material_new(Type, ...) \
{ \
Expand All @@ -18,12 +16,7 @@ static Vec3 Empty_emit(const HitRecord *rec) { return VEC3_ZERO; }
return obj; \
}

static bool SurfaceNormal_scatter(const HitRecord *rec, Vec3 r_in, Vec3 *r_out, Vec3 *color, PCG32 *rng) {
*color = vec3float_mul(vec3_add(rec->normal, 1.0f), 0.5f); // [-1,1] -> [0,1]
return false;
}
static MaterialVTable SURFACE_NORMAL_VTABLE = {SurfaceNormal_scatter, NULL, Empty_emit};
void SurfaceNormal_init(Material *self) { self->vtable = &SURFACE_NORMAL_VTABLE; }
void SurfaceNormal_init(Material *self) { self->tag = SURFACE_NORMAL; }
Material *SurfaceNormal_new() define_material_new(SurfaceNormal);

// sample from p(theta) = cos(theta) / pi, 0 <= theta <= pi/2
Expand All @@ -33,43 +26,40 @@ static Vec3 rand_cosine_theta(PCG32 *rng) {
float phi = 2.0f * (float)M_PI * r1;
return vec3(cosf(phi) * sqrtf(r2), sinf(phi) * sqrtf(r2), sqrtf(1.0f - r2));
}
static bool Lambertian_scatter(const HitRecord *rec, Vec3 r_in, Vec3 *r_out, Vec3 *color, PCG32 *rng) {
static bool Lambertian_scatter(const HitRecord *rec, Vec3 r_in, Vec3 *r_out, Vec3 *color, bool *skip_pdf, PCG32 *rng) {
ONB onb;
ONB_from_w(&onb, rec->normal);

*r_out = ONB_local(&onb, rand_cosine_theta(rng));
*color = _Texture_value(rec);
*skip_pdf = false;
return true;
}
static float Lambertian_scatter_pdf(Vec3 normal, Vec3 r_in, Vec3 r_out) {
float cos_theta = vec3_dot(normal, vec3_normalize(r_out));
return cos_theta < 0.0f ? 0.0f : cos_theta / (float)M_PI;
}
static MaterialVTable LAMBERTIAN_VTABLE = {Lambertian_scatter, Lambertian_scatter_pdf, Empty_emit};
void Lambertian_init(Material *self, Texture *albedo) {
self->vtable = &LAMBERTIAN_VTABLE;
self->albedo = albedo;
}
void Lambertian_init(Material *self, Texture *albedo) { *self = (Material){LAMBERTIAN, albedo}; }
Material *Lambertian_new(Texture *albedo) define_material_new(Lambertian, albedo);

static Vec3 reflect(Vec3 incident, Vec3 normal) {
return vec3_sub(incident, vec3_mul(normal, 2.0f * vec3_dot(incident, normal)));
}
static bool Metal_scatter(const HitRecord *rec, Vec3 r_in, Vec3 *r_out, Vec3 *color, PCG32 *rng) {
static bool Metal_scatter(const HitRecord *rec, Vec3 r_in, Vec3 *r_out, Vec3 *color, bool *skip_pdf, PCG32 *rng) {
Vec3 reflected = reflect(vec3_normalize(r_in), rec->normal);
*r_out = vec3_add(reflected, vec3_mul(vec3_rand_unit_vector(rng), rec->material->fuzz));
*color = _Texture_value(rec);
return vec3_dot(*r_out, rec->normal) > 0.0f; // check for degeneration
}
static MaterialVTable METAL_VTABLE = {Metal_scatter, NULL, Empty_emit};
void Metal_init(Material *self, Texture *albedo, float fuzz) {
self->vtable = &METAL_VTABLE;
self->albedo = albedo;
self->fuzz = fuzz;
*skip_pdf = true;
// fuzzy reflection done by Shirley's book is not quite correct...
// return vec3_dot(*r_out, rec->normal) > 0.0f; // check for degeneration
if (vec3_dot(*r_out, rec->normal) < 0.0f)
*r_out = reflected;
return true;
}
void Metal_init(Material *self, Texture *albedo, float fuzz) { *self = (Material){METAL, albedo, .fuzz = fuzz}; }
Material *Metal_new(Texture *albedo, float fuzz) define_material_new(Metal, albedo, fuzz);

static bool Dielectric_scatter(const HitRecord *rec, Vec3 r_in, Vec3 *r_out, Vec3 *color, PCG32 *rng) {
static bool Dielectric_scatter(const HitRecord *rec, Vec3 r_in, Vec3 *r_out, Vec3 *color, bool *skip_pdf, PCG32 *rng) {
float eta = rec->material->eta;
if (rec->front_face)
eta = 1.0f / eta;
Expand All @@ -91,36 +81,66 @@ static bool Dielectric_scatter(const HitRecord *rec, Vec3 r_in, Vec3 *r_out, Vec
*r_out = vec3_add(r_perp, r_para);
}
*color = vec3(1, 1, 1);
*skip_pdf = true;
return true;
}
static MaterialVTable DIELECTRIC_VTABLE = {Dielectric_scatter, NULL, Empty_emit};
void Dielectric_init(Material *self, float eta) {
self->vtable = &DIELECTRIC_VTABLE;
self->eta = eta;
}
void Dielectric_init(Material *self, float eta) { *self = (Material){DIELECTRIC, NULL, .eta = eta}; }
Material *Dielectric_new(float eta) define_material_new(Dielectric, eta);

static Vec3 DiffuseLight_emit(const HitRecord *rec) { return rec->front_face ? _Texture_value(rec) : VEC3_ZERO; }
static MaterialVTable DIFFUSE_LIGHT_VTABLE = {Empty_scatter, NULL, DiffuseLight_emit};
void DiffuseLight_init(Material *self, Texture *albedo) {
self->vtable = &DIFFUSE_LIGHT_VTABLE;
self->albedo = albedo;
}
void DiffuseLight_init(Material *self, Texture *albedo) { *self = (Material){DIFFUSE_LIGHT, albedo}; }
Material *DiffuseLight_new(Texture *albedo) define_material_new(DiffuseLight, albedo);

static bool Isotropic_scatter(const HitRecord *rec, Vec3 r_in, Vec3 *r_out, Vec3 *color, PCG32 *rng) {
static bool Isotropic_scatter(const HitRecord *rec, Vec3 r_in, Vec3 *r_out, Vec3 *color, bool *skip_pdf, PCG32 *rng) {
*r_out = vec3_rand_unit_vector(rng);
*color = _Texture_value(rec);
*skip_pdf = false;
return true;
}
static float Isotropic_scatter_pdf(Vec3 normal, Vec3 r_in, Vec3 r_out) { return 1.0f / (4.0f * (float)M_PI); }
static MaterialVTable ISOTROPIC_VTABLE = {Isotropic_scatter, Isotropic_scatter_pdf, Empty_emit};
void Isotropic_init(Material *self, Texture *albedo) {
self->vtable = &ISOTROPIC_VTABLE;
self->albedo = albedo;
}
void Isotropic_init(Material *self, Texture *albedo) { *self = (Material){ISOTROPIC, albedo}; }
Material *Isotropic_new(Texture *albedo) define_material_new(Isotropic, albedo);

bool Material_scatter(const HitRecord *rec, Vec3 r_in, Vec3 *r_out, Vec3 *color, bool *skip_pdf, PCG32 *rng) {
switch (rec->material->tag) {
case SURFACE_NORMAL:
*skip_pdf = true;
return false;
case LAMBERTIAN:
return Lambertian_scatter(rec, r_in, r_out, color, skip_pdf, rng);
case METAL:
return Metal_scatter(rec, r_in, r_out, color, skip_pdf, rng);
case DIELECTRIC:
return Dielectric_scatter(rec, r_in, r_out, color, skip_pdf, rng);
case DIFFUSE_LIGHT:
*skip_pdf = true;
return false;
case ISOTROPIC:
return Isotropic_scatter(rec, r_in, r_out, color, skip_pdf, rng);
}
}

float Material_scatter_pdf(const Material *mat, Vec3 normal, Vec3 r_in, Vec3 r_out) {
switch (mat->tag) {
case LAMBERTIAN:
return Lambertian_scatter_pdf(normal, r_in, r_out);
case ISOTROPIC:
return Isotropic_scatter_pdf(normal, r_in, r_out);
default:
return 0.0f;
}
}

Vec3 Material_emit(const HitRecord *rec) {
switch (rec->material->tag) {
case SURFACE_NORMAL:
return vec3_mul(vec3_add(rec->normal, 1.0f), 0.5f); // [-1,1] -> [0,1]
case DIFFUSE_LIGHT:
return rec->front_face ? _Texture_value(rec) : VEC3_ZERO;
default:
return VEC3_ZERO;
}
}

Vec3 ONB_local(const ONB *self, Vec3 a) {
return vec3_add(vec3_mul(self->u, a.x), vec3_mul(self->v, a.y), vec3_mul(self->w, a.z));
}
Expand Down
18 changes: 10 additions & 8 deletions src/raytracing.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "raytracing.h"
#include "material.h"
#include "pcg32.h"
#include "utils.h"
#include <math.h>
#include <stdio.h>

Expand Down Expand Up @@ -42,17 +44,17 @@ static Vec3 Camera_ray_color(const Camera *camera, const Ray *ray, const World *
const Hittable *objects = &world->objects.hittable;
if (objects->vtable->hit(objects, ray, 1e-3f, INFINITY, &rec, rng)) {
Ray r_out = {rec.p};
Vec3 attenuation;
Vec3 emission_color = rec.material->vtable->emit(&rec);
Vec3 albedo;
bool skip_pdf;
Vec3 emission_color = Material_emit(&rec);

if (!rec.material->vtable->scatter(&rec, ray->direction, &r_out.direction, &attenuation, rng))
if (!Material_scatter(&rec, ray->direction, &r_out.direction, &albedo, &skip_pdf, rng))
return emission_color;

float (*scatter_pdf_fn)(Vec3, Vec3, Vec3) = rec.material->vtable->scatter_pdf;
float prob = camera->lights_sampling_prob;

if (scatter_pdf_fn == NULL || prob == 0.0f || world->lights.size == 0) {
Vec3 scatter_color = vec3_mul(attenuation, Camera_ray_color(camera, &r_out, world, depth - 1, rng));
if (skip_pdf || prob == 0.0f || world->lights.size == 0) {
Vec3 scatter_color = vec3_mul(albedo, Camera_ray_color(camera, &r_out, world, depth - 1, rng));
return vec3_add(emission_color, scatter_color);
}

Expand All @@ -61,11 +63,11 @@ static Vec3 Camera_ray_color(const Camera *camera, const Ray *ray, const World *
if (pcg32_f32(rng) < prob)
r_out.direction = lights->vtable->rand(lights, rec.p, rng);

float scatter_pdf = scatter_pdf_fn(rec.normal, ray->direction, r_out.direction);
float scatter_pdf = Material_scatter_pdf(rec.material, rec.normal, ray->direction, r_out.direction);
float sampling_pdf = (1.0f - prob) * scatter_pdf + prob * lights->vtable->pdf(lights, &r_out, rng);

Vec3 scatter_color =
vec3_mul(attenuation, Camera_ray_color(camera, &r_out, world, depth - 1, rng), scatter_pdf / sampling_pdf);
vec3_mul(albedo, Camera_ray_color(camera, &r_out, world, depth - 1, rng), scatter_pdf / sampling_pdf);
return vec3_add(emission_color, scatter_color);
}

Expand Down

0 comments on commit 267d6ef

Please sign in to comment.