home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / grafik / tc_3d / tc-02.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-13  |  7.6 KB  |  219 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. /* global variables */
  16. float x=0.0,y=0.0,z=0.0;                                    /* world coordinates */
  17. float sx=0.0,sy=0.0;                                            /* output of 3d perspective formulas */
  18. float xa=0.0,ya=0.0,za=0.0;                                /* temporary formulas in 3d formulas */
  19. float sxa=0.0,sya=0.0,sxb=0.0,syb=0.0;                    /* 2d line endpoints */
  20. float sxs=0.0,sys=0.0;                                        /* temp storage of 2d line startpoint */
  21. float d=1200.0;                                                /* angular perspective factor */
  22. double r1=5.68319;                                            /* yaw angle in radians */
  23. double r2=6.28319;                                 /* roll angle in radians */
  24. double r3=5.79778;                                            /* pitch angle in radians */
  25. double sr1=0.0,sr2=0.0,sr3=0.0;                            /* sine rotation factors */
  26. double cr1=0.0,cr2=0.0,cr3=0.0;                            /* cosine rotation factors */
  27. float mx=-90.0,my=-50.0,mz=-400.0;                        /* viewpoint position */
  28. int maxx=639,minx=0,maxy=199,miny=0;                    /* scaling viewport */
  29. float screen_x=639,screen_y=199;                            /* dimensions of screen mode */
  30. float rx=0.0,ry=0.0;                                            /* scaling values used in mapping routine */
  31. int t1=0,t2=0;                                                    /* loop counters */
  32. int p1=0;                                                        /* array indexer */
  33.  
  34. /* database of xyz world coordinates for 3d cube */
  35. int array1[][3]={
  36.  30,-30, 30, 30,-30,-30,-30,-30,-30,-30,-30, 30, 30,-30, 30,
  37.  30, 30,-30,-30, 30,-30,-30,-30,-30, 30,-30,-30, 30, 30,-30,
  38. -30, 30,-30,-30, 30, 30,-30,-30, 30,-30,-30,-30,-30, 30,-30,
  39. -30, 30, 30, 30, 30, 30, 30,-30, 30,-30,-30, 30,-30, 30, 30,
  40.  30, 30, 30, 30, 30,-30, 30,-30,-30, 30,-30, 30, 30, 30, 30,
  41. -30, 30,-30, 30, 30,-30, 30, 30, 30,-30, 30, 30,-30, 30,-30,};
  42.  
  43. int     c0=0,c1=1,c2=2,c3=3,c4=4,c5=5,c6=6,c7=7,c8=8,c9=9,c10=10,
  44.         c11=11,c12=12,c13=13,c14=14,c15=15,mode_flag=0;
  45. float sx1,sy1,sx2,sy2;
  46. float x_res,y_res;
  47.  
  48. /* declare global subroutines */
  49. void keyboard(void);void quit_pgm(void);void calc_3d(void);
  50. void rotation(void);void window(void);void graphics_setup(void);
  51. void notice(float x,float y);
  52.  
  53. /* ----------------------------------------------------------------------- */
  54. /* MAIN ROUTINE */
  55. main(){
  56. graphics_setup();                                                /* establish graphics mode */
  57. setcolor(c7);
  58.  
  59. rotation();                                                        /* calculate yaw, roll, pitch rotation factors */
  60. for (t2=1;t2<=6;t2++)
  61.     {
  62.     if (t2<4) setlinestyle (USERBIT_LINE,0x8888,NORM_WIDTH);
  63.     else setlinestyle (USERBIT_LINE,0xffff,NORM_WIDTH);
  64.     x=array1[p1][0];y=array1[p1][1];z=array1[p1][2];
  65.     calc_3d();window();sxa=sx;sya=sy;
  66.  
  67.     for (t1=1;t1<=4;t1++)
  68.         {
  69.         p1++;
  70.         x=array1[p1][0];y=array1[p1][1];z=array1[p1][2];
  71.         calc_3d();window();sxs=sx;sys=sy;sxb=sx;syb=sy;
  72.         moveto (sxa,sya);lineto(sxb,syb);
  73.         sxa=sxs;sya=sys;};
  74.     p1++;};
  75.  
  76. notice(0,0);
  77. for (t1=1;t1!=2 ;) keyboard();                            /* press any key to stop */
  78. quit_pgm();                                                   /* end the program gracefully */
  79. }
  80. /* ----------------------------------------------------------------------- */
  81. /* SUBROUTINE: CALCULATE SIN, COS, FACTORS */
  82. /* Pass: r1,r2,r3 viewing angles for yaw, roll, pitch
  83.    expressed in radians (0.0 - 6.28319).
  84.    Returns: sine and cosine factors */
  85.  
  86. void rotation(void){
  87. sr1=sin(r1);sr2=sin(r2);sr3=sin(r3);cr1=cos(r1);cr2=cos(r2);
  88. cr3=cos(r3);return;
  89. }
  90.  
  91. /* ----------------------------------------------------------------------- */
  92. /* SUBROUTINE: STANDARD 3D FORMULAS */
  93. /* Pass: x,y,z cartesian world coordinates.
  94.    Returns: sx,sy cartesian display coordinates.
  95.           x,y,z catesian view coordinates */
  96.  
  97. void calc_3d(void){
  98. x=(-1)*x;xa=cr1*x-sr1*z;za=sr1*x+cr1*z;x=cr2*xa+sr2*y;
  99. ya=cr2*y-sr2*xa;z=cr3*za-sr3*ya;y=sr3*za+cr3*ya;x=x+mx;y=y+my;
  100. z=z+mz;sx=d*x/z;sy=d*y/z;return;}
  101.  
  102. /* ----------------------------------------------------------------------- */
  103. /* SUBROUTINE: MAP CARTESIAN COORDINATES TO PHYSICAL SCREEN COORDINATES
  104. /* Pass: sx,sy cartesian display coordinated
  105.    Return: sx,sy unclipped physiacl display coordinates. Preserves 4:3
  106.          ratio of the 800:600 world coordinate universe.
  107.  
  108. void window(void){
  109. sx=sx+399;sy=sy+299;rx=screen_x/799;ry=screen_y/599;sx=sx*rx;
  110. sy=sy*ry;return;}
  111.  
  112. /* ----------------------------------------------------------------------- */
  113. /* SUBROUTINE: CHECK THE KEYBOARD BUFFER */
  114.  
  115. void keyboard(void){
  116. if (bioskey(1)==0) return; else quit_pgm();}
  117.  
  118. /* ----------------------------------------------------------------------- */
  119. /* SUBROUTINE: GRACEFUL EXIT FROM THE PROGRAM */
  120.  
  121. void quit_pgm(void){
  122. cleardevice();restorecrtmode();exit(0);}
  123.  
  124. /* ----------------------------------------------------------------------- */
  125. /* SUBROUTINE: VGA/EGA/MCGA/CGA COMPATIBILITY MODULE */
  126.  
  127. void graphics_setup(void){
  128. int graphics_adapter,graphics_mode;
  129. detectgraph(&graphics_adapter,&graphics_mode);
  130. if (graphics_adapter==VGA) goto VGA_mode;
  131. if (graphics_mode==EGAHI) goto EGA_ECD_mode;
  132. if (graphics_mode==EGALO) goto EGA_SCD_mode;
  133. if (graphics_adapter==CGA) goto CGA_mode;
  134. if (graphics_adapter==MCGA) goto CGA_mode;
  135. goto abort_message;
  136.  
  137. VGA_mode:
  138. graphics_adapter=VGA;graphics_mode=VGAHI;
  139. initgraph(&graphics_adapter,&graphics_mode,"");
  140. x_res=640;y_res=480;mode_flag=1;
  141.     maxx=639;minx=0;maxy=479;miny=0;screen_x=639;screen_y=479;
  142.     setcolor(7);moveto(0,472);
  143.     outtext("640*480 16-color VGA mode");
  144.     moveto(472,472);
  145.     outtext("Revisions by A. Helder");
  146.     moveto(160,0);
  147.     outtext("USING C TO GENERATE A 3D WIRE FRAME CUBE");
  148.     return;
  149.  
  150. EGA_ECD_mode:
  151. graphics_adapter=EGA;graphics_mode=EGAHI;
  152. initgraph(&graphics_adapter,&graphics_mode,"");
  153. x_res=640;y_res=350;mode_flag=2;
  154.     maxx=639;minx=0;maxy=349;miny=0;screen_x=639;screen_y=349;
  155.     setcolor(7);moveto(0,342);
  156.     outtext("Revisions by A. Helder");
  157.     moveto(472,342);
  158.     outtext ("Press any key to quit");
  159.     moveto(160,0);
  160.     outtext("USING C TO GENERATE A 3D WIRE FRAME CUBE");
  161.     return;
  162.  
  163. EGA_SCD_mode:
  164. graphics_adapter=EGA;graphics_mode=EGALO;
  165. initgraph(&graphics_adapter,&graphics_mode,"");
  166. x_res=640;y_res=200;mode_flag=3;
  167.     maxx=639;minx=0;maxy=199;miny=0;screen_x=639;screen_y=199;
  168.     setcolor(7);moveto(0,192);
  169.     outtext("Revisions by A. Helder");
  170.     moveto(472,192);
  171.     outtext("PRESS ANY KEY TO QUIT");
  172.     moveto(160,0);
  173.     outtext("USING C TO GENERATE A 3D WIRE FRAME CUBE");
  174.     return;
  175.  
  176. CGA_mode:
  177. graphics_adapter=CGA;graphics_mode=CGAC3;
  178. initgraph(&graphics_adapter,&graphics_mode,"");
  179. x_res=320;y_res=200;mode_flag=1;c7=3;
  180.     maxx=319;minx=0;maxy=199;miny=0;screen_x=319;screen_y=199;
  181.     setcolor(3);moveto(48,192);
  182.     outtext("Revisions by A. Helder");
  183.     moveto(88,0);
  184.     outtext ("3D WIRE FRAME CUBE");
  185.     return;
  186.  
  187. abort_message:
  188. printf("\n\nUnable to proceed - Requires VGA,EGA,CGA or MCGA adapter");
  189. printf("\nWith appropriate monitor");
  190. exit(0);
  191. }
  192.  
  193. /* ----------------------------------------------------------------------- */
  194. /* SUBROUTINE: COPYRIGHT NOTICE */
  195.  
  196. int copyright[][3]={0x7c00,0x0000,0x0000,0x8231,
  197. 0x819c,0x645e,0xba4a,0x4252,0x96d0,0xa231,0x8252,0x955e,0xba4a,
  198. 0x43d2,0xf442,0x8231,0x825c,0x945e,0x7c00,0x0000,0x0000};
  199.  
  200. void notice(float x, float y){
  201. int a,b,c; int t1=0;
  202.  
  203. for (t1=0;t1<=6;t1++)
  204.     {
  205.     a=copyright[t1][0];b=copyright[t1][1];
  206.     c=copyright[t1][2];
  207.     setlinestyle(USERBIT_LINE,a,NORM_WIDTH);
  208.     moveto(x,y);lineto(x+15,y);
  209.     setlinestyle(USERBIT_LINE,b,NORM_WIDTH);
  210.     moveto(x+16,y);lineto(x+31,y);
  211.     setlinestyle(USERBIT_LINE,c,NORM_WIDTH);
  212.     moveto(x+32,y);lineto(x+47,y);y++;
  213.     };
  214. setlinestyle(USERBIT_LINE,0xFFFF,NORM_WIDTH);
  215. return;}
  216.  
  217.  
  218. /* ----------------------------------------------------------------------- */
  219. /* END OF SOURCE CODE */