home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / TC_3D.ZIP / TC-18.C < prev   
Text File  |  1991-02-13  |  22KB  |  529 lines

  1. /* tc-18.c */
  2. /* Draws a container design (soft drink can) */
  3.  
  4. /* ----------------------------------------------------------------------- */
  5. /* INCLUDE FILES */
  6. #include <process.h>
  7. #include <bios.h>
  8. #include <stdio.h>
  9. #include <graphics.h>
  10. #include <math.h>
  11.  
  12. /* ----------------------------------------------------------------------- */
  13. /* DECLARATIONS */
  14.  
  15. float        x=0.0,y=0.0,z=0.0;                                              /* world coordinates */
  16. float        x1=0.0,x2=0.0,x3=0.0;                                        /* polygon vertices */
  17. float        y01=0.0,y2=0.0,y3=0.0;                                        /* polygon vertices */
  18. float        z1=0.0,z2=0.0,z3=0.0;                                        /* polygon vertices */
  19. float        sx1=0.0,sx2=0.0,sx3=0.0,sx4=0.0,sx5=0.0;                /* display coordinaes */
  20. float        sy1=0.0,sy2=0.0,sy3=0.0,sy4=0.0,sy5=0.0;                /* display coordinates */
  21. float        sx=0.0,sy=0.0;                                                    /* output of 3d perspective formulas */
  22. float        xa=0.0,ya=0.0,za=0.0;                                        /* temporary values in 3d formulas */
  23. float        d=1200.0;                                                        /* angular perspective value */
  24. double    r1=5.09448;                                                        /* yaw angle in radians */
  25. double    r2=5.09448;                                                        /* roll angle in radians */
  26. double    r3=6.28319;                                                        /* pitch angle in radians */
  27. double    sr1=0.0,sr2=0.0,sr3=0.0;                                    /* sine rotation factors */
  28. double    cr1=0.0,cr2=0.0,cr3=0.0;                                    /* cosine rotation factors */
  29. float        mx=0.0,my=-10.0,mz=-350.0;                                    /* viewpoint position */
  30. int        maxx=639,minx=0,maxy=199,miny=0;                            /* scaling viewport */
  31. float        screen_x=639,screen_y=199;                                    /* dimensions of screen mode */
  32. int        C0=0,C1=1,C2=2,C3=3,C4=4,C5=5,C6=6,C7=7,C8=8,        /* color varariables */
  33.             C9=9,C10=10,C11=11,C12=12,C13=13,C14=14,C15=15,
  34.             mode_flag=0;
  35. float        rx=0.0,ry=0.0;                                                    /* scaling values used in mapping routine */
  36. int        t=0,t1=0,t2=0;                                                    /* loop counters */
  37. int        p1=0;                                                                /* array indexer */
  38. int        key_matte_clr=6;                                                /* exclusive key matte color */
  39. int        edge_clr=7;                                                        /* used to draw edges on models */
  40. int        solid_clr=0;                                                    /* used to fill surfaces on solid models */
  41. float        x_res,y_res;                                                    /* used for 2d mappinf from 640*480 template */
  42. float        sp=0.0;                                                            /* visibility factor in hidden surface routine */
  43. float        sp1=0.0,sp2=0.0,sp3=0.0;                                    /* temporary values of sp */
  44.  
  45. int        NORM_HUE=0;                                                        /* halftoning color */
  46. int        BASE_CLR=0;                                                        /* prep cooor for dithering */
  47. int        CC4=0;                                                            /* underlay color for dithering */
  48. int        CC5=0;                                                            /* overlay color for dithering */
  49. unsigned short CC6=0xffff;                                                /* pattern for dithering */
  50. float        xx1=0,yy1=.0,zz1=1;                                            /* location of light source - elevation 45 degrees, compas 135 degrees {{REDIFINED !!}}*/
  51. float        v4=0.0;                                                            /* illumination factor 0 to 1 range */
  52. int        v5=0;                                                                /* illumination factor base 1 range */
  53. int        v6=11;                                                            /* VGA and EGA illumination range */
  54. int        v7=0;                                                                /* temporary varaible used for illumination range */
  55. float        xu=0.0,yu=0.0,zu=0.0;                                        /* vector from vertex one to vertex two */
  56. float        xv=0.0,yv=0.0,zv=0.0;                                        /* vector from vertex 1 to vertex 3 */
  57. float        xn=0.0,y0n=0.0,zn=0.0;                                        /* surface perpendicular factor */
  58. float        v1=0.0,v2=0.0;                                                    /* length of surface perpendicular vector */
  59. float        v3=0.0;                                                            /* ratio of length to unit vector magnitude */
  60. float        xw=0.0,yw=0.0,zw=0.0;                                        /* surface perpendicual unit vector */
  61. int        polary[4][2];  /* used in my 3d solid polygon draw routine */
  62.  
  63. /* define halftone codes */
  64. char fill_0[]={0,0,0,0,0,0,0,0};                                        /* 0% fill */
  65. char fill_3[]={0,32,0,0,0,2,0,0};                                    /* 3% fill */
  66. char fill_6[]={32,0,2,0,128,0,8,0};                                    /* 6% fill */
  67. char fill_12[]={32,2,128,8,32,2,128,8};                            /* 12% fill */
  68. char fill_25[]={68,17,68,17,68,17,68,17};                            /* 25% fill */
  69. char fill_37[]={170,68,170,17,170,68,170,17};                    /* 37% fill */
  70. char fill_50[]={85,170,85,170,85,170,85,170};                    /* 50% fill */
  71. char fill_62[]={85,187,85,238,85,187,85,238};                    /* 62% fill */
  72. char fill_75[]={187,238,187,238,187,238,187,238};                /* 75% fill */
  73. char fill_87[]={223,253,127,247,223,253,127,247};                /* 87% fill */
  74. char fill_93[]={255,223,255,223,255,223,255,223};                /* 93% fill */
  75. char fill_100[]={255,255,255,255,255,255,255,255};                /* 100% fill */
  76.  
  77. /* database of z coordinates on surface of container */
  78. float    database_1[]={2,2,1,-1,-2.5,-3.8,-4.8,-6,-6.7,-7.5,-8,-8.5,
  79.         -8.8,-9.2,-10,-10.2,-10.9,-11,-11.2,-11.5,-11.7,
  80.         -11.9,-12,-12,-11.9,-11.7,-11.5,-11.2,-11,-10.9,
  81.         -10.2,-10,-9.2,-8.8,-8.5,-8,-7.5,-6.7,-6,-4.8,
  82.         -3.8,-2.5,-1,1,2};
  83.  
  84. float    database_2[]={26,26,28,29.2,31,32,33,34,35,35.8,36.5,37,37.2,
  85.         37.5,37.8,38,38.2,38.3,38.5,38.6,38.6,38.6,38.5,
  86.         38.3,38.2,38,37.8,37.5,37.2,37,36.5,35.8,35,
  87.         34,33,32,31,29.2,27.5,25.2,23};
  88.  
  89. float    database_3[]={26,26,25.5,25,24.5,24.2,24,23.8,23.7,23.7,23.7,
  90.         23.7,23.7,23.7,23.8,23.9,23.9,24,24.1,24.2,24.5,
  91.         24.8,25.1,25.3,25.3,25.5,25.8,26,26,25.8,25.7,
  92.         25.6,25.5,25.2,25.1,25,24.5,24.3,23.8,23.7,23};
  93.  
  94. float    database_4[]={8.2,7.9,7.5,7.1,7,6.9,6.8,6.7,6.6,6.6,6.6,
  95.         6.7,6.7,6.7,6.8,6.9,7,7.1,7.3,7.5,8,8.2,8.6,
  96.         8.9,9,9.1,9.4,9.6,9.9,10,10,10,10,9.9,9.8,9.5,
  97.         9.4,9.1,8.9,8.4,8};
  98.  
  99. float    database_5[]={8.2,7,5.5,3.7,2,0,-1,-2,-2.8,-3.8,-4.5,-5.1,
  100.         -5.7,-5.9,-6.3,-6.7,-7,-7.2,-7.3,-7.4,-7.5,-7.5,
  101.         -7.4,-7.3,-7.1,-7,-6.7,-6.5,-6,-5.5,-4.8,-4,-3.2,
  102.         -2.5,-1.5,-.5,.5,2.1,3.5,6,8};
  103.  
  104. int        q=0,q1=0,q2=0;                                                    /* cylinder surface and vertex counters */
  105. double    r4=6.28319,r5=6.28319;                                        /* spherical coordinate angles */
  106. float        B11[36][3];                                                        /* 36 sets of xyz coordinates, near end */
  107. float        B12[36][3];                                                        /* 36 sets of xyz view coordinates, far end */
  108. float        B21[36][2];                                                        /* 36 sets of sx,sy display coordinates, near end */
  109. float        B22[36][2];                                                        /* 36 sets of sx,sy display coordinates, far end */
  110. double    sr4=0.0,cr4=0.0,sr5=0.0,cr5=0.0;                            /* rotation factors */
  111.  
  112. /* declare global subroutines */
  113. void keyboard(void);void quit_pgm(void);void calc_3d(void);
  114. void rotation(void);void window(void);void graphics_setup(void);
  115. void coords(void);void draw_poly(void);void notice(int x,int y);
  116. void illumination(void);void dither(void);void shade(void);
  117. void values(void);
  118. void visibility_test(void);void cyl_coords(void);
  119. void draw_surface(void);
  120.  
  121. /* ----------------------------------------------------------------------- */
  122. /* MAIN ROUTINE */
  123.  
  124. main(){
  125. graphics_setup();
  126. setviewport(0,0,maxx,maxy,1);
  127. key_matte_clr=C6;
  128. edge_clr=C7;solid_clr=C0;
  129. NORM_HUE=C7;
  130. rotation();
  131. if (mode_flag==4) my=0.0;                                                /* if cga */
  132.  
  133. /* airbrushed backgroung */
  134.  
  135. sx=170;sy=72 ;coords();sx1=sx;sy1=sy;sx=470;sy=98 ;coords();
  136. sx2=sx;sy2=sy;setfillpattern(fill_6 ,C4);bar(sx1,sy1,sx2,sy2);
  137. sx=170;sy=98 ;coords();sx1=sx;sy1=sy;sx=470;sy=132;coords();
  138. sx2=sx;sy2=sy;setfillpattern(fill_12,C4);bar(sx1,sy1,sx2,sy2);
  139. sx=170;sy=132;coords();sx1=sx;sy1=sy;sx=470;sy=168;coords();
  140. sx2=sx;sy2=sy;setfillpattern(fill_25,C4);bar(sx1,sy1,sx2,sy2);
  141. sx=170;sy=168;coords();sx1=sx;sy1=sy;sx=470;sy=204;coords();
  142. sx2=sx;sy2=sy;setfillpattern(fill_37,C4);bar(sx1,sy1,sx2,sy2);
  143. sx=170;sy=204;coords();sx1=sx;sy1=sy;sx=470;sy=240;coords();
  144. sx2=sx;sy2=sy;setfillpattern(fill_50,C4);bar(sx1,sy1,sx2,sy2);
  145. sx=170;sy=240;coords();sx1=sx;sy1=sy;sx=470;sy=276;coords();
  146. sx2=sx;sy2=sy;setfillpattern(fill_62,C4);bar(sx1,sy1,sx2,sy2);
  147. sx=170;sy=276;coords();sx1=sx;sy1=sy;sx=470;sy=312;coords();
  148. sx2=sx;sy2=sy;setfillpattern(fill_75,C4);bar(sx1,sy1,sx2,sy2);
  149. sx=170;sy=312;coords();sx1=sx;sy1=sy;sx=470;sy=348;coords();
  150. sx2=sx;sy2=sy;setfillpattern(fill_87,C4);bar(sx1,sy1,sx2,sy2);
  151. sx=170;sy=348;coords();sx1=sx;sy1=sy;sx=470;sy=384;coords();
  152. sx2=sx;sy2=sy;setfillpattern(fill_100,C4);bar(sx1,sy1,sx2,sy2);
  153.  
  154. /* calculate coordinates for near end of cylinder */
  155. r4=0;r5=0;
  156. for (t=0;t<=35;t++){
  157.     x=30;cyl_coords();z=z+50;calc_3d();window();
  158.     B11[t][0]=x;B11[t][1]=y;B11[t][2]=z;
  159.     B21[t][0]=sx;B21[t][1]=sy;
  160.     /* keyboard();*/
  161.     r5=r5+.17453;}
  162.  
  163. /* calculate coordinates for far end of cylinder */
  164. r4=0;r5=0;
  165. for (t=0;t<=35;t++){
  166.     x=30;cyl_coords();z=z-50;calc_3d();window();
  167.     B12[t][0]=x;B12[t][1]=y;B12[t][2]=z;
  168.     B22[t][0]=sx;B22[t][1]=sy;
  169.     /* keyboard(); */
  170.     r5=r5+.17453;}
  171.  
  172. /* draw surfaces of 3d cylinder */
  173. for (q1=0;q1<=35;q1++){
  174.     q2=q1+1;if (q2>35) q2=0;
  175.     draw_surface();
  176.     /* keyboard();*/
  177.     }
  178.  
  179. /* draw near end of 3d cylinder */
  180. x1=B11[0][0];y01=B11[0][1];z1=B11[0][2];
  181. x2=B11[25][0];y2=B11[25][1];z2=B11[25][2];
  182. x3=B11[11][0];y3=B11[11][1];z3=B11[11][2];
  183. visibility_test();if (sp>0) goto all_done;
  184. setcolor(key_matte_clr);setfillstyle(SOLID_FILL,key_matte_clr);
  185. setlinestyle(USERBIT_LINE,0xffff,NORM_WIDTH);
  186. for (q1=0;q1<=35;q1++){
  187.     q2=q1+1;if (q2>35) q2=0;
  188.     sx1=B21[q1][0];sy1=B21[q1][1];sx2=B21[q2][0];sy2=B21[q2][1];
  189.     moveto(sx1,sy1);lineto(sx2,sy2);}
  190. x=0.0;y=0.0;z=50.0;calc_3d();window();floodfill(sx,sy,key_matte_clr);
  191. setcolor(edge_clr);
  192.  
  193. for (q1=0;q1<=35;q1++){
  194.     q2=q1+1;if (q2>35) q2=0;
  195.     sx1=B21[q1][0];sy1=B21[q1][1];sx2=B21[q2][0];sy2=B21[q2][1];
  196.     moveto(sx1,sy1);lineto(sx2,sy2);}
  197.  
  198. x=0.0;y=0.0;z=50.0;calc_3d();window();
  199. setfillstyle(SOLID_FILL,solid_clr);floodfill(sx,sy,edge_clr);
  200. x=0.0;y=0.0;z=50.0;calc_3d();window();sx5=sx;sy5=sy;
  201. illumination();shade();keyboard();
  202. for (q1=0;q1<=35;q1++){
  203.     q2=q1+1;if (q2>35) q2=0;
  204.     sx1=B21[q1][0];sy1=B21[q1][1];sx2=B21[q2][0];sy2=B21[q2][1];
  205.     setlinestyle(USERBIT_LINE,0xffff,NORM_WIDTH);setcolor(CC4);
  206.     moveto(sx1,sy1);lineto(sx2,sy2);
  207.     setlinestyle(USERBIT_LINE,CC6,NORM_WIDTH);setcolor(CC5);
  208.     moveto(sx1,sy1);lineto(sx2,sy2);}
  209. setlinestyle(USERBIT_LINE,0xffff,NORM_WIDTH);
  210.  
  211. /* create lower red design */
  212. edge_clr=C4;
  213. if (mode_flag==4) edge_clr=C6;
  214. setcolor(edge_clr);
  215. x=30;r5=2.601631;cyl_coords();z=-40;calc_3d();window();
  216.     moveto(sx,sy);putpixel(sx,sy,edge_clr);                        /* set point */
  217. x=30;r5=2.601631;cyl_coords();z=2.5;calc_3d();window();
  218.     lineto(sx,sy);                                                            /* vertical line */
  219. x=30;r5=.490874;cyl_coords();z=-40;calc_3d();window();
  220.     moveto(sx,sy);putpixel(sx,sy,edge_clr);                        /* set point */
  221. x=30;r5=.490874;cyl_coords();z=2.5;calc_3d();window();
  222.     lineto(sx,sy);                                                            /* vertical line */
  223.  
  224. keyboard();
  225. x=30;r5=.490874;cyl_coords();z=-40;calc_3d();window();
  226.     moveto(sx,sy);putpixel(sx,sy,edge_clr);
  227. for (r5=.490874;r5<=2.601631;r5+=.0490873){
  228.     keyboard();x=30;cyl_coords();z=-40;calc_3d();window();
  229.     lineto(sx,sy);}                                                            /* bottom line */
  230.  
  231. p1=0;
  232. r5=.490874;x=30;cyl_coords();z=database_1[p1];p1++;
  233. calc_3d();window();moveto(sx,sy);putpixel(sx,sy,edge_clr);
  234. for (r5=.490874;r5<=2.601631;r5+=.0490873){
  235.     keyboard();x=30;cyl_coords();
  236.     z=database_1[p1];p1++;calc_3d();window();lineto(sx,sy);}
  237.  
  238. x=30;r5=1.570796;cyl_coords();z=-25;calc_3d();window();
  239. setfillstyle(SOLID_FILL,edge_clr);
  240. floodfill(sx,sy,edge_clr);                                                /* area fill */
  241.  
  242. /*----- create upper semi-circle design -----*/
  243. edge_clr=C4;
  244. if (mode_flag==4) edge_clr=C6;
  245. setcolor(edge_clr);
  246. p1=0;
  247. r5=.638136;x=30;cyl_coords();z=database_2[p1];p1++;
  248. calc_3d();window();moveto(sx,sy);putpixel(sx,sy,edge_clr);
  249. for (r5=.638136;r5<=2.552544;r5+=.0490873){
  250.     keyboard();x=30;cyl_coords();
  251.     z=database_2[p1];p1++;calc_3d();window();lineto(sx,sy);}
  252.  
  253. p1=0;
  254. r5=.638136;x=30;cyl_coords();z=database_3[p1];p1++;
  255. calc_3d();window();moveto(sx,sy);putpixel(sx,sy,edge_clr);
  256. for (r5=.638136;r5<=2.552544;r5+=.0490873){
  257.     keyboard();x=30;cyl_coords();
  258.     z=database_3[p1];p1++;calc_3d();window();lineto(sx,sy);}
  259.  
  260. x=30;r5=1.570796;cyl_coords();z=32;calc_3d();window();
  261. setfillstyle(SOLID_FILL,edge_clr);floodfill(sx,sy,edge_clr);
  262.  
  263. /*----- create central semi-circle design -----*/
  264. edge_clr=C1;
  265. if (mode_flag==4) edge_clr=C6;
  266. setcolor(edge_clr);
  267. p1=0;
  268. r5=.638136;x=30;cyl_coords();z=database_4[p1];p1++;
  269. calc_3d();window();moveto(sx,sy);putpixel(sx,sy,edge_clr);
  270. for (r5=.638136;r5<=2.552544;r5+=.0490873){
  271.     keyboard();x=30;cyl_coords();
  272.     z=database_4[p1];p1++;calc_3d();window();lineto(sx,sy);}
  273.  
  274. p1=0;
  275. r5=.638136;x=30;cyl_coords();z=database_5[p1];p1++;
  276. calc_3d();window();moveto(sx,sy);putpixel(sx,sy,edge_clr);
  277. for (r5=.638136;r5<=2.552544;r5+=.0490873){
  278.     keyboard();x=30;cyl_coords();
  279.     z=database_5[p1];p1++;calc_3d();window();lineto(sx,sy);}
  280.  
  281. x=30;r5=1.570796;cyl_coords();z=0;calc_3d();window();
  282. setfillstyle(SOLID_FILL,edge_clr);floodfill(sx,sy,edge_clr);
  283.  
  284. all_done:
  285. setcolor(C7);notice (0,0);
  286. for (t1=1;t1!=2;) keyboard();
  287. quit_pgm;
  288. }
  289.  
  290. /* ----------------------------------------------------------------------- */
  291. /* SUBROUTINE: CALCULATE WORLD COORDINATES FOR cylinder */
  292.  
  293. void cyl_coords(void){
  294. sr4=sin(r4);cr4=cos(r4);sr5=sin(r5);
  295. cr5=cos(r5);x1=sr5*x;y=cr5*x;x=cr4*x1;z=sr4*x1;return;}
  296.  
  297. /* ----------------------------------------------------------------------- */
  298. /* SUBROUTINE: DRAW 4 SIDED POLYGON SURFACE ON SPHERE */
  299.  
  300. void draw_surface(void){
  301. x1=B11[q1][0];y01=B11[q1][1];z1=B11[q1][2];
  302. x2=B11[q2][0];y2=B11[q2][1];z2=B11[q2][2];
  303. x3=B12[q2][0];y3=B12[q2][1];z3=B12[q2][2];
  304. visibility_test();if (sp>0) return;
  305. sx1=B21[q1][0];sy1=B21[q1][1];
  306. sx2=B21[q2][0];sy2=B21[q2][1];
  307. sx3=B22[q2][0];sy3=B22[q2][1];
  308. sx4=B22[q1][0];sy4=B22[q1][1];
  309. x=x1+.5*(x3-x1);y=y01+.5*(y3-y01);z=z1+.5*(z3-z1);
  310. sx=d*x/z;sy=d*y/z;window();sx5=sx;sy5=sy;
  311. draw_poly();illumination();shade();dither();
  312. return;}
  313.  
  314. /* ----------------------------------------------------------------------- */
  315. /* SUBROUTINE: CALCULATE SIN,COS FACTORS */
  316.  
  317. void rotation(void){
  318. sr1=sin(r1);sr2=sin(r2);sr3=sin(r3);cr1=cos(r1);cr2=cos(r2);
  319. cr3=cos(r3);return;}
  320.  
  321. /* ----------------------------------------------------------------------- */
  322. /* SUBROUTINE: STANDARD 3D FORMULAS */
  323. /* Pass: x,y,z cartesian world coordinates.
  324.    Returns: sx,sy cartesian display coordinates.
  325.           x,y,z catesian view coordinates */
  326.  
  327. void calc_3d(void){
  328. x=(-1)*x;xa=cr1*x-sr1*z;za=sr1*x+cr1*z;x=cr2*xa+sr2*y;
  329. ya=cr2*y-sr2*xa;z=cr3*za-sr3*ya;y=sr3*za+cr3*ya;x=x+mx;y=y+my;
  330. z=z+mz;sx=d*x/z;sy=d*y/z;return;}
  331.  
  332. /* ----------------------------------------------------------------------- */
  333. /* HIDDEN SURFACE VISIBILTY TEST */
  334. void visibility_test(void){
  335. sp1=x1*(y2*z3-y3*z2);sp1=(-1)*sp1;sp2=x2*(y3*z1-y01*z3);
  336. sp3=x3*(y01*z2-y2*z1);sp=sp1-sp2-sp3;return;}
  337.  
  338. /* ----------------------------------------------------------------------- */
  339. /* SUBROUTINE: DRAW 4-SIDED SOLID POLYGON IN 3D SPACE */
  340.  
  341. void draw_poly(void){
  342. setlinestyle(USERBIT_LINE,0xffff,NORM_WIDTH);
  343. setfillstyle(SOLID_FILL,solid_clr);
  344. setcolor(edge_clr);
  345.  
  346. polary[0][0]=sx1;polary[0][1]=sy1;polary[1][0]=sx2;polary[1][1]=sy2;
  347. polary[2][0]=sx3;polary[2][1]=sy3;polary[3][0]=sx4;polary[3][1]=sy4;
  348.  
  349. fillpoly (4,(int far*) polary);
  350. return;
  351. }
  352.  
  353. /* ----------------------------------------------------------------------- */
  354. /* SUBROUTINES: CALCULATE ILLUMINATION LEVEL */
  355.  
  356. void illumination(void){
  357. xu=x2-x1;yu=y2-y01;zu=z2-z1;                                        /* vector from vertex 1 to vertex 2 */
  358. xv=x3-x1;yv=y3-y01;zv=z3-z1;                                        /* vector from vertex 1 to vertex 3 */
  359. xn=(yu*zv)-(zu*yv);y0n=(zu*xv)-(xu*zv);
  360.     zn=(xu*yv)-(yu*xv);                                                /* surface perpendicular vector */
  361. y0n=y0n*(-1);zn=zn*(-1);                                            /* convert to cartesian system */
  362. v1=(xn*xn)+(y0n*y0n)+(zn*zn);
  363. v2=sqrt(v1);                                                            /* magnitude of surface perpendicular vector */
  364. v3=1/v2;                                                                    /* ratio of magnitude to length of unit vector */
  365. xw=v3*xn;yw=v3*y0n;zw=v3*zn;                                        /* surface perpendicular to unit vector */
  366. v4=(xw*xx1)+(yw*yy1)+(zw*zz1);                                    /* illumination factor 0 to 1 */
  367. v4=v4*v6;                                                                /* expand illumination range from base 0 */
  368. v7=v4;                                                                    /* convert illumination range to integer */
  369. v5=v7+1;                                                                    /* illumination range from base 1 */
  370. return;
  371. }
  372.  
  373. /* ----------------------------------------------------------------------- */
  374. /* SUBROUTINE: ILLUMINATION MATRIX */
  375.  
  376. void shade(void){
  377. switch (v5){
  378.     case 1:setfillpattern(fill_6,NORM_HUE);CC6=0x1010;values();return;
  379.     case 2:setfillpattern(fill_6,NORM_HUE);CC6=0x1010;values();return;
  380.     case 3:setfillpattern(fill_6,NORM_HUE);CC6=0x1010;values();return;
  381.     case 4:setfillpattern(fill_12,NORM_HUE);CC6=0x2020;values();return;
  382.     case 5:setfillpattern(fill_25,NORM_HUE);CC6=0x2222;values();return;
  383.     case 6:setfillpattern(fill_37,NORM_HUE);CC6=0xaaaa;values();return;
  384.     case 7:setfillpattern(fill_50,NORM_HUE);CC6=0xaaaa;values();return;
  385.     case 8:setfillpattern(fill_62,NORM_HUE);CC6=0xaaaa;values();return;
  386.     case 9:setfillpattern(fill_75,NORM_HUE);CC6=0xbbbb;values();return;
  387.     case 10:setfillpattern(fill_87,NORM_HUE);CC6=0xdddd;values();return;
  388.     case 11:setfillpattern(fill_93,NORM_HUE);CC6=0xefef;values();return;
  389.     case 12:setfillpattern(fill_100,NORM_HUE);CC6=0xffff;values();return;
  390.     defualt:setfillpattern(fill_6,NORM_HUE);CC6=0x1010;values();}
  391. return;}
  392.  
  393. /* ----------------------------------------------------------------------- */
  394. /* LOCAL SUBROUTINE: prep fill and set dithering variables */
  395. void values(void){
  396. floodfill(sx5,sy5,edge_clr);CC4=BASE_CLR;CC5=NORM_HUE;return;}
  397.  
  398. /* ----------------------------------------------------------------------- */
  399. /* SUBROUTINE: APPLY DITHERING */
  400.  
  401. void dither(void){
  402. setlinestyle(USERBIT_LINE,0xffff,NORM_WIDTH);setcolor(CC4);
  403. moveto (sx1,sy1);lineto(sx2,sy2);lineto(sx3,sy3);
  404. lineto(sx4,sy4);lineto(sx1,sy1);
  405. setlinestyle(USERBIT_LINE,CC6,NORM_WIDTH);setcolor(CC5);
  406. moveto(sx1,sy1);lineto(sx2,sy2);lineto(sx3,sy3);
  407. lineto(sx4,sy4);lineto(sx1,sy1);
  408. return;
  409. }
  410.  
  411. /* ----------------------------------------------------------------------- */
  412. /* SUBROUTINE: MAP CARTESIAN COORDS TO PHYSICAL SCREEN COORDS */
  413.  
  414. void window(void){
  415. sx=sx+399;sy=sy+299;rx=screen_x/799;ry=screen_y/599;sx=sx*rx;
  416. sy=sy*ry;return;}
  417.  
  418. /* ----------------------------------------------------------------------- */
  419. /* SUBROUTINE: CHACK THE KEYBOARD BUFFER */
  420. void keyboard(void){
  421. if (bioskey(1)==0) return; else quit_pgm();}
  422.  
  423. /* ----------------------------------------------------------------------- */
  424. /* SUBROUTINE: GRACEFUL EXIT FROM PROGRAM */
  425.  
  426. void quit_pgm(void){
  427. cleardevice();restorecrtmode();exit(0);}
  428.  
  429. /* ----------------------------------------------------------------------- */
  430. /* SUBROUTINE: VGA/EGA/MCGA/CGA COMPATIBILITY MODULE */
  431.  
  432. void graphics_setup(void){
  433. int graphics_adapter,graphics_mode;
  434. detectgraph(&graphics_adapter,&graphics_mode);
  435. if (graphics_adapter==VGA) goto VGA_mode;
  436. if (graphics_mode==EGAHI) goto EGA_ECD_mode;
  437. if (graphics_mode==EGALO) goto EGA_SCD_mode;
  438. if (graphics_adapter==CGA) goto CGA_mode;
  439. if (graphics_adapter==MCGA) goto CGA_mode;
  440. goto abort_message;
  441.  
  442. VGA_mode:
  443. graphics_adapter=VGA;graphics_mode=VGAHI;
  444. initgraph(&graphics_adapter,&graphics_mode,"");
  445. x_res=640;y_res=480;mode_flag=1;
  446.         maxx=639;minx=0;maxy=479;miny=0;screen_x=639;screen_y=479;
  447.         setcolor(7);moveto(0,472);
  448.           outtext("Revisions by A. Helder");
  449.         moveto(472,472);
  450.         outtext("Press any key to quit");
  451.         moveto(160,0);
  452.           outtext("USING C TO GENERATE PACKAGING DESIGN");
  453.         return;
  454.  
  455. EGA_ECD_mode:
  456. graphics_adapter=EGA;graphics_mode=EGAHI;
  457. initgraph(&graphics_adapter,&graphics_mode,"");
  458. x_res=640;y_res=350;mode_flag=2;
  459.         maxx=639;minx=0;maxy=349;miny=0;screen_x=639;screen_y=349;
  460.         setcolor(7);moveto(0,342);
  461.           outtext("Revisions by A. Helder");
  462.         moveto(472,342);
  463.         outtext ("Press any key to quit");
  464.         moveto(160,0);
  465.           outtext("USING C TO GENERATE PACKAGING DESIGN");
  466.         return;
  467.  
  468. EGA_SCD_mode:
  469. graphics_adapter=EGA;graphics_mode=EGALO;
  470. initgraph(&graphics_adapter,&graphics_mode,"");
  471. x_res=640;y_res=200;mode_flag=3;
  472.         maxx=639;minx=0;maxy=199;miny=0;screen_x=639;screen_y=199;
  473.         setcolor(7);moveto(0,192);
  474.           outtext("Revisions by A. Helder");
  475.         moveto(472,192);
  476.         outtext("PRESS ANY KEY TO QUIT");
  477.         moveto(160,0);
  478.           outtext("USING C TO GENERATE PACKAGING DESIGN");
  479.         return;
  480.  
  481. CGA_mode:
  482. graphics_adapter=CGA;graphics_mode=CGAC3;
  483. initgraph(&graphics_adapter,&graphics_mode,"");
  484. x_res=320;y_res=200;mode_flag=1;C7=3;
  485.         maxx=319;minx=0;maxy=199;miny=0;screen_x=319;screen_y=199;
  486.         setcolor(3);moveto(48,192);
  487.           outtext("Revisions by A. Helder");
  488.         moveto(88,0);
  489.           outtext ("PACKAGING DESIGN");
  490.         return;
  491.  
  492. abort_message:
  493. printf("\n\nUnable to proceed - Requires VGA,EGA,CGA or MCGA adapter");
  494. printf("\nWith appropriate monitor");
  495. exit(0);
  496. }
  497.  
  498. /* ----------------------------------------------------------------------- */
  499. /* SUBROUTINE: MAP 640*480 TEMPLATE TO 2D SCREEN */
  500. void coords(void)
  501. {
  502. sx=sx*(x_res/640);sy=sy*(y_res/480);return;
  503. }
  504.  
  505. /* ----------------------------------------------------------------------- */
  506. /* SUBROUTINE: COPYRIGHT NOTICE */
  507.  
  508. int copyright[][3]={0x7c00,0x0000,0x0000,0x8231,
  509. 0x819c,0x645e,0xba4a,0x4252,0x96d0,0xa231,0x8252,0x955e,0xba4a,
  510. 0x43d2,0xf442,0x8231,0x825c,0x945e,0x7c00,0x0000,0x0000};
  511.  
  512. void notice(int x, int y){
  513. int a,b,c; int t1=0;
  514.  
  515. for (t1=0;t1<=6;t1++)
  516.     {
  517.     a=copyright[t1][0];b=copyright[t1][1];
  518.     c=copyright[t1][2];
  519.     setlinestyle(USERBIT_LINE,a,NORM_WIDTH);
  520.     moveto(x,y);lineto(x+15,y);
  521.     setlinestyle(USERBIT_LINE,b,NORM_WIDTH);
  522.     moveto(x+16,y);lineto(x+31,y);
  523.     setlinestyle(USERBIT_LINE,c,NORM_WIDTH);
  524.     moveto(x+32,y);lineto(x+47,y);y++;
  525.     };
  526. setlinestyle(USERBIT_LINE,0xFFFF,NORM_WIDTH);
  527. return;}
  528.  
  529. /* end of source code */