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

  1. /* writevort.c - dump the internal database to a Vort input file
  2.  *             - written by Glenn M. Lewis - 10/11/91
  3.  */
  4.  
  5. static char rcs_id[] = "$Id: writevort.c,v 1.5 1991/10/16 05:22:35 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];
  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.     fprintf(out, " %g,%g,%g ", f->val[0], f->val[1], f->val[2]);
  53. }
  54.  
  55. static void send_RGB(rgb)            /* Print a common string */
  56. RGB_st *rgb;
  57. {
  58.     fprintf(out, " %g,%g,%g ", ((double)rgb->val[0])/255.0,
  59.         ((double)rgb->val[1])/255.0,
  60.         ((double)rgb->val[2])/255.0);
  61. }
  62.  
  63. /********************/
  64. /* The MAIN section */
  65. /********************/
  66.  
  67. int write_Vort(world, file)
  68. WORLD *world;
  69. FILE *file;
  70. {
  71.     register OBJECT *o;
  72.  
  73.     if (!(out=file)) return(0);    /* File not open */
  74.  
  75.     tab[0] = '\0';
  76.     num_OBJ = num_DESC = num_TOBJ = 0;
  77.  
  78.     fputs("/* TTDDD library - Written by Glenn M. Lewis - 10/11/91 */\n\n",out);
  79.     fputs("maxhitlevel 1\n", out);
  80.     fputs("light {\n  color 1,1,1\n  direction(-1,1,-1)\n}\n", out);
  81.     fputs("up(0,0,1)\nlookat(10,-100,100,0,0,0,0)\n", out);
  82.  
  83.     if (world->info) process_INFO(world->info);
  84.     for (o=world->object; o; o=o->next)
  85.         process_OBJ(o);
  86.     return(1);
  87. }
  88.  
  89. static void process_INFO(info)
  90. INFO *info;
  91. {
  92.     fprintf(out, "%s/* INFO Begin */\n", tab);
  93.     indent();
  94. /*
  95.     for (i=0; i<8; i++)
  96.         if (info->brsh[i][0])
  97.             fprintf(out, "%sBRSH[%d]=\"%s\"\n", tab, i, info->brsh[i]);
  98.  
  99.     for (i=0; i<8; i++)
  100.         if (info->stnc[i][0])
  101.             fprintf(out, "%sSTNC[%d]=\"%s\"\n", tab, i, info->stnc[i]);
  102.  
  103.     for (i=0; i<8; i++)
  104.         if (info->txtr[i][0])
  105.             fprintf(out, "%sTXTR[%d]=\"%s\"\n", tab, i, info->txtr[i]);
  106. */
  107.  
  108.     if (info->obsv) {
  109.         fprintf(out, "%slookat(", tab); send_XYZ(&info->obsv->came);
  110.         fprintf(out, "%s/* OBSV Rotate", tab); send_XYZ(&info->obsv->rota);
  111.         fprintf(out, "%s   focaldist  %g */\n", tab, info->obsv->foca);
  112.         fprintf(out, "%s)", tab);
  113.     }
  114.  
  115. /*    if (info->otrk[0]) fprintf(out, "%sOTRK \"%s\"\n", tab, info->otrk);    */
  116.  
  117. /*
  118.     if (info->ostr) {
  119.         if (info->ostr->path[0])
  120.             fprintf(out, "%sOSTR Path \"%s\"\n", tab, info->ostr->path);
  121.         fprintf(out, "%sOSTR Translate", tab); send_XYZ(&info->ostr->tran);
  122.         fprintf(out, "%sOSTR Rotate   ", tab); send_XYZ(&info->ostr->rota);
  123.         fprintf(out, "%sOSTR Scale    ", tab); send_XYZ(&info->ostr->scal);
  124.         i = info->ostr->info;
  125.         strin[0] = '\0';
  126.         if (i&(1<<0))  strcat(strin, " ABS_TRA");
  127.         if (i&(1<<1))  strcat(strin, " ABS_ROT");
  128.         if (i&(1<<2))  strcat(strin, " ABS_SCL");
  129.         if (i&(1<<4))  strcat(strin, " LOC_TRA");
  130.         if (i&(1<<5))  strcat(strin, " LOC_ROT");
  131.         if (i&(1<<6))  strcat(strin, " LOC_SCL");
  132.         if (i&(1<<8))  strcat(strin, " X_ALIGN");
  133.         if (i&(1<<9))  strcat(strin, " Y_ALIGN");
  134.         if (i&(1<<10)) strcat(strin, " Z_ALIGN");
  135.         if (i&(1<<12)) strcat(strin, " FOLLOW_ME");
  136.         fprintf(out, "%sOSTR Info%s\n", tab, strin);
  137.     }
  138. */
  139.  
  140.     if (info->fade) {
  141.         fprintf(out, "%s/* FADE FadeAt %g*/\n", tab, info->fade->at);
  142.         fprintf(out, "%s/* FADE FadeBy %g*/\n", tab, info->fade->by);
  143.         fprintf(out, "%s/* FADE FadeTo", tab); send_RGB(&info->fade->to);
  144.         fprintf(out, "*/\n");
  145.     }
  146.  
  147.     if (info->skyc) {
  148.         fprintf(out, "%s/* SKYX Horizon", tab); send_RGB(&info->skyc->hori);
  149.         fprintf(out, "*/\n");
  150.         fprintf(out, "%s/* SKYC Zenith ", tab); send_RGB(&info->skyc->zeni);
  151.         fprintf(out, "*/\n");
  152.     }
  153.  
  154.     if (info->ambi)
  155.         { fprintf(out, "%sbackground", tab); send_RGB(info->ambi); }
  156.  
  157. /*
  158.     if (info->glb0)
  159.         for (i=0; i<8; i++)
  160.             fprintf(out, "%sGLB0[%d]=%u\n", tab, i, info->glb0[i]);
  161. */
  162.  
  163.     outdent();
  164.     fprintf(out, "%s/* End INFO */\n", tab);
  165. }
  166.  
  167. static void process_OBJ(obj)
  168. register OBJECT *obj;
  169. {
  170.     register HIER *p;
  171.     num_OBJ++;
  172.     fprintf(out, "%s/* OBJ Begin \"Hierarchy %d\" */\n", tab, num_OBJ);
  173.     num_DESC = num_TOBJ = 0;        /* Reset counters */
  174.     cur_level = 0;
  175.     cur_objnum = 1;
  176.     prntline = 1;
  177.  
  178.     if (obj->extr) process_EXTR(obj->extr);
  179.     else process_DESC(obj);
  180.  
  181.     while (root) {                /* This should happen at most once. */
  182.         p = root->next;
  183.         free((char *)root);        /* Delete this from list */
  184.         root = p;
  185.         outdent();
  186.     }
  187.  
  188.     fprintf(out, "%s/* End OBJ   \"Hierarchy %d\" */\n", tab, num_OBJ);
  189. }
  190.  
  191. static int process_TOBJ()
  192. {
  193.     register HIER *p;
  194.     if (num_DESC-num_TOBJ < cur_level) {    /* Pop old level off HIER */
  195.         cur_level--;
  196.         cur_objnum = root->objnum;
  197.         p = root->next;
  198.         free((char *)root);    /* Delete from list */
  199.         root = p;
  200.         outdent();    /* Pretty file formatting */
  201.     }
  202.     fprintf(out, "%s}\n", tab);
  203.     fprintf(out, "%s/* TOBJ       \"Object %d at level %d of hierarchy %d\" */\n",
  204.         tab, cur_objnum-1, num_DESC-num_TOBJ, num_OBJ);
  205.     num_TOBJ++;
  206.     prntline = 1;
  207. }
  208.  
  209. static void process_EXTR(extr)
  210. EXTR *extr;
  211. {
  212.     register int i;
  213.  
  214.     if (!prntline) fprintf(out, "\n");    /* Print one anyway */
  215.     indent();
  216.     num_DESC++;
  217.  
  218.     fprintf(out, "%s/* EXTR Begin \"Object %d at level %d of hierarchy %d\" */\n",
  219.         tab, cur_objnum, num_DESC-num_TOBJ, num_OBJ);
  220.     fprintf(out, "%scomposite {\n", tab);
  221.     indent();
  222.     fprintf(out, "%s/* LOAD \"%s\" */\n", tab, extr->filename);
  223.     fprintf(out, "include %s\n", extr->filename);
  224.     fprintf(out, "%stranslate(", tab); send_XYZ(&extr->mtrx.tran);
  225.     fprintf(out, ")\n%sscale    (", tab); send_XYZ(&extr->mtrx.scal);
  226.     fprintf(out, "\n%stransform {\n", tab);
  227.     indent();
  228.     fprintf(out, "%s%g,%g,%g,",  tab,
  229.         extr->mtrx.rota1.val[0],
  230.         extr->mtrx.rota1.val[1],
  231.         extr->mtrx.rota1.val[2]);
  232.     fprintf(out, "%s%g,%g,%g,",  tab,
  233.         extr->mtrx.rota2.val[0],
  234.         extr->mtrx.rota2.val[1],
  235.         extr->mtrx.rota2.val[2]);
  236.     fprintf(out, "%s%g,%g,%g\n", tab,
  237.         extr->mtrx.rota3.val[0],
  238.         extr->mtrx.rota3.val[1],
  239.         extr->mtrx.rota3.val[2]);
  240.     outdent();
  241.     fprintf(out, "%s}\n", tab);
  242.  
  243.     outdent();
  244.     fprintf(out, "%s}\n", tab);
  245.     fprintf(out, "%s/* End EXTR   \"Object %d at level %d of hierarchy %d\" */\n",
  246.         tab, cur_objnum, num_DESC-num_TOBJ, num_OBJ);
  247.  
  248.     num_TOBJ++;
  249.     cur_objnum++;
  250.     outdent();
  251.     prntline = 1;
  252. }
  253.  
  254. static void process_DESC(object)
  255. OBJECT *object;
  256. {
  257.     register int i;
  258.     register HIER *p;
  259.     register OBJECT *obj;
  260.     register DESC *desc = object->desc;
  261.     register int p1, p2, p3;
  262.  
  263.     num_DESC++;
  264.     if (num_DESC-num_TOBJ > cur_level) {    /* Push new level in HIER */
  265.         if (!prntline) fprintf(out, "\n");    /* Print one anyway */
  266.         if (!(p = (HIER*)malloc(sizeof(HIER)))) {
  267.             fprintf(stderr, "ERROR!  Out of memory.\n*** ABORT ***\n");
  268.             exit(20);
  269.         }
  270.         p->next = root;        /* Insert into list */
  271.         root = p;
  272.         root->objnum = cur_objnum;
  273.         cur_level++;
  274.         cur_objnum = 1;
  275.         indent();    /* Pretty file formatting */
  276.     }
  277.  
  278.     fprintf(out, "%s/* DESC Begin \"Object %d at level %d of hierarchy %d\" */\n",
  279.         tab, cur_objnum, num_DESC-num_TOBJ, num_OBJ);
  280. /*    fprintf(out, "%sgrid 20 20 20\n", tab);    */
  281.     fprintf(out, "%scomposite {\n", tab);
  282.     indent();
  283.  
  284.     if (desc->name[0]) fprintf(out, "%s/* name \"%s\" */\n", tab, desc->name);
  285.  
  286.     if (desc->posi) {
  287.         fprintf(out, "%stranslate(", tab);
  288.         send_XYZ(desc->posi);
  289.         fprintf(out, ")\n");
  290.     }
  291.  
  292.     if (desc->colr) {
  293.         fprintf(out, "%sambient", tab);
  294.         send_RGB(desc->colr);
  295.         fprintf(out, "\n");
  296.     }
  297.  
  298.     if (desc->refl) {
  299.         fprintf(out, "%s/* specular", tab);
  300.         send_RGB(desc->refl);
  301.         fprintf(out, "*/\n");
  302.     }
  303.  
  304.     if (desc->tran) { fprintf(out, "%s/* TRAN", tab); send_RGB(desc->tran);
  305.         fprintf(out, "*/\n");
  306.     }
  307.  
  308. #ifdef WRITETTDDD
  309.     if (desc->fcount) {
  310.         fprintf(out, "%sCLST Count %u\n", tab, i);
  311.         for (i=0; i<desc->fcount; i++) {
  312.             if (desc->colr) {
  313.                 if (desc->clst[i*3  ]==desc->colr->val[0] &&
  314.                     desc->clst[i*3+1]==desc->colr->val[1] &&
  315.                     desc->clst[i*3+2]==desc->colr->val[2]) continue; /* Skip */
  316.             } else {
  317.                 if (desc->clst[i*3  ]==240 &&
  318.                     desc->clst[i*3+1]==240 &&
  319.                     desc->clst[i*3+2]==240) continue;    /* Skip this one */
  320.             }
  321.             fprintf(out, "%sCLST Color[%u]", tab, i);
  322.             send_RGB((RGB_st*)&desc->clst[i*3]);
  323.         }
  324.         fprintf(out, "%sRLST Count %u\n", tab, i);
  325.         for (i=0; i<desc->fcount; i++) {
  326.             if (desc->refl) {
  327.                 if (desc->rlst[i*3  ]==desc->refl->val[0] &&
  328.                     desc->rlst[i*3+1]==desc->refl->val[1] &&
  329.                     desc->rlst[i*3+2]==desc->refl->val[2]) continue; /* Skip */
  330.             } else {
  331.                 if (desc->rlst[i*3  ]==0 &&
  332.                     desc->rlst[i*3+1]==0 &&
  333.                     desc->rlst[i*3+2]==0) continue;    /* Skip this one */
  334.             }
  335.             fprintf(out, "%sRLST Color[%u]", tab, i);
  336.             send_RGB((RGB_st*)&desc->rlst[i*3]);
  337.         }
  338.         fprintf(out, "%sTLST Count %u\n", tab, i);
  339.         for (i=0; i<desc->fcount; i++) {
  340.             if (desc->tran) {
  341.                 if (desc->tlst[i*3  ]==desc->tran->val[0] &&
  342.                     desc->tlst[i*3+1]==desc->tran->val[1] &&
  343.                     desc->tlst[i*3+2]==desc->tran->val[2]) continue; /* Skip */
  344.             } else {
  345.                 if (desc->tlst[i*3  ]==0 &&
  346.                     desc->tlst[i*3+1]==0 &&
  347.                     desc->tlst[i*3+2]==0) continue;    /* Skip this one */
  348.             }
  349.             fprintf(out, "%sTLST Color[%u]", tab, i);
  350.             send_RGB((RGB_st*)&desc->tlst[i*3]);
  351.         }
  352.     }
  353. #endif
  354.  
  355.     if (desc->tpar) {
  356.         for (i=0; i<16; i++)
  357.             fprintf(out, "%s/* TPAR[%u]=%g */\n", tab, i, desc->tpar[i]);
  358.     }
  359.  
  360.     if (desc->surf) {
  361.         for (i=0; i<5; i++)
  362.             fprintf(out, "%s/* SURF[%u]=%d */\n", tab, i, desc->surf[i]);
  363.     }
  364.  
  365.     if (desc->mttr) {
  366.         fprintf(out, "%s/* MTTR Type =%u */\n", tab, desc->mttr->type);
  367.         fprintf(out, "%s/* MTTR Index=%g */\n", tab, (double)desc->mttr->indx);
  368.     }
  369.  
  370.     if (desc->spec) {
  371.         fprintf(out, "%s/* SPEC Spec=%u */\n", tab, desc->spec[0]);
  372.         fprintf(out, "%s/* SPEC Hard=%u */\n", tab, desc->spec[1]);
  373.     }
  374.  
  375.     if (desc->prp0) {
  376.         for (i=0; i<6; i++)
  377.             fprintf(out, "%s/* PRP0[%u]=%u */\n", tab, i, desc->prp0[i]);
  378.     }
  379.  
  380.     if (desc->ints)
  381.         fprintf(out, "%s/* INTS=%g */\n", tab, desc->ints);
  382.  
  383. #ifdef WRITETDDD
  384.     if (desc->stry) {
  385.         fprintf(out, "%sSTRY Path \"%s\"\n", tab, desc->stry->path);
  386.         fprintf(out, "%sSTRY Translate", tab); send_XYZ(&desc->stry->tran);
  387.         fprintf(out, "%sSTRY Rotate   ", tab); send_XYZ(&desc->stry->rota);
  388.         fprintf(out, "%sSTRY Scale    ", tab); send_XYZ(&desc->stry->scal);
  389.         i = desc->stry->info;
  390.         strin[0] = '\0';
  391.         if (i&(1<<0))  strcat(strin, " ABS_TRA");
  392.         if (i&(1<<1))  strcat(strin, " ABS_ROT");
  393.         if (i&(1<<2))  strcat(strin, " ABS_SCL");
  394.         if (i&(1<<4))  strcat(strin, " LOC_TRA");
  395.         if (i&(1<<5))  strcat(strin, " LOC_ROT");
  396.         if (i&(1<<6))  strcat(strin, " LOC_SCL");
  397.         if (i&(1<<8))  strcat(strin, " X_ALIGN");
  398.         if (i&(1<<9))  strcat(strin, " Y_ALIGN");
  399.         if (i&(1<<10)) strcat(strin, " Z_ALIGN");
  400.         if (i&(1<<12)) strcat(strin, " FOLLOW_ME");
  401.         fprintf(out, "%sSTRY Info%s\n", tab, strin);
  402.     }
  403. #endif
  404.  
  405.     if (desc->shap) {
  406.         fprintf(out, "%s/* SHAP Shape = %u */\n", tab, desc->shap[0]);
  407.         if (desc->shap==0 && !desc->shap[1]) {    /* Sphere */
  408.             fprintf(out, "%ssphere {\n", tab);
  409.             indent();
  410.             fprintf(out, "%scenter(", tab);
  411.             send_XYZ(desc->posi);
  412.             fprintf(out, ")\n%sradius %g\n", tab, desc->size->val[0]);
  413.             outdent();
  414.             fprintf(out, "%s}\n", tab);
  415.             goto SKIP_REST;
  416.         }
  417.         fprintf(out, "%s/* SHAP Lamp  = %u */\n", tab, desc->shap[1]);
  418.         if (desc->shap[1]) {
  419.             fprintf(out, "%slight {\n", tab);
  420.             indent();
  421.             if (desc->colr) {
  422.                 fprintf(out, "%scolor", tab);
  423.                 send_RGB(desc->colr);
  424.                 fprintf(out, "\n");
  425.             }
  426.             if (desc->shap[1]==1)    /* Sunlight */
  427.                 fprintf(out, "%sdirection(%g,%g,%g)\n", tab,
  428.                     -(desc->posi->val[0]),
  429.                     -(desc->posi->val[1]),
  430.                     -(desc->posi->val[2]));
  431.             else    /* Like a lamp */
  432.                 fprintf(out, "%slocation(%g,%g,%g)\n", tab,
  433.                     desc->posi->val[0],
  434.                     desc->posi->val[1],
  435.                     desc->posi->val[2]);
  436.             outdent();
  437.             fprintf(out, "%s}\n", tab);
  438.             goto SKIP_REST;
  439.         }
  440.     }
  441.  
  442. /*
  443.     if (desc->axis) {
  444.         fprintf(out, "%sAXIS XAxis", tab); send_XYZ(&desc->axis->xaxi);
  445.         fprintf(out, "%sAXIS YAxis", tab); send_XYZ(&desc->axis->yaxi);
  446.         fprintf(out, "%sAXIS ZAxis", tab); send_XYZ(&desc->axis->zaxi);
  447.     }
  448.  
  449.     if (desc->size)
  450.         { fprintf(out, "%sSIZE", tab); send_XYZ(desc->size); }
  451.  
  452.     if (desc->pcount) {
  453.         fprintf(out, "%sPNTS PCount %u\n", tab, desc->pcount);
  454.         for (i=0; i<desc->pcount; i++) {
  455.             fprintf(out, "%sPNTS Point[%d]", tab, i);
  456.             send_XYZ(&desc->pnts[i]);
  457.         }
  458.     }
  459.  
  460.     if (desc->ecount) {
  461.         fprintf(out, "%sEDGE ECount %u\n", tab, desc->ecount);
  462.         for (i=0; i<desc->ecount; i++) {
  463.             fprintf(out, "%sEDGE Edge[%d] %u %u\n", tab, i,
  464.                 desc->edge[i<<1], desc->edge[(i<<1)+1]);
  465.         }
  466.     }
  467. */
  468.  
  469.     if (desc->fcount) {
  470.         fprintf(out, "%s/* FACE TCount %u */\n", tab, desc->fcount);
  471.         for (i=0; i<desc->fcount; i++) {
  472. /*            fprintf(out, "%sFACE Connect[%u] %u %u %u\n", tab, i,
  473.                 desc->face[i*3], desc->face[i*3+1], desc->face[i*3+2]);
  474. */
  475.         /* First check to make sure that this triangle is real */
  476.             p1 = desc->edge[(desc->face[i*3])<<1];
  477.             p2 = desc->edge[((desc->face[i*3])<<1)+1];
  478.             if (p1 == p2) continue;    /* How did *this* happen? */
  479.             p3 = desc->edge[(desc->face[i*3+2])<<1];
  480.             if (p1 == p3 || p2 == p3)
  481.                 p3 = desc->edge[((desc->face[i*3+2])<<1)+1];
  482.             if (p1 == p3 || p2 == p3) continue; /* How did *this* happen? */
  483.         /* Now check the actual points for equality */
  484.             if (bcmp(&desc->pnts[p1], &desc->pnts[p2], sizeof(XYZ_st))==0 ||
  485.                 bcmp(&desc->pnts[p1], &desc->pnts[p3], sizeof(XYZ_st))==0 ||
  486.                 bcmp(&desc->pnts[p2], &desc->pnts[p3], sizeof(XYZ_st))==0)
  487.                 continue;
  488.  
  489.             fprintf(out, "%spolygon {\n", tab);
  490.             indent();
  491.             fprintf(out, "%svertex(", tab);        send_XYZ(&desc->pnts[p1]);
  492.             fprintf(out, ")\n%svertex(", tab);    send_XYZ(&desc->pnts[p2]);
  493.             fprintf(out, ")\n%svertex(", tab);    send_XYZ(&desc->pnts[p3]);
  494.             outdent();
  495.             fprintf(out, ")\n%s}\n", tab);
  496.         }
  497.     }
  498.  
  499. SKIP_REST:
  500.  
  501.     for (obj=object->child; obj; obj=obj->next) {
  502.         if (obj->extr) process_EXTR(obj->extr);
  503.         else process_DESC(obj);
  504.     }
  505.  
  506.     outdent();
  507.     fprintf(out, "%s/* End DESC   \"Object %d at level %d of hierarchy %d\" */\n",
  508.         tab, cur_objnum, num_DESC-num_TOBJ, num_OBJ);
  509.  
  510.  
  511.     cur_objnum++;
  512.     prntline = 0;
  513.     process_TOBJ();
  514. }
  515.  
  516.