2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2004 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 * This program opens two GLX windows, renders into one and uses
28 * glCopyPixels to copy the image from the first window into the
29 * second by means of the GLX 1.3 function glxMakeContextCurrent().
30 * This function works just like the glXMakeCurrentReadSGI() function
31 * in the GLX_SGI_make_current_read extension.
35 #define GLX_GLXEXT_PROTOTYPES
38 #include <X11/keysym.h>
45 #ifdef GLX_VERSION_1_3
50 static GLXContext Context
;
51 static Window Win
[2]; /* Win[0] = source, Win[1] = dest */
52 static GLint Width
[2], Height
[2];
54 static GLfloat Angle
= 0.0;
56 static GLboolean DrawFront
= GL_FALSE
;
58 PFNGLXMAKECURRENTREADSGIPROC make_context_current
= NULL
;
61 CreateWindow(Display
*dpy
, int scrnum
, XVisualInfo
*visinfo
,
62 int xpos
, int ypos
, int width
, int height
,
66 XSetWindowAttributes attr
;
70 root
= RootWindow(dpy
, scrnum
);
72 /* window attributes */
73 attr
.background_pixel
= 0;
74 attr
.border_pixel
= 0;
75 attr
.colormap
= XCreateColormap(dpy
, root
, visinfo
->visual
, AllocNone
);
76 attr
.event_mask
= StructureNotifyMask
| ExposureMask
| KeyPressMask
;
77 mask
= CWBackPixel
| CWBorderPixel
| CWColormap
| CWEventMask
;
79 win
= XCreateWindow(dpy
, root
, xpos
, ypos
, width
, height
,
80 0, visinfo
->depth
, InputOutput
,
81 visinfo
->visual
, mask
, &attr
);
86 sizehints
.width
= width
;
87 sizehints
.height
= height
;
88 sizehints
.flags
= USSize
| USPosition
;
89 XSetNormalHints(dpy
, win
, &sizehints
);
90 XSetStandardProperties(dpy
, win
, name
, name
,
91 None
, (char **)NULL
, 0, &sizehints
);
102 /* make the first window the current one */
103 if (! (*make_context_current
)(Dpy
, Win
[0], Win
[0], Context
)) {
104 printf("glXMakeContextCurrent failed in Redraw()\n");
111 glDrawBuffer(GL_FRONT
);
112 glReadBuffer(GL_FRONT
);
115 glDrawBuffer(GL_BACK
);
116 glReadBuffer(GL_BACK
);
119 glViewport(0, 0, Width
[0], Height
[0]);
120 glMatrixMode(GL_PROJECTION
);
122 glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
123 glMatrixMode(GL_MODELVIEW
);
125 glShadeModel(GL_FLAT
);
126 glClearColor(0.5, 0.5, 0.5, 1.0);
127 glClear(GL_COLOR_BUFFER_BIT
);
130 glColor3f(0.3, 0.3, 1.0);
132 glRotatef(Angle
, 0, 0, 1);
134 glVertex2f(-0.5, -0.25);
135 glVertex2f( 0.5, -0.25);
136 glVertex2f( 0.5, 0.25);
137 glVertex2f(-0.5, 0.25);
144 glXSwapBuffers(Dpy
, Win
[0]);
147 /* copy image from window 0 to window 1 */
148 if (!(*make_context_current
)(Dpy
, Win
[1], Win
[0], Context
)) {
149 printf("glXMakeContextCurrent failed in Redraw()\n");
153 /* raster pos setup */
154 glViewport(0, 0, Width
[1], Height
[1]);
157 glMatrixMode(GL_PROJECTION
);
160 glOrtho(-1, 1, -1, 1, -1, 1);
161 glRasterPos2f(-1, -1);
163 /* copy the image between windows */
164 glCopyPixels(0, 0, Width
[0], Height
[0], GL_COLOR
);
167 glMatrixMode(GL_MODELVIEW
);
173 glXSwapBuffers(Dpy
, Win
[1]);
179 Resize(Window win
, unsigned int width
, unsigned int height
)
190 if (!glXMakeCurrent(Dpy
, Win
[i
], Context
)) {
191 printf("glXMakeCurrent failed in Resize()\n");
203 if (XPending(Dpy
) > 0) {
204 XNextEvent( Dpy
, &event
);
205 switch (event
.type
) {
209 case ConfigureNotify
:
210 Resize(event
.xany
.window
, event
.xconfigure
.width
, event
.xconfigure
.height
);
217 XLookupString(&event
.xkey
, buf
, sizeof(buf
), &keySym
, &stat
);
218 if (keySym
== XK_Escape
) {
222 else if (buf
[0] == 'f') {
223 DrawFront
= !DrawFront
;
224 printf("Drawing to %s buffer\n",
225 DrawFront
? "GL_FRONT" : "GL_BACK");
244 XVisualInfo
*visinfo
;
245 int attrib
[] = { GLX_RGBA
,
253 Dpy
= XOpenDisplay(NULL
);
255 printf("Couldn't open default display!\n");
259 ScrNum
= DefaultScreen(Dpy
);
261 glXQueryVersion(Dpy
, &major
, &minor
);
263 if (major
* 100 + minor
>= 103) {
264 make_context_current
= (PFNGLXMAKECURRENTREADSGIPROC
)
265 glXGetProcAddressARB( (GLubyte
*) "glXMakeContextCurrent" );
268 const char * const glxExtensions
= glXQueryExtensionsString(Dpy
, ScrNum
);
269 const char * ext
= strstr( glxExtensions
, "GLX_SGI_make_current_read" );
270 const size_t len
= strlen( "GLX_SGI_make_current_read" );
273 && ((ext
[len
] == ' ') || (ext
[len
] == '\0')) ) {
274 make_context_current
= (PFNGLXMAKECURRENTREADSGIPROC
)
275 glXGetProcAddressARB( (GLubyte
*) "glXMakeCurrentReadSGI" );
279 if (make_context_current
== NULL
) {
280 fprintf(stderr
, "Sorry, this program requires either GLX 1.3 "
281 "or GLX_SGI_make_current_read.\n");
285 visinfo
= glXChooseVisual(Dpy
, ScrNum
, attrib
);
287 printf("Unable to find RGB, double-buffered visual\n");
291 Context
= glXCreateContext(Dpy
, visinfo
, NULL
, True
);
293 printf("Couldn't create GLX context\n");
298 Win
[0] = CreateWindow(Dpy
, ScrNum
, visinfo
,
299 0, 0, 300, 300, "source window");
301 Win
[1] = CreateWindow(Dpy
, ScrNum
, visinfo
,
302 350, 0, 300, 300, "dest window");
304 printf("Press Esc to exit\n");
305 printf("Press 'f' to toggle front/back buffer drawing\n");
310 main(int argc
, char *argv
[])
322 main(int argc
, char *argv
[])
324 printf("This program requires GLX 1.3!\n");
329 #endif /* GLX_VERSION_1_3 */