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