This example demonstrates typical MDL-related tasks. Based on the scene main.mi
we show how to modify inputs of material instances, how to create new material instances, and how to attach function calls to material inputs.
#include <iostream>
#include "example_shared.h"
#include "example_render_target_simple.h"
{
check_success( rc->add_mdl_path( mdl_path) == 0);
check_success( rc->add_mdl_path( ".") == 0);
check_success( rc->add_resource_path( ".") == 0);
}
void render_and_export(
const char* uri)
{
scene->create_render_context( transaction, "iray"));
check_success( render_context.is_valid_interface());
scheduler_mode->set_c_str( "batch");
render_context->set_option( "scheduler_mode", scheduler_mode.get());
scene = 0;
new Render_target( image_api.get(), "Color", 512, 384));
check_success( render_context->render( transaction, render_target.get(), 0) >= 0);
check_success( export_api.is_valid_interface());
export_api->export_canvas( uri, canvas.get());
}
void import_and_render_original_scene(
{
check_success( transaction.is_valid_interface());
check_success( import_api.is_valid_interface());
import_api->import_elements( transaction.get(), "file:main.mi"));
check_success( import_result->get_error_number() == 0);
scene->set_rootgroup( import_result->get_rootgroup());
scene->set_options( import_result->get_options());
scene->set_camera_instance( import_result->get_camera_inst());
transaction->
store( scene.get(),
"the_scene");
render_and_export( neuray, transaction.get(), "file:example_mdl_original.png");
}
void modify_material_instance(
{
check_success( transaction.is_valid_interface());
{
yellow_material->get_arguments());
arguments->get_expression( "tint"));
expression_factory->clone( argument.get()));
check_success( yellow_material->set_argument( "tint", new_argument.get()) == 0);
}
{
transaction.get(), "yellow_material", mdl_factory);
check_success( yellow_material.set_value( "tint", bright_grey) == 0);
}
render_and_export(
neuray, transaction.get(), "file:example_mdl_modified_material_argument.png");
}
void create_new_material_instance(
{
check_success( transaction.is_valid_interface());
{
value_factory->create_color( 1.0f, 1.0f, 1.0f));
expression_factory->create_constant( value.get()));
expression_factory->create_expression_list());
arguments->add_expression( "tint", expression.get());
"mdl::main::diffuse_material(color)"));
material_definition->create_function_call( arguments.get(), &result));
check_success( result == 0);
transaction->
store( material_instance.get(),
"white_material");
check_success(
}
render_and_export( neuray, transaction.get(), "file:example_mdl_new_material_instance.png");
}
void attach_function_call(
{
check_success( transaction.is_valid_interface());
{
check_success( image->reset_file( "nvidia_logo.png") == 0);
transaction->
store( image.get(),
"nvidia_image");
texture->set_image( "nvidia_image");
texture->set_gamma( 2.2f);
transaction->
store( texture.get(),
"nvidia_texture");
}
{
import_api->import_elements( transaction.get(), "${shader}/base.mdl"));
check_success( import_result->get_error_number() == 0);
}
{
module->get_function_overloads( "mdl::base::file_texture"));
check_success( overloads->get_length() == 1);
file_texture_name->get_c_str()));
function_definition->get_parameter_types());
types->get_type( "texture"));
value_factory->create( type.get()));
expression_factory->create_constant( value.get()));
expression_factory->create_expression_list());
arguments->add_expression( "texture", expression.get());
function_definition->create_function_call( arguments.get(), &result));
check_success( result == 0);
function_definition = 0;
transaction->
store( function_call.get(),
"file_texture_call");
}
{
expression_factory->create_call( "file_texture_call"));
expression_factory->create_expression_list());
arguments->add_expression( "s", expression.get());
"mdl::base::texture_return.tint(::base::texture_return)"));
function_definition->create_function_call( arguments.get(), &result));
check_success( result == 0);
transaction->
store( function_call.get(),
"texture_return.tint_call");
}
{
expression_factory->create_call( "texture_return.tint_call"));
check_success( grey_material->set_argument( "tint", expression.get()) == 0);
}
{
transaction.get(), "grey_material", mdl_factory);
check_success( grey_material.set_call( "tint", "texture_return.tint_call") == 0);
}
render_and_export( neuray, transaction.get(), "file:example_mdl_attached_function_call.png");
}
void use_struct_and_array_constructors(
{
check_success( transaction.is_valid_interface());
{
expression_factory->create_call( "texture_return.tint_call"));
type_factory->create_enum( "::base::color_layer_mode"));
mi::Size index = type_mode->find_value(
"color_layer_brightness");
check_success( index !=
static_cast<mi::Size>( -1));
value_factory->create_enum( type_mode.get(), index));
expression_factory->create_constant( value_mode.get()));
expression_factory->create_expression_list());
arguments->add_expression( "layer_color", expression_layer_color.get());
arguments->add_expression( "mode", expression_mode.get());
"mdl::base::color_layer(color,float,::base::color_layer_mode)"));
function_definition->create_function_call( arguments.get(), &result));
check_success( result == 0);
transaction->
store( function_call.get(),
"color_layer0");
}
{
value_factory->create_color( 0.0f, 0.0f, 1.0f));
expression_factory->create_constant( value_layer_color.get()));
type_factory->create_enum( "::base::color_layer_mode"));
mi::Size index = type_mode->find_value(
"color_layer_average");
check_success( index !=
static_cast<mi::Size>( -1));
value_factory->create_enum( type_mode.get(), index));
expression_factory->create_constant( value_mode.get()));
expression_factory->create_expression_list());
arguments->add_expression( "layer_color", expression_layer_color.get());
arguments->add_expression( "mode", expression_mode.get());
"mdl::base::color_layer(color,float,::base::color_layer_mode)"));
function_definition->create_function_call( arguments.get(), &result));
check_success( result == 0);
transaction->
store( function_call.get(),
"color_layer1");
}
{
expression_factory->create_call( "color_layer0"));
expression_factory->create_call( "color_layer1"));
expression_factory->create_expression_list());
arguments->add_expression( "value0", expression_color_layer0.get());
arguments->add_expression( "value1", expression_color_layer1.get());
function_definition->create_function_call( arguments.get(), &result));
check_success( result == 0);
transaction->
store( function_call.get(),
"color_layers");
}
{
expression_factory->create_call( "color_layers"));
value_factory->create_color( 1.0f, 0.0f, 0.0f));
expression_factory->create_constant( value_base.get()));
expression_factory->create_expression_list());
arguments->add_expression( "layers", expression_layers.get());
arguments->add_expression( "base", expression_base.get());
"mdl::base::blend_color_layers(::base::color_layer[P],color,::base::mono_mode)"));
function_definition->create_function_call( arguments.get(), &result));
check_success( result == 0);
transaction->
store( function_call.get(),
"blended_color_layers");
}
{
expression_factory->create_call( "blended_color_layers"));
expression_factory->create_expression_list());
arguments->add_expression( "s", expression.get());
"mdl::base::texture_return.tint(::base::texture_return)"));
function_definition->create_function_call( arguments.get(), &result));
check_success( result == 0);
transaction->
store( function_call.get(),
"texture_return.tint_call2");
}
{
transaction.get(), "grey_material", mdl_factory);
check_success( grey_material.set_call( "tint", "texture_return.tint_call2") == 0);
}
render_and_export(
neuray, transaction.get(), "file:example_mdl_struct_and_array_constructors.png");
}
void create_variant(
{
check_success( transaction.is_valid_interface());
{
grey_material->get_arguments());
expression_factory->clone( arguments.get()));
value_factory->create_string( "a textured variant of ::main::diffuse_material"));
expression_factory->create_constant( anno_arg_value.get()));
expression_factory->create_expression_list());
anno_args->add_expression( "description", anno_arg_expression.get());
expression_factory->create_annotation( "::anno::description(string)", anno_args.get()));
expression_factory->create_annotation_block());
anno_block->add_annotation( anno.get());
transaction.get(),
"mdl::variants",
context.get()));
check_success( module_builder);
check_success( module_builder->add_variant(
"textured_material",
"mdl::main::diffuse_material(color)",
defaults.get(),
anno_block.get(),
0,
true,
context.get()) == 0);
}
{
"mdl::variants::textured_material(color)"));
textured_material_def->create_function_call( 0, &result));
check_success( result == 0);
transaction->
store( textured_material.get(),
"textured_material");
check_success(
}
{
element->set_c_str( "mdl::variants");
export_api->export_elements( transaction.get(), "file:variants.mdl", elements.get()));
}
render_and_export( neuray, transaction.get(), "file:example_mdl_variant.png");
}
void distill(
{
check_success( transaction.is_valid_interface());
{
check_success(grey_material.is_valid_interface());
grey_material->create_compiled_material(flags));
check_success(comiled_material.is_valid_interface());
check_success(distiller_api.is_valid_interface());
distiller_api->distill_material(comiled_material.get(), "diffuse"));
check_success(distilled_material.is_valid_interface());
distilled_material.get(),
"surface.scattering.tint"));
check_success(baker.is_valid_interface());
expression_factory->create_expression_list());
value_factory->create_bool(!baker->is_uniform()));
expression_factory->create_constant(use_tex.get()));
target_material_arguments->add_expression("tint_use_texture", use_tex_expr.get());
if(baker->is_uniform())
{
check_success(baker->bake_constant(constant_color.get()) == 0);
value_factory->create_color(c.r, c.g, c.b));
expression_factory->create_constant(color.get()));
target_material_arguments->add_expression("tint_color", color_expr.get());
}
else
{
check_success(image_api.is_valid_interface());
image_api->create_canvas("Rgb_fp", 1024, 1024));
check_success(canvas.is_valid_interface());
check_success(baker->bake_texture(canvas.get(), 4) == 0);
check_success( export_api.is_valid_interface());
export_api->export_canvas( "file:bake_result.png", canvas.get());
check_success( image->set_from_canvas(canvas.get()));
transaction->
store( image.get(),
"baked_image");
texture->set_image( "baked_image");
transaction->
store( texture.get(),
"baked_texture");
value_factory->create_texture(tex_type.get(), "baked_texture"));
expression_factory->create_constant(tex.get()));
target_material_arguments->add_expression("tint_texture", tex_ref.get());
}
transaction.get(),
"mdl::main::textured_diffuse_material(texture_2d,bool,color)",
mdl_factory);
check_success(target_material_def.is_valid());
target_material_def.create_instance(target_material_arguments.get()));
transaction->
store(target_material.get(),
"target_material");
check_success(
}
render_and_export( neuray, transaction.get(), "file:example_mdl_distilling.png");
}
{
check_success( database.is_valid_interface());
database->get_global_scope());
import_and_render_original_scene( neuray, scope.get());
modify_material_instance( neuray, scope.get(), mdl_factory.get());
create_new_material_instance( neuray, scope.get(), mdl_factory.get());
attach_function_call( neuray, scope.get(), mdl_factory.get());
use_struct_and_array_constructors( neuray, scope.get(), mdl_factory.get());
create_variant( neuray, scope.get(), mdl_factory.get());
distill( neuray, scope.get(), mdl_factory.get());
}
int main( int argc, char* argv[])
{
if( argc != 2) {
std::cerr << "Usage: example_mdl <mdl_path>" << std::endl;
keep_console_open();
return EXIT_FAILURE;
}
const char* mdl_path = argv[1];
check_success( neuray.is_valid_interface());
configuration( neuray.get(), mdl_path);
check_start_success( result);
rendering( neuray.get());
check_success( neuray->
shutdown() == 0);
neuray = 0;
check_success( unload());
keep_console_open();
return EXIT_SUCCESS;
}
This interface represents static arrays, i.e., arrays with a fixed number of elements.
Definition: iarray.h:37
This interface represents RGBA colors.
Definition: icolor.h:28
A simple string class.
Definition: istring.h:22
Handle class template for interfaces, automatizing the lifetime control via reference counting.
Definition: handle.h:113
Standard RGBA color class with floating point elements and operations.
Definition: color.h:81
A wrapper around the interface for MDL material instances and function calls.
Definition: argument_editor.h:57
A wrapper around the interface for MDL function definitions.
Definition: definition_wrapper.h:44
This interface is used to interact with the distributed database.
Definition: idatabase.h:293
This interface is used to export files.
Definition: iexport_api.h:38
A constant expression.
Definition: iexpression.h:94
This interface represents a function call.
Definition: ifunction_call.h:52
This interface represents a function definition.
Definition: ifunction_definition.h:44
This interface provides various utilities related to canvases and buffers.
Definition: iimage_api.h:49
This interface represents a pixel image file.
Definition: iimage.h:65
This interface is used to import files.
Definition: iimport_api.h:100
An instance is a scene element that adds a transformation and attributes to another scene element.
Definition: iinstance.h:117
This interface represents a material instance.
Definition: imaterial_instance.h:34
@ DEFAULT_OPTIONS
Default compilation options (e.g., instance compilation).
Definition: imaterial_instance.h:40
Provides access to various functionality related to MDL distilling.
Definition: imdl_distiller_api.h:47
Factory for various MDL interfaces and functions.
Definition: imdl_factory.h:53
virtual IMdl_execution_context * create_execution_context()=0
Creates an execution context.
virtual IMdl_module_builder * create_module_builder(ITransaction *transaction, const char *module_name, Mdl_version min_module_version, Mdl_version max_module_version, IMdl_execution_context *context)=0
Creates a module builder for a given module.
virtual IType_factory * create_type_factory(ITransaction *transaction)=0
Returns an MDL type factory for the given transaction.
virtual IExpression_factory * create_expression_factory(ITransaction *transaction)=0
Returns an MDL expression factory for the given transaction.
virtual IValue_factory * create_value_factory(ITransaction *transaction)=0
Returns an MDL value factory for the given transaction.
This interface represents an MDL module.
Definition: imodule.h:611
This is an object representing the Iray library.
Definition: ineuray.h:44
virtual Sint32 shutdown(bool blocking=true)=0
Shuts down the library.
virtual base::IInterface * get_api_component(const base::Uuid &uuid) const =0
Returns an API component from the Iray SDK API.
virtual Sint32 start(bool blocking=true)=0
Starts the operation of the Iray library.
This interface is used to load plugins and to query information about loaded plugins.
Definition: iplugin_configuration.h:24
This interface is used to query and change the rendering configuration.
Definition: irendering_configuration.h:109
The scene is the top-level element describing a subset of DB elements to be rendered.
Definition: iscene.h:44
A scope is the context which determines the visibility of database elements.
Definition: iscope.h:48
virtual ITransaction * create_transaction()=0
Creates a new transaction associated with this scope.
Textures add image processing options to images.
Definition: itexture.h:68
A transaction provides a consistent view on the database.
Definition: itransaction.h:81
virtual const base::IInterface * access(const char *name)=0
Retrieves an element from the database.
virtual base::IInterface * create(const char *type_name, Uint32 argc=0, const base::IInterface *argv[]=0)=0
Creates an object of the type type_name.
virtual base::IInterface * edit(const char *name)=0
Retrieves an element from the database and returns it ready for editing.
virtual Sint32 commit()=0
Commits the transaction.
virtual Sint32 store(base::IInterface *db_element, const char *name, Uint8 privacy=LOCAL_SCOPE)=0
Stores the element db_element in the database under the name name and with the privacy level privacy.
@ TS_2D
Two-dimensional texture.
Definition: itype.h:453
#define MI_BASE_DLL_FILE_EXT
The operating system specific default filename extension for shared libraries (DLLs)
Definition: config.h:340
unsigned int Uint32
32-bit unsigned integer.
Definition: types.h:49
Uint64 Size
Unsigned integral type that is large enough to hold the size of all types.
Definition: types.h:112
signed int Sint32
32-bit signed integer.
Definition: types.h:46
mi::Sint32 set_value(mi::neuraylib::IValue *value, const T &v)
Simplifies setting the value of mi::neuraylib::IValue from the corresponding classes from the base an...
Definition: ivalue.h:925
@ MDL_VERSION_1_0
MDL version 1.0.
Definition: iexpression.h:28
@ MDL_VERSION_LATEST
Latest MDL version.
Definition: iexpression.h:38
mi::Sint32 set_value(mi::IData *data, const T &value)
Simplifies setting the value of mi::IData from the corresponding classes from the base and math API.
Definition: set_get.h:52
mi::Sint32 get_value(const mi::IData *data, T &value)
Simplifies reading the value of mi::IData into the corresponding classes from the base and math API.
Definition: set_get.h:267