home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / examples / glx_extensions / visual_info.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  6.3 KB  |  264 lines

  1. /*
  2.  * Copyright 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. /* visual_info.c - simple double buffered RGBA xlib program which 
  18.  *    rotates an object.
  19.  *    
  20.  *    An overlay window is created as a child of the main window and it 
  21.  *    is managed as part of the parent window.  The overlay 
  22.  *    window/context creation uses the visual_info extension to 
  23.  *    find a visual with a transparent pixel.
  24.  */
  25. /* compile: cc -o visual_info visual_info.c -lGLU -lGL -lX11 */
  26.  
  27. #include <GL/glx.h>
  28. #include <GL/glu.h>
  29. #include <X11/keysym.h>
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32.  
  33. static int attributeList[] = {
  34.     GLX_RGBA, GLX_RED_SIZE, 1, GLX_DOUBLEBUFFER, None };
  35. static int ovAttributeList[] = {
  36.     GLX_BUFFER_SIZE, 2,
  37.     GLX_LEVEL, 1,
  38. #ifdef    GLX_EXT_visual_info
  39.     GLX_TRANSPARENT_TYPE_EXT, GLX_TRANSPARENT_INDEX_EXT,
  40. #endif
  41.     None };
  42. static Window win, ovwin;
  43. static GLXContext cx, ovcx;
  44. static Display *dpy;
  45.  
  46. static Bool animate;
  47.  
  48. static int transparent_index = 0;
  49.  
  50. static void
  51. initgfx(void) {
  52.     glShadeModel(GL_FLAT);
  53.     glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  54.     gluPerspective(40., 3.0/2.0, 0.001, 100000.0);
  55.     glTranslatef(0.0, 0.0, -3.0);
  56.     glClearColor(0.2,0.2,0.2,0.);
  57. }
  58.  
  59. static void
  60. side(void) { /* make a square translated 0.5 in the z direction */
  61.     glPushMatrix();
  62.     glTranslatef(0.0,0.0,0.5);
  63.     glRectf(-0.5,-0.5,0.5,0.5);
  64.     glPopMatrix();
  65. }
  66.  
  67. static void
  68. cube(void) { /* make a cube out of 4 squares */
  69.     glPushMatrix();
  70.     side();
  71.     glRotatef(90.,1.,0.,0.);
  72.     side();
  73.     glRotatef(90.,1.,0.,0.);
  74.     side();
  75.     glRotatef(90.,1.,0.,0.);
  76.     side();
  77.     glPopMatrix();
  78. }
  79.  
  80. static void
  81. draw_scene(void) {
  82.     static float rot = 0.;
  83.  
  84.     glClear(GL_COLOR_BUFFER_BIT);
  85.     glColor3f(.1, .1, .8);
  86.     glPushMatrix();
  87.     if (animate && (rot += 5.) > 360.) rot -= 360.;
  88.     glRotatef(rot,0.,1.,0.);
  89.     cube();
  90.     glScalef(0.3,0.3,0.3);
  91.     glColor3f(.8, .8, .1);
  92.     cube();
  93.     glPopMatrix();
  94. }
  95.  
  96. /*
  97.  * the amount of work being done doesn't justify the need for
  98.  * a separate overlay initialization and redraw routines so we
  99.  * do it all together. XXX doesn't allocate and initialize any
  100.  * color cells.
  101.  */
  102. static void
  103. initialize_ov(void) {
  104.     int i;
  105.     /* simply draw a grid of 10 equally spaced lines */
  106.     glLoadIdentity();
  107.     glOrtho(0., 11., 0., 11., -1., 1.);
  108.     glClearIndex(transparent_index);
  109.     glClear(GL_COLOR_BUFFER_BIT);
  110.     glIndexi(1);
  111.     glBegin(GL_LINES);
  112.     for(i = 0; i < 11; i++) {
  113.         glVertex2f(0.5+i, 0.0);
  114.         glVertex2f(0.5+i, 11.0);
  115.         glVertex2f(0.0,  0.5+i);
  116.         glVertex2f(11.0, 0.5+i);
  117.     }
  118.     glEnd();
  119. }
  120.  
  121. static Bool
  122. process_input(void) {
  123.     XEvent event;
  124.     static Bool mapped;
  125.     Bool viewport_ov = 0, redraw_ov = 0, redraw = 0;
  126.     GLsizei w, h;
  127.  
  128.     if(XPending(dpy) || !mapped || !animate) {
  129.         char buf[31];
  130.         KeySym keysym;
  131.  
  132.         XNextEvent(dpy, &event);
  133.         switch(event.type) {
  134.         case Expose:
  135.             redraw_ov = redraw = 1;
  136.             break;
  137.         case ConfigureNotify:
  138.             w = event.xconfigure.width;
  139.             h = event.xconfigure.height;
  140.             glViewport(0, 0, w, h);
  141.             redraw_ov = redraw = 1;
  142.             viewport_ov = 1;
  143.             break;
  144.         case MapNotify:
  145.             mapped = 1; 
  146.             break;
  147.         case UnmapNotify:
  148.             mapped = 0; 
  149.             break;
  150.         case KeyPress:
  151.             (void) XLookupString(&event.xkey, buf, sizeof(buf), 
  152.                         &keysym, NULL);
  153.             switch (keysym) {
  154.             case XK_Escape:
  155.                 exit(EXIT_SUCCESS);
  156.             case XK_space:
  157.                 animate ^= 1; 
  158.                 break;
  159.             default:
  160.                 break;
  161.             }
  162.         default:
  163.             break;
  164.         }
  165.     }
  166.     if (redraw_ov) {
  167.         glXMakeCurrent(dpy, ovwin, ovcx);
  168.         if (viewport_ov) {
  169.             XResizeWindow(dpy, ovwin, w, h);
  170.             glViewport(0, 0, w, h);
  171.         }
  172.         initialize_ov();
  173.         glXMakeCurrent(dpy, win, cx);
  174.     }
  175.     return redraw | animate;
  176. }
  177.  
  178. int
  179. main(int argc, char **argv) {
  180.     XVisualInfo *vi, *ovi;
  181.     XSetWindowAttributes swa;
  182.     Bool isdirect;
  183.     char *s = NULL;
  184.     int             major, minor;
  185.  
  186.     dpy = XOpenDisplay(0);
  187.  
  188.     if (!(vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributeList))) {
  189.         fprintf(stderr, "overlay: no suitable RGB visual available\n");
  190.         exit(EXIT_FAILURE);
  191.     }
  192.  
  193.     /* create a GLX context */
  194.     cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
  195.     isdirect = glXIsDirect(dpy, cx);
  196.  
  197.     swa.colormap = XCreateColormap(dpy, RootWindow(dpy, vi->screen),
  198.         vi->visual, AllocNone);
  199.  
  200.     swa.border_pixel = 0;
  201.     swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask;
  202.     win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, 300, 300,
  203.         0, vi->depth, InputOutput, vi->visual,
  204.         CWBorderPixel|CWColormap|CWEventMask, &swa);
  205.     XStoreName(dpy, win, "overlay");
  206.     XMapWindow(dpy, win);
  207.  
  208.     glXMakeCurrent(dpy, win, cx);
  209.  
  210. #ifdef GLX_SGI_visual_info
  211.     if ( glXQueryVersion(dpy, &major, &minor) == GL_FALSE ) {
  212.         fprintf(stderr, "glXQueryVersion() failed.\n");
  213.         exit(-1);
  214.     }
  215. #ifdef GLX_VERSION_1_1
  216.     if ( minor > 0 || major > 1 )
  217.         s = glXQueryExtensionsString( dpy, DefaultScreen(dpy) );
  218. #endif
  219. #endif
  220.     if (!s || !strstr(s, "GLX_SGI_visual_info")) {
  221.         fprintf(stderr, "GLX_SGI_visual_info not supported\n");
  222.     }
  223.  
  224.     initgfx();
  225.  
  226.     /* now set up overlay window */
  227.  
  228.     if (!(ovi = glXChooseVisual(dpy, DefaultScreen(dpy), 
  229.                     ovAttributeList))){
  230.         fprintf(stderr, 
  231.              "overlay: no suitable overlay index visual available\n");
  232.         exit(EXIT_FAILURE);
  233.     }
  234.  
  235.     ovcx = glXCreateContext(dpy, ovi, 0, GL_TRUE);
  236.  
  237.     swa.colormap = XCreateColormap(dpy, RootWindow(dpy, ovi->screen),
  238.         ovi->visual, AllocNone);
  239.     swa.border_pixel = 0;
  240.     swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask;
  241.     ovwin = XCreateWindow(dpy, win, 0, 0, 300, 300,
  242.         0, ovi->depth, InputOutput, ovi->visual,
  243.         CWBorderPixel|CWColormap|CWEventMask, &swa);
  244.     XMapWindow(dpy, ovwin);
  245.  
  246.     glXMakeCurrent(dpy, ovwin, ovcx);
  247. #ifdef    GLX_EXT_visual_info
  248.     glXGetConfig(dpy, ovi, GLX_TRANSPARENT_INDEX_VALUE_EXT, 
  249.             &transparent_index);
  250. #endif
  251.     initialize_ov();
  252.     glXMakeCurrent(dpy, win, cx);
  253.  
  254.     printf("hit 'spacebar' to toggle animation\n");
  255.  
  256.     while (1) {
  257.         if (process_input()) {
  258.             draw_scene();
  259.             glXSwapBuffers(dpy, win);
  260.             if (!isdirect) glFinish();
  261.         }
  262.     }
  263. }
  264.