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

  1. /* write.c - dump the internal database to a TTDDD file
  2.  *         - written by Glenn M. Lewis - 7/19/91
  3.  */
  4.  
  5. static char rcs_id[] = "$Id: write.c,v 1.4 1991/09/05 17:51:13 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_EXTR();
  13. static void process_INFO();
  14. static void process_OBJ();
  15.  
  16. /* Two-space tabs */
  17. #define TABSTOP "  "
  18.  
  19. static FILE *out;
  20. static char tab[133], strin[133];
  21. static int num_OBJ, num_DESC, num_TOBJ;
  22. static int cur_level, cur_objnum;
  23. static int prntline;
  24.  
  25. static struct save_hier {
  26.     int objnum;
  27.     struct save_hier *next;
  28. } *root = 0;
  29. typedef struct save_hier HIER;
  30.  
  31. /* Here are a few necessary utilities */
  32.  
  33. static void indent()
  34. {
  35.     strcat(tab, TABSTOP);
  36. }
  37.  
  38. static void outdent()
  39. {
  40.     register int i = strlen(tab) - strlen(TABSTOP);
  41.     if (i<0) {
  42.         fprintf(stderr, "Whoa, Glenn!  You blew it!\n");
  43.         tab[0] = '\0';
  44.         return;
  45.     }
  46.     tab[i] = '\0';
  47. }
  48.  
  49. static void send_XYZ(f)            /* Print a common string */
  50. XYZ_st *f;
  51. {
  52. /* I noticed that with a Ultrix C compiler, these in-line function calls were
  53.  * not executed in the order specified.  Since I want this code to be portable,
  54.  * and since some C compilers are broken, I seperated the following lines.
  55.  * This goes for *all* in-line function calls in this source code.
  56.  */
  57.     fprintf(out, " X=%g",   f->val[0]);
  58.     fprintf(out, " Y=%g",   f->val[1]);
  59.     fprintf(out, " Z=%g\n", f->val[2]);
  60. }
  61.  
  62. static void send_RGB(rgb)            /* Print a common string */
  63. RGB_st *rgb;
  64. {
  65.     fprintf(out, " R=%u",   rgb->val[0]);
  66.     fprintf(out, " G=%u",   rgb->val[1]);
  67.     fprintf(out, " B=%u\n", rgb->val[2]);
  68. }
  69.  
  70. /********************/
  71. /* The MAIN section */
  72. /********************/
  73.  
  74. int write_TTDDD(world, file)
  75. WORLD *world;
  76. FILE *file;
  77. {
  78.     register OBJECT *o;
  79.  
  80.     if (!(out=file)) return(0);    /* File not open */
  81.  
  82.     tab[0] = '\0';
  83.     num_OBJ = num_DESC = num_TOBJ = 0;
  84.  
  85.     fputs("% TTDDD library - Written by Glenn M. Lewis - 7/19/91\n\n", out);
  86.     if (world->info) process_INFO(world->info);
  87.     for (o=world->object; o; o=o->next)
  88.         process_OBJ(o);
  89.     return(1);
  90. }
  91.  
  92. static void process_INFO(info)
  93. INFO *info;
  94. {
  95.     register int i;
  96.  
  97.     fprintf(out, "%sINFO Begin\n", tab);
  98.     indent();
  99.     for (i=0; i<8; i++)
  100.         if (info->brsh[i][0])
  101.             fprintf(out, "%sBRSH[%d]=\"%s\"\n", tab, i, info->brsh[i]);
  102.  
  103.     for (i=0; i<8; i++)
  104.         if (info->stnc[i][0])
  105.             fprintf(out, "%sSTNC[%d]=\"%s\"\n", tab, i, info->stnc[i]);
  106.  
  107.     for (i=0; i<8; i++)
  108.         if (info->txtr[i][0])
  109.             fprintf(out, "%sTXTR[%d]=\"%s\"\n", tab, i, info->txtr[i]);
  110.  
  111.     if (info->obsv) {
  112.         fprintf(out, "%sOBSV Camera", tab); send_XYZ(&info->obsv->came);
  113.         fprintf(out, "%sOBSV Rotate", tab); send_XYZ(&info->obsv->rota);
  114.         fprintf(out, "%sOBSV Focal  %g\n", tab, info->obsv->foca);
  115.     }
  116.  
  117.     if (info->otrk[0]) fprintf(out, "%sOTRK \"%s\"\n", tab, info->otrk);
  118.  
  119.     if (info->ostr) {
  120.         if (info->ostr->path[0])
  121.             fprintf(out, "%sOSTR Path \"%s\"\n", tab, info->ostr->path);
  122.         fprintf(out, "%sOSTR Translate", tab); send_XYZ(&info->ostr->tran);
  123.         fprintf(out, "%sOSTR Rotate   ", tab); send_XYZ(&info->ostr->rota);
  124.         fprintf(out, "%sOSTR Scale    ", tab); send_XYZ(&info->ostr->scal);
  125.         i = info->ostr->info;
  126.         strin[0] = '\0';
  127.         if (i&(1<<0))  strcat(strin, " ABS_TRA");
  128.         if (i&(1<<1))  strcat(strin, " ABS_ROT");
  129.         if (i&(1<<2))  strcat(strin, " ABS_SCL");
  130.         if (i&(1<<4))  strcat(strin, " LOC_TRA");
  131.         if (i&(1<<5))  strcat(strin, " LOC_ROT");
  132.         if (i&(1<<6))  strcat(strin, " LOC_SCL");
  133.         if (i&(1<<8))  strcat(strin, " X_ALIGN");
  134.         if (i&(1<<9))  strcat(strin, " Y_ALIGN");
  135.         if (i&(1<<10)) strcat(strin, " Z_ALIGN");
  136.         if (i&(1<<12)) strcat(strin, " FOLLOW_ME");
  137.         fprintf(out, "%sOSTR Info%s\n", tab, strin);
  138.     }
  139.  
  140.     if (info->fade) {
  141.         fprintf(out, "%sFADE FadeAt %g\n", tab, info->fade->at);
  142.         fprintf(out, "%sFADE FadeBy %g\n", tab, info->fade->by);
  143.         fprintf(out, "%sFADE FadeTo", tab); send_RGB(&info->fade->to);
  144.     }
  145.  
  146.     if (info->skyc) {
  147.         fprintf(out, "%sSKYC Horizon", tab); send_RGB(&info->skyc->hori);
  148.         fprintf(out, "%sSKYC Zenith ", tab); send_RGB(&info->skyc->zeni);
  149.     }
  150.  
  151.     if (info->ambi)
  152.         { fprintf(out, "%sAMBI", tab); send_RGB(info->ambi); }
  153.  
  154.     if (info->glb0)
  155.         for (i=0; i<8; i++)
  156.             fprintf(out, "%sGLB0[%d]=%u\n", tab, i, info->glb0[i]);
  157.  
  158.     outdent();
  159.     fprintf(out, "%sEnd INFO\n", tab);
  160. }
  161.  
  162. static void process_OBJ(obj)
  163. register OBJECT *obj;
  164. {
  165.     register HIER *p;
  166.     num_OBJ++;
  167.     fprintf(out, "%sOBJ Begin \"Hierarchy %d\"\n", tab, num_OBJ);
  168.     num_DESC = num_TOBJ = 0;        /* Reset counters */
  169.     cur_level = 0;
  170.     cur_objnum = 1;
  171.     prntline = 1;
  172.  
  173.     if (obj->extr) process_EXTR(obj->extr);
  174.     else process_DESC(obj);
  175.  
  176.     while (root) {                /* This should happen at most once. */
  177.         p = root->next;
  178.         free((char *)root);        /* Delete this from list */
  179.         root = p;
  180.         outdent();
  181.     }
  182.  
  183.     fprintf(out, "%sEnd OBJ   \"Hierarchy %d\"\n", tab, num_OBJ);
  184. }
  185.  
  186. static int process_TOBJ()
  187. {
  188.     register HIER *p;
  189.     if (num_DESC-num_TOBJ < cur_level) {    /* Pop old level off HIER */
  190.         cur_level--;
  191.         cur_objnum = root->objnum;
  192.         p = root->next;
  193.         free((char *)root);    /* Delete from list */
  194.         root = p;
  195.         outdent();    /* Pretty file formatting */
  196.     }
  197.     fprintf(out, "%sTOBJ       \"Object %d at level %d of hierarchy %d\"\n",
  198.         tab, cur_objnum-1, num_DESC-num_TOBJ, num_OBJ);
  199.     num_TOBJ++;
  200.     prntline = 1;
  201. }
  202.  
  203. static void process_EXTR(extr)
  204. EXTR *extr;
  205. {
  206.     register int i;
  207.  
  208.     if (!prntline) fprintf(out, "\n");    /* Print one anyway */
  209.     indent();
  210.     num_DESC++;
  211.  
  212.     fprintf(out, "%sEXTR Begin \"Object %d at level %d of hierarchy %d\"\n",
  213.         tab, cur_objnum, num_DESC-num_TOBJ, num_OBJ);
  214.     indent();
  215.     fprintf(out, "%sMTRX Translate", tab); send_XYZ(&extr->mtrx.tran);
  216.     fprintf(out, "%sMTRX Scale    ", tab); send_XYZ(&extr->mtrx.scal);
  217.     fprintf(out, "%sMTRX Rotate", tab);
  218.     for (i=0; i<3; i++) fprintf(out, " %g", extr->mtrx.rota1.val[i]);
  219.     for (i=0; i<3; i++) fprintf(out, " %g", extr->mtrx.rota2.val[i]);
  220.     for (i=0; i<3; i++) fprintf(out, " %g", extr->mtrx.rota3.val[i]);
  221.     fprintf(out, "\n");
  222.  
  223.     fprintf(out, "%sLOAD \"%s\"\n", tab, extr->filename);
  224.     outdent();
  225.     fprintf(out, "%sEnd EXTR   \"Object %d at level %d of hierarchy %d\"\n",
  226.         tab, cur_objnum, num_DESC-num_TOBJ, num_OBJ);
  227.  
  228.     num_TOBJ++;
  229.     cur_objnum++;
  230.     outdent();
  231.     prntline = 1;
  232. }
  233.  
  234. static void process_DESC(object)
  235. OBJECT *object;
  236. {
  237.     register int i;
  238.     register HIER *p;
  239.     register OBJECT *obj;
  240.     register DESC *desc = object->desc;
  241.  
  242.     num_DESC++;
  243.     if (num_DESC-num_TOBJ > cur_level) {    /* Push new level in HIER */
  244.         if (!prntline) fprintf(out, "\n");    /* Print one anyway */
  245.         if (!(p = (HIER*)malloc(sizeof(HIER)))) {
  246.             fprintf(stderr, "ERROR!  Out of memory.\n*** ABORT ***\n");
  247.             exit(20);
  248.         }
  249.         p->next = root;        /* Insert into list */
  250.         root = p;
  251.         root->objnum = cur_objnum;
  252.         cur_level++;
  253.         cur_objnum = 1;
  254.         indent();    /* Pretty file formatting */
  255.     }
  256.  
  257.     fprintf(out, "%sDESC Begin \"Object %d at level %d of hierarchy %d\"\n",
  258.         tab, cur_objnum, num_DESC-num_TOBJ, num_OBJ);
  259.     indent();
  260.  
  261.     if (desc->name[0]) fprintf(out, "%sNAME \"%s\"\n", tab, desc->name);
  262.  
  263.     if (desc->shap) {
  264.         fprintf(out, "%sSHAP Shape = %u\n", tab, desc->shap[0]);
  265.         fprintf(out, "%sSHAP Lamp  = %u\n", tab, desc->shap[1]);
  266.     }
  267.  
  268.     if (desc->posi)
  269.         { fprintf(out, "%sPOSI", tab); send_XYZ(desc->posi); }
  270.  
  271.     if (desc->axis) {
  272.         fprintf(out, "%sAXIS XAxis", tab); send_XYZ(&desc->axis->xaxi);
  273.         fprintf(out, "%sAXIS YAxis", tab); send_XYZ(&desc->axis->yaxi);
  274.         fprintf(out, "%sAXIS ZAxis", tab); send_XYZ(&desc->axis->zaxi);
  275.     }
  276.  
  277.     if (desc->size)
  278.         { fprintf(out, "%sSIZE", tab); send_XYZ(desc->size); }
  279.  
  280.     if (desc->pcount) {
  281.         fprintf(out, "%sPNTS PCount %u\n", tab, desc->pcount);
  282.         for (i=0; i<desc->pcount; i++) {
  283.             fprintf(out, "%sPNTS Point[%d]", tab, i);
  284.             send_XYZ(&desc->pnts[i]);
  285.         }
  286.     }
  287.  
  288.     if (desc->ecount) {
  289.         fprintf(out, "%sEDGE ECount %u\n", tab, desc->ecount);
  290.         for (i=0; i<desc->ecount; i++) {
  291.             fprintf(out, "%sEDGE Edge[%d] %u %u\n", tab, i,
  292.                 desc->edge[i<<1], desc->edge[(i<<1)+1]);
  293.         }
  294.     }
  295.  
  296.     if (desc->fcount) {
  297.         fprintf(out, "%sFACE TCount %u\n", tab, desc->fcount);
  298.         for (i=0; i<desc->fcount; i++) {
  299.             fprintf(out, "%sFACE Connect[%u] %u %u %u\n", tab, i,
  300.                 desc->face[i*3], desc->face[i*3+1], desc->face[i*3+2]);
  301.         }
  302.     }
  303.  
  304.     if (desc->colr) { fprintf(out, "%sCOLR", tab); send_RGB(desc->colr); }
  305.  
  306.     if (desc->refl) { fprintf(out, "%sREFL", tab); send_RGB(desc->refl); }
  307.  
  308.     if (desc->tran) { fprintf(out, "%sTRAN", tab); send_RGB(desc->tran); }
  309.  
  310.     if (desc->spc1) { fprintf(out, "%sSPC1", tab); send_RGB(desc->spc1); }
  311.  
  312.     if (desc->fcount) {
  313.         fprintf(out, "%sCLST Count %u\n", tab, i);
  314.         for (i=0; i<desc->fcount; i++) {
  315.             if (desc->colr) {
  316.                 if (desc->clst[i*3  ]==desc->colr->val[0] &&
  317.                     desc->clst[i*3+1]==desc->colr->val[1] &&
  318.                     desc->clst[i*3+2]==desc->colr->val[2]) continue; /* Skip */
  319.             } else {
  320.                 if (desc->clst[i*3  ]==240 &&
  321.                     desc->clst[i*3+1]==240 &&
  322.                     desc->clst[i*3+2]==240) continue;    /* Skip this one */
  323.             }
  324.             fprintf(out, "%sCLST Color[%u]", tab, i);
  325.             send_RGB((RGB_st*)&desc->clst[i*3]);
  326.         }
  327.         fprintf(out, "%sRLST Count %u\n", tab, i);
  328.         for (i=0; i<desc->fcount; i++) {
  329.             if (desc->refl) {
  330.                 if (desc->rlst[i*3  ]==desc->refl->val[0] &&
  331.                     desc->rlst[i*3+1]==desc->refl->val[1] &&
  332.                     desc->rlst[i*3+2]==desc->refl->val[2]) continue; /* Skip */
  333.             } else {
  334.                 if (desc->rlst[i*3  ]==0 &&
  335.                     desc->rlst[i*3+1]==0 &&
  336.                     desc->rlst[i*3+2]==0) continue;    /* Skip this one */
  337.             }
  338.             fprintf(out, "%sRLST Color[%u]", tab, i);
  339.             send_RGB((RGB_st*)&desc->rlst[i*3]);
  340.         }
  341.         fprintf(out, "%sTLST Count %u\n", tab, i);
  342.         for (i=0; i<desc->fcount; i++) {
  343.             if (desc->tran) {
  344.                 if (desc->tlst[i*3  ]==desc->tran->val[0] &&
  345.                     desc->tlst[i*3+1]==desc->tran->val[1] &&
  346.                     desc->tlst[i*3+2]==desc->tran->val[2]) continue; /* Skip */
  347.             } else {
  348.                 if (desc->tlst[i*3  ]==0 &&
  349.                     desc->tlst[i*3+1]==0 &&
  350.                     desc->tlst[i*3+2]==0) continue;    /* Skip this one */
  351.             }
  352.             fprintf(out, "%sTLST Color[%u]", tab, i);
  353.             send_RGB((RGB_st*)&desc->tlst[i*3]);
  354.         }
  355.     }
  356.  
  357.     if (desc->tpar) {
  358.         for (i=0; i<16; i++)
  359.             fprintf(out, "%sTPAR[%u]=%g\n", tab, i, desc->tpar[i]);
  360.     }
  361.  
  362.     if (desc->surf) {
  363.         for (i=0; i<5; i++)
  364.             fprintf(out, "%sSURF[%u]=%d\n", tab, i, desc->surf[i]);
  365.     }
  366.  
  367.     if (desc->mttr) {
  368.         fprintf(out, "%sMTTR Type =%u\n", tab, desc->mttr->type);
  369.         fprintf(out, "%sMTTR Index=%g\n", tab, (double)desc->mttr->indx);
  370.     }
  371.  
  372.     if (desc->spec) {
  373.         fprintf(out, "%sSPEC Spec=%u\n", tab, desc->spec[0]);
  374.         fprintf(out, "%sSPEC Hard=%u\n", tab, desc->spec[1]);
  375.     }
  376.  
  377.     if (desc->prp0) {
  378.         for (i=0; i<6; i++)
  379.             fprintf(out, "%sPRP0[%u]=%u\n", tab, i, desc->prp0[i]);
  380.     }
  381.  
  382.     if (desc->prp1) {
  383.         for (i=0; i<8; i++)
  384.             fprintf(out, "%sPRP1[%u]=%u\n", tab, i, desc->prp1[i]);
  385.     }
  386.  
  387.     if (desc->ints)
  388.         fprintf(out, "%sINTS=%g\n", tab, desc->ints);
  389.  
  390.     if (desc->int1) { fprintf(out, "%sINT1", tab); send_RGB(desc->int1); }
  391.  
  392.     if (desc->stry) {
  393.         fprintf(out, "%sSTRY Path \"%s\"\n", tab, desc->stry->path);
  394.         fprintf(out, "%sSTRY Translate", tab); send_XYZ(&desc->stry->tran);
  395.         fprintf(out, "%sSTRY Rotate   ", tab); send_XYZ(&desc->stry->rota);
  396.         fprintf(out, "%sSTRY Scale    ", tab); send_XYZ(&desc->stry->scal);
  397.         i = desc->stry->info;
  398.         strin[0] = '\0';
  399.         if (i&(1<<0))  strcat(strin, " ABS_TRA");
  400.         if (i&(1<<1))  strcat(strin, " ABS_ROT");
  401.         if (i&(1<<2))  strcat(strin, " ABS_SCL");
  402.         if (i&(1<<4))  strcat(strin, " LOC_TRA");
  403.         if (i&(1<<5))  strcat(strin, " LOC_ROT");
  404.         if (i&(1<<6))  strcat(strin, " LOC_SCL");
  405.         if (i&(1<<8))  strcat(strin, " X_ALIGN");
  406.         if (i&(1<<9))  strcat(strin, " Y_ALIGN");
  407.         if (i&(1<<10)) strcat(strin, " Z_ALIGN");
  408.         if (i&(1<<12)) strcat(strin, " FOLLOW_ME");
  409.         fprintf(out, "%sSTRY Info%s\n", tab, strin);
  410.     }
  411.  
  412.     outdent();
  413.     fprintf(out, "%sEnd DESC   \"Object %d at level %d of hierarchy %d\"\n",
  414.         tab, cur_objnum, num_DESC-num_TOBJ, num_OBJ);
  415.  
  416.     for (obj=object->child; obj; obj=obj->next) {
  417.         if (obj->extr) process_EXTR(obj->extr);
  418.         else process_DESC(obj);
  419.     }
  420.  
  421.     cur_objnum++;
  422.     prntline = 0;
  423.     process_TOBJ();
  424. }
  425.  
  426.