The following is the final C program created in this chapter:
/*****************************************************************************
Example NuGraf Rendering Library Script File # 1
See Explanation in the NuGraf Reference Manual - Tutorial Chapter.
This file can be compiled and run stand-alone.
Copyright (c) 1988, 2003 Okino Computer Graphics, Inc. All Rights Reserved.
*****************************************************************************/
#include "ni.h" /* This is the main NuGraf include file */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
/* -------------------------------------------------- */
Nd_Void
main() {
char *home_directory = NULL;
char *current_working_directory = NULL;
/* Sphere primitive parameters */
Nd_Float sph_radius = 0.5;
Nd_Int sph_u_subdiv = 12; /* parametric u & v subdivisions */
Nd_Int sph_v_subdiv = 12;
/* Box primitive parameters */
Nd_Float box_width = 1.0, box_height = 1.0, box_length = 1.0;
Nd_Vector box_center = { 0.0, 0.5, 0.0 };
/* Transformation variables */
Nd_Vector translate_amount;
Nd_Float angle;
/* Location of the 'point' line source */
Nd_Vector light1_shinefrom = { 10.0, 10.0, 10.0 };
/* Camera variables */
Nd_Vector lookat = { 0.0, 0.5, 0.0 };
Nd_Vector lookfrom = { 3.0, 2.0, 3.0 };
/* ----------- Initialize the Library ------------- */
/* Initialize the library and the directory locations */
Ni_Initialize(home_directory, current_working_directory);
/* Define Objects --------------------- */
/* Our simple scene will consist of a sphere centered about the origin */
Ni_Object_Define("sphere object", Nt_CMDEND);
Ni_Primitive(Nt_SPHERE, Nt_RADIUS, (Nd_Float *) &sph_radius,
Nt_CMDSEP, Nt_USUBDIV, (Nd_Int *) &sph_u_subdiv,
Nt_CMDSEP, Nt_VSUBDIV, (Nd_Int *) &sph_v_subdiv,
Nt_CMDSEP, Nt_CMDEND);
Ni_Object_End(Nt_CMDEND);
Ni_Object_Define("box obj", Nt_CMDEND);
/* Begin a new object */
Ni_Primitive(Nt_BOX, Nt_CENTERVERTEX, (Nd_Vector *) box_center,
Nt_CMDSEP, Nt_WIDTH, (Nd_Float *) &box_width,
Nt_CMDSEP, Nt_HEIGHT, (Nd_Float *) &box_height,
Nt_CMDSEP, Nt_LENGTH, (Nd_Float *) &box_length,
Nt_CMDSEP, Nt_CMDEND);
Ni_Object_End(Nt_CMDEND);
/* Close the current object */
/* Make Instances --------------- */
/* Create the empty instance nodes that will be used to connect the */
/* various parts of the tree together */
Ni_Add_Empty_Node(Nt_INSTANCE, "Empty Instance #1", Nt_CMDEND);
Ni_Add_Empty_Node(Nt_INSTANCE, "Empty Instance #2", Nt_CMDEND);
/* Create the branch of the tree by connecting the empty (null) */
/* instances together in a hierarchy. */
Ni_Attach(Nt_INSTANCE, "Empty Instance #1",
Nt_INSTANCE, "world", Nt_CMDEND);
Ni_Attach(Nt_INSTANCE, "Empty Instance #2",
Nt_INSTANCE, "Empty Instance #1", Nt_CMDEND);
/* These statements create instances of the geometry */
Ni_Add_Instance(Nt_INSTANCE, "sphere inst",
Nt_OBJECT, "sphere object", Nt_CMDEND);
Ni_Add_Instance(Nt_INSTANCE, "box inst",
Nt_OBJECT, "box obj", Nt_CMDEND);
/* Now attach the non-empty instances to the branches so that they */
/* become leaves of the tree. */
Ni_Attach(Nt_INSTANCE, "box inst",
Nt_INSTANCE, "Empty Instance #1", Nt_CMDEND);
Ni_Attach(Nt_INSTANCE, "sphere inst",
Nt_INSTANCE, "Empty Instance #2", Nt_CMDEND);
/* Apply Transformations ------------- */
/* Translate the sphere upwards in Y */
/* NOTE: The translation is applied to the parent empty instance of */
/* the sphere instance rather than the sphere instance itself. */
translate_amount[0]="0.0;"
/* X translation amount */
translate_amount[1]="1.5;"
/* Y="Box" height + Sphere radius */
translate_amount[2]="0.0;"
/* Z translation amount */
Ni_Transform(Nt_INSTANCE, "Empty Instance #2",
Nt_POSTMULTIPLY, Nt_TRANSLATE, translate_amount, Nt_CMDEND);
/* Rotate the box instance 10 degrees clock-wise about Z axis */
/* NOTE: the rotation is performed on the parent empty instance of */
/* the box instance so that the transformation gets applied both to */
/* the box and to the sphere. */
angle="10.0;"
/* Angle in degrees */
Ni_Transform(Nt_INSTANCE, "Empty Instance #1",
Nt_POSTMULTIPLY, Nt_ROTATE, Nt_ZAXIS,
(Nd_Float *) &angle, Nt_CMDEND);
/* Render -------------------- */
/* Set up the runtime options */
Ni_Option( Nt_SHADING, Nt_SMOOTH,
Nt_CMDSEP, Nt_RENDERLEVEL, Nt_ZSCANLINE,
Nt_CMDSEP, Nt_CMDEND);
/* Add one point light source to the scene */
Ni_Light("light1", Nt_MODEL, Nt_POINT,
Nt_CMDSEP, Nt_SHINEFROM, light1_shinefrom,
Nt_CMDSEP, Nt_CMDEND);
/* Create a custom camera looking from the positive Z axis */
/* towards the origin. */
Ni_Camera("camera#1", Nt_ACTIVE, Nt_CMDSEP, /* Make it the active camera */
Nt_LOOKFROM, lookfrom, Nt_CMDSEP,
Nt_LOOKAT, lookat, Nt_CMDSEP,
Nt_FORMMAPPINGTYPE, Nt_TYPE4, Nt_CMDSEP, Nt_CMDEND);
/* Select the TIFF file output driver. Send image to 'example.tif' */
Ni_Output_Driver("driver#1", Nt_FILE, Nt_TIFF, Nt_CMDSEP,
Nt_FILENAME, "example1.tif", Nt_CMDEND);
/* Now render the scene to the TIFF output file */
Ni_Render(Nt_CMDEND);
Ni_Exit_Cleanup();
}