home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / GRAPHICS / rayshade.lzh / list.c < prev    next >
Text File  |  1990-05-08  |  3KB  |  128 lines

  1. /*
  2.  * list.c
  3.  *
  4.  * Copyright (C) 1989, Craig E. Kolb
  5.  *
  6.  * This software may be freely copied, modified, and redistributed,
  7.  * provided that this copyright notice is preserved on all copies.
  8.  *
  9.  * There is no warranty or other guarantee of fitness for this software,
  10.  * it is provided solely .  Bug reports or fixes may be sent
  11.  * to the author, who may or may not act on them as he desires.
  12.  *
  13.  * You may not include this software in a program or other software product
  14.  * without supplying the source, or without informing the end-user that the
  15.  * source is available for no extra charge.
  16.  *
  17.  * If you modify this software, you should include a notice giving the
  18.  * name of the person performing the modification, the date of modification,
  19.  * and the reason for such modification.
  20.  *
  21.  * $Id: list.c,v 3.0 89/10/27 02:05:54 craig Exp $
  22.  *
  23.  * $Log:    list.c,v $
  24.  * Revision 3.0  89/10/27  02:05:54  craig
  25.  * Baseline for first official release.
  26.  * 
  27.  */
  28. #include <stdio.h>
  29. #include "constants.h"
  30. #include "typedefs.h"
  31. #include "funcdefs.h"
  32. /*
  33.  * Take a list whose DATA field points to a linked list of objects and
  34.  * turn it into a List.
  35.  */
  36. make_list(obj)
  37. Object *obj;
  38. {
  39.     int i;
  40.     List *list;
  41.     extern ObjList *find_bounds();
  42.  
  43.     list = (List *)Malloc(sizeof(List));
  44.     /*
  45.      * Find the unbounded objects on the list as well as the
  46.      * bounding box of the list.
  47.      */
  48.     list->unbounded = find_bounds((ObjList **)&obj->data, obj->bounds);
  49.     /*
  50.      * Transform bounding box if necessary.
  51.      */
  52.     if (obj->trans)
  53.         transform_bounds(&obj->trans->obj2world, obj->bounds);
  54.     for (i = 0; i < 3; i++) {
  55.         list->bounds[LOW][i] = obj->bounds[LOW][i];
  56.         list->bounds[HIGH][i] = obj->bounds[HIGH][i];
  57.     }
  58.     /*
  59.      * obj->data now holds list of bounded objects.
  60.      */
  61.     list->list = (ObjList *)obj->data;
  62.     obj->data = (char *)list;
  63. }
  64.  
  65. /*
  66.  * Intersect ray & list of objects.
  67.  */
  68. double
  69. int_list(list, source, ray, hitinfo)
  70. List *list;
  71. Primitive *source;
  72. Ray *ray;
  73. HitInfo *hitinfo;
  74. {
  75.     register ObjList *objlist;
  76.     double offset;
  77.     HitInfo hittmp;
  78.     double dist, mindist;
  79.     extern double intersect();
  80.  
  81.     mindist = FAR_AWAY;
  82.     hittmp.totaltrans = hitinfo->totaltrans;
  83.     /*
  84.      * Intersect with unbounded objects.
  85.      */
  86.     for (objlist = list->unbounded; objlist ; objlist = objlist->next) {
  87.         dist = intersect((Object *)objlist->data, source, ray,
  88.                     &hittmp);
  89.         if (dist > EPSILON && dist < mindist) {
  90.             mindist = dist;
  91.             *hitinfo = hittmp;
  92.         }
  93.     }
  94.     /*
  95.      * Check for intersection with bounding box.
  96.      */
  97.     if (OutOfBounds(&ray->pos, list->bounds)) {
  98.         offset = IntBounds(ray, list->bounds);
  99.         if (offset < EPSILON)
  100.             /*
  101.              * Ray never hit list.
  102.              */
  103.             return (mindist == FAR_AWAY ? 0. : mindist);
  104.         else if (mindist < offset)
  105.             /*
  106.              * Ray hit unbounded object closer than bounding box.
  107.              */
  108.             return mindist;
  109.         /*
  110.          * Else the ray enters list-space before it hits an
  111.          * unbounded object.
  112.          */
  113.     }
  114.     /*
  115.      * Intersect with objects on list.
  116.      */
  117.     for (objlist = list->list; objlist ; objlist = objlist->next) {
  118.         dist = intersect((Object *)objlist->data, source, ray,
  119.                         &hittmp);
  120.         if (dist > EPSILON && dist < mindist) {
  121.             mindist = dist;
  122.             *hitinfo = hittmp;
  123.         }
  124.     }
  125.  
  126.     return (mindist == FAR_AWAY ? 0. : mindist);
  127. }
  128.