2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 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 * Test the GLX_EXT_texture_from_pixmap extension
33 #define GL_GLEXT_PROTOTYPES
34 #define GLX_GLXEXT_PROTOTYPES
37 #include <X11/keysym.h>
44 static float top
, bottom
;
46 static PFNGLXBINDTEXIMAGEEXTPROC glXBindTexImageEXT_func
= NULL
;
47 static PFNGLXRELEASETEXIMAGEEXTPROC glXReleaseTexImageEXT_func
= NULL
;
57 dpy
= XOpenDisplay(NULL
);
59 printf("Couldn't open default display!\n");
63 screen
= DefaultScreen(dpy
);
64 ext
= glXQueryExtensionsString(dpy
, screen
);
65 if (!strstr(ext
, "GLX_EXT_texture_from_pixmap")) {
66 fprintf(stderr
, "GLX_EXT_texture_from_pixmap not supported.\n");
70 glXBindTexImageEXT_func
= (PFNGLXBINDTEXIMAGEEXTPROC
)
71 glXGetProcAddress((GLubyte
*) "glXBindTexImageEXT");
72 glXReleaseTexImageEXT_func
= (PFNGLXRELEASETEXIMAGEEXTPROC
)
73 glXGetProcAddress((GLubyte
*) "glXReleaseTexImageEXT");
75 if (!glXBindTexImageEXT_func
|| !glXReleaseTexImageEXT_func
) {
76 fprintf(stderr
, "glXGetProcAddress failed!\n");
85 ChoosePixmapFBConfig(Display
*display
)
87 int screen
= DefaultScreen(display
);
88 GLXFBConfig
*fbconfigs
;
89 int i
, nfbconfigs
= 0, value
;
91 fbconfigs
= glXGetFBConfigs(display
, screen
, &nfbconfigs
);
92 for (i
= 0; i
< nfbconfigs
; i
++) {
94 glXGetFBConfigAttrib(display
, fbconfigs
[i
], GLX_DRAWABLE_TYPE
, &value
);
95 if (!(value
& GLX_PIXMAP_BIT
))
98 glXGetFBConfigAttrib(display
, fbconfigs
[i
],
99 GLX_BIND_TO_TEXTURE_TARGETS_EXT
, &value
);
100 if (!(value
& GLX_TEXTURE_2D_BIT_EXT
))
103 glXGetFBConfigAttrib(display
, fbconfigs
[i
],
104 GLX_BIND_TO_TEXTURE_RGBA_EXT
, &value
);
105 if (value
== False
) {
106 glXGetFBConfigAttrib(display
, fbconfigs
[i
],
107 GLX_BIND_TO_TEXTURE_RGB_EXT
, &value
);
112 glXGetFBConfigAttrib(display
, fbconfigs
[i
],
113 GLX_Y_INVERTED_EXT
, &value
);
126 if (i
== nfbconfigs
) {
127 printf("Unable to find FBconfig for texturing\n");
136 CreatePixmap(Display
*dpy
, GLXFBConfig config
, int w
, int h
, Pixmap
*p
)
139 const int pixmapAttribs
[] = {
140 GLX_TEXTURE_TARGET_EXT
, GLX_TEXTURE_2D_EXT
,
141 GLX_TEXTURE_FORMAT_EXT
, GLX_TEXTURE_FORMAT_RGB_EXT
,
144 Window root
= RootWindow(dpy
, 0);
146 *p
= XCreatePixmap(dpy
, root
, w
, h
, 24);
148 gp
= glXCreatePixmap(dpy
, config
, *p
, pixmapAttribs
);
156 DrawPixmapImage(Display
*dpy
, Pixmap pm
, int w
, int h
)
161 gcvals
.background
= 0;
162 gc
= XCreateGC(dpy
, pm
, GCBackground
, &gcvals
);
164 XSetForeground(dpy
, gc
, 0x0);
165 XFillRectangle(dpy
, pm
, gc
, 0, 0, w
, h
);
167 XSetForeground(dpy
, gc
, 0xff0000);
168 XFillRectangle(dpy
, pm
, gc
, 0, 0, 50, 50);
170 XSetForeground(dpy
, gc
, 0x00ff00);
171 XFillRectangle(dpy
, pm
, gc
, w
- 50, 0, 50, 50);
173 XSetForeground(dpy
, gc
, 0x0000ff);
174 XFillRectangle(dpy
, pm
, gc
, 0, h
- 50, 50, 50);
176 XSetForeground(dpy
, gc
, 0xffffff);
177 XFillRectangle(dpy
, pm
, gc
, h
- 50, h
- 50, 50, 50);
179 XSetForeground(dpy
, gc
, 0xffff00);
180 XSetLineAttributes(dpy
, gc
, 3, LineSolid
, CapButt
, JoinBevel
);
181 XDrawLine(dpy
, pm
, gc
, 0, 0, w
, h
);
182 XDrawLine(dpy
, pm
, gc
, 0, h
, w
, 0);
189 ChooseWindowVisual(Display
*dpy
)
191 int screen
= DefaultScreen(dpy
);
192 XVisualInfo
*visinfo
;
202 visinfo
= glXChooseVisual(dpy
, screen
, attribs
);
204 printf("Unable to find RGB, double-buffered visual\n");
213 CreateWindow(Display
*dpy
, XVisualInfo
*visinfo
,
214 int width
, int height
, const char *name
)
216 int screen
= DefaultScreen(dpy
);
218 XSetWindowAttributes attr
;
222 root
= RootWindow(dpy
, screen
);
224 /* window attributes */
225 attr
.background_pixel
= 0;
226 attr
.border_pixel
= 0;
227 attr
.colormap
= XCreateColormap(dpy
, root
, visinfo
->visual
, AllocNone
);
228 attr
.event_mask
= StructureNotifyMask
| ExposureMask
| KeyPressMask
;
229 mask
= CWBackPixel
| CWBorderPixel
| CWColormap
| CWEventMask
;
231 win
= XCreateWindow(dpy
, root
, 0, 0, width
, height
,
232 0, visinfo
->depth
, InputOutput
,
233 visinfo
->visual
, mask
, &attr
);
235 XSizeHints sizehints
;
236 sizehints
.width
= width
;
237 sizehints
.height
= height
;
238 sizehints
.flags
= USSize
;
239 XSetNormalHints(dpy
, win
, &sizehints
);
240 XSetStandardProperties(dpy
, win
, name
, name
,
241 None
, (char **)NULL
, 0, &sizehints
);
243 XMapWindow(dpy
, win
);
250 BindPixmapTexture(Display
*dpy
, GLXPixmap gp
)
254 glGenTextures(1, &texture
);
255 glBindTexture(GL_TEXTURE_2D
, texture
);
257 glXBindTexImageEXT_func(dpy
, gp
, GLX_FRONT_LEFT_EXT
, NULL
);
259 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
260 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
262 glEnable(GL_TEXTURE_2D
);
264 glXReleaseTexImageEXT_func(display, glxpixmap, GLX_FRONT_LEFT_EXT);
270 Resize(Window win
, unsigned int width
, unsigned int height
)
273 glViewport(0, 0, width
, height
);
274 glMatrixMode(GL_PROJECTION
);
276 glOrtho(-sz
, sz
, -sz
, sz
, -1.0, 1.0);
277 glMatrixMode(GL_MODELVIEW
);
282 Redraw(Display
*dpy
, Window win
, float rot
)
284 glClearColor(0.25, 0.25, 0.25, 0.0);
285 glClear(GL_COLOR_BUFFER_BIT
);
287 glRotatef(rot
, 0, 0, 1);
288 glRotatef(2.0 * rot
, 1, 0, 0);
291 glTexCoord2d(0.0, bottom
);
293 glTexCoord2d(1.0, bottom
);
295 glTexCoord2d(1.0, top
);
296 glVertex2d(1.0, 1.0);
297 glTexCoord2d(0.0, top
);
298 glVertex2f(-1.0, 1.0);
303 glXSwapBuffers(dpy
, win
);
308 EventLoop(Display
*dpy
, Window win
)
314 if (!anim
|| XPending(dpy
) > 0) {
316 XNextEvent(dpy
, &event
);
318 switch (event
.type
) {
320 Redraw(dpy
, win
, rot
);
322 case ConfigureNotify
:
323 Resize(event
.xany
.window
,
324 event
.xconfigure
.width
,
325 event
.xconfigure
.height
);
332 XLookupString(&event
.xkey
, buf
, sizeof(buf
), &keySym
, &stat
);
333 if (keySym
== XK_Escape
) {
336 else if (keySym
== XK_r
) {
338 Redraw(dpy
, win
, rot
);
340 else if (keySym
== XK_a
) {
343 else if (keySym
== XK_R
) {
345 Redraw(dpy
, win
, rot
);
356 Redraw(dpy
, win
, rot
);
364 main(int argc
, char *argv
[])
367 GLXFBConfig pixmapConfig
;
368 XVisualInfo
*windowVis
;
376 pixmapConfig
= ChoosePixmapFBConfig(dpy
);
377 windowVis
= ChooseWindowVisual(dpy
);
378 win
= CreateWindow(dpy
, windowVis
, 500, 500, "Texture From Pixmap");
380 gp
= CreatePixmap(dpy
, pixmapConfig
, 512, 512, &p
);
381 DrawPixmapImage(dpy
, p
, 512, 512);
383 ctx
= glXCreateContext(dpy
, windowVis
, NULL
, True
);
385 printf("Couldn't create GLX context\n");
389 glXMakeCurrent(dpy
, win
, ctx
);
391 BindPixmapTexture(dpy
, gp
);