This example programmatically creates a scene via API calls instead of importing it from a scene file.
#include <iostream>
#include "example_shared.h"
#include "example_render_target_simple.h"
{
attribute->set_value( value);
}
{
mdl_factory->create_value_factory( transaction));
mdl_factory->create_expression_factory( transaction));
{
check_success( import_api.is_valid_interface());
import_api->import_elements( transaction, "file:${shader}/main.mdl"));
check_success( import_result->get_error_number() == 0);
}
{
transaction->
store( options.get(),
"options");
}
{
value_factory->create_color( 0.942f, 0.807216f, 0.33441f));
expression_factory->create_constant( value.get()));
expression_factory->create_expression_list());
arguments->add_expression( "tint", expression.get());
"mdl::main::diffuse_material(color)"));
definition->create_function_call( arguments.get()));
check_success( material.get());
transaction->
store( material.get(),
"yellow_material");
}
{
value_factory->create_color( 0.306959f, 0.306959f, 0.306959f));
expression_factory->create_constant( value.get()));
expression_factory->create_expression_list());
arguments->add_expression( "tint", expression.get());
"mdl::main::diffuse_material(color)"));
definition->create_function_call( arguments.get()));
check_success( material.get());
transaction->
store( material.get(),
"grey_material");
}
{
value_factory->create_color( 1000.0f, 1000.0f, 1000.0f));
expression_factory->create_constant( value.get()));
expression_factory->create_expression_list());
arguments->add_expression( "tint", expression.get());
"mdl::main::diffuse_light(color)"));
definition->create_function_call( arguments.get()));
check_success( material.get());
transaction->
store( material.get(),
"white_light");
}
{
create_flag( mesh.get(), "visible", true);
create_flag( mesh.get(), "reflection_cast", true);
create_flag( mesh.get(), "reflection_recv", true);
create_flag( mesh.get(), "refraction_cast", true);
create_flag( mesh.get(), "refraction_recv", true);
create_flag( mesh.get(), "shadow_cast", true);
create_flag( mesh.get(), "shadow_recv", true);
mesh->reserve_points( cube_n_points);
mesh->append_point( cube_points[i]);
mesh->reserve_triangles( cube_n_triangles);
mesh->append_triangle( cube_mesh_connectivity[i]);
mesh->create_connectivity());
normal_connectivity->set_triangle_indices(
normals->append_vector3( cube_normals[i]);
check_success( normal_connectivity->attach_attribute_vector( normals.get()) == 0);
check_success( mesh->attach_connectivity( normal_connectivity.get()) == 0);
mesh->create_connectivity());
uv_connectivity->set_triangle_indices(
uvs->append_float32( &cube_uvs[i][0], 2);
check_success( uv_connectivity->attach_attribute_vector( uvs.get()) == 0);
check_success( mesh->attach_connectivity( uv_connectivity.get()) == 0);
transaction->
store( mesh.get(),
"cube");
}
{
check_success( instance->attach( "cube") == 0);
matrix.translate( 1.3f, -0.5f, 1.0f);
instance->set_matrix( matrix);
instance->create_attribute<
mi::IRef>(
"material",
"Ref"));
check_success( material->set_reference( "yellow_material") == 0);
transaction->
store( instance.get(),
"cube_instance");
}
{
create_flag( mesh.get(), "visible", true);
create_flag( mesh.get(), "reflection_cast", true);
create_flag( mesh.get(), "reflection_recv", true);
create_flag( mesh.get(), "refraction_cast", true);
create_flag( mesh.get(), "refraction_recv", true);
create_flag( mesh.get(), "shadow_cast", true);
create_flag( mesh.get(), "shadow_recv", true);
mesh->reserve_points( ground_n_points);
mesh->append_point( ground_points[i]);
mesh->reserve_triangles( ground_n_triangles);
for(
mi::Uint32 i = 0; i < ground_n_triangles; ++i)
mesh->append_triangle( ground_mesh_connectivity[i]);
mesh->create_connectivity());
for(
mi::Uint32 i = 0; i < ground_n_triangles; ++i)
normal_connectivity->set_triangle_indices(
normals->append_vector3( ground_normals[i]);
check_success( normal_connectivity->attach_attribute_vector( normals.get()) == 0);
check_success( mesh->attach_connectivity( normal_connectivity.get()) == 0);
mesh->create_connectivity());
for(
mi::Uint32 i = 0; i < ground_n_triangles; ++i)
uv_connectivity->set_triangle_indices(
uvs->append_float32( &ground_uvs[i][0], 2);
check_success( uv_connectivity->attach_attribute_vector( uvs.get()) == 0);
check_success( mesh->attach_connectivity( uv_connectivity.get()) == 0);
transaction->
store( mesh.get(),
"ground");
}
{
check_success( instance->attach( "ground") == 0);
instance->create_attribute<
mi::IRef>(
"material",
"Ref"));
check_success( material->set_reference( "grey_material") == 0);
transaction->
store( instance.get(),
"ground_instance");
}
{
create_flag( light.get(), "shadow_cast", true);
light->create_attribute<
mi::IRef>(
"material",
"Ref"));
check_success( material->set_reference( "white_light") == 0);
transaction->
store( light.get(),
"light");
}
{
check_success( instance->attach( "light") == 0);
matrix.translate( 5.1f, -7.3f, -1.6f);
instance->set_matrix( matrix);
transaction->
store( instance.get(),
"light_instance");
}
{
camera->set_focal( 50.0f);
camera->set_aperture( 44.0f);
camera->set_aspect( 1.33333f);
camera->set_resolution_x( 512);
camera->set_resolution_y( 384);
camera->set_clip_min( 0.1);
camera->set_clip_max( 1000);
transaction->
store( camera.get(),
"camera");
}
{
check_success( instance->attach( "camera") == 0);
0.68826f, 0.37107f, -0.623382f, 0.0f,
0.00000f, 0.85929f, 0.511493f, 0.0f,
0.72546f, -0.35204f, 0.591414f, 0.0f,
0.00000f, 0.00000f, -6.256200f, 1.0f);
instance->set_matrix( matrix);
transaction->
store( instance.get(),
"camera_instance");
}
{
check_success( group->attach( "cube_instance" ) == 0);
check_success( group->attach( "ground_instance") == 0);
check_success( group->attach( "light_instance" ) == 0);
check_success( group->attach( "camera_instance") == 0);
transaction->
store( group.get(),
"rootgroup");
}
return scene;
}
{
check_success( rc.is_valid_interface());
check_success( rc->add_mdl_path( mdl_path) == 0);
check_success( rc->add_mdl_path( ".") == 0);
}
{
check_success( database.is_valid_interface());
database->get_global_scope());
scope->create_transaction());
check_success( transaction.is_valid_interface());
transaction->
store( scene.get(),
"the_scene");
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.get(), render_target.get(), 0) >= 0);
check_success( export_api.is_valid_interface());
export_api->export_canvas( "file:example_scene.png", canvas.get());
}
int main( int argc, char* argv[])
{
if( argc != 2) {
std::cerr << "Usage: example_scene <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 bool.
Definition: inumber.h:122
A reference is an object that acts as a pointer to other database elements.
Definition: iref.h:25
A simple string class.
Definition: istring.h:22
Handle class template for interfaces, automatizing the lifetime control via reference counting.
Definition: handle.h:113
NxM-dimensional matrix class template of fixed dimensions.
Definition: matrix.h:367
Fixed-size math vector class template with generic operations.
Definition: vector.h:286
The attribute set comprises all attributes attached to a database element.
Definition: iattribute_set.h:332
virtual IData * create_attribute(const char *name, const char *type)=0
Creates a new attribute name of the type type.
The camera defines the viewpoint from which the scene is rendered.
Definition: icamera.h:56
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
This interface represents a function definition.
Definition: ifunction_definition.h:44
A group is a container for other scene elements.
Definition: igroup.h:39
This interface provides various utilities related to canvases and buffers.
Definition: iimage_api.h:49
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
Point, spot, directional and area lighting.
Definition: ilight.h:68
Factory for various MDL interfaces and functions.
Definition: imdl_factory.h:53
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.
A scene element that stores scene-specific settings.
Definition: ioptions.h:115
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
virtual Sint32 set_camera_instance(const char *camera)=0
Sets the camera instance of this scene.
virtual Sint32 set_options(const char *options)=0
Sets the options of this scene.
virtual IRender_context * create_render_context(ITransaction *transaction, const char *render_mode, Sint32 *errors=0)=0
Creates a render context suitable for rendering the scene with a given render mode.
virtual Sint32 set_rootgroup(const char *root)=0
Sets the root group of this scene.
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.
Interface representing a triangle mesh.
Definition: itriangle_mesh.h:122
Handle class for type-safety.
Definition: identifier.h:66
A triangle defined by three point indices, starting at index 0.
Definition: itriangle_connectivity.h:33
#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
signed int Sint32
32-bit signed integer.
Definition: types.h:46
math::Vector<Float32, 2> Float32_2
Vector of two Float32.
Definition: vector_typedefs.h:83
math::Vector<Float32, 3> Float32_3
Vector of three Float32.
Definition: vector_typedefs.h:90
@ ATTR_TEXTURE
Texture coordinates of type mi::Float32[] per point or per vertex (also called texture space).
Definition: iattribute_vector.h:101
@ ATTR_NORMAL
Surface normals of type mi::Float32_3 per point or per vertex.
Definition: iattribute_vector.h:65