home *** CD-ROM | disk | FTP | other *** search
/ Virtual Reality Homebrewer's Handbook / vr.iso / 3dgraph / c / chap2_3.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-19  |  2.5 KB  |  119 lines

  1. // chap2_3.cpp;
  2.  
  3. #include <graphics.h>
  4. #include <math.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <conio.h>
  8.  
  9. const xoffset=640/2;
  10. const yoffset=480/2;
  11. const xscale=1;
  12. const yscale=-1;
  13.  
  14. struct point
  15. {
  16.     float xw, yw, zw;
  17.     int   xs, ys, zs;
  18. }   vertex[18];
  19.  
  20. int    edges[27][27];
  21. FILE   *fp;
  22. int    c;
  23. char   ch;
  24.  
  25. // For an unknown reason an error occurs if there isn't a math function in this function
  26. void bug()
  27. {
  28.      float o;
  29.      o=sqrt(1);
  30. };
  31.  
  32.  
  33. void initialise_graphics()
  34. {
  35.      int gdriver=DETECT, gmode, errorcode;
  36.  
  37.      initgraph(&gdriver, &gmode,"");
  38.      errorcode=graphresult();
  39.      if (errorcode!=grOk)
  40.      {
  41.       printf("Graphics Error: %s\n",grapherrormsg(errorcode));
  42.       printf("Program Aborted \n");
  43.       exit(1);
  44.      }
  45.      setlinestyle(0,0,3);
  46.      setcolor(WHITE);
  47.  
  48. }
  49.  
  50. void draw_picture()
  51. {
  52.      int c;
  53.      cleardevice();
  54.      for (c=0; c<27; c++)
  55.       line(vertex[edges[c][0]].xs,vertex[edges[c][0]].ys,vertex[edges[c][1]].xs,vertex[edges[c][1]].ys);
  56. };
  57.  
  58. void transform()
  59. {
  60.      integer : c;
  61.      for (c=0; c<18; c++)
  62.      {
  63.      vertex[c].xs=((vertex[c].xw*xscale)+xoffset);
  64.      vertex[c].ys=((vertex[c].yw*yscale)+yoffset);
  65.      vertex[c].zs=(vertex[c].zw);
  66.      };
  67. };
  68.  
  69. void main()
  70. {
  71.      clrscr();
  72.      printf("\n"); printf("\n"); printf("\n"); printf("\n"); printf("\n"); printf("\n"); printf("\n");
  73.      printf("This is from Chapter 2 Step 3 of the Virtual Reality Homebrewer\'s Handbook and\n");
  74.      printf("shows how to draw a virtual shape in wireframe.\n");
  75.      printf("\n");
  76.      printf("\n");
  77.      printf("\n");
  78.      printf("                Press any key to continue\n");
  79.      ch=getch();
  80.  
  81. //   Read in vertex data
  82.      if ((fp=fopen("points.dat","r"))==NULL)
  83.      {
  84.       puts("Can't open points.dat");
  85.       exit(0);
  86.      }
  87.      for (c=0; c<18; c++)
  88.      {
  89.       fscanf(fp,"%f %f %f \n",&vertex[c].xw,&vertex[c].yw,&vertex[c].zw);
  90.      }
  91.      fclose(fp);
  92.  
  93. //   Move object to z=500
  94.      for (c=0; c<18; c++)
  95.       vertex[c].zw=vertex[c].zw+500;
  96.  
  97.  
  98. //   Read in edge data
  99.      if ((fp=fopen("edges.dat","r"))==NULL)
  100.      {
  101.       puts("Can't open edges.dat");
  102.       exit(0);
  103.      }
  104.      printf("Opening edges.dat \n");
  105.      for (c=0; c<27; c++)
  106.       fscanf(fp,"%d %d \n",&edges[c][0],&edges[c][1]);
  107.      fclose(fp);
  108.      for (c=0; c<27; c++)
  109.      {
  110.       edges[c][0]--; edges[c][1]--;
  111.      }
  112.  
  113.      initialise_graphics();
  114.  
  115.      transform();
  116.      draw_picture();
  117.      ch=getch();
  118.      closegraph();
  119. }