home *** CD-ROM | disk | FTP | other *** search
/ Virtual Reality Homebrewer's Handbook / vr.iso / vr386 / headtrak.c < prev    next >
C/C++ Source or Header  |  1996-03-19  |  4KB  |  123 lines

  1. /* Handles head tracker init, use */
  2.  
  3. /* Written by Dave Stampe, 4/1/94 */
  4.  
  5. // Rewritten, parieal VR-386 port by Dave Stampe, 9/1/94
  6.  
  7.  
  8. /*
  9.  This code is part of the VR-386 project, created by Dave Stampe.
  10.  VR-386 is a desendent of REND386, created by Dave Stampe and
  11.  Bernie Roehl.  Almost all the code has been rewritten by Dave
  12.  Stampre for VR-386.
  13.  
  14.  Copyright (c) 1994 by Dave Stampe:
  15.  May be freely used to write software for release into the public domain
  16.  or for educational use; all commercial endeavours MUST contact Dave Stampe
  17.  (dstampe@psych.toronto.edu) for permission to incorporate any part of
  18.  this software or source code into their products!  Usually there is no
  19.  charge for under 50-100 items for low-cost or shareware products, and terms
  20.  are reasonable.  Any royalties are used for development, so equipment is
  21.  often acceptable payment.
  22.  
  23.  ATTRIBUTION:  If you use any part of this source code or the libraries
  24.  in your projects, you must give attribution to VR-386 and Dave Stampe,
  25.  and any other authors in your documentation, source code, and at startup
  26.  of your program.  Let's keep the freeware ball rolling!
  27.  
  28.  DEVELOPMENT: VR-386 is a effort to develop the process started by
  29.  REND386, improving programmer access by rewriting the code and supplying
  30.  a standard API.  If you write improvements, add new functions rather
  31.  than rewriting current functions.  This will make it possible to
  32.  include you improved code in the next API release.  YOU can help advance
  33.  VR-386.  Comments on the API are welcome.
  34.  
  35.  CONTACT: dstampe@psych.toronto.edu
  36. */
  37.  
  38.  
  39. #include <stdio.h>
  40. #include <stdlib.h>    /* atexit() */
  41. #include <dos.h>
  42.  
  43. #include "pointer.h"
  44. #include "vr_api.h"
  45. #include "intmath.h"
  46. #include "splits.h"
  47. #include "pcdevice.h"
  48. #include "segment.h"
  49.  
  50.  
  51. static PDRIVER *head_device;
  52.  
  53. static MATRIX h_recenter, ht_offset, ht_ioffset;
  54.  
  55. void head_tracker_process(BOOL recenter) /* repositions head based on tracker data */
  56. {
  57.   POINTER hp;
  58.   MATRIX m;
  59.   int c;
  60.  
  61.   if (!head_device) return;
  62.   c = pointer_read(head_device, &hp);         /* read tracker */
  63.  
  64. //  if (recenter) /* use current pos'n as zero */  NOT YET FIXED
  65. //    {
  66. //      std_matrix(m, hp.rx, hp.ry, hp.rz, hp.x, hp.y, hp.z);
  67. //      inverse_matrix(m, h_recenter);
  68. //      matrix_product(ht_ioffset, h_recenter, h_recenter);
  69. //      c = PNEW_POS;
  70. //    }
  71.  
  72.   if (c & (PNEW_POS | PNEW_ROT)) /* compute new hpos */
  73.     {
  74.       multi_matrix(m, hp.rx, hp.ry, hp.rz, hp.x, hp.y, hp.z, RYXZ); // ht to maatrix
  75. //      matrix_product(h_recenter, m, m);
  76.       matrix_product(m, ht_offset, m);            // translate to head coords
  77.       abs_mat_segment(object2segment(head_seg), m);    // load up segment
  78.       position_changed++;                               // redraw needed
  79.     }                               // update occurs with redraw
  80.   return;
  81. }
  82.  
  83.  
  84. void htrack_quit()
  85. {
  86.   pointer_quit(head_device);
  87. }
  88.  
  89.  
  90. PDRIVER *init_head_device(char *dfname, POSE *hdo)
  91. {
  92.   MATRIX m;
  93.   PDRIVER *p;
  94.   POINTER pt;
  95.  
  96.   p = pointer_init(P_IS6H, dfname); /* setup glove device TEST */
  97.   if (p == NULL) return NULL;
  98.  
  99.   init_pointer(&pt);         /* so that defaults are OK */
  100.  
  101.              /* use real-world scaling output*/
  102.   pointer_abscale(p, 65536L, 65536L, 65536L, 65536L, 65536L, 65536L);
  103.  
  104.   pointer_read(p, &pt);
  105.   pointer_read(p, &pt); /* save current position */
  106.  
  107.   head_device = p;
  108.   atexit(htrack_quit);
  109.  
  110.   identity_matrix(h_recenter);        // no recenter
  111.  
  112.   std_matrix(m,hdo->rx,hdo->ry,hdo->rz,hdo->x,hdo->y,hdo->z);   // tracker->head xform
  113.  
  114.   matrix_copy(m, ht_offset);
  115. //  matrix_copy(m, h_recenter);
  116.  
  117.   inverse_matrix(m, ht_ioffset);
  118.   inverse_matrix(m, h_recenter);
  119.   head_tracker_process(1);        // finish by setting up system
  120.   return p;
  121. }
  122.  
  123.