/* * use the de Casteljau algorithm to subdivide the Bezier curve divisions times, * then add the lines connecting the control points to the module. */ void module_bezierCurve(Module *m, BezierCurve *b, int divisions){ Line tempLine; Point deCast[7]; BezierCurve tempbez; if(!m || !b){ printf("Null passed to module_bezierCurve\n"); return; } // base case, add six lines to module if(divisions == 0){ line_set(&tempLine, b->c[0], b->c[1]); module_line(m, &tempLine); line_set(&tempLine, b->c[1], b->c[2]); module_line(m, &tempLine); line_set(&tempLine, b->c[2], b->c[3]); module_line(m, &tempLine); return; } // compute all avg points for 3 orders, down to just one point 3rd order deCasteljau(&(deCast[0]), &(b->c[0])); // left half bezierCurve_set(&tempbez, &(deCast[0])); module_bezierCurve(m, &tempbez, divisions-1); // right half bezierCurve_set(&tempbez, &(deCast[3])); module_bezierCurve(m, &tempbez, divisions-1); }
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); }
/* * use the de Casteljau algorithm to subdivide the Bezier surface divisions times, * then draw either the lines connecting the control points, if solid is 0, * or draw triangles connecting the surface. */ void module_bezierSurface(Module *m, BezierSurface *b, int divisions, int solid){ int i, j, k, l; Line tempLine; Point grid[7][7]; Point controls[7]; Point deCast[7]; Point surfacePoints[16]; BezierSurface tempBezSurf; Polygon *temptri = polygon_create(); if(!m || !b){ printf("Null passed to module_bezierSurface\n"); return; } // base case if(divisions == 0){ // lines if(solid == 0){ for(i=0;i<4;i++){ for(j=0;j<3;j++){ line_set(&tempLine, b->c[i][j], b->c[i][j+1]); module_line(m, &tempLine); line_set(&tempLine, b->c[j][i], b->c[j+1][i]); module_line(m, &tempLine); } } } // triangles else { controls[0] = b->c[0][0]; controls[1] = b->c[0][3]; controls[2] = b->c[3][3]; controls[3] = b->c[3][0]; controls[4] = b->c[0][0]; polygon_set(temptri, 3, &(controls[0])); module_polygon(m, temptri); polygon_set(temptri, 3, &(controls[2])); module_polygon(m, temptri); } } // divide and recurse else { // compute all avg points for 3 orders, down to just one point 3rd order // do for each of the four bezier curves for(i=0;i<4;i++){ deCasteljau(&(deCast[0]), &(b->c[i][0])); for(j=0;j<7;j++){ grid[2*i][j] = deCast[j]; } } // now traverse the other direction, populating grid for(i=0;i<7;i++){ for(j=0;j<4;j++){ controls[j] = grid[2*j][i]; } deCasteljau(&(deCast[0]), &(controls[0])); for(j=0;j<7;j++){ grid[j][i] = deCast[j]; } } // now make the four new bezier surfaces by subdividing across for(i=0;i<2;i++){ for(j=0;j<2;j++){ for(k=0;k<4;k++){ for(l=0;l<4;l++){ surfacePoints[4*k+l] = grid[k+3*i][l+3*j]; } } bezierSurface_set(&tempBezSurf, &(surfacePoints[0])); // recursive call module_bezierSurface(m, &tempBezSurf, divisions-1, solid); } } } // clean up polygon_free(temptri); }
// 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); }