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

  1. /*
  2.  * object.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: object.c,v 3.0.1.2 90/02/12 13:18:44 craig Exp $
  22.  *
  23.  * $Log:    object.c,v $
  24.  * Revision 3.0.1.2  90/02/12  13:18:44  craig
  25.  * patch4: Changes to record number of primitives in each object.
  26.  * 
  27.  * Revision 3.0.1.1  89/12/06  16:33:20  craig
  28.  * patch2: Added calls to new error/warning routines.
  29.  * 
  30.  * Revision 3.0  89/10/27  02:05:58  craig
  31.  * Baseline for first official release.
  32.  * 
  33.  */
  34. #include <stdio.h>
  35. #include <math.h>
  36. #include "constants.h"
  37. #include "typedefs.h"
  38. #include "funcdefs.h"
  39. #include "texture.h"
  40.  
  41. Object *World;            /* World Object */
  42. ObjList *Objects;        /* Linked list of defined objects */
  43. int WorldXSize, WorldYSize, WorldZSize;        /* World grid resolution */
  44.  
  45. /*
  46.  * Create a new object with the given properties.
  47.  */
  48. Object *
  49. new_object(name, type, data, trans)
  50. char *name, *data;
  51. char type;
  52. Trans *trans;
  53. {
  54.     Object *new;
  55.  
  56.     new = (Object *)share_malloc(sizeof(Object));
  57.     new->name = strsave(name);
  58.     new->type = type;
  59.     new->data = data;
  60.     new->trans = trans;
  61.     new->prims = 0;
  62. #ifdef SHAREDMEM
  63.     /*
  64.      * If the counter is in shared memory, processes will
  65.      * be modifying it left-and-right.  So, we cheat and
  66.      * make counter a pointer to a non-shared location and
  67.      * store the value there.
  68.      */
  69.     new->counter = (unsigned long *)malloc(sizeof(unsigned long));
  70.     *new->counter = 0;
  71. #else
  72.     new->counter = 0;
  73. #endif
  74.     new->texture = (Texture *)0;
  75.     /*
  76.      * bounds is left uninitialized.
  77.      */
  78.     return new;
  79. }
  80.  
  81. /*
  82.  * Add a copy of the named object to parent object
  83.  */
  84. Object *
  85. add_child_named(name, parent)
  86. char *name;
  87. Object *parent;
  88. {
  89.     Object *child, *newobj;
  90.     int i;
  91.  
  92.     child = get_object_named(name);
  93.     if (child == (Object *)0)
  94.         yyerror("There is no object named \"%s\".", name);
  95.     /*
  96.      * Create new object that points to child
  97.      * and add to 'parent' list.
  98.      */
  99.     newobj = add_child(child, parent);
  100.     /*
  101.      * New object's bounding box is initally the same
  102.      * as the child's.
  103.      */
  104.     for (i = 0; i < 3; i++) {
  105.         newobj->bounds[0][i] = child->bounds[0][i];
  106.         newobj->bounds[1][i] = child->bounds[1][i];
  107.     }
  108.     return newobj;
  109. }
  110.  
  111. /*
  112.  * Add primitive object to parent object.
  113.  */
  114. add_prim(child, parent)
  115. Object *child, *parent;
  116. {
  117.     ObjList *newnode;
  118.  
  119.     newnode = (ObjList *)share_malloc(sizeof(ObjList));
  120.     newnode->data = child;
  121.     if (parent == (Object *)0) {
  122.         newnode->next = (ObjList *)World->data;
  123.         World->data = (char *)newnode;
  124.     } else {
  125.         newnode->next = (ObjList *)parent->data;
  126.         parent->data = (char *)newnode;
  127.     }
  128. }
  129.  
  130. /*
  131.  * Make a copy of "child" and attach it to parent's linked list
  132.  * of objects.
  133.  */
  134. Object *
  135. add_child(child, parent)
  136. Object *child, *parent;
  137. {
  138.     Object *newobj;
  139.     ObjList *newnode;
  140.  
  141.     newobj = new_object(NULL, child->type, child->data, child->trans);
  142.     newobj->texture = child->texture;
  143.     newnode = (ObjList *)share_malloc(sizeof(ObjList));
  144.     newnode->data = newobj;
  145.     if (parent == (Object *)0) {
  146.         newnode->next = (ObjList *)World->data;
  147.         World->data = (char *)newnode;
  148.         World->prims += child->prims;
  149.     } else {
  150.         newnode->next = (ObjList *)parent->data;
  151.         parent->data = (char *)newnode;
  152.         parent->prims += child->prims;
  153.     }
  154.     return newobj;
  155. }
  156.  
  157. /*
  158.  * Return pointer to named object, NULL if no such object has been defined.
  159.  */
  160. Object *
  161. get_object_named(name)
  162. char *name;
  163. {
  164.     ObjList *ltmp;
  165.     for (ltmp = Objects; ltmp; ltmp = ltmp->next)
  166.         if (strcmp(name, ltmp->data->name) == 0)
  167.             return ltmp->data;
  168.     return (Object *)0;
  169. }
  170.  
  171. /*
  172.  * Add object to list of defined objects.
  173.  */
  174. add_to_objects(obj)
  175. Object *obj;
  176. {
  177.     ObjList *ltmp;
  178.     extern int Verbose;
  179.     extern FILE *fstats;
  180.  
  181.     ltmp = (ObjList *)Malloc(sizeof(ObjList));
  182.     ltmp->data = obj;
  183.     ltmp->next = Objects;
  184.     Objects = ltmp;
  185.     if (Verbose) {
  186.         /*
  187.          * Report bounding box of named object.
  188.          */
  189.         fprintf(fstats,"Object \"%s\" extent:\n", obj->name);
  190.         print_bounds(obj->bounds);
  191.         fprintf(fstats,"\t%ld primitive%c\n",obj->prims,
  192.                 obj->prims == 1 ? ' ' : 's');
  193.     }
  194. }
  195.  
  196. /*
  197.  * Allocate space for a string, copy string into space.
  198.  */
  199. char *
  200. strsave(s)
  201. char *s;
  202. {
  203.     char *tmp;
  204.  
  205.     if (s == (char *)0)
  206.         return (char *)0;
  207.  
  208.     tmp = (char *)Malloc((unsigned)strlen(s) + 1);
  209.     strcpy(tmp, s);
  210.     return tmp;
  211. }
  212.  
  213. /*
  214.  * Set "bounds" of primitive to be the extent of the primitive.
  215.  */
  216. set_prim_bounds(obj)
  217. Object *obj;
  218. {
  219.     extern int (*objextent[])();
  220.  
  221.     (*objextent[((Primitive *)obj->data)->type])
  222.         ((Primitive *)obj->data, obj->bounds);
  223.     obj->bounds[LOW][X] -= EPSILON;
  224.     obj->bounds[HIGH][X] += EPSILON;
  225.     obj->bounds[LOW][Y] -= EPSILON;
  226.     obj->bounds[HIGH][Y] += EPSILON;
  227.     obj->bounds[LOW][Z] -= EPSILON;
  228.     obj->bounds[HIGH][Z] += EPSILON;
  229. }
  230.  
  231.