@@ -28,6 +28,7 @@ THE SOFTWARE.
28
28
#include <../App/CL/scene.cl>
29
29
30
30
31
+ INLINE
31
32
bool IntersectTriangle (ray const * r , float3 v1 , float3 v2 , float3 v3 , float * a , float * b )
32
33
{
33
34
const float3 e1 = v2 - v1 ;
@@ -139,12 +140,12 @@ float3 AreaLight_GetLe(// Emissive object
139
140
{
140
141
ray r ;
141
142
r .o .xyz = dg -> p ;
142
- r .d .xyz = normalize ( * wo ) ;
143
+ r .d .xyz = * wo ;
143
144
144
145
int shapeidx = light -> shapeidx ;
145
146
int primidx = light -> primidx ;
146
147
147
- float v0 , v1 , v2 ;
148
+ float3 v0 , v1 , v2 ;
148
149
Scene_GetTriangleVertices (scene , shapeidx , primidx , & v0 , & v1 , & v2 );
149
150
150
151
float a , b ;
@@ -157,19 +158,17 @@ float3 AreaLight_GetLe(// Emissive object
157
158
Scene_InterpolateAttributes (scene , shapeidx , primidx , make_float2 (a , b ), & p , & n , & tx , & area );
158
159
159
160
float3 d = p - dg -> p ;
160
- float ld = length (d );
161
- * wo = p - dg -> p ;
161
+ * wo = d ;
162
162
163
163
int mat_idx = Scene_GetMaterialIndex (scene , shapeidx , primidx );
164
164
Material mat = scene -> materials [mat_idx ];
165
165
166
166
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 ;
169
168
}
170
169
else
171
170
{
172
- return 0.f ;
171
+ return make_float3 ( 0.f , 0.f , 0.f ) ;
173
172
}
174
173
}
175
174
@@ -252,7 +251,7 @@ float AreaLight_GetPdf(// Emissive object
252
251
int shapeidx = light -> shapeidx ;
253
252
int primidx = light -> primidx ;
254
253
255
- float v0 , v1 , v2 ;
254
+ float3 v0 , v1 , v2 ;
256
255
Scene_GetTriangleVertices (scene , shapeidx , primidx , & v0 , & v1 , & v2 );
257
256
258
257
// Intersect ray against this area light
@@ -277,6 +276,50 @@ float AreaLight_GetPdf(// Emissive object
277
276
}
278
277
}
279
278
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
+
280
323
/*
281
324
Directional light
282
325
*/
@@ -389,6 +432,31 @@ float PointLight_GetPdf(// Emissive object
389
432
return 0.f ;
390
433
}
391
434
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
+
392
460
/*
393
461
Spot light
394
462
*/
@@ -457,8 +525,6 @@ float SpotLight_GetPdf(// Emissive object
457
525
}
458
526
459
527
460
-
461
-
462
528
/*
463
529
Dispatch calls
464
530
*/
@@ -563,6 +629,40 @@ float Light_GetPdf(// Light index
563
629
return 0.f ;
564
630
}
565
631
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
+
566
666
/// Check if the light is singular
567
667
bool Light_IsSingular (__global Light const * light )
568
668
{
0 commit comments