home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / GLUT / progs / contrib / fracviewer.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  12.9 KB  |  493 lines

  1. /*
  2.  * fractviewer.c [from agviewer.c  (version 1.0)]
  3.  *
  4.  * AGV: a glut viewer. Routines for viewing a 3d scene w/ glut
  5.  *
  6.  * See agv_example.c and agviewer.h comments within for more info.
  7.  *
  8.  * I welcome any feedback or improved versions!
  9.  *
  10.  * Philip Winston - 4/11/95
  11.  * pwinston@hmc.edu
  12.  * http://www.cs.hmc.edu/people/pwinston
  13.  */
  14.  
  15. #include <GL/glut.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <math.h>
  19.  
  20. #include "fracviewer.h"
  21.  
  22. /***************************************************************/
  23. /************************** SETTINGS ***************************/
  24. /***************************************************************/
  25.  
  26.    /* Initial polar movement settings */
  27. #define INIT_POLAR_AZ  0.0
  28. #define INIT_POLAR_EL 30.0
  29. #define INIT_DIST      4.0
  30. #define INIT_AZ_SPIN   0.5
  31. #define INIT_EL_SPIN   0.0
  32.  
  33.   /* Initial flying movement settings */
  34. #define INIT_EX        0.0
  35. #define INIT_EY       -2.0
  36. #define INIT_EZ       -2.0
  37. #define INIT_MOVE     0.01
  38. #define MINMOVE      0.001    
  39.  
  40.   /* Start in this mode */
  41. #define INIT_MODE   POLAR   
  42.  
  43.   /* Controls:  */
  44.  
  45.   /* map 0-9 to an EyeMove value when number key is hit in FLYING mode */
  46. #define SPEEDFUNCTION(x) ((x)*(x)*0.001)  
  47.  
  48.   /* Multiply EyeMove by (1+-MOVEFRACTION) when +/- hit in FLYING mode */
  49. #define MOVEFRACTION 0.25   
  50.  
  51.   /* What to multiply number of pixels mouse moved by to get rotation amount */
  52. #define EL_SENS   0.5
  53. #define AZ_SENS   0.5
  54.  
  55.   /* What to multiply number of pixels mouse moved by for movement amounts */
  56. #define DIST_SENS 0.01
  57. #define E_SENS    0.01
  58.  
  59.   /* Minimum spin to allow in polar (lower forced to zero) */
  60. #define MIN_AZSPIN 0.1
  61. #define MIN_ELSPIN 0.1
  62.  
  63.   /* Factors used in computing dAz and dEl (which determine AzSpin, ElSpin) */
  64. #define SLOW_DAZ 0.90
  65. #define SLOW_DEL 0.90
  66. #define PREV_DAZ 0.80
  67. #define PREV_DEL 0.80
  68. #define CUR_DAZ  0.20
  69. #define CUR_DEL  0.20
  70.  
  71. /***************************************************************/
  72. /************************** GLOBALS ****************************/
  73. /***************************************************************/
  74.  
  75. int     MoveMode = INIT_MODE;  /* FLYING or POLAR mode? */
  76.  
  77. GLfloat Ex = INIT_EX,             /* flying parameters */
  78.         Ey = INIT_EY,
  79.         Ez = INIT_EZ,
  80.         EyeMove = INIT_MOVE,     
  81.  
  82.         EyeDist = INIT_DIST,      /* polar params */
  83.         AzSpin  = INIT_AZ_SPIN,
  84.         ElSpin  = INIT_EL_SPIN,
  85.  
  86.         EyeAz = INIT_POLAR_AZ,    /* used by both */
  87.         EyeEl = INIT_POLAR_EL;
  88.  
  89. int agvMoving;    /* Currently moving?  */
  90.  
  91. int downx, downy,   /* for tracking mouse position */
  92.     lastx, lasty,
  93.     downb = -1;     /* and button status */
  94.                         
  95. GLfloat downDist, downEl, downAz, /* for saving state of things */
  96.         downEx, downEy, downEz,   /* when button is pressed */
  97.         downEyeMove;                
  98.  
  99. GLfloat dAz, dEl, lastAz, lastEl;  /* to calculate spinning w/ polar motion */
  100. int     AdjustingAzEl = 0;
  101.  
  102. int AllowIdle, RedisplayWindow; 
  103.    /* If AllowIdle is 1 it means AGV will install its own idle which
  104.     * will update the viewpoint as needed and send glutPostRedisplay() to the
  105.     * window RedisplayWindow which was set in agvInit().  AllowIdle of 0
  106.     * means AGV won't install an idle funciton, and something like
  107.     * "if (agvMoving) agvMove()" should exist at the end of the running
  108.     * idle function.
  109.     */
  110.  
  111. #define MAX(x,y) (((x) > (y)) ? (x) : (y))
  112. #define TORAD(x) ((M_PI/180.0)*(x))
  113. #define TODEG(x) ((180.0/M_PI)*(x))
  114.  
  115. /***************************************************************/
  116. /************************ PROTOTYPES ***************************/
  117. /***************************************************************/
  118.  
  119.   /*
  120.    * these are functions meant for internal use only
  121.    * the other prototypes are in agviewer.h
  122.    */
  123.  
  124. void PolarLookFrom(GLfloat dist, GLfloat elevation, GLfloat azimuth);
  125. void FlyLookFrom(GLfloat x, GLfloat y, GLfloat z,
  126.                         GLfloat az, GLfloat el);
  127. int  ConstrainEl(void);
  128. void MoveOn(int v);
  129. void SetMove(float newmove);
  130. static void normalize(GLfloat v[3]);
  131. static void ncrossprod(float v1[3], float v2[3], float cp[3]);
  132.  
  133.  
  134. /***************************************************************/
  135. /************************ agvInit ******************************/
  136. /***************************************************************/
  137.  
  138. void agvInit(int window)
  139. {
  140.   glutMouseFunc(agvHandleButton);
  141.   glutMotionFunc(agvHandleMotion);
  142.   glutKeyboardFunc(agvHandleKeys);
  143.   RedisplayWindow = glutGetWindow();
  144.   agvSetAllowIdle(window);
  145. }
  146.  
  147. /***************************************************************/
  148. /************************ VIEWPOINT STUFF **********************/
  149. /***************************************************************/
  150.  
  151.   /*
  152.    * viewing transformation modified from page 90 of red book
  153.    */
  154. void PolarLookFrom(GLfloat dist, GLfloat elevation, GLfloat azimuth)
  155. {
  156.   glTranslatef(0, 0, -dist);
  157.   glRotatef(elevation, 1, 0, 0);
  158.   glRotatef(azimuth, 0, 1, 0);
  159.  
  160. }
  161.  
  162.   /*
  163.    * I took the idea of tracking eye position in absolute
  164.    * coords and direction looking in Polar form from denis
  165.    */
  166. void FlyLookFrom(GLfloat x, GLfloat y, GLfloat z, GLfloat az, GLfloat el)
  167. {
  168.   float lookat[3], perp[3], up[3];
  169.  
  170.   lookat[0] = sin(TORAD(az))*cos(TORAD(el));
  171.   lookat[1] = sin(TORAD(el));
  172.   lookat[2] = -cos(TORAD(az))*cos(TORAD(el));
  173.   normalize(lookat);
  174.   perp[0] = lookat[2];
  175.   perp[1] = 0;
  176.   perp[2] = -lookat[0];
  177.   normalize(perp);
  178.   ncrossprod(lookat, perp, up);
  179.   gluLookAt(x, y, z,
  180.             x+lookat[0], y+lookat[1], z+lookat[2],
  181.             up[0], up[1], up[2]);
  182. }
  183.  
  184.   /*
  185.    * Call viewing transformation based on movement mode
  186.    */
  187. void agvViewTransform(void)
  188.   switch (MoveMode) {
  189.     case FLYING:
  190.       FlyLookFrom(Ex, Ey, Ez, EyeAz, EyeEl);
  191.       break;
  192.     case POLAR:
  193.       PolarLookFrom(EyeDist, EyeEl, EyeAz);
  194.       break;
  195.     }
  196. }
  197.  
  198.   /*
  199.    * keep them vertical; I think this makes a lot of things easier, 
  200.    * but maybe it wouldn't be too hard to adapt things to let you go
  201.    * upside down
  202.    */
  203. int ConstrainEl(void)
  204. {
  205.   if (EyeEl <= -90) {
  206.     EyeEl = -89.99;
  207.     return 1;
  208.   } else if (EyeEl >= 90) {
  209.     EyeEl = 89.99;
  210.     return 1;
  211.   }
  212.   return 0;
  213. }
  214.  
  215.  /*
  216.   * Idle Function - moves eyeposition
  217.   */
  218. void agvMove(void)
  219. {
  220.  
  221.   switch (MoveMode)  {
  222.     case FLYING:
  223.       Ex += EyeMove*sin(TORAD(EyeAz))*cos(TORAD(EyeEl));
  224.       Ey += EyeMove*sin(TORAD(EyeEl));
  225.       Ez -= EyeMove*cos(TORAD(EyeAz))*cos(TORAD(EyeEl));
  226.       break;
  227.  
  228.     case POLAR:
  229.       EyeEl += ElSpin;
  230.       EyeAz += AzSpin;
  231.       if (ConstrainEl()) {  /* weird spin thing to make things look     */
  232.         ElSpin = -ElSpin;      /* look better when you are kept from going */
  233.                                /* upside down while spinning - Isn't great */
  234.         if (fabs(ElSpin) > fabs(AzSpin))
  235.           AzSpin = fabs(ElSpin) * ((AzSpin > 0) ? 1 : -1);
  236.       }
  237.       break;
  238.     }
  239.  
  240.   if (AdjustingAzEl) {
  241.     dAz *= SLOW_DAZ;
  242.     dEl *= SLOW_DEL;
  243.   }
  244.  
  245.   if (AllowIdle) {
  246.     glutSetWindow(RedisplayWindow);
  247.     glutPostRedisplay();
  248.   }
  249. }
  250.  
  251.  
  252.   /*
  253.    * Don't install agvMove as idle unless we will be updating the view
  254.    * and we've been given a RedisplayWindow
  255.    */
  256. void MoveOn(int v)
  257. {
  258.   if (v && ((MoveMode == FLYING && EyeMove != 0) ||
  259.              (MoveMode == POLAR &&
  260.              (AzSpin != 0 || ElSpin != 0 || AdjustingAzEl)))) {
  261.     agvMoving = 1;
  262.     if (AllowIdle)
  263.       glutIdleFunc(agvMove);
  264.   } else {
  265.     agvMoving = 0;
  266.     if (AllowIdle)
  267.       glutIdleFunc(NULL);
  268.   }
  269. }
  270.  
  271.   /*
  272.    * set new redisplay window.  If <= 0 it means we are not to install
  273.    * an idle function and will rely on whoever does install one to 
  274.    * put statement like "if (agvMoving) agvMove();" at end of it
  275.    */
  276. void agvSetAllowIdle(int allowidle)
  277. {
  278.   if ((AllowIdle = allowidle))
  279.     MoveOn(1);
  280. }
  281.  
  282.  
  283.   /*
  284.    * when moving to flying we stay in the same spot, moving to polar we
  285.    * reset since we have to be looking at the origin (though a pivot from
  286.    * current position to look at origin might be cooler)
  287.    */
  288. void agvSwitchMoveMode(int move)
  289. {
  290.   switch (move) {
  291.     case FLYING:
  292.       if (MoveMode == FLYING) return;
  293.       Ex    = -EyeDist*sin(TORAD(EyeAz))*cos(TORAD(EyeEl));
  294.       Ey    =  EyeDist*sin(TORAD(EyeEl));
  295.       Ez    =  EyeDist*(cos(TORAD(EyeAz))*cos(TORAD(EyeEl)));
  296.       EyeAz =  EyeAz;
  297.       EyeEl = -EyeEl;
  298.       EyeMove = INIT_MOVE;
  299.       break;
  300.     case POLAR:
  301.       EyeDist = INIT_DIST;
  302.       EyeAz   = INIT_POLAR_AZ;
  303.       EyeEl   = INIT_POLAR_EL;
  304.       AzSpin  = INIT_AZ_SPIN;
  305.       ElSpin  = INIT_EL_SPIN;
  306.       break;
  307.     }
  308.   MoveMode = move;
  309.   MoveOn(1);
  310.   glutPostRedisplay();
  311. }
  312.  
  313. /***************************************************************/
  314. /*******************    MOUSE HANDLING   ***********************/
  315. /***************************************************************/
  316.  
  317. void agvHandleButton(int button, int state, int x, int y)
  318. {
  319.  if (state == GLUT_DOWN && downb == -1) {  
  320.     lastx = downx = x;
  321.     lasty = downy = y;
  322.     downb = button;    
  323.  
  324.     switch (button) {
  325.       case GLUT_LEFT_BUTTON:
  326.         lastEl = downEl = EyeEl;
  327.         lastAz = downAz = EyeAz;
  328.         AzSpin = ElSpin = dAz = dEl = 0;
  329.         AdjustingAzEl = 1;
  330.     MoveOn(1);
  331.         break;
  332.  
  333.       case GLUT_MIDDLE_BUTTON:
  334.         downDist = EyeDist;
  335.     downEx = Ex;
  336.     downEy = Ey;
  337.     downEz = Ez;
  338.     downEyeMove = EyeMove;
  339.     EyeMove = 0;
  340.     }
  341.  
  342.   } else if (state == GLUT_UP && button == downb) {
  343.  
  344.     downb = -1;
  345.  
  346.     switch (button) {
  347.       case GLUT_LEFT_BUTTON:
  348.         if (MoveMode != FLYING) {
  349.       AzSpin =  -dAz;
  350.       if (AzSpin < MIN_AZSPIN && AzSpin > -MIN_AZSPIN)
  351.         AzSpin = 0;    
  352.       ElSpin = -dEl;
  353.       if (ElSpin < MIN_ELSPIN && ElSpin > -MIN_ELSPIN)
  354.         ElSpin = 0; 
  355.     }
  356.         AdjustingAzEl = 0;
  357.         MoveOn(1);
  358.     break;
  359.  
  360.       case GLUT_MIDDLE_BUTTON:
  361.     EyeMove = downEyeMove;
  362.       }
  363.   }
  364. }
  365.  
  366.  /*
  367.   * change EyeEl and EyeAz and position when mouse is moved w/ button down
  368.   */
  369. void agvHandleMotion(int x, int y)
  370. {
  371.   int deltax = x - downx, deltay = y - downy;
  372.  
  373.   switch (downb) {
  374.     case GLUT_LEFT_BUTTON:
  375.       EyeEl  = downEl + EL_SENS * ((MoveMode == FLYING) ? -deltay : deltay);
  376.       ConstrainEl();
  377.       EyeAz  = downAz + AZ_SENS * deltax;
  378.       dAz    = PREV_DAZ*dAz + CUR_DAZ*(lastAz - EyeAz);
  379.       dEl    = PREV_DEL*dEl + CUR_DEL*(lastEl - EyeEl);
  380.       lastAz = EyeAz;
  381.       lastEl = EyeEl;
  382.       break;
  383.     case GLUT_MIDDLE_BUTTON:
  384.         EyeDist = downDist + DIST_SENS*deltay;
  385.         Ex = downEx - E_SENS*deltay*sin(TORAD(EyeAz))*cos(TORAD(EyeEl));
  386.         Ey = downEy - E_SENS*deltay*sin(TORAD(EyeEl));
  387.         Ez = downEz + E_SENS*deltay*cos(TORAD(EyeAz))*cos(TORAD(EyeEl));
  388.       break;
  389.   }
  390.   glutPostRedisplay();
  391. }
  392.  
  393. /***************************************************************/
  394. /********************* KEYBOARD HANDLING ***********************/
  395. /***************************************************************/
  396.  
  397.   /*
  398.    * set EyeMove (current speed) for FLYING mode
  399.    */
  400. void SetMove(float newmove)
  401. {
  402.   if (newmove > MINMOVE) {
  403.     EyeMove = newmove;
  404.     MoveOn(1);
  405.   } else {
  406.     EyeMove = 0;
  407.     MoveOn(0);
  408.   }
  409. }
  410.  
  411.   /*
  412.    * 0->9 set speed, +/- adjust current speed  -- in FLYING mode
  413.    */
  414. void agvHandleKeys(unsigned char key, int x, int y)
  415. {
  416.   if (MoveMode != FLYING)
  417.     return;
  418.  
  419.   if (key >= '0' && key <= '9')
  420.     SetMove(SPEEDFUNCTION((key-'0')));
  421.   else
  422.     switch(key) {
  423.       case '+':  
  424.         if (EyeMove == 0)
  425.           SetMove(MINMOVE);
  426.          else
  427.       SetMove(EyeMove *= (1 + MOVEFRACTION));
  428.         break;
  429.       case '-':
  430.     SetMove(EyeMove *= (1 - MOVEFRACTION));
  431.         break;
  432.     }
  433. }
  434.  
  435. /***************************************************************/
  436. /*********************** VECTOR STUFF **************************/
  437. /***************************************************************/
  438.  
  439.   /* normalizes v */
  440. static void normalize(GLfloat v[3])
  441. {
  442.   GLfloat d = sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
  443.  
  444.   if (d == 0)
  445.     fprintf(stderr, "Zero length vector in normalize\n");
  446.   else
  447.     v[0] /= d; v[1] /= d; v[2] /= d;
  448. }
  449.  
  450.   /* calculates a normalized crossproduct to v1, v2 */
  451. static void ncrossprod(float v1[3], float v2[3], float cp[3])
  452. {
  453.   cp[0] = v1[1]*v2[2] - v1[2]*v2[1];
  454.   cp[1] = v1[2]*v2[0] - v1[0]*v2[2];
  455.   cp[2] = v1[0]*v2[1] - v1[1]*v2[0];
  456.   normalize(cp);
  457. }
  458.  
  459. /***************************************************************/
  460. /**************************** AXES *****************************/
  461. /***************************************************************/
  462.  
  463.  
  464.   /* draw axes -- was helpful to debug/design things */
  465. void agvMakeAxesList(int displaylistnum)
  466. {
  467.   int i,j;
  468.   GLfloat axes_ambuse[] =   { 0.5, 0.0, 0.0, 1.0 };
  469.   glNewList(displaylistnum, GL_COMPILE);
  470.   glPushAttrib(GL_LIGHTING_BIT);
  471.   glMatrixMode(GL_MODELVIEW);
  472.     glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, axes_ambuse);
  473.     glBegin(GL_LINES);
  474.       glVertex3f(15, 0, 0); glVertex3f(-15, 0, 0);
  475.       glVertex3f(0, 15, 0); glVertex3f(0, -15, 0);
  476.       glVertex3f(0, 0, 15); glVertex3f(0, 0, -15);
  477.     glEnd();
  478.     for (i = 0; i < 3; i++) {
  479.       glPushMatrix();
  480.         glTranslatef(-10*(i==0), -10*(i==1), -10*(i==2));
  481.         for (j = 0; j < 21; j++) {
  482.           glutSolidCube(0.1);
  483.           glTranslatef(i==0, i==1, i==2);
  484.     }
  485.       glPopMatrix();
  486.     }
  487.   glPopAttrib();
  488.   glEndList();  
  489. }
  490.  
  491.  
  492.