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

  1. /* tc-07.c */
  2. /* Demonstrates surface mapping of a 3d solid cube.
  3.     A two color design is draw on three sides of the carton */
  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. /* ----------------------------------------------------------------------- */
  14. /* DECLARATIONS */
  15.  
  16. float        x=0.0,y=0.0,z=0.0;                                              /* world coordinates */
  17. float        x1=0.0,x2=0.0,x3=0.0;                                        /* polygon vertices */
  18. float        y01=0.0,y2=0.0,y3=0.0;                                        /* polygon vertices */
  19. float        z1=0.0,z2=0.0,z3=0.0;                                        /* polygon vertices */
  20. float        sx1=0.0,sx2=0.0,sx3=0.0,sx4=0.0,sx5=0.0;                /* display coordinaes */
  21. float        sy1=0.0,sy2=0.0,sy3=0.0,sy4=0.0,sy5=0.0;                /* display coordinates */
  22. float        sx=0.0,sy=0.0;                                                    /* output of 3d perspective formulas */
  23. float        xa=0.0,ya=0.0,za=0.0;                                        /* temporary values in 3d formulas */
  24. float        d=1200.0;                                                        /* angular perspective value */
  25. double    r1=5.68319;                                                        /* yaw angle in radians */
  26. double    r2=6.28319;                                                        /* roll angle in radians */
  27. double    r3=5.79778;                                                        /* pitch angle in radians */
  28. double    sr1=0.0,sr2=0.0,sr3=0.0;                                    /* sine rotation factors */
  29. double    cr1=0.0,cr2=0.0,cr3=0.0;                                    /* cosine rotation factors */
  30. float        mx=0.0,my=0.0,mz=-450.0;                                    /* viewpoint position */
  31. int        maxx=639,minx=0,maxy=199,miny=0;                            /* scaling viewport */
  32. float        screen_x=639,screen_y=199;                                    /* dimensions of screen mode */
  33. int        C0=0,C1=1,C2=2,C3=3,C4=4,C5=5,C6=6,C7=7,C8=8,        /* color varariables */
  34.             C9=9,C10=10,C11=11,C12=12,C13=13,C14=14,C15=15,
  35.             mode_flag=0;
  36. float        rx=0.0,ry=0.0;                                                    /* scaling values used in mapping routine */
  37. int        t=0,t1=0,t2=0;                                                    /* loop counters */
  38. int        p1=0;                                                                /* array indexer */
  39. int        key_matte_clr=6;                                                /* exclusive key matte color */
  40. int        edge_clr=7;                                                        /* used to draw edges on models */
  41. int        solid_clr=2;                                                    /* used to fill surfaces on solid models */
  42. int        design_clr=1;                                                    /* used to draw the design on the model */
  43. int        polary[4][2];                                                    /* my array - used in draw_poly */
  44. float        x_res,y_res;                                                    /* used for 2d mappinf from 640*480 template */
  45. float        sp=0.0;                                                            /* visibility factor in hidden surface routine */
  46. float        sp1=0.0,sp2=0.0,sp3=0.0;                                    /* temporary values of sp */
  47.  
  48. /* database of vertices for 3d model */
  49. int        array1[][3]={
  50.              30,-30, 30,  30, 30, 30, -30, 30, 30, -30,-30,30, 30,30,-30,
  51.             -30, 30,-30, -30,-30,-30,  30,-30,-30};
  52.  
  53. /* database of fill origins for 3d model */
  54. int        array2[][3]={
  55.             0,-30,0,    0,0,-30,    -30,0,0,    0,0,30,    30,0,0,    0,30,0};
  56.  
  57. /* database of vertices for lower design */
  58. int        array3[][3]={
  59.             10,3,30,    17,-15,30,    30,-10,30,    30,11,30,    30,10,-19,
  60.             30,28,-11};
  61.  
  62. /* database of vertices for upper design */
  63. int        array4[][3]={
  64.             5,15,30,    9,7,30,    30,15,30,    30,25,30,    30,30,18,    30,30,-7,
  65.             28,30,-10,    20,30,-6};
  66.  
  67. /* databse of fill seeds for designs */
  68. int        array5[][3]={
  69.             21,-2,30,    30,8,10,    18,15,30,    30,23,18,    26,30,-2};
  70.  
  71. float        B1[8][3];                                                        /* array of 8 sets of xyz points */
  72. float        B2[8][2];                                                        /* array of 8 sets of sx,sy display coordinates */
  73. float        B3[6][2];                                                        /* array of 6 sets of sx,sy fill coordinates */
  74. float        B4[6][2];                                                        /* array of six sets display coordinates lower design */
  75. float        B5[8][2];                                                        /* array of 8 sets of display coordinates upper design */
  76. float        B6[5][2];                                                        /* array of 5 sets of fill seeds for design */
  77.  
  78. /* declare global subroutines */
  79. void keyboard(void);void quit_pgm(void);void calc_3d(void);
  80. void rotation(void);void window(void);void graphics_setup(void);
  81. void coords(void);void draw_poly(void);void notice (int x,int y);
  82. void visibility_test(void);void store_coords(void);
  83. void draw_design(void);
  84.  
  85. /* ----------------------------------------------------------------------- */
  86. /* MAIN ROUTINE */
  87.  
  88. main(){
  89. graphics_setup();
  90. setviewport(0,0,maxx,maxy,1);
  91. key_matte_clr=C6;
  92. edge_clr=C7;solid_clr=C3;
  93. store_coords();                                                            /* calculate and store 3d coords in arrays */
  94.  
  95. surface0:
  96. x1=B1[7][0];y01=B1[7][1];z1=B1[7][2];x2=B1[0][0];y2=B1[0][1];
  97. z2=B1[0][2];x3=B1[3][0];y3=B1[3][1];z3=B1[3][2];visibility_test();
  98. if (sp>0) goto surface1;
  99. sx1=B2[7][0];sy1=B2[7][1];sx2=B2[0][0];sy2=B2[0][1];sx3=B2[3][0];
  100. sy3=B2[3][1];sx4=B2[6][0];sy4=B2[6][1];sx5=B3[0][0];sy5=B3[0][1];
  101. draw_poly();
  102.  
  103. surface1:
  104. x1=B1[6][0];y01=B1[6][1];z1=B1[6][2];x2=B1[5][0];y2=B1[5][1];
  105. z2=B1[5][2];x3=B1[4][0];y3=B1[4][1];z3=B1[4][2];visibility_test();
  106. if (sp>0) goto surface2;
  107. sx1=B2[6][0];sy1=B2[6][1];sx2=B2[5][0];sy2=B2[5][1];sx3=B2[4][0];
  108. sy3=B2[4][1];sx4=B2[7][0];sy4=B2[7][1];sx5=B3[1][0];sy5=B3[1][1];
  109. draw_poly();
  110.  
  111. surface2:
  112. x1=B1[3][0];y01=B1[3][1];z1=B1[3][2];x2=B1[2][0];y2=B1[2][1];
  113. z2=B1[2][2];x3=B1[5][0];y3=B1[5][1];z3=B1[5][2];visibility_test();
  114. if (sp>0) goto surface3;
  115. sx1=B2[3][0];sy1=B2[3][1];sx2=B2[2][0];sy2=B2[2][1];sx3=B2[5][0];
  116. sy3=B2[5][1];sx4=B2[6][0];sy4=B2[6][1];sx5=B3[2][0];sy5=B3[2][1];
  117. draw_poly();
  118.  
  119. surface3:
  120. x1=B1[0][0];y01=B1[0][1];z1=B1[0][2];x2=B1[1][0];y2=B1[1][1];
  121. z2=B1[1][2];x3=B1[2][0];y3=B1[2][1];z3=B1[2][2];visibility_test();
  122. if (sp>0) goto surface4;
  123. sx1=B2[0][0];sy1=B2[0][1];sx2=B2[1][0];sy2=B2[1][1];sx3=B2[2][0];
  124. sy3=B2[2][1];sx4=B2[3][0];sy4=B2[3][1];sx5=B3[3][0];sy5=B3[3][1];
  125. draw_poly();
  126. sx1=B4[0][0];sy1=B4[0][1];sx2=B4[1][0];sy2=B4[1][1];sx3=B4[2][0];
  127. sy3=B4[2][1];sx4=B4[3][0];sy4=B4[3][1];sx5=B6[0][0];sy5=B6[0][1];
  128. design_clr=C1;draw_design();
  129. sx1=B5[0][0];sy1=B5[0][1];sx2=B5[1][0];sy2=B5[1][1];sx3=B5[2][0];
  130. sy3=B5[2][1];sx4=B5[3][0];sy4=B5[3][1];sx5=B6[2][0];sy5=B6[2][1];
  131. design_clr=C4;draw_design();
  132.  
  133. surface4:
  134. x1=B1[7][0];y01=B1[7][1];z1=B1[7][2];x2=B1[4][0];y2=B1[4][1];
  135. z2=B1[4][2];x3=B1[1][0];y3=B1[1][1];z3=B1[1][2];visibility_test();
  136. if (sp>0) goto surface5;
  137. sx1=B2[7][0];sy1=B2[7][1];sx2=B2[4][0];sy2=B2[4][1];sx3=B2[1][0];
  138. sy3=B2[1][1];sx4=B2[0][0];sy4=B2[0][1];sx5=B3[4][0];sy5=B3[4][1];
  139. draw_poly();
  140. sx1=B4[3][0];sy1=B4[3][1];sx2=B4[2][0];sy2=B4[2][1];sx3=B4[4][0];
  141. sy3=B4[4][1];sx4=B4[5][0];sy4=B4[5][1];sx5=B6[1][0];sy5=B6[1][1];
  142. design_clr=C1;draw_design();
  143. sx1=B5[3][0];sy1=B5[3][1];sx2=B5[2][0];sy2=B5[2][1];sx3=B5[5][0];
  144. sy3=B5[5][1];sx4=B5[4][0];sy4=B5[4][1];sx5=B6[3][0];sy5=B6[3][1];
  145. design_clr=C4;draw_design();
  146.  
  147. surface5:
  148. x1=B1[1][0];y01=B1[1][1];z1=B1[1][2];x2=B1[4][0];y2=B1[4][1];
  149. z2=B1[4][2];x3=B1[5][0];y3=B1[5][1];z3=B1[5][2];visibility_test();
  150. if (sp>0) goto surfaces_done;
  151. sx1=B2[1][0];sy1=B2[1][1];sx2=B2[4][0];sy2=B2[4][1];sx3=B2[5][0];
  152. sy3=B2[5][1];sx4=B2[2][0];sy4=B2[2][1];sx5=B3[5][0];sy5=B3[5][1];
  153. draw_poly();
  154. sx1=B5[4][0];sy1=B5[4][1];sx2=B5[5][0];sy2=B5[5][1];sx3=B5[6][0];
  155. sy3=B5[6][1];sx4=B5[7][0];sy4=B5[7][1];sx5=B6[4][0];sy5=B6[4][1];
  156. design_clr=C4;draw_design();
  157.  
  158. surfaces_done:setcolor(C7);notice(0,0);
  159. for (t1=1;t1!=2;) keyboard();
  160. quit_pgm();}
  161.  
  162. /* ----------------------------------------------------------------------- */
  163. /* SUBROUTINE: CALCULATE SIN,COS FACTORS */
  164.  
  165. void rotation(void){
  166. sr1=sin(r1);sr2=sin(r2);sr3=sin(r3);cr1=cos(r1);cr2=cos(r2);
  167. cr3=cos(r3);return;}
  168.  
  169. /* ----------------------------------------------------------------------- */
  170. /* SUBROUTINE: STANDARD 3D FORMULAS */
  171. /* Pass: x,y,z cartesian world coordinates.
  172.    Returns: sx,sy cartesian display coordinates.
  173.           x,y,z catesian view coordinates */
  174.  
  175. void calc_3d(void){
  176. x=(-1)*x;xa=cr1*x-sr1*z;za=sr1*x+cr1*z;x=cr2*xa+sr2*y;
  177. ya=cr2*y-sr2*xa;z=cr3*za-sr3*ya;y=sr3*za+cr3*ya;x=x+mx;y=y+my;
  178. z=z+mz;sx=d*x/z;sy=d*y/z;return;}
  179.  
  180. /* ----------------------------------------------------------------------- */
  181. /* SUBROUTINE: CALCULATE & STORE VIEW COORDINATES & DISPLAY COORDINATES */
  182.  
  183. void store_coords(void){
  184. rotation();
  185. for (t=0;t<=7;t++){                                                        /* store view coordinates */
  186.     x=array1[t][0];y=array1[t][1];z=array1[t][2];
  187.     calc_3d();window();
  188.     B1[t][0]=x;B1[t][1]=y;B1[t][2]=z;
  189.     B2[t][0]=sx;B2[t][1]=sy;
  190.     };
  191.  
  192. for (t=0;t<=5;t++){                                                        /* store fill coordinates */
  193.     x=array2[t][2];y=array1[t][1];z=array2[t][2];
  194.     calc_3d();window();
  195.     B3[t][0]=sx;B3[t][1]=sy;
  196.     };
  197.  
  198. for (t=0;t<=5;t++){
  199.     x=array3[t][0];y=array3[t][1];z=array3[t][2];
  200.     calc_3d();window();
  201.     B4[t][0]=sx;B4[t][1]=sy;                                            /* store lower design display coordinates */
  202.     };
  203.  
  204. for (t=0;t<=7;t++){
  205.     x=array4[t][0];y=array4[t][1];z=array4[t][2];
  206.     calc_3d();window();
  207.     B5[t][0]=sx;B5[t][1]=sy;                                            /* store upper design display coordinates */
  208.     };
  209.  
  210. for (t=0;t<=4;t++){
  211.     x=array5[t][0];y=array5[t][1];z=array5[t][2];
  212.     calc_3d();window();
  213.     B6[t][0]=sx;B6[t][1]=sy;                                            /* store fill display coordinates */
  214.     };
  215.  
  216. return;}
  217.  
  218. /* ----------------------------------------------------------------------- */
  219. /* HIDDEN SURFACE VISIBILTY TEST */
  220. void visibility_test(void){
  221. sp1=x1*(y2*z3-y3*z2);sp1=(-1)*sp1;sp2=x2*(y3*z1-y01*z3);
  222. sp3=x3*(y01*z2-y2*z1);sp=sp1-sp2-sp3;return;}
  223.  
  224. /* ----------------------------------------------------------------------- */
  225. /* SUBROUTINE: DRAW 4-SIDED SOLID POLYGON IN 3D SPACE */
  226.  
  227. void draw_poly(void){
  228. setlinestyle(USERBIT_LINE,0xffff,NORM_WIDTH);
  229. setfillstyle(SOLID_FILL,C0);
  230. setcolor(C9);
  231.  
  232. polary[0][0]=sx2;polary[0][1]=sy2;polary[1][0]=sx3;polary[1][1]=sy3;
  233. polary[2][0]=sx4;polary[2][1]=sy4;polary[3][0]=sx1;polary[3][1]=sy1;
  234.  
  235. fillpoly (4,(int far*) polary);
  236. return;}
  237.  
  238. /* ----------------------------------------------------------------------- */
  239. void draw_design(void){
  240. setlinestyle(USERBIT_LINE,0xffff,NORM_WIDTH);
  241. /* setcolor (key_matte_clr);*/
  242. polary[0][0]=sx2;polary[0][1]=sy2;polary[1][0]=sx3;polary[1][1]=sy3;
  243. polary[2][0]=sx4;polary[2][1]=sy4;polary[3][0]=sx1;polary[3][1]=sy1;
  244. /* setfillstyle(SOLID_FILL,key_matte_clr);*/
  245. /* fillpoly (4,(int far*) polary); */
  246. setcolor(C9);
  247. setfillstyle(SOLID_FILL,design_clr);
  248. fillpoly (4,(int far*) polary);
  249. return;}
  250.  
  251. /* ----------------------------------------------------------------------- */
  252. /* SUBROUTINE: MAP CARTESIAN COORDS TO PHYSICAL SCREEN COORDS */
  253.  
  254. void window(void){
  255. sx=sx+399;sy=sy+299;rx=screen_x/799;ry=screen_y/599;sx=sx*rx;
  256. sy=sy*ry;return;}
  257.  
  258. /* ----------------------------------------------------------------------- */
  259. /* SUBROUTINE: CHACK THE KEYBOARD BUFFER */
  260. void keyboard(void){
  261. if (bioskey(1)==0) return; else quit_pgm();}
  262.  
  263. /* ----------------------------------------------------------------------- */
  264. /* SUBROUTINE: GRACEFUL EXIT FROM PROGRAM */
  265.  
  266. void quit_pgm(void){
  267. cleardevice();restorecrtmode();exit(0);}
  268.  
  269. /* ----------------------------------------------------------------------- */
  270. /* SUBROUTINE: VGA/EGA/MCGA/CGA COMPATIBILITY MODULE */
  271.  
  272. void graphics_setup(void){
  273. int graphics_adapter,graphics_mode;
  274. detectgraph(&graphics_adapter,&graphics_mode);
  275. if (graphics_adapter==VGA) goto VGA_mode;
  276. if (graphics_mode==EGAHI) goto EGA_ECD_mode;
  277. if (graphics_mode==EGALO) goto EGA_SCD_mode;
  278. if (graphics_adapter==CGA) goto CGA_mode;
  279. if (graphics_adapter==MCGA) goto CGA_mode;
  280. goto abort_message;
  281.  
  282. VGA_mode:
  283. graphics_adapter=VGA;graphics_mode=VGAHI;
  284. initgraph(&graphics_adapter,&graphics_mode,"");
  285. x_res=640;y_res=480;mode_flag=1;
  286.         maxx=639;minx=0;maxy=479;miny=0;screen_x=639;screen_y=479;
  287.         setcolor(7);moveto(0,472);
  288.           outtext("Revisions by A. Helder");
  289.         moveto(472,472);
  290.         outtext("Press any key to quit");
  291.           moveto(144,0);
  292.           outtext("USING C TO MAP DESIGNS ONTO A SOLID 3D MODEL");
  293.         return;
  294.  
  295. EGA_ECD_mode:
  296. graphics_adapter=EGA;graphics_mode=EGAHI;
  297. initgraph(&graphics_adapter,&graphics_mode,"");
  298. x_res=640;y_res=350;mode_flag=2;
  299.         maxx=639;minx=0;maxy=349;miny=0;screen_x=639;screen_y=349;
  300.         setcolor(7);moveto(0,342);
  301.           outtext("Revisions by A. Helder");
  302.         moveto(472,342);
  303.         outtext ("Press any key to quit");
  304.           moveto(144,0);
  305.           outtext("USING C TO MAP DESIGNS ONTO A SOLID 3D MODEL");
  306.         return;
  307.  
  308. EGA_SCD_mode:
  309. graphics_adapter=EGA;graphics_mode=EGALO;
  310. initgraph(&graphics_adapter,&graphics_mode,"");
  311. x_res=640;y_res=200;mode_flag=3;
  312.         maxx=639;minx=0;maxy=199;miny=0;screen_x=639;screen_y=199;
  313.         setcolor(7);moveto(0,192);
  314.           outtext("Revisions by A. Helder");
  315.         moveto(472,192);
  316.         outtext("PRESS ANY KEY TO QUIT");
  317.           moveto(144,0);
  318.           outtext("USING C TO MAP DESIGNS ONTO A SOLID 3D MODEL");
  319.           return;
  320.  
  321. CGA_mode:
  322. graphics_adapter=CGA;graphics_mode=CGAC3;
  323. initgraph(&graphics_adapter,&graphics_mode,"");
  324. x_res=320;y_res=200;mode_flag=1;C7=3;
  325.         maxx=319;minx=0;maxy=199;miny=0;screen_x=319;screen_y=199;
  326.           C0=0;C1=1;C2=1;C3=1;C4=2;C5=1;C6=2;C7=3;C8=1;C9=3;C10=3;
  327.           C11=3,C12=3,C13=3;C14=3;C15=3;
  328.           setcolor(3);moveto(48,192);
  329.           outtext("Revisions by A. Helder");
  330.           moveto(104,0);
  331.           outtext ("SURFACE MAPPING");
  332.         return;
  333.  
  334. abort_message:
  335. printf("\n\nUnable to proceed - Requires VGA,EGA,CGA or MCGA adapter");
  336. printf("\nWith appropriate monitor");
  337. exit(0);
  338. }
  339.  
  340. /* ----------------------------------------------------------------------- */
  341. /* SUBROUTINE: MAP 640*480 TEMPLATE TO 2D SCREEN */
  342. void coords(void)
  343. {
  344. sx=sx*(x_res/640);sy=sy*(y_res/480);return;
  345. }
  346.  
  347. /* ----------------------------------------------------------------------- */
  348. /* SUBROUTINE: COPYRIGHT NOTICE */
  349.  
  350. int copyright[][3]={0x7c00,0x0000,0x0000,0x8231,
  351. 0x819c,0x645e,0xba4a,0x4252,0x96d0,0xa231,0x8252,0x955e,0xba4a,
  352. 0x43d2,0xf442,0x8231,0x825c,0x945e,0x7c00,0x0000,0x0000};
  353.  
  354. void notice(int x, int y){
  355. int a,b,c; int t1=0;
  356.  
  357. for (t1=0;t1<=6;t1++)
  358.     {
  359.     a=copyright[t1][0];b=copyright[t1][1];
  360.     c=copyright[t1][2];
  361.     setlinestyle(USERBIT_LINE,a,NORM_WIDTH);
  362.     moveto(x,y);lineto(x+15,y);
  363.     setlinestyle(USERBIT_LINE,b,NORM_WIDTH);
  364.     moveto(x+16,y);lineto(x+31,y);
  365.     setlinestyle(USERBIT_LINE,c,NORM_WIDTH);
  366.     moveto(x+32,y);lineto(x+47,y);y++;
  367.     };
  368. setlinestyle(USERBIT_LINE,0xFFFF,NORM_WIDTH);
  369. return;}
  370.  
  371.