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

  1. /* $XConsortium: auto_box.c,v 5.4 91/07/12 18:04:33 hersh Exp $ */
  2.  
  3. /***********************************************************
  4. Copyright 1989, 1990, 1991 by Sun Microsystems, Inc. and the X Consortium.
  5.  
  6.                         All Rights Reserved
  7.  
  8. Permission to use, copy, modify, and distribute this software and its 
  9. documentation for any purpose and without fee is hereby granted, 
  10. provided that the above copyright notice appear in all copies and that
  11. both that copyright notice and this permission notice appear in 
  12. supporting documentation, and that the names of Sun Microsystems,
  13. the X Consortium, and MIT not be used in advertising or publicity 
  14. pertaining to distribution of the software without specific, written 
  15. prior permission.  
  16.  
  17. SUN MICROSYSTEMS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 
  18. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT 
  19. SHALL SUN MICROSYSTEMS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL 
  20. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  22. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  23. SOFTWARE.
  24.  
  25. ******************************************************************/
  26.  
  27. #include <stdio.h>
  28. #include <errno.h>
  29. #include <math.h>
  30.  
  31. #ifndef M_PI
  32. #define M_PI  3.14159265358979323846
  33. #endif /* M_PI */
  34.  
  35.  
  36. #ifdef USE_X_DRAWABLE
  37. #include <X11/Xlib.h>
  38. #include <X11/Xatom.h>
  39. #endif /*USE_X_DRAWABLE*/
  40.  
  41. #include <phigs/phigs.h>
  42.  
  43. /* Workstation ids */
  44. #define WS1    1
  45.  
  46. /* Colors */
  47. #define BLACK    0
  48. #define WHITE    1
  49. #define RED    2
  50. #define GREEN    3
  51. #define BLUE    4
  52. #define YELLOW    5
  53. #define CYAN    6
  54. #define MAGENTA    7
  55.  
  56. /* Structure names */
  57. #define ROOT 0
  58. #define CUBE 1
  59. #define SUB  2
  60.  
  61. /* Labels */
  62. #define STYLE   1 
  63. #define QUAD_1  101 
  64. #define QUAD_2    102 
  65. #define QUAD_3    103 
  66. #define QUAD_4    104 
  67. #define QUAD_1_1 105 
  68. #define QUAD_4_1 106 
  69.  
  70. #define WIN_X    200
  71. #define WIN_Y    200
  72. #define WIN_W    400
  73. #define WIN_H    400
  74.  
  75. #define PFILLAREASET3
  76.  
  77. #ifdef PFILLAREASET3
  78. #define    PFILLAREA_CALL    pfill_area_set3( &side_list )
  79. #else /*PFILLAREASET3*/
  80. #define    PFILLAREA_CALL    pfill_area3( &side )
  81. #endif /*PFILLAREASET3*/
  82.  
  83. #ifdef USE_X_DRAWABLE
  84. extern void init_window();
  85. #endif /*USE_X_DRAWABLE*/
  86.  
  87. /* Globals */
  88. Ppoint3        pt;
  89. Pvec3        shift;
  90. Pfloat        x_ang, y_ang, z_ang;
  91. Pvec3        scale;
  92. Pmatrix3    bldmat, bldmat2, bldmat3, bldmat4;
  93.  
  94. static void
  95. setup(n, mat)
  96. int n;
  97. float mat[4][4];
  98. {
  99.     int i, j;
  100.  
  101.     for (i = 0; i < n; i++) {
  102.     for (j = 0; j < n; j++) {
  103.         mat[i][j] = 0;
  104.         if (i == j) mat[i][j] = 1.0;
  105.     }
  106.     }
  107.  
  108. static void
  109. rotate_views()
  110. {
  111.     float   theta;
  112.  
  113.     Pint err;
  114.  
  115. /*
  116.  * ... enable editing in the replace mode -- this will be used to update
  117.  *        rotation angles in 4 views
  118.  */
  119.     pset_disp_upd_st( WS1, PDEFER_WAIT, PMODE_UWOR);
  120.     pset_edit_mode( PEDIT_REPLACE);
  121. /*
  122.  * ...  set some constants for building the matrix
  123.  */
  124.     pt.x =  10.; pt.y = 10.; pt.z = 10.;
  125.     shift.delta_x = 0; shift.delta_y = 0; shift.delta_z = 0;
  126.     scale.delta_x = 1.0; scale.delta_y = 1.0; scale.delta_z = 1.0;
  127. /*
  128.  * ...  make identity matrices
  129.  */
  130.     setup( 4, bldmat);
  131.     setup( 4, bldmat2);
  132.     setup( 4, bldmat3);
  133.     setup( 4, bldmat4);
  134. /*
  135.  * ...  open the structure for editing
  136.  */
  137.  
  138.     popen_struct( SUB);
  139. /*
  140.  * ... increment the angle in a postive angle
  141.  */
  142.  
  143.     for (theta = 0; theta < 4 * M_PI; theta +=.4) {
  144. /*
  145.  * ...  set the pointer and the angles, 
  146.  * ...    find the label, 
  147.  * ...    build the matrix, 
  148.  * ...    update the view
  149.  */
  150.     pset_elem_ptr( 0);
  151. /*
  152.  * ... viewport 1
  153.  */
  154.     x_ang = theta;
  155.     y_ang = 0;
  156.     z_ang = 0;
  157.     pset_elem_ptr_label( 1);
  158.     poffset_elem_ptr( 1);
  159.     pbuild_tran_matrix3(    &pt, &shift, x_ang, y_ang, z_ang,
  160.                 &scale, &err, bldmat);
  161.     pset_local_tran3( bldmat, PTYPE_REPLACE);
  162. /*
  163.  * ... viewport 2
  164.  */
  165.     x_ang = 0;
  166.     y_ang = theta;
  167.  
  168.     pset_elem_ptr_label( 2);
  169.     poffset_elem_ptr( 1);
  170.  
  171.     pbuild_tran_matrix3(    &pt, &shift, x_ang, y_ang, z_ang,
  172.                 &scale,&err,bldmat2);
  173.     pset_local_tran3( bldmat2, PTYPE_REPLACE);
  174. /*
  175.  * ... viewport 3
  176.  */
  177.  
  178.     y_ang = 0;
  179.     z_ang = theta;
  180.  
  181.     pset_elem_ptr_label( 3);
  182.     poffset_elem_ptr( 1);
  183.  
  184.     pbuild_tran_matrix3(    &pt, &shift, x_ang, y_ang, z_ang,
  185.                 &scale, &err, bldmat3);
  186.     pset_local_tran3(bldmat3, PTYPE_REPLACE);
  187. /*
  188.  * ... viewport 4
  189.  */
  190.     x_ang = theta;
  191.     y_ang = theta;
  192.     z_ang = theta;
  193.  
  194.     pset_elem_ptr_label(4);
  195.     poffset_elem_ptr(1);
  196.  
  197.     pbuild_tran_matrix3(    &pt, &shift, x_ang, y_ang, z_ang,
  198.                 &scale, &err, bldmat4);
  199.     pset_local_tran3(bldmat4, PTYPE_REPLACE);
  200.         
  201.     pupd_ws(WS1, PFLAG_PERFORM);
  202.     }
  203. /*
  204.  *  close the structure being edited
  205.  */
  206.     pclose_struct();
  207. }
  208.  
  209. static void
  210. scale_views(scale_factor)
  211. float scale_factor;
  212. {
  213.  
  214.     float  i;
  215.     Pint err;
  216.  
  217. /*
  218.  * ... enable editing in the replace mode -- this will be used to update
  219.  *    the scale matrix in 4 views
  220.  */
  221.     pset_disp_upd_st( WS1, PDEFER_WAIT, PMODE_UWOR);
  222.     pset_edit_mode( PEDIT_REPLACE);
  223. /*
  224.  * ...  open the structure for editing
  225.  */
  226.  
  227.     popen_struct(SUB);
  228. /*
  229.  * ... increment the scale matrix 
  230.  */
  231.  
  232.     for (i = 1; i < 4 ; i += .2) {
  233. /*
  234.  * ...  set the pointer and the angles, find the label, build the matrix, update
  235.  * ...  the view
  236.  */
  237.     pset_elem_ptr(0);
  238. /*
  239.  * ... viewport 1
  240.  */
  241.     scale.delta_x = scale.delta_x * scale_factor; 
  242.     scale.delta_y = scale.delta_y * scale_factor; 
  243.     scale.delta_z = scale.delta_z * scale_factor;
  244.     pset_elem_ptr_label( 1);
  245.     poffset_elem_ptr( 1);
  246.     pbuild_tran_matrix3(&pt, &shift, x_ang, y_ang, z_ang, 
  247.                 &scale, &err, bldmat);
  248.     pset_local_tran3( bldmat, PTYPE_REPLACE);
  249. /*
  250.  * ... viewport 2
  251.  */
  252.     pset_elem_ptr_label(2);
  253.     poffset_elem_ptr(1);
  254.  
  255.     pbuild_tran_matrix3(&pt, &shift, x_ang, y_ang, z_ang,
  256.                 &scale, &err, bldmat2);
  257.     pset_local_tran3(bldmat2, PTYPE_REPLACE);
  258. /*
  259.  * ... viewport 3
  260.  */
  261.  
  262.     pset_elem_ptr_label(3);
  263.     poffset_elem_ptr(1);
  264.  
  265.     pbuild_tran_matrix3(&pt, &shift, x_ang, y_ang, z_ang,
  266.                 &scale, &err, bldmat3);
  267.     pset_local_tran3(bldmat3, PTYPE_REPLACE);
  268. /*
  269.  * ... viewport 4
  270.  */
  271.  
  272.     pset_elem_ptr_label(4);
  273.     poffset_elem_ptr(1);
  274.  
  275.     pbuild_tran_matrix3(&pt, &shift, x_ang, y_ang, z_ang,
  276.                 &scale, &err, bldmat4);
  277.     pset_local_tran3(bldmat4, PTYPE_REPLACE);
  278.  
  279.     pupd_ws(WS1, PFLAG_PERFORM);
  280.     }
  281. /*
  282.  *  close the structure being edited
  283.  */
  284.     pclose_struct();
  285. }
  286.  
  287. /*
  288.  *
  289.  *
  290.  *
  291.  */
  292.  
  293. static void
  294. load_box()
  295. {
  296.     Ppoint3    p[5];
  297.     Pvec3    xlv;
  298.     Pmatrix3    mx;
  299.     Pvec3    scale;
  300.     Pvec3    shift;
  301.     Pint    err;
  302.     Pfloat    xang, yang, zang;
  303.     Ppoint_list_list3 side_list;
  304.     Ppoint_list3 side;
  305.  
  306.     side.num_points = 4;
  307.     side.points = p;
  308.     side_list.num_point_lists = 1;
  309.     side_list.point_lists = &side;
  310.  
  311.  
  312.     setup(4, mx);
  313.  
  314.     popen_struct(CUBE);
  315.     plabel(STYLE);
  316.     pset_int_style(PSTYLE_SOLID );
  317.     pset_hlhsr_id(PHIGS_HLHSR_ID_ON );
  318. /*
  319.  * ... make a square to make a front and back of the cube
  320.  */
  321.     p[0].x = 5; p[0].y = 5; p[0].z = 5;
  322.     p[1].x = 15; p[1].y = 5; p[1].z = 5;
  323.     p[2].x = 15; p[2].y = 15; p[2].z = 5;
  324.     p[3].x = 5; p[3].y = 15; p[3].z = 5;
  325.  
  326. /* front */
  327.     xlv.delta_x = 0; xlv.delta_y = 0; xlv.delta_z = 10;
  328.     ptranslate3( &xlv, &err, mx);
  329.     pset_local_tran3( mx, PTYPE_REPLACE);
  330.  
  331.     pset_int_colr_ind( RED);
  332.     PFILLAREA_CALL;
  333.  
  334. /* back */
  335.  
  336.     xlv.delta_x = 5; xlv.delta_y = 10; xlv.delta_z = 5;
  337.     scale.delta_x = 1.0; scale.delta_y = 1.0; scale.delta_z = 1.0;
  338.     xang = 3.14; yang = 0.0; zang = 0.0; 
  339.     shift.delta_x = 0.; shift.delta_y = 0.; shift.delta_z = 0;
  340.     pbuild_tran_matrix3(&xlv, &shift, xang, yang, zang,
  341.             &scale, &err, mx);
  342.     pset_local_tran3(mx, PTYPE_REPLACE);
  343.  
  344.     pset_int_colr_ind(CYAN);
  345.     PFILLAREA_CALL;
  346.  
  347. /* right */
  348.     xlv.delta_x = 5; xlv.delta_y = 5; xlv.delta_z = 5;
  349.     scale.delta_x = 1.0; scale.delta_y = 1.0; scale.delta_z = 1.0;
  350.     xang = 0.0; yang = 3.14*1.5; zang = 0.0; 
  351.     shift.delta_x = 0.; shift.delta_y = 0.; shift.delta_z = 0;
  352.     pbuild_tran_matrix3(&xlv, &shift, xang, yang, zang,
  353.             &scale, &err, mx);
  354.     pset_local_tran3(mx, PTYPE_REPLACE);
  355.  
  356.     pset_int_colr_ind(GREEN);
  357.     PFILLAREA_CALL;
  358.  
  359. /* left */
  360.     xlv.delta_x = 15; xlv.delta_y = 5; xlv.delta_z = 5;
  361.     scale.delta_x = 1.0; scale.delta_y = 1.0; scale.delta_z = 1.0; 
  362.     xang = 0.0; yang = -3.14*1.5; zang = 0.0; 
  363.     shift.delta_x = 0.; shift.delta_y = 0.; shift.delta_z = 0;
  364.     pbuild_tran_matrix3(&xlv, &shift, xang, yang, zang,
  365.             &scale, &err, mx);
  366.     pset_local_tran3(mx, PTYPE_REPLACE);
  367.  
  368.     pset_int_colr_ind(BLUE);
  369.     PFILLAREA_CALL;
  370.  
  371. /* bottom */
  372.     xlv.delta_x = 5; xlv.delta_y = 5; xlv.delta_z = 5;
  373.     scale.delta_x = 1.0; scale.delta_y = 1.0; scale.delta_z = 1.0;
  374.     xang = -1.5*3.14; yang = 0.0; zang = 0.0; 
  375.     shift.delta_x = 0.; shift.delta_y = 0.; shift.delta_z = 0;
  376.     pbuild_tran_matrix3(&xlv, &shift, xang, yang, zang,
  377.             &scale, &err, mx);
  378.     pset_local_tran3(mx, PTYPE_REPLACE);
  379.  
  380.     pset_int_colr_ind( YELLOW);
  381.     PFILLAREA_CALL;
  382.  
  383. /* top */
  384.     xlv.delta_x = 5; xlv.delta_y = 15; xlv.delta_z = 5;
  385.     scale.delta_x = 1.0; scale.delta_y = 1.0; scale.delta_z = 1.0;
  386.     xang = 1.5*3.14; yang = 0.0; zang = 0.0; 
  387.     shift.delta_x = 0.; shift.delta_y = 0.; shift.delta_z = 0;
  388.     pbuild_tran_matrix3(&xlv, &shift, xang, yang, zang,
  389.             &scale, &err, mx);
  390.     pset_local_tran3(mx, PTYPE_REPLACE);
  391.  
  392.     pset_int_colr_ind(MAGENTA);
  393.     PFILLAREA_CALL;
  394.  
  395.     pclose_struct();
  396. }
  397.     
  398. Pview_map3 view_map = {
  399.     { -20.0, 20.0, -20.0, 20.0},
  400.     { 0.0, .5, 0.5, 1.0, 0.0, 1.0},
  401.     PTYPE_PERSPECT,
  402.     { 0., 0., 1000.0},
  403.     0.0,
  404.     -40.0,
  405.     40.0
  406. };
  407.  
  408. Pview_map3 view_map2 = {
  409.     { -20.0, 20.0, -20.0, 20.0},
  410.     { 0.5, 1.0, 0.5, 1.0, 0.0, 1.0},
  411.     PTYPE_PERSPECT,
  412.     { 0., 0., 1000.0},
  413.     0.0,
  414.     -40.0,
  415.     40.0
  416. };
  417.  
  418. Pview_map3 view_map3 = {
  419.     { -20.0, 20.0, -20.0, 20.0},
  420.     { 0.0, .5, 0., .5, 0.0, 1.0},
  421.     PTYPE_PERSPECT,
  422.     { 0., 0., 1000.0},
  423.     0.0,
  424.     -40.0,
  425.     40.0
  426. };
  427.  
  428. Pview_map3 view_map4 = {
  429.     { -20.0, 20.0, -20.0, 20.0},
  430.     { 0.5, 1.0, 0.,.5, 0.0, 1.0},
  431.     PTYPE_PERSPECT,
  432.     { 0., 0., 1000.0},
  433.     0.0,
  434.     -40.0,
  435.     40.0
  436. };
  437.  
  438. static void
  439. load_views(ws_id)
  440. Pint ws_id; 
  441. {
  442.     static Ppoint3 vrp = {0., 0., 0 };        /*  origin */
  443.     static Pvec3 vpn = {1.0, 1.0, 1.0 };    /*  view plane normal */
  444.     static Pvec3 vup = {0.0, 1.0, 0.0 };    /*  view up vector */
  445.     
  446.     Pview_rep3 vrep;                /*  view structure */
  447.  
  448.     Pint err;
  449.  
  450.     peval_view_ori_matrix3( &vrp, &vpn, &vup, &err, vrep.ori_matrix);
  451.  
  452.     if (err != 0) (void) printf( "view orientation error ");
  453.  
  454.     peval_view_map_matrix3(&view_map, &err, vrep.map_matrix);
  455.  
  456.     if (err != 0) (void) printf( "view mapping error ");
  457.  
  458.     vrep.clip_limit =  view_map.proj_vp;
  459.     vrep.xy_clip = PIND_NO_CLIP;
  460.     vrep.back_clip = PIND_NO_CLIP;
  461.     vrep.front_clip = PIND_NO_CLIP;
  462.     pset_view_rep3( ws_id, 1, &vrep);
  463. /*
  464.  *   viewport # 2
  465.  */
  466.     peval_view_ori_matrix3( &vrp, &vpn, &vup, &err, vrep.ori_matrix);
  467.  
  468.     if (err != 0) (void) printf( "view orientation error ");
  469.  
  470.     peval_view_map_matrix3 ( &view_map2, &err, vrep.map_matrix);
  471.  
  472.     if (err != 0) (void) printf( "view mapping error ");
  473.  
  474.     vrep.clip_limit =  view_map2.proj_vp;
  475.     vrep.xy_clip = PIND_NO_CLIP;
  476.     vrep.back_clip = PIND_NO_CLIP;
  477.     vrep.front_clip = PIND_NO_CLIP;
  478.     pset_view_rep3( ws_id, 2, &vrep);
  479. /*
  480.  *   viewport # 3
  481.  */
  482.     peval_view_ori_matrix3( &vrp, &vpn, &vup, &err, vrep.ori_matrix);
  483.  
  484.     if (err != 0) (void) printf( "view orientation error ");
  485.  
  486.     peval_view_map_matrix3( &view_map3, &err, vrep.map_matrix);
  487.  
  488.     if (err != 0) (void) printf( "view mapping error ");
  489.  
  490.     vrep.clip_limit =  view_map3.proj_vp;
  491.     vrep.xy_clip = PIND_NO_CLIP;
  492.     vrep.back_clip = PIND_NO_CLIP;
  493.     vrep.front_clip = PIND_NO_CLIP;
  494.     pset_view_rep3( ws_id, 3, &vrep);
  495. /*
  496.  *   viewport # 4
  497.  */
  498.     peval_view_ori_matrix3( &vrp, &vpn, &vup, &err, vrep.ori_matrix);
  499.  
  500.     if (err != 0) (void) printf ("view orientation error ");
  501.  
  502.     peval_view_map_matrix3( &view_map4, &err, vrep.map_matrix);
  503.  
  504.     if (err != 0) (void) printf( "view mapping error ");
  505.  
  506.     vrep.clip_limit =  view_map4.proj_vp;
  507.     vrep.xy_clip = PIND_NO_CLIP;
  508.     vrep.back_clip = PIND_NO_CLIP;
  509.     vrep.front_clip = PIND_NO_CLIP;
  510.     pset_view_rep3( ws_id, 4, &vrep);
  511. }
  512. static void
  513. build_css()
  514. {
  515.     Ppoint    line[2];
  516.     Ppoint_list p_line;
  517.     Pmatrix3    r1, r2, r3, r4;
  518.     Ppoint    text_pt;
  519.     char    text_str[32];
  520.  
  521.     setup( 4, r1);
  522.     setup( 4, r2);
  523.     setup( 4, r3);
  524.     setup( 4, r4);
  525.  
  526. /*
  527.  * ... open the root structure
  528.  */
  529.     popen_struct( ROOT);
  530.     pset_hlhsr_mode( WS1, 1);
  531.   
  532. /*
  533.  * ...  set up lines that split the viewports
  534.  */
  535.     pset_linewidth( 3.0);
  536.     pset_line_colr_ind( WHITE);
  537.     line[0].x = .5; line[0].y = 0.0; 
  538.     line[1].x = .5; line[1].y = 1.0;
  539.     p_line.num_points = 2;
  540.     p_line.points = line;
  541.     ppolyline( &p_line);
  542.  
  543.     line[0].x = 0.0; line[0].y = .5;
  544.     line[1].x = 1.0; line[1].y = .5;
  545.     ppolyline( &p_line);
  546. /*
  547.  * ... text to explain each viewport
  548.  */
  549.     pset_text_colr_ind( YELLOW);
  550.     pset_text_font(-2);            /* Server will use default font here */
  551.     pset_char_ht( .025);
  552.     text_pt.x = 0; text_pt.y = .52;
  553.     strcpy( text_str, " X Rotation");
  554.     plabel( QUAD_2);
  555.     ptext( &text_pt, text_str);
  556.    
  557.     text_pt.x = .5; text_pt.y = .58;
  558.     strcpy( text_str, " Y Rotation");
  559.     plabel( QUAD_1);
  560.     ptext( &text_pt, text_str);
  561.     text_pt.y = .52;
  562.     strcpy( text_str, " Backfaces Culled");
  563.     plabel( QUAD_1_1 );
  564.     ptext( &text_pt, text_str);
  565.  
  566.     text_pt.x = 0; text_pt.y = .02;
  567.     strcpy( text_str, " Z Rotation");
  568.     plabel( QUAD_3);
  569.     ptext( &text_pt, text_str);
  570.  
  571.     text_pt.x = .5; text_pt.y = .08;
  572.     strcpy( text_str, " XYZ Rotation");
  573.     plabel( QUAD_4);
  574.     ptext( &text_pt, text_str);
  575.     text_pt.y = .02;
  576.     strcpy( text_str, " Frontfaces Culled");
  577.     plabel( QUAD_4_1 );
  578.     ptext( &text_pt, text_str);
  579.  
  580.     pset_linewidth( 1.0);
  581.  
  582.     pexec_struct( SUB);
  583.     pclose_struct();
  584.  
  585.     popen_struct( SUB);
  586.     pset_view_ind( 1);
  587.     plabel( 1);
  588.     pset_local_tran3( r1, PTYPE_PRECONCAT);
  589.  
  590.     /* Cull no faces in this view */
  591.     pset_face_cull_mode(PCULL_NONE);
  592.  
  593.     pexec_struct( CUBE);
  594.  
  595.     pset_view_ind( 2);
  596.     plabel( 2);
  597.     pset_local_tran3( r2, PTYPE_PRECONCAT);
  598.  
  599.     /* Cull backfaces in this view */
  600.     pset_face_cull_mode(PCULL_BACKFACE);
  601.  
  602.     pexec_struct( CUBE);
  603.  
  604.     pset_view_ind( 3);
  605.     plabel( 3);
  606.     pset_local_tran3( r3, PTYPE_PRECONCAT);
  607.  
  608.     /* Cull no faces in this view */
  609.     pset_face_cull_mode(PCULL_NONE);
  610.  
  611.     pexec_struct( CUBE);
  612.  
  613.     pset_view_ind( 4);
  614.     plabel( 4);
  615.     pset_local_tran3( r4, PTYPE_PRECONCAT);
  616.  
  617.     /* Cull frontfaces in this view */
  618.     pset_face_cull_mode(PCULL_FRONTFACE);
  619.  
  620.     pexec_struct( CUBE);
  621.     pclose_struct();
  622. }
  623.  
  624. static void
  625. edit_css()
  626. {
  627.     popen_struct(CUBE);
  628.     pset_elem_ptr(0);
  629.     pset_elem_ptr_label(STYLE);
  630.     poffset_elem_ptr(1);
  631.     pset_int_style(PSTYLE_HOLLOW);
  632. /*
  633.  * ... close the structure being edited
  634.  */
  635.     pclose_struct();
  636. }
  637.  
  638. static void
  639. relabel_quadrants()
  640. {
  641.     char text_str[32];
  642.     Ppoint text_pt;
  643. /*
  644.  * ... relabel the quadrants
  645.  */
  646.     popen_struct( ROOT);
  647.     strcpy( text_str, " Zooming ");
  648.     text_pt.x = 0; text_pt.y = .52;
  649.     pset_elem_ptr( 0);
  650.     pset_elem_ptr_label( QUAD_2);
  651.     poffset_elem_ptr( 1);
  652.     ptext( &text_pt, text_str);
  653.  
  654.     text_pt.x = .5; text_pt.y = .52;
  655.     pset_elem_ptr_label( QUAD_1);
  656.     poffset_elem_ptr( 1);
  657.     ptext( &text_pt, text_str);
  658.  
  659.     text_pt.x = 0; text_pt.y = .02;
  660.     pset_elem_ptr_label( QUAD_3);
  661.     poffset_elem_ptr( 1);
  662.     ptext( &text_pt, text_str);
  663.  
  664.     text_pt.x = .5; text_pt.y = .02;
  665.     pset_elem_ptr_label( QUAD_4);
  666.     poffset_elem_ptr( 1);
  667.     ptext( &text_pt, text_str);
  668.  
  669.     /* Remove the "culling" text labels from the quadrants */
  670.  
  671.     pset_elem_ptr (0);
  672.     pset_elem_ptr_label( QUAD_1_1);
  673.     poffset_elem_ptr( 1);
  674.     pdel_elem();
  675.     pset_elem_ptr_label( QUAD_4_1);
  676.     poffset_elem_ptr( 1);
  677.     pdel_elem();
  678.  
  679.     pclose_struct();
  680. }
  681.  
  682. static void
  683. init_phigs()
  684. {
  685. #ifdef USE_X_DRAWABLE
  686.     extern Display *appl_display;
  687.     extern Window appl_window;
  688.     Pconnid_x_drawable conn;
  689. #endif /*USE_X_DRAWABLE*/
  690.  
  691.     Pmatrix3 s;
  692.     Pmatrix3 mat;
  693.     Pmatrix3 r1, r2, r3, r4;
  694.  
  695. #ifdef USE_X_DRAWABLE
  696.     conn.display = appl_display;
  697.     conn.drawable_id = appl_window;
  698. #endif /*USE_X_DRAWABLE*/
  699.  
  700. /* Initialization of matrices */
  701.  
  702.     setup( 4, mat);
  703.     setup( 4, s);
  704.  
  705.     setup( 4, r1);
  706.     setup( 4, r2);
  707.     setup( 4, r3);
  708.     setup( 4, r4);
  709. /*
  710.  * ...  set some constants for building the matrix
  711.  */
  712.     pt.x =  10.; pt.y = 10.; pt.z = 10.;
  713.     shift.delta_x = 0; shift.delta_y = 0; shift.delta_z = 0;
  714.     scale.delta_x = 1.0; scale.delta_y = 1.0; scale.delta_z = 1.0;
  715. /*
  716.  * ...  make identity matrices
  717.  */
  718.     setup(4, bldmat);
  719.     setup(4, bldmat2);
  720.     setup(4, bldmat3);
  721.     setup(4, bldmat4);
  722. /*
  723.  *   ... open a phigs workstation 
  724.  */
  725.     popen_phigs( NULL, 0);
  726.     {
  727.     Psys_st    sys_state;
  728.  
  729.     pinq_sys_st( &sys_state );
  730.     if (sys_state != PSYS_ST_PHOP) { exit(1); }
  731.     }
  732. #ifdef USE_X_DRAWABLE
  733.     popen_ws(WS1, (Pconnid)(&conn), phigs_ws_type_x_drawable);
  734. #else /*!USE_X_DRAWABLE*/
  735.     popen_ws(WS1, (Pconnid)NULL, phigs_ws_type_x_tool);
  736. #endif /*USE_X_DRAWABLE*/
  737.     {
  738.     Pws_st  ws_state;
  739.  
  740.     pinq_ws_st(&ws_state);
  741.     if (ws_state != PWS_ST_WSOP) { exit(3); }
  742.     }
  743.  
  744. /*
  745.  * ... initialize the viewports
  746.  */
  747.     load_views(WS1);
  748. /*
  749.  * ... load the structure containing the cube
  750.  */
  751.     load_box(); 
  752.  
  753.     build_css();
  754.  
  755. }
  756.  
  757. static void
  758. init()
  759. {
  760. #ifdef USE_X_DRAWABLE
  761.     init_window();
  762. #endif /* USE_X_DRAWABLE */
  763.     init_phigs();
  764. }
  765.  
  766. static void
  767. draw_image()
  768. {
  769.     float scale_factor;
  770.  
  771. /*
  772.  * ... post the structure  --- display it
  773.  */
  774.  
  775.     ppost_struct (WS1, ROOT, 0.0);
  776. #ifdef IGNORE
  777.     strcpy(buf,"Program will auto rotate and scale ... then exit");
  778.     pmessage( WS1, buf);
  779. #endif /*IGNORE*/
  780. /*
  781.  * ... rotate the four views
  782.  */
  783.     rotate_views();
  784.  
  785. /*
  786.  * ... turn off solids and make wireframe
  787.  */
  788.     edit_css();
  789. /*
  790.  * ... rotate the four views in wireframe
  791.  */
  792.     rotate_views();
  793.  
  794.     relabel_quadrants();
  795.  
  796. /*
  797.  * ... scale down the four views
  798.  */
  799.     scale_factor = .5;
  800.     scale_views( scale_factor );
  801. /*
  802.  * ... scale up the four views
  803.  */
  804.     scale_factor = 2.0;
  805.     scale_views( scale_factor );
  806.  
  807. /*
  808.  * test traversal
  809.  */
  810.     predraw_all_structs( WS1, PFLAG_ALWAYS);
  811. }
  812.  
  813. static void
  814. quit_program()
  815. {
  816. /*    clean up and close 
  817.  *    the workstation and Phigs
  818.  */
  819.     sleep(5);
  820.     pclose_ws(WS1);
  821.     pclose_phigs();
  822. }
  823.  
  824. main()
  825. {
  826.     init();
  827.     draw_image();
  828.     quit_program();
  829.     exit(0);
  830. }
  831.