Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

texture_from_pixmap.c 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  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. * Test the GLX_EXT_texture_from_pixmap extension
  26. * Brian Paul
  27. * 19 May 2007
  28. */
  29. #define GL_GLEXT_PROTOTYPES
  30. #define GLX_GLXEXT_PROTOTYPES
  31. #include <GL/gl.h>
  32. #include <GL/glx.h>
  33. #include <X11/keysym.h>
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <unistd.h>
  38. static float top, bottom;
  39. static PFNGLXBINDTEXIMAGEEXTPROC glXBindTexImageEXT_func = NULL;
  40. static PFNGLXRELEASETEXIMAGEEXTPROC glXReleaseTexImageEXT_func = NULL;
  41. static Display *
  42. OpenDisplay(void)
  43. {
  44. int screen;
  45. Display *dpy;
  46. const char *ext;
  47. dpy = XOpenDisplay(NULL);
  48. if (!dpy) {
  49. printf("Couldn't open default display!\n");
  50. exit(1);
  51. }
  52. screen = DefaultScreen(dpy);
  53. ext = glXQueryExtensionsString(dpy, screen);
  54. if (!strstr(ext, "GLX_EXT_texture_from_pixmap")) {
  55. fprintf(stderr, "GLX_EXT_texture_from_pixmap not supported.\n");
  56. exit(1);
  57. }
  58. glXBindTexImageEXT_func = (PFNGLXBINDTEXIMAGEEXTPROC)
  59. glXGetProcAddress((GLubyte *) "glXBindTexImageEXT");
  60. glXReleaseTexImageEXT_func = (PFNGLXRELEASETEXIMAGEEXTPROC)
  61. glXGetProcAddress((GLubyte*) "glXReleaseTexImageEXT");
  62. if (!glXBindTexImageEXT_func || !glXReleaseTexImageEXT_func) {
  63. fprintf(stderr, "glXGetProcAddress failed!\n");
  64. exit(1);
  65. }
  66. return dpy;
  67. }
  68. static GLXFBConfig
  69. ChoosePixmapFBConfig(Display *display)
  70. {
  71. int screen = DefaultScreen(display);
  72. GLXFBConfig *fbconfigs;
  73. int i, nfbconfigs, value;
  74. fbconfigs = glXGetFBConfigs(display, screen, &nfbconfigs);
  75. for (i = 0; i < nfbconfigs; i++) {
  76. glXGetFBConfigAttrib(display, fbconfigs[i], GLX_DRAWABLE_TYPE, &value);
  77. if (!(value & GLX_PIXMAP_BIT))
  78. continue;
  79. glXGetFBConfigAttrib(display, fbconfigs[i],
  80. GLX_BIND_TO_TEXTURE_TARGETS_EXT, &value);
  81. if (!(value & GLX_TEXTURE_2D_BIT_EXT))
  82. continue;
  83. glXGetFBConfigAttrib(display, fbconfigs[i],
  84. GLX_BIND_TO_TEXTURE_RGBA_EXT, &value);
  85. if (value == False) {
  86. glXGetFBConfigAttrib(display, fbconfigs[i],
  87. GLX_BIND_TO_TEXTURE_RGB_EXT, &value);
  88. if (value == False)
  89. continue;
  90. }
  91. glXGetFBConfigAttrib(display, fbconfigs[i],
  92. GLX_Y_INVERTED_EXT, &value);
  93. if (value == True) {
  94. top = 0.0f;
  95. bottom = 1.0f;
  96. }
  97. else {
  98. top = 1.0f;
  99. bottom = 0.0f;
  100. }
  101. break;
  102. }
  103. if (i == nfbconfigs) {
  104. printf("Unable to find FBconfig for texturing\n");
  105. exit(1);
  106. }
  107. return fbconfigs[i];
  108. }
  109. static GLXPixmap
  110. CreatePixmap(Display *dpy, GLXFBConfig config, int w, int h, Pixmap *p)
  111. {
  112. GLXPixmap gp;
  113. const int pixmapAttribs[] = {
  114. GLX_TEXTURE_TARGET_EXT, GLX_TEXTURE_2D_EXT,
  115. GLX_TEXTURE_FORMAT_EXT, GLX_TEXTURE_FORMAT_RGB_EXT,
  116. None
  117. };
  118. Window root = RootWindow(dpy, 0);
  119. *p = XCreatePixmap(dpy, root, w, h, 24);
  120. XSync(dpy, 0);
  121. gp = glXCreatePixmap(dpy, config, *p, pixmapAttribs);
  122. XSync(dpy, 0);
  123. return gp;
  124. }
  125. static void
  126. DrawPixmapImage(Display *dpy, Pixmap pm, int w, int h)
  127. {
  128. XGCValues gcvals;
  129. GC gc;
  130. gcvals.background = 0;
  131. gc = XCreateGC(dpy, pm, GCBackground, &gcvals);
  132. XSetForeground(dpy, gc, 0x0);
  133. XFillRectangle(dpy, pm, gc, 0, 0, w, h);
  134. XSetForeground(dpy, gc, 0xff0000);
  135. XFillRectangle(dpy, pm, gc, 0, 0, 50, 50);
  136. XSetForeground(dpy, gc, 0x00ff00);
  137. XFillRectangle(dpy, pm, gc, w - 50, 0, 50, 50);
  138. XSetForeground(dpy, gc, 0x0000ff);
  139. XFillRectangle(dpy, pm, gc, 0, h - 50, 50, 50);
  140. XSetForeground(dpy, gc, 0xffffff);
  141. XFillRectangle(dpy, pm, gc, h - 50, h - 50, 50, 50);
  142. XSetForeground(dpy, gc, 0xffff00);
  143. XSetLineAttributes(dpy, gc, 3, LineSolid, CapButt, JoinBevel);
  144. XDrawLine(dpy, pm, gc, 0, 0, w, h);
  145. XDrawLine(dpy, pm, gc, 0, h, w, 0);
  146. XFreeGC(dpy, gc);
  147. }
  148. static XVisualInfo *
  149. ChooseWindowVisual(Display *dpy)
  150. {
  151. int screen = DefaultScreen(dpy);
  152. XVisualInfo *visinfo;
  153. int attribs[] = {
  154. GLX_RGBA,
  155. GLX_RED_SIZE, 1,
  156. GLX_GREEN_SIZE, 1,
  157. GLX_BLUE_SIZE, 1,
  158. GLX_DOUBLEBUFFER,
  159. None
  160. };
  161. visinfo = glXChooseVisual(dpy, screen, attribs);
  162. if (!visinfo) {
  163. printf("Unable to find RGB, double-buffered visual\n");
  164. exit(1);
  165. }
  166. return visinfo;
  167. }
  168. static Window
  169. CreateWindow(Display *dpy, XVisualInfo *visinfo,
  170. int width, int height, const char *name)
  171. {
  172. int screen = DefaultScreen(dpy);
  173. Window win;
  174. XSetWindowAttributes attr;
  175. unsigned long mask;
  176. Window root;
  177. root = RootWindow(dpy, screen);
  178. /* window attributes */
  179. attr.background_pixel = 0;
  180. attr.border_pixel = 0;
  181. attr.colormap = XCreateColormap(dpy, root, visinfo->visual, AllocNone);
  182. attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
  183. mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
  184. win = XCreateWindow(dpy, root, 0, 0, width, height,
  185. 0, visinfo->depth, InputOutput,
  186. visinfo->visual, mask, &attr);
  187. if (win) {
  188. XSizeHints sizehints;
  189. sizehints.width = width;
  190. sizehints.height = height;
  191. sizehints.flags = USSize;
  192. XSetNormalHints(dpy, win, &sizehints);
  193. XSetStandardProperties(dpy, win, name, name,
  194. None, (char **)NULL, 0, &sizehints);
  195. XMapWindow(dpy, win);
  196. }
  197. return win;
  198. }
  199. static void
  200. BindPixmapTexture(Display *dpy, GLXPixmap gp)
  201. {
  202. GLuint texture;
  203. glGenTextures(1, &texture);
  204. glBindTexture(GL_TEXTURE_2D, texture);
  205. glXBindTexImageEXT_func(dpy, gp, GLX_FRONT_LEFT_EXT, NULL);
  206. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  207. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  208. glEnable(GL_TEXTURE_2D);
  209. /*
  210. glXReleaseTexImageEXT_func(display, glxpixmap, GLX_FRONT_LEFT_EXT);
  211. */
  212. }
  213. static void
  214. Resize(Window win, unsigned int width, unsigned int height)
  215. {
  216. float sz = 1.5;
  217. glViewport(0, 0, width, height);
  218. glMatrixMode(GL_PROJECTION);
  219. glLoadIdentity();
  220. glOrtho(-sz, sz, -sz, sz, -1.0, 1.0);
  221. glMatrixMode(GL_MODELVIEW);
  222. }
  223. static void
  224. Redraw(Display *dpy, Window win, float rot)
  225. {
  226. glClearColor(0.25, 0.25, 0.25, 0.0);
  227. glClear(GL_COLOR_BUFFER_BIT);
  228. glPushMatrix();
  229. glRotatef(rot, 0, 0, 1);
  230. glRotatef(2.0 * rot, 1, 0, 0);
  231. glBegin(GL_QUADS);
  232. glTexCoord2d(0.0, bottom);
  233. glVertex2f(-1, -1);
  234. glTexCoord2d(1.0, bottom);
  235. glVertex2f( 1, -1);
  236. glTexCoord2d(1.0, top);
  237. glVertex2d(1.0, 1.0);
  238. glTexCoord2d(0.0, top);
  239. glVertex2f(-1.0, 1.0);
  240. glEnd();
  241. glPopMatrix();
  242. glXSwapBuffers(dpy, win);
  243. }
  244. static void
  245. EventLoop(Display *dpy, Window win)
  246. {
  247. GLfloat rot = 0.0;
  248. int anim = 0;
  249. while (1) {
  250. if (!anim || XPending(dpy) > 0) {
  251. XEvent event;
  252. XNextEvent(dpy, &event);
  253. switch (event.type) {
  254. case Expose:
  255. Redraw(dpy, win, rot);
  256. break;
  257. case ConfigureNotify:
  258. Resize(event.xany.window,
  259. event.xconfigure.width,
  260. event.xconfigure.height);
  261. break;
  262. case KeyPress:
  263. {
  264. char buf[100];
  265. KeySym keySym;
  266. XComposeStatus stat;
  267. XLookupString(&event.xkey, buf, sizeof(buf), &keySym, &stat);
  268. if (keySym == XK_Escape) {
  269. return; /* exit */
  270. }
  271. else if (keySym == XK_r) {
  272. rot += 1.0;
  273. Redraw(dpy, win, rot);
  274. }
  275. else if (keySym == XK_a) {
  276. anim = !anim;
  277. }
  278. else if (keySym == XK_R) {
  279. rot -= 1.0;
  280. Redraw(dpy, win, rot);
  281. }
  282. }
  283. break;
  284. default:
  285. ; /*no-op*/
  286. }
  287. }
  288. else {
  289. /* animate */
  290. rot += 1.0;
  291. Redraw(dpy, win, rot);
  292. }
  293. }
  294. }
  295. int
  296. main(int argc, char *argv[])
  297. {
  298. Display *dpy;
  299. GLXFBConfig pixmapConfig;
  300. XVisualInfo *windowVis;
  301. GLXPixmap gp;
  302. Window win;
  303. GLXContext ctx;
  304. Pixmap p;
  305. dpy = OpenDisplay();
  306. pixmapConfig = ChoosePixmapFBConfig(dpy);
  307. windowVis = ChooseWindowVisual(dpy);
  308. win = CreateWindow(dpy, windowVis, 500, 500, "Texture From Pixmap");
  309. gp = CreatePixmap(dpy, pixmapConfig, 512, 512, &p);
  310. DrawPixmapImage(dpy, p, 512, 512);
  311. ctx = glXCreateContext(dpy, windowVis, NULL, True);
  312. if (!ctx) {
  313. printf("Couldn't create GLX context\n");
  314. exit(1);
  315. }
  316. glXMakeCurrent(dpy, win, ctx);
  317. BindPixmapTexture(dpy, gp);
  318. EventLoop(dpy, win);
  319. return 0;
  320. }