home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 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.
- */
- /* visual_info.c - simple double buffered RGBA xlib program which
- * rotates an object.
- *
- * An overlay window is created as a child of the main window and it
- * is managed as part of the parent window. The overlay
- * window/context creation uses the visual_info extension to
- * find a visual with a transparent pixel.
- */
- /* compile: cc -o visual_info visual_info.c -lGLU -lGL -lX11 */
-
- #include <GL/glx.h>
- #include <GL/glu.h>
- #include <X11/keysym.h>
- #include <stdlib.h>
- #include <stdio.h>
-
- static int attributeList[] = {
- GLX_RGBA, GLX_RED_SIZE, 1, GLX_DOUBLEBUFFER, None };
- static int ovAttributeList[] = {
- GLX_BUFFER_SIZE, 2,
- GLX_LEVEL, 1,
- #ifdef GLX_EXT_visual_info
- GLX_TRANSPARENT_TYPE_EXT, GLX_TRANSPARENT_INDEX_EXT,
- #endif
- None };
- static Window win, ovwin;
- static GLXContext cx, ovcx;
- static Display *dpy;
-
- static Bool animate;
-
- static int transparent_index = 0;
-
- static void
- initgfx(void) {
- glShadeModel(GL_FLAT);
- glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
- gluPerspective(40., 3.0/2.0, 0.001, 100000.0);
- glTranslatef(0.0, 0.0, -3.0);
- glClearColor(0.2,0.2,0.2,0.);
- }
-
- static void
- side(void) { /* make a square translated 0.5 in the z direction */
- glPushMatrix();
- glTranslatef(0.0,0.0,0.5);
- glRectf(-0.5,-0.5,0.5,0.5);
- glPopMatrix();
- }
-
- static void
- cube(void) { /* make a cube out of 4 squares */
- glPushMatrix();
- side();
- glRotatef(90.,1.,0.,0.);
- side();
- glRotatef(90.,1.,0.,0.);
- side();
- glRotatef(90.,1.,0.,0.);
- side();
- glPopMatrix();
- }
-
- static void
- draw_scene(void) {
- static float rot = 0.;
-
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(.1, .1, .8);
- glPushMatrix();
- if (animate && (rot += 5.) > 360.) rot -= 360.;
- glRotatef(rot,0.,1.,0.);
- cube();
- glScalef(0.3,0.3,0.3);
- glColor3f(.8, .8, .1);
- cube();
- glPopMatrix();
- }
-
- /*
- * the amount of work being done doesn't justify the need for
- * a separate overlay initialization and redraw routines so we
- * do it all together. XXX doesn't allocate and initialize any
- * color cells.
- */
- static void
- initialize_ov(void) {
- int i;
- /* simply draw a grid of 10 equally spaced lines */
- glLoadIdentity();
- glOrtho(0., 11., 0., 11., -1., 1.);
- glClearIndex(transparent_index);
- glClear(GL_COLOR_BUFFER_BIT);
- glIndexi(1);
- glBegin(GL_LINES);
- for(i = 0; i < 11; i++) {
- glVertex2f(0.5+i, 0.0);
- glVertex2f(0.5+i, 11.0);
- glVertex2f(0.0, 0.5+i);
- glVertex2f(11.0, 0.5+i);
- }
- glEnd();
- }
-
- static Bool
- process_input(void) {
- XEvent event;
- static Bool mapped;
- Bool viewport_ov = 0, redraw_ov = 0, redraw = 0;
- GLsizei w, h;
-
- if(XPending(dpy) || !mapped || !animate) {
- char buf[31];
- KeySym keysym;
-
- XNextEvent(dpy, &event);
- switch(event.type) {
- case Expose:
- redraw_ov = redraw = 1;
- break;
- case ConfigureNotify:
- w = event.xconfigure.width;
- h = event.xconfigure.height;
- glViewport(0, 0, w, h);
- redraw_ov = redraw = 1;
- viewport_ov = 1;
- break;
- case MapNotify:
- mapped = 1;
- break;
- case UnmapNotify:
- mapped = 0;
- break;
- case KeyPress:
- (void) XLookupString(&event.xkey, buf, sizeof(buf),
- &keysym, NULL);
- switch (keysym) {
- case XK_Escape:
- exit(EXIT_SUCCESS);
- case XK_space:
- animate ^= 1;
- break;
- default:
- break;
- }
- default:
- break;
- }
- }
- if (redraw_ov) {
- glXMakeCurrent(dpy, ovwin, ovcx);
- if (viewport_ov) {
- XResizeWindow(dpy, ovwin, w, h);
- glViewport(0, 0, w, h);
- }
- initialize_ov();
- glXMakeCurrent(dpy, win, cx);
- }
- return redraw | animate;
- }
-
- int
- main(int argc, char **argv) {
- XVisualInfo *vi, *ovi;
- XSetWindowAttributes swa;
- Bool isdirect;
- char *s = NULL;
- int major, minor;
-
- dpy = XOpenDisplay(0);
-
- if (!(vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributeList))) {
- fprintf(stderr, "overlay: no suitable RGB visual available\n");
- exit(EXIT_FAILURE);
- }
-
- /* create a GLX context */
- cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
- isdirect = glXIsDirect(dpy, cx);
-
- swa.colormap = XCreateColormap(dpy, RootWindow(dpy, vi->screen),
- vi->visual, AllocNone);
-
- swa.border_pixel = 0;
- swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask;
- win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, 300, 300,
- 0, vi->depth, InputOutput, vi->visual,
- CWBorderPixel|CWColormap|CWEventMask, &swa);
- XStoreName(dpy, win, "overlay");
- XMapWindow(dpy, win);
-
- glXMakeCurrent(dpy, win, cx);
-
- #ifdef GLX_SGI_visual_info
- if ( glXQueryVersion(dpy, &major, &minor) == GL_FALSE ) {
- fprintf(stderr, "glXQueryVersion() failed.\n");
- exit(-1);
- }
- #ifdef GLX_VERSION_1_1
- if ( minor > 0 || major > 1 )
- s = glXQueryExtensionsString( dpy, DefaultScreen(dpy) );
- #endif
- #endif
- if (!s || !strstr(s, "GLX_SGI_visual_info")) {
- fprintf(stderr, "GLX_SGI_visual_info not supported\n");
- }
-
- initgfx();
-
- /* now set up overlay window */
-
- if (!(ovi = glXChooseVisual(dpy, DefaultScreen(dpy),
- ovAttributeList))){
- fprintf(stderr,
- "overlay: no suitable overlay index visual available\n");
- exit(EXIT_FAILURE);
- }
-
- ovcx = glXCreateContext(dpy, ovi, 0, GL_TRUE);
-
- swa.colormap = XCreateColormap(dpy, RootWindow(dpy, ovi->screen),
- ovi->visual, AllocNone);
- swa.border_pixel = 0;
- swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask;
- ovwin = XCreateWindow(dpy, win, 0, 0, 300, 300,
- 0, ovi->depth, InputOutput, ovi->visual,
- CWBorderPixel|CWColormap|CWEventMask, &swa);
- XMapWindow(dpy, ovwin);
-
- glXMakeCurrent(dpy, ovwin, ovcx);
- #ifdef GLX_EXT_visual_info
- glXGetConfig(dpy, ovi, GLX_TRANSPARENT_INDEX_VALUE_EXT,
- &transparent_index);
- #endif
- initialize_ov();
- glXMakeCurrent(dpy, win, cx);
-
- printf("hit 'spacebar' to toggle animation\n");
-
- while (1) {
- if (process_input()) {
- draw_scene();
- glXSwapBuffers(dpy, win);
- if (!isdirect) glFinish();
- }
- }
- }
-