...
Code Block |
---|
|
/**
Polygonal meshes can be created minimally by specifying "P".
NSI's C++ API provides an easy interface to pass parameters to all NSI
API calls through the Args class.
*/
NSI::Context nsi;
const char *k_poly_handle = "simple polygon"; /* avoids typos */
nsi.Create( "simply polygon"k_poly_handle, "mesh" );
NSI::ArgumentList mesh_args;
float points[3*4] = { ... /* insert your favorite 4 points here */}; -1, 1, 0, 1, 1, 0, 1, -1, 0, -1, -1, 0 };
mesh_args.Add(
NSI::Argument::New( "P" )
->SetType( NSITypePoint )
->SetCount( 4 )
->SetValuePointer( const_cast<float*>(points ) );
nsi.SetAttribute( "simple polgyon"k_poly_handle, mesh_args );
|
Specifying normals and other texture coordinates follows the same logic. Constant attributes can be declared in a more concise form:
Code Block |
---|
|
/** Turn our mesh into a subdivision surface */
nsi.SetAttribute( "simple polygon"k_poly_handle,
NSI::CStringPArg("subdivision.scheme", "catmull-clark") ); |
...
In NSI, a geometry is rendered only if connected to scene's root (which has the special handle ".root"). It is possible to directly connect a geometry node (such as the simple polygon above) to scene's root but it wouldn't be very useful. To place/instance geometry anywhere in the 3D world a transform node is used as in the code snippet below.
Code Block |
---|
language | cpp |
---|
theme | FadeToGrey |
---|
|
const char *k_instance1 = "translation";
nsi.Create( "translation"k_instance1, "transform" );
nsi.Connect( "translation"k_instance1, "", NSI_SCENE_ROOT, "objects" );
nsi.Connect( "simple polygon"k_poly_handle, "", "translation"k_instance1, "objects" );
/*
Matrices in NSI are in double format to allow for greater
range and precision.
*/
double trs[16] =
{
1., 0., 0., 0.,
0., 1., 0., 0.,
0., 0., 1., 0.,
0., 1., 0., 1. /* transalte 1 unit in Y */
};
nsi.SetAttribute( "translation"k_instance1,
NSI::DoubleMatrixArg("transformationmatrix", trs) ); |
Instacing Instancing is as simple as connecting geometry to different attributes (yes, instances of instances supported).instances are supported).
Code Block |
---|
language | cpp |
---|
theme | FadeToGrey |
---|
|
const char *k_instance2 = "more translation";
trs[13] += 1.0; /* translate in Y+ */
nsi.Create( k_instance2, "transform" );
nsi.Connect( k_poly_handle, "", k_instance2, "objects" );
nsi.Connect( k_instance2, "", NSI_SCENE_ROOT, "objects" );
/* We know have two instances of the same polygon in the scene */ |
Assigning Shaders
Shaders are created as any other nodes using the NSICreate
API call. They are not assigned directly on geometry but through an intermediate attributes nodes. Having an extra indirection allows for more flexible export as we will see in the following chapters.
...