home *** CD-ROM | disk | FTP | other *** search
/ Altsys Virtuoso 2.0K / virtuoso_20k.iso / DemoApps / Graphics / Viewers / raytracers / rpi / Source / readfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-08  |  3.9 KB  |  156 lines

  1.  
  2. /*
  3.  * (c) 1988 by George Kyriazis
  4.  */
  5.  
  6. /*
  7.  * read the input file of the ray-tracer
  8.  */
  9.  
  10. #include    <stdio.h>
  11. #include    <math.h>
  12. #include    "ray.h"
  13.  
  14. readfile(fname)
  15. char    *fname;
  16. {
  17.     FILE    *f;
  18.     int    i, j;
  19.     int    nos, nosq;
  20.     char    s[10];
  21.  
  22.     f = fopen(fname,"r");
  23.     if(f == NULL) {
  24.         perror("fopen");
  25.         exit(1);
  26.     }
  27.  
  28. /* file format is:
  29.  *    <fov> field of view
  30.  *    <eyex eyey eyez> position of the eye
  31.  *    <dirx diry dirz> dorection of view
  32.  *    <upx upy upz> up vector
  33.  *    <time1 time2> time limits
  34.  *    <#> background cuing
  35.  *    <iter> number of iterations per pixel
  36.  *    <x y z angle> coordinates and angle of the light source
  37.  *    number or spheres  number of squares
  38.  *     <x y z r [ambient] [diff] [spec] refl r g b refr r g b
  39.  *        width index refl_diffuse refr_diffuse tx ty tz> 
  40.  *        for every sphere
  41.  *    <x y z x y z x y z [ambient] [diff] [spec]
  42.  *        refl r g b refr r g b with index 
  43.  *        refl_diffuse refr_diffuse tx ty tz>
  44.  *          for every square
  45.  */
  46.  
  47. /* viewing transform */
  48.     fscanf(f, "%lf", &fov);
  49.     fov = tan( fov * M_PI / 180 ) / sqrt(2.0);
  50.     fscanf(f, "%lf %lf %lf", &eye.x, &eye.y, &eye.z);
  51.     fscanf(f, "%lf %lf %lf", &eye_dir.x, &eye_dir.y, &eye_dir.z);
  52.     fscanf(f, "%lf %lf %lf", &up.x, &up.y, &up.z);
  53.  
  54. /* time information */
  55.     fscanf(f, "%lf %lf", &time1, &time2);
  56.  
  57. /* the background flag */
  58.     fscanf(f, "%s", s);
  59.     if( *s == 'n' ) bgflag = NONE;
  60.     if( *s == 'x' ) bgflag = X;
  61.     if( *s == 'y' ) bgflag = Y;
  62.     if( *s == 'z' ) bgflag = Z;
  63.  
  64. /* how many samples per pixel? */
  65.     fscanf(f, "%d", &tries);
  66.  
  67. /* now the light source */
  68.     fscanf(f, "%lf %lf %lf %lf", &light.org.x, &light.org.y,
  69.         &light.org.z, &light.angle);
  70.     light.angle *= M_PI/180;
  71.  
  72.     fscanf(f, "%d %d", &nos, &nosq);
  73.     noo = nos + nosq;
  74.  
  75.     obj = (struct obj *)malloc(noo * sizeof(struct obj) );
  76.     if(obj == NULL) {
  77.         perror("malloc");
  78.         exit(1);
  79.     }
  80.  
  81.     i = 0;
  82.     for(j = 0; j < nos; j++) {
  83.         obj[i].type = SPHERE;
  84.         fscanf(f, "%lf %lf %lf %lf", &obj[i].data.sphere.center.x,
  85.             &obj[i].data.sphere.center.y,
  86.             &obj[i].data.sphere.center.z,
  87.             &obj[i].data.sphere.radius );
  88.         fscanf(f, "%lf %lf %lf", &obj[i].ambient.r,
  89.             &obj[i].ambient.g,
  90.             &obj[i].ambient.b);
  91.         fscanf(f, "%lf %lf %lf", &obj[i].diffuse.r,
  92.             &obj[i].diffuse.g,
  93.             &obj[i].diffuse.b);
  94.         fscanf(f, "%lf %lf %lf", &obj[i].specular.r,
  95.             &obj[i].specular.g,
  96.             &obj[i].specular.b);
  97.         fscanf(f, "%lf %lf %lf %lf", &obj[i].reflection,
  98.             &obj[i].refl_color.r,
  99.             &obj[i].refl_color.g,
  100.             &obj[i].refl_color.b);
  101.         fscanf(f, "%lf %lf %lf %lf", &obj[i].refraction,
  102.             &obj[i].refr_color.r,
  103.             &obj[i].refr_color.g,
  104.             &obj[i].refr_color.b);
  105.         fscanf(f, "%lf %lf", &obj[i].width, &obj[i].index);
  106.         fscanf(f, "%lf %lf", &obj[i].refl_diffuse,
  107.                 &obj[i].refr_diffuse);
  108.             obj[i].refl_diffuse *= M_PI / 180;
  109.             obj[i].refr_diffuse *= M_PI / 180;
  110.         fscanf(f, "%lf %lf %lf", &obj[i].time.x,
  111.             &obj[i].time.y, &obj[i].time.z);
  112.  
  113.         i++;
  114.     }
  115.  
  116.     for( j = 0; j < nosq; j++) {
  117.         obj[i].type = SQUARE;
  118.         fscanf(f, "%lf %lf %lf", &obj[i].data.quad.p1.x,
  119.             &obj[i].data.quad.p1.y,
  120.             &obj[i].data.quad.p1.z);
  121.         fscanf(f, "%lf %lf %lf", &obj[i].data.quad.p2.x,
  122.             &obj[i].data.quad.p2.y,
  123.             &obj[i].data.quad.p2.z);
  124.         fscanf(f, "%lf %lf %lf", &obj[i].data.quad.p3.x,
  125.             &obj[i].data.quad.p3.y,
  126.             &obj[i].data.quad.p3.z);
  127.         fscanf(f, "%lf %lf %lf", &obj[i].ambient.r,
  128.             &obj[i].ambient.g,
  129.             &obj[i].ambient.b);
  130.         fscanf(f, "%lf %lf %lf", &obj[i].diffuse.r,
  131.             &obj[i].diffuse.g,
  132.             &obj[i].diffuse.b);
  133.         fscanf(f, "%lf %lf %lf", &obj[i].specular.r,
  134.             &obj[i].specular.g,
  135.             &obj[i].specular.b);
  136.         fscanf(f, "%lf %lf %lf %lf", &obj[i].reflection,
  137.             &obj[i].refl_color.r,
  138.             &obj[i].refl_color.g,
  139.             &obj[i].refl_color.b);
  140.         fscanf(f, "%lf %lf %lf %lf", &obj[i].refraction,
  141.             &obj[i].refr_color.r,
  142.             &obj[i].refr_color.g,
  143.             &obj[i].refr_color.b);
  144.         fscanf(f, "%lf %lf", &obj[i].width, &obj[i].index);
  145.         fscanf(f, "%lf %lf", &obj[i].refl_diffuse,
  146.                 &obj[i].refr_diffuse);
  147.             obj[i].refl_diffuse *= M_PI / 180;
  148.             obj[i].refr_diffuse *= M_PI / 180;
  149.         fscanf(f, "%lf %lf %lf", &obj[i].time.x,
  150.             &obj[i].time.y, &obj[i].time.z);
  151.  
  152.         i++;
  153.     }
  154.  
  155. }
  156.