Skip to content

Commit 94aa3af

Browse files
committed
Implement spot light
1 parent 2e5a177 commit 94aa3af

File tree

5 files changed

+109
-7
lines changed

5 files changed

+109
-7
lines changed

App/CL/light.cl

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,72 @@ float PointLight_GetPdf(// Emissive object
448448
return 0.f;
449449
}
450450

451+
/*
452+
Spot light
453+
*/
454+
// Get intensity for a given direction
455+
float3 SpotLight_GetLe(// Emissive object
456+
Light const* light,
457+
// Scene
458+
Scene const* scene,
459+
// Geometry
460+
DifferentialGeometry const* dg,
461+
// Direction to light source
462+
float3* wo,
463+
// Textures
464+
TEXTURE_ARG_LIST
465+
)
466+
{
467+
return 0.f;
468+
}
469+
470+
/// Sample direction to the light
471+
float3 SpotLight_Sample(// Emissive object
472+
Light const* light,
473+
// Scene
474+
Scene const* scene,
475+
// Geometry
476+
DifferentialGeometry const* dg,
477+
// Textures
478+
TEXTURE_ARG_LIST,
479+
// Sample
480+
float2 sample,
481+
// Direction to light source
482+
float3* wo,
483+
// PDF
484+
float* pdf)
485+
{
486+
*wo = light->p - dg->p;
487+
float ddotwo = dot(-normalize(*wo), light->d);
488+
489+
if (ddotwo > light->oa)
490+
{
491+
*pdf = 1.f;
492+
return ddotwo > light->ia ? light->intensity : light->intensity * (1.f - (light->ia - ddotwo) / (light->ia - light->oa));
493+
}
494+
else
495+
{
496+
*pdf = 0.f;
497+
return 0.f;
498+
}
499+
}
500+
501+
/// Get PDF for a given direction
502+
float SpotLight_GetPdf(// Emissive object
503+
Light const* light,
504+
// Scene
505+
Scene const* scene,
506+
// Geometry
507+
DifferentialGeometry const* dg,
508+
// Direction to light source
509+
float3 wo,
510+
// Textures
511+
TEXTURE_ARG_LIST
512+
)
513+
{
514+
return 0.f;
515+
}
516+
451517

452518

453519

@@ -480,6 +546,8 @@ float3 Light_GetLe(// Light index
480546
return DirectionalLight_GetLe(&light, scene, dg, wo, TEXTURE_ARGS);
481547
case kPoint:
482548
return PointLight_GetLe(&light, scene, dg, wo, TEXTURE_ARGS);
549+
case kSpot:
550+
return SpotLight_GetLe(&light, scene, dg, wo, TEXTURE_ARGS);
483551
}
484552

485553
return make_float3(0.f, 0.f, 0.f);
@@ -513,6 +581,8 @@ float3 Light_Sample(// Light index
513581
return DirectionalLight_Sample(&light, scene, dg, TEXTURE_ARGS, sample, wo, pdf);
514582
case kPoint:
515583
return PointLight_Sample(&light, scene, dg, TEXTURE_ARGS, sample, wo, pdf);
584+
case kSpot:
585+
return SpotLight_Sample(&light, scene, dg, TEXTURE_ARGS, sample, wo, pdf);
516586
}
517587

518588
*pdf = 0.f;
@@ -544,6 +614,8 @@ float Light_GetPdf(// Light index
544614
return DirectionalLight_GetPdf(&light, scene, dg, wo, TEXTURE_ARGS);
545615
case kPoint:
546616
return PointLight_GetPdf(&light, scene, dg, wo, TEXTURE_ARGS);
617+
case kSpot:
618+
return SpotLight_GetPdf(&light, scene, dg, wo, TEXTURE_ARGS);
547619
}
548620

549621
return 0.f;

App/CL/payload.cl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,6 @@ typedef struct _Emissive
8787
} Emissive;
8888

8989

90-
91-
9290
typedef enum _PathFlags
9391
{
9492
kNone = 0x0,
@@ -169,6 +167,15 @@ typedef struct _Light
169167
int texdiffuse;
170168
float multiplier;
171169
};
170+
171+
172+
// Spot
173+
struct
174+
{
175+
float ia;
176+
float oa;
177+
float f;
178+
};
172179
};
173180

