The following is the final C program for tutorial # 3: /***************************************************************************** Example NuGraf Rendering Library Script File # 2 25 Spheres Illustrating Different Surface Characteristics. 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> /* Resolution of the image */ #define IMAGE_RESOLUTION 512 /* -------------------------------------------------- */ Nd_Void main() { char *home_directory = NULL; char *current_working_directory = NULL; char handle_name[30], handle_name2[30]; /* General variables */ Nd_Short row, colm, sphere_num, surf_num; /* Sphere primitive parameters */ Nd_Float sph_radius = 1; Nd_Int sph_u_subdiv = 12; /* parametric u & v subdivisions */ Nd_Int sph_v_subdiv = 12; Nd_Vector sphere_center; /* Spacing between the spheres */ Nd_Float x_inc = 2.5, y_inc = 2.5; /* Shading model coefficients */ Nd_Float sphere_ambient_coeff; Nd_Float sphere_diffuse_coeff; Nd_Float sphere_specular_coeff; /* Specular model coefficients */ Nd_Int phong_shininess; Nd_Float c3_coefficient; Nd_Float metal_coefficient = 1.0; Nd_Float index_of_refraction; /* Opacity values */ Nd_Float Opacity_FaceCoeff; Nd_Float Opacity_ReflectCoeff; Nd_Float Opacity_EdgeCoeff; Nd_Float Opacity_EdgeExp; /* Direction of the 'directional' line source (non-normalized vector) */ Nd_Vector light1_shinefrom = { 1.0, 1.0, 1.0 }; /* Camera variables */ Nd_Vector lookat, lookfrom; Nd_Float camera_window[] = { 12.5, 12.5 }; /* Rendering statistics buffer */ Nd_Float stats_buffer[Nc_NUM_STATISTIC_PARAMETERS]; /* Output driver variables */ Nd_Int dummy_int = -1L; /* Image Resolution */ Nd_Int image_res[2]; /* ----------->>>> Initialize the Library <<<<<------------- */ /* Initialize the library and the directory locations */ Ni_Initialize(home_directory, current_working_directory); /*>>>> Define Surfaces <<<<--------------- */ /* The bottom 5 spheres illustrate different ways of specifying */ /* the ambient, diffuse and specular surface colors. */ /* Default colors: white ambient, diffuse and specular colors */ Ni_Surface("surface 1", Nt_COLOR, Nt_COLORNAME, "white", Nt_CMDSEP, Nt_CMDEND); /* White diffuse & specular colors, red ambient color */ Ni_Surface("surface 2", Nt_COLOR, Nt_COLORNAME, "white", Nt_CMDSEP, Nt_AMBIENTCOLOR, Nt_COLOR, Nt_COLORNAME, "red", Nt_ENABLED, Nt_ON, Nt_CMDSEP, Nt_CMDEND); /* White ambient & diffuse colors, red specular color */ Ni_Surface("surface 3", Nt_COLOR, Nt_COLORNAME, "white", Nt_CMDSEP, /* !! NOTE: The "metal" coefficient must be > 0.0 for the */ /* specular colour to be seen. */ Nt_SPECULARCOLOR, Nt_COLOR, Nt_COLORNAME, "red", Nt_ENABLED, Nt_ON, Nt_CMDSEP, Nt_MODEL, Nt_PHONG, Nt_METAL, (Nd_Float *) &metal_coefficient, Nt_CMDSEP, Nt_CMDEND); /* Blue ambient, white diffuse, and red specular colors */ Ni_Copy(Nt_SURFACE, "surface 3", "surface 4", Nt_CMDEND); Ni_Surface("surface 4", Nt_AMBIENTCOLOR, Nt_COLOR, Nt_COLORNAME, "blue", Nt_ENABLED, Nt_ON, Nt_CMDSEP, Nt_CMDEND); /* Same as surface # 1 except that default color is yellow */ Ni_Surface("surface 5", Nt_COLOR, Nt_COLORNAME, "yellow", Nt_CMDSEP, Nt_CMDEND); /* The second row from the bottom varies the ambient, diffuse */ /* and specular surface coefficients. */ for (surf_num = 6; surf_num <= 10; ++surf_num) { sprintf(handle_name, "surface %d", surf_num); switch (surf_num) { case 6: /* All ambient */ sphere_ambient_coeff=0.5; sphere_diffuse_coeff=0.0; sphere_specular_coeff=0.0; break; case 7: /* All diffuse */ sphere_ambient_coeff=0.0; sphere_diffuse_coeff=1.0; sphere_specular_coeff=0.0; break; case 8: /* Diffuse + specular highlight */ sphere_ambient_coeff=0.0; sphere_diffuse_coeff=1.0; sphere_specular_coeff=0.7; break; case 9: /* Ambient + diffuse */ sphere_ambient_coeff=0.3; sphere_diffuse_coeff=0.5; sphere_specular_coeff=0.0; break; case 10: /* Default mixture of ambient, diffuse & specular */ sphere_ambient_coeff=0.3; sphere_diffuse_coeff=0.4; sphere_specular_coeff=0.7; break; } Ni_Surface(handle_name, Nt_SHADINGCOEFFS, Nt_AMBIENT, (Nd_Float *) &sphere_ambient_coeff, Nt_DIFFUSE, (Nd_Float *) &sphere_diffuse_coeff, Nt_SPECULAR, (Nd_Float *) &sphere_specular_coeff, Nt_CMDSEP, Nt_CMDEND); } /* The third row from the bottom uses the Phong specular shading */ /* model and varies the highlight size from left to right. */ for (surf_num=11; surf_num <=15; ++surf_num) { sprintf(handle_name, "surface %d", surf_num); /* Vary the Phong hightlight size from 4 to 64 */ phong_shininess=(surf_num-11)/4.0 * 60 + 4; Ni_Surface(handle_name, Nt_MODEL, Nt_PHONG, Nt_SHININESS, (Nd_Int *) &phong_shininess, Nt_CMDSEP, Nt_CMDEND); } /* The fourth row from the bottom uses the Blinn specular shading */ /* model and varies the highlight size from left to right. */ for (surf_num=16; surf_num <=20; ++surf_num) { sprintf(handle_name, "surface %d", surf_num); /* Vary the Blinn IoR parameter from 1 to 60 */ index_of_refraction=(surf_num-15)/5.0 * 60.0; /* Vary the Blinn C3 coefficient from 0.0 to 0.4 */ /* (this controls the size of the highlight) */ c3_coefficient=(surf_num-16)/4.0 * 0.4; Ni_Surface(handle_name, Nt_MODEL, Nt_BLINN, Nt_C3, (Nd_Float *) &c3_coefficient, Nt_INDEXREFRACT, (Nd_Float *) &index_of_refraction, Nt_CMDSEP, Nt_CMDEND); } /* The top row varies the surface opacity from left to right. */ for (surf_num=21; surf_num <=25; ++surf_num) { sprintf(handle_name, "surface %d", surf_num); Opacity_FaceCoeff=(surf_num-20)/8.0 + 0.3; Opacity_ReflectCoeff=1.0; /* Opaque highlights */ Opacity_EdgeCoeff=1.0; /* Opaque edges */ Opacity_EdgeExp=0.4; Ni_Surface(handle_name, Nt_COLOR, Nt_COLORNAME, "white", Nt_CMDSEP, Nt_OPACITY, Nt_FACECOEFF, (Nd_Float *) &Opacity_FaceCoeff, Nt_REFLECTCOEFF, (Nd_Float *) &Opacity_ReflectCoeff, Nt_EDGECOEFF, (Nd_Float *) &Opacity_EdgeCoeff, Nt_EDGEEXP, (Nd_Float *) &Opacity_EdgeExp, Nt_EDGE, Nt_ENABLED, Nt_ON, Nt_CMDSEP, Nt_CMDEND); } /*>>>> Define Sphere Objects <<<<------------- */ /* Create 25 spheres in a grid pattern */ sphere_num=1; for (row=1; row <=5; ++row) { for (colm=1; colm <=5; ++colm) { sprintf(handle_name, "sphere object %d", sphere_num); sphere_center[0]=(colm-1) * x_inc; sphere_center[1]=(row-1) * y_inc; sphere_center[2]=0.0; Ni_Object_Define(handle_name, Nt_CMDEND); Ni_Primitive(Nt_SPHERE, Nt_CENTERVERTEX, sphere_center, Nt_CMDSEP, Nt_RADIUS, (Nd_Float *) &sph_radius, Nt_CMDSEP, Nt_CMDEND); Ni_Object_End(Nt_CMDEND); sprintf(handle_name2, "sphere instance %d", sphere_num); Ni_Add_Instance(Nt_INSTANCE, handle_name2, Nt_OBJECT, handle_name, Nt_CMDEND); Ni_Attach(Nt_INSTANCE, handle_name2, Nt_INSTANCE, "world", Nt_CMDEND); /* Link surface to each object */ sprintf(handle_name2, "surface %d", sphere_num); Ni_Surfacelink(Nt_SURFACE, handle_name2, Nt_OBJECT, handle_name, Nt_CMDEND); ++sphere_num; } } /*>>>> Render <<<<------------------ */ /* Set the resolution of the image */ image_res[Na_X_VALUE]=IMAGE_RESOLUTION; image_res[Na_Y_VALUE]=IMAGE_RESOLUTION; Ni_Option( Nt_CULLING, Nt_BACK, Nt_CMDSEP, Nt_RESOLUTION, (Nd_Int *) image_res, Nt_CMDSEP, Nt_SHADING, Nt_SMOOTH, Nt_CMDSEP, Nt_SUBDIVISIONS, Nt_SPHERE, Nt_USUBDIV, (Nd_Int *) &sph_u_subdiv, Nt_VSUBDIV, (Nd_Int *) &sph_v_subdiv, Nt_CMDSEP, Nt_CMDEND); /* Add one directional light source to the scene */ Ni_Light("light1", Nt_MODEL, Nt_DIRECTION, Nt_CMDSEP, Nt_SHINEFROM, light1_shinefrom, Nt_CMDSEP, Nt_CMDEND); /* Background color scheme */ Ni_Background(Nt_COLORSCHEME, Nt_PATTERN, Nt_TYPE1, Nt_CMDEND); /* Modify the default camera to use an orthohraphic projection and */ /* view the spheres from the +ve Z axis towards the center of the spheres. */ lookfrom[0]=(2 * x_inc); lookfrom[1]=(2 * y_inc); lookfrom[2]=15.0; lookat[0]=(2 * x_inc); lookat[1]=(2 * y_inc); lookat[2]=0.0; Ni_Camera("default", Nt_ACTIVE, Nt_CMDSEP, /* Make it the active camera */ Nt_LOOKFROM, lookfrom, Nt_CMDSEP, Nt_LOOKAT, lookat, Nt_CMDSEP, Nt_PROJECTION, Nt_ORTHOGRAPHIC, Nt_CMDSEP, Nt_WINDOW, (Nd_Float *) camera_window, Nt_CMDSEP, Nt_CMDEND); /* Select the GIF file output driver. Send image to 'example2.gif' */ /* and quantize the image down from 24-bits to 256 colors. */ Ni_Output_Driver("driver#1", Nt_FILE, Nt_GIF, Nt_CMDSEP, Nt_FILENAME, "example2.gif", Nt_CMDSEP, Nt_OPTIONSTRING, "gif89a="off"," Nt_CMDSEP, Nt_IMAGEQUANTIZE, Nt_ENABLED, Nt_ON, /* The number of quantization colors is taken */ /* from the device's colormap length. */ Nt_NUMCOLORS, (Nd_Int *) &dummy_int, Nt_CMDSEP, Nt_CMDEND); /* Now render the scene to the GIF output file and return rendering */ /* statistics in the 'stats_buffer' array of floats. */ Ni_Render(Nt_STATISTICS, stats_buffer, Nt_CMDEND); /* Print out some of the rendering statistics */ printf("Total memory used (during rendering only)="%ldkb.\n"," (long) stats_buffer[Na_STATS_MEMORY_USED] / 1024L); printf("Number of polygons processed (after culling)="%ld.\n"," (long) stats_buffer[Na_STATS_NUM_POLYGONS]); Ni_Exit_Cleanup(); }
The following is the final C program for tutorial # 3:
/***************************************************************************** Example NuGraf Rendering Library Script File # 2 25 Spheres Illustrating Different Surface Characteristics. 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> /* Resolution of the image */ #define IMAGE_RESOLUTION 512 /* -------------------------------------------------- */ Nd_Void main() { char *home_directory = NULL; char *current_working_directory = NULL; char handle_name[30], handle_name2[30]; /* General variables */ Nd_Short row, colm, sphere_num, surf_num; /* Sphere primitive parameters */ Nd_Float sph_radius = 1; Nd_Int sph_u_subdiv = 12; /* parametric u & v subdivisions */ Nd_Int sph_v_subdiv = 12; Nd_Vector sphere_center; /* Spacing between the spheres */ Nd_Float x_inc = 2.5, y_inc = 2.5; /* Shading model coefficients */ Nd_Float sphere_ambient_coeff; Nd_Float sphere_diffuse_coeff; Nd_Float sphere_specular_coeff; /* Specular model coefficients */ Nd_Int phong_shininess; Nd_Float c3_coefficient; Nd_Float metal_coefficient = 1.0; Nd_Float index_of_refraction; /* Opacity values */ Nd_Float Opacity_FaceCoeff; Nd_Float Opacity_ReflectCoeff; Nd_Float Opacity_EdgeCoeff; Nd_Float Opacity_EdgeExp; /* Direction of the 'directional' line source (non-normalized vector) */ Nd_Vector light1_shinefrom = { 1.0, 1.0, 1.0 }; /* Camera variables */ Nd_Vector lookat, lookfrom; Nd_Float camera_window[] = { 12.5, 12.5 }; /* Rendering statistics buffer */ Nd_Float stats_buffer[Nc_NUM_STATISTIC_PARAMETERS]; /* Output driver variables */ Nd_Int dummy_int = -1L; /* Image Resolution */ Nd_Int image_res[2]; /* ----------->>>> Initialize the Library <<<<<------------- */ /* Initialize the library and the directory locations */ Ni_Initialize(home_directory, current_working_directory); /*>>>> Define Surfaces <<<<--------------- */ /* The bottom 5 spheres illustrate different ways of specifying */ /* the ambient, diffuse and specular surface colors. */ /* Default colors: white ambient, diffuse and specular colors */ Ni_Surface("surface 1", Nt_COLOR, Nt_COLORNAME, "white", Nt_CMDSEP, Nt_CMDEND); /* White diffuse & specular colors, red ambient color */ Ni_Surface("surface 2", Nt_COLOR, Nt_COLORNAME, "white", Nt_CMDSEP, Nt_AMBIENTCOLOR, Nt_COLOR, Nt_COLORNAME, "red", Nt_ENABLED, Nt_ON, Nt_CMDSEP, Nt_CMDEND); /* White ambient & diffuse colors, red specular color */ Ni_Surface("surface 3", Nt_COLOR, Nt_COLORNAME, "white", Nt_CMDSEP, /* !! NOTE: The "metal" coefficient must be > 0.0 for the */ /* specular colour to be seen. */ Nt_SPECULARCOLOR, Nt_COLOR, Nt_COLORNAME, "red", Nt_ENABLED, Nt_ON, Nt_CMDSEP, Nt_MODEL, Nt_PHONG, Nt_METAL, (Nd_Float *) &metal_coefficient, Nt_CMDSEP, Nt_CMDEND); /* Blue ambient, white diffuse, and red specular colors */ Ni_Copy(Nt_SURFACE, "surface 3", "surface 4", Nt_CMDEND); Ni_Surface("surface 4", Nt_AMBIENTCOLOR, Nt_COLOR, Nt_COLORNAME, "blue", Nt_ENABLED, Nt_ON, Nt_CMDSEP, Nt_CMDEND); /* Same as surface # 1 except that default color is yellow */ Ni_Surface("surface 5", Nt_COLOR, Nt_COLORNAME, "yellow", Nt_CMDSEP, Nt_CMDEND); /* The second row from the bottom varies the ambient, diffuse */ /* and specular surface coefficients. */ for (surf_num = 6; surf_num <= 10; ++surf_num) { sprintf(handle_name, "surface %d", surf_num); switch (surf_num) { case 6: /* All ambient */ sphere_ambient_coeff=0.5; sphere_diffuse_coeff=0.0; sphere_specular_coeff=0.0; break; case 7: /* All diffuse */ sphere_ambient_coeff=0.0; sphere_diffuse_coeff=1.0; sphere_specular_coeff=0.0; break; case 8: /* Diffuse + specular highlight */ sphere_ambient_coeff=0.0; sphere_diffuse_coeff=1.0; sphere_specular_coeff=0.7; break; case 9: /* Ambient + diffuse */ sphere_ambient_coeff=0.3; sphere_diffuse_coeff=0.5; sphere_specular_coeff=0.0; break; case 10: /* Default mixture of ambient, diffuse & specular */ sphere_ambient_coeff=0.3; sphere_diffuse_coeff=0.4; sphere_specular_coeff=0.7; break; } Ni_Surface(handle_name, Nt_SHADINGCOEFFS, Nt_AMBIENT, (Nd_Float *) &sphere_ambient_coeff, Nt_DIFFUSE, (Nd_Float *) &sphere_diffuse_coeff, Nt_SPECULAR, (Nd_Float *) &sphere_specular_coeff, Nt_CMDSEP, Nt_CMDEND); } /* The third row from the bottom uses the Phong specular shading */ /* model and varies the highlight size from left to right. */ for (surf_num=11; surf_num <=15; ++surf_num) { sprintf(handle_name, "surface %d", surf_num); /* Vary the Phong hightlight size from 4 to 64 */ phong_shininess=(surf_num-11)/4.0 * 60 + 4; Ni_Surface(handle_name, Nt_MODEL, Nt_PHONG, Nt_SHININESS, (Nd_Int *) &phong_shininess, Nt_CMDSEP, Nt_CMDEND); } /* The fourth row from the bottom uses the Blinn specular shading */ /* model and varies the highlight size from left to right. */ for (surf_num=16; surf_num <=20; ++surf_num) { sprintf(handle_name, "surface %d", surf_num); /* Vary the Blinn IoR parameter from 1 to 60 */ index_of_refraction=(surf_num-15)/5.0 * 60.0; /* Vary the Blinn C3 coefficient from 0.0 to 0.4 */ /* (this controls the size of the highlight) */ c3_coefficient=(surf_num-16)/4.0 * 0.4; Ni_Surface(handle_name, Nt_MODEL, Nt_BLINN, Nt_C3, (Nd_Float *) &c3_coefficient, Nt_INDEXREFRACT, (Nd_Float *) &index_of_refraction, Nt_CMDSEP, Nt_CMDEND); } /* The top row varies the surface opacity from left to right. */ for (surf_num=21; surf_num <=25; ++surf_num) { sprintf(handle_name, "surface %d", surf_num); Opacity_FaceCoeff=(surf_num-20)/8.0 + 0.3; Opacity_ReflectCoeff=1.0; /* Opaque highlights */ Opacity_EdgeCoeff=1.0; /* Opaque edges */ Opacity_EdgeExp=0.4; Ni_Surface(handle_name, Nt_COLOR, Nt_COLORNAME, "white", Nt_CMDSEP, Nt_OPACITY, Nt_FACECOEFF, (Nd_Float *) &Opacity_FaceCoeff, Nt_REFLECTCOEFF, (Nd_Float *) &Opacity_ReflectCoeff, Nt_EDGECOEFF, (Nd_Float *) &Opacity_EdgeCoeff, Nt_EDGEEXP, (Nd_Float *) &Opacity_EdgeExp, Nt_EDGE, Nt_ENABLED, Nt_ON, Nt_CMDSEP, Nt_CMDEND); } /*>>>> Define Sphere Objects <<<<------------- */ /* Create 25 spheres in a grid pattern */ sphere_num=1; for (row=1; row <=5; ++row) { for (colm=1; colm <=5; ++colm) { sprintf(handle_name, "sphere object %d", sphere_num); sphere_center[0]=(colm-1) * x_inc; sphere_center[1]=(row-1) * y_inc; sphere_center[2]=0.0; Ni_Object_Define(handle_name, Nt_CMDEND); Ni_Primitive(Nt_SPHERE, Nt_CENTERVERTEX, sphere_center, Nt_CMDSEP, Nt_RADIUS, (Nd_Float *) &sph_radius, Nt_CMDSEP, Nt_CMDEND); Ni_Object_End(Nt_CMDEND); sprintf(handle_name2, "sphere instance %d", sphere_num); Ni_Add_Instance(Nt_INSTANCE, handle_name2, Nt_OBJECT, handle_name, Nt_CMDEND); Ni_Attach(Nt_INSTANCE, handle_name2, Nt_INSTANCE, "world", Nt_CMDEND); /* Link surface to each object */ sprintf(handle_name2, "surface %d", sphere_num); Ni_Surfacelink(Nt_SURFACE, handle_name2, Nt_OBJECT, handle_name, Nt_CMDEND); ++sphere_num; } } /*>>>> Render <<<<------------------ */ /* Set the resolution of the image */ image_res[Na_X_VALUE]=IMAGE_RESOLUTION; image_res[Na_Y_VALUE]=IMAGE_RESOLUTION; Ni_Option( Nt_CULLING, Nt_BACK, Nt_CMDSEP, Nt_RESOLUTION, (Nd_Int *) image_res, Nt_CMDSEP, Nt_SHADING, Nt_SMOOTH, Nt_CMDSEP, Nt_SUBDIVISIONS, Nt_SPHERE, Nt_USUBDIV, (Nd_Int *) &sph_u_subdiv, Nt_VSUBDIV, (Nd_Int *) &sph_v_subdiv, Nt_CMDSEP, Nt_CMDEND); /* Add one directional light source to the scene */ Ni_Light("light1", Nt_MODEL, Nt_DIRECTION, Nt_CMDSEP, Nt_SHINEFROM, light1_shinefrom, Nt_CMDSEP, Nt_CMDEND); /* Background color scheme */ Ni_Background(Nt_COLORSCHEME, Nt_PATTERN, Nt_TYPE1, Nt_CMDEND); /* Modify the default camera to use an orthohraphic projection and */ /* view the spheres from the +ve Z axis towards the center of the spheres. */ lookfrom[0]=(2 * x_inc); lookfrom[1]=(2 * y_inc); lookfrom[2]=15.0; lookat[0]=(2 * x_inc); lookat[1]=(2 * y_inc); lookat[2]=0.0; Ni_Camera("default", Nt_ACTIVE, Nt_CMDSEP, /* Make it the active camera */ Nt_LOOKFROM, lookfrom, Nt_CMDSEP, Nt_LOOKAT, lookat, Nt_CMDSEP, Nt_PROJECTION, Nt_ORTHOGRAPHIC, Nt_CMDSEP, Nt_WINDOW, (Nd_Float *) camera_window, Nt_CMDSEP, Nt_CMDEND); /* Select the GIF file output driver. Send image to 'example2.gif' */ /* and quantize the image down from 24-bits to 256 colors. */ Ni_Output_Driver("driver#1", Nt_FILE, Nt_GIF, Nt_CMDSEP, Nt_FILENAME, "example2.gif", Nt_CMDSEP, Nt_OPTIONSTRING, "gif89a="off"," Nt_CMDSEP, Nt_IMAGEQUANTIZE, Nt_ENABLED, Nt_ON, /* The number of quantization colors is taken */ /* from the device's colormap length. */ Nt_NUMCOLORS, (Nd_Int *) &dummy_int, Nt_CMDSEP, Nt_CMDEND); /* Now render the scene to the GIF output file and return rendering */ /* statistics in the 'stats_buffer' array of floats. */ Ni_Render(Nt_STATISTICS, stats_buffer, Nt_CMDEND); /* Print out some of the rendering statistics */ printf("Total memory used (during rendering only)="%ldkb.\n"," (long) stats_buffer[Na_STATS_MEMORY_USED] / 1024L); printf("Number of polygons processed (after culling)="%ld.\n"," (long) stats_buffer[Na_STATS_NUM_POLYGONS]); Ni_Exit_Cleanup(); }