Skip to content

Commit

Permalink
Merge branch 'dev' into alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
larc committed Dec 4, 2023
2 parents 3f454db + e845e7e commit bd34a99
Show file tree
Hide file tree
Showing 15 changed files with 271 additions and 161 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
19 changes: 4 additions & 15 deletions include/gproshan/raytracing/optix_params.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,16 @@
namespace gproshan::rt {


struct launch_params
struct launch_params: public base_params
{
vec4 * color_buffer = nullptr;
int buffer_size = 0;
int window_width = 0;
int window_height = 0;
int viewport_x = 0;
int viewport_y = 0;
int n_samples = 0;
int n_lights = 0;
light lights[NL];
light ambient;
vec3 cam_pos;
mat4 inv_proj_view;
bool flat;

OptixTraversableHandle traversable;

scene_data sc;

bool flat;
void * other = nullptr;
vec4 * color_buffer = nullptr;
unsigned int buffer_size = 0;
};


Expand Down
13 changes: 5 additions & 8 deletions include/gproshan/raytracing/raytracing.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,17 @@ namespace gproshan::rt {

class raytracing
{
protected:
size_t n_samples = 0;

public:
raytracing() = default;
virtual ~raytracing() = default;

virtual void render(vec4 * img, const render_params & params, const bool & flat);

virtual float * raycaster( const ivec2 & windows_size,
const mat4 & inv_proj_view,
const vertex & cam_pos,
const index_t & samples = 4
) const;
virtual std::vector<float> raycaster( const uvec2 & windows_size,
const mat4 & inv_proj_view,
const vertex & cam_pos,
const index_t & samples = 4
) const;

virtual eval_hit intersect( const vertex &, // org
const vertex &, //dir
Expand Down
34 changes: 18 additions & 16 deletions include/gproshan/raytracing/render_params.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,26 @@ namespace gproshan::rt {

const size_t NL = 16; // number of lights

struct render_params

struct base_params
{
int window_width = 0;
int window_height = 0;
int viewport_width = 0;
int viewport_height = 0;
int viewport_x = 0;
int viewport_y = 0;
int n_lights = 0;
uvec2 window_size;
uvec2 viewport_pos;
unsigned int depth = 1;
unsigned int n_frames = 0;
unsigned int n_samples = 1;
unsigned int n_lights = 0;
light lights[NL];
light ambient = {0, 1, 0.1};
vertex cam_pos;
mat4 inv_proj_view;
bool restart = false;
vertex cam_pos;
};

struct render_params: public base_params
{
uvec2 viewport_size;
bool viewport_is_window = true;
bool restart = false;

bool add_light(const light & l)
{
Expand All @@ -39,12 +44,9 @@ struct render_params

void log()
{
gproshan_log_var(window_width);
gproshan_log_var(window_height);
gproshan_log_var(viewport_width);
gproshan_log_var(viewport_height);
gproshan_log_var(viewport_x);
gproshan_log_var(viewport_y);
gproshan_log_var(window_size);
gproshan_log_var(viewport_size);
gproshan_log_var(viewport_pos);
gproshan_log_var(n_lights);
gproshan_log_var(cam_pos);
gproshan_log_var(restart);
Expand Down
99 changes: 87 additions & 12 deletions include/gproshan/raytracing/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@
namespace gproshan::rt {


template <class T, uint32_t N = 16>
template <class T, unsigned int N = 16>
struct random
{
uint32_t previous;
unsigned int previous;

__host_device__
random(uint32_t v0, uint32_t v1)
random(unsigned int p): previous(p) {}

__host_device__
random(unsigned int v0, unsigned int v1)
{
uint32_t s = 0;
for(uint32_t i = 0; i < N; ++i)
unsigned int s = 0;
for(unsigned int i = 0; i < N; ++i)
{
s += 0x9e3779b9;
v0 += ((v1 << 4) + 0xa341316c) ^ (v1 + s) ^ ((v1 >> 5) + 0xc8013ea4);
Expand All @@ -37,6 +40,12 @@ struct random
previous = previous * 1664525 + 1013904223;
return T(previous & 0x00FFFFFF) / T(0x01000000);
}

__host_device__
operator unsigned int & ()
{
return previous;
}
};

template<class T>
Expand Down Expand Up @@ -65,6 +74,7 @@ template <class T>
struct t_eval_hit
{
index_t primID = NIL;
int illum = 1;
T dist = 0;
T u = 0, v = 0;
vec<T, 3> position;
Expand All @@ -73,6 +83,7 @@ struct t_eval_hit
vec<T, 3> Kd = 0.5;
vec<T, 3> Ks = 0.2;
T Ns = 10;
T Ni = 0;
T d = 1;

__host_device__
Expand Down Expand Up @@ -117,29 +128,88 @@ struct t_eval_hit

Ka = mat.Ka;
if(mat.map_Ka != -1)
Ka *= texture(sc.textures[mat.map_Ka], texcoord);
Ka = texture(sc.textures[mat.map_Ka], texcoord);

Kd = mat.Kd;
if(mat.map_Kd != -1)
Kd *= texture(sc.textures[mat.map_Kd], texcoord);
Kd = texture(sc.textures[mat.map_Kd], texcoord);

Ks = mat.Ks;
if(mat.map_Ks != -1)
Ks *= texture(sc.textures[mat.map_Ks], texcoord);
Ks = texture(sc.textures[mat.map_Ks], texcoord);

Ns = mat.Ns;
Ni = mat.Ni;

d = mat.d;
// if(mat.map_d != -1)
// d = texture(sc.textures[mat.map_d], texcoord);

illum = mat.illum;
}

// PTX symbols of certain types (e.g. pointers to functions) cannot be used to initialize array
__host_device__
bool scatter_mat(const vec<T, 3> & v, vec<T, 3> & scattered, random<T> & rnd)
{
switch(illum)
{
case 1:
case 2:
return scatter_diffuse(v, scattered, rnd);
case 3:
case 5:
case 6:
case 7:
return scatter_reflect(v, scattered, rnd);
}

return false;
}

__host_device__
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> & )
{
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 * sqrtf(d);
return true;
}

__host_device__
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 = acosf(2 * rnd() - 1);
const T & r = cbrtf(rnd());

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

scattered = normalize(normal + p);

return true;
}
};

template <class T, class Occluded>
__host_device__
vec<T, 3> eval_li(const t_eval_hit<T> & hit, const light & ambient, const light * lights, const int & n_lights, const vec<T, 3> & eye, Occluded occluded)
{
const vec<T, 3> v = normalize(eye - hit.position);
const vec<T, 3> & v = normalize(eye - hit.position);
const vec<T, 3> & n = hit.normal;

T lambertian;
Expand Down Expand Up @@ -179,10 +249,15 @@ using eval_hit = t_eval_hit<float>;

template <class T>
__host_device__
vec<T, 3> ray_view_dir(const ivec2 & pos, const ivec2 & windows_size, const mat<T, 4> & inv_proj_view, const vec<T, 3> & cam_pos)
vec<T, 3> ray_view_dir( const uvec2 & pos,
const uvec2 & windows_size,
const mat<T, 4> & inv_proj_view,
const vec<T, 3> & cam_pos,
random<T> & rnd
)
{
vec2 screen = { (float(pos.x()) + 0.5f) / windows_size.x(),
(float(pos.y()) + 0.5f) / windows_size.y()
vec2 screen = { (float(pos.x()) + rnd()) / windows_size.x(),
(float(pos.y()) + rnd()) / windows_size.y()
};
vec<T, 4> view = {screen.x() * 2 - 1, screen.y() * 2 - 1, 1, 1};
vec<T, 4> q = inv_proj_view * view;
Expand Down
2 changes: 1 addition & 1 deletion include/gproshan/viewer/che_viewer.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class che_viewer
virtual void draw_pointcloud(shader & program);
void draw_selected_vertices(che_viewer & sphere, shader & program);

void select(const ivec2 & pos, const ivec2 & windows_size, const mat4 & inv_proj_view_mat, const vertex & cam_pos);
void select(const uvec2 & pos, const uvec2 & windows_size, const mat4 & inv_proj_view_mat, const vertex & cam_pos);

void log_info();
};
Expand Down
10 changes: 5 additions & 5 deletions include/gproshan/viewer/viewer.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ class viewer

GLFWwindow * window = nullptr;
rt::render_params render_params;
int & window_width = render_params.window_width;
int & window_height = render_params.window_height;
int & viewport_width = render_params.viewport_width;
int & viewport_height = render_params.viewport_height;
unsigned int & window_width = render_params.window_size.x();
unsigned int & window_height = render_params.window_size.y();
unsigned int & viewport_width = render_params.viewport_size.x();
unsigned int & viewport_height = render_params.viewport_size.y();
mat4 proj_mat;
mat4 proj_view_mat;

Expand Down Expand Up @@ -176,7 +176,7 @@ class viewer

static bool m_raycasting(viewer * view);

void pick_vertex(const int & x, const int & y);
void pick_vertex(const uvec2 & pos);
void check_apply_all_meshes(const std::function<void(che_viewer &)> & fun);
};

Expand Down
6 changes: 3 additions & 3 deletions shaders/shading.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ vec3 shading(vec3 color, vec3 n, vec3 pos, vec2 texcoord)
{
Ka = mat.Ka;
if(mat.map_Ka != -1)
Ka *= texture(tex_Ka, texcoord).rgb;
Ka = texture(tex_Ka, texcoord).rgb;

Kd = mat.Kd;
if(mat.map_Kd != -1)
Kd *= texture(tex_Kd, texcoord).rgb;
Kd = texture(tex_Kd, texcoord).rgb;

Ks = mat.Ks;
if(mat.map_Ks != -1)
Ks *= texture(tex_Ks, texcoord).rgb;
Ks = vec3(texture(tex_Ks, texcoord).r);

Ns = mat.Ns;
}
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
3 changes: 2 additions & 1 deletion src/gproshan/raytracing/embree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,12 @@ void embree::build_bvh(const std::vector<che *> & meshes, const std::vector<mat4
if(!meshes[i]->n_trigs || pointcloud)
g_meshes[i]->n_trigs = 0;

[[maybe_unused]]
const index_t & geomID = g_meshes[i]->n_trigs || meshes[i]->is_scene() ?
add_mesh(meshes[i], model_mats[i]) :
add_pointcloud(meshes[i], model_mats[i]);

gproshan_error_var(i == geomID);
gproshan_debug_var(i == geomID);
}

rtcCommitScene(rtc_scene);
Expand Down
Loading

0 comments on commit bd34a99

Please sign in to comment.