home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / Peter's Final Project / src / object.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-10  |  9.6 KB  |  384 lines  |  [TEXT/KAHL]

  1. /*
  2.  *  Peter's Final Project -- A texture mapping demonstration
  3.  *  © 1995, Peter Mattis
  4.  *
  5.  *  E-mail:
  6.  *  petm@soda.csua.berkeley.edu
  7.  *
  8.  *  Snail-mail:
  9.  *   Peter Mattis
  10.  *   557 Fort Laramie Dr.
  11.  *   Sunnyvale, CA 94087
  12.  *
  13.  *  Avaible from:
  14.  *  http://www.csua.berkeley.edu/~petm/final.html
  15.  *
  16.  *  This program is free software; you can redistribute it and/or modify
  17.  *  it under the terms of the GNU General Public License as published by
  18.  *  the Free Software Foundation; either version 2 of the License, or
  19.  *  (at your option) any later version.
  20.  *
  21.  *  This program is distributed in the hope that it will be useful,
  22.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  23.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24.  *  GNU General Public License for more details.
  25.  *
  26.  *  You should have received a copy of the GNU General Public License
  27.  *  along with this program; if not, write to the Free Software
  28.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  29.  */
  30.  
  31. #include <assert.h>
  32. #include "face.h"
  33. #include "matrix.vector.h"
  34. #include "object.h"
  35. #include "point.h"
  36. #include "sys.stuff.h"
  37. #include "texture.h"
  38.  
  39. /*
  40.  * Keep a list of free objects for fast allocation.
  41.  * Keep track of the memory used by objects.
  42.  */
  43.  
  44. static OBJECTS free_objects_list = NULL;
  45. static long object_mem = 0;
  46.  
  47. /*
  48.  * Make an object and initialize its values.
  49.  */
  50.  
  51. OBJECT
  52. make_object ()
  53. {
  54.     OBJECTS objects;
  55.     OBJECT object;
  56.  
  57.     if (free_objects_list)
  58.     {
  59.         objects = free_objects_list;
  60.         object = objects_first (objects);
  61.         free_objects_list = objects_rest (free_objects_list);
  62.         free_list (objects);
  63.     }
  64.     else
  65.     {
  66.         object_mem += sizeof (_OBJECT);
  67.  
  68.         object = (OBJECT) ALLOC (sizeof (_OBJECT));
  69.     }
  70.  
  71.     set_object_id (object, 0);
  72.     set_object_val (object, 0);
  73.  
  74.     set_object_radius (object, 0);
  75.  
  76.     set_object_pos_x (object, 0);
  77.     set_object_pos_y (object, 0);
  78.     set_object_pos_z (object, 0);
  79.     set_object_pos_w (object, NUM_ONE);
  80.  
  81.     set_object_dir_x (object, 0);
  82.     set_object_dir_y (object, 0);
  83.     set_object_dir_z (object, 0);
  84.     set_object_dir_w (object, 0);
  85.  
  86.     set_object_pos_vel (object, 0);
  87.     set_object_dir_vel (object, 0);
  88.     set_object_pos_drag (object, my_float_to_num (1.9));
  89.     set_object_dir_drag (object, my_float_to_num (1.9));
  90.  
  91.     set_object_camera (object, make_camera ());
  92.     set_object_move (object, NULL);
  93.     set_object_data (object, NULL);
  94.  
  95.     set_object_faces (object, NULL);
  96.     object_init (object);
  97.  
  98.     set_object_red (object, NUM_ZERO);
  99.     set_object_green (object, NUM_ZERO);
  100.     set_object_blue (object, NUM_ONE);
  101.  
  102.     set_object_sector (object, NULL);
  103.     set_object_sectors (object, NULL);
  104.  
  105.     return object;
  106. }
  107.  
  108. /*
  109.  * Free an object by placing it on the free list.
  110.  */
  111.  
  112. void
  113. free_object (o)
  114.     OBJECT o;
  115. {
  116.     LIST temp;
  117.  
  118.     temp = make_list ();
  119.     set_list_datum (temp, o);
  120.     set_list_next (temp, free_objects_list);
  121.     free_objects_list = temp;
  122.  
  123.     free_camera (object_camera (o));
  124. }
  125.  
  126. /*
  127.  * Initialize an objects faces and matrix.
  128.  * Currently, objects are cubes.
  129.  */
  130.  
  131. void
  132. object_init (o)
  133.     OBJECT o;
  134. {
  135.     POINT pts;
  136.     POINT norms;
  137.     FACE face;
  138.     short i;
  139.  
  140.     pts = (POINT) ALLOC (sizeof (_POINT) * 8);
  141.     assert (pts != NULL);
  142.  
  143.     norms = (POINT) ALLOC (sizeof (_POINT) * 6);
  144.     assert (norms != NULL);
  145.  
  146.     set_point_x (&pts[0], NUM_ONE);
  147.     set_point_y (&pts[0], NUM_ONE);
  148.     set_point_z (&pts[0], NUM_ONE);
  149.     set_point_w (&pts[0], NUM_ONE);
  150.  
  151.     set_point_x (&pts[1], NUM_ONE);
  152.     set_point_y (&pts[1], -NUM_ONE);
  153.     set_point_z (&pts[1], NUM_ONE);
  154.     set_point_w (&pts[1], NUM_ONE);
  155.  
  156.     set_point_x (&pts[2], -NUM_ONE);
  157.     set_point_y (&pts[2], -NUM_ONE);
  158.     set_point_z (&pts[2], NUM_ONE);
  159.     set_point_w (&pts[2], NUM_ONE);
  160.  
  161.     set_point_x (&pts[3], -NUM_ONE);
  162.     set_point_y (&pts[3], NUM_ONE);
  163.     set_point_z (&pts[3], NUM_ONE);
  164.     set_point_w (&pts[3], NUM_ONE);
  165.  
  166.     set_point_x (&pts[4], NUM_ONE);
  167.     set_point_y (&pts[4], NUM_ONE);
  168.     set_point_z (&pts[4], -NUM_ONE);
  169.     set_point_w (&pts[4], NUM_ONE);
  170.  
  171.     set_point_x (&pts[5], NUM_ONE);
  172.     set_point_y (&pts[5], -NUM_ONE);
  173.     set_point_z (&pts[5], -NUM_ONE);
  174.     set_point_w (&pts[5], NUM_ONE);
  175.  
  176.     set_point_x (&pts[6], -NUM_ONE);
  177.     set_point_y (&pts[6], -NUM_ONE);
  178.     set_point_z (&pts[6], -NUM_ONE);
  179.     set_point_w (&pts[6], NUM_ONE);
  180.  
  181.     set_point_x (&pts[7], -NUM_ONE);
  182.     set_point_y (&pts[7], NUM_ONE);
  183.     set_point_z (&pts[7], -NUM_ONE);
  184.     set_point_w (&pts[7], NUM_ONE);
  185.  
  186.     set_point_x (&norms[0], NUM_ZERO);
  187.     set_point_y (&norms[0], NUM_ZERO);
  188.     set_point_z (&norms[0], NUM_ONE);
  189.     set_point_w (&norms[0], NUM_ZERO);
  190.  
  191.     set_point_x (&norms[1], NUM_ZERO);
  192.     set_point_y (&norms[1], NUM_ZERO);
  193.     set_point_z (&norms[1], -NUM_ONE);
  194.     set_point_w (&norms[1], NUM_ZERO);
  195.  
  196.     set_point_x (&norms[2], NUM_ONE);
  197.     set_point_y (&norms[2], NUM_ZERO);
  198.     set_point_z (&norms[2], NUM_ZERO);
  199.     set_point_w (&norms[2], NUM_ZERO);
  200.  
  201.     set_point_x (&norms[3], -NUM_ONE);
  202.     set_point_y (&norms[3], NUM_ZERO);
  203.     set_point_z (&norms[3], NUM_ZERO);
  204.     set_point_w (&norms[3], NUM_ZERO);
  205.  
  206.     set_point_x (&norms[4], NUM_ZERO);
  207.     set_point_y (&norms[4], NUM_ONE);
  208.     set_point_z (&norms[4], NUM_ZERO);
  209.     set_point_w (&norms[4], NUM_ZERO);
  210.  
  211.     set_point_x (&norms[5], NUM_ZERO);
  212.     set_point_y (&norms[5], -NUM_ONE);
  213.     set_point_z (&norms[5], NUM_ZERO);
  214.     set_point_w (&norms[5], NUM_ZERO);
  215.  
  216.     face = make_face ();
  217.     set_face_normal (face, &norms[0]);
  218.     face_points (face) = points_add_point (face_points (face), &pts[0]);
  219.     face_points (face) = points_add_point (face_points (face), &pts[1]);
  220.     face_points (face) = points_add_point (face_points (face), &pts[2]);
  221.     face_points (face) = points_add_point (face_points (face), &pts[3]);
  222.     object_faces (o) = faces_add_face (object_faces (o), face);
  223.  
  224.     face = make_face ();
  225.     set_face_normal (face, &norms[1]);
  226.     face_points (face) = points_add_point (face_points (face), &pts[4]);
  227.     face_points (face) = points_add_point (face_points (face), &pts[5]);
  228.     face_points (face) = points_add_point (face_points (face), &pts[6]);
  229.     face_points (face) = points_add_point (face_points (face), &pts[7]);
  230.     object_faces (o) = faces_add_face (object_faces (o), face);
  231.  
  232.     face = make_face ();
  233.     set_face_normal (face, &norms[2]);
  234.     face_points (face) = points_add_point (face_points (face), &pts[0]);
  235.     face_points (face) = points_add_point (face_points (face), &pts[1]);
  236.     face_points (face) = points_add_point (face_points (face), &pts[5]);
  237.     face_points (face) = points_add_point (face_points (face), &pts[4]);
  238.     object_faces (o) = faces_add_face (object_faces (o), face);
  239.  
  240.     face = make_face ();
  241.     set_face_normal (face, &norms[3]);
  242.     face_points (face) = points_add_point (face_points (face), &pts[7]);
  243.     face_points (face) = points_add_point (face_points (face), &pts[6]);
  244.     face_points (face) = points_add_point (face_points (face), &pts[2]);
  245.     face_points (face) = points_add_point (face_points (face), &pts[3]);
  246.     object_faces (o) = faces_add_face (object_faces (o), face);
  247.  
  248.     face = make_face ();
  249.     set_face_normal (face, &norms[4]);
  250.     face_points (face) = points_add_point (face_points (face), &pts[7]);
  251.     face_points (face) = points_add_point (face_points (face), &pts[3]);
  252.     face_points (face) = points_add_point (face_points (face), &pts[0]);
  253.     face_points (face) = points_add_point (face_points (face), &pts[4]);
  254.     object_faces (o) = faces_add_face (object_faces (o), face);
  255.  
  256.     face = make_face ();
  257.     set_face_normal (face, &norms[5]);
  258.     face_points (face) = points_add_point (face_points (face), &pts[6]);
  259.     face_points (face) = points_add_point (face_points (face), &pts[2]);
  260.     face_points (face) = points_add_point (face_points (face), &pts[1]);
  261.     face_points (face) = points_add_point (face_points (face), &pts[5]);
  262.     object_faces (o) = faces_add_face (object_faces (o), face);
  263.  
  264.     {
  265.         MATRIX m1, m2, m3, m4;
  266.  
  267.         matrix_scale (m1, my_float_to_num (0.25), 
  268.         my_float_to_num (0.25), 
  269.         my_float_to_num (0.25));
  270.         matrix_rotate_x (m2, my_float_to_num (0.7854));
  271.         matrix_rotate_y (m3, my_float_to_num (0.7854));
  272.  
  273.         matrix_compose (m2, m3, m4);
  274.         matrix_compose (m1, m4, object_matrix (o));
  275.     }
  276. }
  277.  
  278. /*
  279.  * Have the object update its camera.
  280.  */
  281.  
  282. void
  283. object_update_camera (o)
  284.     OBJECT o;
  285. {
  286.     if (o && object_camera (o))
  287.     {
  288.         set_camera_pos_x (object_camera (o), object_pos_x (o));
  289.         set_camera_pos_y (object_camera (o), object_pos_y (o));
  290.         set_camera_pos_z (object_camera (o), object_pos_z (o));
  291.         set_camera_pos_w (object_camera (o), NUM_ONE);
  292.  
  293.         set_camera_vpn_x (object_camera (o), -object_dir_x (o));
  294.         set_camera_vpn_y (object_camera (o), -object_dir_y (o));
  295.         set_camera_vpn_z (object_camera (o), -object_dir_z (o));
  296.         set_camera_vpn_w (object_camera (o), NUM_ZERO);
  297.  
  298.         set_camera_vup_x (object_camera (o), NUM_ZERO);
  299.         set_camera_vup_y (object_camera (o), NUM_ONE);
  300.         set_camera_vup_z (object_camera (o), NUM_ZERO);
  301.         set_camera_vup_w (object_camera (o), NUM_ZERO);
  302.  
  303.         set_camera_u_min (object_camera (o), NUM_ZERO);
  304.         set_camera_v_min (object_camera (o), NUM_ZERO);
  305.         set_camera_u_max (object_camera (o), my_int_to_num (get_frame_buffer_width ()));
  306.         set_camera_v_max (object_camera (o), my_int_to_num (get_frame_buffer_height ()));
  307.  
  308.         camera_determine_matrices (object_camera (o));
  309.     }
  310. }
  311.  
  312. /*
  313.  * Append an object to a list of objects.
  314.  */
  315.  
  316. OBJECTS
  317. objects_append_object (set, o)
  318.     OBJECTS set;
  319.     OBJECT o;
  320. {
  321.     LIST n;
  322.  
  323.     n = make_list ();
  324.     set_list_datum (n, o);
  325.  
  326.     return ((OBJECTS) list_append_list ((LIST) set, n));
  327. }
  328.  
  329. /*
  330.  * Prepend an object to a list of objects.
  331.  */
  332.  
  333. OBJECTS
  334. objects_prepend_object (set, o)
  335.     OBJECTS set;
  336.     OBJECT o;
  337. {
  338.     LIST n;
  339.  
  340.     n = make_list ();
  341.     set_list_datum (n, o);
  342.  
  343.     return ((OBJECTS) list_prepend_list ((LIST) set, n));
  344. }
  345.  
  346. /*
  347.  * Remove an object from a list of objects.
  348.  */
  349.  
  350. OBJECTS
  351. objects_remove_object (set, o)
  352.     OBJECTS set;
  353.     OBJECT o;
  354. {
  355.     return ((OBJECTS) list_remove_list ((LIST) set, (void *) o));
  356. }
  357.  
  358. /*
  359.  * Free a list of objects.
  360.  */
  361.  
  362. void
  363. free_objects (set)
  364.     OBJECTS set;
  365. {
  366.     if (set == NULL)
  367.         return;
  368.  
  369.     free_objects (objects_rest (set));
  370.  
  371.     set_objects_rest (set, free_objects_list);
  372.     free_objects_list = set;
  373. }
  374.  
  375. /*
  376.  * Return the object memory usage.
  377.  */
  378.  
  379. long
  380. object_mem_usage ()
  381. {
  382.     return object_mem;
  383. }
  384.