home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / IRIT / DRAWFN3S.ZIP / VIEWOBJ.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-06  |  8.7 KB  |  224 lines

  1. /*****************************************************************************
  2. *   Routines to    set and    transform the view matrix (type    MatrixType):         *
  3. * 1. Rotate about X, Y or Z axes.                         *
  4. * 2. Scale (same to all    axes).                             *
  5. * 3. Translate about X, Y or Z axes.                         *
  6. *                                         *
  7. * Written by:  Gershon Elber                   Ver 0.1,    Apr. 1988    *
  8. *****************************************************************************/
  9.  
  10. #include <graphics.h>
  11. #include <math.h>
  12. #include <stdio.h>
  13. #include "Program.h"
  14. #include "Expr2TrG.h"
  15. #include "GenMat.h"
  16. #include "GraphGnG.h"
  17. #include "GraphGnL.h"           /* Needs the sizes of windows, so be careful! */
  18. #include "ViewObj.h"
  19. #include "MouseDrv.h"
  20.  
  21. /* Interactive mode menu set up structure is define below (See ViewObjL.H):  */
  22. InteractWindowStruct InteractMenu = {
  23.     { { (SW_MIN_X + SW_MAX_X) / 2.0, SW_MAX_Y - 0.2, RED,   "Rotate" },
  24.       { (SW_MIN_X + SW_MAX_X) / 2.0, SW_MAX_Y - 0.6, GREEN, "Translate" },
  25.       { (SW_MIN_X + SW_MAX_X) / 2.0, SW_MAX_Y - 1.0, CYAN,  "Scale" },
  26.     },
  27.     { { (SW_MIN_X + SW_MAX_X) / 2.0, SW_MAX_Y - 0.1, YELLOW,  TRUE,  "Screen Coords." },
  28.       { (SW_MIN_X + SW_MAX_X) / 2.0, SW_MAX_Y - 0.3, RED,     FALSE, "X" },   /* Rot. */
  29.       { (SW_MIN_X + SW_MAX_X) / 2.0, SW_MAX_Y - 0.4, RED,     FALSE, "Y" },
  30.       { (SW_MIN_X + SW_MAX_X) / 2.0, SW_MAX_Y - 0.5, RED,     FALSE, "Z" },
  31.       { (SW_MIN_X + SW_MAX_X) / 2.0, SW_MAX_Y - 0.7, GREEN,   FALSE, "X" }, /* Trans. */
  32.       { (SW_MIN_X + SW_MAX_X) / 2.0, SW_MAX_Y - 0.8, GREEN,   FALSE, "Y" },
  33.       { (SW_MIN_X + SW_MAX_X) / 2.0, SW_MAX_Y - 0.9, GREEN,   FALSE, "Z" },
  34.       { (SW_MIN_X + SW_MAX_X) / 2.0, SW_MAX_Y - 1.1, CYAN,    FALSE, "" },  /* Scale. */
  35.       { (SW_MIN_X + SW_MAX_X) / 2.0, SW_MAX_Y - 1.25, YELLOW, TRUE,  "Save Matrix" },
  36.       { (SW_MIN_X + SW_MAX_X) / 2.0, SW_MAX_Y - 1.4, YELLOW,  TRUE,  "Reset Matrix" },
  37.       { (SW_MIN_X + SW_MAX_X) / 2.0, SW_MAX_Y - 1.8, WHITE,   TRUE,  "Quit" },
  38.     }
  39. };
  40.  
  41. static void SaveCurrentMat(MatrixType TransMat);
  42.  
  43. /*****************************************************************************
  44. *  Routine to draw the Interactive Menu in the Status window, using the      *
  45. * InteractiveMenu structure defined above.                     *
  46. *  It is assumed that string not inside of SubWindow will be of length 1.    *
  47. *****************************************************************************/
  48. void InteractDrawMenu(void)
  49. {
  50.     int i;
  51.     struct textsettingstype oldtext;
  52.  
  53.     gettextsettings(&oldtext);
  54.     settextjustify(CENTER_TEXT, CENTER_TEXT);       /* Draw strings centered. */
  55.  
  56.     GGClearMenuArea();                 /* Clear the status window. */
  57.  
  58.     for (i=0; i<INTERACT_NUM_OF_STRINGS; i++) {   /* Draw strings of struct. */
  59.         GGMySetColor(InteractMenu.Strings[i].Color);
  60.     GGPutMsgXY(InteractMenu.Strings[i].Str,
  61.            InteractMenu.Strings[i].X,
  62.            InteractMenu.Strings[i].Y);
  63.     }
  64.  
  65.     for (i=0; i<INTERACT_NUM_OF_SUB_WINDOWS; i++) {/* Draw struct sub wndws. */
  66.     GGMySetColor(InteractMenu.SubWindows[i].Color);
  67.     /* Draw the frame of the SubWindow: */
  68.     GGMyMove(InteractMenu.SubWindows[i].X - INTERACT_SUB_WINDOW_WIDTH,
  69.          InteractMenu.SubWindows[i].Y - INTERACT_SUB_WINDOW_HEIGHT);
  70.     GGMyDraw(InteractMenu.SubWindows[i].X + INTERACT_SUB_WINDOW_WIDTH,
  71.          InteractMenu.SubWindows[i].Y - INTERACT_SUB_WINDOW_HEIGHT);
  72.     GGMyDraw(InteractMenu.SubWindows[i].X + INTERACT_SUB_WINDOW_WIDTH,
  73.          InteractMenu.SubWindows[i].Y + INTERACT_SUB_WINDOW_HEIGHT);
  74.     GGMyDraw(InteractMenu.SubWindows[i].X - INTERACT_SUB_WINDOW_WIDTH,
  75.          InteractMenu.SubWindows[i].Y + INTERACT_SUB_WINDOW_HEIGHT);
  76.     GGMyDraw(InteractMenu.SubWindows[i].X - INTERACT_SUB_WINDOW_WIDTH,
  77.          InteractMenu.SubWindows[i].Y - INTERACT_SUB_WINDOW_HEIGHT);
  78.  
  79.     /* Now the strings inside (and if outside, a middle vertical line): */
  80.     if (InteractMenu.SubWindows[i].TextInside)
  81.         GGPutMsgXY(InteractMenu.SubWindows[i].Str,
  82.                InteractMenu.SubWindows[i].X,
  83.                InteractMenu.SubWindows[i].Y);
  84.  
  85.     else {
  86.         GGPutMsgXY(InteractMenu.SubWindows[i].Str,
  87.                InteractMenu.SubWindows[i].X -
  88.                 INTERACT_SUB_WINDOW_WIDTH - 0.025,
  89.                InteractMenu.SubWindows[i].Y);
  90.         GGMyMove(InteractMenu.SubWindows[i].X,
  91.              InteractMenu.SubWindows[i].Y - INTERACT_SUB_WINDOW_HEIGHT);
  92.         GGMyDraw(InteractMenu.SubWindows[i].X,
  93.              InteractMenu.SubWindows[i].Y + INTERACT_SUB_WINDOW_HEIGHT);
  94.     }
  95.     }
  96.  
  97.     settextjustify(oldtext.horiz, oldtext.vert);
  98. }
  99.  
  100. /*****************************************************************************
  101. *  Routine to handle data from the input device (keyboard, mouse etc.) -     *
  102. * clip it against the sub windows of the interactive menu and perform the    *
  103. * required transfomation, by updating the given matrix TransMat.         *
  104. *  The input data in the Rotation/Translation/Scaling sub windows is used    *
  105. * (horizontal distance from sub window center) to set amount of change.         *
  106. *  Return TRUE if quit was signalled...                         *
  107. *****************************************************************************/
  108. int InteractHandleInput(MatrixType TransMat, MatrixType OrigMat)
  109. {
  110.     int i, j, UpdateView, ScreenCoord;
  111.     double x, y, ChangeFactor;
  112.     MatrixType Mat;
  113.  
  114.     ScreenCoord = (InteractMenu.SubWindows[0].Str[0] == 'S');
  115.     do {
  116.     MouseFlushBuffer();      /* Flush out any duplicated points picked. */
  117.     GGGetPoint(&x, &y);        /* Get coordinates from pick device. */
  118.     for (i=0; i<INTERACT_NUM_OF_SUB_WINDOWS; i++)/* Test all sub windows.*/
  119.         if (fabs(InteractMenu.SubWindows[i].Y - y) <
  120.                         INTERACT_SUB_WINDOW_HEIGHT &&
  121.         fabs(InteractMenu.SubWindows[i].X - x) <
  122.                         INTERACT_SUB_WINDOW_WIDTH)
  123.         break;              /* The picked point is in this window! */
  124.  
  125.     ChangeFactor = (x - InteractMenu.SubWindows[i].X) //* Between -1..1. */
  126.                         INTERACT_SUB_WINDOW_WIDTH;
  127.     if (i >= INTERACT_NUM_OF_SUB_WINDOWS) {
  128.         GGTone(1000, 100);
  129.         GGTone(1500, 100);
  130.         GGTone(1000, 100);
  131.         UpdateView = FALSE;
  132.         continue;
  133.     }
  134.  
  135.     UpdateView = TRUE;
  136.  
  137.     switch (i) {
  138.         case 0:               /* Its Coordinate system - toggle it. */
  139.         if (ScreenCoord) {
  140.             InteractMenu.SubWindows[0].Str = "Object Coords.";
  141.             ScreenCoord = FALSE;
  142.         }
  143.         else {
  144.             InteractMenu.SubWindows[0].Str = "Screen Coords.";
  145.             ScreenCoord = TRUE;
  146.         }
  147.         InteractDrawMenu();      /* Must update the menu on screen. */
  148.         UpdateView = FALSE;
  149.         break;
  150.         case 1:               /* Its rotation along the X axis. */
  151.         GenMatRotX1(DEG2RAD(ChangeFactor * MAX_ROTATE_ANGLE), Mat);
  152.         break;
  153.         case 2:               /* Its rotation along the Y axis. */
  154.         GenMatRotY1(DEG2RAD(ChangeFactor * MAX_ROTATE_ANGLE), Mat);
  155.         break;
  156.         case 3:               /* Its rotation along the Z axis. */
  157.         GenMatRotZ1(DEG2RAD(ChangeFactor * MAX_ROTATE_ANGLE), Mat);
  158.         break;
  159.         case 4:            /* Its translation along the X axis. */
  160.         GenMatTrans(ChangeFactor * MAX_TRANSLATE_FACTOR, 0.0, 0.0,
  161.                                     Mat);
  162.         break;
  163.         case 5:            /* Its translation along the Y axis. */
  164.         GenMatTrans(0.0, ChangeFactor * MAX_TRANSLATE_FACTOR, 0.0,
  165.                                     Mat);
  166.         break;
  167.         case 6:            /* Its translation along the Z axis. */
  168.         GenMatTrans(0.0, 0.0, ChangeFactor * MAX_TRANSLATE_FACTOR,
  169.                                     Mat);
  170.         break;
  171.         case 7:                  /* Its scaling along all axes. */
  172.         if (ChangeFactor > 0.0)              /* Make it around 1... */
  173.              ChangeFactor = ChangeFactor * MAX_SCALE_FACTOR + 1.0;
  174.         else ChangeFactor = 1.0 /
  175.             (-ChangeFactor * MAX_SCALE_FACTOR + 1.0);
  176.         GenMatScale(ChangeFactor, ChangeFactor, ChangeFactor, Mat);
  177.         break;
  178.         case 8:                  /* Save transformation matrix. */
  179.         SaveCurrentMat(TransMat);
  180.         UpdateView = FALSE;
  181.         break;
  182.         case 9:                 /* Reset transformation matrix. */
  183.         for (i=0; i<4; i++)
  184.             for (j=0; j<4; j++) TransMat[i][j] = OrigMat[i][j];
  185.         GenUnitMat(Mat);               /* No transformation! */
  186.         break;
  187.         case 10:
  188.         return TRUE;                    /* Its Quit. */
  189.         default:
  190.         GGTone(1000, 100);               /* Do some noise! */
  191.         UpdateView = FALSE;
  192.         break;
  193.     }
  194.     }
  195.     while (!UpdateView);
  196.  
  197.     if (ScreenCoord)            /* Udpate the global viewing matrix. */
  198.      MultTwo4by4(TransMat, TransMat, Mat);
  199.     else MultTwo4by4(TransMat, Mat, TransMat);
  200.  
  201.     return FALSE;
  202. }
  203.  
  204. /*****************************************************************************
  205. * Routine to save the current view trans. GlblViewMat to a generic mat file  *
  206. *****************************************************************************/
  207. static void SaveCurrentMat(MatrixType TransMat)
  208. {
  209.     int    i, j;
  210.     FILE *f;
  211.  
  212.     if ((f = fopen(GENERIC_MAT_FILE, "wt")) == NULL) {
  213.     GGTone(700, 200);
  214.     return;
  215.     }
  216.  
  217.     for    (i=0; i<4; i++)    {
  218.     for (j=0; j<4; j++) fprintf(f, "%12lf ", TransMat[i][j]);
  219.     fprintf(f, "\n");
  220.     }
  221.  
  222.     fclose(f);
  223. }
  224.