home *** CD-ROM | disk | FTP | other *** search
- // chap2_5.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;
- float xe, ye, ze;
-
- // 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(float x, float y, float z)
- {
- int c;
- for (c=0; c<18; c++)
- {
- vertex[c].zs=(vertex[c].zw-z);
- vertex[c].xs=(((vertex[c].xw-x)*(persp/float(vertex[c].zs))*xscale)+xoffset);
- vertex[c].ys=(((vertex[c].yw-y)*(persp/float(vertex[c].zs))*yscale)+yoffset);
- };
- };
-
- void main()
- {
- clrscr();
- printf(" \n"); printf(" \n"); printf(" \n"); printf(" \n"); printf(" \n"); printf(" \n");
- printf("This is from Chapter 2 Step 5 of the Virtual Reality Homebrewer\'s Handbook and\n");
- printf("shows how to simply move (no turning) around a virtual shape drawn in wireframe.\n");
- printf(" \n");
- printf(" \n");
- printf("Controls: 8/2 - move forwards/back\n");
- printf(" 4/6 - move left/back\n");
- printf(" 7/1 - move up/down\n");
- printf(" \n");
- printf(" q - quit\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();
-
- xe=ye=ze=0;
- transform(xe,ye,ze);
- draw_picture();
-
- do
- {
- ch=getch();
- switch (ch)
- {
- case '8': {
- ze=ze+10;
- break;
- };
- case '2': {
- ze=ze-10;
- break;
- };
- case '4': {
- xe=xe-10;
- break;
- };
- case '6': {
- xe=xe+10;
- break;
- };
- case '1': {
- ye=ye-10;
- break;
- };
- case '7': {
- ye=ye+10;
- break;
- };
- }
- transform(xe,ye,ze);
- draw_picture();
- }
- while (ch!='q');
- closegraph();
- }