home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Raytrace & Morphing / SOS-RAYTRACE.ISO / programm / source / devel5 / stereov.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-25  |  4.7 KB  |  162 lines

  1. /* Support for steroscopic viewing */
  2.  
  3. /* Written by Dave Stampe, July 1992 */
  4.  
  5. /* Copyright 1992 by Dave Stampe and Bernie Roehl.
  6.    May be freely used to write software for release into the public domain;
  7.    all commercial endeavours MUST contact Bernie Roehl and Dave Stampe
  8.    for permission to incorporate any part of this software into their
  9.    products!
  10.  
  11.      ATTRIBUTION:  If you use any part of this source code or the libraries
  12.      in your projects, you must give attribution to REND386, Dave Stampe,
  13.      and Bernie Roehl in your documentation, source code, and at startup
  14.      of your program.  Let's keep the freeware ball rolling!
  15.  */
  16.  
  17. #include <conio.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <ctype.h>   /* toupper() */
  21. #include <dos.h>
  22.  
  23. #include "rend386.h"
  24. #include "intmath.h"
  25.  
  26. /* default screen data setup: example only */
  27.  
  28. /* STEREO default_stereo = { 1000, 250, 320, 63, 1000, 10*65536L }; */
  29.  
  30. /* static VIEW default_view = {       (EXAMPLE ONLY) */
  31. /*            0,0,-1000,       */ /* ex,ey,ez */
  32. /*            0,0,0,           */ /* pan,tilt,roll */
  33. /*            9*65536L,        */ /* zoom */
  34. /*            1000,15000,-5000, */ /* lx,ly,lz */
  35. /*            0,319,0,200,     */ /* left,right,top,bottom */
  36. /*            1,100000,        */ /* hither, yon */
  37. /*            1/1.25*65536L,   */ /* aspect ratio */
  38. /*            0,               */ /* flags */
  39. /*            0,0                 */ /* no offset */
  40. /*          };               */ /* don't init. matrix */
  41.  
  42.  
  43. struct stview {
  44.     MATRIX xform;
  45.     long zoom;
  46.     int x_offset;
  47.     long left, top, right, bottom;
  48.     int orientation;
  49. };
  50.  
  51. static struct stview sview[2];
  52.  
  53. int splitlx1=-1, splitly1, splitlx2, splitly2;
  54.  
  55. /* call once for each eye to create cached stereo data */
  56. /* as general as possible to support all devices       */
  57.  
  58. void compute_stereo_data(STEREO *stereo, int eye, int xflip, int xdist, long yr,
  59.     long left, long top, long right, long bottom )
  60. {
  61.     long x_fixup;
  62.     long world_iod;
  63.     MATRIX c;
  64.  
  65.     sview[eye].zoom = (2*65536L * stereo->phys_screen_dist) /
  66.         stereo->phys_screen_width;
  67.  
  68.     /* screen offset */
  69.     x_fixup = (stereo->phys_eye_spacing *
  70.         stereo->phys_screen_dist * stereo->pixel_width ) /
  71.         (stereo->phys_convergence * 2 *stereo->phys_screen_width);
  72.  
  73.     /* eye point */
  74.     world_iod = (stereo->phys_eye_spacing * stereo->world_scaling) >> 17;
  75.  
  76.     /* make left/right */
  77.     if (eye == 0)
  78.     {
  79.         sview[eye].x_offset = xdist + ((xflip) ? -x_fixup : x_fixup) ;
  80.         std_matrix(sview[eye].xform, 0, -yr, 0, -world_iod, 0, 0);
  81.     }
  82.     else if (eye == 1)
  83.     {
  84.         sview[eye].x_offset = xdist + ((xflip) ? x_fixup : -x_fixup) ;
  85.         std_matrix(sview[eye].xform, 0, yr, 0, world_iod, 0, 0);
  86.     }
  87.     sview[eye].left = left;
  88.     sview[eye].right = right;
  89.     sview[eye].top = top;
  90.     sview[eye].bottom = bottom;
  91.     sview[eye].orientation = xflip ? XFLIP : 0;
  92.  
  93.     if(eye==1)
  94.      {
  95.       if( abs(sview[0].top-sview[1].top)<20 &&
  96.           abs(sview[0].bottom-sview[1].bottom)<20 )
  97.        {
  98.         splitly1 = (sview[0].top<sview[1].top) ? sview[0].top : sview[1].top ;
  99.         splitly2 = (sview[0].bottom<sview[1].bottom) ? sview[0].bottom : sview[1].bottom ;
  100.         if (sview[0].left<sview[1].left)
  101.           splitlx1 = splitlx2 = (sview[0].right+sview[1].left)/2;
  102.         else
  103.           splitlx1 = splitlx2 = (sview[1].right+sview[0].left)/2;
  104.        }
  105.       else splitlx1 = -1;
  106.      }
  107. }
  108.  
  109.  
  110. void make_stereo_view(VIEW *root, VIEW *nview, int eye)
  111. {
  112.     MATRIX c;
  113.  
  114.     *nview = *root; /* copy root view */
  115.     nview->zoom = sview[eye].zoom;
  116.  
  117.     nview->x_offset +=
  118.         sview[eye].orientation ? -sview[eye].x_offset : -sview[eye].x_offset ;
  119.  
  120.     view_to_matrix(nview,c);
  121.     matrix_product(c, sview[eye].xform, c);
  122.     matrix_view_factors(nview,c);
  123.  
  124.     nview->left = sview[eye].left;
  125.     nview->right = sview[eye].right;
  126.     nview->top = sview[eye].top;
  127.     nview->bottom = sview[eye].bottom;
  128.  
  129.     nview->orientation = sview[eye].orientation ^ nview->orientation;
  130.  
  131.     initialize_screen_factors(nview);
  132. }
  133.  
  134.  
  135. /* sufficient if only view point has changed and no twist or offset */
  136. void update_stereo_view(VIEW *v, STEREO *s, int eye)
  137. {
  138.     long world_iod = (s->phys_eye_spacing * s->world_scaling) >> 17;
  139.  
  140.     if (eye == LEFT_EYE)
  141.     {
  142.         v->eye_xform[3][0] -= m_mult(world_iod,v->eye_xform[0][0]);
  143.         v->eye_xform[3][1] -= m_mult(world_iod,v->eye_xform[1][0]);
  144.         v->eye_xform[3][2] -= m_mult(world_iod,v->eye_xform[2][0]);
  145.     }
  146.     else if (eye == RIGHT_EYE)
  147.     {
  148.         v->eye_xform[3][0] += m_mult(world_iod,v->eye_xform[0][0]);
  149.         v->eye_xform[3][1] += m_mult(world_iod,v->eye_xform[1][0]);
  150.         v->eye_xform[3][2] += m_mult(world_iod,v->eye_xform[2][0]);
  151.     }
  152. }
  153.  
  154. /* makes a standard SWITCHED stereo screen */
  155.  
  156. void default_stereo_setup(VIEW *v, STEREO *s)
  157. {
  158.     compute_stereo_data(s, 0, 0, 0, 0*65536L, v->left, v->top, v->right, v->bottom);
  159.     compute_stereo_data(s, 1, 0, 0, 0*65536L, v->left, v->top, v->right, v->bottom);
  160. }
  161.  
  162.