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.

bindtex.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /*
  2. * Simple demo for eglBindTexImage. Based on xegl_tri.c by
  3. *
  4. * Copyright (C) 2008 Brian Paul All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included
  14. * in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  20. * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. /*
  24. * The spec says that eglBindTexImage supports only OpenGL ES context, but this
  25. * demo uses OpenGL context. Keep in mind that this is non-standard.
  26. */
  27. #include <math.h>
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <X11/Xlib.h>
  32. #include <X11/Xutil.h>
  33. #include <X11/keysym.h>
  34. #include <GLES/gl.h>
  35. #include <EGL/egl.h>
  36. static EGLDisplay dpy;
  37. static EGLContext ctx_win, ctx_pbuf;
  38. static EGLSurface surf_win, surf_pbuf;
  39. static GLuint tex_pbuf;
  40. static GLfloat view_rotx = 0.0, view_roty = 0.0, view_rotz = 0.0;
  41. static GLboolean blend = GL_TRUE;
  42. static GLuint color_flow;
  43. static void
  44. make_pbuffer(int width, int height)
  45. {
  46. static const EGLint config_attribs[] = {
  47. EGL_RED_SIZE, 8,
  48. EGL_GREEN_SIZE, 8,
  49. EGL_BLUE_SIZE, 8,
  50. EGL_BIND_TO_TEXTURE_RGB, EGL_TRUE,
  51. EGL_NONE
  52. };
  53. EGLint pbuf_attribs[] = {
  54. EGL_WIDTH, width,
  55. EGL_HEIGHT, height,
  56. EGL_TEXTURE_FORMAT, EGL_TEXTURE_RGB,
  57. EGL_TEXTURE_TARGET, EGL_TEXTURE_2D,
  58. EGL_NONE
  59. };
  60. EGLConfig config;
  61. EGLint num_configs;
  62. if (!eglChooseConfig(dpy, config_attribs, &config, 1, &num_configs) ||
  63. !num_configs) {
  64. printf("Error: couldn't get an EGL visual config for pbuffer\n");
  65. exit(1);
  66. }
  67. ctx_pbuf = eglCreateContext(dpy, config, EGL_NO_CONTEXT, NULL );
  68. surf_pbuf = eglCreatePbufferSurface(dpy, config, pbuf_attribs);
  69. if (surf_pbuf == EGL_NO_SURFACE) {
  70. printf("failed to allocate pbuffer\n");
  71. exit(1);
  72. }
  73. }
  74. static void
  75. use_pbuffer(void)
  76. {
  77. static int initialized;
  78. eglMakeCurrent(dpy, surf_pbuf, surf_pbuf, ctx_pbuf);
  79. if (!initialized) {
  80. EGLint width, height;
  81. GLfloat ar;
  82. initialized = 1;
  83. eglQuerySurface(dpy, surf_pbuf, EGL_WIDTH, &width);
  84. eglQuerySurface(dpy, surf_pbuf, EGL_WIDTH, &height);
  85. ar = (GLfloat) width / (GLfloat) height;
  86. glViewport(0, 0, (GLint) width, (GLint) height);
  87. glMatrixMode(GL_PROJECTION);
  88. glLoadIdentity();
  89. glFrustumf(-ar, ar, -1, 1, 1.0, 10.0);
  90. glMatrixMode(GL_MODELVIEW);
  91. glLoadIdentity();
  92. /* y-inverted */
  93. glScalef(1.0, -1.0, 1.0);
  94. glTranslatef(0.0, 0.0, -5.0);
  95. glClearColor(0.2, 0.2, 0.2, 0.0);
  96. glGenTextures(1, &tex_pbuf);
  97. }
  98. }
  99. static void
  100. make_window(Display *x_dpy, const char *name,
  101. int x, int y, int width, int height,
  102. Window *winRet)
  103. {
  104. static const EGLint attribs[] = {
  105. EGL_RED_SIZE, 8,
  106. EGL_GREEN_SIZE, 8,
  107. EGL_BLUE_SIZE, 8,
  108. EGL_ALPHA_SIZE, 8,
  109. EGL_DEPTH_SIZE, 8,
  110. EGL_NONE
  111. };
  112. int scrnum;
  113. XSetWindowAttributes attr;
  114. unsigned long mask;
  115. Window root;
  116. Window win;
  117. XVisualInfo *visInfo, visTemplate;
  118. int num_visuals;
  119. EGLConfig config;
  120. EGLint num_configs, vid;
  121. scrnum = DefaultScreen( x_dpy );
  122. root = RootWindow( x_dpy, scrnum );
  123. if (!eglChooseConfig(dpy, attribs, &config, 1, &num_configs) ||
  124. !num_configs) {
  125. printf("Error: couldn't get an EGL visual config\n");
  126. exit(1);
  127. }
  128. if (!eglGetConfigAttrib(dpy, config, EGL_NATIVE_VISUAL_ID, &vid)) {
  129. printf("Error: eglGetConfigAttrib() failed\n");
  130. exit(1);
  131. }
  132. /* The X window visual must match the EGL config */
  133. visTemplate.visualid = vid;
  134. visInfo = XGetVisualInfo(x_dpy, VisualIDMask, &visTemplate, &num_visuals);
  135. if (!visInfo) {
  136. printf("Error: couldn't get X visual\n");
  137. exit(1);
  138. }
  139. /* window attributes */
  140. attr.background_pixel = 0;
  141. attr.border_pixel = 0;
  142. attr.colormap = XCreateColormap( x_dpy, root, visInfo->visual, AllocNone);
  143. attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
  144. attr.override_redirect = 0;
  145. mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask | CWOverrideRedirect;
  146. win = XCreateWindow( x_dpy, root, 0, 0, width, height,
  147. 0, visInfo->depth, InputOutput,
  148. visInfo->visual, mask, &attr );
  149. /* set hints and properties */
  150. {
  151. XSizeHints sizehints;
  152. sizehints.x = x;
  153. sizehints.y = y;
  154. sizehints.width = width;
  155. sizehints.height = height;
  156. sizehints.flags = USSize | USPosition;
  157. XSetNormalHints(x_dpy, win, &sizehints);
  158. XSetStandardProperties(x_dpy, win, name, name,
  159. None, (char **)NULL, 0, &sizehints);
  160. }
  161. ctx_win = eglCreateContext(dpy, config, EGL_NO_CONTEXT, NULL );
  162. if (!ctx_win) {
  163. printf("Error: eglCreateContext failed\n");
  164. exit(1);
  165. }
  166. surf_win = eglCreateWindowSurface(dpy, config, win, NULL);
  167. XFree(visInfo);
  168. *winRet = win;
  169. }
  170. static void
  171. use_window(void)
  172. {
  173. static int initialized;
  174. eglMakeCurrent(dpy, surf_win, surf_win, ctx_win);
  175. if (!initialized) {
  176. initialized = 1;
  177. glEnable(GL_TEXTURE_2D);
  178. glBindTexture(GL_TEXTURE_2D, tex_pbuf);
  179. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  180. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  181. }
  182. }
  183. static void
  184. draw_triangle(void)
  185. {
  186. static const GLfloat verts[3][2] = {
  187. { -3, -3 },
  188. { 3, -3 },
  189. { 0, 3 }
  190. };
  191. GLfloat colors[3][4] = {
  192. { 1, 0, 0, 1 },
  193. { 0, 1, 0, 1 },
  194. { 0, 0, 1, 1 }
  195. };
  196. GLint i;
  197. /* flow the color */
  198. for (i = 0; i < 3; i++) {
  199. GLint first = (i + color_flow / 256) % 3;
  200. GLint second = (first + 1) % 3;
  201. GLint third = (second + 1) % 3;
  202. GLfloat c = (color_flow % 256) / 256.0f;
  203. c = c * c * c;
  204. colors[i][first] = 1.0f - c;
  205. colors[i][second] = c;
  206. colors[i][third] = 0.0f;
  207. }
  208. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  209. glVertexPointer(2, GL_FLOAT, 0, verts);
  210. glColorPointer(4, GL_FLOAT, 0, colors);
  211. glEnableClientState(GL_VERTEX_ARRAY);
  212. glEnableClientState(GL_COLOR_ARRAY);
  213. glDrawArrays(GL_TRIANGLES, 0, 3);
  214. glDisableClientState(GL_VERTEX_ARRAY);
  215. glDisableClientState(GL_COLOR_ARRAY);
  216. }
  217. static void
  218. draw_textured_cube(void)
  219. {
  220. static const GLfloat verts[][2] = {
  221. { -4, -4 },
  222. { 4, -4 },
  223. { 4, 4 },
  224. { -4, 4 }
  225. };
  226. static const GLfloat colors[][4] = {
  227. { 1, 1, 1, 0.5 },
  228. { 1, 1, 1, 0.5 },
  229. { 1, 1, 1, 0.5 },
  230. { 1, 1, 1, 0.5 }
  231. };
  232. static const GLfloat texs[][2] = {
  233. { 0, 0 },
  234. { 1, 0 },
  235. { 1, 1 },
  236. { 0, 1 }
  237. };
  238. static const GLfloat xforms[6][4] = {
  239. { 0, 0, 1, 0 },
  240. { 90, 0, 1, 0 },
  241. { 180, 0, 1, 0 },
  242. { 270, 0, 1, 0 },
  243. { 90, 1, 0, 0 },
  244. { -90, 1, 0, 0 }
  245. };
  246. GLint i;
  247. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  248. if (blend) {
  249. glDisable(GL_DEPTH_TEST);
  250. glEnable(GL_BLEND);
  251. } else {
  252. glEnable(GL_DEPTH_TEST);
  253. glDisable(GL_BLEND);
  254. }
  255. glVertexPointer(2, GL_FLOAT, 0, verts);
  256. glColorPointer(4, GL_FLOAT, 0, colors);
  257. glTexCoordPointer(2, GL_FLOAT, 0, texs);
  258. glEnableClientState(GL_VERTEX_ARRAY);
  259. glEnableClientState(GL_COLOR_ARRAY);
  260. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  261. for (i = 0; i < 6; i++) {
  262. glPushMatrix();
  263. glRotatef(xforms[i][0], xforms[i][1], xforms[i][2], xforms[i][3]);
  264. glTranslatef(0, 0, 4.1);
  265. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  266. glPopMatrix();
  267. }
  268. glDisableClientState(GL_VERTEX_ARRAY);
  269. glDisableClientState(GL_COLOR_ARRAY);
  270. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  271. }
  272. static void
  273. draw(void)
  274. {
  275. use_pbuffer();
  276. draw_triangle();
  277. use_window();
  278. eglBindTexImage(dpy, surf_pbuf, EGL_BACK_BUFFER);
  279. glPushMatrix();
  280. glRotatef(view_rotx, 1, 0, 0);
  281. glRotatef(view_roty, 0, 1, 0);
  282. glRotatef(view_rotz, 0, 0, 1);
  283. draw_textured_cube();
  284. glPopMatrix();
  285. eglReleaseTexImage(dpy, surf_pbuf, EGL_BACK_BUFFER);
  286. }
  287. /* new window size or exposure */
  288. static void
  289. reshape(int width, int height)
  290. {
  291. GLfloat ar = (GLfloat) width / (GLfloat) height;
  292. use_window();
  293. glViewport(0, 0, (GLint) width, (GLint) height);
  294. glMatrixMode(GL_PROJECTION);
  295. glLoadIdentity();
  296. glFrustumf(-ar, ar, -1, 1, 5.0, 60.0);
  297. glMatrixMode(GL_MODELVIEW);
  298. glLoadIdentity();
  299. glTranslatef(0.0, 0.0, -40.0);
  300. }
  301. static void
  302. event_loop(Display *x_dpy, Window win)
  303. {
  304. while (1) {
  305. int redraw = 1;
  306. if (XPending(x_dpy) > 0) {
  307. XEvent event;
  308. XNextEvent(x_dpy, &event);
  309. switch (event.type) {
  310. case Expose:
  311. redraw = 1;
  312. break;
  313. case ConfigureNotify:
  314. reshape(event.xconfigure.width, event.xconfigure.height);
  315. break;
  316. case KeyPress:
  317. {
  318. char buffer[10];
  319. int r, code;
  320. code = XLookupKeysym(&event.xkey, 0);
  321. if (code == XK_Left) {
  322. view_roty += 5.0;
  323. }
  324. else if (code == XK_Right) {
  325. view_roty -= 5.0;
  326. }
  327. else if (code == XK_Up) {
  328. view_rotx += 5.0;
  329. }
  330. else if (code == XK_Down) {
  331. view_rotx -= 5.0;
  332. }
  333. else if (code == XK_b) {
  334. blend = !blend;
  335. }
  336. else {
  337. r = XLookupString(&event.xkey, buffer, sizeof(buffer),
  338. NULL, NULL);
  339. if (buffer[0] == 27) {
  340. /* escape */
  341. return;
  342. }
  343. }
  344. }
  345. redraw = 1;
  346. break;
  347. default:
  348. ; /*no-op*/
  349. }
  350. }
  351. if (redraw) {
  352. view_rotx += 1.0;
  353. view_roty += 2.0;
  354. view_rotz += 1.5;
  355. color_flow += 20;
  356. draw();
  357. eglSwapBuffers(dpy, surf_win);
  358. }
  359. }
  360. }
  361. int
  362. main(int argc, char *argv[])
  363. {
  364. const int winWidth = 300, winHeight = 300;
  365. Display *x_dpy;
  366. Window win;
  367. char *dpyName = NULL;
  368. EGLint egl_major, egl_minor;
  369. const char *s;
  370. x_dpy = XOpenDisplay(dpyName);
  371. if (!x_dpy) {
  372. printf("Error: couldn't open display %s\n",
  373. dpyName ? dpyName : getenv("DISPLAY"));
  374. return -1;
  375. }
  376. dpy = eglGetDisplay(x_dpy);
  377. if (!dpy) {
  378. printf("Error: eglGetDisplay() failed\n");
  379. return -1;
  380. }
  381. if (!eglInitialize(dpy, &egl_major, &egl_minor)) {
  382. printf("Error: eglInitialize() failed\n");
  383. return -1;
  384. }
  385. s = eglQueryString(dpy, EGL_VERSION);
  386. printf("EGL_VERSION = %s\n", s);
  387. make_window(x_dpy, "color flow", 0, 0, winWidth, winHeight, &win);
  388. make_pbuffer(winWidth, winHeight);
  389. XMapWindow(x_dpy, win);
  390. reshape(winWidth, winHeight);
  391. event_loop(x_dpy, win);
  392. glDeleteTextures(1, &tex_pbuf);
  393. eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
  394. eglTerminate(dpy);
  395. XDestroyWindow(x_dpy, win);
  396. XCloseDisplay(x_dpy);
  397. return 0;
  398. }