home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / grafik / tc_3d / tc-08.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-13  |  12.7 KB  |  362 lines

  1. /* TC-08.C */
  2. /* Demonstrates surface mapping of a 3d cylider.  A two color design
  3.     is mapped onto a can */
  4.  
  5. /* ----------------------------------------------------------------------- */
  6. /* INCLUDE FILES */
  7. #include <process.h>
  8. #include <bios.h>
  9. #include <stdio.h>
  10. #include <graphics.h>
  11. #include <math.h>
  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.89778;                                                        /* yaw angle in radians */
  25. double    r2=6.28319;                                                        /* roll angle in radians */
  26. double    r3=.58539;                                                        /* 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=0.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. int        design_clr=1;                                                    /* used to draw the design on the model */
  42. float        x_res,y_res;                                                    /* used for 2d mappinf from 640*480 template */
  43. float        sp=0.0;                                                            /* visibility factor in hidden surface routine */
  44. float        sp1=0.0,sp2=0.0,sp3=0.0;                                    /* temporary values of sp */
  45.  
  46. int        q=0,q1=0,q2=0;                                                    /* cylinder surface and vertex counters */
  47. double    r4=6.28319,r5=6.28319;                                        /* spherical coordinate angles */
  48. int        polary[4][2];  /* used in my 3d solid polygon draw routine */
  49. float        B11[36][3];                                                        /* 36 sets of xyz view coordinates, near end */
  50. float        B12[36][3];                                                        /* 36 sets of xyz view coordinates, far end */
  51. float        B21[36][2];                                                        /* 36 sets of sx,sy display coordinates, near end */
  52. float        B22[36][2];                                                        /* 36 sets of sx,sy display coordinates, far end */
  53. double    sr4=0.0,cr4=0.0,sr5=0.0,cr5=0.0;                            /* rotation factor */
  54.  
  55. /* declare global subroutines */
  56. void keyboard(void);void quit_pgm(void);void calc_3d(void);
  57. void rotation(void);void window(void);void graphics_setup(void);
  58. void coords(void);void draw_poly(void);void notice (int x,int y);
  59. void visibility_test(void);
  60. void cyl_coords(void);void draw_surface(void);
  61.  
  62. /* ----------------------------------------------------------------------- */
  63. main(){
  64. graphics_setup();
  65. setviewport(0,0,maxx,maxy,1);
  66. key_matte_clr=C6;
  67. edge_clr=C7;solid_clr=C5;                                                /* define modeling colors */
  68. rotation();
  69.  
  70. /* calculate coordinates for near end of cylinder */
  71. r4=0;r5=0;
  72. for (t=0;t<=35;t++){
  73.     x=30;cyl_coords();z=z+60;calc_3d();window();
  74.     B11[t][0]=x;B11[t][1]=y;B11[t][2]=z;
  75.     B21[t][0]=sx;B21[t][1]=sy;
  76.     keyboard();
  77.     r5=r5+.17453;}
  78.  
  79. /* calculate coordinates for far end of cylinder */
  80. r4=0;r5=0;
  81. for (t=0;t<=35;t++){
  82.     x=30;cyl_coords();z=z-60;calc_3d();window();
  83.     B12[t][0]=x;B12[t][1]=y;B12[t][2]=z;
  84.     B22[t][0]=sx;B22[t][1]=sy;
  85.     keyboard();
  86.     r5=r5+.17453;}
  87.  
  88. /* draw surfaces of 3d cylinder */
  89. solid_clr=C5;
  90. for (q1=0;q1<=35;q1++){
  91.     q2=q1+1;if (q2>35) q2=0;
  92.     draw_surface();keyboard();}
  93.  
  94. /* draw near end of 3d cylinder */
  95. x1=B11[0][0];y01=B11[0][1];z1=B11[0][2];
  96. x2=B11[25][0];y2=B11[25][1];z2=B11[25][2];
  97. x3=B11[11][0];y3=B11[11][1];z3=B11[11][2];
  98. visibility_test();if (sp>0) goto draw_the_design;
  99. setcolor (key_matte_clr);setfillstyle(SOLID_FILL,key_matte_clr);
  100. setlinestyle(USERBIT_LINE,0xffff,NORM_WIDTH);
  101. for (q1=0;q1<=35;q1++){
  102.     q2=q1+1;if (q2>35) q2=0;
  103.     sx1=B21[q1][0];sy1=B21[q1][1];
  104.     sx2=B21[q2][0];sy2=B21[q2][1];
  105.     moveto(sx1,sy1);lineto(sx2,sy2);}
  106. solid_clr=C13;
  107. x=0.0;y=0.0;z=60.0;calc_3d();window();floodfill(sx,sy,key_matte_clr);
  108. keyboard();
  109. edge_clr=C13;
  110. setcolor(edge_clr);
  111. solid_clr=C13;
  112. for (q1=0;q1<=35;q1++){
  113.     q2=q1+1;if (q2>35) q2=0;
  114.     sx1=B21[q1][0];sy1=B21[q1][1];
  115.     sx2=B21[q2][0];sy2=B21[q2][1];
  116.     moveto(sx1,sy1);lineto(sx2,sy2);}
  117. x=0.0;y=0.0;z=60.0;calc_3d();window();
  118. setfillstyle(SOLID_FILL,solid_clr);floodfill(sx,sy,edge_clr);
  119.  
  120. /* draw design on cylinder */
  121. draw_the_design:
  122. r4=0;r5=1.5;
  123. design_clr=C1;                                                                /* prepare to draw lower graphic */
  124. setcolor(design_clr);
  125. x=30;cyl_coords();z=z+20;calc_3d();window();moveto(sx,sy);
  126. r5=r5+.08727;
  127. for (t=0;t<=21;t++){
  128.     x=30;cyl_coords();z=z+20;calc_3d();window();
  129.     lineto(sx,sy);
  130.     keyboard();
  131.     r5=r5+.08727;}
  132.  
  133. r5=r5-.08727;                                                                /* negate the final loop increment */
  134. x=30;cyl_coords();z=z-40;calc_3d();window();lineto(sx,sy);
  135. r5=r5-.08727;
  136. for (t=0;t<=21;t++){
  137.     x=30;cyl_coords();z=z-40;calc_3d();window();
  138.     lineto(sx,sy);
  139.     keyboard();
  140.     r5=r5-.08727;}
  141.  
  142. r5=r5+.08727;                                                                /* negate the final loop decrement */
  143. x=30;cyl_coords();z=z+20;calc_3d();window();lineto(sx,sy);
  144. r5=r5+(10*.08727);
  145. x=30;cyl_coords();z=z-10;calc_3d();window();
  146. setfillstyle(SOLID_FILL,design_clr);
  147. floodfill(sx,sy,design_clr);
  148.  
  149. r4=0;r5=1.5;
  150. design_clr=C4;                                                                /* prepare to draw lower graphic */
  151. setcolor(design_clr);
  152. x=30;cyl_coords();z=z+30;calc_3d();window();moveto(sx,sy);
  153. r5=r5+.08727;
  154. for (t=0;t<=21;t++){
  155.     x=30;cyl_coords();z=z+30;calc_3d();window();
  156.     lineto(sx,sy);
  157.     keyboard();
  158.     r5=r5+.08727;}
  159.  
  160. r5=r5-.08727;                                                                /* negate the final loop increment */
  161. x=30;cyl_coords();z=z+50;calc_3d();window();lineto(sx,sy);
  162. r5=r5-.08727;
  163. for (t=0;t<=21;t++){
  164.     x=30;cyl_coords();z=z+50;calc_3d();window();
  165.     lineto(sx,sy);
  166.     keyboard();
  167.     r5=r5-.08727;}
  168.  
  169. r5=r5+.08727;                                                                /* negate the final loop decrement */
  170. x=30;cyl_coords();z=z+30;calc_3d();window();lineto(sx,sy);
  171. r5=r5+(10*.08727);
  172. x=30;cyl_coords();z=z+40;calc_3d();window();
  173. setfillstyle(SOLID_FILL,design_clr);
  174. floodfill(sx,sy,design_clr);
  175.  
  176. setcolor(C7);notice(0,0);
  177. for (t1=1;t1!=2;) keyboard();
  178. quit_pgm();}
  179.  
  180. /* ----------------------------------------------------------------------- */
  181. /* SUBROUTINE: CALCULATE WORLD COORDINATES FOR cylinder */
  182.  
  183. void cyl_coords(void){
  184. sr4=sin(r4);cr4=cos(r4);sr5=sin(r5);
  185. cr5=cos(r5);x1=sr5*x;y=cr5*x;x=cr4*x1;z=sr4*x1;return;}
  186.  
  187. /* ----------------------------------------------------------------------- */
  188. /* SUBROUTINE: DRAW 4 SIDED POLYGON SURFACE ON SPHERE */
  189.  
  190. void draw_surface(void){
  191. x1=B11[q1][0];y01=B11[q1][1];z1=B11[q1][2];
  192. x2=B11[q2][0];y2=B11[q2][1];z2=B11[q2][2];
  193. x3=B12[q2][0];y3=B12[q2][1];z3=B12[q2][2];
  194. visibility_test();if (sp>0) return;
  195. sx1=B21[q1][0];sy1=B21[q1][1];
  196. sx2=B21[q2][0];sy2=B21[q2][1];
  197. sx3=B22[q2][0];sy3=B22[q2][1];
  198. sx4=B22[q1][0];sy4=B22[q1][1];
  199. x=x1+.5*(x3-x1);y=y01+.5*(y3-y01);z=z1+.5*(z3-z1);
  200. sx=d*x/z;sy=d*y/z;window();sx5=sx=sy5=sy;
  201. draw_poly();
  202. return;}
  203.  
  204. /* ----------------------------------------------------------------------- */
  205. /* SUBROUTINE: CALCULATE SIN,COS FACTORS */
  206.  
  207. void rotation(void){
  208. sr1=sin(r1);sr2=sin(r2);sr3=sin(r3);cr1=cos(r1);cr2=cos(r2);
  209. cr3=cos(r3);return;}
  210.  
  211. /* ----------------------------------------------------------------------- */
  212. /* SUBROUTINE: STANDARD 3D FORMULAS */
  213. /* Pass: x,y,z cartesian world coordinates.
  214.    Returns: sx,sy cartesian display coordinates.
  215.           x,y,z catesian view coordinates */
  216.  
  217. void calc_3d(void){
  218. x=(-1)*x;xa=cr1*x-sr1*z;za=sr1*x+cr1*z;x=cr2*xa+sr2*y;
  219. ya=cr2*y-sr2*xa;z=cr3*za-sr3*ya;y=sr3*za+cr3*ya;x=x+mx;y=y+my;
  220. z=z+mz;sx=d*x/z;sy=d*y/z;return;}
  221.  
  222. /* ----------------------------------------------------------------------- */
  223. /* HIDDEN SURFACE VISIBILTY TEST */
  224. void visibility_test(void){
  225. sp1=x1*(y2*z3-y3*z2);sp1=(-1)*sp1;sp2=x2*(y3*z1-y01*z3);
  226. sp3=x3*(y01*z2-y2*z1);sp=sp1-sp2-sp3;return;}
  227.  
  228. /* ----------------------------------------------------------------------- */
  229. /* SUBROUTINE: DRAW 4-SIDED SOLID POLYGON IN 3D SPACE */
  230.  
  231. void draw_poly(void){
  232. setlinestyle(USERBIT_LINE,0xffff,NORM_WIDTH);
  233. setfillstyle(SOLID_FILL,solid_clr);
  234. setcolor(edge_clr);
  235.  
  236. polary[0][0]=sx1;polary[0][1]=sy1;polary[1][0]=sx2;polary[1][1]=sy2;
  237. polary[2][0]=sx3;polary[2][1]=sy3;polary[3][0]=sx4;polary[3][1]=sy4;
  238.  
  239. fillpoly (4,(int far*) polary);
  240. return;
  241. }
  242.  
  243. /* ----------------------------------------------------------------------- */
  244. /* SUBROUTINE: MAP CARTESIAN COORDS TO PHYSICAL SCREEN COORDS */
  245.  
  246. void window(void){
  247. sx=sx+399;sy=sy+299;rx=screen_x/799;ry=screen_y/599;sx=sx*rx;
  248. sy=sy*ry;return;}
  249.  
  250. /* ----------------------------------------------------------------------- */
  251. /* SUBROUTINE: CHACK THE KEYBOARD BUFFER */
  252. void keyboard(void){
  253. if (bioskey(1)==0) return; else quit_pgm();}
  254.  
  255. /* ----------------------------------------------------------------------- */
  256. /* SUBROUTINE: GRACEFUL EXIT FROM PROGRAM */
  257.  
  258. void quit_pgm(void){
  259. cleardevice();restorecrtmode();exit(0);}
  260.  
  261. /* ----------------------------------------------------------------------- */
  262. /* SUBROUTINE: VGA/EGA/MCGA/CGA COMPATIBILITY MODULE */
  263.  
  264. void graphics_setup(void){
  265. int graphics_adapter,graphics_mode;
  266. detectgraph(&graphics_adapter,&graphics_mode);
  267. if (graphics_adapter==VGA) goto VGA_mode;
  268. if (graphics_mode==EGAHI) goto EGA_ECD_mode;
  269. if (graphics_mode==EGALO) goto EGA_SCD_mode;
  270. if (graphics_adapter==CGA) goto CGA_mode;
  271. if (graphics_adapter==MCGA) goto CGA_mode;
  272. goto abort_message;
  273.  
  274. VGA_mode:
  275. graphics_adapter=VGA;graphics_mode=VGAHI;
  276. initgraph(&graphics_adapter,&graphics_mode,"");
  277. x_res=640;y_res=480;mode_flag=1;
  278.         maxx=639;minx=0;maxy=479;miny=0;screen_x=639;screen_y=479;
  279.         setcolor(7);moveto(0,472);
  280.           outtext("Revisions by A. Helder");
  281.         moveto(472,472);
  282.         outtext("Press any key to quit");
  283.         moveto(160,0);
  284.         outtext("USING C TO GENERATE A 3D WIRE FRAME CUBE");
  285.         return;
  286.  
  287. EGA_ECD_mode:
  288. graphics_adapter=EGA;graphics_mode=EGAHI;
  289. initgraph(&graphics_adapter,&graphics_mode,"");
  290. x_res=640;y_res=350;mode_flag=2;
  291.         maxx=639;minx=0;maxy=349;miny=0;screen_x=639;screen_y=349;
  292.         setcolor(7);moveto(0,342);
  293.           outtext("Revisions by A. Helder");
  294.         moveto(472,342);
  295.         outtext ("Press any key to quit");
  296.         moveto(160,0);
  297.           outtext("USING C TO MAP DESIGNS ONTO A SOLID 3D MODEL");
  298.         return;
  299.  
  300. EGA_SCD_mode:
  301. graphics_adapter=EGA;graphics_mode=EGALO;
  302. initgraph(&graphics_adapter,&graphics_mode,"");
  303. x_res=640;y_res=200;mode_flag=3;
  304.         maxx=639;minx=0;maxy=199;miny=0;screen_x=639;screen_y=199;
  305.         setcolor(7);moveto(0,192);
  306.           outtext("Revisions by A. Helder");
  307.         moveto(472,192);
  308.         outtext("PRESS ANY KEY TO QUIT");
  309.         moveto(160,0);
  310.           outtext("USING C TO MAP DESIGNS ONTO A SOLID 3D MODEL");
  311.         return;
  312.  
  313. CGA_mode:
  314. graphics_adapter=CGA;graphics_mode=CGAC3;
  315. initgraph(&graphics_adapter,&graphics_mode,"");
  316. x_res=320;y_res=200;mode_flag=1;C7=3;
  317.         maxx=319;minx=0;maxy=199;miny=0;screen_x=319;screen_y=199;
  318.         setcolor(3);moveto(48,192);
  319.           outtext("Revisions by A. Helder");
  320.         moveto(88,0);
  321.           outtext ("MAPPING A CYLINDER");
  322.         return;
  323.  
  324. abort_message:
  325. printf("\n\nUnable to proceed - Requires VGA,EGA,CGA or MCGA adapter");
  326. printf("\nWith appropriate monitor");
  327. exit(0);
  328. }
  329.  
  330. /* ----------------------------------------------------------------------- */
  331. /* SUBROUTINE: MAP 640*480 TEMPLATE TO 2D SCREEN */
  332. void coords(void)
  333. {
  334. sx=sx*(x_res/640);sy=sy*(y_res/480);return;
  335. }
  336.  
  337. /* ----------------------------------------------------------------------- */
  338. /* SUBROUTINE: COPYRIGHT NOTICE */
  339.  
  340. int copyright[][3]={0x7c00,0x0000,0x0000,0x8231,
  341. 0x819c,0x645e,0xba4a,0x4252,0x96d0,0xa231,0x8252,0x955e,0xba4a,
  342. 0x43d2,0xf442,0x8231,0x825c,0x945e,0x7c00,0x0000,0x0000};
  343.  
  344. void notice(int x, int y){
  345. int a,b,c; int t1=0;
  346.  
  347. for (t1=0;t1<=6;t1++)
  348.     {
  349.     a=copyright[t1][0];b=copyright[t1][1];
  350.     c=copyright[t1][2];
  351.     setlinestyle(USERBIT_LINE,a,NORM_WIDTH);
  352.     moveto(x,y);lineto(x+15,y);
  353.     setlinestyle(USERBIT_LINE,b,NORM_WIDTH);
  354.     moveto(x+16,y);lineto(x+31,y);
  355.     setlinestyle(USERBIT_LINE,c,NORM_WIDTH);
  356.     moveto(x+32,y);lineto(x+47,y);y++;
  357.     };
  358. setlinestyle(USERBIT_LINE,0xFFFF,NORM_WIDTH);
  359. return;}
  360.  
  361. /* end of source code */
  362.