Skip to content

Commit

Permalink
mirror and whitted in dev
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenxiZhou0619 committed Feb 28, 2023
1 parent e0f1914 commit f709c44
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 7 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ src/FunctionLayer/Texture/ImageTexture.cpp
src/FunctionLayer/Texture/Texture.cpp
src/FunctionLayer/Texture/Mipmap.cpp
src/FunctionLayer/Material/Matte.cpp
src/FunctionLayer/Material/Mirror.cpp
src/FunctionLayer/Material/Material.cpp
src/FunctionLayer/Sampler/IndependentSampler.cpp
src/ResourceLayer/Image.cpp
Expand Down
7 changes: 3 additions & 4 deletions examples/bunny/scene.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"output" : {
"filename" : "cornell-18.png"
"filename" : "bunny-1.hdr"
},
"sampler" : {
"type" : "independent",
Expand All @@ -24,7 +24,7 @@
}
},
"integrator" : {
"type" : "directSampleLight"
"type" : "directSampleBSDF"
},
"scene" : {
"shapes" : [
Expand All @@ -45,8 +45,7 @@
"edge0" : [0, 0, 20],
"edge1" : [20,0,0],
"material" : {
"type" : "matte",
"albedo" : [0.8, 0.8, 0.8]
"type" : "mirror"
}
}
],
Expand Down
45 changes: 45 additions & 0 deletions src/FunctionLayer/Integrator/WhittedIntegrator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "WhittedIntegrator.h"
#include <FunctionLayer/Material/Material.h>

Spectrum WhittedIntegrator::li(const Ray &_ray, const Scene &scene,
std::shared_ptr<Sampler> sampler) const {
Spectrum spectrum(.0f), beta(.0f);
Ray ray(_ray);

do {
auto itsOpt = scene.rayIntersect(ray);

// escape the scene
if (!itsOpt.has_value()) {
for (auto light : scene.infiniteLights) {
spectrum += light->evaluateEmission(ray);
}
break;
}

Intersection its = itsOpt.value();
computeRayDifferentials(&its, ray);
auto bsdf = its.shape->material->computeBSDF(its);

auto bsdfSampleResult = bsdf->sample(-ray.direction, sampler->next2D());

// If the surface is specular, spwan the ray
if (bsdfSampleResult.type == BSDFType::Specular) {
ray = Ray(its.position, bsdfSampleResult.wi);
beta *= bsdfSampleResult.weight;
continue;
}
// If the surface is not specular, sample the light
else {

// First, sample infinite light
for (auto light : scene.infiniteLights) {
auto lightSampleResult = light->sample(its, sampler->next2D());
Ray shadowRay(its.position, lightSampleResult.direction, 1e-4f,
FLT_MAX);
}
}
} while (1);

return spectrum;
}
15 changes: 15 additions & 0 deletions src/FunctionLayer/Integrator/WhittedIntegrator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include "Integrator.h"

class WhittedIntegrator : public Integrator {
public:
WhittedIntegrator() = default;

WhittedIntegrator(const Json &json) : Integrator(json) {}

virtual ~WhittedIntegrator() = default;

virtual Spectrum li(const Ray &ray, const Scene &scene,
std::shared_ptr<Sampler> sampler) const override;
};
4 changes: 4 additions & 0 deletions src/FunctionLayer/Material/BxDF/BSDF.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#pragma once
#include <CoreLayer/ColorSpace/Spectrum.h>
#include <FunctionLayer/Shape/Intersection.h>

enum class BSDFType { Diffuse, Specular };

struct BSDFSampleResult {
Spectrum weight;
Vector3f wi;
float pdf;
BSDFType type;
};

class BSDF {
Expand Down
2 changes: 1 addition & 1 deletion src/FunctionLayer/Material/BxDF/Lambert.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class LambertReflection : public BSDF {
Spectrum weight = albedo;
Vector3f wi = squareToCosineHemisphere(sample);
float pdf = squareToCosineHemispherePdf(wi);
return {weight, toWorld(wi), pdf};
return {weight, toWorld(wi), pdf, BSDFType::Diffuse};
}

private:
Expand Down
3 changes: 1 addition & 2 deletions src/FunctionLayer/Material/BxDF/Specular.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ class SpecularReflection : public BSDF {

virtual BSDFSampleResult sample(const Vector3f &wo,
const Vector2f &sample) const override {
//
Vector3f woLocal = toLocal(wo);
Vector3f wiLocal{-woLocal[0], woLocal[1], -woLocal[2]};
return {Spectrum(1.f), toWorld(wiLocal), 1.f};
return {Spectrum(1.f), toWorld(wiLocal), 1.f, BSDFType::Specular};
}
};
13 changes: 13 additions & 0 deletions src/FunctionLayer/Material/Mirror.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "Mirror.h"
#include "./BxDF/Specular.h"

MirrorMaterial::MirrorMaterial(const Json &json) : Material(json) {}

std::shared_ptr<BSDF>
MirrorMaterial::computeBSDF(const Intersection &intersection) const {
Vector3f normal, tangent, bitangent;
computeShadingGeometry(intersection, &normal, &tangent, &bitangent);
return std::make_shared<SpecularReflection>(normal, tangent, bitangent);
}

REGISTER_CLASS(MirrorMaterial, "mirror")
12 changes: 12 additions & 0 deletions src/FunctionLayer/Material/Mirror.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once
#include "Material.h"

class MirrorMaterial : public Material {
public:
MirrorMaterial();

MirrorMaterial(const Json &json);

virtual std::shared_ptr<BSDF>
computeBSDF(const Intersection &intersection) const override;
};

0 comments on commit f709c44

Please sign in to comment.