// draw some random lines, then two boxes int main(int argc, char *argv[]) { const int nPoints = 1000; Polyline thing1; Polyline *thing2; Point p[nPoints]; int i; int rows = 500; int cols = 500; Image *src; Color Blue; Color Red; Color Green; Color_set( &Blue, 0.1, 0.15, 0.7 ); Color_set( &Red, 0.8, 0.2, 0.1 ); Color_set( &Green, 0.2, 0.6, 0.2 ); printf("Startup\n"); for(i=0;i<nPoints;i++) { point_set2D( &(p[i]), drand48()*cols/2, drand48()*rows/2 ); } // init is necessary, because otherwise the fields are full of garbage printf("Polyline init and set\n"); polyline_init( &thing1 ); polyline_set( &thing1, 20, &( p[42] ) ); printf("Polyline create\n"); thing2 = polyline_createp( 10, &(p[105] ) ); printf("Creating image\n"); src = image_create( rows, cols ); printf("Drawing poly-lines\n"); // draw some random lines polyline_draw( &thing1, src, Blue ); polyline_draw( thing2, src, Red ); // draw a box going counter-clockwise, should be ok point_set2D( &(p[500]), 50, 300 ); point_set2D( &(p[501]), 50, 350 ); point_set2D( &(p[502]), 100, 350 ); point_set2D( &(p[503]), 100, 300 ); point_set2D( &(p[504]), 50, 300 ); printf("Counter-clockwise box\n"); polyline_set( thing2, 5, &(p[500]) ); polyline_draw( thing2, src, Green ); // draw a box going clockwise, should not be ok point_set2D( &(p[500]), 350, 300 ); point_set2D( &(p[501]), 400, 300 ); point_set2D( &(p[502]), 400, 350 ); point_set2D( &(p[503]), 350, 350 ); point_set2D( &(p[504]), 350, 300 ); printf("Clockwise box\n"); polyline_set( thing2, 5, &(p[500]) ); polyline_draw( thing2, src, Green ); image_write( src, "test3c.ppm"); printf("Cleanup\n"); polyline_clear( &thing1 ); printf("clear\n"); polyline_free( thing2 ); printf("free\n"); image_free( src ); 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); }