home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Raytrace & Morphing / SOS-RAYTRACE.ISO / programm / source / radsrc22 / src / ot / writeoct.c < prev   
Encoding:
C/C++ Source or Header  |  1992-07-13  |  3.2 KB  |  155 lines

  1. /* Copyright (c) 1992 Regents of the University of California */
  2.  
  3. #ifndef lint
  4. static char SCCSid[] = "@(#)writeoct.c 2.2 7/13/92 LBL";
  5. #endif
  6.  
  7. /*
  8.  *  writeoct.c - routines for writing octree information to stdout.
  9.  *
  10.  *     7/30/85
  11.  */
  12.  
  13. #include  "standard.h"
  14.  
  15. #include  "octree.h"
  16.  
  17. #include  "object.h"
  18.  
  19. #include  "otypes.h"
  20.  
  21. static int  oputint(), oputstr(), puttree(), putobj();
  22.  
  23.  
  24. writeoct(store, scene, ofn)        /* write octree structures to stdout */
  25. int  store;
  26. CUBE  *scene;
  27. char  *ofn[];
  28. {
  29.     char  sbuf[64];
  30.     register int  i;
  31.                     /* write format number */
  32.     oputint((long)(OCTMAGIC+sizeof(OBJECT)), 2);
  33.  
  34.     if (!(store & IO_BOUNDS))
  35.         return;
  36.                     /* write boundaries */
  37.     for (i = 0; i < 3; i++) {
  38.         sprintf(sbuf, "%.12g", scene->cuorg[i]);
  39.         oputstr(sbuf);
  40.     }
  41.     sprintf(sbuf, "%.12g", scene->cusize);
  42.     oputstr(sbuf);
  43.                     /* write object file names */
  44.     if (store & IO_FILES)
  45.         for (i = 0; ofn[i] != NULL; i++)
  46.             oputstr(ofn[i]);
  47.     oputstr("");
  48.                     /* write number of objects */
  49.     oputint((long)nobjects, sizeof(OBJECT));
  50.  
  51.     if (!(store & IO_TREE))
  52.         return;
  53.                     /* write the octree */
  54.     puttree(scene->cutree);
  55.  
  56.     if (store & IO_FILES || !(store & IO_SCENE))
  57.         return;
  58.                     /* write the scene */
  59.     for (i = 0; i < NUMOTYPE; i++)
  60.         oputstr(ofun[i].funame);
  61.     oputstr("");
  62.     for (i = 0; i < nobjects; i++)
  63.         putobj(objptr(i));
  64.     putobj(NULL);
  65. }
  66.  
  67.  
  68. static
  69. oputstr(s)            /* write null-terminated string to stdout */
  70. register char  *s;
  71. {
  72.     putstr(s, stdout);
  73.     if (ferror(stdout))
  74.         error(SYSTEM, "write error in putstr");
  75. }
  76.  
  77.  
  78. static
  79. putfullnode(fn)            /* write out a full node */
  80. OCTREE  fn;
  81. {
  82.     OBJECT  oset[MAXSET+1];
  83.     register int  i;
  84.  
  85.     objset(oset, fn);
  86.     for (i = 0; i <= oset[0]; i++)
  87.         oputint((long)oset[i], sizeof(OBJECT));
  88. }
  89.  
  90.  
  91. static
  92. oputint(i, siz)            /* write a siz-byte integer to stdout */
  93. register long  i;
  94. register int  siz;
  95. {
  96.     putint(i, siz, stdout);
  97.     if (ferror(stdout))
  98.         error(SYSTEM, "write error in putint");
  99. }
  100.  
  101.  
  102. static
  103. oputflt(f)            /* put out floating point number */
  104. double  f;
  105. {
  106.     putflt(f, stdout);
  107.     if (ferror(stdout))
  108.         error(SYSTEM, "write error in putflt");
  109. }
  110.  
  111.  
  112. static
  113. puttree(ot)            /* write octree to stdout in pre-order form */
  114. register OCTREE  ot;
  115. {
  116.     register int  i;
  117.     
  118.     if (istree(ot)) {
  119.         putc(OT_TREE, stdout);        /* indicate tree */
  120.         for (i = 0; i < 8; i++)        /* write tree */
  121.             puttree(octkid(ot, i));
  122.     } else if (isfull(ot)) {
  123.         putc(OT_FULL, stdout);        /* indicate fullnode */
  124.         putfullnode(ot);        /* write fullnode */
  125.     } else
  126.         putc(OT_EMPTY, stdout);        /* indicate empty */
  127. }
  128.  
  129.  
  130. static
  131. putobj(o)            /* write out object */
  132. register OBJREC  *o;
  133. {
  134.     register int  i;
  135.  
  136.     if (o == NULL) {        /* terminator */
  137.         oputint(-1L, 1);
  138.         return;
  139.     }
  140.     oputint((long)o->otype, 1);
  141.     oputint((long)o->omod, sizeof(OBJECT));
  142.     oputstr(o->oname);
  143.     oputint((long)o->oargs.nsargs, 2);
  144.     for (i = 0; i < o->oargs.nsargs; i++)
  145.         oputstr(o->oargs.sarg[i]);
  146. #ifdef  IARGS
  147.     oputint((long)o->oargs.niargs, 2);
  148.     for (i = 0; i < o->oargs.niargs; i++)
  149.         oputint((long)o->oargs.iarg[i], 4);
  150. #endif
  151.     oputint((long)o->oargs.nfargs, 2);
  152.     for (i = 0; i < o->oargs.nfargs; i++)
  153.         oputflt(o->oargs.farg[i]);
  154. }
  155.