Skip to content

Commit

Permalink
add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
yumcyaWiz committed Nov 23, 2021
1 parent 31ec7a6 commit a8065b0
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 15 deletions.
2 changes: 2 additions & 0 deletions include/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class Camera {
spdlog::info("[Camera] focal_length: {}", focal_length);
}

// sample ray emitting from the given sensor coordinate
// NOTE: uv: [-1, -1] x [1, 1], sensor coordinate
bool sampleRay(const Vec2f& uv, Ray& ray, float& pdf) const {
const Vec3f pinholePos = position + focal_length * forward;
const Vec3f sensorPos = position + uv[0] * right + uv[1] * up;
Expand Down
9 changes: 6 additions & 3 deletions include/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,13 @@ inline void orthonormalBasis(const Vec3f& n, Vec3f& t, Vec3f& b) {
b = normalize(cross(t, n));
}

// transform direction from world to local
inline Vec3f worldToLocal(const Vec3f& v, const Vec3f& lx, const Vec3f& ly,
const Vec3f& lz) {
return Vec3f(dot(v, lx), dot(v, ly), dot(v, lz));
}

// transform direction from local to world
inline Vec3f localToWorld(const Vec3f& v, const Vec3f& lx, const Vec3f& ly,
const Vec3f& lz) {
Vec3f ret;
Expand All @@ -244,6 +246,7 @@ inline Vec3f localToWorld(const Vec3f& v, const Vec3f& lx, const Vec3f& ly,
return ret;
}

// compute cartesian coordinates from spherical coordinates
inline Vec3f sphericalToCartesian(float theta, float phi) {
return Vec3f(std::cos(phi) * std::sin(theta), std::cos(theta),
std::sin(phi) * std::sin(theta));
Expand All @@ -265,8 +268,8 @@ struct Ray {
struct SurfaceInfo {
Vec3f position;
Vec3f normal;
Vec3f dpdu;
Vec3f dpdv;
Vec3f dpdu; // tangent vector
Vec3f dpdv; // bitangent vector
Vec2f texcoords;
Vec2f barycentric;
};
Expand All @@ -275,7 +278,7 @@ struct SurfaceInfo {
class Primitive;

struct IntersectInfo {
float t;
float t; // distance to the hit point
SurfaceInfo surfaceInfo;
const Primitive* hitPrimitive;
};
Expand Down
13 changes: 12 additions & 1 deletion include/integrator.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Integrator {
// do preliminary jobs before calling integrate
virtual void build(const Scene& scene, Sampler& sampler) = 0;

// compute radiance coming from given ray
// compute radiance coming from the given ray
virtual Vec3f integrate(const Ray& ray, const Scene& scene,
Sampler& sampler) const = 0;
};
Expand Down Expand Up @@ -76,11 +76,22 @@ class PathTracing : public Integrator {
// implementation of photon mapping
class PhotonMapping : public Integrator {
private:
// number of photons used for making global photon map
const int nPhotonsGlobal;

// number of photons used for radiance estimation by global photon map
const int nEstimationGlobal;

// number of photons for making caustics photon map
const int nPhotonsCaustics;

// number of photons used for radiance estimation by caustics photon map
const int nEstimationCaustics;

// maximum depth to estimate radiance by final gathering
const int finalGatheringDepth;

// maximum depth of photon tracing, eye tracing
const int maxDepth;

PhotonMap globalPhotonMap;
Expand Down
6 changes: 5 additions & 1 deletion include/light.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "sampler.h"
#include "triangle.h"

// light interface
class Light {
public:
virtual Vec3f Le(const SurfaceInfo& info, const Vec3f& dir) const = 0;
Expand All @@ -15,21 +16,24 @@ class Light {

class AreaLight : public Light {
private:
const Vec3f le;
const Vec3f le; // emission
const Triangle* triangle;

public:
AreaLight(const Vec3f& le, const Triangle* triangle)
: le(le), triangle(triangle) {}

// return emission
Vec3f Le(const SurfaceInfo& info, const Vec3f& dir) const override {
return le;
}

// sample point on the light
SurfaceInfo samplePoint(Sampler& sampler, float& pdf) const override {
return triangle->samplePoint(sampler, pdf);
}

// sample direction from the light
Vec3f sampleDirection(const SurfaceInfo& surfInfo, Sampler& sampler,
float& pdf) const override {
const Vec3f dir = sampleCosineHemisphere(sampler.getNext2D(), pdf);
Expand Down
15 changes: 10 additions & 5 deletions include/material.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ enum class BxDFType { DIFFUSE, SPECULAR };

using DirectionPair = std::pair<Vec3f, Vec3f>;

// represent BRDF or BTDF
// direction vectors are in tangent space(x: tangent, y: normal, z: bitangent)
class BxDF {
private:
BxDFType type;
Expand All @@ -19,9 +21,12 @@ class BxDF {
static float cosTheta(const Vec3f& v) { return v[1]; }
static float absCosTheta(const Vec3f& v) { return std::abs(cosTheta(v)); }

// compute reflection direction
static Vec3f reflect(const Vec3f& v, const Vec3f& n) {
return -v + 2.0f * dot(v, n) * n;
}

// compute refracted direction
static bool refract(const Vec3f& v, const Vec3f& n, float iorI, float iorT,
Vec3f& t) {
const Vec3f t_h = -iorI / iorT * (v - dot(v, n) * n);
Expand All @@ -34,7 +39,7 @@ class BxDF {
return true;
}

// schlick approximation
// schlick approximation of fresnel reflectance
static float fresnel(float cosThetaI, float iorI, float iorT) {
const float f0 =
(iorI - iorT) * (iorI - iorT) / ((iorI + iorT) * (iorI + iorT));
Expand All @@ -48,14 +53,14 @@ class BxDF {
// evaluate BxDF
virtual Vec3f evaluate(const Vec3f& wo, const Vec3f& wi) const = 0;

// sample direction
// pdf is set to be propotional to BxDF
// sample direction by BxDF.
// its pdf is propotional to the shape of BxDF
virtual Vec3f sampleDirection(const Vec3f& wo, Sampler& sampler, Vec3f& wi,
float& pdf) const = 0;

// sample all samplable direction
// get all samplable direction
// NOTE: for specular only
// NOTE: used for drawing fresnel reflection at low number of samples
// NOTE: used for drawing fresnel reflection nicely at low number of samples
virtual std::vector<DirectionPair> sampleAllDirection(
const Vec3f& wo) const = 0;
};
Expand Down
10 changes: 6 additions & 4 deletions include/photon_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
#include "core.h"

struct Photon {
Vec3f throughput; // F*G/pdf
Vec3f throughput; // BxDF * Geometric Term / pdf
Vec3f position;
Vec3f wi; // incident direction

// implement Point
// implementation of Point concept
static constexpr int dim = 3;
float operator[](int i) const { return position[i]; }

Expand All @@ -21,13 +21,14 @@ struct Photon {
: throughput(flux), position(position), wi(wi) {}
};

// Point concept
template <typename T>
concept Point = requires(T& x, int i) {
{ T::dim } -> std::convertible_to<int>; // dimension
{ x[i] } -> std::convertible_to<float>; // element access
};

// compute squared distance between given points
// compute the squared distance between given points
// NOTE: assume PointT and PointU has the same dimension
template <typename PointT, typename PointU>
requires Point<PointT> && Point<PointU>
Expand All @@ -39,6 +40,7 @@ inline float distance2(const PointT& p1, const PointU& p2) {
return dist2;
}

// implementation of kd-tree
template <typename PointT>
requires Point<PointT>
class KdTree {
Expand All @@ -54,7 +56,7 @@ class KdTree {

std::vector<Node> nodes; // array of tree nodes
const PointT* points; // pointer to array of points
int nPoints;
int nPoints; // number of points

void buildNode(int* indices, int n_points, int depth) {
if (n_points <= 0) return;
Expand Down
6 changes: 6 additions & 0 deletions include/primitive.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "material.h"
#include "triangle.h"

// primitive provides an abstraction layer of the object's shape(triangle),
// material, area light
class Primitive {
private:
const Triangle* triangle;
Expand All @@ -20,6 +22,7 @@ class Primitive {

bool hasAreaLight() const { return areaLight != nullptr; }

// return emission
Vec3f Le(const SurfaceInfo& surfInfo, const Vec3f& dir) const {
return areaLight->Le(surfInfo, dir);
}
Expand All @@ -37,6 +40,8 @@ class Primitive {
return bxdf->evaluate(wo_l, wi_l);
}

// sample direction by BxDF
// its pdf is propotional to the shape od BxDF
Vec3f sampleBxDF(const Vec3f& wo, const SurfaceInfo& surfInfo,
Sampler& sampler, Vec3f& wi, float& pdf) const {
// world to local transform
Expand All @@ -53,6 +58,7 @@ class Primitive {
return f;
}

// get all samplable direction
std::vector<DirectionPair> sampleAllBxDF(const Vec3f& wo,
const SurfaceInfo& surfInfo) const {
// world to local transform
Expand Down
1 change: 1 addition & 0 deletions include/scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ class Scene {
rtcCommitScene(scene);
}

// ray-scene intersection
bool intersect(const Ray& ray, IntersectInfo& info) const {
RTCRayHit rayhit;
rayhit.ray.org_x = ray.origin[0];
Expand Down
2 changes: 1 addition & 1 deletion include/triangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class Triangle {
t3 * barycentric[1];
}

// sample point on triangle
// sample point on the triangle
SurfaceInfo samplePoint(Sampler& sampler, float& pdf) const {
SurfaceInfo ret;

Expand Down

0 comments on commit a8065b0

Please sign in to comment.