#include #include #include #include extern const char *__progname; #include "3d.h" #include "model.h" static MODEL *m; static int matx; static int lx[12]; static PT3 xyz_to_pt3(XYZ i) { return((PT3){i.x,i.y,i.z}); } static XYZ pt3_to_xyz(PT3 i) { return((XYZ){i.x,i.y,i.z}); } static int addloc(double x, double y, double z) { int lx; LOCATION *l; lx = new_location(m); l = get_location(m,lx); l->l.x = x; l->l.y = y; l->l.z = z; return(lx); } static int ptx(int lx, PT3 normal) { int px; POINT *p; px = new_point(m); p = get_point(m,px); p->loc = lx; p->type = PT_SURFACE; p->normal = pt3_to_xyz(normal); return(px); } static void tri(int pt, int pa, int pb) { TRIANGLE *t; t = get_triangle(m,new_triangle(m)); t->material = matx; t->corner[0] = pt; t->corner[1] = pa; t->corner[2] = pb; #if 0 printf("triangle %d: (%g,%g,%g) - (%g,%g,%g) - (%g,%g,%g)\n", t->inx, get_location(m,get_point(m,pt)->loc)->l.x, get_location(m,get_point(m,pt)->loc)->l.y, get_location(m,get_point(m,pt)->loc)->l.z, get_location(m,get_point(m,pa)->loc)->l.x, get_location(m,get_point(m,pa)->loc)->l.y, get_location(m,get_point(m,pa)->loc)->l.z, get_location(m,get_point(m,pb)->loc)->l.x, get_location(m,get_point(m,pb)->loc)->l.y, get_location(m,get_point(m,pb)->loc)->l.z); #endif } static void face(int p1, int p2, int p3) { PT3 normal; normal = unit3(cross( sub3(xyz_to_pt3(get_location(m,p1)->l),xyz_to_pt3(get_location(m,p2)->l)), sub3(xyz_to_pt3(get_location(m,p3)->l),xyz_to_pt3(get_location(m,p2)->l)) )); tri(ptx(lx[p1],normal),ptx(lx[p2],normal),ptx(lx[p3],normal)); } static void addmark(int at, const char *text) { MARK *k; k = get_mark(m,new_mark(m)); k->at = at; k->text = strdup(text); } int main(int, char **); int main(int ac, char **av) { MATERIAL *mat; OBJECT *o; int i; int exact; do { if (ac == 2) { if (!strcmp(av[1],"-exact")) { exact = 1; break; } else if (!strcmp(av[1],"-loose")) { exact = 0; break; } } fprintf(stderr,"Usage: %s [-exact|-loose]\n",__progname); exit(1); } while (0); m = new_model(); matx = new_material(m); mat = get_material(m,matx); mat->name = strdup("M"); mat->selflum = (RGB){0,0,0}; mat->reflect = (RGB){.7,.7,.7}; mat->spec_exp = 10; mat->spec_mul = .5; if (exact) { /* 2 1 0 0 0 0 2 1 1 6 1 0 2 2 6 2 -1 0 3 4 0 2 -1 4 6 -1 0 2 5 14 -2 1 0 6 16 0 -2 1 7 14 1 0 -2 8 6 -2 -1 0 9 20 0 -2 -1 10 14 -1 0 -2 11 14 */ lx[ 0] = addloc(2,1,0); lx[ 1] = addloc(0,2,1); lx[ 2] = addloc(1,0,2); lx[ 3] = addloc(2,-1,0); lx[ 4] = addloc(0,2,-1); lx[ 5] = addloc(-1,0,2); lx[ 6] = addloc(-2,1,0); lx[ 7] = addloc(0,-2,1); lx[ 8] = addloc(1,0,-2); lx[ 9] = addloc(-2,-1,0); lx[10] = addloc(0,-2,-1); lx[11] = addloc(-1,0,-2); addmark(lx[0],"0"); addmark(lx[1],"1"); addmark(lx[2],"2"); addmark(lx[3],"3"); addmark(lx[4],"4"); addmark(lx[5],"5"); addmark(lx[6],"6"); addmark(lx[7],"7"); addmark(lx[8],"8"); addmark(lx[9],"9"); addmark(lx[10],"10"); addmark(lx[11],"11"); #if 0 { void plen(int a, int b) { printf("@ %d %d -> %g\n",a,b,norm3(sub3(xyz_to_pt3(get_location(m,a)->l),xyz_to_pt3(get_location(m,b)->l)))); } plen(0,1); plen(0,2); plen(0,4); plen(0,6); plen(0,8); plen(1,2); plen(1,5); plen(1,6); plen(1,7); plen(10,11); plen(2,3); plen(2,7); plen(2,8); plen(3,10); plen(3,7); plen(3,8); plen(3,9); plen(4,10); plen(4,11); plen(4,6); plen(4,8); plen(5,11); plen(5,6); plen(5,7); plen(5,9); plen(6,11); plen(7,9); plen(8,10); plen(9,10); plen(9,11); } #endif face(0,2,1); face(0,8,2); face(3,2,8); face(0,4,8); face(4,0,6); face(1,6,0); face(6,1,5); face(2,7,1); face(5,1,7); face(7,2,3); face(7,3,9); face(10,9,3); face(10,3,8); face(11,10,4); face(8,4,10); face(11,4,6); face(9,5,7); face(9,10,11); face(6,5,11); face(9,11,5); } else { double sqrt5; double r; double h; double s36; double c36; double s72; double c72; double s144; double c144; sqrt5 = sqrt(5); c36 = (sqrt5 + 1) / 4; s36 = sqrt((5-sqrt5)/8); c72 = (sqrt5 - 1) / 4; s72 = sqrt((5+sqrt5)/8); c144 = - c36; s144 = s36; h = 1 / sqrt5; r = 2 / sqrt5; lx[ 0] = addloc(0,0,1); lx[ 1] = addloc(r,0,h); lx[ 2] = addloc(r*c72,r*s72,h); lx[ 3] = addloc(r*c144,r*s144,h); lx[ 4] = addloc(r*c144,-r*s144,h); lx[ 5] = addloc(r*c72,-r*s72,h); lx[ 6] = addloc(0,0,-1); lx[ 7] = addloc(-r,0,-h); lx[ 8] = addloc(-r*c72,r*s72,-h); lx[ 9] = addloc(-r*c144,r*s144,-h); lx[10] = addloc(-r*c144,-r*s144,-h); lx[11] = addloc(-r*c72,-r*s72,-h); } #if 0 face(0,1,4); face(4,1,5); face(0,4,3); face(0,3,8); face(0,2,1); face(1,2,10); face(10,2,9); face(2,8,9); face(8,2,0); face(3,7,8); face(7,3,11); face(11,3,4); face(11,4,5); face(5,10,6); face(10,5,1); face(6,11,5); face(6,10,9); face(6,9,7); face(7,9,8); face(6,7,11); #endif o = get_object(m,new_object(m)); o->ntriangles = n_triangles(m); o->triangles = malloc(o->ntriangles*sizeof(*o->triangles)); for (i=o->ntriangles-1;i>=0;i--) o->triangles[i] = i; save_model(m,stdout); return(0); }