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

  1. /* 3D cursor handling  */
  2. /* some manipulation routines */
  3. // Rewritten for VR-386 by Dave Stampe 9/1/94
  4.  
  5. /* Original written by Dave Stampe, Aug. 1992 */
  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. #include <stdio.h>
  39. #include <dos.h>
  40.  
  41. #include "vrconst.h"
  42. #include "pointer.h"
  43. #include "vr_api.h"
  44. #include "intmath.h"
  45. #include "segment.h"
  46.  
  47.  
  48.  
  49. /************* 3D/6D CURSOR SUPPORT ***********/
  50.  
  51.     // sets up the 3D/6D manip system
  52. void set_3D_manip_data(PDRIVER *ptr, SEGMENT *move, SEGMENT *sel, POSE *seloffset);
  53.  
  54. static SEGMENT *cursor_seg;
  55.  
  56. static int load_3D_cursor(PDRIVER *gd, char *cursor_fname)
  57. {
  58.   int i;
  59.   FILE *in;
  60.   OBJECT *obj;
  61.   POSE p = ZERO_POSE;
  62.  
  63.   if ((in = fopen(cursor_fname, "r")) == NULL)
  64.     {
  65.       errprintf("Cannot read 3D cursor object %s",cursor_fname);
  66.       return -1;
  67.     }
  68.   cursor_seg = create_invisible_object();
  69.   attach_object(cursor_seg, body_seg, 0);
  70.   obj = load_plg_object(in, &p, 1, 1, 1, 0);
  71.   if(!obj)
  72.     {
  73.       errprintf("Error in loading 3D cursor object %s",cursor_fname);
  74.       return -2;
  75.     }
  76.   make_fixed_object_moveable(obj, cursor_seg);
  77.   update_object(cursor_seg);
  78.   add_object_to_world(obj);
  79.   fclose(in);
  80.  
  81.   make_object_unselectable(obj);
  82.  
  83.   set_3D_manip_data(gd, cursor_seg, cursor_seg, &p);
  84.  
  85. }
  86.  
  87.  
  88.  
  89. int cursor_update_3D(PDRIVER *d, POINTER *p) /* read pointer, update positions */
  90. {
  91.     int c;
  92.  
  93.     c = pointer_read(d, p);
  94.  
  95.     if ((c & (PNEW_POS | PNEW_ROT)) == 0) return 0;
  96.  
  97.     abs_move_segment(cursor_seg, p->x, p->y + Y_PTR_OFFSET, p->z + PTR_DIST);
  98.     abs_rot_segment(cursor_seg, p->rx, p->ry, p->rz, RYXZ);
  99.  
  100.     world_changed++;
  101.     return 1;
  102. }
  103.  
  104. static PDRIVER *ptr3d;
  105.  
  106. static void ptr3d_quit()
  107. {
  108.    pointer_quit(ptr3d);
  109. }
  110.  
  111.  
  112. PDRIVER *pointer_initialize(char *ptrdrv, char *ptrobj, POSE *s)
  113. {
  114.   PDRIVER *p;
  115.   POINTER x;
  116.  
  117.   p = pointer_init(P_IS3DG | P_IS6DG, ptrdrv); /* setup glove device */
  118.   if (p == NULL) return NULL;
  119.  
  120.   init_pointer(&x); /* so that defaults are OK */
  121.     /* use abs. glove motion */
  122.   pointer_abscale(p, s->x, s->y, s->z, s->rx, s->ry, s->rz);
  123.   set_mouse_limits(p, screeninfo->xmax, screeninfo->ymax);
  124.   pointer_read(p, &x);
  125.   pointer_read(p, &x); /* save current position */
  126.   mouse_read(p, NULL, NULL, NULL);
  127.  
  128.   ptr3d = p;
  129.   atexit(ptr3d_quit);
  130.  
  131.   load_3D_cursor(p, ptrobj);
  132.  
  133.   return p;
  134. }
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.     // read, handle 3D/6D pointer commands
  142. void pointer_process()
  143. {
  144.   POINTER gp;
  145.   int g;
  146.  
  147.   cursor_update_3D(ptr3d, &gp);
  148.   switch(gp.buttons)
  149.     {
  150.       case 2 :
  151.     g = GRASP_DO;
  152.     break;
  153.       case 3:
  154.     g = ROTATE_DO;
  155.     break;
  156.       case 1:
  157.     g = SELECT_DO;
  158.     break;
  159.       default:
  160.     g = FREE_DO;
  161.     break;
  162.    }
  163.  do_3D_manip(g);
  164. }
  165.