This example creates material instances and function calls by instantiating the corresponding material and function definitions. It also shows how to change arguments of material instances and function calls.
#include <iostream>
#include <string>
#include "example_shared.h"
const char* material_definition_name = "mdl::nvidia::sdk_examples::tutorials::example_material";
const char* material_instance_name = "instance of example_material";
const char* function_definition_name =
"mdl::nvidia::sdk_examples::tutorials::example_function(color,float)";
const char* function_call_name = "call of example_function";
template <class T>
void dump_instance(
{
mi::Size count = material->get_parameter_count();
for(
mi::Size index = 0; index < count; index++) {
arguments->get_expression( index));
std::string name = material->get_parameter_name( index);
expression_factory->
dump( argument.get(), name.c_str(), 1));
s <<
" argument " << argument_text->get_c_str() <<
std::endl;
}
}
void instantiate_definitions(
{
transaction, "::nvidia::sdk_examples::tutorials", context.get()) >= 0);
print_messages( context.get());
check_success( module.is_valid_interface());
{
material_definition_name));
material_definition->create_material_instance( 0, &result));
check_success( result == 0);
std::cout <<
"Dumping material instance \"" << material_instance_name <<
"\":" <<
std::endl;
dump_instance( expression_factory.get(), material_instance.get(),
std::cout);
transaction->
store( material_instance.get(), material_instance_name);
}
{
function_definition_name));
value_factory->create_color( 1.0f, 0.0f, 0.0f));
arguments->add_expression( "tint", tint_expr.get());
value_factory->create_float( 2.0f));
arguments->add_expression( "distance", distance_expr.get());
function_definition->create_function_call( arguments.get(), &result));
check_success( result == 0);
dump_instance( expression_factory.get(), function_call.get(),
std::cout);
transaction->
store( function_call.get(), function_call_name);
}
}
{
{
check_success( material_instance.is_valid_interface());
arguments->get_expression( "tint"));
expression_factory->
clone( argument.get()));
check_success( material_instance->
set_argument(
"tint", new_argument.get()) == 0);
std::cout <<
"Dumping modified material instance \"" << material_instance_name <<
"\":"
dump_instance( expression_factory.get(), material_instance.get(),
std::cout);
}
{
{
transaction, function_call_name, mdl_factory.get());
check_success( argument_editor.set_value( "tint", blue) == 0);
}
check_success( function_call.is_valid_interface());
std::cout <<
"Dumping modified function call \"" << function_call_name <<
"\":"
dump_instance( expression_factory.get(), function_call.get(),
std::cout);
}
}
{
mi::neuraylib::Annotation_wrapper annotations( anno_block);
<< annotations.get_annotation_count() << " annotation(s).\n";
for(
mi::Size a = 0; a < annotations.get_annotation_count(); ++a)
{
std::string signature( annotations.get_annotation_name( a));
size_t p = signature.
find(
'(');
if( p != std::string::npos)
name = signature.
substr( 0, p);
if( p != std::string::npos)
<< annotations.get_annotation_param_count( a) << " parameter(s):\n";
for(
mi::Size p = 0; p < annotations.get_annotation_param_count( a); ++p)
{
annotations.get_annotation_param_type( a, p));
std::cout <<
" '" << annotations.get_annotation_param_name( a, p)
<< "' of kind of type '"
<< type_handle->get_kind() << "' -> ";
switch( type_handle->get_kind())
{
{
const char* string_value = nullptr;
annotations.get_annotation_param_value<const char*>( a, p, string_value);
break;
}
{
annotations.get_annotation_param_value<
mi::Float32>( a, p, float_value);
break;
}
default:
{
annotations.get_annotation_param_value( a, p)).get() << "\n";
break;
}
}
}
}
std::cout <<
"Index of 'hard_range': " << annotations.get_annotation_index(
"::anno::hard_range(float,float)") << "\n";
"::anno::foo(int)")) << " (which is not present)\n";
const char* descValue = nullptr;
mi::Sint32 res = annotations.get_annotation_param_value_by_name<
const char*>(
"::anno::description(string)", 0, descValue);
std::cout <<
"Value of 'description': \"" << ( res == 0 ? descValue :
"nullptr") <<
"\"\n";
res = annotations.get_annotation_param_value_by_name<
mi::Sint32>(
"::anno::foo(int)", 0, fooValue);
if ( res != 0)
std::cout <<
"Value of 'foo' not found (annotation is not present)\n";
}
{
expression_factory->
clone( arguments.get()));
value_factory->create_string(
"a variant of ::nvidia::sdk_examples::tutorials::example_material"
" with different defaults"));
anno_args->add_expression( "description", anno_arg_expression.get());
expression_factory->
create_annotation(
"::anno::description(string)", anno_args.get()));
range_args->add_expression( "min", range_min_expression.get());
range_args->add_expression( "max", range_max_expression.get());
"::anno::hard_range(float,float)", range_args.get()));
variant_name->set_c_str( "green_example_material") ;
prototype_name->set_c_str( "mdl::nvidia::sdk_examples::tutorials::example_material");
check_success( variant->set_value( "defaults", defaults.get()) == 0);
check_success( variant->set_value( "annotations", anno_block.get()) == 0);
print_annotations( anno_block.get());
transaction, "::variants", variant_data.get()) == 0);
"mdl::variants::green_example_material"));
material_instance = material_definition->create_material_instance( 0, &result);
check_success( result == 0);
std::cout <<
"Dumping material instance with defaults of material definition "
"\"mdl::variants::green_example_material\":" <<
std::endl;
dump_instance( expression_factory.get(), material_instance.get(),
std::cout);
check_success( mdl_compiler->
export_module( transaction,
"mdl::variants",
"variants.mdl") == 0);
}
int main( int , char* [])
{
check_success( neuray.is_valid_interface());
configure( neuray.get());
check_start_success( result);
{
{
instantiate_definitions( neuray.get(), transaction.get());
change_arguments( neuray.get(), transaction.get());
create_variant( neuray.get(), transaction.get());
}
}
check_success( neuray->
shutdown() == 0);
neuray = 0;
check_success( unload());
keep_console_open();
return EXIT_SUCCESS;
}