static int get_rlist_result (zhash_t *mods, flux_mrpc_t *mrpc, uint32_t nodeid, int *ep) { JSON o = NULL; int rc = -1; const char *name, *digest; int i, len, errnum, size, idle; module_t *m; if (flux_mrpc_get_outarg_obj (mrpc, nodeid, &o) < 0) goto done; if (modctl_rlist_dec (o, &errnum, &len) < 0) goto done; for (i = 0; i < len; i++) { if (modctl_rlist_dec_nth (o, i, &name, &size, &digest, &idle) < 0) goto done; if (!(m = zhash_lookup (mods, digest))) { if (!(m = module_create (name, size, digest, idle, nodeid))) goto done; zhash_update (mods, digest, m); zhash_freefn (mods, digest, (zhash_free_fn *)module_destroy); } else if (module_update (m, idle, nodeid) < 0) goto done; } *ep = errnum; rc = 0; done: Jput (o); return rc; }
int main(int argc, char* argv[]) { if (argc != 3) { printf("%s module function\n", argv[0]); return 0; } Module* module = module_create(argv[1], 0); if (module != NULL) { void* func = module_sym(module, argv[2]); printf("func=%p\n", func); module_destroy(module); } else { printf("load %s failed\n", argv[1]); } return 0; }
static void insmod_request_cb (flux_t *h, flux_msg_handler_t *mh, const flux_msg_t *msg, void *arg) { const char *path; json_t *args; size_t index; json_t *value; char *argz = NULL; size_t argz_len = 0; module_t *m = NULL; error_t e; if (flux_request_unpack (msg, NULL, "{s:s s:o}", "path", &path, "args", &args) < 0) goto error; if (!json_is_array (args)) goto proto; json_array_foreach (args, index, value) { if (!json_is_string (value)) goto proto; if ((e = argz_add (&argz, &argz_len, json_string_value (value)))) { errno = e; goto error; } } if (!(m = module_create (path, argz, argz_len))) goto error; flux_log (h, LOG_DEBUG, "insmod %s", m->name); if (flux_respond (h, msg, NULL) < 0) flux_log_error (h, "%s: flux_respond", __FUNCTION__); free (argz); return; proto: errno = EPROTO; error: if (flux_respond_error (h, msg, errno, NULL) < 0) flux_log_error (h, "%s: flux_respond_error", __FUNCTION__); free (argz); }
static MODULE_HANDLE NativeModuleHost_Create(BROKER_HANDLE broker, const void* configuration) { MODULE_HOST * result; if (broker == NULL || configuration == NULL) { /*Codes_SRS_NATIVEMODULEHOST_17_008: [ NativeModuleHost_Create shall return NULL if broker is NULL. ]*/ /*Codes_SRS_NATIVEMODULEHOST_17_009: [ NativeModuleHost_Create shall return NULL if configuration does not contain valid JSON. ]*/ LogError("broker [%p] or configuration [%p] is NULL, both are required", broker, configuration); result = NULL; } else { /*Codes_SRS_NATIVEMODULEHOST_17_010: [ NativeModuleHost_Create shall intialize the Module_Loader. ]*/ if (ModuleLoader_Initialize() != MODULE_LOADER_SUCCESS) { /*Codes_SRS_NATIVEMODULEHOST_17_026: [ If any step above fails, then NativeModuleHost_Create shall free all resources allocated and return NULL. ]*/ LogError("ModuleLoader_Initialize failed"); result = NULL; } else { /*Codes_SRS_NATIVEMODULEHOST_17_011: [ NativeModuleHost_Create shall parse the configuration JSON. ]*/ char * outprocess_module_args = (char *)configuration; JSON_Value *root_value = json_parse_string(outprocess_module_args); JSON_Object * module_host_args; if ((root_value == NULL) || ((module_host_args = json_value_get_object(root_value)) == NULL)) { if (root_value != NULL) { json_value_free(root_value); } LogError("NativeModuleHost_Create could not parse arguments as JSON"); result = NULL; } else { /*Codes_SRS_NATIVEMODULEHOST_17_035: [ If the "outprocess.loaders" array exists in the configuration JSON, NativeModuleHost_Create shall initialize the Module_Loader from this array. ]*/ JSON_Value * loaders_array = json_object_get_value(module_host_args, OOP_MODULE_LOADERS_ARRAY_KEY); if ((loaders_array != NULL) && (ModuleLoader_InitializeFromJson(loaders_array) != MODULE_LOADER_SUCCESS)) { LogError("NativeModuleHost_Create could not extract loaders array from module arguments."); result = NULL; } else { /*Codes_SRS_NATIVEMODULEHOST_17_012: [ NativeModuleHost_Create shall get the "outprocess.loader" object from the configuration JSON. ]*/ JSON_Object * loader_args = json_object_get_object(module_host_args, OOP_MODULE_LOADER_KEY); if (loader_args == NULL) { LogError("NativeModuleHost_Create could not get loader arguments."); result = NULL; } else { GATEWAY_MODULE_LOADER_INFO loader_info; if (parse_loader(loader_args, &loader_info) != 0) { /*Codes_SRS_NATIVEMODULEHOST_17_026: [ If any step above fails, then NativeModuleHost_Create shall free all resources allocated and return NULL. ]*/ LogError("NativeModuleHost_Create could not extract loader information from loader arguments."); result = NULL; } else { // Have loader and entrypoint now, get module. result = (MODULE_HOST*)malloc(sizeof(MODULE_HOST)); if (result == NULL) { LogError("NativeModuleHost_Create could not allocate module."); result = NULL; } else { /*Codes_SRS_NATIVEMODULEHOST_17_018: [ NativeModuleHost_Create shall get the "module.args" object from the configuration JSON. ]*/ JSON_Value * module_args = json_object_get_value(module_host_args, OOP_MODULE_ARGS_KEY); char * module_args_string = json_serialize_to_string(module_args); if (module_create(result, broker, &loader_info, module_args_string) != 0) { /*Codes_SRS_NATIVEMODULEHOST_17_026: [ If any step above fails, then NativeModuleHost_Create shall free all resources allocated and return NULL. ]*/ LogError("NativeModuleHost_Create could not load module."); free(result); result = NULL; } /*Codes_SRS_NATIVEMODULEHOST_17_024: [ NativeModuleHost_Create shall free all resources used during module loading. ]*/ json_free_serialized_string(module_args_string); } loader_info.loader->api->FreeEntrypoint(loader_info.loader, loader_info.entrypoint); } } } /*Codes_SRS_NATIVEMODULEHOST_17_024: [ NativeModuleHost_Create shall free all resources used during module loading. ]*/ json_value_free(root_value); } if (result == NULL) { // failed to create a module, give up entirely. /*Codes_SRS_NATIVEMODULEHOST_17_026: [ If any step above fails, then NativeModuleHost_Create shall free all resources allocated and return NULL. ]*/ ModuleLoader_Destroy(); } } } return result; }
int main( int argc, char *argv[]){ //set up the images Image *src; Image *myImage; //set up the modules!! Module *basket; Module *connector; Module *connector1; Line l; Point pts[2]; Module *circle; Module *hotAirBalloon; Module *teamOfBalloons; Module *scene; //pick the colors Color colors[6]; Color brown; Color blue; Color green; Color grey; //set up view stuff, and image file stuff Matrix vtm, gtm; int rows; int cols; View3D view; DrawState *ds; char filename[256]; int i, j, k, m, n; //Set the colors up color_set(&brown, 0.2, 0.1, 0.0); color_set(&blue, 0.6, 0.8, 1.0); color_set(&green, 0.1, 0.4, 0.0); color_set(&grey, 0.3, 0.3, 0.3); color_set(&colors[0], 1.0, 0.0, 0.0);//Red color_set(&colors[1],1.0, 0.5, 0.0);//orange color_set(&colors[2],1.0, 1.0, 0.1);//yellow color_set(&colors[3], 0.0, 1.0, 0.0);//green color_set(&colors[4], 0.0, 0.0, 1.0);//blue color_set(&colors[5], 0.4, 0.0, 0.8);//purple //if you want to supply a background image you can, otherwise I set a default sky blue background! if(argc>1){ printf("you supplied an image\n"); myImage = image_read(argv[1]); rows = myImage->rows; cols = myImage->cols; printf("%d %d\n", rows, cols); }else{ printf("We are giving your image a default size!\n"); rows = 500; cols = 500; myImage = image_create(rows, cols); for(m = 0; m< rows; m++){ for(n = 0; n<cols; n++){ image_setColor(myImage, m, n, blue); } } } //Loop over the scene 300 times moving the scene around!! for(k = 0; k<300; k++){ //Set up the view point_set( &(view.vrp), 150, 100, 200, 1.0); vector_set( &(view.vpn), -view.vrp.val[0], -view.vrp.val[1], -view.vrp.val[2]); vector_set( &(view.vup), 0, 1.0, 0); view.d = 20; view.du = 10; view.dv = view.du* (float)rows/cols; view.f = 0; view.b = 5; view.screenx = rows; view.screeny = cols; matrix_setView3D( &vtm, &view); matrix_identity( >m ); // basket: cube (will need 1 in hot air balloon module) basket = module_create(); module_color(basket, &brown); module_cube(basket, 1); //connectors: lines need 4 connector = module_create(); point_set3D(&pts[0],1,1,1 ); point_set3D(&pts[1],2,4.2,1); line_set(&l, pts[0], pts[1]); module_line(connector, &l); connector1 = module_create(); point_set3D(&pts[0], 1,1 , 1); point_set3D(&pts[1], 0,4.2 ,1 ); line_set(&l, pts[0], pts[1]); module_line(connector1, &l); // balloon: circles nested on top of eachother circle = module_create(); module_color(circle, &colors[0]); module_rotateX(circle, 0, 1); module_translate2D(circle, 0, 3); module_circle(circle, 1); module_color(circle, &colors[1]); module_rotateX(circle, 1, 0); module_translate2D(circle, 0, 1); module_circle(circle, 2.5); module_color(circle, &colors[2]); module_rotateX(circle, 1, 0); module_translate2D(circle, 0, 1); module_circle(circle, 3); module_color(circle, &colors[3]); module_rotateX(circle, 1, 0); module_translate2D(circle, 0, 1); module_circle(circle, 4); module_color(circle, &colors[4]); module_rotateX(circle, 1, 0); module_translate2D(circle, 0, 1); module_circle(circle, 5); module_color(circle, &colors[5]); module_rotateX(circle, 1, 0); module_translate2D(circle, 0, 1); module_circle(circle, 6); module_color(circle, &colors[0]); module_rotateX(circle, 1,0); module_translate2D(circle, 0, 1); module_circle(circle, 7); module_color(circle, &colors[1]); module_rotateX(circle, 1, 0); module_translate2D(circle, 0, 1); module_circle(circle, 7); module_color(circle, &colors[2]); module_rotateX(circle, 1, 0); module_translate2D(circle, 0,1); module_circle(circle, 7); module_color(circle, &colors[3]); module_rotateX(circle, 1, 0); module_translate2D(circle, 0, 1); module_circle(circle, 7); module_color(circle, &colors[4]); module_rotateX(circle, 1, 0); module_translate2D(circle, 0, 1); module_circle(circle, 6); module_color(circle, &colors[5]); module_rotateX(circle, 1, 0); module_translate2D(circle, 0, 1); module_circle(circle, 5); module_color(circle, &colors[0]); module_rotateX(circle, 1, 0); module_translate2D(circle, 0, 1); module_circle(circle, 4); module_color(circle, &colors[1]); module_rotateX(circle, 1, 0); module_translate2D(circle, 0, 0.5); module_circle(circle, 3); module_color(circle, &colors[2]); module_rotateX(circle, 1, 0); module_translate2D(circle, 0, 0.5); module_circle(circle, 2); //hotAirBalloon: put the above parts together hotAirBalloon = module_create(); module_module(hotAirBalloon, basket); module_module(hotAirBalloon, connector); module_translate(hotAirBalloon, -2,0 ,0 ); module_module(hotAirBalloon, connector1); module_translate(hotAirBalloon, 0, 0, -2); module_module(hotAirBalloon, connector1); module_translate(hotAirBalloon, 2, 0, 0); module_module(hotAirBalloon, connector); module_module(hotAirBalloon, circle); //make a team of balloons teamOfBalloons = module_create(); module_translate(teamOfBalloons, 0,-30,-15); module_module(teamOfBalloons, hotAirBalloon); module_translate(teamOfBalloons, 0, 0+(k*0.1),15); module_module(teamOfBalloons, hotAirBalloon); module_translate(teamOfBalloons, 0, 0+(k*0.1),15); module_module(teamOfBalloons, hotAirBalloon); //make a scene of balloons scene = module_create(); module_translate(scene, -40, 10+(k*0.1), 10); module_module(scene, teamOfBalloons); module_translate(scene, 140, 10, 55); module_module(scene, teamOfBalloons); module_translate(scene, -50, 10+(k*0.1), -40); module_module(scene, teamOfBalloons); module_translate(scene, 30, 10+(k*0.2) , -55); module_module(scene, teamOfBalloons); //set up the image src = image_create(rows, cols); //Either draw the background image you supplied or use my default background for (i = 0; i<rows; i++){ for (j = 0; j<cols; j++){ blue = image_getColor(myImage, i, j); image_setColor(src,i, j, blue); } } //set up the draw state if you want filled polygons need to use ShadeConstant instead of ShadeFrame ds = drawstate_create(); ds->shade = ShadeFrame; //draw the modules! module_draw( scene, &vtm, >m, ds, NULL, src); //put the files in the right place with the right name!! sprintf(filename, "/export/home/vedwards/Desktop/Graphics/images/hotAirBalloons3/frame-%04d.ppm", k ); printf("Writing image\n"); image_write( src, filename ); } //Clean up, delete all of the modules, images, and drawState module_delete(basket); module_delete(connector); module_delete(connector1); module_delete(circle); module_delete(hotAirBalloon); module_delete(teamOfBalloons); free(ds); image_free( src ); image_free( myImage ); return(0); }
struct module *builtin_init(struct error *error) { struct module *modl = module_create("Builtin"); int r; #define DEFINE_NATIVE(modl, name, nargs, impl, types ...) \ r = define_native(error, modl, name, nargs, impl, ##types); \ if (r < 0) goto error; DEFINE_NATIVE(modl, "gensym", 1, gensym, T_STRING, T_STRING); /* Primitive lenses */ DEFINE_NATIVE(modl, "del", 2, lns_del, T_REGEXP, T_STRING, T_LENS); DEFINE_NATIVE(modl, "store", 1, lns_store, T_REGEXP, T_LENS); DEFINE_NATIVE(modl, "value", 1, lns_value, T_STRING, T_LENS); DEFINE_NATIVE(modl, "key", 1, lns_key, T_REGEXP, T_LENS); DEFINE_NATIVE(modl, "label", 1, lns_label, T_STRING, T_LENS); DEFINE_NATIVE(modl, "seq", 1, lns_seq, T_STRING, T_LENS); DEFINE_NATIVE(modl, "counter", 1, lns_counter, T_STRING, T_LENS); /* Applying lenses (mostly for tests) */ DEFINE_NATIVE(modl, "get", 2, lens_get, T_LENS, T_STRING, T_TREE); DEFINE_NATIVE(modl, "put", 3, lens_put, T_LENS, T_TREE, T_STRING, T_STRING); /* Tree manipulation used by the PUT tests */ DEFINE_NATIVE(modl, "set", 3, tree_set_glue, T_STRING, T_STRING, T_TREE, T_TREE); DEFINE_NATIVE(modl, "clear", 2, tree_clear_glue, T_STRING, T_TREE, T_TREE); DEFINE_NATIVE(modl, "rm", 2, tree_rm_glue, T_STRING, T_TREE, T_TREE); DEFINE_NATIVE(modl, "insa", 3, tree_insa_glue, T_STRING, T_STRING, T_TREE, T_TREE); DEFINE_NATIVE(modl, "insb", 3, tree_insb_glue, T_STRING, T_STRING, T_TREE, T_TREE); /* Transforms and filters */ DEFINE_NATIVE(modl, "incl", 1, xform_incl, T_STRING, T_FILTER); DEFINE_NATIVE(modl, "excl", 1, xform_excl, T_STRING, T_FILTER); DEFINE_NATIVE(modl, "transform", 2, xform_transform, T_LENS, T_FILTER, T_TRANSFORM); DEFINE_NATIVE(modl, LNS_CHECK_REC_NAME, 2, lns_check_rec_glue, T_LENS, T_LENS, T_LENS); /* Printing */ DEFINE_NATIVE(modl, "print_string", 1, pr_string, T_STRING, T_UNIT); DEFINE_NATIVE(modl, "print_regexp", 1, pr_regexp, T_REGEXP, T_UNIT); DEFINE_NATIVE(modl, "print_endline", 1, pr_endline, T_STRING, T_UNIT); /* Lens inspection */ DEFINE_NATIVE(modl, "lens_ctype", 1, lns_ctype, T_LENS, T_REGEXP); DEFINE_NATIVE(modl, "lens_atype", 1, lns_atype, T_LENS, T_REGEXP); DEFINE_NATIVE(modl, "lens_vtype", 1, lns_vtype, T_LENS, T_REGEXP); DEFINE_NATIVE(modl, "lens_ktype", 1, lns_ktype, T_LENS, T_REGEXP); DEFINE_NATIVE(modl, "lens_format_atype", 1, lns_fmt_atype, T_LENS, T_STRING); /* System functions */ struct module *sys = module_create("Sys"); modl->next = sys; DEFINE_NATIVE(sys, "getenv", 1, sys_getenv, T_STRING, T_STRING); DEFINE_NATIVE(sys, "read_file", 1, sys_read_file, T_STRING, T_STRING); return modl; error: unref(modl, module); return NULL; }
int main(int argc, char *argv[]) { int frame; Color blue, green, purple, red, white; Point p[16]; BezierSurface bc; DrawState ds; Module *curve; View3D view; Matrix VTM, GTM; int divisions = 4; int rows = 300, cols = 400; Image *src = image_create(rows, cols); // grab the command line argument, if one exists if(argc > 1) { int tmp = atoi(argv[1]); if( tmp >= 0 && tmp < 10 ) divisions = tmp; } printf("Creating Bezier surface with %d subdivisions\n", divisions); color_set(&white, 1.0, 1.0, 1.0 ); color_set(&blue, .1, .2, .8); color_set(&green, .2, 0.7, 0.3 ); color_set(&purple, 0.6, 0.1, 0.7 ); color_set(&red, 0.75, 0.3, 0.3 ); curve = module_create(); // create a flat plane point_set3D(&p[0], 0.0, -0.2, 0.0); // first row, constant x, even spacing in z point_set3D(&p[1], 0.0, -0.2, 0.33); point_set3D(&p[2], 0.0, -0.2, 0.66); point_set3D(&p[3], 0.0, -0.2, 1.0); point_set3D(&p[4], 0.33, -0.2, 0.0); // second row point_set3D(&p[5], 0.33, -0.2, 0.33); point_set3D(&p[6], 0.33, -0.2, 0.66); point_set3D(&p[7], 0.33, -0.2, 1.0); point_set3D(&p[8], 0.66, -0.2, 0.0); // third row point_set3D(&p[9], 0.66, -0.2, 0.33); point_set3D(&p[10], 0.66, -0.2, 0.66); point_set3D(&p[11], 0.66, -0.2, 1.0); point_set3D(&p[12], 1.0, -0.2, 0.0); // fourth row point_set3D(&p[13], 1.0, -0.2, 0.33); point_set3D(&p[14], 1.0, -0.2, 0.66); point_set3D(&p[15], 1.0, -0.2, 1.0); bezierSurface_set(&bc, p); // put the curve into a module module_color(curve, &red); module_bezierSurface(curve, &bc, divisions, 0); // create a curved surface sitting above the plane point_set3D(&p[0], 0.0, 0.0, 0.0); // first row, constant x, even spacing in z point_set3D(&p[1], 0.0, 0.2, 0.33); point_set3D(&p[2], 0.0, 0.5, 0.66); point_set3D(&p[3], 0.0, 0.1, 1.0); point_set3D(&p[4], 0.33, 0.8, 0.0); // second row point_set3D(&p[5], 0.33, -0.1, 0.33); point_set3D(&p[6], 0.33, 0.0, 0.66); point_set3D(&p[7], 0.33, 0.3, 1.0); point_set3D(&p[8], 0.66, 0.3, 0.0); // third row point_set3D(&p[9], 0.66, 0.8, 0.33); point_set3D(&p[10], 0.66, 0.9, 0.66); point_set3D(&p[11], 0.66, 0.5, 1.0); point_set3D(&p[12], 1.0, 0.4, 0.0); // fourth row point_set3D(&p[13], 1.0, 0.2, 0.33); point_set3D(&p[14], 1.0, 0.5, 0.66); point_set3D(&p[15], 1.0, 1.0, 1.0); bezierSurface_set(&bc, p); // put the curve into a module module_color(curve, &green); module_bezierSurface(curve, &bc, divisions, 1); // set up the drawstate drawstate_setColor(&ds, white); ds.shade = ShadeFrame; // set up the view point_set3D(&(view.vrp), 0.0, 1.2, -3.0 ); vector_set( &(view.vpn), 0.0, -0.8, 2.5 ); vector_set( &(view.vup), 0.0, 1.0, 0.0 ); view.d = 1.5; view.du = 1.0; view.dv = 1.0*rows/cols; view.screeny = rows; view.screenx = cols; view.f = 0.0; view.b = 3.0; matrix_setView3D( &VTM, &view ); matrix_identity( >M ); // Create the animation by adjusting the GTM for(frame=0;frame<60;frame++) { char buffer[256]; matrix_rotateY(>M, cos(M_PI/30.0), sin(M_PI/30.0) ); module_draw( curve, &VTM, >M, &ds, NULL, src ); sprintf(buffer, "bezSurf-frame%03d.ppm", frame); image_write(src, buffer); image_reset(src); } printf("converting to gif...\n"); system("convert -delay 1.5 -loop 0 bezSurf-frame*.ppm bezSurf.gif"); system("rm bezSurf-frame*.ppm"); // clean up image_free( src ); module_delete( curve ); return(0); }
celix_status_t bundle_createModule(bundle_pt bundle, module_pt *module) { celix_status_t status = CELIX_SUCCESS; bundle_archive_pt archive = NULL; bundle_revision_pt revision = NULL; manifest_pt headerMap = NULL; status = CELIX_DO_IF(status, bundle_getArchive(bundle, &archive)); status = CELIX_DO_IF(status, bundleArchive_getCurrentRevision(archive, &revision)); status = CELIX_DO_IF(status, bundleRevision_getManifest(revision, &headerMap)); if (status == CELIX_SUCCESS) { long bundleId; status = bundleArchive_getId(bundle->archive, &bundleId); if (status == CELIX_SUCCESS) { int revision = 0; char moduleId[512]; char *mId; snprintf(moduleId, sizeof(moduleId), "%ld.%d", bundleId, revision); mId = strdup(moduleId); *module = module_create(headerMap, mId, bundle); free(mId); if (*module != NULL) { version_pt bundleVersion = module_getVersion(*module); char * symName = NULL; status = module_getSymbolicName(*module, &symName); if (status == CELIX_SUCCESS) { array_list_pt bundles = framework_getBundles(bundle->framework); unsigned int i; for (i = 0; i < arrayList_size(bundles); i++) { bundle_pt check = (bundle_pt) arrayList_get(bundles, i); long id; if (bundleArchive_getId(check->archive, &id) == CELIX_SUCCESS) { if (id != bundleId) { module_pt mod = NULL; char * sym = NULL; version_pt version; int cmp; status = bundle_getCurrentModule(check, &mod); status = module_getSymbolicName(mod, &sym); version = module_getVersion(mod); version_compareTo(bundleVersion, version, &cmp); if ((symName != NULL) && (sym != NULL) && !strcmp(symName, sym) && !cmp) { char *versionString = NULL; version_toString(version, &versionString); printf("Bundle symbolic name and version are not unique: %s:%s\n", sym, versionString); free(versionString); status = CELIX_BUNDLE_EXCEPTION; break; } } } } arrayList_destroy(bundles); } } } } framework_logIfError(bundle->framework->logger, status, NULL, "Failed to create module"); return status; }
// makes 3 X-wing fighters in a loose formation int main(int argc, char *argv[]) { int i, j; //loop variables Image *src; Module* wall; Module* ray; Module* ray2; Module *scene1; Module* scene2; Polygon p; Line l; Point point[4]; Point point2[2]; View3D view; Matrix vtm, gtm; DrawState *ds; char filename[100]; Color Flame = { { 1.0, 0.7, 0.2 } }; Color Red = { { 1.0, 0.2, 0.1 } }; Color Grey = { { 0.745, 0.745, 0.745} }; Color Blue = {{0.117647, 0.564706, 1}}; // Color Grey = {{1, 1, 1}}; // set up the view point_set3D( &(view.vrp), 4, 4, 4 ); vector_set( &(view.vpn), -view.vrp.val[0], -view.vrp.val[1], -view.vrp.val[2] ); vector_set( &(view.vup), 0, 1, 0 ); view.d = 1; view.du = 1.6; view.dv = 0.9; view.f = 1; view.b = 50; view.screenx = 640; view.screeny = 360; matrix_setView3D( &vtm, &view ); matrix_identity( >m ); // //wall wall = module_create(); module_color(wall, &Red); polygon_init(&p); point_set3D(&point[0], 1,1,2); point_set3D(&point[1], 1,0,2); point_set3D(&point[2], 0,0,2); point_set3D(&point[3], 0,1,2); polygon_set(&p, 4, &point[0]); module_polygon(wall, &p); //ray ray = module_create(); module_color(ray, &Blue); for(i=0; i< 5; i++){ point_set3D(&point2[0], -1+0.01*i, -1, 1); point_set3D(&point2[1], 1+0.01*i, 1, 1); line_set(&l, point2[0], point2[1]); module_line(ray, &l); } //ray2 ray2 = module_create(); module_color(ray2, &Red); for(i=0; i< 5; i++){ point_set3D(&point2[0], -1+0.01*i, 1, -1); point_set3D(&point2[1], 1+0.01*i, -1, -1); line_set(&l, point2[0], point2[1]); // line_zBuffer(&l, 0); module_line(ray2, &l); } //scene // scene = module_create(); // module_module(scene, wall); // module_module(scene, ray); // module_module(scene, ray2); // module_module(scene, wall); for(i=0; i< 36; i++){ //scene scene1 = module_create(); scene2 = module_create(); module_rotateZ(scene1, cos(i*10 * M_PI/180), sin(i*10 * M_PI/180)); module_scale( scene1, 3, 1, 2 ); module_color( scene1, &Blue ); module_cube( scene1, 1); module_scale(scene2, 0.5, 0.5, 0.5); module_cylinder(scene2, 30); // create the image and drawstate src = image_create( 360, 640 ); ds = drawstate_create(); drawstate_setAlpha(ds, 1); ds->shade = ShadeDepth; // draw into the scene // module_draw( scene1, &vtm, >m, ds, src ); drawstate_setAlpha(ds, 1 ); module_draw( scene1, &vtm, >m, ds, src ); // write out the scene sprintf(filename, "frame_%.2d.ppm", i); image_write( src, filename ); module_delete( scene1); } //free the polygon data // polygon_clear( &p ); // free the modules // module_delete( scene); // module_delete( wall ); // free the drawstate free(ds); // free the image image_free( src ); return(0); }
int main(int argc, char *argv[]) { int i, frame; Color blue, green, purple, red, white; Point p[4]; BezierCurve bc; DrawState ds; Module *curveA; Module *curveB; Module *curves; View3D view; Matrix VTM, GTM; int divisions = 4; int rows = 300, cols = 400; Image *src = image_create(rows, cols); // grab the command line argument, if one exists if(argc > 1) { int tmp = atoi(argv[1]); if( tmp >= 0 && tmp < 10 ) divisions = tmp; } printf("Creating Bezier curves with %d subdivisions\n", divisions); color_set(&white, 1.0, 1.0, 1.0 ); color_set(&blue, .1, .2, .8); color_set(&green, .2, 0.7, 0.3 ); color_set(&purple, 0.6, 0.1, 0.7 ); color_set(&red, 0.75, 0.3, 0.3 ); // set one curve point_set3D(&p[0], 0.0, 0.0, 0.0); point_set3D(&p[1], 1.0, 0.2, 0.0); point_set3D(&p[2], 0.7, 0.5, 0.2); point_set3D(&p[3], 1.0, 1.0, 1.0); bezierCurve_set(&bc, p); // put the curve into a module curveA = module_create(); module_color(curveA, &blue); module_bezierCurve(curveA, &bc, divisions); // set the second curve point_set3D(&p[0], 0.0, 0.0, 0.0); point_set3D(&p[1], 0.0, 0.2, 1.0); point_set3D(&p[2], 0.2, 0.5, 0.7); point_set3D(&p[3], 1.0, 1.0, 1.0); bezierCurve_set(&bc, p); // put the curve into a module curveB = module_create(); module_color(curveB, &green); module_bezierCurve(curveB, &bc, divisions); // create a module with six curves curves = module_create(); for(i=0;i<3;i++) { module_module( curves, curveA ); module_module( curves, curveB ); module_rotateY( curves, cos(2.0*M_PI/3.0), sin(2.0*M_PI/3.0) ); } // set up the drawstate drawstate_setColor(&ds, white); // set up the view point_set3D(&(view.vrp), 0.0, 0.5, -3.0 ); vector_set( &(view.vpn), 0.0, 0.0, 1.0 ); vector_set( &(view.vup), 0.0, 1.0, 0.0 ); view.d = 1.0; view.du = 1.0; view.dv = 1.0*rows/cols; view.screeny = rows; view.screenx = cols; view.f = 0.0; view.b = 3.0; matrix_setView3D( &VTM, &view ); matrix_identity( >M ); // Create the animation by adjusting the GTM for(frame=0;frame<60;frame++) { char buffer[256]; matrix_rotateY(>M, cos(M_PI/30.0), sin(M_PI/30.0) ); module_draw( curves, &VTM, >M, &ds, NULL, src ); sprintf(buffer, "bez3d-frame%03d.ppm", frame); image_write(src, buffer); image_reset(src); } printf("converting to gif...\n"); system("convert -delay 1.5 -loop 0 bez3d-frame*.ppm bez3d.gif"); system("rm bez3d-frame*.ppm"); // clean up image_free( src ); module_delete( curveA ); module_delete( curveB ); module_delete( curves ); return(0); }
int main(int argc, char* argv[]){ Image* src; Module *scene; Module* GRAPHICS; View3D view; Matrix vtm, gtm; DrawState *ds; Lighting *light; Point center;//center of animation Polygon poly;//polygon that holds the animation path points int frameNum;//holds the frame number for animation char filename[100];//holds the frame name Color Red; Color Blue; Color Green; Color Grey; Color Lemon; Color Black; Color White; Color Yellow; Color DarkAmbiant; Color randColor[10]; //setting colors Color_set(&Red, 1.0, 0.2, 0.1 ); Color_set(&Blue, 0.1, 0.1, 1.0); Color_set(&Green, 0.1, 1, 0.1 ); Color_set(&White, 1, 1, 1 ); Color_set(&Grey, 0.7, 0.7, 0.7 ); Color_set(&Lemon, 1.0, 1.0, 0.8); Color_set(&Black, 0.05, 0.05, 0.05); Color_set(&Yellow, 1, 0.894118, 0.709804); Color_set(&DarkAmbiant, 0.3, 0.3, 0.3); Color_set(&randColor[0], 0, 1, 1); Color_set(&randColor[1], 0.498039, 1, 0); Color_set(&randColor[2], 1, 0.54902, 0); Color_set(&randColor[3], 1, 0.0784314, 0.576471); Color_set(&randColor[4], 1, 0.843137, 0); Color_set(&randColor[5], 0.960784, 1, 0.980392); Color_set(&randColor[6], 1, 0.647059, 0); Color_set(&randColor[7], 1, 0.270588, 0); Color_set(&randColor[8], 0, 1, 0.498039); Color_set(&randColor[9], 1, 1, 0); // setting the view point_set3D( &(view.vrp), 35, 60, 30 ); vector_set( &(view.vpn), -view.vrp.val[0]+35, -view.vrp.val[1], -view.vrp.val[2]); vector_set( &(view.vup), 0, 1, 0 ); view.d = 1; view.du = 1.6; view.dv = 0.9; view.f = 1; view.b = 200; view.screenx = 1280; view.screeny = 720; matrix_setView3D( &vtm, &view ); matrix_identity( >m ); //creating GRAPHICS module GRAPHICS = module_create(); module_alphabet_G(GRAPHICS); module_identity(GRAPHICS); module_translate(GRAPHICS, 10, 0, 0); module_alphabet_R(GRAPHICS); module_identity(GRAPHICS); module_translate(GRAPHICS, 20, 0, 0); module_alphabet_A(GRAPHICS); module_identity(GRAPHICS); module_translate(GRAPHICS, 30, 0, 0); module_alphabet_P(GRAPHICS); module_identity(GRAPHICS); module_translate(GRAPHICS, 40, 0, 0); module_alphabet_H(GRAPHICS); module_identity(GRAPHICS); module_translate(GRAPHICS, 50, 0, 0); module_alphabet_I(GRAPHICS); module_identity(GRAPHICS); module_translate(GRAPHICS, 56, 0, 0); module_alphabet_C(GRAPHICS); module_identity(GRAPHICS); module_translate(GRAPHICS, 68, 0, 0); module_alphabet_S(GRAPHICS); // setting the light light = lighting_create(); lighting_add( light, LightAmbient, &Lemon, NULL, NULL, 0, 0); lighting_add(light, LightPoint, &White , NULL, &view.vrp, 0, 0); lighting_add(light, LightSpot, &White, &view.vpn, &view.vrp, cos(10*M_PI/180), 40); //setting drawstate ds = drawstate_create(); point_copy(&(ds->viewer), &(view.vrp) ); ds->shade = ShadePhong; // ds->shade = ShadeDepth; drawstate_setBody(ds, Black); drawstate_setSurface(ds, Red); drawstate_setSurfaceCoeff(ds, 10); //Animation frameNum =0; //path #1 point_set3D(¢er, 40, 0, -10); view_rotate_circle(&poly, ¢er, 100, 50, 0, 0, 0); polygon_print(&poly, stdout); for(int k=0; k<100; k++){ frameNum++; point_set3D( &(view.vrp), poly.vertex[k].val[0], poly.vertex[k].val[1], poly.vertex[k].val[2]); vector_set( &(view.vpn), -view.vrp.val[0], -view.vrp.val[1], -view.vrp.val[2] ); matrix_setView3D( &vtm, &view ); //creating scene module scene = module_create(); module_module(scene, GRAPHICS); // image src = image_create( view.screeny, view.screenx ); image_fillrgb(src, 1.0, 1.0, 0.8); //Drawing module_draw( scene, &vtm, >m, ds, light, src ); sprintf(filename, "../images/frame_%.4d.ppm",frameNum); image_write( src, filename); } //path #2 point_set3D(¢er, 40, 0, -10); view_rotate_circle(&poly, ¢er, 100, 90, 0, 0 , 0); // polygon_print(&poly, stdout); for(int k=0; k<100; k++){ if(frameNum == 119){ point_print(&view.vrp, stdout); break; } view_rotate_circle(&poly, ¢er, 50, 50+k, 0-2*k, 0 , 0); frameNum++; point_set3D( &(view.vrp), poly.vertex[k].val[0], poly.vertex[k].val[1], poly.vertex[k].val[2]); vector_set( &(view.vpn), -view.vrp.val[0], -view.vrp.val[1], -view.vrp.val[2] ); matrix_setView3D( &vtm, &view ); //creating scene module scene = module_create(); module_module(scene, GRAPHICS); // image src = image_create( view.screeny, view.screenx ); image_fillrgb(src, 1.0, 1.0, 0.8); //Drawing module_draw( scene, &vtm, >m, ds, light, src ); sprintf(filename, "../images/frame_%.4d.ppm",frameNum); image_write( src, filename); } //path #3 for(int k=0; k<120; k++){ frameNum++; if(frameNum <= 160){ point_set3D( &(view.vrp), -3.345+(k), 36.298+(k), 28.391-(k/2.0)); vector_set( &(view.vpn), -view.vrp.val[0]+k, -view.vrp.val[1], -view.vrp.val[2]); matrix_setView3D( &vtm, &view ); } //creating scene module scene = module_create(); module_module(scene, GRAPHICS); if(frameNum == 160){ light = lighting_create(); lighting_add( light, LightAmbient, &Grey, NULL, NULL, 0, 0); } // setting the light if(frameNum == 165){ Point plight1; point_set3D(&plight1, -5, 10, -10); lighting_add(light, LightSpot, &White, &view.vpn, &plight1, cos(10*M_PI/180), 40); } if(frameNum == 175){ Point plight2; point_set3D(&plight2, 85, 10, -10); lighting_add(light, LightSpot, &White, &view.vpn, &plight2, cos(10*M_PI/180), 40); } if(frameNum == 180){ lighting_add(light, LightSpot, &White, &view.vpn, &view.vrp, cos(10*M_PI/180), 40); } if(frameNum >= 183){ light = lighting_create(); lighting_add( light, LightAmbient, &Grey, NULL, NULL, 0, 0); int output1 = 0 + (rand() % (int)(10 - 0 + 1)); int output2 = 0 + (rand() % (int)(10 - 0 + 1)); int output3 = 0 + (rand() % (int)(10 - 0 + 1)); Point plight1; point_set3D(&plight1, -5, 10, -10); lighting_add(light, LightSpot, &randColor[output1], &view.vpn, &plight1, cos(10*M_PI/180), 40); Point plight2; point_set3D(&plight2, 85, 10, -10); lighting_add(light, LightSpot, &randColor[output2], &view.vpn, &plight2, cos(10*M_PI/180), 40); lighting_add(light, LightSpot, &randColor[output3], &view.vpn, &view.vrp, cos(10*M_PI/180), 40); } // image src = image_create( view.screeny, view.screenx ); image_fillrgb(src, 1.0, 1.0, 0.8); //setting drawstate ds = drawstate_create(); point_copy(&(ds->viewer), &(view.vrp) ); ds->shade = ShadePhong; // ds->shade = ShadeDepth; drawstate_setBody(ds, Black); drawstate_setSurface(ds, Red); drawstate_setSurfaceCoeff(ds, 10); //Drawing module_draw( scene, &vtm, >m, ds, light, src ); sprintf(filename, "../images/frame_%.4d.ppm",frameNum); image_write( src, filename); } //***Uncomment for making the .gif animation*** printf("Making the demo.gif file....\n"); system("convert -delay 10 ../images/frame_*.ppm ../images/demo.gif"); printf("Cleaning up...\n"); system("rm -f ../images/frame_*.ppm"); printf("Finished Successfully :)\n"); return(0); }
/*we are going to draw a cube using our new module hiearchy*/ int main( int argc, char *argv[]){ Image *src; Module *cube1; Module *cube2; Module *cube3; Module *cube4; Module *cube5; Module *cube6; Module *cube7; Module *cube8; Module *manyCubes; Color Blue; View3D view; Matrix vtm, gtm; int rows = 320; int cols = 320; DrawState *ds; int count; char filename[256]; //Set the colors up color_set(&Blue, 0.0, 0, 1.0); // set up the view point_set( &(view.vrp), 1, 2, 0,0 ); vector_set( &(view.vpn), -view.vrp.val[0], -view.vrp.val[1], -view.vrp.val[2] ); vector_set( &(view.vup),0, 1.0, 0); view.d = 5; view.du = 5; view.dv = view.du * (float)rows/cols; view.f = 0; view.b = 5; view.screenx = rows; view.screeny = cols; matrix_setView3D( &vtm, &view ); matrix_identity( >m ); for (count = 0; count< 205; count++){ //Start first cube cube1 = module_create(); module_color( cube1, &Blue); module_translate(cube1, 0, 0, 0-count*0.01); module_cube(cube1, 0); //Set the colors up color_set(&Blue, 0.1, 0.1, 1.0); //Start second cube cube2 = module_create(); module_color(cube2, &Blue); module_translate(cube2, 0, 0, 0+count*0.01); module_cube(cube2, 0); //Set the colors up color_set(&Blue, 0.2, 0.2, 1.0); //start third cube cube3 = module_create(); module_color(cube3, &Blue); module_translate(cube3, 0+count*0.01,0, 0); module_cube(cube3, 0); //Set the colors up color_set(&Blue, 0.3, 0.3, 1.0); //start fourth cube cube4 = module_create(); module_color(cube4, &Blue); module_translate(cube4, 0-count*0.01,0, 0); module_cube(cube4, 0); //Set the colors up color_set(&Blue, 0.4, 0.4, 1.0); //start fifth cube cube5 = module_create(); module_color(cube5, &Blue); module_translate(cube5, 0-count*0.01, 0, 0-count*0.01); module_cube(cube5, 0); //Set the colors up color_set(&Blue, 0.5, 0.5, 1.0); cube6 = module_create(); module_color(cube6, &Blue); module_translate(cube6, 0+count*0.01, 0, 0+count*0.01); module_cube(cube6, 0); //Set the colors up color_set(&Blue, 0.6, 0.6, 1.0); cube7 = module_create(); module_color(cube7, &Blue); module_translate(cube7, 0-count*0.01, 0, 0+count*0.01); module_cube(cube7, 0); //Set the colors up color_set(&Blue, 0.7, 0.7, 1.0); cube8 = module_create(); module_color(cube8, &Blue); module_translate(cube8, 0+count*0.01, 0, 0-count*0.01); module_cube(cube8, 0); //put the cubes into a scene! manyCubes = module_create(); module_module(manyCubes, cube5); module_module(manyCubes, cube6); module_module(manyCubes, cube7); module_module(manyCubes, cube8); module_module(manyCubes, cube1); module_module(manyCubes, cube2); module_module(manyCubes, cube3); module_module(manyCubes, cube4); //Create the image draw state src = image_create(rows, cols); ds = drawstate_create(); ds->shade = ShadeConstant; //Draw the scene module_draw( manyCubes, &vtm, >m, ds, NULL, src ); //write out the scene sprintf(filename, "/export/home/vedwards/Desktop/Graphics/images/blueCube/frame-%04d.ppm", count ); printf("Writing image\n"); image_write( src, filename ); //image_write(src, "blueCubeNew.ppm"); } //free the modules module_delete( manyCubes ); module_delete( cube1 ); module_delete( cube2 ); module_delete( cube3 ); module_delete( cube4 ); module_delete( cube5 ); module_delete( cube6 ); module_delete( cube7 ); module_delete( cube8 ); //free the drawState free(ds); //free the image image_free( src ); return(0); }