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

  1. /* TC-14.C */
  2. /* This program demonstrates high-speed frame
  3.     animation of a 3d intuitively-shaded object
  4.  
  5.     Supports VGA and EGA graphics adapters and
  6.     monitors.  The software uses 4 pages in the 640*200
  7.     16-color mode.
  8.  
  9.     The speed of the microprocessor has a dramatic affect
  10.     uppon the required animation speed.
  11.  
  12.     The radial pre-sort method of hidden surface removal is
  13.     used */
  14.  
  15. /* ----------------------------------------------------------------------- */
  16. /* INCLUDE FILES */
  17. #include <process.h>
  18. #include <bios.h>
  19. #include <stdio.h>
  20. #include <graphics.h>
  21. #include <math.h>
  22.  
  23. /* ----------------------------------------------------------------------- */
  24. /* DECLARATIONS */
  25. void keyboard(void);void quit_pgm(void);
  26. void notice(int x,int y);void coords(void);
  27. void graphics_setup(void);void labels(void);void draw_object(void);
  28. void calc_3d(void);void rotation(void);void window(void);
  29. void get_coords(void);void draw_poly(void);void animVGAEGA(void);
  30.  
  31. int        t1=1,t2=2,t3=1;                                                /* loop counters */
  32. float        x_res,y_res;                                                    /* used by compatibility mapping routines */
  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. int        CLR,CLR1,CLR2,CLR3,EDGE;
  37.  
  38. int        p1=0,p2=0;                                                        /* pointer into arrays */
  39. float        x=0.0,y=0.0,z=0.0;                                              /* world coordinates */
  40. float        sx=0.0,sy=0.0;                                                    /* output of 3d perspective formula */
  41. float        xa=0.0,ya=0.0,za=0.0;                                        /* temporary values in 3d formulas */
  42. float        sxa=0.0,sya=0.0,sxb=0.0,syb=0.0;                            /* 2d line endpoints */
  43. float        sxs=0.0,sys=0.0;                                                /* temp storage od 2d line endpoint */
  44. float        d=1200.0;                                                        /* angular perspective value */
  45. double    r1=.48539;                                                        /* yaw angle in radians */
  46. double    r2=6.28319;                                                        /* roll angle in radians */
  47. double    r3=5.79778;                                                        /* pitch angle in radians */
  48. double    sr1=0.0,sr2=0.0,sr3=0.0;                                    /* sine rotation factors */
  49. double    cr1=0.0,cr2=0.0,cr3=0.0;                                    /* cosine rotation factors */
  50. float        mx=0.0,my=-5.0,mz=-250.0;                                    /* viewpoint position */
  51. int        maxx=639,minx=0,maxy=199,miny=0;                            /* scaling viewport */
  52. float        screen_x=639,screen_y=199;                                    /* dimensions of screen mode */
  53. float        rx=0.0,ry=0.0;                                                    /* scaling values used in mapping routine */
  54.  
  55. int        array_xyz[][3]={
  56.             20,-20,-30,    20,20,-30,    20,20,-20,    20,-20,-20,    20,-20,-30,    20,0,-25,
  57.             20,-20,-30,    -20,-20,-30,    -20,-20,-20,    20,-20,-20,    20,-20,-30,    0,-20,-25,
  58.  
  59.             -30,30,-30,    30,30,-30,    30,30,-20,    -30,30,-20,    -30,30,-30,    0,30,-25,
  60.             -30,30,-30,    -30,30,-20,    -30,-30,-20,    -30,-30,-30,    -30,30,-30,    -30,0,-25,
  61.             -30,30,-20,    30,30,-20,    30,20,-20,    -30,20,-20,    -30,30,-20,    0,25,-20,
  62.             30,30,-20,    30,-30,-20,    20,-30,-20,    20,30,-20,    30,30,-20,    25,0,-20,
  63.             30,-30,-20,    -30,-30,-20,    -30,-20,-20,    30,-20,-20,    30,-30,-20,    0,-25,-20,
  64.             -30,-30,-20,    -30,30,-20,    -20,30,-20,    -20,-30,-20,    -30,-30,-20,    -25,0,-20,
  65.  
  66.             20,-30,-20,    20,-20,-20,    20,-20,20,    20,-30,20,    20,-30,-20,    20,-25,0,
  67.  
  68.             -30,-30,30,    -30,-20,30,    30,-20,30,    30,-30,30,    -30,-30,30,    0,-25,30,
  69.             -30,-30,-30,    -30,-20,-30,    -30,-20,30,    -30,-30,30,    -30,-30,-30,    -30,-25,0,
  70.             -30,-20,-20,    -20,-20,-20,    -20,-20,30,    -30,-20,30,    -30,-20,-20,    -25,-20,0,
  71.             30,-20,-20,    30,-20,30,    20,-20,30,    20,-20,-20,    30,-20,-20,    25,-20,0,
  72.             -30,-20,30,    -30,-20,20,    30,-20,20,    30,-20,30,    -30,-20,30,    0,-20,25,
  73.  
  74.             20,30,-20,    20,30,30,    20,20,30,    20,20,-20,    20,30,-20,    20,25,0,
  75.             20,30,30,    20,-20,30,    20,-20,20,    20,30,20,    20,30,30,    20,0,25,
  76.  
  77.             -30,30,-30,    -20,30,-30,    -20,30,30,    -30,30,30,    -30,30,-30,    -25,30,0,
  78.             20,30,-30,    30,30,-30,    30,30,30,    20,30,30,    20,30,-30,    25,30,0,
  79.             -30,30,30,    -30,30,20,    30,30,20,    30,30,30,    -30,30,30,    0,30,25,
  80.  
  81.             -30,-30,30,    -30,30,30,    -20,30,30,    -20,-30,30,    -30,-30,30,    -25,0,30,
  82.             20,-30,30,    20,30,30,    30,30,30,    30,-30,30,    20,-30,30,    25,0,30,
  83.             -30,30,30,    30,30,30,    30,20,30,    -30,20,30,    -30,30,30,    0,25,30,
  84.             -30,20,-30,    -30,30,-30,    -30,30,30,    -30,20,30,    -30,20,-30,    -30,25,0,
  85.             -30,-30,20,    -30,30,20,    -30,30,30,    -30,-30,30,    -30,-30,20,    -30,0,25};
  86.  
  87. /* ----------------------------------------------------------------------- */
  88. /* MAIN ROUTINE */
  89.  
  90. main(){
  91. graphics_setup();
  92. setviewport(0,0,maxx,maxy,1);
  93. CLR1=C15;CLR2=C8;CLR3=C7;EDGE=C7;                                    /* color scheme for rendering */
  94.  
  95. setvisualpage(0);setactivepage(0);cleardevice();
  96. r1=.58539;r2=6.28319;r3=5.79778;labels();draw_object();keyboard();
  97.  
  98. setvisualpage(1);setactivepage(1);cleardevice();
  99. r1=r1+.08727;r2=6.28319;r3=5.79778;labels();draw_object();keyboard();
  100.  
  101. setvisualpage(2);setactivepage(2);cleardevice();
  102. r1=r1+.08727;r2=6.28319;r3=5.79778;labels();draw_object();keyboard();
  103.  
  104. setvisualpage(3);setactivepage(3);cleardevice();
  105. r1=r1+.08727;r2=6.28319;r3=5.79778;labels();draw_object();keyboard();
  106.  
  107. setvisualpage(0);setactivepage(0);                                    /* reset to frame 0 */
  108. for (t1=1;t1<=30000;t1++);                                                /* puase */
  109. animVGAEGA();
  110. quit_pgm();
  111. }
  112.  
  113. /* ----------------------------------------------------------------------- */
  114. /* SUBROUTINE: DISPLAY ALPHANUMERIC LABELS */
  115.  
  116. void labels(void){
  117. setcolor(C7);moveto(0,192);
  118. outtext("Revisions by A. Helder");
  119. moveto(168,0);setcolor(C12);
  120. outtext("USING C FOR HIGH-SPEED FRAME ANIMATION");
  121. moveto(464,192);outtext("Press any key to stop.");
  122. moveto(176,16);setcolor(C14);
  123. outtext("Animated rotation of solid 3d model");
  124. setcolor(C7);return;
  125. }
  126.  
  127. /* ----------------------------------------------------------------------- */
  128. /* SUBROUTINE: DRAW THE GRAPHICS */
  129.  
  130. void draw_object(void){
  131.  
  132. rotation();
  133. p1=0;
  134.  
  135. CLR=CLR3;EDGE=CLR3;draw_poly();                                        /* step 1 */
  136. CLR=CLR1;EDGE=CLR1;draw_poly();
  137.  
  138. draw_poly();                                                                /* step 2 */
  139. CLR=CLR3;EDGE=CLR3;draw_poly();
  140. CLR=CLR2;EDGE=CLR2;for (t3=1;t3<=4;t3++)
  141.     {
  142.     draw_poly();
  143.     }
  144.  
  145. CLR=CLR3;EDGE=CLR3;draw_poly();                                        /* step 3 */
  146.  
  147. CLR=CLR2;EDGE=CLR2;draw_poly();                                        /* step 4 */
  148. CLR=CLR3;EDGE=CLR3;draw_poly();
  149. CLR=CLR1;EDGE=CLR1;for (t3=1;t3<=3;t3++)
  150.     {
  151.     draw_poly();
  152.     }
  153.  
  154. CLR=CLR3;EDGE=CLR3;for (t3=1;t3<=2;t3++)                            /* step 5 */
  155.     {
  156.     draw_poly();
  157.     }
  158.  
  159. CLR=CLR1;EDGE=CLR1;for (t3=1;t3<=3;t3++)                            /* step 6 */
  160.     {
  161.     draw_poly();
  162.     }
  163.  
  164. CLR=CLR2;EDGE=CLR2;for (t3=1;t3<=3;t3++){draw_poly();}
  165. CLR=CLR3;EDGE=CLR3;for (t3=1;t3<=2;t3++){draw_poly();}
  166.  
  167. setcolor(C7);sx=5;sy=400;coords();notice(sx,sy);return;}        /* for faster animation, delete ";notice (sx,sy)"
  168.  
  169. /* ----------------------------------------------------------------------- */
  170. /* SUBROUTINE: DRAW AND FILL POLYGON IN 3D SPACE */
  171.  
  172. void draw_poly(void){
  173.  
  174. setcolor(C13);
  175. setfillstyle(SOLID_FILL,C13);
  176. get_coords();                                                                /* retrieve xyz vertex coords from database */
  177. calc_3d();                                                                    /* 3d rotation,translation,projection */
  178. window();                                                                    /* map display coords to fil 4:3 screen */
  179. sxa=sx;sya=sy;                                                                /* line start point */
  180. for (t1=1;t1<=4;t1++){                                                    /* draw 4 lines in 3d */
  181.     get_coords();                                                            /* retrieve xyz vertex coordinates from database */
  182.     calc_3d();                                                                /* 3d rotation, translation, projection */
  183.     window();                                                                /* map diaplsy coordinates to fit 4:3 screen */
  184.     sxs=sx;sys=sy;sxb=sx;syb=sy;                                        /* line is sxa,sya to sxb,syb */
  185.     moveto(sxa,sya);lineto(sxb,syb);                                    /* draw line in 3d */
  186.     sxa=sxs;sya=sys;                                                        /* define next start point */
  187.     }
  188.  
  189. get_coords();                                                                /* retrieve xyz area fill from database */
  190. calc_3d();window();floodfill(sx,sy,C13);                            /* area fill */
  191.  
  192. setcolor(CLR);EDGE+CLR;p1=p1-6;                                        /* reset index pointer */
  193. setfillstyle(SOLID_FILL,CLR);
  194. get_coords();calc_3d();window();sxa=sx;sya=sy;
  195. for (t1=1;t1<=4;t1++){
  196.     get_coords();calc_3d();window();sxs=sx;sys=sy;sxb=sx;syb=sy;
  197.     moveto(sxa,sya);lineto(sxb,syb);sxa=sxs;sya=sys;
  198.     }
  199.  
  200. get_coords();calc_3d();window();floodfill(sx,sy,EDGE);
  201. keyboard();
  202. return;
  203. }
  204.  
  205. /* ----------------------------------------------------------------------- */
  206. /* SUBROUTINE: FRAME ANIMATION MANAGER FOR VGA AND EGA */
  207.  
  208. void animVGAEGA(void){
  209. for (t1=1;t1!=2;){
  210.     setvisualpage(1);for (t2=1;t2<=30000;t2++);
  211.     setvisualpage(2);for (t2=1;t2<=30000;t2++);
  212.     setvisualpage(3);for (t2=1;t2<=30000;t2++);
  213.     setvisualpage(2);for (t2=1;t2<=30000;t2++);keyboard();
  214.     setvisualpage(1);for (t2=1;t2<=30000;t2++);
  215.     setvisualpage(0);for (t2=1;t2<=30000;t2++);
  216.     /* setvisualpage(1);for (t2=1;t2<=10000;t2++);keyboard();};*/
  217.     }
  218.  
  219. return;
  220. }
  221.  
  222. /* ----------------------------------------------------------------------- */
  223. /* SUBROUTINE: RETRIEVE XYZ WORLD COORDINATES DROM DATABASE */
  224.  
  225. void get_coords(void){
  226. x=array_xyz[p1][0];y=array_xyz[p1][1];z=array_xyz[p1][2];
  227. p1++;return;}
  228.  
  229. /* ----------------------------------------------------------------------- */
  230. /* SUBROUTINE: CALCULATE SIN,COS FACTORS */
  231.  
  232. void rotation(void){
  233. sr1=sin(r1);sr2=sin(r2);sr3=sin(r3);cr1=cos(r1);cr2=cos(r2);
  234. cr3=cos(r3);return;}
  235.  
  236. /* ----------------------------------------------------------------------- */
  237. /* SUBROUTINE: STANDARD 3D FORMULAS */
  238. /* Pass: x,y,z cartesian world coordinates.
  239.    Returns: sx,sy cartesian display coordinates.
  240.           x,y,z catesian view coordinates */
  241.  
  242. void calc_3d(void){
  243. x=(-1)*x;xa=cr1*x-sr1*z;za=sr1*x+cr1*z;x=cr2*xa+sr2*y;
  244. ya=cr2*y-sr2*xa;z=cr3*za-sr3*ya;y=sr3*za+cr3*ya;x=x+mx;y=y+my;
  245. z=z+mz;sx=d*x/z;sy=d*y/z;return;}
  246.  
  247. /* ----------------------------------------------------------------------- */
  248. /* SUBROUTINE: MAP CARTESIAN COORDS TO PHYSICAL SCREEN COORDS */
  249.  
  250. void window(void){
  251. sx=sx+399;sy=sy+299;rx=screen_x/799;ry=screen_y/599;sx=sx*rx;
  252. sy=sy*ry;return;}
  253.  
  254. /* ----------------------------------------------------------------------- */
  255. /* SUBROUTINE: CHACK THE KEYBOARD BUFFER */
  256. void keyboard(void){
  257. if (bioskey(1)==0) return; else quit_pgm();}
  258.  
  259. /* ----------------------------------------------------------------------- */
  260. /* SUBROUTINE: GRACEFUL EXIT FROM PROGRAM */
  261.  
  262. void quit_pgm(void){
  263. cleardevice();restorecrtmode();exit(0);}
  264.  
  265. /* ----------------------------------------------------------------------- */
  266. /* SUBROUTINE: VGA/EGA/MCGA/CGA COMPATIBILITY MODULE */
  267.  
  268. void graphics_setup(void){
  269. int graphics_adapter,graphics_mode;
  270. detectgraph(&graphics_adapter,&graphics_mode);
  271. if (graphics_adapter==VGA) goto EGA_SCD_mode;
  272. if (graphics_mode==EGAHI) goto EGA_SCD_mode;
  273. if (graphics_mode==EGALO) goto EGA_SCD_mode;
  274. if (graphics_adapter==CGA) goto abort_message;
  275. if (graphics_adapter==MCGA) goto abort_message;
  276. goto abort_message;
  277.  
  278. EGA_SCD_mode:
  279. graphics_adapter=EGA;graphics_mode=EGALO;
  280. initgraph(&graphics_adapter,&graphics_mode,"");
  281. x_res=640;y_res=200;mode_flag=3;
  282. maxx=639;minx=0;maxy=199;miny=0;screen_x=639;screen_y=199;
  283. return;
  284.  
  285. abort_message:
  286. printf("\n\nUnable to proceed - Requires VGA,EGA,CGA or MCGA adapter");
  287. printf("\nWith appropriate monitor");
  288. exit(0);
  289. }
  290.  
  291. /* ----------------------------------------------------------------------- */
  292. /* SUBROUTINE: MAP 640*480 TEMPLATE TO 2D SCREEN */
  293. void coords(void)
  294. {
  295. sx=sx*(x_res/640);sy=sy*(y_res/480);return;
  296. }
  297.  
  298. /* ----------------------------------------------------------------------- */
  299. /* SUBROUTINE: COPYRIGHT NOTICE */
  300.  
  301. int copyright[][3]={0x7c00,0x0000,0x0000,0x8231,
  302. 0x819c,0x645e,0xba4a,0x4252,0x96d0,0xa231,0x8252,0x955e,0xba4a,
  303. 0x43d2,0xf442,0x8231,0x825c,0x945e,0x7c00,0x0000,0x0000};
  304.  
  305. void notice(int x, int y){
  306. int a,b,c; int t1=0;
  307.  
  308. for (t1=0;t1<=6;t1++)
  309.     {
  310.     a=copyright[t1][0];b=copyright[t1][1];
  311.     c=copyright[t1][2];
  312.     setlinestyle(USERBIT_LINE,a,NORM_WIDTH);
  313.     moveto(x,y);lineto(x+15,y);
  314.     setlinestyle(USERBIT_LINE,b,NORM_WIDTH);
  315.     moveto(x+16,y);lineto(x+31,y);
  316.     setlinestyle(USERBIT_LINE,c,NORM_WIDTH);
  317.     moveto(x+32,y);lineto(x+47,y);y++;
  318.     };
  319. setlinestyle(USERBIT_LINE,0xFFFF,NORM_WIDTH);
  320. return;}
  321.  
  322.