5#ifndef EXAMPLES_RAY_SAMPLING_OVERLAPPING_H
6#define EXAMPLES_RAY_SAMPLING_OVERLAPPING_H
10#include <nv/index/iscene_group.h>
11#include <nv/index/icylinder.h>
12#include <nv/index/icone.h>
13#include <nv/index/isphere.h>
14#include <nv/index/iellipsoid.h>
15#include <nv/index/iplane.h>
17#include <nv/index/imaterial.h>
18#include <nv/index/ilight.h>
20#include <nv/index/app/application_layer_utility.h>
25"NV_IDX_XAC_VERSION_1_0 \n"
27"class Surface_sample_program \n"
29" NV_IDX_SURFACE_SAMPLE_PROGRAM \n"
34" NV_IDX_DEVICE_INLINE_MEMBER void initialize() \n"
46" NV_IDX_DEVICE_INLINE_MEMBER int execute( \n"
47" const Sample_info_self& sample_info, \n"
48" Sample_output& sample_output) \n"
50" const uint light_id = state.self.get_light_id(); \n"
51" const uint material_id = state.self.get_material_id(); \n"
53" const float3 ray_direction = normalize(sample_info.ray_direction); \n"
54" float4 color = nv::index::xaclib::phong_shading( \n"
55" state.scene, material_id, light_id, \n"
57" sample_info.sample_normal, true); \n"
60" const float screen_gamma = 0.6f; \n"
61" color = nv::index::xaclib::gamma_correct(color, screen_gamma); \n"
62" sample_output.set_color(color); \n"
63" return NV_IDX_PROG_OK; \n"
68" // The user program used for picking. \n"
69" NV_IDX_DEVICE_INLINE_MEMBER int inquire( \n"
70" const Sample_info_self& sample_info, \n"
71" Query_results& query_results) \n"
73" // sample_info.ray_t is automatically written \n"
74" // color is automatically written \n"
76" // we could invalidate current sample to skip/ignore it \n"
77" //query_results.invalidate_sample(); \n"
79" // write some attribute value of current sample \n"
81" sample_info.scene_position.x; \n"
82" query_results.write_value<float>(0u /*user_value_idx*/, v); \n"
85" if (counter & 1) { \n"
86" // write every other counter as user value \n"
87" query_results.write_value<int>(1u /*user_value_idx*/, counter); \n"
90" const float3 normal = sample_info.sample_normal; \n"
91" query_results.write_value<float3>(2u /*user_value_idx*/, normal); \n"
93" return NV_IDX_PROG_OK; \n"
95" // we can stop sampling: \n"
96" //return NV_IDX_PROG_TERMINATE_PROGRAM_INSTANCE; \n"
106"NV_IDX_XAC_VERSION_1_0 \n"
108"class Volume_sample_program \n"
110" NV_IDX_VOLUME_SAMPLE_PROGRAM \n"
115" NV_IDX_DEVICE_INLINE_MEMBER void initialize() \n"
127" NV_IDX_DEVICE_INLINE_MEMBER int execute( \n"
128" const Sample_info_self& sample_info, \n"
129" Sample_output& sample_output) \n"
142" sample_output.set_color( make_float4(0.2f, 0.2f, 0.2f, 0.3f)); \n"
144" return NV_IDX_PROG_OK; \n"
149" NV_IDX_DEVICE_INLINE_MEMBER int execute( \n"
150" const Sample_info_self& sample_info, \n"
151" Sample_output& sample_output) \n"
153" const uint field_idx = 0u; \n"
154" const float3& sample_position = sample_info.sample_position_object_space; \n"
155" // lookup color/opacity values from sampler and colormap using sample alpha value \n"
156" const auto& sample_context = sample_info.sample_context; \n"
157" // get the volume sampler from the sparse volume reference \n"
158" const auto sampler = state.self.generate_sampler<float>(field_idx, sample_context);\n"
159" // scalar volume sample \n"
160" const float volume_sample = sampler.fetch_sample(sample_position); \n"
161" const float4 sample_color = state.self.get_colormap().lookup(volume_sample.w); \n"
163" // output swizzled volume color, but using remapped opacity value \n"
164" sample_output.set_color( make_float4(sample_color.y, \n"
167" sample_color.w)); \n"
169" return NV_IDX_PROG_OK; \n"
174" // The user program used for picking. \n"
175" NV_IDX_DEVICE_INLINE_MEMBER int inquire( \n"
176" const Sample_info_self& sample_info, \n"
177" Query_results& query_results) \n"
179" // sample_info.ray_t is automatically written \n"
180" // color is automatically written \n"
182" // we could invalidate current sample to skip/ignore it \n"
183" //query_results.invalidate_sample(); \n"
184" const uint field_idx = 0u; \n"
185" const float3& sample_position = sample_info.sample_position_object_space; \n"
186" // lookup color and opacity values \n"
187" const auto& sample_context = sample_info.sample_context; \n"
188" // get the volume sampler from the sparse volume reference \n"
189" const auto sampler = state.self.generate_sampler<float>(field_idx, sample_context);\n"
190" // scalar volume sample \n"
191" const float v = sampler.fetch_sample(sample_position); \n"
193" query_results.write_value<float>(0u /*user_value_idx*/, v); \n"
196" if (counter & 1) { \n"
197" // write every other counter as user value \n"
198" query_results.write_value<int>(1u /*user_value_idx*/, counter); \n"
201" const float3 normal = make_float3(1.f, 0.3f, 0.1f); \n"
202" query_results.write_value<float3>(2u /*user_value_idx*/, normal); \n"
204" return NV_IDX_PROG_OK; \n"
206" // we can stop sampling: \n"
207" //return NV_IDX_PROG_TERMINATE_PROGRAM_INSTANCE; \n"
217 const char*
name()
const {
return "Overlapping Volume and Shapes"; }
226 return "0 0 0 1024 1024 1024";
231 return nv::index::app::get_bbox_from_string<mi::Float32,3>(
get_roi_string());
235 nv::index::app::IApplication_layer* app_layer,
237 const mi::math::Bbox< mi::Float32, 3>& roi_bbox,
238 const mi::neuraylib::Tag& session_tag,
239 std::map<std::string, std::string>& opt_map,
240 mi::neuraylib::IDice_transaction* dice_transaction)
const
243 mi::base::Handle<const nv::index::ISession> session(
244 dice_transaction->access<
const nv::index::ISession>(session_tag));
248 mi::base::Handle<nv::index::IScene> scene(
249 dice_transaction->edit<nv::index::IScene>(session->get_scene()));
256 mi::base::Handle<nv::index::IDirectional_light> light_global(
257 scene->create_attribute<nv::index::IDirectional_light>());
259 light_global->set_direction(mi::math::Vector<mi::Float32, 3>(0.5f, 0.f, 1.f));
260 const mi::neuraylib::Tag light_global_tag = dice_transaction->store_for_reference_counting(light_global.get());
262 scene->append(light_global_tag, dice_transaction);
265 mi::base::Handle<nv::index::IPhong_gl> phong_global(scene->create_attribute<nv::index::IPhong_gl>());
267 phong_global->set_ambient(mi::math::Color(0.1f, 0.15f, 0.1f, 1.0f));
268 phong_global->set_diffuse(mi::math::Color(0.95f, 0.3f, 0.3f, 1.0f));
269 phong_global->set_specular(mi::math::Color(0.4f, 0.4f, 0.5f, 1.0f));
270 phong_global->set_shininess(100);
271 const mi::neuraylib::Tag phong_global_tag = dice_transaction->store_for_reference_counting(phong_global.get());
273 scene->append(phong_global_tag, dice_transaction);
279 mi::base::Handle<nv::index::IStatic_scene_group> shape_group(
280 scene->create_scene_group<nv::index::IStatic_scene_group>());
285 const int user_prg = nv::index::app::get_sint32(opt_map[
"user_program_mode"]);
288 mi::base::Handle<nv::index::ISurface_sample_program> surface_sample_program(
289 scene->create_attribute<nv::index::ISurface_sample_program>());
293 if (program_src.empty()) {
303 surface_sample_program->set_program_source(program_src.c_str());
305 const mi::neuraylib::Tag surface_program_tag =
306 dice_transaction->store_for_reference_counting(surface_sample_program.get());
308 shape_group->append(surface_program_tag, dice_transaction);
315 mi::base::Handle<nv::index::IPhong_gl> phong_0(scene->create_attribute<nv::index::IPhong_gl>());
317 phong_0->set_ambient(mi::math::Color(0.f, 0.3f, 0.0f, 1.0f));
318 phong_0->set_diffuse(mi::math::Color(0.f, 0.1f, 0.0f, 1.0f));
319 phong_0->set_specular(mi::math::Color(0.2f, 0.2f, 0.2f, 1.0f));
320 phong_0->set_shininess(10);
321 const mi::neuraylib::Tag phong_0_tag = dice_transaction->store_for_reference_counting(phong_0.get());
323 shape_group->append(phong_0_tag, dice_transaction);
325 mi::base::Handle<nv::index::ISphere> sphere(scene->create_shape<nv::index::ISphere>());
327 sphere->set_center(mi::math::Vector<mi::Float32, 3>(0.3f, 0.5f, 0.3f));
328 sphere->set_radius(0.1f);
330 const mi::neuraylib::Tag tag = dice_transaction->store_for_reference_counting(
331 sphere.get(), mi::neuraylib:
:NULL_TAG,
"sphere");
333 shape_group->append(tag, dice_transaction);
339 mi::base::Handle<nv::index::IPhong_gl> phong_1(scene->create_attribute<nv::index::IPhong_gl>());
341 phong_1->set_ambient(mi::math::Color(0.f, 0.1f, 0.4f, 1.0f));
342 phong_1->set_diffuse(mi::math::Color(0.f, 0.3f, 0.6f, 1.0f));
343 phong_1->set_specular(mi::math::Color(0.4f, 0.4f, 0.5f, 1.0f));
344 phong_1->set_shininess(10);
345 const mi::neuraylib::Tag phong_1_tag = dice_transaction->store_for_reference_counting(phong_1.get());
347 shape_group->append(phong_1_tag, dice_transaction);
349 mi::base::Handle<nv::index::IEllipsoid> ellipsoid(scene->create_shape<nv::index::IEllipsoid>());
351 ellipsoid->set_center(mi::math::Vector<mi::Float32, 3>(0.750f, 0.410f, 0.100f));
352 ellipsoid->set_semi_axes(
353 mi::math::Vector<mi::Float32, 3>(0.15f, 0.f, 0.f),
354 mi::math::Vector<mi::Float32, 3>(0.f, 0.15f, 0.f),
357 const mi::neuraylib::Tag tag = dice_transaction->store_for_reference_counting(
358 ellipsoid.get(), mi::neuraylib:
:NULL_TAG,
"ellipsoid");
360 shape_group->append(tag, dice_transaction);
366 mi::base::Handle<nv::index::IPhong_gl> phong_2(scene->create_attribute<nv::index::IPhong_gl>());
368 phong_2->set_ambient(mi::math::Color(0.15f, 0.1f, 0.1f, 1.0f));
369 phong_2->set_diffuse(mi::math::Color(0.75f, 0.1f, 0.1f, 1.0f));
370 phong_2->set_specular(mi::math::Color(0.5f, 0.4f, 0.4f, 1.0f));
371 phong_2->set_shininess(50);
372 const mi::neuraylib::Tag phong_2_tag = dice_transaction->store_for_reference_counting(phong_2.get());
374 shape_group->append(phong_2_tag, dice_transaction);
376 mi::base::Handle<nv::index::ICone> cone(scene->create_shape<nv::index::ICone>());
378 cone->set_center(mi::math::Vector<mi::Float32, 3>(0.250f, 0.075f, 0.100f));
379 cone->set_tip(mi::math::Vector<mi::Float32, 3>(0.350f, 0.075f, 0.050f));
380 cone->set_radius(0.10f);
381 cone->set_capped(
false);
383 const mi::neuraylib::Tag tag = dice_transaction->store_for_reference_counting(
384 cone.get(), mi::neuraylib:
:NULL_TAG,
"cone");
386 shape_group->append(tag, dice_transaction);
392 mi::base::Handle<nv::index::IPhong_gl> phong_3(scene->create_attribute<nv::index::IPhong_gl>());
394 phong_3->set_ambient(mi::math::Color(0.1f, 0.15f, 0.1f, 1.0f));
395 phong_3->set_diffuse(mi::math::Color(0.5f, 0.55f, 0.5f, 1.0f));
396 phong_3->set_specular(mi::math::Color(0.4f, 0.4f, 0.5f, 1.0f));
397 phong_3->set_shininess(80);
398 const mi::neuraylib::Tag phong_3_tag = dice_transaction->store_for_reference_counting(phong_3.get());
400 shape_group->append(phong_3_tag, dice_transaction);
403 mi::base::Handle<nv::index::ICylinder> cylinder(scene->create_shape<nv::index::ICylinder>());
405 cylinder->set_bottom(mi::math::Vector<mi::Float32, 3>(0.850f, 0.705f, 0.700f));
406 cylinder->set_top(mi::math::Vector<mi::Float32, 3>(0.810f, 0.745f, 0.700f));
407 cylinder->set_radius(0.10f);
408 cylinder->set_capped(
true);
410 const mi::neuraylib::Tag tag = dice_transaction->store_for_reference_counting(
411 cylinder.get(), mi::neuraylib:
:NULL_TAG,
"cylinder");
413 shape_group->append(tag, dice_transaction);
418 mi::neuraylib::Tag tag = dice_transaction->store_for_reference_counting(
419 shape_group.get(), mi::neuraylib:
:NULL_TAG,
"shapes");
423 scene->append(tag, dice_transaction);
429 mi::base::Handle<nv::index::IStatic_scene_group> volume_group(
430 scene->create_scene_group<nv::index::IStatic_scene_group>());
433 const int user_prg = nv::index::app::get_sint32(opt_map[
"user_program_mode"]);
435 mi::base::Handle<nv::index::IVolume_sample_program> volume_sample_program(
436 scene->create_attribute<nv::index::IVolume_sample_program>());
445 volume_sample_program->set_program_source(program_src.c_str());
447 const mi::neuraylib::Tag volume_program_tag =
448 dice_transaction->store_for_reference_counting(volume_sample_program.get());
450 volume_group->append(volume_program_tag, dice_transaction);
455 mi::base::Handle<nv::index::ISparse_volume_rendering_properties> sparse_render_prop(
456 scene->create_attribute<nv::index::ISparse_volume_rendering_properties>());
457 sparse_render_prop->set_filter_mode(nv::index:
:SPARSE_VOLUME_FILTER_NEAREST);
458 sparse_render_prop->set_sampling_distance( 1.0);
459 sparse_render_prop->set_reference_sampling_distance( 1.0);
460 sparse_render_prop->set_voxel_offsets( mi::math::Vector<mi::Float32, 3>(0.5f, 0.5f, 0.5f));
461 sparse_render_prop->set_preintegrated_volume_rendering(
false);
462 sparse_render_prop->set_lod_rendering_enabled(
false);
463 sparse_render_prop->set_lod_pixel_threshold( 2.0);
464 sparse_render_prop->set_debug_visualization_option( 0);
465 const mi::neuraylib::Tag sparse_render_prop_tag
466 = dice_transaction->store_for_reference_counting(sparse_render_prop.get());
468 volume_group->append(sparse_render_prop_tag, dice_transaction);
469 INFO_LOG <<
"Created a sparse_render_prop_tag: tag = " << sparse_render_prop_tag;
473 const mi::Sint32 colormap_entry_id = 40;
474 mi::neuraylib::Tag colormap_tag =
475 create_colormap(colormap_entry_id, scene.get(), dice_transaction);
479 volume_group->append(colormap_tag, dice_transaction);
480 INFO_LOG <<
"Add a colormap to the scene: tag = " << colormap_tag;
484 const mi::math::Vector<mi::Uint32, 3> volume_size(4, 4, 2);
485 const mi::math::Bbox<mi::Uint32, 3> bbox_u32(mi::math::Vector<mi::Uint32, 3>(0), volume_size);
486 const mi::math::Bbox<mi::Float32, 3> bbox_f32(bbox_u32);
489 std::map<std::string, std::string> sparse_volume_opt;
490 sparse_volume_opt[
"type"] =
"sparse_volume";
491 sparse_volume_opt[
"importer"] =
"synthetic";
492 std::stringstream sstr;
493 sstr <<
"0 0 0 " << volume_size.x <<
" " << volume_size.y <<
" " << volume_size.z;
494 sparse_volume_opt[
"bbox"] = sstr.str();
495 sparse_volume_opt[
"voxel_format"] =
"uint8";
496 sparse_volume_opt[
"synthetic_type"] =
"default";
497 nv::index::IDistributed_data_import_callback* importer_callback =
498 get_importer_from_application_layer(
500 "nv::index::plugin::base_importer.Sparse_volume_generator_synthetic",
504 mi::math::Matrix<mi::Float32, 4, 4> transform_mat(1.0f);
505 const mi::math::Vector<mi::Float32, 3> scale(1.0f, 1.0f, 1.0f);
506 const mi::math::Vector<mi::Float32, 3> translate(0.0f, 0.0f, 0.0f);
507 const bool is_rendering_enabled =
true;
509 mi::base::Handle<nv::index::ISparse_volume_scene_element> volume_scene_element(
510 scene->create_sparse_volume(bbox_f32, transform_mat, importer_callback, dice_transaction));
512 volume_scene_element->set_enabled(is_rendering_enabled);
515 const mi::neuraylib::Tag tag = dice_transaction->store_for_reference_counting(
516 volume_scene_element.get(), mi::neuraylib:
:NULL_TAG,
"volume");
518 volume_group->append(tag, dice_transaction);
522 mi::neuraylib::Tag tag = dice_transaction->store_for_reference_counting(
523 volume_group.get(), mi::neuraylib:
:NULL_TAG,
"volume_group");
527 scene->append(tag, dice_transaction);
536 const mi::neuraylib::Tag& camera_tag,
537 mi::neuraylib::IDice_transaction* transaction)
const
542 mi::base::Handle<nv::index::IPerspective_camera> cam(
543 transaction->edit<nv::index::IPerspective_camera>(camera_tag));
547 const mi::math::Vector<mi::Float32, 3> from(0.36143f, -0.77451f, 0.60702f);
548 const mi::math::Vector<mi::Float32, 3> up(0.093147f, 0.652653f, 0.751908f);
549 mi::math::Vector<mi::Float32, 3> viewdir(0.12330f, 0.741816f, -0.659170f);
552 cam->set(from, viewdir, up);
553 cam->set_aperture(0.033f);
554 cam->set_aspect(1.f);
555 cam->set_focal(0.03f);
556 cam->set_clip_min(0.001f);
557 cam->set_clip_max(10000.f);
const char * get_roi_string() const
bool create_scene(nv::index::app::IApplication_layer *app_layer, Scene_info &scene_info, const mi::math::Bbox< mi::Float32, 3 > &roi_bbox, const mi::neuraylib::Tag &session_tag, std::map< std::string, std::string > &opt_map, mi::neuraylib::IDice_transaction *dice_transaction) const
void register_classes(nv::index::IIndex *index_interface) const
void setup_camera(const mi::neuraylib::Tag &camera_tag, mi::neuraylib::IDice_transaction *transaction) const
const char * name() const
const mi::math::Bbox< mi::Float32, 3 > get_roi_box() const
static const char * program_end
static const char * color_program_scalar
static const char * color_program_rgba
static const char * inquire_program
static const char * program_begin
static const char * program_end
static const char * program_begin
static const char * inquire_program
static const char * color_program
Scene setup interface for ray sampling example.
#define check_success(expr)
std::string rtc_program_source