Skip to content

Commit e0d1915

Browse files
committed
Small fix for lights
1 parent aec25ae commit e0d1915

File tree

1 file changed

+110
-10
lines changed

1 file changed

+110
-10
lines changed

App/CL/light.cl

Lines changed: 110 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ THE SOFTWARE.
2828
#include <../App/CL/scene.cl>
2929

3030

31+
INLINE
3132
bool IntersectTriangle(ray const* r, float3 v1, float3 v2, float3 v3, float* a, float* b)
3233
{
3334
const float3 e1 = v2 - v1;
@@ -139,12 +140,12 @@ float3 AreaLight_GetLe(// Emissive object
139140
{
140141
ray r;
141142
r.o.xyz = dg->p;
142-
r.d.xyz = normalize(*wo);
143+
r.d.xyz = *wo;
143144

144145
int shapeidx = light->shapeidx;
145146
int primidx = light->primidx;
146147

147-
float v0, v1, v2;
148+
float3 v0, v1, v2;
148149
Scene_GetTriangleVertices(scene, shapeidx, primidx, &v0, &v1, &v2);
149150

150151
float a, b;
@@ -157,19 +158,17 @@ float3 AreaLight_GetLe(// Emissive object
157158
Scene_InterpolateAttributes(scene, shapeidx, primidx, make_float2(a, b), &p, &n, &tx, &area);
158159

159160
float3 d = p - dg->p;
160-
float ld = length(d);
161-
*wo = p - dg->p;
161+
*wo = d;
162162

163163
int mat_idx = Scene_GetMaterialIndex(scene, shapeidx, primidx);
164164
Material mat = scene->materials[mat_idx];
165165

166166
const float3 ke = Texture_GetValue3f(mat.kx.xyz, tx, TEXTURE_ARGS_IDX(mat.kxmapidx));
167-
float ndotv = dot(n, -(normalize(d)));
168-
return ke;
167+
return ke;
169168
}
170169
else
171170
{
172-
return 0.f;
171+
return make_float3(0.f, 0.f, 0.f);
173172
}
174173
}
175174

@@ -252,7 +251,7 @@ float AreaLight_GetPdf(// Emissive object
252251
int shapeidx = light->shapeidx;
253252
int primidx = light->primidx;
254253

255-
float v0, v1, v2;
254+
float3 v0, v1, v2;
256255
Scene_GetTriangleVertices(scene, shapeidx, primidx, &v0, &v1, &v2);
257256

258257
// Intersect ray against this area light
@@ -277,6 +276,50 @@ float AreaLight_GetPdf(// Emissive object
277276
}
278277
}
279278

279+
float3 AreaLight_SampleVertex(
280+
// Emissive object
281+
Light const* light,
282+
// Scene
283+
Scene const* scene,
284+
// Textures
285+
TEXTURE_ARG_LIST,
286+
// Sample
287+
float2 sample0,
288+
float2 sample1,
289+
// Direction to light source
290+
float3* p,
291+
float3* n,
292+
float3* wo,
293+
// PDF
294+
float* pdf)
295+
{
296+
int shapeidx = light->shapeidx;
297+
int primidx = light->primidx;
298+
299+
// Generate sample on triangle
300+
float r0 = sample0.x;
301+
float r1 = sample0.y;
302+
303+
// Convert random to barycentric coords
304+
float2 uv;
305+
uv.x = native_sqrt(r0) * (1.f - r1);
306+
uv.y = native_sqrt(r0) * r1;
307+
308+
float2 tx;
309+
float area;
310+
Scene_InterpolateAttributes(scene, shapeidx, primidx, uv, p, n, &tx, &area);
311+
312+
int mat_idx = Scene_GetMaterialIndex(scene, shapeidx, primidx);
313+
Material mat = scene->materials[mat_idx];
314+
315+
const float3 ke = Texture_GetValue3f(mat.kx.xyz, tx, TEXTURE_ARGS_IDX(mat.kxmapidx));
316+
317+
*wo = Sample_MapToHemisphere(sample1, *n, 1.f);
318+
*pdf = (1.f / area) * fabs(dot(*n, *wo)) / PI;
319+
320+
return ke;
321+
}
322+
280323
/*
281324
Directional light
282325
*/
@@ -389,6 +432,31 @@ float PointLight_GetPdf(// Emissive object
389432
return 0.f;
390433
}
391434

435+
/// Sample vertex on the light
436+
float3 PointLight_SampleVertex(
437+
// Light object
438+
Light const* light,
439+
// Scene
440+
Scene const* scene,
441+
// Textures
442+
TEXTURE_ARG_LIST,
443+
// Sample
444+
float2 sample0,
445+
float2 sample1,
446+
// Direction to light source
447+
float3* p,
448+
float3* n,
449+
float3* wo,
450+
// PDF
451+
float* pdf)
452+
{
453+
*p = light->p;
454+
*n = make_float3(0.f, 1.f, 0.f);
455+
*wo = Sample_MapToSphere(sample0);
456+
*pdf = 1.f / (4.f * PI);
457+
return light->intensity;
458+
}
459+
392460
/*
393461
Spot light
394462
*/
@@ -457,8 +525,6 @@ float SpotLight_GetPdf(// Emissive object
457525
}
458526

459527

460-
461-
462528
/*
463529
Dispatch calls
464530
*/
@@ -563,6 +629,40 @@ float Light_GetPdf(// Light index
563629
return 0.f;
564630
}
565631

632+
/// Sample vertex on the light
633+
float3 Light_SampleVertex(
634+
// Light index
635+
int idx,
636+
// Scene
637+
Scene const* scene,
638+
// Textures
639+
TEXTURE_ARG_LIST,
640+
// Sample
641+
float2 sample0,
642+
float2 sample1,
643+
// Point on the light
644+
float3* p,
645+
// Normal at light vertex
646+
float3* n,
647+
// Direction
648+
float3* wo,
649+
// PDF
650+
float* pdf)
651+
{
652+
Light light = scene->lights[idx];
653+
654+
switch (light.type)
655+
{
656+
case kArea:
657+
return AreaLight_SampleVertex(&light, scene, TEXTURE_ARGS, sample0, sample1, p, n, wo, pdf);
658+
case kPoint:
659+
return PointLight_SampleVertex(&light, scene, TEXTURE_ARGS, sample0, sample1, p, n, wo, pdf);
660+
}
661+
662+
*pdf = 0.f;
663+
return make_float3(0.f, 0.f, 0.f);
664+
}
665+
566666
/// Check if the light is singular
567667
bool Light_IsSingular(__global Light const* light)
568668
{

0 commit comments

Comments
 (0)