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

  1. // chap2_4.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.  
  26. // For an unknown reason an error occurs if there isn't a math function in this function
  27. void bug()
  28. {
  29.      float o;
  30.      o=sqrt(1);
  31. };
  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()
  60. {
  61.      for (c=0; c<18; c++)
  62.      {
  63.      vertex[c].zs=(vertex[c].zw);
  64.      vertex[c].xs=((vertex[c].xw*(persp/float(vertex[c].zs))*xscale)+xoffset);
  65.      vertex[c].ys=((vertex[c].yw*(persp/float(vertex[c].zs))*yscale)+yoffset);
  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 4 of the Virtual Reality Homebrewer\'s Handbook and\n");
  74.      printf("shows how to draw a virtual shape in wireframe with perspective.\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. }