Skip to content

Commit

Permalink
Clean up material parsing quite a bit
Browse files Browse the repository at this point in the history
Reworked the emission data too, it's much simpler now. Still slightly
WIP - we don't flag emissive instances correctly yet, so for now, every
instance is flagged as emissive.
  • Loading branch information
vkoskiv committed Oct 30, 2023
1 parent 7fadfb3 commit e54b504
Show file tree
Hide file tree
Showing 14 changed files with 166 additions and 225 deletions.
178 changes: 100 additions & 78 deletions input/hdr.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,15 @@
]
}
],
"color": {
"r": 1.0,
"g": 0.0,
"b": 1.0
"material": {
"type": "metal",
"color": {
"r": 1.0,
"g": 0.0,
"b": 1.0
},
"roughness": 0.05
},
"bsdf": "metal",
"roughness": 0.05,
"radius": 0.1
},
{
Expand All @@ -116,13 +118,15 @@
]
}
],
"color": {
"r": 0.8,
"g": 0.8,
"b": 0.8
"material": {
"type": "metal",
"color": {
"r": 0.8,
"g": 0.8,
"b": 0.8
},
"roughness": 0.00
},
"bsdf": "metal",
"roughness": 0.00,
"radius": 0.05
},
{
Expand All @@ -138,14 +142,16 @@
}
]
}
],
"color": {
"r": 0.0,
"g": 0.8,
"b": 0.0
],
"material": {
"type": "metal",
"color": {
"r": 0.0,
"g": 0.8,
"b": 0.0
},
"roughness": 0.00
},
"bsdf": "metal",
"roughness": 0.00,
"radius": 0.05
},
{
Expand All @@ -161,15 +167,17 @@
}
]
}
],
"color": {
"r": 1.0,
"g": 1.0,
"b": 1.0
],
"material": {
"type": "glass",
"color": {
"r": 1.0,
"g": 1.0,
"b": 1.0
},
"IOR": 1.9,
"roughness": 0
},
"bsdf": "glass",
"IOR": 1.9,
"roughness": 0,
"radius": 0.05
},
{
Expand All @@ -185,15 +193,17 @@
}
]
}
],
"color": {
"r": 1.0,
"g": 0.1,
"b": 0.1
],
"material": {
"type": "glass",
"color": {
"r": 1.0,
"g": 0.1,
"b": 0.1
},
"IOR": 1.9,
"roughness": 0
},
"bsdf": "glass",
"IOR": 1.9,
"roughness": 0,
"radius": 0.025
},
{
Expand All @@ -209,15 +219,17 @@
}
]
}
],
"color": {
"r": 0.1,
"g": 1.0,
"b": 0.1
],
"material": {
"type": "glass",
"color": {
"r": 0.1,
"g": 1.0,
"b": 0.1
},
"IOR": 1.9,
"roughness": 0
},
"bsdf": "glass",
"IOR": 1.9,
"roughness": 0,
"radius": 0.025
},
{
Expand All @@ -234,14 +246,16 @@
]
}
],
"color": {
"r": 0.1,
"g": 0.1,
"b": 1.0
"material": {
"type": "glass",
"color": {
"r": 0.1,
"g": 0.1,
"b": 1.0
},
"IOR": 1.9,
"roughness": 0
},
"bsdf": "glass",
"IOR": 1.9,
"roughness": 0,
"radius": 0.025
},
{
Expand All @@ -258,14 +272,16 @@
]
}
],
"radius": 0.01,
"color": {
"r": 1.0,
"g": 0.0,
"b": 0.0
"material": {
"type": "metal",
"color": {
"r": 1.0,
"g": 0.0,
"b": 0.0
},
"roughness": 1.0
},
"bsdf": "metal",
"roughness": 1.0
"radius": 0.01
},
{
"type": "sphere",
Expand Down Expand Up @@ -322,14 +338,16 @@
]
}
],
"radius": 2.5,
"color": {
"r": 0.0,
"g": 1.0,
"b": 0.0
"material": {
"type": "emissive",
"color": {
"r": 0.0,
"g": 1.0,
"b": 0.0
},
"intensity": 10.0
},
"bsdf": "emissive",
"intensity": 10.0
"radius": 2.5
},
{
"type": "sphere",
Expand All @@ -345,14 +363,16 @@
]
}
],
"radius": 2.5,
"color": {
"r": 0.0,
"g": 0.0,
"b": 1.0
"material": {
"type": "emissive",
"color": {
"r": 0.0,
"g": 0.0,
"b": 1.0
},
"intensity": 10.0
},
"bsdf": "emissive",
"intensity": 10.0
"radius": 2.5
},
{
"type": "sphere",
Expand All @@ -367,14 +387,16 @@
}
]
}
],
"color": {
"r": 1.0,
"g": 1.0,
"b": 1.0
],
"material": {
"type": "emissive",
"color": {
"r": 1.0,
"g": 1.0,
"b": 1.0
},
"intensity": 10.0
},
"bsdf": "emissive",
"intensity": 10.0,
"radius": 10
}
],
Expand Down
2 changes: 0 additions & 2 deletions src/datatypes/hitrecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ struct hitRecord {
struct vector incident_dir; //Incident ray direction
struct vector hitPoint; //Hit point vector in world space
struct vector surfaceNormal; //Surface normal at that point of intersection
//FIXME: Do we even have cases without UV anymore?
struct coord uv; //UV barycentric coordinates for intersection point
const struct bsdfNode *bsdf; //Surface properties of the intersected object
const struct color *emission; //FIXME: Hack - Shouldn't have this here
struct poly *polygon; //ptr to polygon that was encountered
float distance; //Distance to intersection point
int instIndex; //Instance index, negative if no intersection
Expand Down
15 changes: 1 addition & 14 deletions src/datatypes/material.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,9 @@
#include "image/texture.h"
#include "../datatypes/scene.h"

static struct material emptyMaterial() {
return (struct material){0};
}

struct material defaultMaterial() {
struct material newMat = emptyMaterial();
newMat.diffuse = g_gray_color;
newMat.reflectivity = 1.0f;
newMat.type = lambertian;
newMat.IOR = 1.0f;
return newMat;
}

//To showcase missing .MTL file, for example
struct material warningMaterial() {
struct material newMat = emptyMaterial();
struct material newMat = { 0 };
newMat.type = lambertian;
newMat.diffuse = (struct color){1.0f, 0.0f, 0.5f, 1.0f};
return newMat;
Expand Down
3 changes: 0 additions & 3 deletions src/datatypes/material.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ struct material {
struct texture *texture;
struct texture *normalMap;
struct texture *specularMap;
struct color ambient;
struct color diffuse;
struct color specular;
struct color emission;
Expand All @@ -79,10 +78,8 @@ struct material {
float glossiness;

enum bsdfType type; // FIXME: Temporary
const struct bsdfNode *bsdf;
};

struct material defaultMaterial(void);
struct material warningMaterial(void);

struct node_storage;
Expand Down
1 change: 0 additions & 1 deletion src/datatypes/scene.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,6 @@ void destroyScene(struct world *scene) {
destroyBlocks(scene->storage.node_pool);
for (int i = 0; i < scene->instanceCount; ++i) {
if (scene->instances[i].bsdfs) free(scene->instances[i].bsdfs);
if (scene->instances[i].emissions) free(scene->instances[i].emissions);
}
free(scene->instances);
free(scene->meshes);
Expand Down
4 changes: 0 additions & 4 deletions src/datatypes/sphere.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
#include "../renderer/pathtrace.h"
#include "lightray.h"

struct sphere defaultSphere() {
return (struct sphere){ 10.0f, NULL, g_black_color, 0.0f };
}

//Calculates intersection with a sphere and a light ray
bool intersect(const struct lightRay *ray, const struct sphere *sphere, float *t) {
//Vector dot product of the direction
Expand Down
6 changes: 0 additions & 6 deletions src/datatypes/sphere.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,9 @@
#include "vector.h"
#include "material.h"

//Sphere
struct sphere {
float radius;
const struct bsdfNode *bsdf;
struct color emission;
float rayOffset;
};

struct sphere defaultSphere(void);

//Calculates intersection between a light ray and a sphere
bool rayIntersectsWithSphere(const struct lightRay *ray, const struct sphere *sphere, struct hitRecord *isect);
1 change: 1 addition & 0 deletions src/nodes/bsdfnode.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct bsdfSample {
struct vector out;
float pdf;
struct color weight;
struct color emitted; // FIXME: Not really the right place for this
};

//TODO: Expand and refactor to match a standard bsdf signature with eval, sample and pdf
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/shaders/emission.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ static struct bsdfSample sample(const struct bsdfNode *bsdf, sampler *sampler, c
const struct vector scatterDir = vec_normalize(vec_add(record->surfaceNormal, vec_on_unit_sphere(sampler)));
return (struct bsdfSample){
.out = scatterDir,
.weight = colorCoef(emitBsdf->strength->eval(emitBsdf->strength, sampler, record), emitBsdf->color->eval(emitBsdf->color, sampler, record))
.emitted = colorCoef(emitBsdf->strength->eval(emitBsdf->strength, sampler, record), emitBsdf->color->eval(emitBsdf->color, sampler, record))
};
}

Expand Down
Loading

0 comments on commit e54b504

Please sign in to comment.