home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / mesa-1.2.8 / samples / oglinfo.c < prev    next >
C/C++ Source or Header  |  1996-05-27  |  5KB  |  200 lines

  1. /* oglinfo.c */
  2.  
  3. /* This demo modified by BrianP to accomodate Mesa and test the GLX 1.1 functions. */
  4.  
  5.  
  6.  
  7. #include <GL/glx.h>
  8. #include <GL/gl.h>
  9. #include <GL/glu.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12.  
  13. int visual_request[] = { None }; /* don't need much of a visual */
  14.  
  15. main(int argc, char **argv)
  16. {
  17.   char *display_name = NULL;
  18.   char *string;
  19.   Display       *dpy;
  20.   int           screen_num;
  21.   int           major, minor;
  22.   int           dontcare; /* for returned arguments we don't care about */
  23.   XVisualInfo   *vis;
  24.   GLXContext    ctx;
  25.   Window        root,  win;
  26.   Colormap      cmap;
  27.   XSetWindowAttributes swa;
  28.   XEvent        event;
  29.  
  30.   /* parse arguments */
  31.   if(argc > 1)
  32.     if(!strcmp(argv[1],"-display"))
  33.       display_name = argv[2];
  34.     else {
  35.       fprintf(stderr, "Usage: %s [-display <display>]\n",argv[0]);
  36.       return -1;
  37.     }
  38.  
  39.   /* get display */
  40.   if (!(dpy = XOpenDisplay(display_name))) {
  41.     fprintf(stderr,"Error: XOpenDisplay() failed.\n");
  42.     return -1;
  43.   }
  44.  
  45.   /* does the server know about OpenGL & GLX? */
  46. #ifndef MESA
  47.   if(!XQueryExtension(dpy, "GLX", &dontcare, &dontcare, &dontcare)) {
  48.     fprintf(stderr,"This system doesn't appear to support OpenGL\n");
  49.     return -1;
  50.   }
  51. #endif
  52.  
  53.   /* find the glx version */
  54.   if(glXQueryVersion(dpy, &major, &minor))
  55.     printf("GLX Version: %d.%d\n", major, minor);
  56.   else {
  57.     fprintf(stderr, "Error: glXQueryVersion() failed.\n");
  58.     return -1;
  59.   }
  60.  
  61.   /* get screen number */
  62.   screen_num = DefaultScreen(dpy);
  63.  
  64. /* This #ifdef isn't redundant. It keeps the build from breaking
  65. ** if you are building on a machine that has an old (1.0) version
  66. ** of glx.
  67. **
  68. ** This program could still be *run* on a machine that has an old 
  69. ** version of glx, even if it was *compiled* on a version that has
  70. ** a new version.
  71. **
  72. ** If compiled on a system with an old version of glx, then it will 
  73. ** never recognize glx extensions, since that code would have been
  74. ** #ifdef'ed out.
  75. */
  76. #ifdef GLX_VERSION_1_1
  77.  
  78.   /*
  79.   ** This test guarantees that glx, on the display you are inquiring,
  80.   ** suppports glXQueryExtensionsString().
  81.   */
  82.   if(minor > 0 || major > 1)
  83.     string = (char *) glXQueryExtensionsString(dpy, screen_num);
  84.   else
  85.     string = "";
  86.  
  87.   if(string)
  88.     printf("GLX Extensions (client & server): %s\n",
  89.        string);
  90.   else {
  91.     fprintf(stderr, "Error: glXQueryExtensionsString() failed.\n");
  92.     return -1;
  93.   }
  94.  
  95.   if (minor>0 || major>1) {
  96.      printf("glXGetClientString(GLX_VENDOR): %s\n", glXGetClientString(dpy,GLX_VENDOR));
  97.      printf("glXGetClientString(GLX_VERSION): %s\n", glXGetClientString(dpy,GLX_VERSION));
  98.      printf("glXGetClientString(GLX_EXTENSIONS): %s\n", glXGetClientString(dpy,GLX_EXTENSIONS));
  99.      printf("glXQueryServerString(GLX_VENDOR): %s\n", glXQueryServerString(dpy,screen_num,GLX_VENDOR));
  100.      printf("glXQueryServerString(GLX_VERSION): %s\n", glXQueryServerString(dpy,screen_num,GLX_VERSION));
  101.      printf("glXQueryServerString(GLX_EXTENSIONS): %s\n", glXQueryServerString(dpy,screen_num,GLX_EXTENSIONS));
  102.   }
  103.  
  104.  
  105. #endif
  106.  
  107.    /* get any valid OpenGL visual */
  108.    if (!(vis = glXChooseVisual(dpy, screen_num, visual_request)))  {
  109.      fprintf(stderr,"Error: glXChooseVisual() failed.\n");
  110.      return -1;
  111.      }
  112.  
  113.    /* get context */
  114.    ctx = glXCreateContext(dpy,vis,0,GL_TRUE);
  115.  
  116.    /* root window */
  117.    root = RootWindow(dpy,vis->screen);
  118.  
  119.    /* get RGBA colormap */
  120.    cmap = XCreateColormap(dpy, root, vis->visual, AllocNone);
  121.  
  122.    /* get window */
  123.    swa.colormap = cmap;
  124.    swa.border_pixel = 0;
  125.    swa.event_mask = StructureNotifyMask;
  126.    win = XCreateWindow(dpy, root, 0, 0, 1, 1, 0, vis->depth,
  127.                InputOutput,vis->visual,
  128.                CWBorderPixel|CWColormap|CWEventMask,
  129.                &swa);
  130.  
  131.    glXMakeCurrent(dpy,win,ctx);
  132.  
  133.   string = (char *) glGetString(GL_VERSION);
  134.   if(string)
  135. #ifdef MESA
  136.     printf("Mesa Version: %s\n", string);
  137. #else
  138.     printf("OpenGL Version: %s\n", string);
  139. #endif
  140.   else {
  141.     fprintf(stderr, "Error: glGetString(GL_VERSION) failed.\n");
  142.     return -1;
  143.   }
  144.  
  145.   string = (char *) glGetString(GL_EXTENSIONS);
  146.  
  147.   if(string)
  148. #ifdef MESA
  149.     printf("Mesa Extensions: %s\n", string);
  150. #else
  151.     printf("OpenGL Extensions: %s\n", string);
  152. #endif
  153.   else {
  154.     fprintf(stderr, "Error: glGetString(GL_EXTENSIONS) failed.\n");
  155.     return -1;
  156.   }
  157.  
  158. /*
  159. ** This #ifdef prevents a build failure if you compile on an a
  160. ** machine with an old GLU library. 
  161. **
  162. ** If you build on a pre GLU 1.1 machine, you will never be able
  163. ** to get glu info, even if you run on a GLU 1.1 or latter machine,
  164. ** since the code has been #ifdef'ed out.
  165. */
  166. #ifdef GLU_VERSION_1_1
  167.  
  168.   /*
  169.   ** If the glx version is 1.1 or latter, gluGetString() is guaranteed
  170.   ** to exist.
  171.   */
  172.   if(minor > 0 || major > 1)
  173.     string = (char *) gluGetString(GLU_VERSION);
  174.   else
  175.     string = "1.0";
  176.  
  177.   if(string)
  178.     printf("GLU Version: %s\n", string);
  179.   else {
  180.     fprintf(stderr, "Error: gluGetString(GLU_VERSION) failed.\n");
  181.     return -1;
  182.   }
  183.   
  184.   if(minor > 0 || major > 1)
  185.     string = (char *) gluGetString(GLU_EXTENSIONS);
  186.   else
  187.     string = "";
  188.  
  189.   if(string)
  190.     printf("GLU Extensions: %s\n", string);
  191.   else {
  192.     fprintf(stderr, "Error: gluGetString(GLU_EXTENSIONS) failed.\n");
  193.     return -1;
  194.   }
  195.  
  196.  
  197. #endif
  198.  
  199. }
  200.