home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / raytrace / dbw_render / source / c / FIL < prev    next >
Encoding:
Text File  |  1992-10-23  |  17.0 KB  |  537 lines

  1. /************************************************************************
  2.  *                                                                      *
  3.  *                  Copyright (c) 1987, David B. Wecker                 *
  4.  *                          All Rights Reserved                         *
  5.  *                                                                      *
  6.  * This file is part of DBW_Render                                      *
  7.  *                                                                      *
  8.  * DBW_Render is distributed in the hope that it will be useful, but    *
  9.  * WITHOUT ANY WARRANTY. No author or distributor accepts               *
  10.  * responsibility to anyone for the consequences of using it or for     *
  11.  * whether it serves any particular purpose or works at all, unless     *
  12.  * he says so in writing. Refer to the DBW_Render General Public        *
  13.  * License for full details.                                            *
  14.  *                                                                      *
  15.  * Everyone is granted permission to copy, modify and redistribute      *
  16.  * DBW_Render, but only under the conditions described in the           *
  17.  * DBW_Render General Public License. A copy of this license is         *
  18.  * supposed to have been given to you along with DBW_Render so you      *
  19.  * can know your rights and responsibilities. It should be in a file    *
  20.  * named COPYING. Among other things, the copyright notice and this     *
  21.  * notice must be preserved on all copies.                              *
  22.  ************************************************************************
  23.  
  24. #define MODULE_FILEIO
  25. #include "ray.h"
  26.  
  27. static long         *fracopp;
  28. static triangle     *scr_t;
  29. static extent       *scr_e;
  30. static sphere       *scr_s;
  31. static quad         *scr_q;
  32. static ring         *scr_r;
  33. static cylinder     *scr_c;
  34. static int          whichf;
  35.  
  36. void dumpnode(n)
  37. node    *n;
  38. {
  39.      while (n) 
  40.      {
  41.           switch (n->kind) 
  42.           {
  43.           case EXTENT :
  44.                printf("extent\n");
  45.                vecdump(eptr(n)->center,"center");
  46.                printf("radius = %f\n",eptr(n)->radius);
  47.                dumpnode(((extent *) n)->sub);
  48.                break;
  49.  
  50.           case SPHERE :
  51.                printf("sphere\n");
  52.                break;
  53.  
  54.           case TRIANGLE :
  55.  
  56.                printf("triangle\n");
  57.                break;
  58.  
  59.           case QUAD :
  60.                printf("quad\n");
  61.                break;
  62.  
  63.           case RING :
  64.                printf("ring\n");
  65.                break;
  66.  
  67.           default :
  68.  
  69.                printf("UNKNOWN KIND %d\n",n->kind);
  70.                break;
  71.           }
  72.           n = n->next;
  73.      }
  74.      printf("end\n");
  75. }
  76.  
  77. void copyattr( oa, na )
  78. attributes *oa, *na;
  79. {
  80.      na->ref = oa->ref;
  81.      na->idx = oa->idx;
  82.      na->fuz = oa->fuz;
  83.      na->tex = oa->tex;
  84.      veccopy( oa->tra, na->tra );
  85.      veccopy( oa->amb, na->amb );
  86.      veccopy( oa->dif, na->dif );
  87. }
  88.  
  89. void read_vec(v)
  90. vector  v;
  91. {
  92.      fscanf(df,"%f %f %f",&v[0],&v[1],&v[2]);
  93. }
  94.  
  95. void read_attr(attr)
  96. attributes *attr;
  97. {
  98.      fscanf(df,"%d %f %f %f",&attr->tex,&attr->fuz,&attr->ref,&attr->idx);
  99.      read_vec(attr->tra);
  100.      read_vec(attr->amb);
  101.      read_vec(attr->dif);
  102.      if (attr->tra[0] > 0.0 || attr->tra[1] > 0.0 || attr->tra[2] > 0.0)
  103.           allopaque = 0;
  104.      attr->fuz /= 10.0;  /* Assume more reasonable scale */
  105. }
  106.  
  107. void dofractal(level,a,b,c,attr)
  108. int         level;
  109. vector      a,b,c;
  110. attributes  *attr;
  111. {
  112.      float       aclen,bclen,ablen;
  113.      vector      ab,ac,bc,abbump,acbump,bcbump,v1;
  114.      long        *savedopp;
  115.  
  116.      if (level == 1) 
  117.      {
  118.           CHECK_ALLOC(scr_t,triangle);
  119.           *fracopp = (long)scr_t;
  120.           tptr(*fracopp)->next = NULL;
  121.           tptr(*fracopp)->kind = TRIANGLE;
  122.           copyattr(attr,&tptr(*fracopp)->attr);
  123.           veccopy(a,tptr(*fracopp)->position);
  124.           vecsub(c,a,ac);
  125.           veccopy(ac,tptr(*fracopp)->ve);
  126.           vecsub(b,a,ab);
  127.           veccopy(ab,tptr(*fracopp)->vp);
  128.           fracopp = (long *) &tptr(*fracopp)->next;
  129.      }
  130.      else
  131.      {
  132.           level--;
  133.           /* compute four subfaces */
  134.  
  135.           /* length of edges */
  136.           vecsub(a,c,v1);
  137.           aclen = norm(v1);
  138.           vecsub(a,b,v1);
  139.           ablen = norm(v1);
  140.           vecsub(b,c,v1);
  141.           bclen = norm(v1);
  142.  
  143.           /* edge midpoints */
  144.           vecsum(a,c,ac);
  145.           vecscale(0.5,ac,ac);
  146.           vecsum(a,b,ab);
  147.           vecscale(0.5,ab,ab);
  148.           vecsum(b,c,bc);
  149.           vecscale(0.5,bc,bc);
  150.  
  151.           /* midpoint perturbations */
  152.           noise3(ac,acbump);
  153.           if (ac[1] == 0.0)
  154.                acbump[1] = 0.0;
  155.           noise3(ab,abbump);
  156.           if (ab[1] == 0.0)
  157.                abbump[1] = 0.0;
  158.           noise3(bc,bcbump);
  159.           if (bc[1] == 0.0)
  160.                bcbump[1] = 0.0;
  161.           acbump[0] *= fractal[whichf].xscale;
  162.           acbump[1] *= fractal[whichf].yscale;
  163.           acbump[2] *= fractal[whichf].zscale;
  164.  
  165.           bcbump[0] *= fractal[whichf].xscale;
  166.           bcbump[1] *= fractal[whichf].yscale;
  167.           bcbump[2] *= fractal[whichf].zscale;
  168.  
  169.           abbump[0] *= fractal[whichf].xscale;
  170.           abbump[1] *= fractal[whichf].yscale;
  171.           abbump[2] *= fractal[whichf].zscale;
  172.  
  173.           /* scale the perturbations proportional to side length */
  174.           vecscale(aclen,acbump,acbump);
  175.           vecscale(ablen,abbump,abbump);
  176.           vecscale(bclen,bcbump,bcbump);
  177.  
  178.           /* new perturbed midpoints */
  179.           vecsum(abbump,ab,ab);
  180.           vecsum(acbump,ac,ac);
  181.           vecsum(bcbump,bc,bc);
  182.  
  183.           CHECK_ALLOC(scr_e,extent);
  184.           *fracopp = (long)scr_e;
  185.           eptr(*fracopp)->next = NULL;
  186.           eptr(*fracopp)->kind = EXTENT;
  187.           eptr(*fracopp)->sub = NULL;
  188.           savedopp = &*fracopp;
  189.           fracopp = (long *) &eptr(*fracopp)->sub;
  190.  
  191.           dofractal(level,a, ab,ac,attr);
  192.           dofractal(level,ac,bc,c, attr);
  193.           dofractal(level,ab,b, bc,attr);
  194.           dofractal(level,ac,ab,bc,attr);
  195.  
  196.           fracopp = (long *) &eptr(*savedopp)->next;
  197.      }
  198. }
  199.  
  200. void readimagefile(opp)
  201. long *opp;
  202. {
  203.      triangle    temp;
  204.      vector      a,b,c;
  205.  
  206.      while (EOF != fscanf(df,"%1s",str))
  207.           switch(str[0]) 
  208.           {
  209.           case 'D' :
  210.                fscanf(df,"%d %d",&MAXCOL,&MAXROW); 
  211.                if (MAXCOL > MAXX)
  212.                     MAXCOL = MAXX;
  213.                if (MAXROW > MAXY)
  214.                     MAXROW = MAXY;
  215.                break;
  216.           case 'R' :
  217.                fscanf(df,"%f",&maxhours);
  218.                break;
  219.           case 'N' :
  220.                fscanf(df,"%f",&idxref);
  221.                break;
  222.           case 'Z' :
  223.                fscanf(df,"%d",&histogram);
  224.                break;
  225.           case 'a' :
  226.                fscanf(df,"%f",&ambscale);
  227.                break;
  228.           case 'A' :
  229.                fscanf(df,"%d %f",&antialias,&variance);
  230.                break;
  231.           case 'F' :
  232.                fscanf(df,"%f %f",&aperture,&focus);
  233.                break;
  234.           case 'M' :
  235.                fscanf(df,"%d %d %d",&ambientlight,&amblitnum,&amblitdenom);
  236.                break;
  237.           case '&' :
  238.                fscanf(df,"%d %d",&startrow,&endrow); 
  239.                break;
  240.           case '*' :
  241.                fgets(fname,255,df);     /* get, and display    */ 
  242.                printf("%s",fname);
  243.                break;
  244.           case '!' :
  245.                fgets(fname,255,df);     /* get, and throw away */
  246.                break;
  247.           case 'b' :
  248.                read_vec(backgroundval);
  249.                break;
  250.           case 'w' :
  251.                read_vec(wave[numwaves].center);
  252.                fscanf(df,"%f",&wave[numwaves].wavelength);
  253.                fscanf(df,"%f",&wave[numwaves].amplitude);
  254.                fscanf(df,"%f",&wave[numwaves].drag);
  255.                fscanf(df,"%f",&wave[numwaves].propagate);
  256.                wave[numwaves].wavelength /= 2.0;  /* need half-wave */
  257.                wave[numwaves].propagate  /= 2.0;  /* need half-wave */
  258.                numwaves++;
  259.                break;
  260.           case 'g' :
  261.                read_vec(blend[numblends].color);
  262.                fscanf(df,"%f",&blend[numblends].start);
  263.                fscanf(df,"%f",&blend[numblends].scale);
  264.                numblends++;
  265.                break;
  266.           case 'n' :
  267.                fscanf(df,"%f",&snow[numsnows].start);
  268.                fscanf(df,"%f",&snow[numsnows].altscale);
  269.                fscanf(df,"%f",&snow[numsnows].altfactor);
  270.                fscanf(df,"%f",&snow[numsnows].threshhold);
  271.                numsnows++;
  272.                break;
  273.           case 'p' :
  274.                fscanf(df,"%f",&pebble[numpebbles].scale);
  275.                fscanf(df,"%f",&pebble[numpebbles].zoom);
  276.                numpebbles++;
  277.                break;
  278.           case 'k' :
  279.                read_vec(checker[numcheckers].color);
  280.                fscanf(df,"%f",&checker[numcheckers].x);
  281.                fscanf(df,"%f",&checker[numcheckers].y);
  282.                fscanf(df,"%f",&checker[numcheckers].z);
  283.                fscanf(df,"%f",&checker[numcheckers].bevel);
  284.                fscanf(df,"%f",&checker[numcheckers].angle);
  285.                fscanf(df,"%d", &checker[numcheckers].beveltype);
  286.                checker[numcheckers].bevel /= checker[numcheckers].x;
  287.                numcheckers++;
  288.                break;
  289.           case 'H' :
  290.                read_vec(haze[numhazes].color);
  291.                fscanf(df,"%f",&haze[numhazes].distscale);
  292.                numhazes++;
  293.                break;
  294.           case 'f' :
  295.                fscanf(df,"%d", &fractal[numfractals].level);
  296.                fscanf(df,"%f",&fractal[numfractals].xscale);
  297.                fscanf(df,"%f",&fractal[numfractals].yscale);
  298.                fscanf(df,"%f",&fractal[numfractals].zscale);
  299.                fscanf(df,"%d", &fractal[numfractals].texture);
  300.                numfractals++;
  301.                break;
  302.           case 'm' :
  303.                read_vec(marble[nummarble].veincolor);
  304.                fscanf(df,"%f",&marble[nummarble].xscale);
  305.                fscanf(df,"%f",&marble[nummarble].turbscale);
  306.                fscanf(df,"%d", &marble[nummarble].squeeze);
  307.                nummarble++;
  308.                break;
  309.           case 'd' :
  310.                read_vec(wood[numwoods].othercolor);
  311.                fscanf(df,"%f",&wood[numwoods].thickscale);
  312.                fscanf(df,"%f",&wood[numwoods].ringspacing);
  313.                fscanf(df,"%f",&wood[numwoods].turbscale);
  314.                fscanf(df,"%d", &wood[numwoods].squeeze);
  315.                numwoods++;
  316.                break;
  317.           case 'e' :
  318.                read_vec(eye);
  319.                read_vec(vrp);
  320.                read_vec(vu);
  321.                vecscale((float) MAXCOL / 256.0,vrp,vrp);  /* uniform scale */
  322.                cross(vrp,vu,vr);
  323.                cross(vr,vrp,vu);
  324.                normalize(vu);
  325.                normalize(vr);
  326.                vecsum(vrp,eye,vrp);
  327.                break;
  328.           case 'l' :
  329.                light[numlits].kind = 0;
  330.                read_vec(light[numlits].intensity);
  331.                read_vec(light[numlits].direction);
  332.                normalize(light[numlits++].direction);
  333.                break;
  334.           case 'L' :
  335.                light[numlits].kind = 1;
  336.                read_vec(light[numlits].intensity);
  337.                read_vec(light[numlits].direction);
  338.                fscanf(df,"%f",&light[numlits].distscale);
  339.                fscanf(df,"%f",&light[numlits].radius);
  340.                light[numlits].distscale = 
  341.                  light[numlits].distscale * light[numlits].distscale;
  342.                numlits++;
  343.                break;
  344.           case '{' :
  345.                CHECK_ALLOC(scr_e,extent);
  346.                *opp = (long)scr_e;
  347.                eptr(*opp)->next = NULL;
  348.                eptr(*opp)->kind = EXTENT;
  349.                eptr(*opp)->sub = NULL;
  350.                readimagefile(&eptr(*opp)->sub);
  351.                opp = (long *) &eptr(*opp)->next;
  352.                break;
  353.           case '}' :
  354.                return;
  355.                break;
  356.           case 's' :
  357.                CHECK_ALLOC(scr_s,sphere);
  358.                *opp = (long)scr_s;
  359.                sptr(*opp)->next = NULL;
  360.                sptr(*opp)->kind = SPHERE;
  361.                read_attr(&sptr(*opp)->attr);
  362.                read_vec(sptr(*opp)->center);
  363.                fscanf(df,"%f",&sptr(*opp)->radius);
  364.                opp = (long *) &sptr(*opp)->next;
  365.                break;
  366.           case 't' :
  367.                CHECK_ALLOC(scr_t,triangle);
  368.                *opp = (long)scr_t;
  369.                tptr(*opp)->next = NULL;
  370.                tptr(*opp)->kind = TRIANGLE;
  371.                read_attr(&tptr(*opp)->attr);
  372.                read_vec(tptr(*opp)->position);
  373.                read_vec(tptr(*opp)->ve);
  374.                read_vec(tptr(*opp)->vp);
  375.                opp = (long *) &tptr(*opp)->next;
  376.                break;
  377.           case 'x' :
  378.                read_attr(&temp.attr);
  379.                read_vec(temp.position);
  380.                read_vec(c);
  381.                read_vec(b);
  382.                whichf = temp.attr.tex - 60;
  383.                temp.attr.tex = fractal[whichf].texture;
  384.                fracopp = &*opp;
  385.                dofractal(fractal[whichf].level,
  386.                  temp.position,b,c,&temp.attr);
  387.                opp = &*fracopp;
  388.                break;
  389.           case 'q' :
  390.                CHECK_ALLOC(scr_q,quad);
  391.                *opp = (long)scr_q;
  392.                qptr(*opp)->next = NULL;
  393.                qptr(*opp)->kind = QUAD;
  394.                read_attr(&qptr(*opp)->attr);
  395.                read_vec(qptr(*opp)->position);
  396.                read_vec(qptr(*opp)->ve);
  397.                read_vec(qptr(*opp)->vp);
  398.                opp = (long *) &qptr(*opp)->next;
  399.                break;
  400.           case 'r' :
  401.                CHECK_ALLOC(scr_r ,ring);
  402.                *opp = (long)scr_r;
  403.                rptr(*opp)->next = NULL;
  404.                rptr(*opp)->kind = RING;
  405.                read_attr(&rptr(*opp)->attr);
  406.                read_vec(rptr(*opp)->position);
  407.                read_vec(rptr(*opp)->ve);
  408.                read_vec(rptr(*opp)->vp);
  409.                fscanf(df,"%f",&rptr(*opp)->minrad);
  410.                fscanf(df,"%f",&rptr(*opp)->maxrad);
  411.                rptr(*opp)->minrad =
  412.                  rptr(*opp)->minrad * rptr(*opp)->minrad;
  413.                rptr(*opp)->maxrad =
  414.                  rptr(*opp)->maxrad *rptr(*opp)->maxrad;
  415.                opp = (long *) &rptr(*opp)->next;
  416.                break;
  417.           case 'c' :
  418.                CHECK_ALLOC(scr_c ,cylinder);
  419.                *opp = (long)scr_c;
  420.                cptr(*opp)->next = NULL;
  421.                cptr(*opp)->kind = CYLINDER;
  422.                read_attr(&cptr(*opp)->attr);
  423.                read_vec(cptr(*opp)->bottom);
  424.                read_vec(cptr(*opp)->top);
  425.                fscanf(df,"%f",&cptr(*opp)->a);
  426.                fscanf(df,"%f",&cptr(*opp)->b);
  427.                fscanf(df,"%f",&cptr(*opp)->c);
  428.                opp = (long *) &cptr(*opp)->next;
  429.                break;
  430.           default  :
  431.                sprintf(fname,"Bad command char '%c' encountered",str[0]);
  432.                ERROR(fname);
  433.                break;
  434.           }
  435. }
  436.  
  437. void getinput(argc,argv)
  438. int     argc;
  439. char    **argv;
  440. {
  441.      int       i;
  442.      char      *s, *strchr();
  443.      rextent   re;
  444.  
  445.      if (argc == 1) 
  446.      {
  447.           printf("Scene description file? ");
  448.           scanf("%s",fname);
  449.      }
  450.      else
  451.           sprintf(fname,"%s",argv[1]);
  452.  
  453.      /* if no filetype specified, default it */
  454.  
  455.      if (strchr( fname, '.' ) == NULL)
  456.      {
  457.           strcat( fname, ".DAT" );
  458.      }
  459.  
  460.      df = fopen(fname,"r");
  461.      if (df == NULL)                               /* v1.01  */
  462.           ERROR("Error opening input file.");       /* v1.01  */
  463.  
  464.      /* get name portion of file, append .$ST to it for statistics */
  465.  
  466.      strcpy( outname, fname );
  467.      s = strchr( outname, '.');              /* guaranteed to be there */
  468.      if (s == NULL)
  469.           s = outname + strlen(outname);     /* but just to be sure... */
  470.  
  471.      *s = '\0';
  472.      strcat( outname, ".ST" );
  473.      fpout = fopen(outname,"w");
  474.      if (fpout == NULL)                             /* v1.01  */
  475.           ERROR("Error opening statistics file.");  /* v1.01  */
  476.  
  477.  
  478.      readimagefile(&root);
  479.  
  480.      getextent(root,&re);
  481.  
  482.      /* dumpnode(root);   DEBUG output */
  483.  
  484.      fclose(df);
  485. }
  486.  
  487. void write_scanline()
  488. {
  489.      int siz;
  490.  
  491. /*     fwrite(&sline,1,sizeof(int),fp);*/
  492.      fwrite(&sline,1,2,fp);
  493. /*- 1/21/89 ---
  494.     fwrite(redline,1,sizeof(scanlinetype),fp);
  495.     fwrite(greenline,1,sizeof(scanlinetype),fp);
  496.     fwrite(blueline,1,sizeof(scanlinetype),fp);
  497. -*/
  498.      siz = (MAXCOL/PPW)*sizeof(int);     /*file line size */
  499.  
  500.      fwrite(redline,1,siz,fp);
  501.      fwrite(greenline,1,siz,fp);
  502.      fwrite(blueline,1,siz,fp);
  503.  
  504.      sline++;
  505. }
  506.  
  507. void getoutput(argc,argv)
  508. int     argc;
  509. char    **argv;
  510. {
  511.      if (argc == 1) 
  512.      {
  513.           printf( "File name to save picture (.tmp)? " );
  514.           scanf( "%s", fname );
  515.      }
  516.      else
  517.           sprintf(fname,"%s.tmp",argv[1]);
  518.  
  519. #ifdef IBM_PC
  520.      fp = fopen(fname,"wb");   /* V1.01 - IBM level 2 i/o must be binary */
  521. #else
  522.      fp = fopen(fname,"w");
  523. #endif
  524.  
  525.      if (fp == NULL)
  526.      {
  527.           ERROR( "Error creating output file." );
  528.      }
  529.      else
  530.      {
  531. /*          fwrite(&MAXCOL,1,sizeof( MAXCOL ),fp);
  532.           fwrite(&MAXROW,1,sizeof( MAXROW ),fp);*/
  533.           fwrite(&MAXCOL,1,2,fp);
  534.           fwrite(&MAXROW,1,2,fp);
  535.      }
  536. }
  537.