home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (C) 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- /*
- * Nested cubes example of stereo
- *
- * If given any arguments, will run in stereo windows mode; a stereo
- * background program should probably be run before it to avoid a
- * messy looking screen.
- */
-
- #include <string.h>
- #include <math.h>
-
- #define USE_X
- #include "stereo.h"
-
-
- static Matrix objmat = {
- {1.0, 0.0, 0.0, 0.0},
- {0.0, 1.0, 0.0, 0.0},
- {0.0, 0.0, 1.0, 0.0},
- {0.0, 0.0, 0.0, 1.0},
- };
-
- enum { NOTHING, ORIENT, SPIN };
- int mode = SPIN;
-
- int omx, mx, omy, my; /* old and new mouse position */
-
- int use_windows = 0; /* Hog whole screen by default */
-
- int softStereo; /* If set then SoftStereo is supported */
-
- long wid; /* Window identifier */
-
- extern Display * dpy;
-
- /*
- * Local function prototypes
- */
- static void initialize(char **, int);
- static void update_scene();
- static void orient();
- static void spin();
- static void draw_scene();
- static void draw_cube();
-
- main(int argc, char* argv[])
- {
- int dev;
- Window val;
- XEvent event;
- int primary = STR_BOT;
-
- if (argc > 1)
- primary = STR_TOP; /* use top */
-
- initialize(argv, primary);
-
- draw_scene();
-
- while (TRUE)
- {
- Window rw, cw;
- int xw,yw,xr,yr;
- unsigned int keys_buttons;
-
- if (mode==SPIN && !XCheckMaskEvent(dpy, ButtonPressMask|ButtonReleaseMask|
- ExposureMask|KeyPressMask|
- StructureNotifyMask|Button2MotionMask|PointerMotionHintMask, &event))
- {
- update_scene();
- continue;
- }
- else if (mode == ORIENT)
- XNextEvent(dpy, &event);
-
- switch(event.type)
- {
- case KeyPress:
- {
- char buffer[5];
- int bufsize = 5;
- KeySym key;
- XComposeStatus cs;
- int count;
-
- count = XLookupString((XKeyEvent *)&event, buffer, bufsize, &key, &cs);
- if (count == 0) break;
- if (buffer[0] == '\33')
- { /* ESC */
- stereo_off(); /* From libstereo.a */
- exit(0);
- }
- }
- case ConfigureNotify:
- case Expose:
- reshapeviewport();
- draw_scene();
- break;
- case ButtonPress:
- if (event.xbutton.button == Button2)
- {
- mode = ORIENT;
- }
- break;
- case MotionNotify:
- XQueryPointer(dpy, event.xmotion.window,
- &rw, &cw, &xr, &yr, &xw, &yw, &keys_buttons);
-
- omx = mx;
- omy = my;
- mx = xw;
- my = yw;
- update_scene();
- break;
- } /* End of switch */
- } /* End of while(TRUE) */
- }
-
- static int xmaxscreen, ymaxscreen;
-
- static void
- initialize(char **argv, int primary)
- {
- char *t; /* Set to name of executable */
- t = strrchr(argv[0], '/');
- if (t == NULL) t = argv[0];
- else t +=1 ;
-
- xmaxscreen = getgdesc(GD_XPMAX);
- ymaxscreen = getgdesc(GD_YPMAX);
-
- wid = st_winopen(t, primary, &softStereo);
- if(!softStereo)
- {
- printf("SoftStereo isn't supported on this display\n");
- exit(1);
- }
-
- st_left(wid);
- mmode(MVIEWING);
- frontbuffer(TRUE); RGBcolor(0, 0, 0); clear(); frontbuffer(FALSE);
-
- st_right(wid);
- mmode(MVIEWING);
- frontbuffer(TRUE); RGBcolor(0, 0, 0); clear(); frontbuffer(FALSE);
- }
-
- static void
- update_scene()
- {
- switch (mode)
- {
- case ORIENT:
- orient();
- break;
- case SPIN:
- spin();
- break;
- }
-
- if (mode != NOTHING) draw_scene();
- }
-
- static void
- orient ()
- {
- pushmatrix();
-
- rotate(mx-omx, 'y');
- rotate(my - omy, 'x');
-
- multmatrix(objmat);
- getmatrix(objmat);
-
- popmatrix();
- }
-
- static void
- spin()
- {
- static float a, b, c;
- float sa, sb, sc;
-
- sa = fsin(a);
- sb = fsin(b);
- sc = fsin(c);
-
- if ((a += 1.0 * M_PI/1800.) > 2.0*M_PI) a -= 2.0*M_PI;
- if ((b += 3.0 * M_PI/1800.) > 2.0*M_PI) b -= 2.0*M_PI;
- if ((c += 7.0 * M_PI/1800.) > 2.0*M_PI) c -= 2.0*M_PI;
-
- pushmatrix();
-
- rot(sa, 'y');
- rot(sb, 'x');
- rot(sc, 'z');
-
- multmatrix(objmat);
- getmatrix(objmat);
-
- popmatrix();
- }
-
- static int fovy = 300;
- static float near = 4.0, far = 8.0, conv = 6.0, eye = 0.2;
- static float dist = 6.0;
-
- static void
- draw_scene()
- {
- int i;
- float aspect =
- (float)xmaxscreen/(float)YMAXSTEREO/2.0;
-
- /* right eye */
-
- st_right(wid);
-
- RGBcolor(255, 255, 255); clear();
-
- pushmatrix();
- stereopersp(fovy, aspect, near, far, conv, eye);
- translate(0.0, 0.0, -dist);
- multmatrix(objmat);
- for (i=0; i<10; i++)
- {
- scale(0.8, 0.8, 0.8);
- rotate(50, 'x');
- rotate(70, 'y');
- rotate(90, 'z');
- draw_cube();
- }
- popmatrix();
-
- /* left eye */
-
- st_left(wid);
-
- RGBcolor(255, 255, 255); clear();
-
- pushmatrix();
- stereopersp(fovy, aspect, near, far, conv, -eye);
- translate(0.0, 0.0, -dist);
- multmatrix(objmat);
- for (i=0; i<10; i++)
- {
- scale(0.8, 0.8, 0.8);
- rotate(50, 'x');
- rotate(70, 'y');
- rotate(90, 'z');
- draw_cube();
- }
- popmatrix();
-
- swapbuffers();
-
- }
-
- static float cube_vert[8][3] = {
- {1.0, 1.0, -1.0,},
- {1.0, -1.0, -1.0,},
- {-1.0, -1.0, -1.0,},
- {-1.0, 1.0, -1.0,},
- {1.0, 1.0, 1.0,},
- {1.0, -1.0, 1.0,},
- {-1.0, -1.0, 1.0,},
- {-1.0, 1.0, 1.0,},
- };
-
-
- static void
- draw_cube()
- {
- RGBcolor(0, 0, 0);
-
- bgnclosedline();
- v3f(cube_vert[0]);
- v3f(cube_vert[1]);
- v3f(cube_vert[2]);
- v3f(cube_vert[3]);
- endclosedline();
-
- bgnclosedline();
- v3f(cube_vert[4]);
- v3f(cube_vert[5]);
- v3f(cube_vert[6]);
- v3f(cube_vert[7]);
- endclosedline();
-
- bgnline();
- v3f(cube_vert[0]);
- v3f(cube_vert[4]);
- endline();
-
- bgnline();
- v3f(cube_vert[1]);
- v3f(cube_vert[5]);
- endline();
-
- bgnline();
- v3f(cube_vert[2]);
- v3f(cube_vert[6]);
- endline();
-
- bgnline();
- v3f(cube_vert[3]);
- v3f(cube_vert[7]);
- endline();
- }
-