aboutsummaryrefslogtreecommitdiffstats
path: root/progs/xdemos/texture_from_pixmap.c
blob: 222295670361f6e54c020b93cdfc405dceebcafb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
 * Mesa 3-D graphics library
 * Version:  7.1
 * 
 * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */


/*
 * Test the GLX_EXT_texture_from_pixmap extension
 * Brian Paul
 * 19 May 2007
 */


#define GL_GLEXT_PROTOTYPES
#define GLX_GLXEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glx.h>
#include <X11/keysym.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>


static Display *
OpenDisplay(void)
{
   int screen;
   Display *dpy;
   const char *ext;

   dpy = XOpenDisplay(NULL);
   if (!dpy) {
      printf("Couldn't open default display!\n");
      exit(1);
   }

   screen = DefaultScreen(dpy);
   ext = glXQueryExtensionsString(dpy, screen);
   if (!strstr(ext, "GLX_EXT_texture_from_pixmap")) {
      printf("GLX_EXT_texture_from_pixmap not supported by GLX\n");
      exit(1);
   }

   return dpy;
}


static GLXFBConfig
ChoosePixmapFBConfig(Display *display)
{
   int screen = DefaultScreen(display);
   GLXFBConfig *fbconfigs;
   int i, nfbconfigs, value;
   float top, bottom;

   fbconfigs = glXGetFBConfigs(display, screen, &nfbconfigs);
   for (i = 0; i < nfbconfigs; i++) {

      glXGetFBConfigAttrib(display, fbconfigs[i], GLX_DRAWABLE_TYPE, &value);
      if (!(value & GLX_PIXMAP_BIT))
         continue;

      glXGetFBConfigAttrib(display, fbconfigs[i],
                           GLX_BIND_TO_TEXTURE_TARGETS_EXT, &value);
      if (!(value & GLX_TEXTURE_2D_BIT_EXT))
         continue;

      glXGetFBConfigAttrib(display, fbconfigs[i],
                           GLX_BIND_TO_TEXTURE_RGBA_EXT, &value);
      if (value == False) {
         glXGetFBConfigAttrib(display, fbconfigs[i],
                              GLX_BIND_TO_TEXTURE_RGB_EXT, &value);
         if (value == False)
            continue;
      }

      glXGetFBConfigAttrib(display, fbconfigs[i],
                           GLX_Y_INVERTED_EXT, &value);
      if (value == True) {
         top = 0.0f;
         bottom = 1.0f;
      }
      else {
         top = 1.0f;
         bottom = 0.0f;
      }

      break;
   }

   if (i == nfbconfigs) {
      printf("Unable to find FBconfig for texturing\n");
      exit(1);
   }

   return fbconfigs[i];
}


static GLXPixmap
CreatePixmap(Display *dpy, GLXFBConfig config, int w, int h, Pixmap *p)
{
   GLXPixmap gp;
   const int pixmapAttribs[] = {
      GLX_TEXTURE_TARGET_EXT, GLX_TEXTURE_2D_EXT,
      GLX_TEXTURE_FORMAT_EXT, GLX_TEXTURE_FORMAT_RGB_EXT,
      None
   };
   Window root = RootWindow(dpy, 0);

   *p = XCreatePixmap(dpy, root, w, h, 24);
   XSync(dpy, 0);
   gp = glXCreatePixmap(dpy, config, *p, pixmapAttribs);
   XSync(dpy, 0);

   return gp;
}


static void
DrawPixmapImage(Display *dpy, Pixmap pm, int w, int h)
{
   XGCValues gcvals;
   GC gc;

   gcvals.background = 0;
   gc = XCreateGC(dpy, pm, GCBackground, &gcvals);

   XSetForeground(dpy, gc, 0x0);
   XFillRectangle(dpy, pm, gc, 0, 0, w, h);

   XSetForeground(dpy, gc, 0xff0000);
   XFillRectangle(dpy, pm, gc, 0, 0, 50, 50);

   XSetForeground(dpy, gc, 0x00ff00);
   XFillRectangle(dpy, pm, gc, w - 50, 0, 50, 50);

   XSetForeground(dpy, gc, 0x0000ff);
   XFillRectangle(dpy, pm, gc, 0, h - 50, 50, 50);

   XSetForeground(dpy, gc, 0xffffff);
   XFillRectangle(dpy, pm, gc, h - 50, h - 50, 50, 50);

   XSetForeground(dpy, gc, 0xffff00);
   XSetLineAttributes(dpy, gc, 3, LineSolid, CapButt, JoinBevel);
   XDrawLine(dpy, pm, gc, 0, 0, w, h);
   XDrawLine(dpy, pm, gc, 0, h, w, 0);

   XFreeGC(dpy, gc);
}


static XVisualInfo *
ChooseWindowVisual(Display *dpy)
{
   int screen = DefaultScreen(dpy);
   XVisualInfo *visinfo;
   int attribs[] = {
      GLX_RGBA,
      GLX_RED_SIZE, 1,
      GLX_GREEN_SIZE, 1,
      GLX_BLUE_SIZE, 1,
      GLX_DOUBLEBUFFER,
      None
   };

   visinfo = glXChooseVisual(dpy, screen, attribs);
   if (!visinfo) {
      printf("Unable to find RGB, double-buffered visual\n");
      exit(1);
   }

   return visinfo;
}


