home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / graphic / qrt / stack.c < prev    next >
Text File  |  1990-01-21  |  10KB  |  383 lines

  1. /*******************************************************
  2.  
  3.                   stacks and queues
  4.  
  5.  *******************************************************/
  6.  
  7. #include "qrt.h"
  8. #include "pattern.h"
  9.  
  10. /* #define STACKDEBUG 1 */
  11.  
  12. char *malloc();
  13.  
  14. /**********************************************************
  15.  
  16.     Calls the pre-computing routine for each object in
  17.     the object tree by calling Do_Precomp_Tree().  Also,
  18.     does a bit of general housekeeping, like finding the
  19.     center of the screen, etc.
  20.  
  21.  **********************************************************/
  22.  
  23. Do_Precomp(node)
  24.   OBJ_PTR node;
  25. {
  26.   def.x_center = def.x_res/2; 
  27.   def.y_center = def.y_res/2; 
  28.  
  29.   THEWORLD.x_divisor = THEWORLD.flength * def.x_res *
  30.                        def.aspect / 60;
  31.  
  32.   THEWORLD.y_divisor = THEWORLD.flength * def.y_res / 70;
  33.  
  34.   Do_Precomp_Tree(node);
  35. }
  36.  
  37.  
  38. /**********************************************************
  39.  
  40.     Calls the pre-computing routine for each object in
  41.     the object tree.  The pre-computing routines figure
  42.     out some sub-expressions that don't change with
  43.     different calls to the intersection routines.
  44.  
  45.  **********************************************************/
  46.  
  47. Do_Precomp_Tree(node)
  48.   OBJ_PTR node;
  49. {
  50.  
  51. # ifdef STACKDEBUG
  52.     printf("DOPRECOMP: type = %d\n", node->type);
  53. # endif
  54.  
  55.   (*(ObjData[node->type].PreComp))(node);  /* precompute */
  56.  
  57.   if (node->child != NULL) {               /* node has children ? */
  58.  
  59. #   ifdef ROBUST
  60.       if (node->type != BBOX) Error(INTERNAL_ERROR,802);
  61. #   endif
  62.  
  63.     Do_Precomp(node->child);
  64.   }
  65.  
  66.   if (node->nextobj != NULL) {
  67.     Do_Precomp(node->nextobj);
  68.   }
  69. }
  70.  
  71. /**********************************************************
  72.  
  73.     Assigns bounding box values for entire object tree.
  74.     This must be called once before any ray-tracing is done.
  75.  
  76.  **********************************************************/
  77.  
  78. Make_Bbox(node)
  79.   OBJ_PTR node;
  80. {
  81.   OBJ_PTR tnode;
  82.   VECTOR  v1,v2;
  83.  
  84. # ifdef STACKDEBUG
  85.     printf("MAKEBBOX\n");
  86. # endif
  87.  
  88.   if (node->child!=NULL) {                 /* node has children ? */
  89.  
  90. #   ifdef ROBUST
  91.       if (node->type!=BBOX) Error(INTERNAL_ERROR,801);
  92. #   endif
  93.  
  94.     Make_Bbox(node->child);
  95.   }
  96.  
  97.   if (node->nextobj!=NULL) {               /* node has siblings ? */
  98.     Make_Bbox(node->nextobj);
  99.   }
  100.  
  101.   if (node->type==BBOX) {
  102.     tnode=node->child;
  103.  
  104.     node->lower.x=node->lower.y=node->lower.z=  BIG;
  105.     node->upper.x=node->upper.y=node->upper.z= -BIG;
  106.  
  107.     while (tnode!=NULL) {
  108.       (*(ObjData[tnode->type].FindBbox))(&v1,&v2,tnode);
  109.       node->lower.x=MIN(node->lower.x,v1.x);
  110.       node->lower.y=MIN(node->lower.y,v1.y);
  111.       node->lower.z=MIN(node->lower.z,v1.z);
  112.  
  113.       node->upper.x=MAX(node->upper.x,v2.x);
  114.       node->upper.y=MAX(node->upper.y,v2.y);
  115.       node->upper.z=MAX(node->upper.z,v2.z);
  116.  
  117. #     ifdef STACKDEBUG
  118.         printf("MAKEBBOX: v1 x,y,z = %f %f %f\n",
  119.                node->lower.x,node->lower.y,node->lower.z);
  120.         printf("          v2 x,y,z = %f %f %f\n",
  121.                node->upper.x,node->upper.y,node->upper.z);
  122. #     endif
  123.  
  124.       tnode=tnode->nextobj;
  125.     }
  126.   }
  127. }
  128.  
  129.  
  130. /**********************************************************
  131.  
  132.     Returns pointer to pattern structure given name, or
  133.     null if not found.
  134.  
  135.  **********************************************************/
  136.  
  137. PATTERN_PTR find_pat(name)
  138.   char *name;
  139. {
  140.   PATTERN_PTR pat;
  141.  
  142.   pat=THEWORLD.patlist;
  143.  
  144.   while (pat!=NULL) {
  145.     if (strcmp(name,pat->name)==0) return(pat);
  146.     pat=pat->sibling;
  147.   }
  148.  
  149.   return(NULL);
  150. }
  151.  
  152.  
  153. /**********************************************************
  154.  
  155.      Allocates a new pattern structure and returns a
  156.      pointer to it.
  157.  
  158.  **********************************************************/
  159.  
  160. PATTERN_PTR new_pat()
  161. {
  162.   PATTERN_PTR pat;
  163.  
  164.   if ((pat=(PATTERN_PTR)malloc(sizeof(PATTERN)))==NULL)
  165.     Error(MALLOC_FAILURE,802);
  166.  
  167.   pat->name    = NULL;
  168.   pat->child   =
  169.   pat->sibling =
  170.   pat->link    = NULL;
  171.  
  172.   return(pat);
  173. }
  174.  
  175.  
  176. /**********************************************************
  177.  
  178.      Allocates a new object structure, stuffs most of
  179.      its information fields, and returns a pointer to it.
  180.  
  181.  **********************************************************/
  182.  
  183. OBJ_PTR new_obj(type,loc,v1,v2,v3,cinfo,pattern,remove,name,
  184.                 upper, lower, cterm, xmult, ymult)
  185.  
  186.         short        type;
  187.         VECT_PTR     loc, v1, v2, v3, upper, lower;
  188.         CINFO_PTR    cinfo;
  189.         PATTERN_PTR  pattern, remove;
  190.         float        cterm, xmult, ymult;
  191.         char         *name;
  192. {
  193.   OBJ_PTR obj;
  194.  
  195.   if ((obj=(OBJ_PTR)malloc(sizeof(OBJ_STRUCT)))==NULL)
  196.     Error(MALLOC_FAILURE,803);
  197.  
  198.   obj->type=type;                            /* copy info */
  199.   VectEQ(&(obj->loc),loc);
  200.   VectEQ(&(obj->vect1),v1);
  201.   VectEQ(&(obj->vect2),v2);
  202.   VectEQ(&(obj->vect3),v3);
  203.   VectEQ(&(obj->lower),lower);
  204.   VectEQ(&(obj->upper),upper);
  205.  
  206.   obj->cterm = cterm;
  207.   obj->xmult = xmult;
  208.   obj->ymult = ymult;
  209.  
  210.   obj->name = name;
  211.  
  212.   copy_colorinfo(&(obj->cinfo),cinfo);       /* colorinfo */
  213.  
  214.   obj->nextobj = obj->child = NULL;          /* no relatives */
  215.   obj->pattern = pattern;
  216.   obj->remove  = remove;
  217.  
  218.   return(obj);
  219. }
  220.  
  221.  
  222. /**********************************************************
  223.  
  224.                 Generates an empty line
  225.  
  226.    - changed 13 Mar 89 to include fix by Paul Balyoz to
  227.      intialize cinfo structure.
  228.  
  229.  **********************************************************/
  230.  
  231. OBJ_PTR new_line() {
  232.   CINFO   cinfo;
  233.   VECTOR  loc,v1,v2,v3, upper, lower;
  234.   OBJ_PTR line;
  235.  
  236.   def_colorinfo(&cinfo);      /* initialize cinfo structure */
  237.  
  238.   VectEqZero(&loc);
  239.   VectEqZero(&v1);
  240.   VectEqZero(&v2);
  241.   VectEqZero(&v3);
  242.   VectEqZero(&upper);
  243.   VectEqZero(&lower);
  244.  
  245.   line=new_obj(LINE,&loc,&v1,&v2,&v3,&cinfo,NULL,NULL,NULL,
  246.                &upper,&lower,(float)0.0,(float)0.0,(float)0.0);
  247.  
  248.   line->flag = FALSE;
  249.   return(line);
  250. }
  251.  
  252.  
  253. /**********************************************************
  254.  
  255.                  Adds a lamp to the world
  256.  
  257.  **********************************************************/
  258.  
  259. add_lamp(object)                   /* add a lamp to the world */
  260.   OBJ_PTR object;
  261. {
  262.    object->nextobj=THEWORLD.lamps;
  263.    THEWORLD.lamps=object;
  264. }
  265.  
  266.  
  267. /**********************************************************
  268.  
  269.        Debugging routine to print object structure
  270.                  MAY BE WRONG BY NOW
  271.  
  272.  **********************************************************/
  273.  
  274. #ifdef STACKDEBUG
  275.  
  276.   print_obj(obj)                     /* display object */
  277.     OBJ_PTR obj;
  278.   {
  279.     printf("OBJECT :  type: ");
  280.     switch (obj->type) {
  281.       case LINE:           printf("LINE\n");          break;
  282.       case SPHERE:         printf("SPHERE\n");        break;
  283.       case PARALLELOGRAM:  printf("PARALLELOGRAM\n"); break;
  284.       case TRIANGLE:       printf("TRIANGLE\n");      break;
  285.       case LAMP:           printf("LAMP\n");          break;
  286.       case OBSERVER:       printf("OBSERVER\n");      break;
  287.       case GROUND:         printf("GROUND\n");        break;
  288.       case SKY:            printf("SKY\n");           break;
  289.       case BBOX:           printf("BBOX\n");          break;
  290.       case RING:           printf("RING\n");          break;
  291.       case QUADRATIC:      printf("QUADRATIC\n");     break;
  292.       default:             printf("Unknown!\n");      break;
  293.     }
  294.  
  295.     printf("          loc   : %f, %f, %f\n",
  296.            (obj->loc.x),
  297.            (obj->loc.y),
  298.            (obj->loc.z));
  299.  
  300.     printf("          vect1 : %f, %f, %f\n",
  301.            (obj->vect1.x),
  302.            (obj->vect1.y),
  303.            (obj->vect1.z));
  304.  
  305.     printf("          vect2 : %f, %f, %f\n",
  306.            (obj->vect2.x),
  307.            (obj->vect2.y),
  308.            (obj->vect2.z));
  309.  
  310.     printf("          vect3 : %f, %f, %f\n",
  311.            (obj->vect3.x),
  312.            (obj->vect3.y),
  313.            (obj->vect3.z));
  314.  
  315.   }
  316. #endif
  317.  
  318.  
  319. /**********************************************************
  320.  
  321.             Prints some interesting statistics
  322.  
  323.  **********************************************************/
  324.  
  325. World_Stats() {
  326.   printf("World Statistics:\n");
  327.   printf("  Objects:      %4d\n",THEWORLD.objcount);
  328.   printf("  Lamps:        %4d\n",THEWORLD.lampcount);
  329.  
  330.   printf("  Intersect tests         : %-8ld\n", THEWORLD.intersect_tests);
  331.   printf("  Total Intersections     : %-8ld\n",(THEWORLD.ray_intersects+
  332.                                                 THEWORLD.bbox_intersects));
  333.   printf("     Object intersections : %-8ld\n", THEWORLD.ray_intersects);
  334.   printf("     Bbox   intersections : %-8ld\n", THEWORLD.bbox_intersects);
  335.   printf("  Rays traced             : %-8ld\n",(THEWORLD.primary_traced+
  336.                                                 THEWORLD.to_lamp+
  337.                                                 THEWORLD.refl_trans));
  338.   printf("     Primary              : %-8ld\n", THEWORLD.primary_traced);
  339.   printf("     To lamps             : %-8ld\n", THEWORLD.to_lamp);
  340.   printf("     Refl. or Trans.      : %-8ld\n", THEWORLD.refl_trans);
  341.   printf("  Pattern match checks    : %-8ld\n", THEWORLD.pattern_matches);
  342.  
  343.   printf("  Image statistics\n");
  344.   printf("     X Resolution         : %-8d\n",  def.x_res);
  345.   printf("     Y Resolution         : %-8d\n",  def.y_res);
  346.   printf("     Aspect Ratio         : %-8.4f\n",def.aspect);
  347.  
  348.   printf("\n  Data sent to: %s\n\n",THEWORLD.outfile);
  349. }
  350.  
  351. /**********************************************************
  352.  
  353.             Open file, and send image size
  354.  
  355.  **********************************************************/
  356.  
  357. Open_File() {
  358. #ifdef MSDOS
  359.   if ((THEWORLD.filept=fopen(THEWORLD.outfile,"wb"))==NULL)
  360.     Error(FILE_ERROR,804);
  361. #else
  362.   if ((THEWORLD.filept=fopen(THEWORLD.outfile,"w"))==NULL)
  363.     Error(FILE_ERROR,804);
  364. #endif
  365.   fputc((unsigned char)(def.x_res &  0xff),THEWORLD.filept);
  366.   fputc((unsigned char)(def.x_res >> 8),   THEWORLD.filept);
  367.   fputc((unsigned char)(def.y_res &  0xff),THEWORLD.filept);
  368.   fputc((unsigned char)(def.y_res >> 8),   THEWORLD.filept);
  369. }
  370.  
  371.  
  372. /**********************************************************
  373.  
  374.                       Close file
  375.  
  376.  **********************************************************/
  377.  
  378. Close_File() {
  379.   if (fclose(THEWORLD.filept)) Error(FILE_ERROR,805);
  380. }
  381.  
  382.  
  383.