This example uses function calls to create a textured material.
Arguments of material instances and function calls are not values, but expressions. For example, the argument for the "tint"
parameter in the previous example is an instance of mi::neuraylib::IExpression, and not of mi::neuraylib::IValue_color. Why is this indirection needed?
If material instances and function calls are only used in isolation, simple values like mi::neuraylib::IValue_color would be sufficient. However, much of the flexibility of MDL comes from the fact that one can create connections between material instances and function calls (provided the return type and the parameter type match). These connections are represented by call expressions while simple values are represented by constant expressions.
The method create_texture_material()
shows all the steps needed to create a textured material in a bottom-up order. First we create a DB element of type mi::neuraylib::IImage that represents the provided .png file, and a DB element of type mi::neuraylib::ITexture that references the image.
Next we load the MDL modules we want to use. The example
module contains the material definition that we are going to use later. The base
module contains a function definition that we are using in the next step to make use of the texture.
The function definition base::file_texture()
is used to access the texture object in the DB. Apart from the texture name it provides several arguments to influence the texture lookup process. In this example we just set the texture name and leave the other arguments at their defaults. This step uses mi::neuraylib::IModule::get_function_overloads() to retrieve the exact name of a function definition (which includes the signature). Using this method frees one from hardcoding the signature if there is only one overload.
The return type of the function definition base::file_texture()
is base::texture_return
, a struct of two fields: "tint"
of type mi::neuraylib::IType_color and "mono"
of type mi::neuraylib::IType_float. Since the material definition we plan to use has a parameter of type mi::neuraylib::IType_color we cannot directly attach the function call of base::file_texture()
. Instead we have to isolate the "tint"
field in the returned struct. This can be done by a so-called member selection operator (see Structs for details). In this case, the name of the required struct getter is base::texture_return.tint()
.
Finally, we connect the just created instance of the member selection operator to the "tint"
argument of our diffuse material. The dump of the material instance and all function calls clearly shows the created call expressions.
Source Code Location: examples/mdl_sdk/example_calls.cpp