home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Raytrace & Morphing / SOS-RAYTRACE.ISO / programm / source / rayce27s / composit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-02  |  5.8 KB  |  275 lines

  1. /*
  2.  * composite.c -- standard routines for composites
  3.  * 
  4.  * (c) 1993, 1994 by Han-Wen Nienhuys <hanwen@stack.urc.tue.nl>
  5.  * 
  6.  * This program is free software; you can redistribute it and/or modify it
  7.  * under the terms of the GNU General Public License as published by the
  8.  * Free Software Foundation;
  9.  * 
  10.  * This program is distributed in the hope that it will be useful, but
  11.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  * General Public License for more details.
  14.  * 
  15.  * You should have received a copy of the GNU General Public License along
  16.  * with this program; if not, write to the Free Software Foundation, Inc.,
  17.  * 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. #include "ray.h"
  21. #include "proto.h"
  22. #include "extern.h"
  23.  
  24. extern struct methods my_methods;
  25.  
  26. /*
  27.  * Intersect a ray with a composite.
  28.  * 
  29.  * 1. intersect the element list of the composite
  30.  */
  31.  
  32. /*
  33.  * we should do some special handling of clipping shapes.
  34.  */
  35.  
  36. PRIVATE bool
  37. all_vanilla_composite_intersections(dqueue * globq, object *c, struct ray *r, int flags, bool *ins)
  38. {
  39.     object         *p;
  40.     bool            gotcha;
  41.  
  42.  
  43.  
  44.     gotcha = FALSE;
  45.  
  46.     for (p = c->data.composite->contents; p != NULL; p = p->next) {
  47.  
  48.     /* walk the element list, and intersect each element */
  49.     if (intersect_object(globq, p, r, flags))
  50.         gotcha = TRUE;
  51.  
  52.     if (!(flags & CHKALL))
  53.         r->maxt = globq->t;
  54.  
  55.     if (globq->t < r->mint)    /* early exit possible? */
  56.         break;
  57.     }
  58.  
  59.     return gotcha;
  60. }
  61.  
  62. PRIVATE bool
  63. all_composite_intersections(dqueue * globq, object *c, struct ray *r, int flags, bool *ins)
  64. {
  65.  
  66.     bool            gotcha;
  67.  
  68.     my_methods.test++;
  69.     gotcha = c->data.composite->all_intersections_method(globq, c, r, flags, ins);
  70.     if (gotcha)
  71.     my_methods.hit++;
  72.  
  73.     return gotcha;
  74. }
  75.  
  76.  
  77.  
  78.  
  79. /* speed up the composite */
  80. PUBLIC void
  81. speed_composite(object *o, vector spd)
  82. {
  83.     speed_object_list(o->data.composite->contents, spd);
  84. }
  85.  
  86.  
  87. /* initialize a composite_data struct */
  88. PRIVATE void
  89. init_composite(struct composite_data *t)
  90. {
  91.     t->contents = NULL;
  92.     t->optitype = NONE;
  93.     t->all_intersections_method = all_vanilla_composite_intersections;
  94. }
  95.  
  96. PRIVATE struct composite_data *
  97. get_new_composite(void)
  98. {
  99.     struct composite_data *p;
  100.     p = ALLOC(struct composite_data);
  101.  
  102.     CHECK_MEM(p, my_methods.name);
  103.     init_composite(p);
  104.     return p;
  105. }
  106.  
  107. /* append an object to the composite c */
  108. PUBLIC void
  109. add_to_composite(object *c, object *o)
  110. {
  111.     object         *tmp;
  112.  
  113.     tmp = c->data.composite->contents;
  114.     c->data.composite->contents = o;
  115.     o->next = tmp;
  116.     o->daddy = c;
  117. }
  118.  
  119. /* this is getting boring... */
  120. PRIVATE void
  121. copy_composite(object *dst, object *src)
  122. {
  123.     object         *p,
  124.                    *new;
  125.  
  126.  
  127.     if (dst->type != COMPOSITE)
  128.     dst->data.composite = get_new_composite();
  129.  
  130.     dst->type = src->type;
  131.  
  132.     for (p = src->data.composite->contents; p != NULL; p = p->next) {
  133.     new = get_new_object();
  134.     copy_object(new, p);
  135.     add_to_composite(dst, new);
  136.     }
  137. }
  138.  
  139. /* standard */
  140. PRIVATE void
  141. translate_composite(object *c, vector t)
  142. {
  143.     translate_object_list(c->data.composite->contents, t);
  144. }
  145.  
  146. /* hooooeeewaahhh, Geez, why do I write these comments? */
  147. PRIVATE void
  148. rotate_composite(object *c, matrix rotmat)
  149. {
  150.     rotate_object_list(c->data.composite->contents, rotmat);
  151. }
  152.  
  153. /* 3X raden (Yes, you folks out there, I'm now commenting in Dutch! */
  154. PRIVATE void
  155. scale_composite(object *c, vector s)
  156. {
  157.     scale_object_list(c->data.composite->contents, s);
  158. }
  159.  
  160. /*
  161.  * I guess .. because somebody will read them, some day (Hello, is there
  162.  * somebody out there?
  163.  * 
  164.  * and another free_XXX() (en wat hebben ze gewonnen, Pierre, als ze goed
  165.  * raden waar dit voor is ?
  166.  */
  167. PRIVATE void
  168. free_composite(object *c)
  169. {
  170.     free_object_list(c->data.composite->contents);
  171.     c->type = NOSHAPE;
  172. }
  173.  
  174. /* do debugging stuff */
  175. PRIVATE void
  176. print_composite(object *c)
  177. {
  178. #ifdef DEBUG
  179.     struct composite_data *p = c->data.composite;
  180.  
  181.     printf(" { optimisation %d\n", p->optitype);
  182.     print_object_list(p->contents);
  183.     printf("}");
  184. #endif
  185. }
  186.  
  187. PRIVATE bool
  188. inside_composite(object *o, vector x)
  189. {
  190.     assert(FALSE);
  191. }
  192.  
  193. PRIVATE vector
  194. composite_normal(struct intersect i, vector x)
  195. {
  196.     assert(FALSE);
  197. }
  198.  
  199. PRIVATE void
  200. precompute_composite(object *o)
  201. {
  202.     object         *op;
  203.     struct composite_data *c;
  204.  
  205.     c = o->data.composite;
  206.     precompute_object_list(c->contents);
  207.  
  208.     /* for the data: union of bboxes */
  209.     for (op = c->contents; op != NULL; op = op->next) {
  210.     if (op->bmin.x < o->bmin.x)
  211.         o->bmin.x = op->bmin.x;
  212.     if (op->bmin.y < o->bmin.y)
  213.         o->bmin.y = op->bmin.y;
  214.     if (op->bmin.z < o->bmin.z)
  215.         o->bmin.z = op->bmin.z;
  216.     if (op->bmax.x > o->bmax.x)
  217.         o->bmax.x = op->bmax.x;
  218.     if (op->bmax.y > o->bmax.y)
  219.         o->bmax.y = op->bmax.y;
  220.     if (op->bmax.z > o->bmax.z)
  221.         o->bmax.z = op->bmax.z;
  222.     }
  223.  
  224.     /* bounds: intersection of bboxes */
  225.     for (op = o->bound; op != NULL; op = op->next) {
  226.     o->bmin.x = MAX(o->bmin.x, op->bmin.x);
  227.     o->bmin.y = MAX(o->bmin.y, op->bmin.y);
  228.     o->bmin.z = MAX(o->bmin.z, op->bmin.z);
  229.     o->bmax.x = MIN(o->bmax.x, op->bmax.x);
  230.     o->bmax.y = MIN(o->bmax.y, op->bmax.y);
  231.     o->bmax.z = MIN(o->bmax.z, op->bmax.z);
  232.     }
  233.  
  234.     switch (c->optitype) {
  235.     case NONE:
  236.     c->all_intersections_method = all_vanilla_composite_intersections;
  237.     break;
  238.     default:
  239.     assert(FALSE);
  240.     }
  241.     my_methods.howmuch++;
  242.     global_stats.nonprims++;
  243. }
  244.  
  245. PRIVATE struct methods my_methods =
  246. {
  247.     all_composite_intersections,
  248.     composite_normal,
  249.     copy_composite,
  250.     inside_composite,
  251.     rotate_composite,
  252.     translate_composite,
  253.     scale_composite,
  254.     free_composite,
  255.     print_composite,
  256.     precompute_composite,
  257.     "composite",
  258. };
  259.  
  260.  
  261. PUBLIC object  *
  262. get_new_composite_object(void)
  263. {
  264.     object         *p;
  265.  
  266.     p = (object *) get_new_object();
  267.     p->data.composite = get_new_composite();
  268.  
  269.     p->type = COMPOSITE;
  270.     p->methods = &my_methods;
  271.     return p;
  272. }
  273.  
  274. /* hoihoihoihoi. */
  275.