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

  1. /* tc-02.c2
  2.  
  3. This program draws a 3d wire-frame cube.  No hidden surface
  4. removal is used */
  5.  
  6. /* INCLUDE FILES */
  7. #include <bios.h>
  8. #include <stdio.h>
  9. #include <graphics.h>
  10. #include <math.h>
  11. #include <process.h>
  12.  
  13. /* ----------------------------------------------------------------------- */
  14. /* DECLARATIONS */
  15. /* declare global varaiables */
  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 coordinates */
  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=000.0,my=000.0,mz=-450.0;                                /* viewpoint position - may have to be changed */
  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. float        rx=0.0,ry=0.0;                                     /* scaling values used in mapping routine */
  34. int        t=0,t1=0,t2=0;                                                    /* loop counters */
  35. int        p1=0;                                                                /* array indexer */
  36. int        C0=0,C1=1,C2=2,C3=3,C4=4,C5=5,C6=6,C7=7,C8=8,C9=9,    /* color varaibles for graphics_setup */
  37.             C10=10,C11=11,C12=12,C13=13,C14=14,C15=15,
  38.             mode_flag=0;
  39. int        key_matte_clr=6;                                                /* exlusive key matte color */
  40. int        edge_clr=7;                                                        /* used to draw edges of models */
  41. int        solid_clr=0;                                                    /* used to fill surfaces of solid models */
  42. float        x_res,y_res;                                                    /* used for 2d mapping from 640*480 template */
  43. float        sp=0.0;                                                            /* visibilty factor in hidden surface routine */
  44. float        sp1=0.0,sp2=0.0,sp3=0.0;                                    /* temporary values of sp */
  45.  
  46. /* declare database arrays */
  47. int        array1[][3]={                                                    /* database for vertices of 3d model */
  48. 30,-30,30,    30,30,30,    -30,30,30,    -30,-30,30,
  49. 30,30,-30,    -30,30,-30,    -30,-30,-30,    30,-30,-30};
  50.  
  51. int        array2[][3]={                                                    /* database of fill origins for 3d models */
  52. 0,-30,0,    0,0,-30,    -30,0,0,    0,0,30,    30,0,0,    0,30,0};
  53. float        B1[8][3];                                                        /* array of 8 sets of xyz view coordinates */
  54. float        B2[8][2];                                                        /* array of 8 sets of sx,sy display coordinates */
  55. float        B3[6][2];                                                        /* array of 6 sets of sx,sy fill coordinates */
  56.  
  57. /* declare global subroutines */
  58. void keyboard(void);void quit_pgm(void);void calc_3d(void);
  59. void rotation(void);void window(void);void graphics_setup(void);
  60. void coords(void);void draw_poly(void);void notice(int x,int y);
  61. void visibility_test(void);void store_coords(void);
  62.  
  63. /* ----------------------------------------------------------------------- */
  64. /* MAIN ROUTINE */
  65. main(){
  66. graphics_setup();                                                            /* establish graphics mode */
  67. setviewport(0,0,maxx,maxy,1);                                            /* define clipping boundaries */
  68. key_matte_clr=C6;                                                            /* define key matte color */
  69. edge_clr=C7;solid_clr=C0;                                                /* define modeling colors - can be changed */
  70. store_coords();                                                            /* calculate and store 3d coordinates in arrays */
  71.  
  72. surface0:
  73. x1=B1[7][0];y01=B1[7][1];z1=B1[7][2];x2=B1[0][0];y2=B1[0][1];
  74. z2=B1[0][2];x3=B1[3][0];y3=B1[3][1];z3=B1[3][2];visibility_test();
  75. /* sp=sp*(-1); */
  76. if (sp>0) goto surface1;
  77. sx1=B2[7][0];sy1=B2[7][1];sx2=B2[0][0];sy2=B2[0][1];sx3=B2[3][0];
  78. sy3=B2[3][1];sx4=B2[6][0];sy4=B2[6][1];sx5=B3[0][0];sy5=B3[0][1];
  79. draw_poly();
  80.  
  81. surface1:
  82. x1=B1[6][0];y01=B1[6][1];z1=B1[6][2];x2=B1[5][0];y2=B1[5][1];
  83. z2=B1[5][2];x3=B1[4][0];y3=B1[4][1];z3=B1[4][2];visibility_test();
  84. /* sp=sp*(-1); */
  85. if (sp>0) goto surface2;
  86. sx1=B2[6][0];sy1=B2[6][1];sx2=B2[5][0];sy2=B2[5][1];sx3=B2[4][0];
  87. sy3=B2[4][1];sx4=B2[7][0];sy4=B2[7][1];sx5=B3[1][0];sy5=B3[1][1];
  88. draw_poly();
  89.  
  90. surface2:
  91. x1=B1[3][0];y01=B1[3][1];z1=B1[3][2];x2=B1[2][0];y2=B1[2][1];
  92. z2=B1[2][2];x3=B1[5][0];y3=B1[5][1];z3=B1[5][2];visibility_test();
  93. /* sp=sp*(-1); */
  94. if (sp>0) goto surface3;
  95. sx1=B2[3][0];sy1=B2[3][1];sx2=B2[2][0];sy2=B2[2][1];sx3=B2[5][0];
  96. sy3=B2[5][1];sx4=B2[6][0];sy4=B2[6][1];sx5=B3[2][0];sy5=B3[2][1];
  97. draw_poly();
  98.  
  99. surface3:
  100. x1=B1[0][0];y01=B1[0][1];z1=B1[0][2];x2=B1[1][0];y2=B1[1][1];
  101. z2=B1[1][2];x3=B1[2][0];y3=B1[2][1];z3=B1[2][2];visibility_test();
  102. /* sp=sp*(-1); */
  103. if (sp>0) goto surface4;
  104. sx1=B2[0][0];sy1=B2[0][1];sx2=B2[1][0];sy2=B2[1][1];sx3=B2[2][0];
  105. sy3=B2[2][1];sx4=B2[3][0];sy4=B2[3][1];sx5=B3[3][0];sy5=B3[3][1];
  106. draw_poly();
  107.  
  108. surface4:
  109. x1=B1[7][0];y01=B1[7][1];z1=B1[7][2];x2=B1[4][0];y2=B1[4][1];
  110. z2=B1[4][2];x3=B1[1][0];y3=B1[1][1];z3=B1[1][2];visibility_test();
  111. /* sp=sp*(-1); */
  112. if (sp>0) goto surface5;
  113. sx1=B2[7][0];sy1=B2[7][1];sx2=B2[4][0];sy2=B2[4][1];sx3=B2[1][0];
  114. sy3=B2[1][1];sx4=B2[0][0];sy4=B2[0][1];sx5=B3[4][0];sy5=B3[4][1];
  115. draw_poly();
  116.  
  117. surface5:
  118. x1=B1[1][0];y01=B1[1][1];z1=B1[1][2];x2=B1[4][0];y2=B1[4][1];
  119. z2=B1[4][2];x3=B1[5][0];y3=B1[5][1];z3=B1[5][2];visibility_test();
  120. /* sp=sp*(-1); */
  121. if (sp>0) goto surfaces_done;
  122. sx1=B2[1][0];sy1=B2[1][1];sx2=B2[4][0];sy2=B2[4][1];sx3=B2[5][0];
  123. sy3=B2[5][1];sx4=B2[2][0];sy4=B2[2][1];sx5=B3[5][0];sy5=B3[5][1];
  124. draw_poly();
  125.  
  126. surfaces_done:setcolor(C7);notice(0,0);
  127. for (t1=1;t1!=2;) keyboard();                               /* press any key to stop */
  128. quit_pgm();}
  129.  
  130. /* ----------------------------------------------------------------------- */
  131. /* SUBROUTINE: CALCULATE SIN, COS, FACTORS */
  132. /* Pass: r1,r2,r3 viewing angles for yaw, roll, pitch
  133.    expressed in radians (0.0 - 6.28319).
  134.    Returns: sine and cosine factors */
  135.  
  136. void rotation(void){
  137. sr1=sin(r1);sr2=sin(r2);sr3=sin(r3);cr1=cos(r1);cr2=cos(r2);
  138. cr3=cos(r3);return;
  139. }
  140.  
  141. /* ----------------------------------------------------------------------- */
  142. /* SUBROUTINE: STANDARD 3D FORMULAS */
  143. /* Pass: x,y,z cartesian world coordinates.
  144.    Returns: sx,sy cartesian display coordinates.
  145.           x,y,z catesian view coordinates */
  146.  
  147. void calc_3d(void){
  148. x=(-1)*x;xa=cr1*x-sr1*z;za=sr1*x+cr1*z;x=cr2*xa+sr2*y;
  149. ya=cr2*y-sr2*xa;z=cr3*za-sr3*ya;y=sr3*za+cr3*ya;x=x+mx;y=y+my;
  150. z=z+mz;sx=d*x/z;sy=d*y/z;return;}
  151.  
  152. /* ----------------------------------------------------------------------- */
  153. /* SUBROUTINE: CALCULATE & STORE VIEW COORDINATES & DISPLAY COORDINATES */
  154. void store_coords(void){
  155. rotation();                                                                    /* calculate yaw, roll, pitch rotation factors */
  156. for (t=0;t<=7;t++){
  157.     x=array1[t][0];y=array1[t][1];z=array1[t][2];
  158.     calc_3d();window();
  159.     B1[t][0]=x;B1[t][1]=y;B1[t][2]=z;                                /* store view coordinates */
  160.     B2[t][0]=sx;B2[t][1]=sy;                                            /* store diskplay coordinates */
  161.     };
  162. for (t=0;t<=5;t++){
  163.     x=array2[t][0];y=array2[t][1];z=array2[t][2];
  164.     calc_3d();window();
  165.     B3[t][0]=sx;B3[t][1]=sy;
  166.     };
  167. return;}
  168.  
  169. /* ----------------------------------------------------------------------- */
  170. /* HIDDEN SURFACE VISIBILTY TEST */
  171. void visibility_test(void){
  172. sp1=x1*(y2*z3-y3*z2);sp1=(-1)*sp1;sp2=x2*(y3*z1-y01*z3);
  173. sp3=x3*(y01*z2-y2*z1);sp=sp1-sp2-sp3;return;}
  174.  
  175. /* ----------------------------------------------------------------------- */
  176. /* SUBROUTINE: DRAW 4-SIDED SOLID POLYGON IN 3D SPACE */
  177.  
  178. void draw_poly(void){
  179. setlinestyle(USERBIT_LINE,0xffff,NORM_WIDTH);
  180. setcolor(key_matte_clr);moveto(sx1,sy1);
  181. lineto(sx2,sy2);lineto(sx3,sy3);lineto(sx4,sy4);lineto(sx1,sy1);
  182. setfillstyle(SOLID_FILL,key_matte_clr);floodfill(sx5,sy5,key_matte_clr);
  183. setcolor(edge_clr);moveto(sx1,sy1);
  184. lineto(sx2,sy2);lineto(sx3,sy3);lineto(sx4,sy4);lineto(sx1,sy1);
  185. setfillstyle(SOLID_FILL,solid_clr);floodfill(sx5,sy5,edge_clr);
  186. return;
  187. }
  188.  
  189. /* ----------------------------------------------------------------------- */
  190. /* SUBROUTINE: MAP CARTESIAN COORDINATES TO PHYSICAL SCREEN COORDINATES
  191. /* Pass: sx,sy cartesian display coordinated
  192.    Return: sx,sy unclipped physiacl display coordinates. Preserves 4:3
  193.          ratio of the 800:600 world coordinate universe.*/
  194.  
  195. void window(void){
  196. sx=sx+399;sy=sy+299;rx=screen_x/799;ry=screen_y/599;sx=sx*rx;
  197. sy=sy*ry;return;}
  198.  
  199. /* ----------------------------------------------------------------------- */
  200. /* SUBROUTINE: CHECK THE KEYBOARD BUFFER */
  201.  
  202. void keyboard(void){
  203. if (bioskey(1)==0) return; else quit_pgm();}
  204.  
  205. /* ----------------------------------------------------------------------- */
  206. /* SUBROUTINE: GRACEFUL EXIT FROM THE PROGRAM */
  207.  
  208. void quit_pgm(void){
  209. cleardevice();restorecrtmode();exit(0);}
  210.  
  211. /* ----------------------------------------------------------------------- */
  212. /* SUBROUTINE: VGA/EGA/MCGA/CGA COMPATIBILITY MODULE */
  213.  
  214. void graphics_setup(void){
  215. int graphics_adapter,graphics_mode;
  216. detectgraph(&graphics_adapter,&graphics_mode);
  217. if (graphics_adapter==VGA) goto VGA_mode;
  218. if (graphics_mode==EGAHI) goto EGA_ECD_mode;
  219. if (graphics_mode==EGALO) goto EGA_SCD_mode;
  220. if (graphics_adapter==CGA) goto CGA_mode;
  221. if (graphics_adapter==MCGA) goto CGA_mode;
  222. goto abort_message;
  223.  
  224. VGA_mode:
  225. graphics_adapter=VGA;graphics_mode=VGAHI;
  226. initgraph(&graphics_adapter,&graphics_mode,"");
  227. x_res=640;y_res=480;mode_flag=1;
  228.     maxx=639;minx=0;maxy=479;miny=0;screen_x=639;screen_y=479;
  229.     setcolor(7);moveto(0,472);
  230.     outtext("Revisions by A. Helder");
  231.     moveto(472,472);
  232.     outtext("Presss any key to quit");
  233.     moveto(160,0);
  234.     outtext("USING C TO GENERATE A SOLID 3D MODEL");
  235.     return;
  236.  
  237. EGA_ECD_mode:
  238. graphics_adapter=EGA;graphics_mode=EGAHI;
  239. initgraph(&graphics_adapter,&graphics_mode,"");
  240. x_res=640;y_res=350;mode_flag=2;
  241.     maxx=639;minx=0;maxy=349;miny=0;screen_x=639;screen_y=349;
  242.     setcolor(7);moveto(0,342);
  243.     outtext("Revisions by A. Helder");
  244.     moveto(472,342);
  245.     outtext ("Press any key to quit");
  246.     moveto(160,0);
  247.     outtext("USING C TO GENERATE A SOLID 3D MODEL");
  248.     return;
  249.  
  250. EGA_SCD_mode:
  251. graphics_adapter=EGA;graphics_mode=EGALO;
  252. initgraph(&graphics_adapter,&graphics_mode,"");
  253. x_res=640;y_res=200;mode_flag=3;
  254.     maxx=639;minx=0;maxy=199;miny=0;screen_x=639;screen_y=199;
  255.     setcolor(7);moveto(0,192);
  256.     outtext("Revisions by A. Helder");
  257.     moveto(472,192);
  258.     outtext("PRESS ANY KEY TO QUIT");
  259.     moveto(160,0);
  260.     outtext("USING C TO GENERATE A SOLID 3D MODEL");
  261.     return;
  262.  
  263. CGA_mode:
  264. graphics_adapter=CGA;graphics_mode=CGAC3;
  265. initgraph(&graphics_adapter,&graphics_mode,"");
  266. x_res=320;y_res=200;mode_flag=1;C7=3;
  267.     maxx=319;minx=0;maxy=199;miny=0;screen_x=319;screen_y=199;
  268.     setcolor(3);moveto(48,192);
  269.     outtext("Revisions by A. Helder");
  270.     moveto(88,0);
  271.     outtext ("SOLID 3D MODEL");
  272.     return;
  273.  
  274. abort_message:
  275. printf("\n\nUnable to proceed - Requires VGA,EGA,CGA or MCGA adapter");
  276. printf("\nWith appropriate monitor");
  277. exit(0);
  278. }
  279.  
  280. /* ----------------------------------------------------------------------- */
  281. /* SUBROUTINE: MAP 640*480 TEMPLATE TO 2D SCREEN */
  282. void coords(void){
  283. sx=sx*(x_res/640);sy=sy*(y_res/480);return;}
  284.  
  285. /* ----------------------------------------------------------------------- */
  286. /* SUBROUTINE: COPYRIGHT NOTICE */
  287.  
  288. int copyright[][3]={0x7c00,0x0000,0x0000,0x8231,
  289. 0x819c,0x645e,0xba4a,0x4252,0x96d0,0xa231,0x8252,0x955e,0xba4a,
  290. 0x43d2,0xf442,0x8231,0x825c,0x945e,0x7c00,0x0000,0x0000};
  291.  
  292. void notice(int x, int y){
  293. int a,b,c; int t1=0;
  294.  
  295. for (t1=0;t1<=6;t1++)
  296.     {
  297.     a=copyright[t1][0];b=copyright[t1][1];
  298.     c=copyright[t1][2];
  299.     setlinestyle(USERBIT_LINE,a,NORM_WIDTH);
  300.     moveto(x,y);lineto(x+15,y);
  301.     setlinestyle(USERBIT_LINE,b,NORM_WIDTH);
  302.     moveto(x+16,y);lineto(x+31,y);
  303.     setlinestyle(USERBIT_LINE,c,NORM_WIDTH);
  304.     moveto(x+32,y);lineto(x+47,y);y++;
  305.     };
  306. setlinestyle(USERBIT_LINE,0xFFFF,NORM_WIDTH);
  307. return;}
  308.  
  309.  
  310. /* ----------------------------------------------------------------------- */
  311. /* END OF SOURCE CODE */