home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / opengl / xlib / tvorder.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  5.5 KB  |  223 lines

  1. /*
  2.  * Copyright 1993, 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. #include <GL/glx.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <X11/keysym.h>
  21.  
  22. static int attributes[] = {
  23.     GLX_RGBA,
  24.     GLX_RED_SIZE, 1,
  25.     GLX_GREEN_SIZE, 1,
  26.     GLX_BLUE_SIZE, 1,
  27.     None,
  28. };
  29.  
  30. int width = 200, height = 200;
  31.  
  32. static void DoDisplay(void)
  33. {
  34.     static GLfloat red[] = { 1, 0, 0, 1 };
  35.     static GLfloat green[] = { 0, 1, 0, 1 };
  36.     static GLfloat black[] = { 0, 0, 0, 1 };
  37.  
  38.     /* Init two sided lighting */
  39.     glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
  40.     glMaterialfv(GL_FRONT, GL_EMISSION, red);
  41.     glMaterialfv(GL_BACK, GL_EMISSION, green);
  42.     glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, black);
  43.     glEnable(GL_LIGHTING);
  44.     glEnable(GL_LIGHT0);
  45.  
  46.     glMatrixMode(GL_PROJECTION);
  47.     glLoadIdentity();
  48.     glOrtho(-1, 1, -1, 1, 0, 1);
  49.     glMatrixMode(GL_MODELVIEW);
  50.     glLoadIdentity();
  51.  
  52.     glViewport(0, 0, width, height);
  53.  
  54.     /* Draw quad strip */
  55.     glClear(GL_COLOR_BUFFER_BIT);
  56.     glBegin(GL_QUAD_STRIP);
  57.     glVertex2f(-0.5,  0.5);
  58.     glVertex2f(-0.5, -0.5);
  59.     glVertex2f( 0.5,  0.5);
  60.     glVertex2f( 0.5, -0.5);
  61.     glVertex2f( 0.0,  0.5);
  62.     glVertex2f( 0.0, -0.5);
  63.     glEnd();
  64.     glFlush();
  65.  
  66.     {
  67.     GLfloat buf[10][4];
  68.     GLint i;
  69.  
  70.     /* Read back some of the pixels from the middle of the window */
  71.     glReadPixels(width/2 - 5, height/2, 10, 1, GL_RGBA, GL_FLOAT, buf);
  72.     printf("Pixels @ %d,%d:\n", width/2-5, height/2);
  73.     for (i = 0; i < 10; i++) {
  74.         printf("%g %g %g %g\n",
  75.            buf[i][0], buf[i][1], buf[i][2], buf[i][3]);
  76.     }
  77.     }
  78. }
  79.  
  80. static void Usage(void)
  81. {
  82.     printf("Usage: tvorder [-u]\n");
  83.     printf("   -u:  Render to unmapped window\n");
  84.     exit(-1);
  85. }
  86.  
  87. static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
  88. {
  89.     if ((e->type == MapNotify) && (e->xmap.window == (Window)arg)) {
  90.     return GL_TRUE;
  91.     }
  92.     return GL_FALSE;
  93. }
  94.  
  95. static Bool WaitForUnmapNotify(Display *d, XEvent *e, char *arg)
  96. {
  97.     if ((e->type == UnmapNotify) && (e->xmap.window == (Window)arg)) {
  98.     return GL_TRUE;
  99.     }
  100.     return GL_FALSE;
  101. }
  102.  
  103. int main(long argc, char** argv)
  104. {
  105.     XVisualInfo *vi;
  106.     Display *dpy;
  107.     Colormap cmap;
  108.     Window window;
  109.     XSetWindowAttributes swa;
  110.     GLXContext cx;
  111.     XEvent event;
  112.     GLboolean needDisplay;
  113.     GLboolean unmapped = GL_FALSE;
  114.     int i;
  115.  
  116.     for (i = 1; i < argc; i++) {
  117.         if (argv[i][0] == '-') {
  118.             switch (argv[i][1]) {
  119.               case 'u':
  120.                 unmapped = GL_TRUE;
  121.                 break;
  122.               default:
  123.                 Usage();
  124.             }
  125.         } else {
  126.             Usage();
  127.         }
  128.     }
  129.  
  130.     dpy = XOpenDisplay(0);
  131.     if (!dpy) {
  132.     fprintf(stderr, "Can't connect to display \"%s\"\n", getenv("DISPLAY"));
  133.     return -1;
  134.     }
  135.  
  136.     vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributes);
  137.     if (!vi) {
  138.     fprintf(stderr, "No singlebuffered rgba visual on \"%s\"\n",
  139.         getenv("DISPLAY"));
  140.     return -1;
  141.     }
  142.  
  143.     cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,
  144.                AllocNone);
  145.     swa.border_pixel = 0;
  146.     swa.colormap = cmap;
  147.     swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask
  148.     | KeyReleaseMask;
  149.     window = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 10, 10,
  150.                width, height,
  151.                0, vi->depth, InputOutput, vi->visual,
  152.                CWBorderPixel|CWColormap|CWEventMask, &swa);
  153.     XSetStandardProperties(dpy, window, "tvorder", "tvorder", None,
  154.                argv, argc, NULL);
  155.     XSetWMColormapWindows(dpy, window, &window, 1);
  156.     XMapWindow(dpy, window);
  157.     XIfEvent(dpy, &event, WaitForMapNotify, (char*)window);
  158.  
  159.     cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
  160.     if (!glXMakeCurrent(dpy, window, cx)) {
  161.     fprintf(stderr, "Can't make window current to context\n");
  162.     return -1;
  163.     }
  164.  
  165.     if (unmapped) {
  166.         DoDisplay();
  167.         XUnmapWindow(dpy, window);
  168.         XIfEvent(dpy, &event, WaitForUnmapNotify, (char*)window);
  169.         DoDisplay();
  170.         return 0;
  171.     }
  172.  
  173.     needDisplay = GL_TRUE;
  174.     for (;;) {
  175.     do {
  176.         XNextEvent(dpy, &event);
  177.         switch (event.type) {
  178.           case Expose:
  179.         needDisplay = GL_TRUE;
  180.         break;
  181.           case ConfigureNotify:
  182.         width = event.xconfigure.width;
  183.         height = event.xconfigure.height;
  184.         needDisplay = GL_TRUE;
  185.         break;
  186.           case KeyPress:
  187.         {
  188.             char buf[100];
  189.             int rv;
  190.             KeySym ks;
  191.  
  192.             rv = XLookupString(&event.xkey, buf, sizeof(buf), &ks, 0);
  193.             switch (ks) {
  194.               case XK_p:
  195.               case XK_P:
  196.             glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
  197.             needDisplay = GL_TRUE;
  198.             break;
  199.               case XK_l:
  200.               case XK_L:
  201.             glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  202.             needDisplay = GL_TRUE;
  203.             break;
  204.               case XK_f:
  205.               case XK_F:
  206.             glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  207.             needDisplay = GL_TRUE;
  208.             break;
  209.               case XK_Escape:
  210.             return 0;
  211.             }
  212.         }
  213.         break;
  214.         }
  215.     } while (XPending(dpy) != 0);
  216.  
  217.     if (needDisplay) {
  218.         needDisplay = GL_FALSE;
  219.         DoDisplay();
  220.     }
  221.     }
  222. }
  223.