home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / TC_3D.ZIP / TC-05SOL.C < prev    next >
Text File  |  1991-02-13  |  10KB  |  311 lines

  1. /* tc-05 */
  2.  
  3. /* Draw a solid 3d cylinder using the plane equation method
  4.     of hidden surface removal */
  5.  
  6. /* ----------------------------------------------------------------------- */
  7. /* INCLUDE FILES */
  8. #include <process.h>
  9. #include <bios.h>
  10. #include <stdio.h>
  11. #include <graphics.h>
  12. #include <math.h>
  13.  
  14. /* ----------------------------------------------------------------------- */
  15. /* DECLARATIONS */
  16.  
  17. float        x=0.0,y=0.0,z=0.0;
  18. float        x1=0.0,x2=0.0,x3=0.0;
  19. float        y01=0.0,y2=0.0,y3=0.0;
  20. float        z1=0.0,z2=0.0,z3=0.0;
  21. float        sx1=0.0,sx2=0.0,sx3=0.0,sx4=0.0,sx5=0.0;
  22. float        sy1=0.0,sy2=0.0,sy3=0.0,sy4=0.0,sy5=0.0;
  23. float        sx=0.0,sy=0.0;
  24. float        xa=0.0,ya=0.0,za=0.0;
  25. float        d=1200.0;
  26. double    r1=5.89778;                                                        /* yaw angle in radians */
  27. double    r2=6.28319;                                                        /* roll angle in radians */
  28. double    r3=.58539;                                                        /* pitch angle in radians */
  29. double    sr1=0.0,sr2=0.0,sr3=0.0;
  30. double    cr1=0.0,cr2=0.0,cr3=0.0;
  31. float        mx=0.0,my=0.0,mz=-400.0;                                    /* viewpoint position */
  32. int        maxx=639,minx=0,maxy=199,miny=0;
  33. float        screen_x=639,screen_y=199;
  34. float        rx=0.0,ry=0.0;
  35. int        t=0,t1=0,t2=0;
  36. int        p1=0;
  37. int        C0=0,C1=1,C2=2,C3=3,C4=4,C5=5,C6=6,C7=7,C8=8,C9=9,
  38.             C10=10,C11=11,C12=12,C13=13,C14=14,C15=15,
  39.             mode_flag=0;
  40. int        key_matte_clr=6;
  41. int        edge_clr=7;
  42. int        solid_clr=0;
  43. float        x_res,y_res;
  44. float        sp1=0.0,sp2=0.0,sp3=0.0;
  45. float        sp=0.0;
  46. int        q=0,q1=0,q2=0;
  47. double    r4=6.28319,r5=6.28319;                                        /* spherical coordinate values */
  48. int        polary[4][2];  /* used in my 3d solid polygon draw routine */
  49. float        B11[36][3];                                                        /* 30 sets of xyz coordinates, near end */
  50. float        B12[36][3];                                                        /* 30 sets of xyz coordinates, far end */
  51. float        B21[36][2];                                                        /* 36 sets of sx,sy display coordinates, near end */
  52. float        B22[36][2];                                                        /* 36 sets of sx,sy display coordinates, far end */
  53. double    sr4=0.0,cr4=0.0,sr5=0.0,cr5=0.0;
  54.  
  55. /* declare global subroutines */
  56. void keyboard(void);void quit_pgm(void);void calc_3d(void);
  57. void rotation(void);void window(void);void graphics_setup(void);
  58. void coords(void);void draw_poly(void);void notice (int x,int y);
  59. void visibility_test(void);
  60. void cyl_coords(void);void draw_surface(void);
  61.  
  62. /* ----------------------------------------------------------------------- */
  63. /* MAIN ROUTINE */
  64.  
  65. main(){
  66. graphics_setup();
  67. setviewport(0,0,maxx,maxy,1);
  68. key_matte_clr=C6;
  69. edge_clr=C7;solid_clr=C0;
  70. rotation();
  71.  
  72. /* calculate coordinates for near end of cylinder */
  73. r4=0;r5=0;
  74. for (t=0;t<=35;t++){
  75.     x=30;cyl_coords();z=z+60;calc_3d();window();
  76.     B11[t][0]=x;B11[t][1]=y;B11[t][2]=z;
  77.     B21[t][0]=sx;B21[t][1]=sy;
  78.     keyboard();
  79.     r5=r5+.17453;}
  80.  
  81. /* calculate coordinates for far end of cylinder */
  82. r4=0;r5=0;
  83. for (t=0;t<=35;t++){
  84.     x=30;cyl_coords();z=z-60;calc_3d();window();
  85.     B12[t][0]=x;B12[t][1]=y;B12[t][2]=z;
  86.     B22[t][0]=sx;B22[t][1]=sy;
  87.     keyboard();
  88.     r5=r5+.17453;}
  89.  
  90. /* draw surfaces of 3d cylinder */
  91. for (q1=0;q1<=35;q1++){
  92.     q2=q1+1;if (q2>35) q2=0;
  93.     draw_surface();keyboard();}
  94.  
  95. /* draw near end of 3d cylinder */
  96. x1=B11[0][0];y01=B11[0][1];z1=B11[0][2];
  97. x2=B11[25][0];y2=B11[25][1];z2=B11[25][2];
  98. x3=B11[11][0];y3=B11[11][1];z3=B11[11][2];
  99. visibility_test();if (sp>0) goto all_done;
  100. setcolor (key_matte_clr);setfillstyle(SOLID_FILL,key_matte_clr);
  101. setlinestyle(USERBIT_LINE,0xffff,NORM_WIDTH);
  102. for (q1=0;q1<=35;q1++){
  103.     q2=q1+1;if (q2>35) q2=0;
  104.     sx1=B21[q1][0];sy1=B21[q1][1];
  105.     sx2=B21[q2][0];sy2=B21[q2][1];
  106.     moveto(sx1,sy1);lineto(sx2,sy2);}
  107. x=0.0;y=0.0;z=60.0;calc_3d();window();/*floodfill(sx,sy,key_matte_clr);*/
  108. keyboard();
  109. setcolor(edge_clr);
  110.  
  111. for (q1=0;q1<=35;q1++){
  112.     q2=q1+1;if (q2>35) q2=0;
  113.     sx1=B21[q1][0];sy1=B21[q1][1];
  114.     sx2=B21[q2][0];sy2=B21[q2][1];
  115.     moveto(sx1,sy1);lineto(sx2,sy2);}
  116. x=0.0;y=0.0;z=60.0;calc_3d();window();
  117. setfillstyle(SOLID_FILL,solid_clr);floodfill(sx,sy,edge_clr);
  118.  
  119. all_done:
  120. setcolor(C7);notice(0,0);
  121. for (t1=1;t1!=2;) keyboard();
  122. quit_pgm();}
  123.  
  124. /* ----------------------------------------------------------------------- */
  125. /* SUBROUTINE: CALCULATE WORLD COORDINATES FOR cylinder */
  126.  
  127. void cyl_coords(void){
  128. sr4=sin(r4);cr4=cos(r4);sr5=sin(r5);
  129. cr5=cos(r5);x1=sr5*x;y=cr5*x;x=cr4*x1;z=sr4*x1;return;}
  130.  
  131. /* ----------------------------------------------------------------------- */
  132. /* SUBROUTINE: DRAW 4 SIDED POLYGON SURFACE ON SPHERE */
  133.  
  134. void draw_surface(void){
  135. x1=B11[q1][0];y01=B11[q1][1];z1=B11[q1][2];
  136. x2=B11[q2][0];y2=B11[q2][1];z2=B11[q2][2];
  137. x3=B12[q2][0];y3=B12[q2][1];z3=B12[q2][2];
  138. visibility_test();if (sp>0) return;
  139. sx1=B21[q1][0];sy1=B21[q1][1];
  140. sx2=B21[q2][0];sy2=B21[q2][1];
  141. sx3=B22[q2][0];sy3=B22[q2][1];
  142. sx4=B22[q1][0];sy4=B22[q1][1];
  143. x=x1+.5*(x3-x1);y=y01+.5*(y3-y01);z=z1+.5*(z3-z1);
  144. sx=d*x/z;sy=d*y/z;window();sx5=sx=sy5=sy;
  145. draw_poly();
  146. return;}
  147.  
  148. /* ----------------------------------------------------------------------- */
  149. /* SUBROUTINE: CALCULATE SIN,COS FACTORS */
  150.  
  151. void rotation(void){
  152. sr1=sin(r1);sr2=sin(r2);sr3=sin(r3);cr1=cos(r1);cr2=cos(r2);
  153. cr3=cos(r3);return;}
  154.  
  155. /* ----------------------------------------------------------------------- */
  156. /* SUBROUTINE: STANDARD 3D FORMULAS */
  157. /* Pass: x,y,z cartesian world coordinates.
  158.    Returns: sx,sy cartesian display coordinates.
  159.           x,y,z catesian view coordinates */
  160.  
  161. void calc_3d(void){
  162. x=(-1)*x;xa=cr1*x-sr1*z;za=sr1*x+cr1*z;x=cr2*xa+sr2*y;
  163. ya=cr2*y-sr2*xa;z=cr3*za-sr3*ya;y=sr3*za+cr3*ya;x=x+mx;y=y+my;
  164. z=z+mz;sx=d*x/z;sy=d*y/z;return;}
  165.  
  166. /* ----------------------------------------------------------------------- */
  167. /* HIDDEN SURFACE VISIBILTY TEST */
  168. void visibility_test(void){
  169. sp1=x1*(y2*z3-y3*z2);sp1=(-1)*sp1;sp2=x2*(y3*z1-y01*z3);
  170. sp3=x3*(y01*z2-y2*z1);sp=sp1-sp2-sp3;return;}
  171.  
  172. /* ----------------------------------------------------------------------- */
  173. /* SUBROUTINE: DRAW 4-SIDED SOLID POLYGON IN 3D SPACE */
  174.  
  175. void draw_poly(void){
  176. setlinestyle(USERBIT_LINE,0xffff,NORM_WIDTH);
  177. setfillstyle(SOLID_FILL,key_matte_clr);
  178. setcolor(edge_clr);
  179.  
  180. polary[0][0]=sx1;polary[0][1]=sy1;polary[1][0]=sx2;polary[1][1]=sy2;
  181. polary[2][0]=sx3;polary[2][1]=sy3;polary[3][0]=sx4;polary[3][1]=sy4;
  182.  
  183. fillpoly (4,(int far*) polary);
  184. /* else
  185.     {
  186.     moveto(sx1,sy1);lineto(sx2,sy2);lineto(sx3,sy3);
  187.     lineto(sx4,sy4);lineto(sx1,sy1);
  188.     } */
  189. return;
  190. }
  191.  
  192. /* ----------------------------------------------------------------------- */
  193. /* SUBROUTINE: MAP CARTESIAN COORDS TO PHYSICAL SCREEN COORDS */
  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: CHACK THE KEYBOARD BUFFER */
  201. void keyboard(void){
  202. if (bioskey(1)==0) return; else quit_pgm();}
  203.  
  204. /* ----------------------------------------------------------------------- */
  205. /* SUBROUTINE: GRACEFUL EXIT FROM PROGRAM */
  206.  
  207. void quit_pgm(void){
  208. cleardevice();restorecrtmode();exit(0);}
  209.  
  210. /* ----------------------------------------------------------------------- */
  211. /* SUBROUTINE: VGA/EGA/MCGA/CGA COMPATIBILITY MODULE */
  212.  
  213. void graphics_setup(void){
  214. int graphics_adapter,graphics_mode;
  215. detectgraph(&graphics_adapter,&graphics_mode);
  216. if (graphics_adapter==VGA) goto VGA_mode;
  217. if (graphics_mode==EGAHI) goto EGA_ECD_mode;
  218. if (graphics_mode==EGALO) goto EGA_SCD_mode;
  219. if (graphics_adapter==CGA) goto CGA_mode;
  220. if (graphics_adapter==MCGA) goto CGA_mode;
  221. goto abort_message;
  222.  
  223. VGA_mode:
  224. graphics_adapter=VGA;graphics_mode=VGAHI;
  225. initgraph(&graphics_adapter,&graphics_mode,"");
  226. x_res=640;y_res=480;mode_flag=1;
  227.     maxx=639;minx=0;maxy=479;miny=0;screen_x=639;screen_y=479;
  228.     setcolor(7);moveto(0,472);
  229.     outtext("Revisions by A. Helder");
  230.     moveto(472,472);
  231.     outtext("Press any key to quit");
  232.     moveto(160,0);
  233.     outtext("USING C TO GENERATE A 3D SYLINDER");
  234.     return;
  235.  
  236. EGA_ECD_mode:
  237. graphics_adapter=EGA;graphics_mode=EGAHI;
  238. initgraph(&graphics_adapter,&graphics_mode,"");
  239. x_res=640;y_res=350;mode_flag=2;
  240.     maxx=639;minx=0;maxy=349;miny=0;screen_x=639;screen_y=349;
  241.     setcolor(7);moveto(0,342);
  242.     outtext("Revisions by A. Helder");
  243.     moveto(472,342);
  244.     outtext ("Press any key to quit");
  245.     moveto(160,0);
  246.     outtext("USING C TO GENERATE A 3D CYLINDER");
  247.     return;
  248.  
  249. EGA_SCD_mode:
  250. graphics_adapter=EGA;graphics_mode=EGALO;
  251. initgraph(&graphics_adapter,&graphics_mode,"");
  252. x_res=640;y_res=200;mode_flag=3;
  253.     maxx=639;minx=0;maxy=199;miny=0;screen_x=639;screen_y=199;
  254.     setcolor(7);moveto(0,192);
  255.     outtext("Revisions by A. Helder");
  256.     moveto(472,192);
  257.     outtext("PRESS ANY KEY TO QUIT");
  258.     moveto(160,0);
  259.     outtext("USING C TO GENERATE A 3D CYLINDER");
  260.     return;
  261.  
  262. CGA_mode:
  263. graphics_adapter=CGA;graphics_mode=CGAC3;
  264. initgraph(&graphics_adapter,&graphics_mode,"");
  265. x_res=320;y_res=200;mode_flag=1;C7=3;
  266.     maxx=319;minx=0;maxy=199;miny=0;screen_x=319;screen_y=199;
  267.     setcolor(3);moveto(48,192);
  268.     outtext("Revisions by A. Helder");
  269.     moveto(88,0);
  270.     outtext ("3D CYLINDER");
  271.     return;
  272.  
  273. abort_message:
  274. printf("\n\nUnable to proceed - Requires VGA,EGA,CGA or MCGA adapter");
  275. printf("\nWith appropriate monitor");
  276. exit(0);
  277. }
  278.  
  279.  
  280. /* ----------------------------------------------------------------------- */
  281. /* SUBROUTINE: MAP 640*480 TEMPLATE TO 2D SCREEN */
  282. void coords(void)
  283. {
  284. sx=sx*(x_res/640);sy=sy*(y_res/480);return;
  285. }
  286.  
  287. /* ----------------------------------------------------------------------- */
  288. /* SUBROUTINE: COPYRIGHT NOTICE */
  289.  
  290. int copyright[][3]={0x7c00,0x0000,0x0000,0x8231,
  291. 0x819c,0x645e,0xba4a,0x4252,0x96d0,0xa231,0x8252,0x955e,0xba4a,
  292. 0x43d2,0xf442,0x8231,0x825c,0x945e,0x7c00,0x0000,0x0000};
  293.  
  294. void notice(int x, int y){
  295. int a,b,c; int t1=0;
  296.  
  297. for (t1=0;t1<=6;t1++)
  298.     {
  299.     a=copyright[t1][0];b=copyright[t1][1];
  300.     c=copyright[t1][2];
  301.     setlinestyle(USERBIT_LINE,a,NORM_WIDTH);
  302.     moveto(x,y);lineto(x+15,y);
  303.     setlinestyle(USERBIT_LINE,b,NORM_WIDTH);
  304.     moveto(x+16,y);lineto(x+31,y);
  305.     setlinestyle(USERBIT_LINE,c,NORM_WIDTH);
  306.     moveto(x+32,y);lineto(x+47,y);y++;
  307.     };
  308. setlinestyle(USERBIT_LINE,0xFFFF,NORM_WIDTH);
  309. return;}
  310.  
  311. /* END OF SOURCE CODE */