mesa: added new linux-gallium and linux-gallium-debug configs
[mesa.git] / progs / xdemos / wincopy.c
index ccf332b9a7f082cfa11e7032f81cc5494c7ffba8..f670983a0f700272dbbab25b3968869d68bcf8d6 100644 (file)
@@ -1,10 +1,8 @@
-/* $Id: wincopy.c,v 1.1 1999/11/25 17:41:51 brianp Exp $ */
-
 /*
  * Mesa 3-D graphics library
- * Version:  3.3
+ * Version:  6.5.2
  * 
- * Copyright (C) 1999  Brian Paul   All Rights Reserved.
+ * Copyright (C) 1999-2006  Brian Paul   All Rights Reserved.
  * 
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
  */
 
 
-
+#define GL_GLEXT_PROTOTYPES
+#define GLX_GLXEXT_PROTOTYPES
 #include <GL/gl.h>
 #include <GL/glx.h>
+#include <X11/keysym.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <unistd.h>
 
 
@@ -50,10 +51,12 @@ static int ScrNum;
 static GLXContext Context;
 static Window Win[2];  /* Win[0] = source,  Win[1] = dest */
 static GLint Width[2], Height[2];
-
+static GLboolean TestClipping = GL_FALSE;
 static GLfloat Angle = 0.0;
 
+static GLboolean DrawFront = GL_FALSE;
 
+PFNGLXMAKECURRENTREADSGIPROC make_context_current = NULL;
 
 static Window
 CreateWindow(Display *dpy, int scrnum, XVisualInfo *visinfo,
@@ -98,13 +101,22 @@ static void
 Redraw(void)
 {
    /* make the first window the current one */
-   if (!glXMakeContextCurrent(Dpy, Win[0], Win[0], Context)) {
+   if (! (*make_context_current)(Dpy, Win[0], Win[0], Context)) {
       printf("glXMakeContextCurrent failed in Redraw()\n");
       return;
    }
 
    Angle += 1.0;
 
+   if (DrawFront) {
+      glDrawBuffer(GL_FRONT);
+      glReadBuffer(GL_FRONT);
+   }
+   else {
+      glDrawBuffer(GL_BACK);
+      glReadBuffer(GL_BACK);
+   }
+
    glViewport(0, 0, Width[0], Height[0]);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
@@ -112,7 +124,7 @@ Redraw(void)
    glMatrixMode(GL_MODELVIEW);
 
    glShadeModel(GL_FLAT);
-   glClearColor(0.5, 0.5, 0.5, 1.0);
+   glClearColor(0.5, 0.5, 0.5, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);
 
    /* draw blue quad */
@@ -127,33 +139,35 @@ Redraw(void)
    glEnd();
    glPopMatrix();
 
-   glXSwapBuffers(Dpy, Win[0]);
+   if (DrawFront)
+      glFinish();
+   else
+      glXSwapBuffers(Dpy, Win[0]);
 
 
    /* copy image from window 0 to window 1 */
-   if (!glXMakeContextCurrent(Dpy, Win[1], Win[0], Context)) {
+   if (!(*make_context_current)(Dpy, Win[1], Win[0], Context)) {
       printf("glXMakeContextCurrent failed in Redraw()\n");
       return;
    }
 
-   /* raster pos setup */
-   glViewport(0, 0, Width[1], Height[1]);
-   glPushMatrix();
-   glLoadIdentity();
-   glMatrixMode(GL_PROJECTION);
-   glPushMatrix();
-   glLoadIdentity();
-   glOrtho(-1, 1, -1, 1, -1, 1);
-   glRasterPos2f(-1, -1);
-
    /* copy the image between windows */
-   glDrawBuffer(GL_FRONT);
-   glCopyPixels(0, 0, Width[0], Height[0], GL_COLOR);
-   glDrawBuffer(GL_BACK);
+   glClearColor(0.0, 0.0, 0.0, 0.0);
+   glClear(GL_COLOR_BUFFER_BIT);
 
-   glPopMatrix();
-   glMatrixMode(GL_MODELVIEW);
-   glPopMatrix();
+   if (TestClipping) {
+      glWindowPos2iARB(-2, -2);
+      glCopyPixels(-2, -2, Width[0] + 4, Height[0] + 4, GL_COLOR);
+   }
+   else {
+      glWindowPos2iARB(0, 0);
+      glCopyPixels(0, 0, Width[0], Height[0], GL_COLOR);
+   }
+
+   if (DrawFront)
+      glFinish();
+   else
+      glXSwapBuffers(Dpy, Win[1]);
 }
 
 
@@ -193,7 +207,22 @@ EventLoop(void)
                Resize(event.xany.window, event.xconfigure.width, event.xconfigure.height);
                break;
             case KeyPress:
-               return;
+               {
+                  char buf[100];
+                  KeySym keySym;
+                  XComposeStatus stat;
+                  XLookupString(&event.xkey, buf, sizeof(buf), &keySym, &stat);
+                  if (keySym == XK_Escape) {
+                        /* exit */
+                        return;
+                  }
+                  else if (buf[0] == 'f') {
+                     DrawFront = !DrawFront;
+                     printf("Drawing to %s buffer\n",
+                            DrawFront ? "GL_FRONT" : "GL_BACK");
+                  }
+               }
+               break;
             default:
                /*no-op*/ ;
          }
@@ -216,6 +245,7 @@ Init(void)
                    GLX_BLUE_SIZE, 1,
                    GLX_DOUBLEBUFFER,
                    None };
+   int major, minor;
 
    Dpy = XOpenDisplay(NULL);
    if (!Dpy) {
@@ -225,6 +255,30 @@ Init(void)
 
    ScrNum = DefaultScreen(Dpy);
 
+   glXQueryVersion(Dpy, &major, &minor);
+
+   if (major * 100 + minor >= 103) {
+      make_context_current = (PFNGLXMAKECURRENTREADSGIPROC)
+         glXGetProcAddressARB( (GLubyte *) "glXMakeContextCurrent" );
+   }
+   else {
+      const char * const glxExtensions = glXQueryExtensionsString(Dpy, ScrNum);
+      const char * ext = strstr( glxExtensions, "GLX_SGI_make_current_read" );
+      const size_t len = strlen( "GLX_SGI_make_current_read" );
+      
+      if ( (ext != NULL) 
+          && ((ext[len] == ' ') || (ext[len] == '\0')) ) {
+        make_context_current = (PFNGLXMAKECURRENTREADSGIPROC) 
+            glXGetProcAddressARB( (GLubyte *) "glXMakeCurrentReadSGI" );
+      }
+   }
+
+   if (make_context_current == NULL) {
+      fprintf(stderr, "Sorry, this program requires either GLX 1.3 "
+             "or GLX_SGI_make_current_read.\n");
+      exit(1);
+   }
+
    visinfo = glXChooseVisual(Dpy, ScrNum, attrib);
    if (!visinfo) {
       printf("Unable to find RGB, double-buffered visual\n");
@@ -244,12 +298,16 @@ Init(void)
    Win[1] = CreateWindow(Dpy, ScrNum, visinfo,
                          350, 0, 300, 300, "dest window");
 
+   printf("Press Esc to exit\n");
+   printf("Press 'f' to toggle front/back buffer drawing\n");
 }
 
 
 int
 main(int argc, char *argv[])
 {
+   if (argc > 1 && strcmp(argv[1], "-clip") == 0)
+      TestClipping = GL_TRUE;
    Init();
    EventLoop();
    return 0;