174181
float3 p;

App/Scene/scene.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ Scene* Scene::LoadFromObj(std::string const& filename, std::string const& basepa
919919
matmap[i] = scene->materials_.size() - 1;
920920
continue;
921921
}
922-
else if (objmaterials[i].name == "light" || objmaterials[i].name == "Emit1" || objmaterials[i].name == "Light3" || objmaterials[i].name == "dayLight_portal")
922+
else if (objmaterials[i].name == "light1" || objmaterials[i].name == "Emit1" || objmaterials[i].name == "Light3" || objmaterials[i].name == "dayLight_portal")
923923
{
924924
Material emissive;
925925
emissive.kx = 10.f * float3(0.8f, 0.8f, 0.8f);
@@ -1261,7 +1261,7 @@ void Scene::AddDirectionalLight(RadeonRays::float3 const& d, RadeonRays::float3
12611261
{
12621262
Light light;
12631263
light.type = kDirectional;
1264-
light.d = d;
1264+
light.d = normalize(d);
12651265
light.intensity = e;
12661266
lights_.push_back(light);
12671267
}
@@ -1275,6 +1275,18 @@ void Scene::AddPointLight(RadeonRays::float3 const& p, RadeonRays::float3 const&
12751275
lights_.push_back(light);
12761276
}
12771277

1278+
void Scene::AddSpotLight(RadeonRays::float3 const& p, RadeonRays::float3 const& d, RadeonRays::float3 const& e, float ia, float oa)
1279+
{
1280+
Light light;
1281+
light.type = kSpot;
1282+
light.p = p;
1283+
light.d = normalize(d);
1284+
light.intensity = e;
1285+
light.ia = ia;
1286+
light.oa = oa;
1287+
lights_.push_back(light);
1288+
}
1289+
12781290
void Scene::SetEnvironment(std::string const& filename, std::string const& basepath, float envmapmul)
12791291
{
12801292
// Save multiplier

App/Scene/scene.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ namespace Baikal
4646
void AddDirectionalLight(RadeonRays::float3 const& d, RadeonRays::float3 const& e);
4747

4848
void AddPointLight(RadeonRays::float3 const& p, RadeonRays::float3 const& e);
49+
50+
void AddSpotLight(RadeonRays::float3 const& p, RadeonRays::float3 const& d, RadeonRays::float3 const& e, float ia, float oa);
4951

5052
enum DirtyFlags
5153
{
@@ -200,6 +202,14 @@ namespace Baikal
200202
int texdiffuse;
201203
float multiplier;
202204
};
205+
206+
// Spot
207+
struct
208+
{
209+
float ia;
210+
float oa;
211+
float f;
212+
};
203213
};
204214

205215
RadeonRays::float3 p;

App/main.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,9 +364,10 @@ void InitData()
364364
std::cout << "Sensor size: " << g_camera_sensor_size.x * 1000.f << "x" << g_camera_sensor_size.y * 1000.f << "mm\n";
365365

366366
//g_scene->SetEnvironment(g_envmapname, "", g_envmapmul);
367-
g_scene->AddDirectionalLight(RadeonRays::float3(-0.3f, -1.f, -0.4f), RadeonRays::float3(1.f, 1.f, 1.f));
368-
g_scene->AddPointLight(RadeonRays::float3(0.5f, 1.5f, 0.0f), RadeonRays::float3(1.f, 0.6f, 1.f));
369-
367+
//g_scene->AddDirectionalLight(RadeonRays::float3(-0.3f, -1.f, -0.4f), 2.f * RadeonRays::float3(1.f, 1.f, 1.f));
368+
//g_scene->AddPointLight(RadeonRays::float3(0.5f, 1.5f, 0.0f), RadeonRays::float3(1.f, 0.9f, 0.6f));
369+
g_scene->AddSpotLight(RadeonRays::float3(0.5f, 1.5f, 0.0f), RadeonRays::float3(-0.5f, -1.0f, 0.1f), RadeonRays::float3(1.f, 0.9f, 0.6f),
370+
std::cos(M_PI_4/2), std::cos(M_PI_4));
370371
#pragma omp parallel for
371372
for (int i = 0; i < g_cfgs.size(); ++i)
372373
{

0 commit comments

Comments
 (0)