home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / stereo / GL_5.2 / nestedcubes.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  5.9 KB  |  326 lines

  1. /*
  2.  * Copyright (C) 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *                    Nested cubes example of stereo
  19.  *
  20.  *     If given any arguments, will run in stereo windows mode; a stereo
  21.  *   background program should probably be run before it to avoid a
  22.  *   messy looking screen.
  23.  */
  24.  
  25. #include <string.h>
  26. #include <math.h>
  27.  
  28. #define USE_X
  29. #include "stereo.h"
  30.  
  31.  
  32. static Matrix objmat = {
  33.     {1.0, 0.0, 0.0, 0.0},
  34.     {0.0, 1.0, 0.0, 0.0},
  35.     {0.0, 0.0, 1.0, 0.0},
  36.     {0.0, 0.0, 0.0, 1.0},
  37. };
  38.  
  39. enum { NOTHING, ORIENT, SPIN };
  40. int mode = SPIN;
  41.  
  42. int omx, mx, omy, my;    /* old and new mouse position */
  43.  
  44. int use_windows = 0;    /* Hog whole screen by default */
  45.  
  46. int softStereo;         /* If set then SoftStereo is supported */
  47.  
  48. long wid;    /* Window identifier */
  49.  
  50. extern Display * dpy;
  51.  
  52. /*
  53.  * Local function prototypes
  54.  */
  55. static void initialize(char **, int);
  56. static void update_scene();
  57. static void orient();
  58. static void spin();
  59. static void draw_scene();
  60. static void draw_cube();
  61.  
  62. main(int argc, char* argv[])
  63. {
  64.     int dev;
  65.     Window val;
  66.     XEvent event;
  67.     int primary = STR_BOT;
  68.  
  69.     if (argc > 1)
  70.         primary = STR_TOP;    /* use top */
  71.  
  72.     initialize(argv, primary);
  73.  
  74.     draw_scene();
  75.  
  76.     while (TRUE)
  77.     {
  78.         Window rw, cw;
  79.         int xw,yw,xr,yr;
  80.             unsigned int keys_buttons;
  81.  
  82.         if (mode==SPIN && !XCheckMaskEvent(dpy, ButtonPressMask|ButtonReleaseMask|
  83.             ExposureMask|KeyPressMask|
  84.             StructureNotifyMask|Button2MotionMask|PointerMotionHintMask, &event))
  85.         {
  86.             update_scene();
  87.             continue;
  88.         }
  89.         else if (mode == ORIENT)
  90.             XNextEvent(dpy, &event);
  91.  
  92.         switch(event.type)
  93.         {
  94.         case KeyPress:
  95.         {
  96.             char buffer[5];
  97.             int bufsize = 5;
  98.             KeySym key;
  99.             XComposeStatus cs;
  100.             int count;
  101.  
  102.             count = XLookupString((XKeyEvent *)&event, buffer, bufsize, &key, &cs);
  103.             if (count == 0) break;
  104.             if (buffer[0] == '\33')
  105.             {    /* ESC */
  106.             stereo_off();    /* From libstereo.a */
  107.             exit(0);
  108.             }
  109.         }
  110.         case ConfigureNotify:
  111.         case Expose:
  112.             reshapeviewport();
  113.             draw_scene();
  114.             break;
  115.         case ButtonPress:
  116.             if (event.xbutton.button == Button2)
  117.             {
  118.             mode = ORIENT;
  119.             }
  120.             break;
  121.         case MotionNotify:
  122.             XQueryPointer(dpy, event.xmotion.window,
  123.             &rw, &cw, &xr, &yr, &xw, &yw, &keys_buttons);
  124.  
  125.             omx = mx;
  126.             omy = my;
  127.             mx = xw;
  128.             my = yw;
  129.             update_scene();
  130.             break;
  131.         }    /* End of switch */
  132.     }    /* End of while(TRUE) */
  133. }
  134.  
  135. static int xmaxscreen, ymaxscreen;
  136.  
  137. static void
  138. initialize(char **argv, int primary)
  139. {
  140.     char *t;    /* Set to name of executable */
  141.     t = strrchr(argv[0], '/');
  142.     if (t == NULL) t = argv[0];
  143.     else t +=1 ;
  144.  
  145.     xmaxscreen = getgdesc(GD_XPMAX);
  146.     ymaxscreen = getgdesc(GD_YPMAX);
  147.  
  148.     wid = st_winopen(t, primary, &softStereo);
  149.         if(!softStereo)
  150.         {
  151.              printf("SoftStereo isn't supported on this display\n");
  152.              exit(1);
  153.         }
  154.  
  155.     st_left(wid);
  156.     mmode(MVIEWING);
  157.     frontbuffer(TRUE); RGBcolor(0, 0, 0); clear(); frontbuffer(FALSE);
  158.  
  159.     st_right(wid);
  160.     mmode(MVIEWING);
  161.     frontbuffer(TRUE); RGBcolor(0, 0, 0); clear(); frontbuffer(FALSE);
  162. }
  163.  
  164. static void
  165. update_scene()
  166. {
  167.     switch (mode)
  168.     {
  169.     case ORIENT:
  170.         orient();
  171.         break;
  172.     case SPIN:
  173.         spin();
  174.         break;
  175.     }
  176.  
  177.     if (mode != NOTHING) draw_scene();
  178. }
  179.  
  180. static void
  181. orient ()
  182. {
  183.     pushmatrix();
  184.  
  185.     rotate(mx-omx, 'y');
  186.     rotate(my - omy, 'x');
  187.  
  188.     multmatrix(objmat);
  189.     getmatrix(objmat);
  190.  
  191.     popmatrix();
  192. }
  193.  
  194. static void
  195. spin()
  196. {
  197.     static float a, b, c;
  198.     float sa, sb, sc;
  199.  
  200.     sa = fsin(a);
  201.     sb = fsin(b);
  202.     sc = fsin(c);
  203.  
  204.     if ((a += 1.0 * M_PI/1800.) > 2.0*M_PI) a -= 2.0*M_PI;
  205.     if ((b += 3.0 * M_PI/1800.) > 2.0*M_PI) b -= 2.0*M_PI;
  206.     if ((c += 7.0 * M_PI/1800.) > 2.0*M_PI) c -= 2.0*M_PI;
  207.  
  208.     pushmatrix();
  209.  
  210.     rot(sa, 'y');
  211.     rot(sb, 'x');
  212.     rot(sc, 'z');
  213.  
  214.     multmatrix(objmat);
  215.     getmatrix(objmat);
  216.  
  217.     popmatrix();
  218. }
  219.  
  220. static int fovy = 300;
  221. static float near = 4.0, far = 8.0, conv = 6.0, eye = 0.2;
  222. static float dist = 6.0;
  223.  
  224. static void
  225. draw_scene() 
  226. {
  227.     int i;
  228.     float aspect =
  229.         (float)xmaxscreen/(float)YMAXSTEREO/2.0;
  230.  
  231.     /* right eye */
  232.  
  233.     st_right(wid);
  234.  
  235.     RGBcolor(255, 255, 255); clear();
  236.  
  237.     pushmatrix();
  238.     stereopersp(fovy, aspect, near, far, conv, eye);
  239.     translate(0.0, 0.0, -dist);
  240.     multmatrix(objmat);
  241.     for (i=0; i<10; i++)
  242.     {
  243.         scale(0.8, 0.8, 0.8);
  244.         rotate(50, 'x');
  245.         rotate(70, 'y');
  246.         rotate(90, 'z');
  247.         draw_cube();
  248.     }
  249.     popmatrix();
  250.  
  251.     /* left eye */
  252.  
  253.     st_left(wid);
  254.  
  255.     RGBcolor(255, 255, 255); clear();
  256.  
  257.     pushmatrix();
  258.     stereopersp(fovy, aspect, near, far, conv, -eye);
  259.     translate(0.0, 0.0, -dist);
  260.     multmatrix(objmat);
  261.     for (i=0; i<10; i++)
  262.     {
  263.         scale(0.8, 0.8, 0.8);
  264.         rotate(50, 'x');
  265.         rotate(70, 'y');
  266.         rotate(90, 'z');
  267.         draw_cube();
  268.     }
  269.     popmatrix();
  270.  
  271.     swapbuffers();
  272.  
  273. }
  274.  
  275. static float cube_vert[8][3] = {
  276.     {1.0, 1.0, -1.0,},
  277.     {1.0, -1.0, -1.0,},
  278.     {-1.0, -1.0, -1.0,},
  279.     {-1.0, 1.0, -1.0,},
  280.     {1.0, 1.0, 1.0,},
  281.     {1.0, -1.0, 1.0,},
  282.     {-1.0, -1.0, 1.0,},
  283.     {-1.0, 1.0, 1.0,},
  284. };
  285.  
  286.  
  287. static void
  288. draw_cube()
  289. {
  290.     RGBcolor(0, 0, 0);
  291.  
  292.     bgnclosedline();
  293.     v3f(cube_vert[0]);
  294.     v3f(cube_vert[1]);
  295.     v3f(cube_vert[2]);
  296.     v3f(cube_vert[3]);
  297.     endclosedline();
  298.  
  299.     bgnclosedline();
  300.     v3f(cube_vert[4]);
  301.     v3f(cube_vert[5]);
  302.     v3f(cube_vert[6]);
  303.     v3f(cube_vert[7]);
  304.     endclosedline();
  305.  
  306.     bgnline();
  307.     v3f(cube_vert[0]);
  308.     v3f(cube_vert[4]);
  309.     endline();
  310.  
  311.     bgnline();
  312.     v3f(cube_vert[1]);
  313.     v3f(cube_vert[5]);
  314.     endline();
  315.  
  316.     bgnline();
  317.     v3f(cube_vert[2]);
  318.     v3f(cube_vert[6]);
  319.     endline();
  320.  
  321.     bgnline();
  322.     v3f(cube_vert[3]);
  323.     v3f(cube_vert[7]);
  324.     endline();
  325. }
  326.