Skip to content

Commit

Permalink
rt: implementing a simple path tracer
Browse files Browse the repository at this point in the history
  • Loading branch information
larc committed Dec 4, 2023
1 parent 38b3dc2 commit e845e7e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
1 change: 1 addition & 0 deletions include/gproshan/app_viewer.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class app_viewer : public viewer

// Scenes
static bool process_simulate_scanner(viewer * p_view);
static bool process_scatter(viewer * p_view);

// Geometry
static bool process_sampling_4points(viewer * p_view);
Expand Down
24 changes: 12 additions & 12 deletions include/gproshan/raytracing/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ template <class T, unsigned int N = 16>
struct random
{
unsigned int previous;

__host_device__
random(unsigned int p): previous(p) {}

Expand All @@ -40,7 +40,7 @@ struct random
previous = previous * 1664525 + 1013904223;
return T(previous & 0x00FFFFFF) / T(0x01000000);
}

__host_device__
operator unsigned int & ()
{
Expand Down Expand Up @@ -168,38 +168,38 @@ struct t_eval_hit
}

__host_device__
bool scatter_reflect(const vec<T, 3> & v, vec<T, 3> & scattered, random<T> & rnd)
bool scatter_reflect(const vec<T, 3> & v, vec<T, 3> & scattered, random<T> & )
{
scattered = normalize(v - 2 * dot(v, normal) * normal);
return dot(scattered, normal) > 0;
}

__host_device__
bool scatter_refract(const vec<T, 3> & v, vec<T, 3> & scattered, random<T> & rnd)
bool scatter_refract(const vec<T, 3> & v, vec<T, 3> & scattered, random<T> & )
{
const float dvn = dot(v, normal);
const float d = 1 - Ni * Ni * (1 - dvn * dvn);

if(d <= 0) return false;

scattered = Ni * (v - dvn * normal) - normal * sqrt(d);
scattered = Ni * (v - dvn * normal) - normal * sqrtf(d);
return true;
}

__host_device__
bool scatter_diffuse(const vec<T, 3> & v, vec<T, 3> & scattered, random<T> & rnd)
bool scatter_diffuse(const vec<T, 3> & , vec<T, 3> & scattered, random<T> & rnd)
{
// random unit sphere
const T & theta = rnd() * 2 * M_PI;
const T & phi = acos(2 * rnd() - 1);
const T & phi = acosf(2 * rnd() - 1);
const T & r = cbrtf(rnd());
const vec<T, 3> p = { r * sin(phi) * cos(theta)
, r * sin(phi) * sin(theta)
, r * cos(phi)

const vec<T, 3> p = { r * sinf(phi) * cosf(theta)
, r * sinf(phi) * sinf(theta)
, r * cosf(phi)
};

scattered = normalize(normal + scattered);
scattered = normalize(normal + p);

return true;
}
Expand Down
19 changes: 18 additions & 1 deletion src/gproshan/app_viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ void app_viewer::init()

add_menu("Scenes",
{
add_process("Scan Scene", process_simulate_scanner)
add_process("Scan Scene", process_simulate_scanner),
add_process("RT Scatter Points", process_scatter)
});

add_menu("Geometry",
Expand Down Expand Up @@ -219,6 +220,22 @@ bool app_viewer::process_simulate_scanner(viewer * p_view)
return true;
}

bool app_viewer::process_scatter(viewer * p_view)
{
app_viewer * view = (app_viewer *) p_view;

rt::eval_hit h;
std::vector<vertex> scatter(100);

rt::random<real_t> rnd(0xABCDEF);
for(vertex & v: scatter)
h.scatter_diffuse({}, v, rnd);

view->add_mesh(new che(scatter.data(), scatter.size(), nullptr, 0));

return false;
}


// Geometry
bool app_viewer::process_sampling_4points(viewer * p_view)
Expand Down
4 changes: 3 additions & 1 deletion src/gproshan/raytracing/optix.cu
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,14 @@ extern "C" __global__ void __closesthit__radiance()
return occluded != 0;
});

random<float> rnd = optixGetPayload_2();
color *= attenuation;
position = hit.position;

random<float> rnd = optixGetPayload_2();
if(!hit.scatter_mat(scattered, scattered, rnd))
attenuation = 0;

attenuation /= 2;
optixSetPayload_2(rnd);
}

Expand Down

0 comments on commit e845e7e

Please sign in to comment.