static Window
CreateWindow(Display *dpy, XVisualInfo *visinfo,
             int width, int height, const char *name)
{
   int screen = DefaultScreen(dpy);
   Window win;
   XSetWindowAttributes attr;
   unsigned long mask;
   Window root;

   root = RootWindow(dpy, screen);

   /* window attributes */
   attr.background_pixel = 0;
   attr.border_pixel = 0;
   attr.colormap = XCreateColormap(dpy, root, visinfo->visual, AllocNone);
   attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
   mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;

   win = XCreateWindow(dpy, root, 0, 0, width, height,
		        0, visinfo->depth, InputOutput,
		        visinfo->visual, mask, &attr);
   if (win) {
      XSizeHints sizehints;
      sizehints.width  = width;
      sizehints.height = height;
      sizehints.flags = USSize;
      XSetNormalHints(dpy, win, &sizehints);
      XSetStandardProperties(dpy, win, name, name,
                              None, (char **)NULL, 0, &sizehints);

      XMapWindow(dpy, win);
   }
   return win;
}


static void
BindPixmapTexture(Display *dpy, GLXPixmap gp)
{
   GLuint texture;

   glGenTextures(1, &texture);
   glBindTexture(GL_TEXTURE_2D, texture);

   glXBindTexImageEXT(dpy, gp, GLX_FRONT_LEFT_EXT, NULL);

   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

   glEnable(GL_TEXTURE_2D);
   /*
     glXReleaseTexImageEXT (display, glxpixmap, GLX_FRONT_LEFT_EXT);
   */
}


static void
Resize(Window win, unsigned int width, unsigned int height)
{
   float sz = 1.5;
   glViewport(0, 0, width, height);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glOrtho(-sz, sz, -sz, sz, -1.0, 1.0);
   glMatrixMode(GL_MODELVIEW);
}


static void
Redraw(Display *dpy, Window win, float rot)
{
   glClearColor(0.25, 0.25, 0.25, 0.0);
   glClear(GL_COLOR_BUFFER_BIT);
   glPushMatrix();
   glRotatef(rot, 0, 0, 1);
   glRotatef(2.0 * rot, 1, 0, 0);

   glBegin(GL_QUADS);
   glTexCoord2d(0.0, 0.0);
   glVertex2f(-1, -1);
   glTexCoord2d(1.0, 0.0);
   glVertex2f( 1, -1);
   glTexCoord2d(1.0, 1.0);
   glVertex2d(1.0, 1.0);
   glTexCoord2d(0.0, 1.0);
   glVertex2f(-1.0, 1.0);
   glEnd();

   glPopMatrix();

   glXSwapBuffers(dpy, win);
}


static void
EventLoop(Display *dpy, Window win)
{
   GLfloat rot = 0.0;
   int anim = 0;
 
   while (1) {
      if (!anim || XPending(dpy) > 0) {
         XEvent event;
         XNextEvent(dpy, &event);

         switch (event.type) {
         case Expose:
            Redraw(dpy, win, rot);
            break;
         case ConfigureNotify:
            Resize(event.xany.window,
                   event.xconfigure.width,
                   event.xconfigure.height);
            break;
         case KeyPress:
            {
               char buf[100];
               KeySym keySym;
               XComposeStatus stat;
               XLookupString(&event.xkey, buf, sizeof(buf), &keySym, &stat);
               if (keySym == XK_Escape) {
                  return; /* exit */
               }
               else if (keySym == XK_r) {
                  rot += 1.0;
                  Redraw(dpy, win, rot);
               }
               else if (keySym == XK_a) {
                  anim = !anim;
               }
               else if (keySym == XK_R) {
                  rot -= 1.0;
                  Redraw(dpy, win, rot);
               }
            }
            break;
         default:
            ; /*no-op*/
         }
      }
      else {
         /* animate */
         rot += 1.0;
         Redraw(dpy, win, rot);
      }
   }
}



int
main(int argc, char *argv[])
{
   Display *dpy;
   GLXFBConfig pixmapConfig;
   XVisualInfo *windowVis;
   GLXPixmap gp;
   Window win;
   GLXContext ctx;
   Pixmap p;

   dpy = OpenDisplay();

   pixmapConfig = ChoosePixmapFBConfig(dpy);
   windowVis = ChooseWindowVisual(dpy);
   win = CreateWindow(dpy, windowVis, 500, 500, "Texture From Pixmap");

   gp = CreatePixmap(dpy, pixmapConfig, 512, 512, &p);
   DrawPixmapImage(dpy, p, 512, 512);

   ctx = glXCreateContext(dpy, windowVis, NULL, True);
   if (!ctx) {
      printf("Couldn't create GLX context\n");
      exit(1);
   }

   glXMakeCurrent(dpy, win, ctx);

   BindPixmapTexture(dpy, gp);

   EventLoop(dpy, win);

   return 0;
}