void polygons_draw(struct polygon_fifo * polygons, struct screen * display) { int i, j; for(i=0, j=polygons->fifo_tail; i<polygons->nb_items; ++i, ++j) { if(j>=MAX_POLYGONS) j=0; polygon_draw(&(polygons->tab[j]), display); } }
/* * Drawing stuff */ static void polygons_draw(struct polygon_fifo *polygons, ttk_surface srf) { int i, j; for(i = 0, j = polygons->fifo_tail; i < polygons->nb_items; ++i, ++j){ if(j >= MAX_POLYGONS){ j = 0; } polygon_draw(&(polygons->tab[j]), srf); } }
int main(int argc, char *argv[]) { const int rows = 1000; const int cols = 1000; View3D view; Matrix vtm; Polygon side[4]; Polygon tpoly; Point tv[3]; Point v[4]; Color color[4]; Image *src; char filename[100]; double x; int i; // set some colors Color_set( &color[0], 0, 0, 1 ); Color_set( &color[1], 0, 1, 0 ); Color_set( &color[2], 1, 0, 0 ); Color_set( &color[3], 1, 0, 1 ); // initialize polygons for(i=0;i<4;i++) { polygon_init( &side[i] ); } // corners of a cube, centered at (0, 0, 0) point_set3D( &v[0], 0, 1, 0 ); point_set3D( &v[1], 1, -1, 0); point_set3D( &v[2], -1, -1, 0 ); point_set3D( &v[3], 0, 0, 1 ); //base of the pyramid polygon_set(&side[3], 3, &(v[0])); //side1 point_copy(&tv[0], &v[0]); point_copy(&tv[1], &v[2]); point_copy(&tv[2], &v[3]); polygon_set(&side[0], 3, tv); //side2 point_copy(&tv[0], &v[0]); point_copy(&tv[1], &v[1]); point_copy(&tv[2], &v[3]); polygon_set(&side[1], 3, tv); //side3 point_copy(&tv[0], &v[1]); point_copy(&tv[1], &v[2]); point_copy(&tv[2], &v[3]); polygon_set(&side[2], 3, tv); point_set3D( &(view.vrp), 1, 0, 0); vector_set( &(view.vpn), -view.vrp.val[0], -view.vrp.val[1], -view.vrp.val[2] ); vector_set( &(view.vup), 0, 0, 1 ); view.d = 1; // focal length view.du = 2; view.dv = view.du * (float)rows / cols; view.f = 0; // front clip plane view.b = 4; // back clip plane view.screenx = cols; view.screeny = rows; matrix_setView3D( &vtm, &view ); // use a temprary polygon to transform stuff polygon_init( &tpoly ); printf("Drawing pyramid\n"); x = -2; int j; for (j=0; j<50; j++){ // create image src = image_create( rows, cols ); point_set3D(&(view.vrp), x , 2, 0.5); vector_set( &(view.vpn), -x, -2, -0.5); matrix_setView3D(&vtm, &view); i=0; for(i=0;i<4;i++) { polygon_copy( &tpoly, &side[i] ); matrix_xformPolygon( &vtm, &tpoly ); // normalize by homogeneous coordinate before drawing polygon_normalize( &tpoly ); polygon_draw( &tpoly, src, color[i] ); //polygon_print( &tpoly, stdout ); } printf("Writing image\n"); sprintf(filename, "pyramid_%04d.ppm", j); image_write( src, filename); free(src); if(j<25){ x += 0.08; }else{ x -= 0.08; } } printf("Making the .gif file...\n"); system("convert -delay 10 pyramid_*.ppm ../images/ext1.gif"); system("rm -f pyramid*"); return(0); }
int main(int argc, char *argv[]) { const int rows = 180; const int cols = 320; const int nFrames = 100; View3D view; Matrix vtm; Polygon side[6]; Polygon tpoly; Point tv[4]; Point v[8]; Color color[6]; Image *src; int i, t; char filename[256]; // set some colors color_set( &color[0], 0, 0, 1 ); color_set( &color[1], 0, 1, 0 ); color_set( &color[2], 1, 0, 0 ); color_set( &color[3], 1, 0, 1 ); color_set( &color[4], 0, 1, 1 ); color_set( &color[5], 1, 1, 0 ); for(t=0;t<nFrames;t++) { // initialize polygons for(i=0;i<6;i++) { polygon_init( &side[i] ); } // corners of a cube, centered at (0, 0, 0) point_set1( &v[0], -1, -1, -1 ); point_set1( &v[1], 1, -1, -1 ); point_set1( &v[2], 1, 1, -1 ); point_set1( &v[3], -1, 1, -1 ); point_set1( &v[4], -1, -1, 1 ); point_set1( &v[5], 1, -1, 1 ); point_set1( &v[6], 1, 1, 1 ); point_set1( &v[7], -1, 1, 1 ); // front side polygon_set( &side[0], 4, &(v[0]) ); // back side polygon_set( &side[1], 4, &(v[4]) ); // top side point_copy( &tv[0], &v[2] ); point_copy( &tv[1], &v[3] ); point_copy( &tv[2], &v[7] ); point_copy( &tv[3], &v[6] ); polygon_set( &side[2], 4, tv ); // bottom side point_copy( &tv[0], &v[0] ); point_copy( &tv[1], &v[1] ); point_copy( &tv[2], &v[5] ); point_copy( &tv[3], &v[4] ); polygon_set( &side[3], 4, tv ); // left side point_copy( &tv[0], &v[0] ); point_copy( &tv[1], &v[3] ); point_copy( &tv[2], &v[7] ); point_copy( &tv[3], &v[4] ); polygon_set( &side[4], 4, tv ); // right side point_copy( &tv[0], &v[1] ); point_copy( &tv[1], &v[2] ); point_copy( &tv[2], &v[6] ); point_copy( &tv[3], &v[5] ); polygon_set( &side[5], 4, tv ); // grab command line argument to determine viewpoint // and set up the view structure /*if( argc > 1 ) { float alpha = atof( argv[1] ); if( alpha < 0.0 || alpha > 1.0 ) alpha = 0.0; point_set1( &(view.vrp), 3*alpha, 2*alpha, -2*alpha - (1.0-alpha)*3 ); } else { point_set1( &(view.vrp), 3, 2, -2 ); } */ float alpha = abs(t - (0.5*nFrames)) / (0.5*nFrames); point_set1( &(view.vrp), 3*alpha, 2*alpha, -2*alpha - (1.0-alpha)*3 ); 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; // focal length view.du = 2; view.dv = view.du * (float)rows / cols; view.f = 0; // front clip plane view.b = 4; // back clip plane view.screenx = cols; view.screeny = rows; matrix_setView3D( &vtm, &view ); // create image src = image_create( rows, cols ); // use a temprary polygon to transform stuff polygon_init( &tpoly ); //printf("Drawing Polygons\n"); for(i=0;i<6;i++) { polygon_copy( &tpoly, &side[i] ); matrix_xformPolygon( &vtm, &tpoly ); // normalize by homogeneous coordinate before drawing polygon_normalize( &tpoly ); polygon_draw( &tpoly, src, color[i] ); //polygon_print( &tpoly, stdout ); } sprintf(filename, "frame-%04d.ppm", t ); image_write( src, filename ); /*printf("Writing image\n"); image_write( src, "cube.ppm" );*/ } system("convert frame-*.ppm cube.gif"); system("rm frame-*.ppm"); return(0); }
/* * Draw the module into the image using the given view transformation matrix [VTM], * Lighting and DrawState by traversing the list of Elements. * (For now, Lighting can be an empty structure.) */ void module_draw(Module *md, Matrix *VTM, Matrix *GTM, DrawState *ds, Lighting *lighting, Image *src){ /* for antialiasing Module *thickLineMod = module_create(); Element *thickLineE; float dx, dy, dz, lineLength; Vector u, v, w;*/ // all locally needed variables Matrix LTM, tempGTM; Line tempLine; DrawState *tempds = drawstate_create(); Point tempPointLTM, tempPointGTM, tempPointVTM; Polyline *tempPolyline = polyline_create(); Polygon *tempPolygon = polygon_create(); Element *e = md->head; matrix_identity(<M); // loop until the end of the linked list is reached while(e){ //printf("Handling type %d\n", e->type); // draw based on type switch(e->type){ case ObjNone: break; case ObjPoint: //printf("drawing point "); // copy, xform, normalize, draw matrix_xformPoint(<M, &(e->obj.point), &tempPointLTM); matrix_xformPoint(GTM, &tempPointLTM, &tempPointGTM); matrix_xformPoint(VTM, &tempPointGTM, &tempPointVTM); point_normalize(&(tempPointVTM)); //point_print(&tempPointVTM, stdout); point_draw(&tempPointVTM, src, ds->color); break; case ObjLine: line_copy(&tempLine, &(e->obj.line)); //printf("drawing line "); // copy, xform, normalize, draw matrix_xformLine(<M, &tempLine); matrix_xformLine(GTM, &tempLine); matrix_xformLine(VTM, &tempLine); line_normalize(&tempLine); line_draw(&tempLine, src, ds->color); //line_print(&tempLine, stdout); /*if(antialias){ dx = tempLine.b.val[0]-tempLine.a.val[0]; dy = tempLine.b.val[1]-tempLine.a.val[1]; dz = tempLine.b.val[2]-tempLine.a.val[2]; lineLength = sqrt(dx*dx+dy*dy+dz*dz); module_scale( thickLineMod, 1, lineLength, 1 ); vector_set(&v, dx, dy, dz); vector_normalize(&v); vector_set(&u, -dz, dx, dy); vector_cross(&u, &v, &w); vector_cross(&v, &w, &u); vector_normalize(&u); vector_normalize(&w); module_rotateXYZ( thickLineMod, &u, &v, &w ); module_translate( thickLineMod, tempLine.a.val[0], tempLine.a.val[1], tempLine.a.val[2] ); module_cylinder( thickLineMod, 4, 1, 1, 0, 0, 0 ); thickLineE = element_init(ObjModule, thickLineMod); thickLineE->next = e->next; e->next = thickLineE; }*/ break; case ObjPolyline: //printf("drawing polyline "); // copy, xform, normalize, draw polyline_copy(tempPolyline, &(e->obj.polyline)); matrix_xformPolyline(<M, tempPolyline); matrix_xformPolyline(GTM, tempPolyline); matrix_xformPolyline(VTM, tempPolyline); polyline_normalize(tempPolyline); //polyline_print(tempPolyline, stdout); polyline_draw(tempPolyline, src, ds->color); break; case ObjPolygon: //printf("drawing polygon "); // copy, xform, normalize, draw polygon_copy(tempPolygon, &(e->obj.polygon)); matrix_xformPolygon(<M, tempPolygon); matrix_xformPolygon(GTM, tempPolygon); matrix_xformPolygon(VTM, tempPolygon); polygon_normalize(tempPolygon); //polygon_print(tempPolygon, stdout); polygon_draw(tempPolygon, src, ds); break; case ObjColor: drawstate_setColor(ds, e->obj.color); break; case ObjBodyColor: break; case ObjSurfaceColor: break; case ObjSurfaceCoeff: break; case ObjLight: break; case ObjIdentity: matrix_identity(<M); break; case ObjMatrix: matrix_multiply(&(e->obj.matrix), <M, <M); break; case ObjModule: matrix_multiply(GTM, <M, &tempGTM); drawstate_copy(tempds, ds); module_draw(e->obj.module, VTM, &tempGTM, tempds, lighting, src); break; default: printf("ObjectType %d is not handled in module_draw\n",e->type); } // advance traversal e = e->next; } // clean up polygon_free(tempPolygon); polyline_free(tempPolyline); free(tempds); }