home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Graphics / Graphics.zip / DBWBOUND.ZIP / EXT.C < prev    next >
Text File  |  1989-04-17  |  6KB  |  230 lines

  1. /************************************************************************
  2.  *                                                                      *
  3.  *                  Copyright (c) 1987, David B. Wecker                 *
  4.  *                         All Rights Reserved                          *
  5.  *                                                                      *
  6.  * This file is part of DBW_Render                                      *
  7.  *                                                                      *
  8.  * DBW_Render is distributed in the hope that it will be useful, but    *
  9.  * WITHOUT ANY WARRANTY. No author or distributor accepts               *
  10.  * responsibility to anyone for the consequences of using it or for     *
  11.  * whether it serves any particular purpose or works at all, unless     *
  12.  * he says so in writing. Refer to the DBW_Render General Public        *
  13.  * License for full details.                                            *
  14.  *                                                                      *
  15.  * Everyone is granted permission to copy, modify and redistribute      *
  16.  * DBW_Render, but only under the conditions described in the           *
  17.  * DBW_Render General Public License. A copy of this license is         *
  18.  * supposed to have been given to you along with DBW_Render so you      *
  19.  * can know your rights and responsibilities. It should be in a file    *
  20.  * named COPYING. Among other things, the copyright notice and this     *
  21.  * notice must be preserved on all copies.                              *
  22.  ************************************************************************
  23. #                                                                       #
  24. # Authors:                                                              #
  25. #     DBW - David B. Wecker                                             #
  26. #     jhl - John H. Lowery (IBM port)                                   #
  27. #                                                                       #
  28. # Versions:                                                             #
  29. #     V1.0 870125 DBW     - First released version                      #
  30. #     V2.0 880000 jhl - ported to IBM PC (MCGA/VGA display hardware)    #
  31. #                                                                       #
  32.  ************************************************************************/
  33.  
  34. #define MODULE_EXTENT
  35. #include "ray.h"
  36.  
  37. node *masternp = NULL;
  38. node **nppstack[100] = 
  39. {
  40.      &masternp,
  41. }
  42. ;
  43. int  nppsp = 0;
  44.  
  45. /* V1.01  */
  46. #ifdef MCH_AMIGA
  47. #define   LARGEST     ((float)1.7e+38)
  48. #else
  49.  
  50. /* V1.01  */
  51. #     ifdef IBM_PC
  52. #define   LARGEST     ((float)1.7e+38)
  53. #     else
  54. #define   LARGEST     HUGE
  55. #     endif
  56.  
  57. /* V1.01  */
  58. #endif
  59.  
  60. #define   curnpp         (nppstack[nppsp])
  61. #define   pushnpp(npp)   (nppstack[++nppsp] = npp)
  62. #define   popnpp         (nppstack[nppsp--])
  63.  
  64. void sphere_rextent();
  65. void triangle_rextent();
  66. void quad_rextent();
  67. void ring_rextent();
  68. void cyl_rextent();
  69. void cone_rextent();
  70.  
  71.  
  72.  
  73. void setextent(ep,re)
  74. extent *ep;
  75. rextent *re;
  76. {
  77.      int i;
  78.      float t;
  79.  
  80.      for (ep->radius = 0.,i = 0; i < 3; i++) 
  81.      {
  82.           ep->center[i] = (re->min[i] + re->max[i]) / 2.0;
  83.           t = re->max[i] - ep->center[i];
  84.           ep->radius += t * t;
  85.      }
  86.      ep->radius = (float)sqrt(ep->radius);
  87. }
  88.  
  89. /*
  90.  *   find the bounding extent (sphere) for the current object
  91.  */
  92.  
  93. void getextent(np,re)
  94. node *np;
  95. rextent *re;
  96. {
  97.      rextent newre;
  98.  
  99.      cv(LARGEST,LARGEST,LARGEST,re->min);     /* V1.01  */
  100.      cv(-LARGEST,-LARGEST,-LARGEST,re->max);  /* V1.01  */
  101.  
  102.      while (np) 
  103.      {
  104.           switch (np->kind) 
  105.           {
  106.                /* user-defined extent */
  107.           case EXTENT :
  108.  
  109.                getextent(eptr(np)->sub,&newre);
  110.                setextent(np,&newre);
  111.                break;
  112.  
  113.                /* planar objects */
  114.           case TRIANGLE:
  115.  
  116.                triangle_rextent(np,&newre); 
  117.                break;
  118.           case QUAD:
  119.  
  120.                quad_rextent(np,&newre); 
  121.                break;
  122.           case RING:
  123.  
  124.                ring_rextent(np,&newre); 
  125.                break;
  126.  
  127.                /* volume objects */
  128.           case SPHERE:
  129.  
  130.                sphere_rextent(np,&newre); 
  131.                break;
  132.           case CYLINDER:
  133.                /* in work */
  134.                cyl_rextent(np,&newre);
  135.                break;
  136.  
  137.           }
  138.           unionrextent(re,&newre,re);
  139.           np = np->next;
  140.      }
  141. }
  142.  
  143. /*
  144.  *   The bounding extent for a sphere is the sphere itself.
  145.  */
  146.  
  147. void sphere_rextent(sp,re)
  148. sphere *sp;
  149. rextent *re;
  150. {
  151.      int i;
  152.  
  153.      for (i = 0; i < 3; i++) 
  154.      {
  155.           re->min[i] = sp->center[i] - sp->radius;
  156.           re->max[i] = sp->center[i] + sp->radius;
  157.      }
  158. }
  159.  
  160. /*
  161.  *   The bounding extent for a triangle is
  162.  */
  163.  
  164. void triangle_rextent(tp,re)
  165. triangle *tp;
  166. rextent *re;
  167. {
  168.      vector v1,v2;
  169.  
  170.      cv(LARGEST,LARGEST,LARGEST,re->min);     /* V1.01  */
  171.      cv(-LARGEST,-LARGEST,-LARGEST,re->max);  /* V1.01  */
  172.  
  173.      vecsum(tp->position,tp->ve,v1);
  174.      vecsum(tp->position,tp->vp,v2);
  175.  
  176.      unionvector(re,tp->position,re);
  177.      unionvector(re,v1,re);
  178.      unionvector(re,v2,re);
  179. }
  180.  
  181. void quad_rextent(qp,re)
  182. quad *qp;
  183. rextent *re;
  184. {
  185.      vector v;
  186.  
  187.      cv(LARGEST,LARGEST,LARGEST,re->min);     /* V1.01  */
  188.      cv(-LARGEST,-LARGEST,-LARGEST,re->max);  /* V1.01  */
  189.  
  190.      unionvector(re,qp->position,re);
  191.  
  192.      vecsum(qp->position,qp->ve,v);
  193.      unionvector(re,v,re);
  194.  
  195.      vecsum(qp->position,qp->vp,v);
  196.      unionvector(re,v,re);
  197.  
  198.      vecsum(v,qp->ve,v);
  199.      unionvector(re,v,re);
  200. }
  201.  
  202. /*
  203.  *   The bounding extent for a ring is a sphere with
  204.  *   the same radius as the outside radius of the ring.
  205.  */
  206.  
  207. void ring_rextent(rp,re)
  208. ring *rp;
  209. rextent *re;
  210. {
  211.      int i;
  212.  
  213.      for (i = 0; i < 3; i++) 
  214.      {
  215.           re->min[i] = rp->position[i] - rp->maxrad;
  216.           re->max[i] = rp->position[i] + rp->maxrad;
  217.      }
  218. }
  219.  
  220. /* 
  221.  *   The bounding extent for a cylinder is a sphere
  222.  *   in which the cylinder will fit
  223.  */
  224.  
  225. void cyl_rextent(rp,re)
  226. cylinder *rp;
  227. rextent *re;
  228. {
  229. }
  230.