home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 3 / RISC_DISC_3.iso / resources / etexts / gems / gemsv / ch3_5 / mainbsp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-22  |  3.3 KB  |  95 lines

  1. /* mainBsp.c: main driver of BSP application
  2.  * Copyright (c) Norman Chin 
  3.  */
  4. #include "bsp.h"
  5.  
  6. #define MAXBUFFER 80
  7. #define NOBLOCK 'n'
  8. #define COMMENT ';'
  9.  
  10. /* local functions */
  11. int main(int argc,char *argv[]);
  12. void getScene(const char *fileName,POINT *position,FACE **faceList);
  13. #define dumpPosition(p) (printf("Position: (%f,%f,%f)\n",p.xx,p.yy,p.zz))
  14.  
  15. /* Main driver */
  16. int main(int argc,char *argv[])
  17. {
  18.    FACE *faceList; POINT oldPosition;
  19.    if (argc != 2) {fprintf(stderr,"Usage: %s <datefile>\n",argv[0]);exit(1);}
  20.    
  21.    getScene(argv[1],&oldPosition,&faceList); /* get list of faces from file */
  22.    drawFaceList(stdout,faceList); /* dump faces */
  23.    dumpPosition(oldPosition);    /* dump viewer position */
  24.  
  25.    {
  26.       BSPNODE *root= BSPconstructTree(&faceList); /* construct BSP tree */
  27.  
  28.       BSPtraverseTreeAndRender(root,&oldPosition); /* traverse and render it */
  29.  
  30.       BSPfreeTree(&root);    /* free it */
  31.    }
  32.    freeFaceList(&faceList);    /* free list of faces read in */
  33.    
  34.    return(0);
  35. } /* main() */
  36.  
  37. /* Reads from fileName a list of convex planar faces oriented counter-clockwise
  38.  * of at least 3 vertices each where the first three vertices are not 
  39.  * collinear. The last vertex of each face will automatically be duplicated. 
  40.  * This list of faces is returned.
  41.  * 
  42.  */ 
  43. void getScene(const char *fileName,POINT *position,FACE **fList)
  44. {
  45.    FACE *fListTail= NULL_FACE;
  46.    VERTEX *vList= NULL_VERTEX, *vListTail= NULL_VERTEX;
  47.    static char buffer[MAXBUFFER];
  48.    COLOR color; PLANE plane;
  49.  
  50.    FILE *fp= fopen(fileName,"r");
  51.    if (fp == NULL) {fprintf(stderr,"?Unable to open %s\n",fileName); exit(1);}
  52.    printf("File: %s\n",fileName);
  53.  
  54.    position->xx= 0.0; position->yy= 5.0; position->zz= 10.0; 
  55.    *fList= NULL_FACE; 
  56.  
  57.    while (fgets(buffer,MAXBUFFER,fp)) {
  58.       if (buffer[0] == 'f') {    /* start of face */
  59.      if (vList != NULL_VERTEX) { /* previous face? */
  60.         appendVertex(&vList,&vListTail, /* append duplicate 1st vertex  */
  61.              allocVertex(vList->xx,vList->yy,vList->zz));
  62.         computePlane(vList->xx,vList->yy,vList->zz,
  63.              vList->vnext->xx,vList->vnext->yy,vList->vnext->zz,
  64.              vList->vnext->vnext->xx,
  65.              vList->vnext->vnext->yy,
  66.              vList->vnext->vnext->zz,&plane);
  67.         appendFace(fList,&fListTail,allocFace(&color,vList,&plane));
  68.      }
  69.  
  70.      sscanf(buffer,"%*c %f %f %f",&color.rr,&color.gg,&color.bb);
  71.      /* save vars for this face and start new vertex list */
  72.      /* printf("f %f/%f/%f",color.rr,color.gg,color.bb); */
  73.      vList= vListTail= NULL_VERTEX;
  74.       }
  75.       else if (buffer[0] == 'v') { /* read in vertex */
  76.      float xx,yy,zz; sscanf(buffer,"%*c %f %f %f",&xx,&yy,&zz);
  77.      /* printf("v (%f,%f,%f)\n",xx,yy,zz); */
  78.      appendVertex(&vList,&vListTail,allocVertex(xx,yy,zz));
  79.       }
  80.       else if (buffer[0] == COMMENT) { } /* comment */
  81.       else fprintf(stderr,"?Illegal command: '%c'\n",buffer[0]);
  82.    } /* while */
  83.    if (vList != NULL_VERTEX) { /* process last face (or only) */ 
  84.       appendVertex(&vList,&vListTail, /* append duplicate 1st vertex */
  85.            allocVertex(vList->xx,vList->yy,vList->zz));
  86.       computePlane(vList->xx,vList->yy,vList->zz,vList->vnext->xx,
  87.            vList->vnext->yy,vList->vnext->zz,vList->vnext->vnext->xx,
  88.            vList->vnext->vnext->yy,vList->vnext->vnext->zz,&plane);
  89.       appendFace(fList,&fListTail,allocFace(&color,vList,&plane));
  90.    }
  91.  
  92.    fclose(fp);
  93. } /* getScene() */
  94. /*** mainBsp.c ***/
  95.