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

  1. /* tc-06 */
  2. /* draws cubic parametric curves in 3d.
  3.     Supports MCGA,CGA,EGA and VGA monitors */
  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        j01=0.0,j2=0.0,j3=0.0,j4=0.0;
  17. float        t=0.0,t2=0.0,t3=0.0;
  18. float        x=0.0,y=0.0,z=0.0;
  19. float        sx=0.0,sy=0.0;
  20. float        xa=0.0,ya=0.0,za=0.0;
  21. float        d=1200.0;                                                        /* angular perspective factor */
  22. double    r1=5.88319;                                                        /* 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=0.0,my=0.0,mz=-150.0;                                    /* viewpoint position */
  28. int        maxx=639,minx=0,maxy=199,miny=0;
  29. float        screen_x=639,screen_y=199;
  30. float        rx=0.0,ry=0.0;
  31. int        C0=0,C1=1,C2=2,C3=3,C4=4,C5=5,C6=6,C7=7,C8=8,C9=9,
  32.             C10=10,C11=11,C12=12,C13=13,C14=14,C15=15,
  33.             mode_flag=0;
  34. int        edge_clr=7;                                                        /* used to draw edges of models */
  35. int        t1=0;
  36. int        h=0;                                                                /* loop counter and pointer into array */
  37.  
  38. float        B11[21][2];                                                        /* 21 sets of sx,sy coordinates for near curve */
  39. float        B12[21][2];                                                        /* 21 sets of sx,sy coordinates for far curve */
  40. float        x1=-30,y01=0.0,x4=30.0,y4=0.0,                            /* curve endpoints */
  41.             x2=-10.0,y2=15.0,x3=10.0,y3=-35.0;                        /* curve control points */
  42. float        sx1=0.0,sy1=0.0,sx2=0.0,sy2=0.0;                            /* line endpoints */
  43.  
  44. /* global subroutines */
  45. void keyboard(void);void quit_pgm(void);void calc_3d(void);
  46. void rotation(void);void window(void);void graphics_setup(void);
  47. void notice(int x,int y); void freeform(void);
  48.  
  49. /* ----------------------------------------------------------------------- */
  50. /* MAIN ROUTINE */
  51.  
  52. main(){
  53. graphics_setup();
  54. setviewport(minx,miny,maxx,maxy,1);
  55. edge_clr=C7;
  56. setcolor(edge_clr);                                                         /* active drawing color */
  57. rotation();
  58.  
  59. /* draw near edge of mesh and store vertices in array */
  60. t=0.0;t2=t*t;t3=t*t*t;
  61. freeform();z=30.0;calc_3d();window();
  62. moveto(sx,sy);
  63. putpixel (sx,sy,edge_clr);
  64. h=0;
  65. for (t=0.0;t<=1.01;t+=.05){
  66.     t2=t*t;t3=t*t*t;freeform();z=30.0;calc_3d();window();
  67.     lineto(sx,sy);B11[h][0]=sx;B11[h][1]=sy;h=h+1;}
  68.  
  69. /* draw far edge of mesh and store vertices in array */
  70. t=0.0;t2=t*t;t3=t*t*t;
  71. freeform();z=-30.0;calc_3d();window();
  72. moveto(sx,sy);
  73. putpixel (sx,sy,edge_clr);
  74. h=0;                                                                            /* establish start point */
  75. for (t=0.0;t<=1.01;t+=.05){
  76.     t2=t*t;t3=t*t*t;freeform();z=-30.0;calc_3d();window();
  77.     lineto(sx,sy);B12[h][0]=sx;B12[h][1]=sy;h=h+1;}
  78.  
  79. for (h=-20;h<=20;h+=10){
  80.     t=0.0;t2=t*t;t3=t*t*t;
  81.     freeform();z=h;calc_3d();window();moveto(sx,sy);
  82.     putpixel(sx,sy,edge_clr);
  83.     for (t=0.0;t<=1.01;t+=.05){
  84.         t2=t*t;t3=t*t*t;freeform();z=h;calc_3d();window();
  85.         lineto(sx,sy);
  86.         }                                                                        /* logical end of t loop */
  87.  
  88. }                                                                                /* logical end of h loop */
  89.  
  90. /* connect previosly stored vertices */
  91. for (h=0;h<=20;h+=2){
  92.     sx1=B11[h][0];sy1=B11[h][1];
  93.     sx2=B12[h][0];sy2=B12[h][1];
  94.     moveto(sx1,sy1);lineto(sx2,sy2);}
  95.  
  96. setcolor(C7);notice(0,0);
  97. for (t1=1;t1!=2;) keyboard();
  98. quit_pgm();}
  99.  
  100. /* SUBROUTINE: CALCULATE POINT ON FREE-FORM CURVE */
  101. void freeform(void){
  102. j01=x1*(-t3+3*t2-3*t+1);j2=x2*(3*t3-6*t2+3*t);j3=x3*(-3*t3+3*t2);
  103. j4=x4*t3;x=j01+j2+j3+j4;
  104. j01=y01*(-t3+3*t2-3*t+1);j2=y2*(3*t3-6*t2+3*t);j3=y3*(-3*t3+3*t2);
  105. j4=y4*t3;y=j01+j2+j3+j4;
  106. return;}
  107.  
  108. /* ----------------------------------------------------------------------- */
  109. /* SUBROUTINE: CALCULATE SIN,COS FACTORS */
  110.  
  111. void rotation(void){
  112. sr1=sin(r1);sr2=sin(r2);sr3=sin(r3);cr1=cos(r1);cr2=cos(r2);
  113. cr3=cos(r3);return;}
  114.  
  115. /* ----------------------------------------------------------------------- */
  116. /* SUBROUTINE: STANDARD 3D FORMULAS */
  117. /* Pass: x,y,z cartesian world coordinates.
  118.    Returns: sx,sy cartesian display coordinates.
  119.           x,y,z catesian view coordinates */
  120.  
  121. void calc_3d(void){
  122. x=(-1)*x;xa=cr1*x-sr1*z;za=sr1*x+cr1*z;x=cr2*xa+sr2*y;
  123. ya=cr2*y-sr2*xa;z=cr3*za-sr3*ya;y=sr3*za+cr3*ya;x=x+mx;y=y+my;
  124. z=z+mz;sx=d*x/z;sy=d*y/z;return;}
  125.  
  126. /* ----------------------------------------------------------------------- */
  127. /* SUBROUTINE: MAP CARTESIAN COORDS TO PHYSICAL SCREEN COORDS */
  128.  
  129. void window(void){
  130. sx=sx+399;sy=sy+299;rx=screen_x/799;ry=screen_y/599;sx=sx*rx;
  131. sy=sy*ry;return;}
  132.  
  133. /* ----------------------------------------------------------------------- */
  134. /* SUBROUTINE: CHACK THE KEYBOARD BUFFER */
  135. void keyboard(void){
  136. if (bioskey(1)==0) return; else quit_pgm();}
  137.  
  138. /* ----------------------------------------------------------------------- */
  139. /* SUBROUTINE: GRACEFUL EXIT FROM PROGRAM */
  140.  
  141. void quit_pgm(void){
  142. cleardevice();restorecrtmode();exit(0);}
  143.  
  144. /* ----------------------------------------------------------------------- */
  145. /* SUBROUTINE: VGA/EGA/MCGA/CGA COMPATIBILITY MODULE */
  146.  
  147. void graphics_setup(void){
  148. int graphics_adapter,graphics_mode;
  149. detectgraph(&graphics_adapter,&graphics_mode);
  150. if (graphics_adapter==VGA) goto VGA_mode;
  151. if (graphics_mode==EGAHI) goto EGA_ECD_mode;
  152. if (graphics_mode==EGALO) goto EGA_SCD_mode;
  153. if (graphics_adapter==CGA) goto CGA_mode;
  154. if (graphics_adapter==MCGA) goto CGA_mode;
  155. goto abort_message;
  156.  
  157. VGA_mode:
  158. graphics_adapter=VGA;graphics_mode=VGAHI;
  159. initgraph(&graphics_adapter,&graphics_mode,"");
  160.     maxx=639;minx=0;maxy=479;miny=0;screen_x=639;screen_y=479;
  161.     setcolor(7);moveto(0,472);
  162.     outtext("Revisions by A. Helder");
  163.     moveto(472,472);
  164.     outtext("Press any key to quit");
  165.     moveto(160,0);
  166.     outtext("USING C TO GENERATE 3D CURVES");
  167.     return;
  168.  
  169. EGA_ECD_mode:
  170. graphics_adapter=EGA;graphics_mode=EGAHI;
  171. initgraph(&graphics_adapter,&graphics_mode,"");
  172.     maxx=639;minx=0;maxy=349;miny=0;screen_x=639;screen_y=349;
  173.     setcolor(7);moveto(0,342);
  174.     outtext("Revisions by A. Helder");
  175.     moveto(472,342);
  176.     outtext ("Press any key to quit");
  177.     moveto(160,0);
  178.     outtext("USING C TO GENERATE 3D CURVES");
  179.     return;
  180.  
  181. EGA_SCD_mode:
  182. graphics_adapter=EGA;graphics_mode=EGALO;
  183. initgraph(&graphics_adapter,&graphics_mode,"");
  184.     maxx=639;minx=0;maxy=199;miny=0;screen_x=639;screen_y=199;
  185.     setcolor(7);moveto(0,192);
  186.     outtext("Revisions by A. Helder");
  187.     moveto(472,192);
  188.     outtext("PRESS ANY KEY TO QUIT");
  189.     moveto(160,0);
  190.     outtext("USING C TO GENERATE 3D CURVES");
  191.     return;
  192.  
  193. CGA_mode:
  194. graphics_adapter=CGA;graphics_mode=CGAC3;
  195. initgraph(&graphics_adapter,&graphics_mode,"");
  196. C7=3;
  197.     maxx=319;minx=0;maxy=199;miny=0;screen_x=319;screen_y=199;
  198.     setcolor(3);moveto(48,192);
  199.     outtext("Revisions by A. Helder");
  200.     moveto(88,0);
  201.     outtext ("3D WIRE FRAME CUBE");
  202.     return;
  203.  
  204. abort_message:
  205. printf("\n\nUnable to proceed - Requires VGA,EGA,CGA or MCGA adapter");
  206. printf("\nWith appropriate monitor");
  207. exit(0);
  208. }
  209.  
  210. /* ----------------------------------------------------------------------- */
  211. /* SUBROUTINE: COPYRIGHT NOTICE */
  212.  
  213. int copyright[][3]={0x7c00,0x0000,0x0000,0x8231,
  214. 0x819c,0x645e,0xba4a,0x4252,0x96d0,0xa231,0x8252,0x955e,0xba4a,
  215. 0x43d2,0xf442,0x8231,0x825c,0x945e,0x7c00,0x0000,0x0000};
  216.  
  217. void notice(int x, int y){
  218. int a,b,c; int t1=0;
  219.  
  220. for (t1=0;t1<=6;t1++)
  221.     {
  222.     a=copyright[t1][0];b=copyright[t1][1];
  223.     c=copyright[t1][2];
  224.     setlinestyle(USERBIT_LINE,a,NORM_WIDTH);
  225.     moveto(x,y);lineto(x+15,y);
  226.     setlinestyle(USERBIT_LINE,b,NORM_WIDTH);
  227.     moveto(x+16,y);lineto(x+31,y);
  228.     setlinestyle(USERBIT_LINE,c,NORM_WIDTH);
  229.     moveto(x+32,y);lineto(x+47,y);y++;
  230.     };
  231. setlinestyle(USERBIT_LINE,0xFFFF,NORM_WIDTH);
  232. return;}
  233.  
  234.  
  235. /* END OF SOURCE CODE */
  236.