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

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