home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / demos / gpc / maptools.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-12  |  12.7 KB  |  389 lines

  1. /* $XConsortium: maptools.c,v 5.2 91/07/12 18:05:40 hersh Exp $ */
  2. /***********************************************************
  3. Copyright(c) 1989,1990, 1991 by Sun Microsystems, Inc. and the X Consortium at M.I.T.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Sun Microsystems,
  12. the X Consortium, and MIT not be used in advertising or publicity
  13. pertaining to distribution of the software without specific, written
  14. prior permission.
  15.  
  16. SUN MICROSYSTEMS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  17. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT
  18. SHALL SUN MICROSYSTEMS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  19. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  21. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  22. SOFTWARE.
  23.  
  24. ******************************************************************/
  25.  
  26. /*--------------------------------------------------------------------*\
  27. |
  28. |  Copyright (C) 1989,1990, 1991, National Computer Graphics Association
  29. |
  30. |  Permission is granted to any individual or institution to use, copy, or
  31. |  redistribute this software so long as it is not sold for profit, provided
  32. |  this copyright notice is retained.
  33. |
  34. |                         Developed for the
  35. |                National Computer Graphics Association
  36. |                         2722 Merrilee Drive
  37. |                         Fairfax, VA  22031
  38. |                           (703) 698-9600
  39. |
  40. |                                by
  41. |                 SimGraphics Engineering Corporation
  42. |                    1137 Huntington Drive  Unit A
  43. |                      South Pasadena, CA  91030
  44. |                           (213) 255-0900
  45. |---------------------------------------------------------------------
  46. |
  47. | Author        :    SimGraphics Engineering Corportation
  48. |
  49. | File          :    maptools.c
  50. | Date          :    Fri Feb  9 10:46:55 PST 1990
  51. | Project       :    PLB
  52. | Description    :    Utilities for view mapping and
  53. |            view specification entities 
  54. | Status        :    Version 1.0
  55. |
  56. | Revisions     :    
  57. |    6/28/89        ISO PHIGS View Calls        JMZ
  58. |
  59. |       2/90            MFC Tektronix, Inc.: PEX-SI API implementation.
  60. |
  61. |       5/90            MFC Tektronix, Inc.: PEX-SI API Binding change.
  62. |
  63. |      12/90            MFC Tektronix, Inc.: PEX-SI PEX5R1 Release.
  64. |
  65. \*--------------------------------------------------------------------*/
  66.  
  67. /*--------------------------------------------------------------------*\
  68. |    Table of Contents
  69. |
  70. |    void adjust_window(*float, int, float )
  71. |        :    Adjust the specified window to for the physical
  72. |    void compute_default_view( *BIF_Defaultviewspecification, *float)
  73. |        :    Compute and complete a default view specification
  74. |    int compute_vieworient_matrix(*float, *float, *float, *float)
  75. |        :    Compute a view orientation matrix based on the 
  76. |
  77. \*--------------------------------------------------------------------*/
  78.  
  79. /*--------------------------------------------------------------------*\
  80. |    Include files
  81. \*--------------------------------------------------------------------*/
  82.  
  83. #include "biftypes.h"
  84. #include "bifparse.h"
  85. #include "bifbuild.h"
  86. #include "bifmacro.h"
  87. #include "ph_map.h"
  88.  
  89.  
  90. /* ---------------------------------------------------------------------*\
  91. | Local global variables                                                   |
  92. \*--------------------------------------------------------------------- */
  93.  
  94. static float ident_matrix[4][4] =
  95. {
  96.     1., 0., 0., 0.,
  97.     0., 1., 0., 0.,
  98.     0., 0., 1., 0.,
  99.     0., 0., 0., 1.,
  100. };
  101.  
  102. /*--------------------------------------------------------------------*\
  103. |    BEGIN PROCEDURE CODE
  104. \*--------------------------------------------------------------------*/
  105.  
  106. /*--------------------------------------------------------------------*\
  107. | Procedure    :    void adjust_window(*float, int, float )
  108. |-----------------------------------------------------------------------
  109. | Description    :    Adjust the specified window to for the physical
  110. |            size of the display.  Eliminate the Stretch /
  111. |            squash effect. Match_type defines the type
  112. |            of adjustment to perform.
  113. |-----------------------------------------------------------------------
  114. | Return    :    None.
  115. \*--------------------------------------------------------------------*/
  116. void adjust_window(uvWindow, match_type, aspect_ratio )
  117. float uvWindow[];
  118. int match_type;
  119. float aspect_ratio;
  120.  
  121. {
  122.     float aspect, u_size, u_avg, v_size, v_avg;
  123.  
  124.     if ( match_type != BIF_NO_AREA_MATCH )
  125.     {
  126.         /*----------------------------------------------------*\
  127.         |    Basic Size Information
  128.         \*----------------------------------------------------*/
  129.         u_avg  = (uvWindow[1] + uvWindow[0]) / 2.;
  130.         u_size =  uvWindow[1] - uvWindow[0];
  131.         v_avg  = (uvWindow[3] + uvWindow[2]) / 2.;
  132.         v_size =  uvWindow[3] - uvWindow[2];
  133.         /* Aspect Ratio */
  134.         if ( v_size != 0. )
  135.             aspect = u_size / v_size;
  136.         else
  137.             aspect = u_size;
  138.  
  139.         /*----------------------------------------------------*\
  140.         |    Do the size correction
  141.         \*----------------------------------------------------*/
  142.         switch ( match_type )
  143.         {
  144.         case ADJUST_X:
  145.             /* Set the x size of the window for a match */
  146.             u_size =  v_size * aspect_ratio;
  147.             break;
  148.  
  149.         case ADJUST_Y:
  150.             /* Set the y size of the window for a match */
  151.             v_size =  u_size / aspect_ratio;
  152.             break;
  153.  
  154.         case GROW:
  155.             /* Find which one to GROW */
  156.             if ( aspect < aspect_ratio )
  157.                 /*------------------------------------*\
  158.                 |    Grow X to match
  159.                 \*------------------------------------*/
  160.                 u_size =  v_size * aspect_ratio;
  161.             else
  162.                 /*------------------------------------*\
  163.                 |    Grow Y to match
  164.                 \*------------------------------------*/
  165.                 v_size =  u_size / aspect_ratio;
  166.  
  167.             break;
  168.  
  169.     
  170.         case SHRINK:
  171.             /* Find which one to SHRINK */
  172.             if ( aspect > aspect_ratio )
  173.                 /*------------------------------------*\
  174.                 |    Shrink X size for a match
  175.                 \*------------------------------------*/
  176.                 u_size =  v_size * aspect_ratio;
  177.             else
  178.                 /*------------------------------------*\
  179.                 |    Shrink Y size for a match
  180.                 \*------------------------------------*/
  181.                 v_size =  u_size / aspect_ratio;
  182.  
  183.             break;
  184.  
  185.         }
  186.  
  187.         /*----------------------------------------------------*\
  188.         |    Generate a new window based on the new sizes
  189.         \*----------------------------------------------------*/
  190.         uvWindow[0] = u_avg - u_size/2.;
  191.         uvWindow[1] = u_avg + u_size/2.;
  192.         uvWindow[2] = v_avg - v_size/2.;
  193.         uvWindow[3] = v_avg + v_size/2.;
  194.     }
  195.  
  196. } /* End procedure adjust_window */
  197.  
  198. /*--------------------------------------------------------------------*\
  199. | Procedure    :    void compute_default_view(
  200. |                *BIF_Defaultviewspecification, *float)
  201. |-----------------------------------------------------------------------
  202. | Description    :    Compute and complete a default view specification
  203. |             Adjust the specified window to for the physical
  204. |            size of the display.  Eliminate the Stretch /
  205. |            squash effect. 
  206. |            View looks at objects of a given radius on the
  207. |            origin.
  208. |
  209. |-----------------------------------------------------------------------
  210. | Return    :    None.
  211. \*--------------------------------------------------------------------*/
  212. void compute_default_view(ent, aspect_ratio )
  213. BIF_Defaultviewspecification *ent;
  214. float aspect_ratio;
  215.  
  216. {
  217.     static Ppoint3 d_vrp = { 0., 0., 0. };  /* View the origin */
  218.     static Pvec3 d_vpn = { 0., 0., 1. };  /* Look along -z   */
  219.     static Pvec3 d_vup = { 0., 1., 0. };  /* y is vertical   */
  220.     static float npcMinMax[6] = 
  221.     {
  222.         0., 1.,
  223.         0., 1.,
  224.         0., 1.
  225.     };
  226.  
  227.     float front_plane, back_plane;
  228.     float uvMinMax[4];
  229.     float proj_reference[3];
  230.     int ierr;
  231.     Pview_map3 mapping;
  232.  
  233.     /*------------------------------------------------------------*\
  234.     |    Compute the View orientation matrix
  235.     |    store it in the defaultviewspec entity
  236.     \*------------------------------------------------------------*/
  237. #ifdef USING_PHIGS
  238.     /* Using PHIGS Utilities */
  239.     peval_view_ori_matrix3(&d_vrp,&d_vpn,&d_vup,&ierr,
  240.                     ent->rep.ori_matrix);
  241.  
  242. #else /* USING_PHIGS */
  243.     /* Using Native Utilities or application code */
  244.     /* set to identity */
  245.     mx_identity(ent->rep.ori_matrix);
  246.  
  247. #endif /* USING_PHIGS */
  248.  
  249.     /*------------------------------------------------------------*\
  250.     |    Compute the View mapping parameters
  251.     |    First the window in the view space based on radius
  252.     \*------------------------------------------------------------*/
  253.     /* The view window type */
  254.     uvMinMax[0] = -ent->radius_of_view;
  255.     uvMinMax[1] =  ent->radius_of_view;
  256.     uvMinMax[2] = -ent->radius_of_view;
  257.     uvMinMax[3] =  ent->radius_of_view;
  258.     adjust_window(uvMinMax, GROW, aspect_ratio );
  259.  
  260.  
  261.     /* The projection reference point */
  262.     proj_reference[0] = 0.;
  263.     proj_reference[1] = 0.;
  264.     /* Check BIF_PARALLEL againts GX4000 ENUMs */
  265.     if ( ent->proj_type == BIF_PARALLEL )
  266.         /* Viewer offset for orthographic projections */
  267.         proj_reference[2] = 3.0 * ent->radius_of_view;
  268.     else
  269.         /* Viewer offset for perspective projections */
  270.         proj_reference[2] = 2.13 * ent->radius_of_view;
  271.  
  272.     /* The clipping planes */
  273.     front_plane     =  2.0 * ent->radius_of_view;
  274.     back_plane      = -2.0 * ent->radius_of_view;
  275.  
  276.     /*------------------------------------------------------------*\
  277.     |    Compute the View Mapping Matrix
  278.     \*------------------------------------------------------------*/
  279. #ifdef USING_PHIGS
  280.     /* Using PHIGS Utilities */
  281.     mapping.win.x_min = uvMinMax[0];
  282.     mapping.win.x_max = uvMinMax[1];
  283.     mapping.win.y_min = uvMinMax[2];
  284.     mapping.win.y_max = uvMinMax[3];
  285.     mapping.proj_vp.x_min = npcMinMax[0];
  286.     mapping.proj_vp.y_min = npcMinMax[2];
  287.     if (aspect_ratio != 1.0) {
  288.         if (aspect_ratio < 1.0) { /* Shrink X */
  289.         mapping.proj_vp.x_max = npcMinMax[1] * aspect_ratio;
  290.         mapping.proj_vp.y_max = npcMinMax[3];
  291.         }
  292.         else { /* Shrink Y */
  293.         mapping.proj_vp.x_max = npcMinMax[1];
  294.         mapping.proj_vp.y_max = npcMinMax[3] / aspect_ratio;
  295.         }
  296.     }
  297.     else {
  298.         mapping.proj_vp.x_max = npcMinMax[1];
  299.         mapping.proj_vp.y_max = npcMinMax[3];
  300.     }
  301.     mapping.proj_vp.z_min = npcMinMax[4];
  302.     mapping.proj_vp.z_max = npcMinMax[5];
  303.     mapping.proj_type = (Pproj_type)ent->proj_type;
  304.     mapping.proj_ref_point.x = proj_reference[0];
  305.     mapping.proj_ref_point.y = proj_reference[1];
  306.     mapping.proj_ref_point.z = proj_reference[2];
  307.     mapping.view_plane = 0.0;
  308.     mapping.back_plane = back_plane;
  309.     mapping.front_plane = front_plane;
  310.     peval_view_map_matrix3(&mapping,&ierr,
  311.                 ent->rep.map_matrix);
  312. #else /* USING_PHIGS */
  313.     /* Using Native Utilities or application code */
  314.     /* set to identity (to make sure it HAS a value */
  315.     mx_identity(ent->rep.map_matrix);
  316.  
  317. #endif /* USING_PHIGS */
  318.  
  319.     /*------------------------------------------------------------*\
  320.     |    Fill the View Specification Parameters
  321.     \*------------------------------------------------------------*/
  322.     /* Clipping Flags */
  323.     ent->rep.xy_clip    = (Pclip_ind)BIF_CLIP;
  324.     ent->rep.front_clip = (Pclip_ind)BIF_CLIP;
  325.     ent->rep.back_clip  = (Pclip_ind)BIF_CLIP;
  326.  
  327.     /* View area : Normalized BIF Display Coord. */
  328.     ent->rep.clip_limit.x_min = 0.;
  329.     ent->rep.clip_limit.y_min = 0.;
  330.     if (aspect_ratio != 1.0) {
  331.         if (aspect_ratio < 1.0) { /* Shrink X */
  332.         ent->rep.clip_limit.x_max = 1. * aspect_ratio;
  333.         ent->rep.clip_limit.y_max = 1.;
  334.         }
  335.         else { /* Shrink Y */
  336.         ent->rep.clip_limit.x_max = 1.;
  337.         ent->rep.clip_limit.y_max = 1. / aspect_ratio;
  338.         }
  339.     }
  340.     else {
  341.         ent->rep.clip_limit.x_max = 1.;
  342.         ent->rep.clip_limit.y_max = 1.;
  343.     }
  344.     ent->rep.clip_limit.z_min = 0.;
  345.     ent->rep.clip_limit.z_max = 1.;
  346.  
  347. } /* End procedure compute_default_view */
  348.  
  349. /*--------------------------------------------------------------------*\
  350. | Procedure    :    int compute_vieworient_matrix(*float, *float,
  351. |                        *float, *float)
  352. |---------------------------------------------------------------------
  353. | Description    :    Compute a view orientation matrix based on the 
  354. |            three vectors given:
  355. |            --------------------------------
  356. |            vrp    View reference point
  357. |            vpn    View plane normal
  358. |            vup    View up vector
  359. |            matrix    matrix to hold result
  360. |---------------------------------------------------------------------
  361. | Return    :    Error Code ( -1 Error, 0 else.)
  362. \*--------------------------------------------------------------------*/
  363. int compute_vieworient_matrix(vrp, vpn, vup, matrix)
  364. float *vrp, *vpn, *vup;
  365. Pmatrix3 matrix;
  366.  
  367. {
  368.     int ierr;
  369.     /*------------------------------------------------------------*\
  370.     |    Compute the View orientation matrix
  371.     |    store it in the matrix specified
  372.     \*------------------------------------------------------------*/
  373. #ifdef USING_PHIGS
  374.     /* Using PHIGS Utilities */
  375.     peval_view_ori_matrix3((Ppoint3 *)vrp,(Pvec3 *)vpn,
  376.                     (Pvec3 *)vup,&ierr,matrix);
  377.  
  378. #else /* USING_PHIGS */
  379.     /* Using Native Utilities or application code */
  380.     /* set to identity so it HAS a value */
  381.     mx_identity(matrix);
  382.     ierr = 0;
  383.  
  384. #endif /* USING_PHIGS */
  385.  
  386.     return(ierr);
  387.  
  388. }
  389.