From a8065b06611760cfff9aa62f01e5e121f659256b Mon Sep 17 00:00:00 2001 From: yumcyawiz Date: Wed, 24 Nov 2021 06:26:34 +0900 Subject: [PATCH] add comments --- include/camera.h | 2 ++ include/core.h | 9 ++++++--- include/integrator.h | 13 ++++++++++++- include/light.h | 6 +++++- include/material.h | 15 ++++++++++----- include/photon_map.h | 10 ++++++---- include/primitive.h | 6 ++++++ include/scene.h | 1 + include/triangle.h | 2 +- 9 files changed, 49 insertions(+), 15 deletions(-) diff --git a/include/camera.h b/include/camera.h index b5c8bf3..e7bf046 100644 --- a/include/camera.h +++ b/include/camera.h @@ -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; diff --git a/include/core.h b/include/core.h index ce880e8..e1e00b9 100644 --- a/include/core.h +++ b/include/core.h @@ -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; @@ -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)); @@ -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; }; @@ -275,7 +278,7 @@ struct SurfaceInfo { class Primitive; struct IntersectInfo { - float t; + float t; // distance to the hit point SurfaceInfo surfaceInfo; const Primitive* hitPrimitive; }; diff --git a/include/integrator.h b/include/integrator.h index 52275f9..1fe24a1 100644 --- a/include/integrator.h +++ b/include/integrator.h @@ -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; }; @@ -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; diff --git a/include/light.h b/include/light.h index fbe97a6..0e2e344 100644 --- a/include/light.h +++ b/include/light.h @@ -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; @@ -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); diff --git a/include/material.h b/include/material.h index 65544fe..bad055a 100644 --- a/include/material.h +++ b/include/material.h @@ -9,6 +9,8 @@ enum class BxDFType { DIFFUSE, SPECULAR }; using DirectionPair = std::pair; +// represent BRDF or BTDF +// direction vectors are in tangent space(x: tangent, y: normal, z: bitangent) class BxDF { private: BxDFType type; @@ -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); @@ -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)); @@ -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 sampleAllDirection( const Vec3f& wo) const = 0; }; diff --git a/include/photon_map.h b/include/photon_map.h index 4f855f4..b62468e 100644 --- a/include/photon_map.h +++ b/include/photon_map.h @@ -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]; } @@ -21,13 +21,14 @@ struct Photon { : throughput(flux), position(position), wi(wi) {} }; +// Point concept template concept Point = requires(T& x, int i) { { T::dim } -> std::convertible_to; // dimension { x[i] } -> std::convertible_to; // 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 requires Point && Point @@ -39,6 +40,7 @@ inline float distance2(const PointT& p1, const PointU& p2) { return dist2; } +// implementation of kd-tree template requires Point class KdTree { @@ -54,7 +56,7 @@ class KdTree { std::vector 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; diff --git a/include/primitive.h b/include/primitive.h index 5c0b236..665d1d1 100644 --- a/include/primitive.h +++ b/include/primitive.h @@ -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; @@ -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); } @@ -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 @@ -53,6 +58,7 @@ class Primitive { return f; } + // get all samplable direction std::vector sampleAllBxDF(const Vec3f& wo, const SurfaceInfo& surfInfo) const { // world to local transform diff --git a/include/scene.h b/include/scene.h index 4a8b40e..26bc56b 100644 --- a/include/scene.h +++ b/include/scene.h @@ -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]; diff --git a/include/triangle.h b/include/triangle.h index b33cc1e..be34437 100644 --- a/include/triangle.h +++ b/include/triangle.h @@ -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;