2 * Create N GLX windows/contexts and render to them in round-robin order.
3 * Also, have the contexts share all texture objects.
4 * Press 'd' to delete a texture, 'u' to unbind it.
6 * Copyright (C) 2000 Brian Paul All Rights Reserved.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 #include <X11/keysym.h>
38 * Each display/window/context:
41 char DisplayName
[1000];
53 static struct head Heads
[MAX_HEADS
];
54 static int NumHeads
= 0;
55 static GLboolean SwapSeparate
= GL_TRUE
;
56 static GLuint TexObj
= 0;
60 Error(const char *display
, const char *msg
)
62 fprintf(stderr
, "Error on display %s - %s\n", XDisplayName(display
), msg
);
68 AddHead(const char *displayName
, const char *name
)
73 int attrib
[] = { GLX_RGBA
,
80 XSetWindowAttributes attr
;
84 int width
= 90, height
= 90;
85 int xpos
= 0, ypos
= 0;
87 if (NumHeads
>= MAX_HEADS
)
90 dpy
= XOpenDisplay(displayName
);
92 Error(displayName
, "Unable to open display");
96 scrnum
= DefaultScreen(dpy
);
97 root
= RootWindow(dpy
, scrnum
);
99 visinfo
= glXChooseVisual(dpy
, scrnum
, attrib
);
101 Error(displayName
, "Unable to find RGB, double-buffered visual");
105 /* window attributes */
106 xpos
= (NumHeads
% 10) * 100;
107 ypos
= (NumHeads
/ 10) * 100;
108 printf("%d, %d\n", xpos
, ypos
);
109 attr
.background_pixel
= 0;
110 attr
.border_pixel
= 0;
111 attr
.colormap
= XCreateColormap(dpy
, root
, visinfo
->visual
, AllocNone
);
112 attr
.event_mask
= StructureNotifyMask
| ExposureMask
| KeyPressMask
;
113 mask
= CWBackPixel
| CWBorderPixel
| CWColormap
| CWEventMask
;
115 win
= XCreateWindow(dpy
, root
, xpos
, ypos
, width
, height
,
116 0, visinfo
->depth
, InputOutput
,
117 visinfo
->visual
, mask
, &attr
);
119 Error(displayName
, "Couldn't create window");
124 XSizeHints sizehints
;
127 sizehints
.width
= width
;
128 sizehints
.height
= height
;
129 sizehints
.flags
= USSize
| USPosition
;
130 XSetNormalHints(dpy
, win
, &sizehints
);
131 XSetStandardProperties(dpy
, win
, name
, name
,
132 None
, (char **)NULL
, 0, &sizehints
);
136 ctx
= glXCreateContext(dpy
, visinfo
, NULL
, True
);
139 /* share textures & dlists with 0th context */
141 ctx
= glXCreateContext(dpy
, visinfo
, Heads
[0].Context
, True
);
144 Error(displayName
, "Couldn't create GLX context");
148 XMapWindow(dpy
, win
);
150 if (!glXMakeCurrent(dpy
, win
, ctx
)) {
151 Error(displayName
, "glXMakeCurrent failed");
152 printf("glXMakeCurrent failed in Redraw()\n");
157 /* create texture object now */
158 static const GLubyte checker
[2][2][4] = {
159 { {255, 255, 255, 255}, { 0, 0, 0, 255} },
160 { { 0, 0, 0, 0}, {255, 255, 255, 255} }
162 glGenTextures(1, &TexObj
);
164 glBindTexture(GL_TEXTURE_2D
, TexObj
);
165 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, 2, 2, 0, GL_RGB
,
166 GL_UNSIGNED_BYTE
, checker
);
167 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
168 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
171 /* bind 0th context's texture in this context too */
173 glBindTexture(GL_TEXTURE_2D
, TexObj
);
175 glEnable(GL_TEXTURE_2D
);
177 /* save the info for this head */
179 struct head
*h
= &Heads
[NumHeads
];
180 strcpy(h
->DisplayName
, name
);
185 strcpy(h
->Version
, (char *) glGetString(GL_VERSION
));
186 strcpy(h
->Vendor
, (char *) glGetString(GL_VENDOR
));
187 strcpy(h
->Renderer
, (char *) glGetString(GL_RENDERER
));
189 return &Heads
[NumHeads
-1];
199 for (i
= 0; i
< NumHeads
; i
++) {
200 XDestroyWindow(Heads
[i
].Dpy
, Heads
[i
].Win
);
201 glXDestroyContext(Heads
[i
].Dpy
, Heads
[i
].Context
);
202 XCloseDisplay(Heads
[i
].Dpy
);
208 Redraw(struct head
*h
)
210 if (!glXMakeCurrent(h
->Dpy
, h
->Win
, h
->Context
)) {
211 Error(h
->DisplayName
, "glXMakeCurrent failed");
212 printf("glXMakeCurrent failed in Redraw()\n");
218 glShadeModel(GL_FLAT
);
219 glClearColor(0.5, 0.5, 0.5, 1.0);
220 glClear(GL_COLOR_BUFFER_BIT
);
222 /* draw green triangle */
223 glColor3f(0.0, 1.0, 0.0);
225 glRotatef(h
->Angle
, 0, 0, 1);
226 glBegin(GL_TRIANGLES
);
227 glTexCoord2f(0.5, 1.0); glVertex2f(0, 0.8);
228 glTexCoord2f(0.0, 0.0); glVertex2f(-0.8, -0.7);
229 glTexCoord2f(1.0, 0.0); glVertex2f(0.8, -0.7);
234 glXSwapBuffers(h
->Dpy
, h
->Win
);
241 glXSwapBuffers(h
->Dpy
, h
->Win
);
246 Resize(const struct head
*h
, unsigned int width
, unsigned int height
)
248 if (!glXMakeCurrent(h
->Dpy
, h
->Win
, h
->Context
)) {
249 Error(h
->DisplayName
, "glXMakeCurrent failed in Resize()");
253 glViewport(0, 0, width
, height
);
254 glMatrixMode(GL_PROJECTION
);
256 glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
266 for (i
= 0; i
< NumHeads
; i
++) {
267 struct head
*h
= &Heads
[i
];
268 while (XPending(h
->Dpy
) > 0) {
270 XNextEvent(h
->Dpy
, &event
);
271 if (event
.xany
.window
== h
->Win
) {
272 switch (event
.type
) {
278 case ConfigureNotify
:
279 Resize(h
, event
.xconfigure
.width
, event
.xconfigure
.height
);
286 XLookupString(&event
.xkey
, buf
, sizeof(buf
), &keySym
, &stat
);
293 printf("Delete Texture in window %d\n", i
);
294 glXMakeCurrent(h
->Dpy
, h
->Win
, h
->Context
);
295 glDeleteTextures(1, &TexObj
);
299 printf("Unbind Texture in window %d\n", i
);
300 glXMakeCurrent(h
->Dpy
, h
->Win
, h
->Context
);
301 glBindTexture(GL_TEXTURE_2D
, 0);
311 printf("window mismatch\n");
316 /* redraw all windows */
317 for (i
= 0; i
< NumHeads
; i
++) {
320 /* swapbuffers on all windows, if not already done */
322 for (i
= 0; i
< NumHeads
; i
++) {
333 PrintInfo(const struct head
*h
)
335 printf("Name: %s\n", h
->DisplayName
);
336 printf(" Display: %p\n", (void *) h
->Dpy
);
337 printf(" Window: 0x%x\n", (int) h
->Win
);
338 printf(" Context: 0x%lx\n", (long) h
->Context
);
339 printf(" GL_VERSION: %s\n", h
->Version
);
340 printf(" GL_VENDOR: %s\n", h
->Vendor
);
341 printf(" GL_RENDERER: %s\n", h
->Renderer
);
346 main(int argc
, char *argv
[])
348 char *dpyName
= NULL
;
352 printf("manywin: open N simultaneous glx windows\n");
354 printf(" manywin [-s] numWindows\n");
355 printf("Options:\n");
356 printf(" -s = swap immediately after drawing (see src code)\n");
357 printf("Example:\n");
358 printf(" manywin 10\n");
363 for (i
= 1; i
< argc
; i
++) {
364 if (strcmp(argv
[i
], "-s") == 0) {
365 SwapSeparate
= GL_FALSE
;
367 else if (strcmp(argv
[i
], "-display") == 0 && i
< argc
) {
378 printf("%d windows\n", n
);
379 for (i
= 0; i
< n
; i
++) {
382 sprintf(name
, "%d", i
);
383 h
= AddHead(dpyName
, name
);