blob: 50870c4df4afdcc40a0a0ecb2b6a7af31735486b [file] [log] [blame]
Brian44653772007-05-19 08:28:41 -06001/*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
23 */
24
25
26/*
27 * Test the GLX_EXT_texture_from_pixmap extension
28 * Brian Paul
29 * 19 May 2007
30 */
31
32
33#define GL_GLEXT_PROTOTYPES
34#define GLX_GLXEXT_PROTOTYPES
35#include <GL/gl.h>
36#include <GL/glx.h>
37#include <X11/keysym.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41#include <unistd.h>
42
43
Brianb4f8d442007-05-21 20:57:09 -060044static float top, bottom;
45
46
Brian44653772007-05-19 08:28:41 -060047static Display *
48OpenDisplay(void)
49{
50 int screen;
51 Display *dpy;
52 const char *ext;
53
54 dpy = XOpenDisplay(NULL);
55 if (!dpy) {
56 printf("Couldn't open default display!\n");
57 exit(1);
58 }
59
60 screen = DefaultScreen(dpy);
61 ext = glXQueryExtensionsString(dpy, screen);
62 if (!strstr(ext, "GLX_EXT_texture_from_pixmap")) {
63 printf("GLX_EXT_texture_from_pixmap not supported by GLX\n");
64 exit(1);
65 }
66
67 return dpy;
68}
69
70
71static GLXFBConfig
72ChoosePixmapFBConfig(Display *display)
73{
74 int screen = DefaultScreen(display);
75 GLXFBConfig *fbconfigs;
76 int i, nfbconfigs, value;
Brian44653772007-05-19 08:28:41 -060077
78 fbconfigs = glXGetFBConfigs(display, screen, &nfbconfigs);
79 for (i = 0; i < nfbconfigs; i++) {
80
81 glXGetFBConfigAttrib(display, fbconfigs[i], GLX_DRAWABLE_TYPE, &value);
82 if (!(value & GLX_PIXMAP_BIT))
83 continue;
84
85 glXGetFBConfigAttrib(display, fbconfigs[i],
86 GLX_BIND_TO_TEXTURE_TARGETS_EXT, &value);
87 if (!(value & GLX_TEXTURE_2D_BIT_EXT))
88 continue;
89
90 glXGetFBConfigAttrib(display, fbconfigs[i],
91 GLX_BIND_TO_TEXTURE_RGBA_EXT, &value);
92 if (value == False) {
93 glXGetFBConfigAttrib(display, fbconfigs[i],
94 GLX_BIND_TO_TEXTURE_RGB_EXT, &value);
95 if (value == False)
96 continue;
97 }
98
99 glXGetFBConfigAttrib(display, fbconfigs[i],
100 GLX_Y_INVERTED_EXT, &value);
101 if (value == True) {
102 top = 0.0f;
103 bottom = 1.0f;
104 }
105 else {
106 top = 1.0f;
107 bottom = 0.0f;
108 }
109
110 break;
111 }
112
113 if (i == nfbconfigs) {
114 printf("Unable to find FBconfig for texturing\n");
115 exit(1);
116 }
117
118 return fbconfigs[i];
119}
120
121
122static GLXPixmap
123CreatePixmap(Display *dpy, GLXFBConfig config, int w, int h, Pixmap *p)
124{
125 GLXPixmap gp;
126 const int pixmapAttribs[] = {
127 GLX_TEXTURE_TARGET_EXT, GLX_TEXTURE_2D_EXT,
128 GLX_TEXTURE_FORMAT_EXT, GLX_TEXTURE_FORMAT_RGB_EXT,
129 None
130 };
131 Window root = RootWindow(dpy, 0);
132
133 *p = XCreatePixmap(dpy, root, w, h, 24);
134 XSync(dpy, 0);
135 gp = glXCreatePixmap(dpy, config, *p, pixmapAttribs);
136 XSync(dpy, 0);
137
138 return gp;
139}
140
141
142static void
143DrawPixmapImage(Display *dpy, Pixmap pm, int w, int h)
144{
145 XGCValues gcvals;
146 GC gc;
147
148 gcvals.background = 0;
149 gc = XCreateGC(dpy, pm, GCBackground, &gcvals);
150
151 XSetForeground(dpy, gc, 0x0);
152 XFillRectangle(dpy, pm, gc, 0, 0, w, h);
153
154 XSetForeground(dpy, gc, 0xff0000);
155 XFillRectangle(dpy, pm, gc, 0, 0, 50, 50);
156
157 XSetForeground(dpy, gc, 0x00ff00);
158 XFillRectangle(dpy, pm, gc, w - 50, 0, 50, 50);
159
160 XSetForeground(dpy, gc, 0x0000ff);
161 XFillRectangle(dpy, pm, gc, 0, h - 50, 50, 50);
162
163 XSetForeground(dpy, gc, 0xffffff);
164 XFillRectangle(dpy, pm, gc, h - 50, h - 50, 50, 50);
165
166 XSetForeground(dpy, gc, 0xffff00);
167 XSetLineAttributes(dpy, gc, 3, LineSolid, CapButt, JoinBevel);
168 XDrawLine(dpy, pm, gc, 0, 0, w, h);
169 XDrawLine(dpy, pm, gc, 0, h, w, 0);
170
171 XFreeGC(dpy, gc);
172}
173
174
175static XVisualInfo *
176ChooseWindowVisual(Display *dpy)
177{
178 int screen = DefaultScreen(dpy);
179 XVisualInfo *visinfo;
180 int attribs[] = {
181 GLX_RGBA,
182 GLX_RED_SIZE, 1,
183 GLX_GREEN_SIZE, 1,
184 GLX_BLUE_SIZE, 1,
185 GLX_DOUBLEBUFFER,
186 None
187 };
188
189 visinfo = glXChooseVisual(dpy, screen, attribs);
190 if (!visinfo) {
191 printf("Unable to find RGB, double-buffered visual\n");
192 exit(1);
193 }
194
195 return visinfo;
196}
197
198
199static Window
200CreateWindow(Display *dpy, XVisualInfo *visinfo,
201 int width, int height, const char *name)
202{
203 int screen = DefaultScreen(dpy);
204 Window win;
205 XSetWindowAttributes attr;
206 unsigned long mask;
207 Window root;
208
209 root = RootWindow(dpy, screen);
210
211 /* window attributes */
212 attr.background_pixel = 0;
213 attr.border_pixel = 0;
214 attr.colormap = XCreateColormap(dpy, root, visinfo->visual, AllocNone);
215 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
216 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
217
218 win = XCreateWindow(dpy, root, 0, 0, width, height,
219 0, visinfo->depth, InputOutput,
220 visinfo->visual, mask, &attr);
221 if (win) {
222 XSizeHints sizehints;
223 sizehints.width = width;
224 sizehints.height = height;
225 sizehints.flags = USSize;
226 XSetNormalHints(dpy, win, &sizehints);
227 XSetStandardProperties(dpy, win, name, name,
228 None, (char **)NULL, 0, &sizehints);
229
230 XMapWindow(dpy, win);
231 }
232 return win;
233}
234
235
236static void
237BindPixmapTexture(Display *dpy, GLXPixmap gp)
238{
239 GLuint texture;
240
241 glGenTextures(1, &texture);
242 glBindTexture(GL_TEXTURE_2D, texture);
243
244 glXBindTexImageEXT(dpy, gp, GLX_FRONT_LEFT_EXT, NULL);
245
246 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
247 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
248
249 glEnable(GL_TEXTURE_2D);
250 /*
251 glXReleaseTexImageEXT (display, glxpixmap, GLX_FRONT_LEFT_EXT);
252 */
253}
254
255
256static void
257Resize(Window win, unsigned int width, unsigned int height)
258{
259 float sz = 1.5;
260 glViewport(0, 0, width, height);
261 glMatrixMode(GL_PROJECTION);
262 glLoadIdentity();
263 glOrtho(-sz, sz, -sz, sz, -1.0, 1.0);
264 glMatrixMode(GL_MODELVIEW);
265}
266
267
268static void
269Redraw(Display *dpy, Window win, float rot)
270{
271 glClearColor(0.25, 0.25, 0.25, 0.0);
272 glClear(GL_COLOR_BUFFER_BIT);
273 glPushMatrix();
274 glRotatef(rot, 0, 0, 1);
275 glRotatef(2.0 * rot, 1, 0, 0);
276
277 glBegin(GL_QUADS);
Brianb4f8d442007-05-21 20:57:09 -0600278 glTexCoord2d(0.0, bottom);
Brian44653772007-05-19 08:28:41 -0600279 glVertex2f(-1, -1);
Brianb4f8d442007-05-21 20:57:09 -0600280 glTexCoord2d(1.0, bottom);
Brian44653772007-05-19 08:28:41 -0600281 glVertex2f( 1, -1);
Brianb4f8d442007-05-21 20:57:09 -0600282 glTexCoord2d(1.0, top);
Brian44653772007-05-19 08:28:41 -0600283 glVertex2d(1.0, 1.0);
Brianb4f8d442007-05-21 20:57:09 -0600284 glTexCoord2d(0.0, top);
Brian44653772007-05-19 08:28:41 -0600285 glVertex2f(-1.0, 1.0);
286 glEnd();
287
288 glPopMatrix();
289
290 glXSwapBuffers(dpy, win);
291}
292
293
294static void
295EventLoop(Display *dpy, Window win)
296{
297 GLfloat rot = 0.0;
298 int anim = 0;
299
300 while (1) {
301 if (!anim || XPending(dpy) > 0) {
302 XEvent event;
303 XNextEvent(dpy, &event);
304
305 switch (event.type) {
306 case Expose:
307 Redraw(dpy, win, rot);
308 break;
309 case ConfigureNotify:
310 Resize(event.xany.window,
311 event.xconfigure.width,
312 event.xconfigure.height);
313 break;
314 case KeyPress:
315 {
316 char buf[100];
317 KeySym keySym;
318 XComposeStatus stat;
319 XLookupString(&event.xkey, buf, sizeof(buf), &keySym, &stat);
320 if (keySym == XK_Escape) {
321 return; /* exit */
322 }
323 else if (keySym == XK_r) {
324 rot += 1.0;
325 Redraw(dpy, win, rot);
326 }
327 else if (keySym == XK_a) {
328 anim = !anim;
329 }
330 else if (keySym == XK_R) {
331 rot -= 1.0;
332 Redraw(dpy, win, rot);
333 }
334 }
335 break;
336 default:
337 ; /*no-op*/
338 }
339 }
340 else {
341 /* animate */
342 rot += 1.0;
343 Redraw(dpy, win, rot);
344 }
345 }
346}
347
348
349
350int
351main(int argc, char *argv[])
352{
353 Display *dpy;
354 GLXFBConfig pixmapConfig;
355 XVisualInfo *windowVis;
356 GLXPixmap gp;
357 Window win;
358 GLXContext ctx;
359 Pixmap p;
360
361 dpy = OpenDisplay();
362
363 pixmapConfig = ChoosePixmapFBConfig(dpy);
364 windowVis = ChooseWindowVisual(dpy);
365 win = CreateWindow(dpy, windowVis, 500, 500, "Texture From Pixmap");
366
367 gp = CreatePixmap(dpy, pixmapConfig, 512, 512, &p);
368 DrawPixmapImage(dpy, p, 512, 512);
369
370 ctx = glXCreateContext(dpy, windowVis, NULL, True);
371 if (!ctx) {
372 printf("Couldn't create GLX context\n");
373 exit(1);
374 }
375
376 glXMakeCurrent(dpy, win, ctx);
377
378 BindPixmapTexture(dpy, gp);
379
380 EventLoop(dpy, win);
381
382 return 0;
383}