Skip to content

Commit

Permalink
make material OOP. remove MaterialList
Browse files Browse the repository at this point in the history
  • Loading branch information
gau-nernst committed Nov 25, 2023
1 parent 669cefd commit 42f1f58
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 224 deletions.
16 changes: 8 additions & 8 deletions include/hittable.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ typedef struct Sphere {
Hittable hittable;
Vec3 center;
float radius;
Material material;
Material *material;
AABB bbox;
} Sphere;

void Sphere_init(Sphere *self, Vec3 center, float radius, Material mat);
Hittable *Sphere_new(Vec3 center, float radius, Material mat);
void Sphere_init(Sphere *self, Vec3 center, float radius, Material *mat);
Hittable *Sphere_new(Vec3 center, float radius, Material *mat);

typedef struct Quad {
Hittable hittable;
Expand All @@ -60,13 +60,13 @@ typedef struct Quad {
Vec3 normal;
float D;
Vec3 w;
Material material;
Material *material;
AABB bbox;
} Quad;

void Quad_init(Quad *self, Vec3 Q, Vec3 u, Vec3 v, Material mat);
Hittable *Quad_new(Vec3 Q, Vec3 u, Vec3 v, Material mat);
Hittable *Box_new(Vec3 a, Vec3 b, Material mat);
void Quad_init(Quad *self, Vec3 Q, Vec3 u, Vec3 v, Material *mat);
Hittable *Quad_new(Vec3 Q, Vec3 u, Vec3 v, Material *mat);
Hittable *Box_new(Vec3 a, Vec3 b, Material *mat);

typedef struct BVHNode {
Hittable hittable;
Expand Down Expand Up @@ -103,7 +103,7 @@ typedef struct ConstantMedium {
Hittable hittable;
Hittable *boundary;
float neg_inv_density;
Material phase_fn;
Material *phase_fn;
} ConstantMedium;

void ConstantMedium_init(ConstantMedium *self, Hittable *boundary, float density, Texture *albedo);
Expand Down
81 changes: 35 additions & 46 deletions include/material.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,76 +6,65 @@
#include "vec3.h"
#include <stdbool.h>

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

typedef struct Material {
MaterialType type;
void *ptr;
} Material;
struct Material {
bool (*scatter)(HitRecord *rec, Vec3 incident, PCG32State *rng, Vec3 *scattered, Vec3 *color);
Vec3 (*emit)(HitRecord *rec);
};

define_list_header(Material);

#define material(ptr) \
(Material) { \
_Generic((ptr), \
SurfaceNormal *: SURFACE_NORMAL, \
Lambertian *: LAMBERTIAN, \
Metal *: METAL, \
Dielectric *: DIELECTRIC, \
DiffuseLight *: DIFFUSE_LIGHT, \
Isotropic *: ISOTROPIC), \
ptr \
}
struct HitRecord {
Vec3 p;
Vec3 normal;
Material *material;
float t;
float u;
float v;
bool front_face;
};

typedef struct SurfaceNormal SurfaceNormal;
typedef struct SurfaceNormal {
Material mat;
} SurfaceNormal;
void SurfaceNormal_init(SurfaceNormal *self);
Material *SurfaceNormal_new();

typedef struct Lambertian {
Material mat;
Texture *albedo;
} Lambertian;
void Lambertian_init(Lambertian *self, Texture *albedo);
Material *Lambertian_new(Texture *albedo);

typedef struct Metal {
Material mat;
Texture *albedo;
float fuzz;
} Metal;
void Metal_init(Metal *self, Texture *albedo, float fuzz);
Material *Metal_new(Texture *albedo, float fuzz);

typedef struct Dielectric {
Material mat;
Texture *albedo;
float eta;
} Dielectric;
void Dielectric_init(Dielectric *self, Texture *albedo, float eta);
Material *Dielectric_new(Texture *albedo, float eta);

typedef struct DiffuseLight {
Material mat;
Texture *albedo;
} DiffuseLight;
void DiffuseLight_init(DiffuseLight *self, Texture *albedo);
Material *DiffuseLight_new(Texture *albedo);

typedef struct Isotropic {
Material mat;
Texture *albedo;
} Isotropic;

SurfaceNormal *SurfaceNormal_new();
Lambertian *Lambertian_new(Texture *albedo);
Metal *Metal_new(Texture *albedo, float fuzz);
Dielectric *Dielectric_new(Texture *albedo, float eta);
DiffuseLight *DiffuseLight_new(Texture *albedo);
Isotropic *Isotropic_new(Texture *albedo);

typedef struct HitRecord {
Vec3 p;
Vec3 normal;
Material material;
float t;
float u;
float v;
bool front_face;
} HitRecord;

bool scatter(Vec3 incident, HitRecord *rec, PCG32State *rng, Vec3 *scattered, Vec3 *color);
Vec3 emit(HitRecord *rec);
void Isotropic_init(Isotropic *self, Texture *albedo);
Material *Isotropic_new(Texture *albedo);

#endif // MATERIAL_H
3 changes: 1 addition & 2 deletions include/raytracing.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@

typedef struct World {
HittableList objects;
MaterialList materials;
} World;

void World_init(World *world, size_t max_objects, size_t max_materials);
void World_init(World *world, size_t max_objects);

typedef struct Camera {
float aspect_ratio;
Expand Down
24 changes: 1 addition & 23 deletions include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,9 @@
#define define_init_new(Type, ...) \
{ \
Type *obj = my_malloc(sizeof(Type)); \
Type##_init(obj, ##__VA_ARGS__); \
Type##_init(obj, ##__VA_ARGS__); \
return (void *)obj; \
}
void *my_malloc(size_t size);

#define define_list_header(Type) \
typedef struct Type##List { \
size_t max_size; \
size_t size; \
Type *items; \
} Type##List; \
void Type##List_init(Type##List *list, size_t max_size); \
Type##List *Type##List_new(size_t max_size); \
void Type##List_append(Type##List *list, Type item);

#define define_list_source(Type) \
void Type##List_init(Type##List *list, size_t max_size) { \
list->max_size = max_size; \
list->size = 0; \
list->items = my_malloc(sizeof(list->items[0]) * max_size); \
} \
Type##List *Type##List_new(size_t max_size) define_init_new(Type##List, max_size); \
void Type##List_append(Type##List *list, Type item) { \
assert((list->size < list->max_size) && "List is full"); \
list->items[list->size++] = item; \
}

