home *** CD-ROM | disk | FTP | other *** search
/ Virtual Reality Zone / VRZONE.ISO / mac / PC / REND386 / DEVEL5 / CURSORS.C < prev    next >
C/C++ Source or Header  |  1992-09-29  |  5KB  |  201 lines

  1. /* 2D and 3D cursor handling  */
  2. /* some manipulation routines */
  3.  
  4. /* Written by Dave Stampe, Aug. 1992 */
  5.  
  6. /* Copyright 1992 by Bernie Roehl.
  7.    May be freely used to write software for release into the public domain;
  8.    all commercial endeavours MUST contact Bernie Roehl and Dave Stampe
  9.    for permission to incorporate any part of this software into their
  10.    products!
  11.  */
  12.  
  13. #include <stdio.h>
  14. #include <dos.h>
  15.  
  16. #include "../include/rend386.h"
  17. #include "../include/f3dkitd.h"
  18. #include "../include/intmath.h"
  19. #include "../include/pointer.h"
  20. #include "../include/cursor.h"
  21. #include "../include/segio.h"
  22. #include "../include/plg.h"
  23.  
  24. extern int redraw;
  25. extern SEGMENT *cursor_seg;
  26.  
  27. /***************** 2D (screen) cursor handling ************/
  28.  
  29. static int oldx = 160, oldy = 100;
  30. static int cursor_flag = -1;
  31. static int lastpage;
  32.  
  33. void cursor_move(int x, int y) /* move cursor if visible */
  34. {
  35.     oldx = x; 
  36.     oldy = y;
  37.     if (cursor_flag == 0)
  38.     {
  39.         set_drawpage(lastpage);
  40.         erase_cursor(lastpage);
  41.         draw_cursor(x,y,0,lastpage);
  42.     }
  43. }
  44.  
  45. int cursor_hide() /* erase cursor */
  46. {
  47.     if (cursor_flag >= 0) erase_cursor(lastpage);
  48.     --cursor_flag;
  49.     return lastpage;
  50. }
  51.  
  52. int cursor_forget() /* will be erased by redraw: ignore it! */
  53. {
  54.     --cursor_flag;
  55.     return lastpage;
  56. }
  57.  
  58. void cursor_show(int page) /* redisplay cursor */
  59. {
  60.     if (++cursor_flag == 0)
  61.     {
  62.         set_drawpage(page);
  63.         lastpage = page;
  64.         draw_cursor(oldx, oldy, 0, page);
  65.     }
  66. }
  67.  
  68.  
  69. int move_2D(PDRIVER *d, int *x, int *y, unsigned *b)
  70. {
  71.     int c;
  72.  
  73.     if (d == NULL) return 0;
  74.     if (PNEW_POS & (c = mouse_read(d, x, y, b)))
  75.         cursor_move(*x, *y);
  76.     return c;
  77. }
  78.  
  79.  
  80. int move_till_click(PDRIVER *d, unsigned b, int *x, int *y) /* b is button mask */
  81. {
  82.     int s;
  83.     unsigned c;
  84.  
  85.     if (d == NULL) return -1;
  86.     while(1)
  87.     {
  88.         if (((s = move_2D(d, x, y, &c)) & PNEW_BUT) && (c & b)) break;
  89.     }
  90.     return s;
  91. }
  92.  
  93.  
  94. /* find world coordinates of pointer based on given view */
  95.  
  96. void pointer_to_world(POINTER *p, VIEW *v, long *x, long *y, long *z)
  97. {
  98.     MATRIX n;
  99.     *x = p->x;
  100.     *y = p->y;
  101.     *z = p->z;
  102.     view_to_matrix(v,n);
  103.     matrix_point(n,x,y,z);
  104.     p->wpos[3][0] = *x;
  105.     p->wpos[3][1] = *y;
  106.     p->wpos[3][2] = *z;
  107.     p->wpos_valid = 1;
  108. }
  109.  
  110.  
  111. /* rotate data to view alignment */
  112.  
  113. void rotate_to_view( VIEW *v, long *x, long *y, long *z)
  114. {
  115.     MATRIX n;
  116.     view_to_matrix(v,n);
  117.     matrix_rotate(n,x,y,z);
  118. }
  119.  
  120.  
  121. /***************** GLOVE CURSOR SUPPORT *************/
  122.  
  123. static SEGMENT *wrist_seg; /* wrist-on-body segment */
  124. /* also used to manip. objects */
  125.  
  126. static SEGMENT *glove_seg; /* wrist twist for presentation */
  127. static SEGMENT *glove_joints[20]; /* joints on hand */
  128.  
  129. #define Y_GLV_OFFSET  0      /* needed as "body" is at neck level */
  130. #define GLOVE_DIST    1000
  131.  
  132. #define SELECT_LENGTH 20    /* distance along last index finger segment */
  133. /* to selection point */
  134.  
  135. int glove_update(PDRIVER *d, POINTER *p) /* read glove, update positions */
  136. {
  137.     int c;
  138.  
  139.     c = pointer_read(d, p);
  140.  
  141.     if ((c & (PNEW_POS | PNEW_ROT | PNEW_FLEX)) == 0) return 0;
  142.  
  143.     abs_move_segment(wrist_seg, p->x, p->y + Y_GLV_OFFSET, p->z + GLOVE_DIST);
  144.     if (d->pdata->type&P_IS6DG)
  145.         abs_rot_segment(wrist_seg, p->rx, p->ry, p->rz, RYXZ);
  146.     else
  147.         abs_rot_segment(glove_seg, -2000L*p->y, p->ry, -4000L*p->x, RYXZ);
  148.  
  149.     abs_rot_segment(glove_joints[1], 0L,30*65536L+18061L*p->flex[0],0L, RYXZ);
  150.     abs_rot_segment(glove_joints[2], -20*65536L,90*65536L,-5*65536L+38700L*p->flex[1], RYZX);
  151.     abs_rot_segment(glove_joints[3], 38700*p->flex[2],0L,0L, RYXZ);
  152.     abs_rot_segment(glove_joints[4], 38700*p->flex[3],0L,0L, RYXZ);
  153.     abs_rot_segment(glove_joints[5], 38700*p->flex[4],0L,0L, RYXZ);
  154.     abs_rot_segment(glove_joints[6], 38700*p->flex[5],0L,0L, RYXZ);
  155.     abs_rot_segment(glove_joints[7], 38700*p->flex[6],0L,0L, RYXZ);
  156.     abs_rot_segment(glove_joints[8], 38700*p->flex[7],0L,0L, RYXZ);
  157.     abs_rot_segment(glove_joints[9], 38700*p->flex[8],0L,0L, RYXZ);
  158.     abs_rot_segment(glove_joints[10],38700*p->flex[9],0L,0L, RYXZ);
  159.  
  160.     redraw = 1;
  161.     return 1;
  162. }
  163.  
  164. int cursor_update3D(PDRIVER *d, POINTER *p) /* read pointer, update positions */
  165. {
  166.     int c;
  167.  
  168.     c = pointer_read(d, p);
  169.  
  170.     if ((c & (PNEW_POS | PNEW_ROT)) == 0) return 0;
  171.  
  172.     abs_move_segment(cursor_seg, p->x, p->y + Y_GLV_OFFSET, p->z + GLOVE_DIST);
  173.     abs_rot_segment(cursor_seg, p->rx, p->ry, p->rz, RYXZ);
  174.  
  175.     redraw = 1;
  176.     return 1;
  177. }
  178.  
  179.  
  180. /************* USED FOR 3D/6D MANIPULATION *************/
  181.  
  182. extern int have_glove, use_glove, have_ptr;
  183.  
  184. SEGMENT *manip_data(PDRIVER *d, long *x, long *y, long *z)
  185. {
  186.  
  187.     if (!have_ptr)
  188.     {
  189.         *x = 0;
  190.         *y = 0;
  191.         *z = SELECT_LENGTH;
  192.         matrix_point(*get_seg_pmatrix(glove_joints[4]), x, y, z);
  193.         return wrist_seg;
  194.     }
  195.     else
  196.         {
  197.         seg_getposxyz(cursor_seg, x ,y ,z);
  198.         return cursor_seg;
  199.     }
  200. }
  201.