home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / qt3_emx.zip / examples / opengl / overlay_x11 / utilities / glxvisuals / glxvisuals.c next >
Encoding:
C/C++ Source or Header  |  2001-05-07  |  4.9 KB  |  148 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1996. */
  3.  
  4. /* This program is freely distributable without licensing fees 
  5.    and is provided without guarantee or warrantee expressed or 
  6.    implied. This program is -not- in the public domain. */
  7.  
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <X11/Xlib.h>
  11. #include <GL/glx.h>
  12.  
  13. static char *ClassOf(int c);
  14. static char *Format(int n, int w);
  15.  
  16. void
  17. main(int argc, char *argv[])
  18. {
  19.   Display *dpy;
  20.   XVisualInfo match, *visualList, *vi, *visualToTry;
  21.   int errorBase, eventBase, major, minor, found;
  22.   int glxCapable, bufferSize, level, renderType, doubleBuffer, stereo,
  23.     auxBuffers, redSize, greenSize, blueSize, alphaSize, depthSize,
  24.     stencilSize, acRedSize, acGreenSize, acBlueSize, acAlphaSize;
  25.  
  26.   dpy = XOpenDisplay(NULL);
  27.   if (!dpy) {
  28.     fprintf(stderr, "Could not connect to %s.\n", XDisplayName(NULL));
  29.     exit(1);
  30.   }
  31.   if (glXQueryExtension(dpy, &errorBase, &eventBase) == False) {
  32.     fprintf(stderr, "OpenGL not supported by X server.\n");
  33.     exit(1);
  34.   }
  35.  
  36.   glXQueryVersion(dpy, &major, &minor);
  37.   printf("display: %s\n", XDisplayName(NULL));
  38.   printf("using GLX version: %d.%d\n\n", major, minor);
  39.  
  40.   match.screen = DefaultScreen(dpy);
  41.   visualList = XGetVisualInfo(dpy, VisualScreenMask, &match, &found);
  42.  
  43.   printf("   visual     bf lv rg d st  r  g  b a   ax dp st accum buffs\n");
  44.   printf(" id dep cl    sz l  ci b ro sz sz sz sz  bf th cl  r  g  b  a\n");
  45.   printf("-------------------------------------------------------------\n");
  46.  
  47.   visualToTry = NULL;
  48.   for(vi = visualList; found > 0; found--, vi++) {
  49.     glXGetConfig(dpy, vi, GLX_USE_GL, &glxCapable);
  50.     if (glxCapable) {
  51.       printf("0x%x %2d %s", vi->visualid, vi->depth, ClassOf(vi->class));
  52.       glXGetConfig(dpy, vi, GLX_BUFFER_SIZE, &bufferSize);
  53.       glXGetConfig(dpy, vi, GLX_LEVEL, &level);
  54.       glXGetConfig(dpy, vi, GLX_RGBA, &renderType);
  55.       glXGetConfig(dpy, vi, GLX_DOUBLEBUFFER, &doubleBuffer);
  56.       glXGetConfig(dpy, vi, GLX_STEREO, &stereo);
  57.       glXGetConfig(dpy, vi, GLX_AUX_BUFFERS, &auxBuffers);
  58.       glXGetConfig(dpy, vi, GLX_RED_SIZE, &redSize);
  59.       glXGetConfig(dpy, vi, GLX_GREEN_SIZE, &greenSize);
  60.       glXGetConfig(dpy, vi, GLX_BLUE_SIZE, &blueSize);
  61.       glXGetConfig(dpy, vi, GLX_ALPHA_SIZE, &alphaSize);
  62.       glXGetConfig(dpy, vi, GLX_DEPTH_SIZE, &depthSize);
  63.       glXGetConfig(dpy, vi, GLX_STENCIL_SIZE, &stencilSize);
  64.       glXGetConfig(dpy, vi, GLX_ACCUM_RED_SIZE, &acRedSize);
  65.       glXGetConfig(dpy, vi, GLX_ACCUM_GREEN_SIZE, &acGreenSize);
  66.       glXGetConfig(dpy, vi, GLX_ACCUM_BLUE_SIZE, &acBlueSize);
  67.       glXGetConfig(dpy, vi, GLX_ACCUM_ALPHA_SIZE, &acAlphaSize);
  68.       printf("    %2s %2s %1s  %1s  %1s ",
  69.         Format(bufferSize, 2), Format(level, 2),
  70.         renderType ? "r" : "c",
  71.     doubleBuffer ? "y" : ".", 
  72.     stereo ? "y" : ".");
  73.       printf("%2s %2s %2s %2s ",
  74.         Format(redSize, 2), Format(greenSize, 2),
  75.     Format(blueSize, 2), Format(alphaSize, 2));
  76.       printf("%2s %2s %2s %2s %2s %2s %2s",
  77.         Format(auxBuffers, 2), Format(depthSize, 2), Format(stencilSize, 2),
  78.         Format(acRedSize, 2), Format(acGreenSize, 2),
  79.         Format(acBlueSize, 2), Format(acAlphaSize, 2));
  80.       printf("\n");
  81.       visualToTry = vi;
  82.     }
  83.   }
  84.  
  85.   if (visualToTry) {
  86.     GLXContext context;
  87.     Window window;
  88.     Colormap colormap;
  89.     XSetWindowAttributes swa;
  90.  
  91.     context = glXCreateContext(dpy, visualToTry, 0, GL_TRUE);
  92.     colormap = XCreateColormap(dpy,
  93.       RootWindow(dpy, visualToTry->screen),
  94.       visualToTry->visual, AllocNone);
  95.     swa.colormap = colormap;
  96.     swa.border_pixel = 0;
  97.     window = XCreateWindow(dpy, RootWindow(dpy, visualToTry->screen), 0, 0, 100, 100,
  98.       0, visualToTry->depth, InputOutput, visualToTry->visual,
  99.       CWBorderPixel | CWColormap, &swa);
  100.     glXMakeCurrent(dpy, window, context);
  101.     printf("\n");
  102.     printf("OpenGL vendor string: %s\n", glGetString(GL_VENDOR));
  103.     printf("OpenGL renderer string: %s\n", glGetString(GL_RENDERER));
  104.     printf("OpenGL version string: %s\n", glGetString(GL_VERSION));
  105.     if (glXIsDirect(dpy, context))
  106.       printf("direct rendering: supported\n");
  107.     printf( "GL extensions: '%s'\n\n", glGetString(GL_EXTENSIONS) );
  108. #if defined(GLX_VERSION_1_1)
  109.     printf( "GLX extensions: '%s'\n\n", glXQueryExtensionsString( dpy, visualToTry->screen ) );
  110. #endif
  111.  
  112.   } else
  113.     printf("No GLX-capable visuals!\n");
  114.   XFree(visualList);
  115. }
  116.  
  117. static char *
  118. ClassOf(int c)
  119. {
  120.   switch (c) {
  121.   case StaticGray:   return "sg";
  122.   case GrayScale:    return "gs";
  123.   case StaticColor:  return "sc";
  124.   case PseudoColor:  return "pc";
  125.   case TrueColor:    return "tc";
  126.   case DirectColor:  return "dc";
  127.   default:           return "??";
  128.   }
  129. }
  130.  
  131. static char *
  132. Format(int n, int w)
  133. {
  134.   static char buffer[256];
  135.   static int bufptr;
  136.   char *buf;
  137.  
  138.   if (bufptr >= sizeof(buffer) - w)
  139.     bufptr = 0;
  140.   buf = buffer + bufptr;
  141.   if (n == 0)
  142.     sprintf(buf, "%*s", w, ".");
  143.   else
  144.     sprintf(buf, "%*d", w, n);
  145.   bufptr += w + 1;
  146.   return buf;
  147. }
  148.