#endif // UTILS_H
16 changes: 8 additions & 8 deletions src/hittable.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ static bool HittableList_hit(const Hittable *self_, const Ray *ray, float t_min,

static HittableHitFn Sphere_hit;
static AABB Sphere_bbox(const Hittable *self_) { return ((Sphere *)self_)->bbox; }
void Sphere_init(Sphere *sphere, Vec3 center, float radius, Material mat) {
void Sphere_init(Sphere *sphere, Vec3 center, float radius, Material *mat) {
*sphere = (Sphere){{Sphere_hit, Sphere_bbox},
center,
radius,
mat,
AABB_from_Vec3(vec3_sub(center, radius), vec3_add(center, radius))};
}
Hittable *Sphere_new(Vec3 center, float radius, Material mat) define_init_new(Sphere, center, radius, mat);
Hittable *Sphere_new(Vec3 center, float radius, Material *mat) define_init_new(Sphere, center, radius, mat);
static bool Sphere_hit(const Hittable *self_, const Ray *ray, float t_min, float t_max, HitRecord *rec,
PCG32State *rng) {
Sphere *self = (Sphere *)self_;
Expand Down Expand Up @@ -126,20 +126,20 @@ static bool Sphere_hit(const Hittable *self_, const Ray *ray, float t_min, float

static HittableHitFn Quad_hit;
static AABB Quad_bbox(const Hittable *self_) { return ((Quad *)self_)->bbox; }
void Quad_init(Quad *self, Vec3 Q, Vec3 u, Vec3 v, Material mat) {
void Quad_init(Quad *self, Vec3 Q, Vec3 u, Vec3 v, Material *mat) {
self->hittable = (Hittable){Quad_hit, Quad_bbox};
self->Q = Q;
self->u = u;
self->v = v;
self->material = mat;
self->bbox = AABB_pad(AABB_from_Vec3(Q, vec3_add(Q, u, v)));

Vec3 n = vec3_cross(self->u, self->v);
Vec3 n = vec3_cross(u, v);
self->normal = vec3_normalize(n);
self->D = vec3_dot(self->normal, self->Q);
self->D = vec3_dot(self->normal, Q);
self->w = vec3_div(n, vec3_length2(n));
}
Hittable *Quad_new(Vec3 Q, Vec3 u, Vec3 v, Material mat) define_init_new(Quad, Q, u, v, mat);
Hittable *Quad_new(Vec3 Q, Vec3 u, Vec3 v, Material *mat) define_init_new(Quad, Q, u, v, mat);
static bool Quad_hit(const Hittable *self_, const Ray *ray, float t_min, float t_max, HitRecord *rec, PCG32State *rng) {
Quad *self = (Quad *)self_;

Expand Down Expand Up @@ -170,7 +170,7 @@ static bool Quad_hit(const Hittable *self_, const Ray *ray, float t_min, float t
return true;
}

Hittable *Box_new(Vec3 a, Vec3 b, Material mat) {
Hittable *Box_new(Vec3 a, Vec3 b, Material *mat) {
HittableList *list = (HittableList *)HittableList_new(6);

Vec3 min_p = vec3_min(a, b);
Expand Down Expand Up @@ -350,7 +350,7 @@ void ConstantMedium_init(ConstantMedium *self, Hittable *boundary, float density
{ConstantMedium_hit, ConstantMedium_bbox},
boundary,
-1.0f / density,
material(Isotropic_new(albedo)),
Isotropic_new(albedo),
};
}
Hittable *ConstantMedium_new(Hittable *boundary, float density, Texture *albedo)
Expand Down
Loading

0 comments on commit 42f1f58

Please sign in to comment.