home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_12 / 9n12048a < prev    next >
Text File  |  1991-10-14  |  2KB  |  60 lines

  1. #include "solid.h"
  2.  
  3. struct solid_obj *make_object(int this_obj_type)
  4. /*  Constructs descriptor for instance of a specified
  5.     object type.  Initializes scaling factor,
  6.     translation and rotation.  */
  7. {
  8.     struct solid_obj *this_obj_ptr, *prev_obj_ptr,
  9.         *next_obj_ptr;
  10.     struct vertex *vertex_ptr1, *vertex_ptr2;
  11.     int coord_index, vertex_index;
  12.  
  13.     if (defn_ptr[this_obj_type] == (struct obj_defn *)
  14.         NULL) /* must construct defininition */
  15.         define_solid(this_obj_type);
  16.     if ((this_obj_ptr = (struct solid_obj *)malloc
  17.         (sizeof(struct solid_obj) + (defn_ptr
  18.         [this_obj_type]->facet_count) * sizeof(BOOL)))
  19.         == NULL) /* construct object descriptor */
  20.         quit(ERR_MEMORY, __FILE__, __LINE__);
  21.     this_obj_ptr->obj_type = this_obj_type;
  22.     this_obj_ptr->scale = 1.0;
  23.     for (coord_index = 0; coord_index < 3;
  24.         ++coord_index) {
  25.         this_obj_ptr->xlate[coord_index] = 0.0;
  26.         this_obj_ptr->rotate[coord_index] = 0.0;
  27.     }
  28.     if ((this_obj_ptr->vertex_first =
  29.         (struct vertex *)malloc(defn_ptr[this_obj_type]
  30.         ->vertex_count * sizeof(struct vertex))) ==
  31.         NULL) /* construct vertex descriptors */
  32.         quit(ERR_MEMORY, __FILE__, __LINE__);
  33.  
  34.     /* copy vertices from definition to instance */
  35.     for (vertex_ptr1 = this_obj_ptr->vertex_first,
  36.         vertex_ptr2 = defn_ptr[this_obj_type]->
  37.         vertex_first, vertex_index = 0; vertex_index <
  38.         defn_ptr[this_obj_type]->vertex_count;
  39.         ++vertex_index, ++vertex_ptr1, ++vertex_ptr2)
  40.         for (coord_index = 0; coord_index < 3;
  41.             ++coord_index)
  42.             vertex_ptr1->coord[coord_index] =
  43.                 vertex_ptr2->coord[coord_index];
  44.             this_obj_ptr->obj_next =
  45.                 (struct solid_obj *)NULL; /* pointer
  46.                 to next object */
  47.     if (obj_first == (struct solid_obj *)NULL) /* this
  48.         is first object */
  49.         obj_first = this_obj_ptr;
  50.     else {
  51.         for (prev_obj_ptr = next_obj_ptr = obj_first;
  52.             next_obj_ptr != (struct solid_obj *)NULL;
  53.             prev_obj_ptr = next_obj_ptr, next_obj_ptr
  54.             = next_obj_ptr->obj_next); /* find previous
  55.             last object */
  56.         prev_obj_ptr->obj_next = this_obj_ptr;
  57.     }
  58.     return(this_obj_ptr);
  59. }
  60.