Back to index

radiance  4R0+20100331
genclock.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: genclock.c,v 2.5 2003/06/08 12:03:09 schorsch Exp $";
00003 #endif
00004 /*
00005  * Generate an analog clock.
00006  */
00007 
00008 #include <stdio.h>
00009 #include <stdlib.h>
00010 #include <math.h>
00011 #include <ctype.h>
00012 
00013 #define PI           3.14159265358979323846
00014 
00015 #define FACEBITMAP   "clockface.hex"
00016 
00017 char   myfacemat[] = "white_plastic";
00018 double myfacearg[5] = {.85,.85,.85,0,0};
00019 char   mycasemat[] = "black_plastic";
00020 double mycasearg[5] = {.08,.08,.08,.03,.04};
00021 char   *facemat = myfacemat;
00022 char   *casemat = mycasemat;
00023 char   *name = "clock";
00024 
00025 static void
00026 genmats()                          /* put out our materials */
00027 {
00028        if (facemat == myfacemat)
00029               printf("\nvoid plastic %s\n0\n0\n5 %f %f %f %f %f\n",
00030                             myfacemat, myfacearg[0], myfacearg[1],
00031                             myfacearg[2], myfacearg[3], myfacearg[4]);
00032        if (casemat == mycasemat)
00033               printf("\nvoid plastic %s\n0\n0\n5 %f %f %f %f %f\n",
00034                             mycasemat, mycasearg[0], mycasearg[1],
00035                             mycasearg[2], mycasearg[3], mycasearg[4]);
00036        printf("\n%s brighttext clock_face_paint\n2 hexbit4x1.fnt %s\n",
00037                      facemat, FACEBITMAP);
00038        printf("0\n11\n\t0\t-1\t1\n\t0\t.0185\t0\n\t0\t0\t-.00463\n");
00039        printf("\t.02\t1\n");
00040        printf("\nvoid glass clock_crystal\n0\n0\n3 .95 .95 .95\n");
00041        printf("\nvoid plastic hand_paint\n0\n0\n5 .03 .03 .03 0 0\n");
00042 }
00043 
00044 
00045 static void
00046 genclock()                         /* put out clock body */
00047 {
00048        printf("\n%s ring %s.case_back\n", casemat, name);
00049        printf("0\n0\n8\t0\t0\t0\n\t-1\t0\t0\n\t0\t1.1\n");
00050        printf("\n%s cylinder %s.case_outer\n", casemat, name);
00051        printf("0\n0\n7\t0\t0\t0\n\t.12\t0\t0\n\t1.1\n");
00052        printf("\n%s ring %s.case_front\n", casemat, name);
00053        printf("0\n0\n8\t.12\t0\t0\n\t1\t0\t0\n\t1\t1.1\n");
00054        printf("\n%s cylinder %s.case_inner\n", casemat, name);
00055        printf("0\n0\n7\t.05\t0\t0\n\t.12\t0\t0\n\t1\n");
00056        printf("\nclock_crystal ring %s.crystal\n", name);
00057        printf("0\n0\n8\t.10\t0\t0\n\t1\t0\t0\n\t0\t1\n");
00058        printf("\nclock_face_paint ring %s.face\n", name);
00059        printf("0\n0\n8\t.05\t0\t0\n\t1\t0\t0\n\t0\t1\n");
00060 }
00061 
00062 
00063 static void
00064 rvert(x, y, z, ang)                /* print rotated vertex */
00065 double x, y, z, ang;
00066 {
00067        static double lastang=0, sa=0, ca=1;
00068 
00069        if (ang != lastang) {
00070               sa = sin(-ang);
00071               ca = cos(-ang);
00072               lastang = ang;
00073        }
00074        printf("%15.12g %15.12g %15.12g\n", x, y*ca-z*sa, z*ca+y*sa);
00075 }
00076 
00077 
00078 static void
00079 genhands(hour)                            /* generate correct hand positions */
00080 double hour;
00081 {
00082        double hrot, mrot;
00083 
00084        hrot = 2.*PI/12. * hour;
00085        mrot = 2.*PI * (hour - floor(hour));
00086 
00087        printf("\nhand_paint polygon %s.hour_hand\n", name);
00088        printf("0\n0\n12\n");
00089        rvert(.06, -.03, -.06, hrot);
00090        rvert(.06, .03, -.06, hrot);
00091        rvert(.06, .025, .5, hrot);
00092        rvert(.06, -.025, .5, hrot);
00093 
00094        printf("\nhand_paint polygon %s.minute_hand\n", name);
00095        printf("0\n0\n12\n");
00096        rvert(.07, -.02, -.1, mrot);
00097        rvert(.07, .02, -.1, mrot);
00098        rvert(.07, .01, .9, mrot);
00099        rvert(.07, -.01, .9, mrot);
00100 }
00101 
00102 
00103 int
00104 main(argc, argv)
00105 int    argc;
00106 char   *argv[];
00107 {
00108        int    i, j;
00109        double hour;
00110 
00111        for (i = 1; i < argc && argv[i][0] == '-'; i++)
00112               switch (argv[i][1]) {
00113               case 'f':
00114                      facemat = argv[++i];
00115                      break;
00116               case 'c':
00117                      casemat = argv[++i];
00118                      break;
00119               case 'n':
00120                      name = argv[++i];
00121                      break;
00122               default:
00123                      goto userr;
00124               }
00125        if (i >= argc)
00126               goto userr;
00127        if (!isdigit(argv[i][0]))
00128               goto userr;
00129        for (j = 1; isdigit(argv[i][j]); j++)
00130               ;
00131        if (argv[i][j] == ':')
00132               hour = atoi(argv[i]) + atoi(argv[i]+j+1)/60.0;
00133        else if (!argv[i][j] || argv[i][j] == '.')
00134               hour = atof(argv[i]);
00135        else
00136               goto userr;
00137        putchar('#');               /* print header */
00138        for (i = 0; i < argc; i++) {
00139               putchar(' ');
00140               fputs(argv[i], stdout);
00141        }
00142        putchar('\n');
00143        genmats();                  /* print materials */
00144        genclock();                 /* generate clock */
00145        genhands(hour);                    /* generate hands */
00146        exit(0);
00147 userr:
00148        fputs("Usage: ", stderr);
00149        fputs(argv[0], stderr);
00150        fputs(" [-f face_mat][-c case_mat][-n name] {HH:MM | HH.hh}\n", stderr);
00151        exit(1);
00152 }
00153