home *** CD-ROM | disk | FTP | other *** search
- // chap2_4.cpp;
-
- #include <graphics.h>
- #include <math.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <conio.h>
-
- const xoffset=640/2;
- const yoffset=480/2;
- const xscale=1;
- const yscale=-1;
- const persp=500;
-
- struct point
- {
- float xw, yw, zw;
- int xs, ys, zs;
- } vertex[18];
-
- int edges[27][27];
- FILE *fp;
- int c;
- char ch;
-
- // For an unknown reason an error occurs if there isn't a math function in this function
- void bug()
- {
- float o;
- o=sqrt(1);
- };
-
-
- void initialise_graphics()
- {
- int gdriver=DETECT, gmode, errorcode;
-
- initgraph(&gdriver, &gmode,"");
- errorcode=graphresult();
- if (errorcode!=grOk)
- {
- printf("Graphics Error: %s\n",grapherrormsg(errorcode));
- printf("Program Aborted \n");
- exit(1);
- }
- setlinestyle(0,0,3);
- setcolor(WHITE);
-
- }
-
- void draw_picture()
- {
- int c;
- cleardevice();
- for (c=0; c<27; c++)
- line(vertex[edges[c][0]].xs,vertex[edges[c][0]].ys,vertex[edges[c][1]].xs,vertex[edges[c][1]].ys);
- };
-
- void transform()
- {
- for (c=0; c<18; c++)
- {
- vertex[c].zs=(vertex[c].zw);
- vertex[c].xs=((vertex[c].xw*(persp/float(vertex[c].zs))*xscale)+xoffset);
- vertex[c].ys=((vertex[c].yw*(persp/float(vertex[c].zs))*yscale)+yoffset);
- };
- };
-
- void main()
- {
- clrscr();
- printf("\n"); printf("\n"); printf("\n"); printf("\n"); printf("\n"); printf("\n"); printf("\n");
- printf("This is from Chapter 2 Step 4 of the Virtual Reality Homebrewer\'s Handbook and\n");
- printf("shows how to draw a virtual shape in wireframe with perspective.\n");
- printf("\n");
- printf("\n");
- printf("\n");
- printf(" Press any key to continue\n");
- ch=getch();
-
- // Read in vertex data
- if ((fp=fopen("points.dat","r"))==NULL)
- {
- puts("Can't open points.dat");
- exit(0);
- }
- for (c=0; c<18; c++)
- {
- fscanf(fp,"%f %f %f \n",&vertex[c].xw,&vertex[c].yw,&vertex[c].zw);
- }
- fclose(fp);
-
- // Move object to z=500
- for (c=0; c<18; c++)
- vertex[c].zw=vertex[c].zw+500;
-
-
- // Read in edge data
- if ((fp=fopen("edges.dat","r"))==NULL)
- {
- puts("Can't open edges.dat");
- exit(0);
- }
- printf("Opening edges.dat \n");
- for (c=0; c<27; c++)
- fscanf(fp,"%d %d \n",&edges[c][0],&edges[c][1]);
- fclose(fp);
- for (c=0; c<27; c++)
- {
- edges[c][0]--; edges[c][1]--;
- }
-
- initialise_graphics();
-
- transform();
- draw_picture();
- ch=getch();
- closegraph();
- }