[Bf-blender-cvs] [c4235ce] texture_nodes_refactor: Cycles: Implement an area preserving parameterization sampling for area lamps

Sergey Sharybin noreply at git.blender.org
Sat Oct 11 15:04:31 CEST 2014


Commit: c4235ce820b0a9c3d019e31e7d4360162a0282a5
Author: Sergey Sharybin
Date:   Fri Oct 10 19:24:12 2014 +0600
Branches: texture_nodes_refactor
https://developer.blender.org/rBc4235ce820b0a9c3d019e31e7d4360162a0282a5

Cycles: Implement an area preserving parameterization sampling for area lamps

Replace old code for area lamps which was more like incorrect with more correct
one using the following paper as a reference:

  Carlos Urena et al.
  An Area-Preserving Parametrization for Spherical Rectangles.
  https://www.solidangle.com/research/egsr2013_spherical_rectangle.pdf

Implementation is straight from the paper, currently the rectangle contants are
calculated for each of the samples. Ideally we need to pre-calculate them.

The old PDF is still used, which makes difference real subtle. This is to be
corrected before final commit.

Reviewers: brecht, juicyfruit

Subscribers: dingto, ton

Differential Revision: https://developer.blender.org/D823

===================================================================

M	intern/cycles/kernel/kernel_light.h

===================================================================

diff --git a/intern/cycles/kernel/kernel_light.h b/intern/cycles/kernel/kernel_light.h
index e7f62f2..2b64b0a 100644
--- a/intern/cycles/kernel/kernel_light.h
+++ b/intern/cycles/kernel/kernel_light.h
@@ -167,12 +167,92 @@ ccl_device float3 sphere_light_sample(float3 P, float3 center, float radius, flo
 	return disk_light_sample(normalize(P - center), randu, randv)*radius;
 }
 
-ccl_device float3 area_light_sample(float3 axisu, float3 axisv, float randu, float randv)
+/* Uses the following paper:
+ *
+ * Carlos Urena et al.
+ * An Area-Preserving Parametrization for Spherical Rectangles.
+ *
+ * https://www.solidangle.com/research/egsr2013_spherical_rectangle.pdf
+ */
+ccl_device float3 area_light_sample(float3 P,
+                                    float3 light_p,
+                                    float3 axisu, float3 axisv,
+                                    float randu, float randv,
+                                    float *pdf)
 {
-	randu = randu - 0.5f;
-	randv = randv - 0.5f;
-
-	return axisu*randu + axisv*randv;
+	/* In our name system we're using P for the center,
+	 * which is o in the paper.
+	 */
+
+	float3 corner = light_p - axisu * 0.5f - axisv * 0.5f;
+	float axisu_len, axisv_len;
+	/* Compute local reference system R. */
+	float3 x = normalize_len(axisu, &axisu_len);
+	float3 y = normalize_len(axisv, &axisv_len);
+	float3 z = cross(x, y);
+	/* Compute rectangle coords in local reference system. */
+	float3 dir = corner - P;
+	float z0 = dot(dir, z);
+	/* Flip 'z' to make it point against Q. */
+	if(z0 > 0.0f) {
+		z *= -1.0f;
+		z0 *= -1.0f;
+	}
+	float z0sq = z0 * z0;
+	float x0 = dot(dir, x);
+	float y0 = dot(dir, y);
+	float x1 = x0 + axisu_len;
+	float y1 = y0 + axisv_len;
+	float y0sq = y0 * y0;
+	float y1sq = y1 * y1;
+	/* Create vectors to four vertices. */
+	float3 v00 = make_float3(x0, y0, z0);
+	float3 v01 = make_float3(x0, y1, z0);
+	float3 v10 = make_float3(x1, y0, z0);
+	float3 v11 = make_float3(x1, y1, z0);
+	/* Compute normals to edges. */
+	float3 n0 = normalize(cross(v00, v10));
+	float3 n1 = normalize(cross(v10, v11));
+	float3 n2 = normalize(cross(v11, v01));
+	float3 n3 = normalize(cross(v01, v00));
+	/* Compute internal angles (gamma_i). */
+	float g0 = acosf(-dot(n0, n1));
+	float g1 = acosf(-dot(n1, n2));
+	float g2 = acosf(-dot(n2, n3));
+	float g3 = acosf(-dot(n3, n0));
+	/* Compute predefined constants. */
+	float b0 = n0.z;
+	float b1 = n2.z;
+	float b0sq = b0 * b0;
+	float k = 2 * M_PI_F - g2 - g3;
+	/* Compute solid angle from internal angles. */
+	float S = g0 + g1 - k;
+
+	/* Compute cu. */
+	float au = randu * S + k;
+	float fu = (cosf(au) * b0 - b1) / sinf(au);
+	float cu = 1.0f / sqrtf(fu * fu + b0sq) * (fu > 0.0f ? 1.0f : -1.0f);
+	cu = clamp(cu, -1.0f, 1.0f);
+	/* Compute xu. */
+	float xu = -(cu * z0) / sqrtf(1.0f - cu * cu);
+	xu = clamp(xu, x0, x1);
+	/* Compute yv. */
+	float d = sqrt(xu * xu + z0sq);
+	float h0 = y0 / sqrtf(d * d + y0sq);
+	float h1 = y1 / sqrtf(d * d + y1sq);
+	float hv = h0 + randv * (h1 - h0), hv2 = hv * hv;
+	float yv = (hv2 < 1.0f - 1e-6f) ? (hv * d) / sqrtf(1.0f - hv2) : y1;
+
+	/* Transform (xu, yv, z0) to world coords. */
+	float3 p = P + xu * x + yv * y + z0 * z;
+
+	/* Calculate pdf as per section 5.2 of the paper. */
+	float wi_len;
+	float3 wi = normalize_len(P - p, &wi_len);
+	float3 np = normalize(cross(axisv, axisu));
+	*pdf = 1.0f / S * dot(np, wi) / (wi_len * wi_len);
+
+	return p;
 }
 
 ccl_device float spot_light_attenuation(float4 data1, float4 data2, LightSample *ls)
@@ -286,14 +366,16 @@ ccl_device void lamp_light_sample(KernelGlobals *kg, int lamp,
 			float3 axisv = make_float3(data2.y, data2.z, data2.w);
 			float3 D = make_float3(data3.y, data3.z, data3.w);
 
-			ls->P += area_light_sample(axisu, axisv, randu, randv);
+			ls->P = area_light_sample(P, ls->P,
+			                          axisu, axisv,
+			                          randu, randv,
+			                          &ls->pdf);
+
 			ls->Ng = D;
 			ls->D = normalize_len(ls->P - P, &ls->t);
 
 			float invarea = data2.x;
-
 			ls->eval_fac = 0.25f*invarea;
-			ls->pdf = invarea;
 		}
 
 		ls->eval_fac *= kernel_data.integrator.inv_pdf_lights;




More information about the Bf-blender-cvs mailing list