/* * insert a pyramid into the module */ void module_pyramid(Module *md, int solid, float size, float x, float y, float z){ if(!md){ printf("Null md passed to module_pyramid\n"); return; } Polygon side; Point tv[3]; Point v[5]; Line l; Element *e; int i; polygon_init(&side); // corners of the pyramid point_set3D(&v[0], -1, -1, -1 ); point_set3D(&v[1], 1, -1, -1 ); point_set3D(&v[2], 1, -1, 1 ); point_set3D(&v[3], -1, -1, 1 ); point_set3D(&v[4], 0, 0, 0); //printf("points created\n"); // set pyramid parameters module_scale(md, (int)size, (int)size, (int)size); module_translate(md, (float)x, (float)y, (float)z); //printf("parameters set\n"); if (solid == 0){ // add only lines // foundation for(i=0;i<3;i++){ line_set( &l, v[i], v[i+1] ); e = element_init(ObjLine, &l); module_insert(md, e); } line_set( &l, v[3], v[0] ); e = element_init(ObjLine, &l); module_insert(md, e); // connecting lines line_set( &l, v[4], v[0] ); e = element_init(ObjLine, &l); module_insert(md, e); line_set( &l, v[1], v[4] ); e = element_init(ObjLine, &l); module_insert(md, e); line_set( &l, v[2], v[4] ); e = element_init(ObjLine, &l); module_insert(md, e); line_set( &l, v[3], v[4] ); e = element_init(ObjLine, &l); module_insert(md, e); //printf("successfully passed to module\n"); } else{ // front side point_copy(&tv[0], &v[0]); point_copy(&tv[1], &v[1]); point_copy(&tv[2], &v[4]); polygon_set(&side, 3, tv); e = element_init(ObjPolygon, &side); module_insert(md, e); // back side point_copy(&tv[0], &v[3]); point_copy(&tv[1], &v[2]); point_copy(&tv[2], &v[4]); polygon_set(&side, 3, tv); e = element_init(ObjPolygon, &side); module_insert(md, e); // bottom side polygon_set(&side, 4, &(v[0])); e = element_init(ObjPolygon, &side); module_insert(md, e); // left side point_copy(&tv[0], &v[0]); point_copy(&tv[1], &v[3]); point_copy(&tv[2], &v[4]); polygon_set(&side, 3, tv); e = element_init(ObjPolygon, &side); module_insert(md, e); // right side point_copy(&tv[0], &v[1]); point_copy(&tv[1], &v[2]); point_copy(&tv[2], &v[4]); polygon_set(&side, 3, tv); e = element_init(ObjPolygon, &side); module_insert(md, e); //printf("successfully passed to module\n"); } polygon_clear(&side); }
/* * Sourced from coursework file test6b.c (Bruce Maxwell) */ void module_cone( Module *mod, int sides, int fill, int size, float x, float y, float z) { Polygon p; Point xtop, xbot; Element *e; Line l; double x1, x2, z1, z2; int i; if(!mod){ printf("Null md passed to module_cylinder\n"); return; } // set cone parameters module_scale(mod, (int)size, (int)size, (int)size); module_translate(mod, (float)x, (float)y, (float)z); printf("parameters set\n"); polygon_init( &p ); point_set3D( &xtop, 0, 1.0, 0.0 ); point_set3D( &xbot, 0, 0.0, 0.0 ); if (fill == 1){ // make a fan for the top and bottom sides // and quadrilaterals for the sides for(i=0;i<sides;i++) { Point pt[6]; x1 = cos( i * M_PI * 2.0 / sides ); z1 = sin( i * M_PI * 2.0 / sides ); x2 = cos( ( (i+1)%sides ) * M_PI * 2.0 / sides ); z2 = sin( ( (i+1)%sides ) * M_PI * 2.0 / sides ); point_copy( &pt[0], &xbot ); point_set3D( &pt[1], x1, 0.0, z1 ); point_set3D( &pt[2], x2, 0.0, z2 ); polygon_set( &p, 3, pt ); e = element_init(ObjPolygon, &p); module_insert(mod, e); point_set3D( &pt[3], x1, 0.0, z1 ); point_set3D( &pt[4], x2, 0.0, z2 ); point_copy( &pt[5], &xtop); polygon_set( &p, 3, &pt[3] ); e = element_init(ObjPolygon, &p); module_insert(mod, e); } } else{ // make a fan for the top and bottom sides // and quadrilaterals for the sides for(i=0;i<sides;i++) { Point pt[8]; x1 = cos( i * M_PI * 2.0 / sides ); z1 = sin( i * M_PI * 2.0 / sides ); x2 = cos( ( (i+1)%sides ) * M_PI * 2.0 / sides ); z2 = sin( ( (i+1)%sides ) * M_PI * 2.0 / sides ); point_copy( &pt[0], &xbot ); point_set3D( &pt[1], x1, 0.0, z1 ); point_set3D( &pt[2], x2, 0.0, z2 ); line_set( &l, pt[0], pt[1] ); e = element_init(ObjLine, &l); module_insert(mod, e); line_set( &l, pt[1], pt[2] ); e = element_init(ObjLine, &l); module_insert(mod, e); line_set( &l, pt[2], pt[0]); e = element_init(ObjLine, &l); module_insert(mod, e); point_set3D( &pt[3], x1, 0.0, z1 ); point_set3D( &pt[4], x2, 0.0, z2 ); point_set3D( &pt[5], x2, 1.0, z2 ); point_set3D( &pt[6], x1, 1.0, z1 ); point_copy( &pt[7], &xtop); line_set( &l, pt[0], pt[7] ); e = element_init(ObjLine, &l); module_insert(mod, e); line_set( &l, pt[1], pt[7] ); e = element_init(ObjLine, &l); module_insert(mod, e); line_set( &l, pt[2], pt[7]); e = element_init(ObjLine, &l); module_insert(mod, e); line_set( &l, pt[3], pt[7]); e = element_init(ObjLine, &l); module_insert(mod, e); } } polygon_clear( &p ); }
// 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); }