Brian Paul | b5e4a16 | 1999-11-25 17:41:51 +0000 | [diff] [blame^] | 1 | /* $Id: wincopy.c,v 1.1 1999/11/25 17:41:51 brianp Exp $ */ |
| 2 | |
| 3 | /* |
| 4 | * Mesa 3-D graphics library |
| 5 | * Version: 3.3 |
| 6 | * |
| 7 | * Copyright (C) 1999 Brian Paul All Rights Reserved. |
| 8 | * |
| 9 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 10 | * copy of this software and associated documentation files (the "Software"), |
| 11 | * to deal in the Software without restriction, including without limitation |
| 12 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 13 | * and/or sell copies of the Software, and to permit persons to whom the |
| 14 | * Software is furnished to do so, subject to the following conditions: |
| 15 | * |
| 16 | * The above copyright notice and this permission notice shall be included |
| 17 | * in all copies or substantial portions of the Software. |
| 18 | * |
| 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 20 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 22 | * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN |
| 23 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 24 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 25 | */ |
| 26 | |
| 27 | |
| 28 | /* |
| 29 | * This program opens two GLX windows, renders into one and uses |
| 30 | * glCopyPixels to copy the image from the first window into the |
| 31 | * second by means of the GLX 1.3 function glxMakeContextCurrent(). |
| 32 | * This function works just like the glXMakeCurrentReadSGI() function |
| 33 | * in the GLX_SGI_make_current_read extension. |
| 34 | */ |
| 35 | |
| 36 | |
| 37 | |
| 38 | #include <GL/gl.h> |
| 39 | #include <GL/glx.h> |
| 40 | #include <stdio.h> |
| 41 | #include <stdlib.h> |
| 42 | #include <unistd.h> |
| 43 | |
| 44 | |
| 45 | #ifdef GLX_VERSION_1_3 |
| 46 | |
| 47 | |
| 48 | static Display *Dpy; |
| 49 | static int ScrNum; |
| 50 | static GLXContext Context; |
| 51 | static Window Win[2]; /* Win[0] = source, Win[1] = dest */ |
| 52 | static GLint Width[2], Height[2]; |
| 53 | |
| 54 | static GLfloat Angle = 0.0; |
| 55 | |
| 56 | |
| 57 | |
| 58 | static Window |
| 59 | CreateWindow(Display *dpy, int scrnum, XVisualInfo *visinfo, |
| 60 | int xpos, int ypos, int width, int height, |
| 61 | const char *name) |
| 62 | { |
| 63 | Window win; |
| 64 | XSetWindowAttributes attr; |
| 65 | unsigned long mask; |
| 66 | Window root; |
| 67 | |
| 68 | root = RootWindow(dpy, scrnum); |
| 69 | |
| 70 | /* window attributes */ |
| 71 | attr.background_pixel = 0; |
| 72 | attr.border_pixel = 0; |
| 73 | attr.colormap = XCreateColormap(dpy, root, visinfo->visual, AllocNone); |
| 74 | attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask; |
| 75 | mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask; |
| 76 | |
| 77 | win = XCreateWindow(dpy, root, xpos, ypos, width, height, |
| 78 | 0, visinfo->depth, InputOutput, |
| 79 | visinfo->visual, mask, &attr); |
| 80 | if (win) { |
| 81 | XSizeHints sizehints; |
| 82 | sizehints.x = xpos; |
| 83 | sizehints.y = ypos; |
| 84 | sizehints.width = width; |
| 85 | sizehints.height = height; |
| 86 | sizehints.flags = USSize | USPosition; |
| 87 | XSetNormalHints(dpy, win, &sizehints); |
| 88 | XSetStandardProperties(dpy, win, name, name, |
| 89 | None, (char **)NULL, 0, &sizehints); |
| 90 | |
| 91 | XMapWindow(dpy, win); |
| 92 | } |
| 93 | return win; |
| 94 | } |
| 95 | |
| 96 | |
| 97 | static void |
| 98 | Redraw(void) |
| 99 | { |
| 100 | /* make the first window the current one */ |
| 101 | if (!glXMakeContextCurrent(Dpy, Win[0], Win[0], Context)) { |
| 102 | printf("glXMakeContextCurrent failed in Redraw()\n"); |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | Angle += 1.0; |
| 107 | |
| 108 | glViewport(0, 0, Width[0], Height[0]); |
| 109 | glMatrixMode(GL_PROJECTION); |
| 110 | glLoadIdentity(); |
| 111 | glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); |
| 112 | glMatrixMode(GL_MODELVIEW); |
| 113 | |
| 114 | glShadeModel(GL_FLAT); |
| 115 | glClearColor(0.5, 0.5, 0.5, 1.0); |
| 116 | glClear(GL_COLOR_BUFFER_BIT); |
| 117 | |
| 118 | /* draw blue quad */ |
| 119 | glColor3f(0.3, 0.3, 1.0); |
| 120 | glPushMatrix(); |
| 121 | glRotatef(Angle, 0, 0, 1); |
| 122 | glBegin(GL_POLYGON); |
| 123 | glVertex2f(-0.5, -0.25); |
| 124 | glVertex2f( 0.5, -0.25); |
| 125 | glVertex2f( 0.5, 0.25); |
| 126 | glVertex2f(-0.5, 0.25); |
| 127 | glEnd(); |
| 128 | glPopMatrix(); |
| 129 | |
| 130 | glXSwapBuffers(Dpy, Win[0]); |
| 131 | |
| 132 | |
| 133 | /* copy image from window 0 to window 1 */ |
| 134 | if (!glXMakeContextCurrent(Dpy, Win[1], Win[0], Context)) { |
| 135 | printf("glXMakeContextCurrent failed in Redraw()\n"); |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | /* raster pos setup */ |
| 140 | glViewport(0, 0, Width[1], Height[1]); |
| 141 | glPushMatrix(); |
| 142 | glLoadIdentity(); |
| 143 | glMatrixMode(GL_PROJECTION); |
| 144 | glPushMatrix(); |
| 145 | glLoadIdentity(); |
| 146 | glOrtho(-1, 1, -1, 1, -1, 1); |
| 147 | glRasterPos2f(-1, -1); |
| 148 | |
| 149 | /* copy the image between windows */ |
| 150 | glDrawBuffer(GL_FRONT); |
| 151 | glCopyPixels(0, 0, Width[0], Height[0], GL_COLOR); |
| 152 | glDrawBuffer(GL_BACK); |
| 153 | |
| 154 | glPopMatrix(); |
| 155 | glMatrixMode(GL_MODELVIEW); |
| 156 | glPopMatrix(); |
| 157 | } |
| 158 | |
| 159 | |
| 160 | |
| 161 | static void |
| 162 | Resize(Window win, unsigned int width, unsigned int height) |
| 163 | { |
| 164 | int i; |
| 165 | if (win == Win[0]) { |
| 166 | i = 0; |
| 167 | } |
| 168 | else { |
| 169 | i = 1; |
| 170 | } |
| 171 | Width[i] = width; |
| 172 | Height[i] = height; |
| 173 | if (!glXMakeCurrent(Dpy, Win[i], Context)) { |
| 174 | printf("glXMakeCurrent failed in Resize()\n"); |
| 175 | return; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | |
| 180 | |
| 181 | static void |
| 182 | EventLoop(void) |
| 183 | { |
| 184 | XEvent event; |
| 185 | while (1) { |
| 186 | if (XPending(Dpy) > 0) { |
| 187 | XNextEvent( Dpy, &event ); |
| 188 | switch (event.type) { |
| 189 | case Expose: |
| 190 | Redraw(); |
| 191 | break; |
| 192 | case ConfigureNotify: |
| 193 | Resize(event.xany.window, event.xconfigure.width, event.xconfigure.height); |
| 194 | break; |
| 195 | case KeyPress: |
| 196 | return; |
| 197 | default: |
| 198 | /*no-op*/ ; |
| 199 | } |
| 200 | } |
| 201 | else { |
| 202 | /* animate */ |
| 203 | Redraw(); |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | |
| 209 | static void |
| 210 | Init(void) |
| 211 | { |
| 212 | XVisualInfo *visinfo; |
| 213 | int attrib[] = { GLX_RGBA, |
| 214 | GLX_RED_SIZE, 1, |
| 215 | GLX_GREEN_SIZE, 1, |
| 216 | GLX_BLUE_SIZE, 1, |
| 217 | GLX_DOUBLEBUFFER, |
| 218 | None }; |
| 219 | |
| 220 | Dpy = XOpenDisplay(NULL); |
| 221 | if (!Dpy) { |
| 222 | printf("Couldn't open default display!\n"); |
| 223 | exit(1); |
| 224 | } |
| 225 | |
| 226 | ScrNum = DefaultScreen(Dpy); |
| 227 | |
| 228 | visinfo = glXChooseVisual(Dpy, ScrNum, attrib); |
| 229 | if (!visinfo) { |
| 230 | printf("Unable to find RGB, double-buffered visual\n"); |
| 231 | exit(1); |
| 232 | } |
| 233 | |
| 234 | Context = glXCreateContext(Dpy, visinfo, NULL, True); |
| 235 | if (!Context) { |
| 236 | printf("Couldn't create GLX context\n"); |
| 237 | exit(1); |
| 238 | } |
| 239 | |
| 240 | |
| 241 | Win[0] = CreateWindow(Dpy, ScrNum, visinfo, |
| 242 | 0, 0, 300, 300, "source window"); |
| 243 | |
| 244 | Win[1] = CreateWindow(Dpy, ScrNum, visinfo, |
| 245 | 350, 0, 300, 300, "dest window"); |
| 246 | |
| 247 | } |
| 248 | |
| 249 | |
| 250 | int |
| 251 | main(int argc, char *argv[]) |
| 252 | { |
| 253 | Init(); |
| 254 | EventLoop(); |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | |
| 259 | #else |
| 260 | |
| 261 | |
| 262 | int |
| 263 | main(int argc, char *argv[]) |
| 264 | { |
| 265 | printf("This program requires GLX 1.3!\n"); |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | |
| 270 | #endif /* GLX_VERSION_1_3 */ |