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

  1. // chap2_5.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. const persp=500;
  14.  
  15. struct point
  16. {
  17.     float xw, yw, zw;
  18.     int   xs, ys, zs;
  19. }   vertex[18];
  20.  
  21. int    edges[27][27];
  22. FILE   *fp;
  23. int    c;
  24. char   ch;
  25. float  xe, ye, ze;
  26.  
  27. // For an unknown reason an error occurs if there isn't a math function in this function
  28. void bug()
  29. {
  30.      float o;
  31.      o=sqrt(1);
  32. };
  33.  
  34. void initialise_graphics()
  35. {
  36.      int gdriver=DETECT, gmode, errorcode;
  37.  
  38.      initgraph(&gdriver, &gmode,"");
  39.      errorcode=graphresult();
  40.      if (errorcode!=grOk)
  41.      {
  42.       printf("Graphics Error: %s\n",grapherrormsg(errorcode));
  43.       printf("Program Aborted \n");
  44.       exit(1);
  45.      }
  46.      setlinestyle(0,0,3);
  47.      setcolor(WHITE);
  48.  
  49. }
  50.  
  51. void draw_picture()
  52. {
  53.      int c;
  54.      cleardevice();
  55.      for (c=0; c<27; c++)
  56.       line(vertex[edges[c][0]].xs,vertex[edges[c][0]].ys,vertex[edges[c][1]].xs,vertex[edges[c][1]].ys);
  57. };
  58.  
  59. void transform(float x, float y, float z)
  60. {
  61.      int c;
  62.      for (c=0; c<18; c++)
  63.      {
  64.      vertex[c].zs=(vertex[c].zw-z);
  65.      vertex[c].xs=(((vertex[c].xw-x)*(persp/float(vertex[c].zs))*xscale)+xoffset);
  66.      vertex[c].ys=(((vertex[c].yw-y)*(persp/float(vertex[c].zs))*yscale)+yoffset);
  67.      };
  68. };
  69.  
  70. void main()
  71. {
  72.      clrscr();
  73.      printf(" \n"); printf(" \n"); printf(" \n"); printf(" \n"); printf(" \n"); printf(" \n");
  74.      printf("This is from Chapter 2 Step 5 of the Virtual Reality Homebrewer\'s Handbook and\n");
  75.      printf("shows how to simply move (no turning) around a virtual shape drawn in wireframe.\n");
  76.      printf(" \n");
  77.      printf(" \n");
  78.      printf("Controls:       8/2  - move forwards/back\n");
  79.      printf("                4/6  - move left/back\n");
  80.      printf("                7/1  - move up/down\n");
  81.      printf(" \n");
  82.      printf("                q    - quit\n");
  83.      printf(" \n");
  84.      printf("                Press any key to continue\n");
  85.      ch=getch();
  86.  
  87. //   Read in vertex data
  88.      if ((fp=fopen("points.dat","r"))==NULL)
  89.      {
  90.       puts("Can't open points.dat");
  91.       exit(0);
  92.      }
  93.      for (c=0; c<18; c++)
  94.      {
  95.       fscanf(fp,"%f %f %f \n",&vertex[c].xw,&vertex[c].yw,&vertex[c].zw);
  96.      }
  97.      fclose(fp);
  98.  
  99. //   Move object to z=500
  100.      for (c=0; c<18; c++)
  101.       vertex[c].zw=vertex[c].zw+500;
  102.  
  103.  
  104. //   Read in edge data
  105.      if ((fp=fopen("edges.dat","r"))==NULL)
  106.      {
  107.       puts("Can't open edges.dat");
  108.       exit(0);
  109.      }
  110.      printf("Opening edges.dat \n");
  111.      for (c=0; c<27; c++)
  112.       fscanf(fp,"%d %d \n",&edges[c][0],&edges[c][1]);
  113.      fclose(fp);
  114.      for (c=0; c<27; c++)
  115.      {
  116.       edges[c][0]--; edges[c][1]--;
  117.      }
  118.  
  119.      initialise_graphics();
  120.  
  121.      xe=ye=ze=0;
  122.      transform(xe,ye,ze);
  123.      draw_picture();
  124.  
  125.      do
  126.      {
  127.        ch=getch();
  128.        switch (ch)
  129.        {
  130.        case '8': {
  131.                ze=ze+10;
  132.                break;
  133.              };
  134.        case    '2': {
  135.                ze=ze-10;
  136.                break;
  137.              };
  138.        case    '4': {
  139.                xe=xe-10;
  140.                break;
  141.              };
  142.        case    '6': {
  143.                xe=xe+10;
  144.                break;
  145.              };
  146.        case    '1': {
  147.                ye=ye-10;
  148.                break;
  149.              };
  150.        case    '7': {
  151.                ye=ye+10;
  152.                break;
  153.              };
  154.        }
  155.        transform(xe,ye,ze);
  156.        draw_picture();
  157.      }
  158.      while (ch!='q');
  159.      closegraph();
  160. }