The following is the final C program for tutorial # 4: /***************************************************************************** Example NuGraf Rendering Library Script File # 3. 16 Spheres Illustrating Different Texture Mapping Methods. 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 i, row, colm, sphere_num; Nd_Vector vec; Nd_Int Oversampling_Values[2] = { 1, 4 }; /* 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; /* Texture mapping parameters */ Nd_Float checker_u_scale = 1.0 / 4.0; Nd_Float checker_v_scale = 1.0 / 4.0; Nd_Float checker_u_offset = 0.0; Nd_Float checker_v_offset = 0.0; Nd_Float bump_map_multiplier = -0.5; Nd_Float color_mixing_value = 0.6; /* Procedural texture parameters */ Nd_Float procedural_bump_scale = 2.0; Nd_Float procedural_noise3d_turb_scale = 1.0; Nd_Float procedural_noise3d_noise_scale = 1.0; Nd_Float procedural_windy_wind_scale = 1.0; Nd_Float procedural_windy_bump_scale = 2.0; Nd_Float starting_frequency = 1.0; Nd_Int num_octaves_of_noise = 6; /* Spherical environment mapping parameters */ Nd_Float cropwindow_params[4] = { 0.1, 0.0, 0.8, 0.7 }; Nd_Float sph_env_reflectcoeff = 1.0; Nd_Float sph_env_rot_angle = 45.0; Nd_Float ambient_coeff = 0.0; Nd_Float diffuse_coeff = 0.0; Nd_Float specular_coeff = 1.0; /* Shading coefficients for the opacity mapped sphere */ Nd_Float ambient_coeff2 = 0.5; Nd_Float diffuse_coeff2 = 0.8; Nd_Float specular_coeff2 = 0.7; /* Shading coefficients for the procedurally texture mapped spheres */ Nd_Float ambient_coeff3 = 0.3; Nd_Float diffuse_coeff3 = 0.5; /* Cubical environment mapping parameters */ Nd_Float cubical_env_map_reflect_coeff = 1.0; /* Background texture mapping parameters */ Nd_Float background_u_scale = 1.0 / 1.0; Nd_Float background_v_scale = 1.0 / 2.0; Nd_Float background_u_offset = 0.0; Nd_Float background_v_offset = 0.0; /* Orthographic texture parameterization variables */ Nd_Vector txtrparam_ortho_origin = { -1.0, -1.0, 0.0 }; Nd_Vector txtrparam_ortho_upoint = { 1.0, -1.0, 0.0 }; Nd_Vector txtrparam_ortho_vpoint = { -1.0, 1.0, 0.0 }; /* Cylindrical texture parameterization variables */ Nd_Vector txtrparam_cyl_origin = { 0.0, -1.0, 0.0 }; Nd_Vector txtrparam_cyl_upoint = { 1.0, -1.0, 0.0 }; Nd_Vector txtrparam_cyl_vpoint = { 0.0, 1.0, 0.0 }; /* Direction of the 'directional' line source (non-normalized vector) */ Nd_Vector light1_shinefrom = { 1.0, 2.0, 0.5 }; /* Camera variables */ Nd_Vector lookat, lookfrom; Nd_Float camera_window[] = { 10.0, 10.0 }; /* 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 Textures <<<<----------------- */ /* 2D texture image for general texture mapping */ Ni_Texture("checker bitmap texture", Nt_IMAGE, Nt_TEXTUREIMAGE, Nt_TIFF, "check256.tif", Nt_CMDSEP, Nt_FILTER, Nt_MIPMAP, Nt_CMDSEP, Nt_CMDEND); /* 2D texture image for the chrome spherical environment map */ Ni_Texture("reflect map texture", Nt_IMAGE, Nt_TEXTUREIMAGE, Nt_TIFF, "fireplac.tif", Nt_CMDSEP, Nt_CROPWINDOW, (Nd_Float *) cropwindow_params, Nt_CMDSEP, Nt_FILTER, Nt_MIPMAP, Nt_CMDSEP, Nt_CMDEND); /* 2D texture image to be tiled across the background */ Ni_Texture("background texture", Nt_IMAGE, Nt_TEXTUREIMAGE, Nt_TIFF, "masonry.tif", Nt_CMDSEP, /* Nt_FILTER, Nt_POINTSAMPLE, Nt_CMDSEP, */ Nt_FILTER, Nt_MIPMAP, Nt_CMDSEP, Nt_WRAPU, Nt_ENABLED, Nt_ON, Nt_CMDSEP, Nt_WRAPV, Nt_ENABLED, Nt_ON, Nt_CMDSEP, Nt_CMDEND); /* 3D procedural texture # 1 Black Marble */ Ni_Texture("procedural texture 1", Nt_PROCEDURAL, Nt_NAME, "marble2", Nt_CMDSEP, Nt_ENABLED, Nt_ON, Nt_CMDSEP, Nt_FREQUENCY, (Nd_Float *) &starting_frequency, Nt_CMDSEP, Nt_NUMOCTAVES, (Nd_Int *) &num_octaves_of_noise, Nt_CMDSEP, Nt_CMDEND); /* 3D procedural texture # 2 Bump map */ Ni_Texture("procedural texture 2", Nt_PROCEDURAL, Nt_NAME, "bump", Nt_CMDSEP, Nt_ENABLED, Nt_ON, Nt_CMDSEP, Nt_PARAM1, (Nd_Float *) &procedural_bump_scale, Nt_CMDSEP, Nt_CMDEND); /* Scale the bump texture smaller */ vec[0]=vec[1] / 3.0; Ni_Transform(Nt_TEXTURE, "procedural texture 2", Nt_POSTMULTIPLY, Nt_SCALE, (Nd_Vector *) vec, Nt_CMDEND); /* 3D procedural texture # 3 Hlssweep */ Ni_Texture("procedural texture 3", Nt_PROCEDURAL, Nt_NAME, "hlssweep", Nt_CMDSEP, Nt_ENABLED, Nt_ON, Nt_CMDSEP, Nt_CMDEND); /* 3D procedural texture # 4 Chaotic3d */ Ni_Texture("procedural texture 4", Nt_PROCEDURAL, Nt_NAME, "chaotic3d", Nt_CMDSEP, Nt_ENABLED, Nt_ON, Nt_CMDSEP, Nt_COLOR1, Nt_COLORNAME, "white", Nt_CMDSEP, Nt_FREQUENCY, (Nd_Float *) &starting_frequency, Nt_CMDSEP, Nt_NUMOCTAVES, (Nd_Int *) &num_octaves_of_noise, Nt_CMDSEP, Nt_CMDEND); /* 3D procedural texture # 5 3D Checkerboard */ Ni_Texture("procedural texture 5", Nt_PROCEDURAL, Nt_NAME, "checker", Nt_CMDSEP, Nt_ENABLED, Nt_ON, Nt_CMDSEP, Nt_COLOR1, Nt_COLORNAME, "white", Nt_CMDSEP, Nt_COLOR2, Nt_COLORNAME, "black", Nt_CMDSEP, Nt_CMDEND); /* Scale the 3D chekerboard texture smaller */ vec[0]=vec[1] / 3.0; Ni_Transform(Nt_TEXTURE, "procedural texture 5", Nt_POSTMULTIPLY, Nt_SCALE, (Nd_Vector *) vec, Nt_CMDEND); /* 3D procedural texture # 6 Windy bump map */ Ni_Texture("procedural texture 6", Nt_PROCEDURAL, Nt_NAME, "windy", Nt_CMDSEP, Nt_ENABLED, Nt_ON, Nt_CMDSEP, Nt_PARAM1, (Nd_Float *) &procedural_windy_wind_scale, Nt_CMDSEP, Nt_PARAM2, (Nd_Float *) &procedural_windy_bump_scale, Nt_CMDSEP, Nt_FREQUENCY, (Nd_Float *) &starting_frequency, Nt_CMDSEP, Nt_NUMOCTAVES, (Nd_Int *) &num_octaves_of_noise, Nt_CMDSEP, Nt_CMDEND); /* 3D procedural texture # 7 Noise3D */ Ni_Texture("procedural texture 7", Nt_PROCEDURAL, Nt_NAME, "noise3d", Nt_CMDSEP, Nt_ENABLED, Nt_ON, Nt_CMDSEP, Nt_PARAM1, (Nd_Float *) &procedural_noise3d_turb_scale, Nt_CMDSEP, Nt_PARAM2, (Nd_Float *) &procedural_noise3d_noise_scale, Nt_CMDSEP, Nt_COLOR1, Nt_COLORNAME, "white", Nt_CMDSEP, Nt_FREQUENCY, (Nd_Float *) &starting_frequency, Nt_CMDSEP, Nt_NUMOCTAVES, (Nd_Int *) &num_octaves_of_noise, Nt_CMDSEP, Nt_CMDEND); /* Scale the Noise3D texture smaller */ vec[0]=vec[1] / 1.5; Ni_Transform(Nt_TEXTURE, "procedural texture 7", Nt_POSTMULTIPLY, Nt_SCALE, (Nd_Vector *) vec, Nt_CMDEND); /*>>>> Define Surfaces <<<<--------------- */ /* Surface definition used to tile a texture across the background */ Ni_Surface("background surface", Nt_TEXTURE, "layer 1", Nt_TEXTURELINK, "background texture", Nt_USCALE, (Nd_Float *) &background_u_scale, Nt_VSCALE, (Nd_Float *) &background_v_scale, Nt_UOFFSET, (Nd_Float *) &background_u_offset, Nt_VOFFSET, (Nd_Float *) &background_v_offset, Nt_CMDSEP, Nt_CMDEND); /* Surface 1: Checkerboard bitmap texture */ Ni_Surface("surface 1", Nt_TEXTURE, "layer 1", Nt_TEXTURELINK, "checker bitmap texture", Nt_USCALE, (Nd_Float *) &checker_u_scale, Nt_VSCALE, (Nd_Float *) &checker_v_scale, Nt_UOFFSET, (Nd_Float *) &checker_u_offset, Nt_VOFFSET, (Nd_Float *) &checker_v_offset, Nt_MODULATE, Nt_COLOR, Nt_ENABLED, Nt_ON, Nt_CMDSEP, Nt_CMDEND); /* Surface 2: Bump mapped surface using checkerboard bitmap texture */ Ni_Surface("surface 2", Nt_TEXTURE, "layer 1", Nt_TEXTURELINK, "checker bitmap texture", Nt_USCALE, (Nd_Float *) &checker_u_scale, Nt_VSCALE, (Nd_Float *) &checker_v_scale, Nt_MODULATE, Nt_COLOR, Nt_ENABLED, Nt_OFF, Nt_MODULATE, Nt_BUMP, Nt_ENABLED, Nt_ON, Nt_MULTIPLIER, Nt_BUMPU, (Nd_Float *) &bump_map_multiplier, Nt_MULTIPLIER, Nt_BUMPV, (Nd_Float *) &bump_map_multiplier, Nt_CMDSEP, Nt_CMDEND); /* Surface 3: Color + bump mapped surface using checkerboard bitmap texture */ Ni_Surface("surface 3", Nt_TEXTURE, "layer 1", Nt_TEXTURELINK, "checker bitmap texture", Nt_USCALE, (Nd_Float *) &checker_u_scale, Nt_VSCALE, (Nd_Float *) &checker_v_scale, Nt_MODULATE, Nt_COLOR, Nt_ENABLED, Nt_ON, Nt_MODULATE, Nt_BUMP, Nt_ENABLED, Nt_ON, Nt_MULTIPLIER, Nt_BUMPU, (Nd_Float *) &bump_map_multiplier, Nt_MULTIPLIER, Nt_BUMPV, (Nd_Float *) &bump_map_multiplier, Nt_CMDSEP, Nt_CMDEND); /* Surface 4: Opacity mapped surface using checkerboard bitmap texture */ Ni_Surface("surface 4", Nt_TEXTURE, "layer 1", Nt_TEXTURELINK, "checker bitmap texture", Nt_USCALE, (Nd_Float *) &checker_u_scale, Nt_VSCALE, (Nd_Float *) &checker_v_scale, Nt_MODULATE, Nt_COLOR, Nt_ENABLED, Nt_OFF, Nt_MODULATE, Nt_FACEOPACITY, Nt_ENABLED, Nt_ON, Nt_MODULATE, Nt_REFLECTOPACITY, Nt_ENABLED, Nt_ON, Nt_CMDSEP, /* Turn up the ambient and diffuse shading components so that */ /* the sphere will be more visible against the background. */ Nt_SHADINGCOEFFS, Nt_AMBIENT, (Nd_Float *) &ambient_coeff2, Nt_DIFFUSE, (Nd_Float *) &diffuse_coeff2, Nt_SPECULAR, (Nd_Float *) &specular_coeff2, Nt_CMDSEP, Nt_CMDEND); /* Surface 5: spherical environment map (chrome using fireplace texture) */ Ni_Surface("surface 5", /* Turn off ambient and diffuse shading so that we can simulate chrome */ Nt_SHADINGCOEFFS, Nt_AMBIENT, (Nd_Float *) &ambient_coeff, Nt_DIFFUSE, (Nd_Float *) &diffuse_coeff, Nt_SPECULAR, (Nd_Float *) &specular_coeff, Nt_CMDSEP, /* Associate a texture map as a spherical environmap map */ Nt_SPHENVMAP, Nt_ENABLED, Nt_ON, Nt_TEXTURELINK, "reflect map texture", Nt_REFLECTCOEFF, (Nd_Float *) &sph_env_reflectcoeff, Nt_ROTATE, Nt_YAXIS, (Nd_Float *) &sph_env_rot_angle, Nt_CMDSEP, Nt_CMDEND); /* Surface 6: Cubical environment mapping using the same texture for all 6 sides */ Ni_Surface("surface 6", /* Turn off ambient and diffuse shading so that we can simulate chrome */ Nt_SHADINGCOEFFS, Nt_AMBIENT, (Nd_Float *) &ambient_coeff, Nt_DIFFUSE, (Nd_Float *) &diffuse_coeff, Nt_SPECULAR, (Nd_Float *) &specular_coeff, Nt_CMDEND); Ni_Surface("surface 6", Nt_CUBICALENVMAP, Nt_ENABLED, Nt_ON, Nt_REFLECTCOEFF, (Nd_Float *) &cubical_env_map_reflect_coeff, Nt_TEXTUREIMAGE, Nt_FRONT, Nt_TIFF, "masonry.tif", Nt_TEXTUREIMAGE, Nt_BACK, Nt_TIFF, "masonry.tif", Nt_TEXTUREIMAGE, Nt_LEFT, Nt_TIFF, "fireplac.tif", Nt_TEXTUREIMAGE, Nt_RIGHT, Nt_TIFF, "fireplac.tif", Nt_TEXTUREIMAGE, Nt_TOP, Nt_TIFF, "check256.tif", Nt_TEXTUREIMAGE, Nt_BOTTOM, Nt_TIFF, "check256.tif", Nt_CMDEND); /* Surface 7: Example of orthographic texture parameterization */ Ni_Copy(Nt_SURFACE, "surface 1", "surface 7", Nt_CMDEND); /* Surface 8: Example of cylindrical texture parameterization */ Ni_Copy(Nt_SURFACE, "surface 1", "surface 8", Nt_CMDEND); /* Surfaces 9-15: 3D procedural texture maps */ for (i="0;" i < 7; ++i) { sprintf(handle_name, "surface %d", i+9); sprintf(handle_name2, "procedural texture %d", i+1); Ni_Surface(handle_name, /* Increase the ambient and diffuse shading coefficients a bit */ Nt_SHADINGCOEFFS, Nt_AMBIENT, (Nd_Float *) &ambient_coeff3, Nt_DIFFUSE, (Nd_Float *) &diffuse_coeff3, Nt_CMDSEP, Nt_TEXTURE, "layer 1", Nt_ENABLED, Nt_ON, Nt_TEXTURELINK, handle_name2, Nt_CMDSEP, Nt_CMDEND); } /* Surface 16. Two layered textures: Layer 1="2D" bitmap checkerboard */ /* texture, Layer 2="3D" procedural texture. */ Ni_Surface("surface 16", Nt_TEXTURE, "layer 1", Nt_ENABLED, Nt_ON, Nt_TEXTURELINK, "checker bitmap texture", Nt_USCALE, (Nd_Float *) &checker_u_scale, Nt_VSCALE, (Nd_Float *) &checker_v_scale, Nt_CMDSEP, Nt_TEXTURE, "layer 2", Nt_ENABLED, Nt_ON, Nt_TEXTURELINK, "procedural texture 1", /* This sets the color mixing with the previous layer */ Nt_COLORBLEND, Nt_DIFFUSE, (Nd_Float *) &color_mixing_value, Nt_CMDSEP, Nt_CMDSEP, Nt_CMDEND); /*>>>> Define Sphere Objects <<<<------------- */ /* Create a single sphere object centered at the origin */ Ni_Object_Define("sphere object", Nt_CMDEND); Ni_Primitive(Nt_SPHERE, Nt_RADIUS, (Nd_Float *) &sph_radius, Nt_CMDSEP, Nt_CMDEND); Ni_Object_End(Nt_CMDEND); /* Create 16 spheres in a grid pattern */ sphere_num=1; for (row=1; row <=4; ++row) { for (colm=1; colm <=4; ++colm) { sprintf(handle_name2, "sphere instance %d", sphere_num); Ni_Add_Instance(Nt_INSTANCE, handle_name2, Nt_OBJECT, "sphere object", Nt_CMDEND); Ni_Attach(Nt_INSTANCE, handle_name2, Nt_INSTANCE, "world", Nt_CMDEND); /* Move the sphere instance into place */ sphere_center[0]=(colm-1) * x_inc; sphere_center[1]=(row-1) * y_inc; sphere_center[2]=0.0; Ni_Transform(Nt_INSTANCE, handle_name2, Nt_POSTMULTIPLY, Nt_TRANSLATE, (Nd_Vector *) sphere_center, Nt_CMDEND); /* Link surface to each instance */ sprintf(handle_name, "surface %d", sphere_num); Ni_Surfacelink(Nt_SURFACE, handle_name, Nt_INSTANCE, handle_name2, Nt_CMDEND); ++sphere_num; } } /*>>>> Texture Parameterizations <<<<----------- */ /* This statement creates new texture coordinates for the seventh sphere */ /* by overriding the sphere's built-in u/v texture coordinates with new */ /* coordinates created by a front-on orthographic projection method. */ Ni_TxtrParam(Nt_INSTANCE, "sphere instance 7", Nt_MODEL, Nt_ORTHOGRAPHIC, Nt_CMDSEP, Nt_ENABLED, Nt_ON, Nt_CMDSEP, Nt_COORDSYSTEM, Nt_PRIMITIVE, Nt_CMDSEP, Nt_ORIGIN, (Nd_Vector *) txtrparam_ortho_origin, Nt_CMDSEP, Nt_UPOINT, (Nd_Vector *) txtrparam_ortho_upoint, Nt_CMDSEP, Nt_VPOINT, (Nd_Vector *) txtrparam_ortho_vpoint, Nt_CMDSEP, Nt_CMDEND); /* This statement creates new texture coordinates for the eighth sphere */ /* by overriding the sphere's built-in u/v texture coordinates with new */ /* coordinates created by a cylindrical projection method (aligned with Y axis). */ Ni_TxtrParam(Nt_INSTANCE, "sphere instance 8", Nt_MODEL, Nt_CYLINDRICAL, Nt_CMDSEP, Nt_ENABLED, Nt_ON, Nt_CMDSEP, Nt_COORDSYSTEM, Nt_PRIMITIVE, Nt_CMDSEP, Nt_ORIGIN, (Nd_Vector *) txtrparam_cyl_origin, Nt_CMDSEP, Nt_UPOINT, (Nd_Vector *) txtrparam_cyl_upoint, Nt_CMDSEP, Nt_VPOINT, (Nd_Vector *) txtrparam_cyl_vpoint, Nt_CMDSEP, Nt_CMDEND); /*>>>> 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_ANTIALIAS, Nt_ENABLED, Nt_OFF, Nt_OVERSAMPLE, (Nd_Int *) Oversampling_Values, 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_SURFACELINK, "background surface", 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]=(1.5 * x_inc); lookfrom[1]=(1.5 * y_inc); lookfrom[2]=15.0; lookat[0]=(1.5 * x_inc); lookat[1]=(1.5 * 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 TARGA file output driver. */ Ni_Output_Driver("driver#1", Nt_FILE, Nt_TARGA, Nt_CMDSEP, Nt_FILENAME, "example3.tga", Nt_CMDSEP, Nt_CMDEND); /* Now render the scene to the TARGA output file */ Ni_Render(Nt_CMDEND); Ni_Exit_Cleanup(); }
The following is the final C program for tutorial # 4:
/***************************************************************************** Example NuGraf Rendering Library Script File # 3. 16 Spheres Illustrating Different Texture Mapping Methods. 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 i, row, colm, sphere_num; Nd_Vector vec; Nd_Int Oversampling_Values[2] = { 1, 4 }; /* 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; /* Texture mapping parameters */ Nd_Float checker_u_scale = 1.0 / 4.0; Nd_Float checker_v_scale = 1.0 / 4.0; Nd_Float checker_u_offset = 0.0; Nd_Float checker_v_offset = 0.0; Nd_Float bump_map_multiplier = -0.5; Nd_Float color_mixing_value = 0.6; /* Procedural texture parameters */ Nd_Float procedural_bump_scale = 2.0; Nd_Float procedural_noise3d_turb_scale = 1.0; Nd_Float procedural_noise3d_noise_scale = 1.0; Nd_Float procedural_windy_wind_scale = 1.0; Nd_Float procedural_windy_bump_scale = 2.0; Nd_Float starting_frequency = 1.0; Nd_Int num_octaves_of_noise = 6; /* Spherical environment mapping parameters */ Nd_Float cropwindow_params[4] = { 0.1, 0.0, 0.8, 0.7 }; Nd_Float sph_env_reflectcoeff = 1.0; Nd_Float sph_env_rot_angle = 45.0; Nd_Float ambient_coeff = 0.0; Nd_Float diffuse_coeff = 0.0; Nd_Float specular_coeff = 1.0; /* Shading coefficients for the opacity mapped sphere */ Nd_Float ambient_coeff2 = 0.5; Nd_Float diffuse_coeff2 = 0.8; Nd_Float specular_coeff2 = 0.7; /* Shading coefficients for the procedurally texture mapped spheres */ Nd_Float ambient_coeff3 = 0.3; Nd_Float diffuse_coeff3 = 0.5; /* Cubical environment mapping parameters */ Nd_Float cubical_env_map_reflect_coeff = 1.0; /* Background texture mapping parameters */ Nd_Float background_u_scale = 1.0 / 1.0; Nd_Float background_v_scale = 1.0 / 2.0; Nd_Float background_u_offset = 0.0; Nd_Float background_v_offset = 0.0; /* Orthographic texture parameterization variables */ Nd_Vector txtrparam_ortho_origin = { -1.0, -1.0, 0.0 }; Nd_Vector txtrparam_ortho_upoint = { 1.0, -1.0, 0.0 }; Nd_Vector txtrparam_ortho_vpoint = { -1.0, 1.0, 0.0 }; /* Cylindrical texture parameterization variables */ Nd_Vector txtrparam_cyl_origin = { 0.0, -1.0, 0.0 }; Nd_Vector txtrparam_cyl_upoint = { 1.0, -1.0, 0.0 }; Nd_Vector txtrparam_cyl_vpoint = { 0.0, 1.0, 0.0 }; /* Direction of the 'directional' line source (non-normalized vector) */ Nd_Vector light1_shinefrom = { 1.0, 2.0, 0.5 }; /* Camera variables */ Nd_Vector lookat, lookfrom; Nd_Float camera_window[] = { 10.0, 10.0 }; /* 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 Textures <<<<----------------- */ /* 2D texture image for general texture mapping */ Ni_Texture("checker bitmap texture", Nt_IMAGE, Nt_TEXTUREIMAGE, Nt_TIFF, "check256.tif", Nt_CMDSEP, Nt_FILTER, Nt_MIPMAP, Nt_CMDSEP, Nt_CMDEND); /* 2D texture image for the chrome spherical environment map */ Ni_Texture("reflect map texture", Nt_IMAGE, Nt_TEXTUREIMAGE, Nt_TIFF, "fireplac.tif", Nt_CMDSEP, Nt_CROPWINDOW, (Nd_Float *) cropwindow_params, Nt_CMDSEP, Nt_FILTER, Nt_MIPMAP, Nt_CMDSEP, Nt_CMDEND); /* 2D texture image to be tiled across the background */ Ni_Texture("background texture", Nt_IMAGE, Nt_TEXTUREIMAGE, Nt_TIFF, "masonry.tif", Nt_CMDSEP, /* Nt_FILTER, Nt_POINTSAMPLE, Nt_CMDSEP, */ Nt_FILTER, Nt_MIPMAP, Nt_CMDSEP, Nt_WRAPU, Nt_ENABLED, Nt_ON, Nt_CMDSEP, Nt_WRAPV, Nt_ENABLED, Nt_ON, Nt_CMDSEP, Nt_CMDEND); /* 3D procedural texture # 1 Black Marble */ Ni_Texture("procedural texture 1", Nt_PROCEDURAL, Nt_NAME, "marble2", Nt_CMDSEP, Nt_ENABLED, Nt_ON, Nt_CMDSEP, Nt_FREQUENCY, (Nd_Float *) &starting_frequency, Nt_CMDSEP, Nt_NUMOCTAVES, (Nd_Int *) &num_octaves_of_noise, Nt_CMDSEP, Nt_CMDEND); /* 3D procedural texture # 2 Bump map */ Ni_Texture("procedural texture 2", Nt_PROCEDURAL, Nt_NAME, "bump", Nt_CMDSEP, Nt_ENABLED, Nt_ON, Nt_CMDSEP, Nt_PARAM1, (Nd_Float *) &procedural_bump_scale, Nt_CMDSEP, Nt_CMDEND); /* Scale the bump texture smaller */ vec[0]=vec[1] / 3.0; Ni_Transform(Nt_TEXTURE, "procedural texture 2", Nt_POSTMULTIPLY, Nt_SCALE, (Nd_Vector *) vec, Nt_CMDEND); /* 3D procedural texture # 3 Hlssweep */ Ni_Texture("procedural texture 3", Nt_PROCEDURAL, Nt_NAME, "hlssweep", Nt_CMDSEP, Nt_ENABLED, Nt_ON, Nt_CMDSEP, Nt_CMDEND); /* 3D procedural texture # 4 Chaotic3d */ Ni_Texture("procedural texture 4", Nt_PROCEDURAL, Nt_NAME, "chaotic3d", Nt_CMDSEP, Nt_ENABLED, Nt_ON, Nt_CMDSEP, Nt_COLOR1, Nt_COLORNAME, "white", Nt_CMDSEP, Nt_FREQUENCY, (Nd_Float *) &starting_frequency, Nt_CMDSEP, Nt_NUMOCTAVES, (Nd_Int *) &num_octaves_of_noise, Nt_CMDSEP, Nt_CMDEND); /* 3D procedural texture # 5 3D Checkerboard */ Ni_Texture("procedural texture 5", Nt_PROCEDURAL, Nt_NAME, "checker", Nt_CMDSEP, Nt_ENABLED, Nt_ON, Nt_CMDSEP, Nt_COLOR1, Nt_COLORNAME, "white", Nt_CMDSEP, Nt_COLOR2, Nt_COLORNAME, "black", Nt_CMDSEP, Nt_CMDEND); /* Scale the 3D chekerboard texture smaller */ vec[0]=vec[1] / 3.0; Ni_Transform(Nt_TEXTURE, "procedural texture 5", Nt_POSTMULTIPLY, Nt_SCALE, (Nd_Vector *) vec, Nt_CMDEND); /* 3D procedural texture # 6 Windy bump map */ Ni_Texture("procedural texture 6", Nt_PROCEDURAL, Nt_NAME, "windy", Nt_CMDSEP, Nt_ENABLED, Nt_ON, Nt_CMDSEP, Nt_PARAM1, (Nd_Float *) &procedural_windy_wind_scale, Nt_CMDSEP, Nt_PARAM2, (Nd_Float *) &procedural_windy_bump_scale, Nt_CMDSEP, Nt_FREQUENCY, (Nd_Float *) &starting_frequency, Nt_CMDSEP, Nt_NUMOCTAVES, (Nd_Int *) &num_octaves_of_noise, Nt_CMDSEP, Nt_CMDEND); /* 3D procedural texture # 7 Noise3D */ Ni_Texture("procedural texture 7", Nt_PROCEDURAL, Nt_NAME, "noise3d", Nt_CMDSEP, Nt_ENABLED, Nt_ON, Nt_CMDSEP, Nt_PARAM1, (Nd_Float *) &procedural_noise3d_turb_scale, Nt_CMDSEP, Nt_PARAM2, (Nd_Float *) &procedural_noise3d_noise_scale, Nt_CMDSEP, Nt_COLOR1, Nt_COLORNAME, "white", Nt_CMDSEP, Nt_FREQUENCY, (Nd_Float *) &starting_frequency, Nt_CMDSEP, Nt_NUMOCTAVES, (Nd_Int *) &num_octaves_of_noise, Nt_CMDSEP, Nt_CMDEND); /* Scale the Noise3D texture smaller */ vec[0]=vec[1] / 1.5; Ni_Transform(Nt_TEXTURE, "procedural texture 7", Nt_POSTMULTIPLY, Nt_SCALE, (Nd_Vector *) vec, Nt_CMDEND); /*>>>> Define Surfaces <<<<--------------- */ /* Surface definition used to tile a texture across the background */ Ni_Surface("background surface", Nt_TEXTURE, "layer 1", Nt_TEXTURELINK, "background texture", Nt_USCALE, (Nd_Float *) &background_u_scale, Nt_VSCALE, (Nd_Float *) &background_v_scale, Nt_UOFFSET, (Nd_Float *) &background_u_offset, Nt_VOFFSET, (Nd_Float *) &background_v_offset, Nt_CMDSEP, Nt_CMDEND); /* Surface 1: Checkerboard bitmap texture */ Ni_Surface("surface 1", Nt_TEXTURE, "layer 1", Nt_TEXTURELINK, "checker bitmap texture", Nt_USCALE, (Nd_Float *) &checker_u_scale, Nt_VSCALE, (Nd_Float *) &checker_v_scale, Nt_UOFFSET, (Nd_Float *) &checker_u_offset, Nt_VOFFSET, (Nd_Float *) &checker_v_offset, Nt_MODULATE, Nt_COLOR, Nt_ENABLED, Nt_ON, Nt_CMDSEP, Nt_CMDEND); /* Surface 2: Bump mapped surface using checkerboard bitmap texture */ Ni_Surface("surface 2", Nt_TEXTURE, "layer 1", Nt_TEXTURELINK, "checker bitmap texture", Nt_USCALE, (Nd_Float *) &checker_u_scale, Nt_VSCALE, (Nd_Float *) &checker_v_scale, Nt_MODULATE, Nt_COLOR, Nt_ENABLED, Nt_OFF, Nt_MODULATE, Nt_BUMP, Nt_ENABLED, Nt_ON, Nt_MULTIPLIER, Nt_BUMPU, (Nd_Float *) &bump_map_multiplier, Nt_MULTIPLIER, Nt_BUMPV, (Nd_Float *) &bump_map_multiplier, Nt_CMDSEP, Nt_CMDEND); /* Surface 3: Color + bump mapped surface using checkerboard bitmap texture */ Ni_Surface("surface 3", Nt_TEXTURE, "layer 1", Nt_TEXTURELINK, "checker bitmap texture", Nt_USCALE, (Nd_Float *) &checker_u_scale, Nt_VSCALE, (Nd_Float *) &checker_v_scale, Nt_MODULATE, Nt_COLOR, Nt_ENABLED, Nt_ON, Nt_MODULATE, Nt_BUMP, Nt_ENABLED, Nt_ON, Nt_MULTIPLIER, Nt_BUMPU, (Nd_Float *) &bump_map_multiplier, Nt_MULTIPLIER, Nt_BUMPV, (Nd_Float *) &bump_map_multiplier, Nt_CMDSEP, Nt_CMDEND); /* Surface 4: Opacity mapped surface using checkerboard bitmap texture */ Ni_Surface("surface 4", Nt_TEXTURE, "layer 1", Nt_TEXTURELINK, "checker bitmap texture", Nt_USCALE, (Nd_Float *) &checker_u_scale, Nt_VSCALE, (Nd_Float *) &checker_v_scale, Nt_MODULATE, Nt_COLOR, Nt_ENABLED, Nt_OFF, Nt_MODULATE, Nt_FACEOPACITY, Nt_ENABLED, Nt_ON, Nt_MODULATE, Nt_REFLECTOPACITY, Nt_ENABLED, Nt_ON, Nt_CMDSEP, /* Turn up the ambient and diffuse shading components so that */ /* the sphere will be more visible against the background. */ Nt_SHADINGCOEFFS, Nt_AMBIENT, (Nd_Float *) &ambient_coeff2, Nt_DIFFUSE, (Nd_Float *) &diffuse_coeff2, Nt_SPECULAR, (Nd_Float *) &specular_coeff2, Nt_CMDSEP, Nt_CMDEND); /* Surface 5: spherical environment map (chrome using fireplace texture) */ Ni_Surface("surface 5", /* Turn off ambient and diffuse shading so that we can simulate chrome */ Nt_SHADINGCOEFFS, Nt_AMBIENT, (Nd_Float *) &ambient_coeff, Nt_DIFFUSE, (Nd_Float *) &diffuse_coeff, Nt_SPECULAR, (Nd_Float *) &specular_coeff, Nt_CMDSEP, /* Associate a texture map as a spherical environmap map */ Nt_SPHENVMAP, Nt_ENABLED, Nt_ON, Nt_TEXTURELINK, "reflect map texture", Nt_REFLECTCOEFF, (Nd_Float *) &sph_env_reflectcoeff, Nt_ROTATE, Nt_YAXIS, (Nd_Float *) &sph_env_rot_angle, Nt_CMDSEP, Nt_CMDEND); /* Surface 6: Cubical environment mapping using the same texture for all 6 sides */ Ni_Surface("surface 6", /* Turn off ambient and diffuse shading so that we can simulate chrome */ Nt_SHADINGCOEFFS, Nt_AMBIENT, (Nd_Float *) &ambient_coeff, Nt_DIFFUSE, (Nd_Float *) &diffuse_coeff, Nt_SPECULAR, (Nd_Float *) &specular_coeff, Nt_CMDEND); Ni_Surface("surface 6", Nt_CUBICALENVMAP, Nt_ENABLED, Nt_ON, Nt_REFLECTCOEFF, (Nd_Float *) &cubical_env_map_reflect_coeff, Nt_TEXTUREIMAGE, Nt_FRONT, Nt_TIFF, "masonry.tif", Nt_TEXTUREIMAGE, Nt_BACK, Nt_TIFF, "masonry.tif", Nt_TEXTUREIMAGE, Nt_LEFT, Nt_TIFF, "fireplac.tif", Nt_TEXTUREIMAGE, Nt_RIGHT, Nt_TIFF, "fireplac.tif", Nt_TEXTUREIMAGE, Nt_TOP, Nt_TIFF, "check256.tif", Nt_TEXTUREIMAGE, Nt_BOTTOM, Nt_TIFF, "check256.tif", Nt_CMDEND); /* Surface 7: Example of orthographic texture parameterization */ Ni_Copy(Nt_SURFACE, "surface 1", "surface 7", Nt_CMDEND); /* Surface 8: Example of cylindrical texture parameterization */ Ni_Copy(Nt_SURFACE, "surface 1", "surface 8", Nt_CMDEND); /* Surfaces 9-15: 3D procedural texture maps */ for (i="0;" i < 7; ++i) { sprintf(handle_name, "surface %d", i+9); sprintf(handle_name2, "procedural texture %d", i+1); Ni_Surface(handle_name, /* Increase the ambient and diffuse shading coefficients a bit */ Nt_SHADINGCOEFFS, Nt_AMBIENT, (Nd_Float *) &ambient_coeff3, Nt_DIFFUSE, (Nd_Float *) &diffuse_coeff3, Nt_CMDSEP, Nt_TEXTURE, "layer 1", Nt_ENABLED, Nt_ON, Nt_TEXTURELINK, handle_name2, Nt_CMDSEP, Nt_CMDEND); } /* Surface 16. Two layered textures: Layer 1="2D" bitmap checkerboard */ /* texture, Layer 2="3D" procedural texture. */ Ni_Surface("surface 16", Nt_TEXTURE, "layer 1", Nt_ENABLED, Nt_ON, Nt_TEXTURELINK, "checker bitmap texture", Nt_USCALE, (Nd_Float *) &checker_u_scale, Nt_VSCALE, (Nd_Float *) &checker_v_scale, Nt_CMDSEP, Nt_TEXTURE, "layer 2", Nt_ENABLED, Nt_ON, Nt_TEXTURELINK, "procedural texture 1", /* This sets the color mixing with the previous layer */ Nt_COLORBLEND, Nt_DIFFUSE, (Nd_Float *) &color_mixing_value, Nt_CMDSEP, Nt_CMDSEP, Nt_CMDEND); /*>>>> Define Sphere Objects <<<<------------- */ /* Create a single sphere object centered at the origin */ Ni_Object_Define("sphere object", Nt_CMDEND); Ni_Primitive(Nt_SPHERE, Nt_RADIUS, (Nd_Float *) &sph_radius, Nt_CMDSEP, Nt_CMDEND); Ni_Object_End(Nt_CMDEND); /* Create 16 spheres in a grid pattern */ sphere_num=1; for (row=1; row <=4; ++row) { for (colm=1; colm <=4; ++colm) { sprintf(handle_name2, "sphere instance %d", sphere_num); Ni_Add_Instance(Nt_INSTANCE, handle_name2, Nt_OBJECT, "sphere object", Nt_CMDEND); Ni_Attach(Nt_INSTANCE, handle_name2, Nt_INSTANCE, "world", Nt_CMDEND); /* Move the sphere instance into place */ sphere_center[0]=(colm-1) * x_inc; sphere_center[1]=(row-1) * y_inc; sphere_center[2]=0.0; Ni_Transform(Nt_INSTANCE, handle_name2, Nt_POSTMULTIPLY, Nt_TRANSLATE, (Nd_Vector *) sphere_center, Nt_CMDEND); /* Link surface to each instance */ sprintf(handle_name, "surface %d", sphere_num); Ni_Surfacelink(Nt_SURFACE, handle_name, Nt_INSTANCE, handle_name2, Nt_CMDEND); ++sphere_num; } } /*>>>> Texture Parameterizations <<<<----------- */ /* This statement creates new texture coordinates for the seventh sphere */ /* by overriding the sphere's built-in u/v texture coordinates with new */ /* coordinates created by a front-on orthographic projection method. */ Ni_TxtrParam(Nt_INSTANCE, "sphere instance 7", Nt_MODEL, Nt_ORTHOGRAPHIC, Nt_CMDSEP, Nt_ENABLED, Nt_ON, Nt_CMDSEP, Nt_COORDSYSTEM, Nt_PRIMITIVE, Nt_CMDSEP, Nt_ORIGIN, (Nd_Vector *) txtrparam_ortho_origin, Nt_CMDSEP, Nt_UPOINT, (Nd_Vector *) txtrparam_ortho_upoint, Nt_CMDSEP, Nt_VPOINT, (Nd_Vector *) txtrparam_ortho_vpoint, Nt_CMDSEP, Nt_CMDEND); /* This statement creates new texture coordinates for the eighth sphere */ /* by overriding the sphere's built-in u/v texture coordinates with new */ /* coordinates created by a cylindrical projection method (aligned with Y axis). */ Ni_TxtrParam(Nt_INSTANCE, "sphere instance 8", Nt_MODEL, Nt_CYLINDRICAL, Nt_CMDSEP, Nt_ENABLED, Nt_ON, Nt_CMDSEP, Nt_COORDSYSTEM, Nt_PRIMITIVE, Nt_CMDSEP, Nt_ORIGIN, (Nd_Vector *) txtrparam_cyl_origin, Nt_CMDSEP, Nt_UPOINT, (Nd_Vector *) txtrparam_cyl_upoint, Nt_CMDSEP, Nt_VPOINT, (Nd_Vector *) txtrparam_cyl_vpoint, Nt_CMDSEP, Nt_CMDEND); /*>>>> 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_ANTIALIAS, Nt_ENABLED, Nt_OFF, Nt_OVERSAMPLE, (Nd_Int *) Oversampling_Values, 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_SURFACELINK, "background surface", 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]=(1.5 * x_inc); lookfrom[1]=(1.5 * y_inc); lookfrom[2]=15.0; lookat[0]=(1.5 * x_inc); lookat[1]=(1.5 * 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 TARGA file output driver. */ Ni_Output_Driver("driver#1", Nt_FILE, Nt_TARGA, Nt_CMDSEP, Nt_FILENAME, "example3.tga", Nt_CMDSEP, Nt_CMDEND); /* Now render the scene to the TARGA output file */ Ni_Render(Nt_CMDEND); Ni_Exit_Cleanup(); }