home *** CD-ROM | disk | FTP | other *** search
/ Compendium Deluxe 2 / LSD and 17bit Compendium Deluxe - Volume II.iso / a / prog / cprog / tdddconv.lha / writenff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-25  |  2.3 KB  |  100 lines

  1. /* writenff.c - dump the internal database to an NFF file
  2.  *            - written by Glenn M. Lewis - 10/29/91
  3.  */
  4.  
  5. static char rcs_id[] = "$Id: writenff.c,v 1.5 1991/11/25 21:32:10 glewis Exp glewis $";
  6.  
  7. #include <stdio.h>
  8. #include <ctype.h>
  9. #include "ttdddlib.h"
  10.  
  11. static void process_DESC();
  12. static void process_INFO();
  13. static FILE *out;
  14.  
  15. /* Here are a few necessary utilities */
  16.  
  17. static void send_XYZ(f)            /* Print a common string */
  18. XYZ_st *f;
  19. {
  20.     fprintf(out, "%g\t%g\t%g\n", f->val[0], f->val[1], f->val[2]);
  21. }
  22.  
  23. static void send_RGB(rgb)            /* Print a common string */
  24. RGB_st *rgb;
  25. {
  26.     fprintf(out, "%g\t%g\t%g\n", ((double)rgb->val[0])/255.0,
  27.         ((double)rgb->val[1])/255.0,
  28.         ((double)rgb->val[2])/255.0);
  29. }
  30.  
  31. /********************/
  32. /* The MAIN section */
  33. /********************/
  34.  
  35. int write_NFF(world, file)
  36. WORLD *world;
  37. FILE *file;
  38. {
  39.     register OBJECT *o;
  40.  
  41.     if (!(out = file)) return(0);
  42.  
  43.     if (world->info) process_INFO(world->info);
  44.  
  45.     for (o=world->object; o; o=o->next)
  46.         if (!o->extr) process_DESC(o);
  47.  
  48.     return(1);
  49. }
  50.  
  51. static void process_DESC(object)
  52. OBJECT *object;
  53. {
  54.     register int i;
  55.     register OBJECT *obj;
  56.     register DESC *desc = object->desc;
  57.     register int p1, p2, p3;
  58.  
  59.     /* Process children first */
  60.     for (obj=object->child; obj; obj=obj->next) {
  61.         if (!obj->extr) process_DESC(obj);
  62.     }
  63.  
  64.     if (!desc->pcount || !desc->fcount) return;
  65.  
  66.     fprintf(out, "\n# Start of new object\n");
  67.  
  68.     for (i=0; i<desc->fcount; i++) {
  69.     /* First check to make sure that this triangle is real */
  70.         p1 = desc->edge[(desc->face[i*3])<<1];
  71.         p2 = desc->edge[((desc->face[i*3])<<1)+1];
  72.         if (p1 == p2) continue;    /* How did *this* happen? */
  73.         p3 = desc->edge[(desc->face[i*3+2])<<1];
  74.         if (p1 == p3 || p2 == p3)
  75.             p3 = desc->edge[((desc->face[i*3+2])<<1)+1];
  76.         if (p1 == p3 || p2 == p3) continue; /* How did *this* happen? */
  77.         /* Now check the actual points for equality */
  78.         if (bcmp(&desc->pnts[p1], &desc->pnts[p2], sizeof(XYZ_st))==0 ||
  79.             bcmp(&desc->pnts[p1], &desc->pnts[p3], sizeof(XYZ_st))==0 ||
  80.             bcmp(&desc->pnts[p2], &desc->pnts[p3], sizeof(XYZ_st))==0)
  81.             continue;
  82.  
  83.         fprintf(out, "p 3\n");
  84.         send_XYZ(&desc->pnts[p1]);
  85.         send_XYZ(&desc->pnts[p2]);
  86.         send_XYZ(&desc->pnts[p3]);
  87.     }
  88. }
  89.  
  90. static void process_INFO(info)
  91. INFO *info;
  92. {
  93.     fprintf(out, "v\n");
  94.     if (info->obsv) {
  95.         fprintf(out, "from "); send_XYZ(&info->obsv->came);
  96.     }
  97.     if (info->ambi)
  98.         { fprintf(out, "b "); send_RGB(info->ambi); }
  99. }
  100.