Clone of mesa.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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 <GL/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_RENDERABLE_TYPE, EGL_OPENGL_BIT,
  52. EGL_NONE
  53. };
  54. EGLint pbuf_attribs[] = {
  55. EGL_WIDTH, width,
  56. EGL_HEIGHT, height,
  57. EGL_TEXTURE_FORMAT, EGL_TEXTURE_RGB,
  58. EGL_TEXTURE_TARGET, EGL_TEXTURE_2D,
  59. EGL_NONE
  60. };
  61. EGLConfig config;
  62. EGLint num_configs;
  63. if (!eglChooseConfig(dpy, config_attribs, &config, 1, &num_configs) ||
  64. !num_configs) {
  65. printf("Error: couldn't get an EGL visual config for pbuffer\n");
  66. exit(1);
  67. }
  68. eglBindAPI(EGL_OPENGL_API);
  69. ctx_pbuf = eglCreateContext(dpy, config, EGL_NO_CONTEXT, NULL );
  70. surf_pbuf = eglCreatePbufferSurface(dpy, config, pbuf_attribs);
  71. if (surf_pbuf == EGL_NO_SURFACE) {
  72. printf("failed to allocate pbuffer\n");
  73. exit(1);
  74. }
  75. }
  76. static void
  77. use_pbuffer(void)
  78. {
  79. static int initialized;
  80. eglMakeCurrent(dpy, surf_pbuf, surf_pbuf, ctx_pbuf);
  81. if (!initialized) {
  82. EGLint width, height;
  83. GLfloat ar;
  84. initialized = 1;
  85. eglQuerySurface(dpy, surf_pbuf, EGL_WIDTH, &width);
  86. eglQuerySurface(dpy, surf_pbuf, EGL_WIDTH, &height);
  87. ar = (GLfloat) width / (GLfloat) height;
  88. glViewport(0, 0, (GLint) width, (GLint) height);
  89. glMatrixMode(GL_PROJECTION);
  90. glLoadIdentity();
  91. glFrustum(-ar, ar, -1, 1, 1.0, 10.0);
  92. glMatrixMode(GL_MODELVIEW);
  93. glLoadIdentity();
  94. /* y-inverted */
  95. glScalef(1.0, -1.0, 1.0);
  96. glTranslatef(0.0, 0.0, -5.0);
  97. glClearColor(0.2, 0.2, 0.2, 0.0);
  98. glGenTextures(1, &tex_pbuf);
  99. }
  100. }
  101. static void
  102. make_window(Display *x_dpy, const char *name,
  103. int x, int y, int width, int height,
  104. Window *winRet)
  105. {
  106. static const EGLint attribs[] = {
  107. EGL_RED_SIZE, 8,
  108. EGL_GREEN_SIZE, 8,
  109. EGL_BLUE_SIZE, 8,
  110. EGL_ALPHA_SIZE, 8,
  111. EGL_DEPTH_SIZE, 8,
  112. EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
  113. EGL_NONE
  114. };
  115. int scrnum;
  116. XSetWindowAttributes attr;
  117. unsigned long mask;
  118. Window root;
  119. Window win;
  120. XVisualInfo *visInfo, visTemplate;
  121. int num_visuals;
  122. EGLConfig config;
  123. EGLint num_configs, vid;
  124. scrnum = DefaultScreen( x_dpy );
  125. root = RootWindow( x_dpy, scrnum );
  126. if (!eglChooseConfig(dpy, attribs, &config, 1, &num_configs) ||
  127. !num_configs) {
  128. printf("Error: couldn't get an EGL visual config\n");
  129. exit(1);
  130. }
  131. if (!eglGetConfigAttrib(dpy, config, EGL_NATIVE_VISUAL_ID, &vid)) {
  132. printf("Error: eglGetConfigAttrib() failed\n");
  133. exit(1);
  134. }
  135. /* The X window visual must match the EGL config */
  136. visTemplate.visualid = vid;
  137. visInfo = XGetVisualInfo(x_dpy, VisualIDMask, &visTemplate, &num_visuals);
  138. if (!visInfo) {
  139. printf("Error: couldn't get X visual\n");
  140. exit(1);
  141. }
  142. /* window attributes */
  143. attr.background_pixel = 0;
  144. attr.border_pixel = 0;
  145. attr.colormap = XCreateColormap( x_dpy, root, visInfo->visual, AllocNone);
  146. attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
  147. attr.override_redirect = 0;
  148. mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask | CWOverrideRedirect;
  149. win = XCreateWindow( x_dpy, root, 0, 0, width, height,
  150. 0, visInfo->depth, InputOutput,
  151. visInfo->visual, mask, &attr );
  152. /* set hints and properties */
  153. {
  154. XSizeHints sizehints;
  155. sizehints.x = x;
  156. sizehints.y = y;
  157. sizehints.width = width;
  158. sizehints.height = height;
  159. sizehints.flags = USSize | USPosition;
  160. XSetNormalHints(x_dpy, win, &sizehints);
  161. XSetStandardProperties(x_dpy, win, name, name,
  162. None, (char **)NULL, 0, &sizehints);
  163. }
  164. eglBindAPI(EGL_OPENGL_API);
  165. ctx_win = eglCreateContext(dpy, config, EGL_NO_CONTEXT, NULL );
  166. if (!ctx_win) {
  167. printf("Error: glXCreateContext failed\n");
  168. exit(1);
  169. }
  170. surf_win = eglCreateWindowSurface(dpy, config, win, NULL);
  171. XFree(visInfo);
  172. *winRet = win;
  173. }
  174. static void
  175. use_window(void)
  176. {
  177. static int initialized;
  178. eglMakeCurrent(dpy, surf_win, surf_win, ctx_win);
  179. if (!initialized) {
  180. initialized = 1;
  181. glEnable(GL_TEXTURE_2D);
  182. glBindTexture(GL_TEXTURE_2D, tex_pbuf);
  183. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  184. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  185. }
  186. }
  187. static void
  188. draw_triangle(void)
  189. {
  190. static const GLfloat verts[3][2] = {
  191. { -3, -3 },
  192. { 3, -3 },
  193. { 0, 3 }
  194. };
  195. GLfloat colors[3][3] = {
  196. { 1, 0, 0 },
  197. { 0, 1, 0 },
  198. { 0, 0, 1 }
  199. };
  200. GLint i;
  201. /* flow the color */
  202. for (i = 0; i < 3; i++) {
  203. GLint first = (i + color_flow / 256) % 3;
  204. GLint second = (first + 1) % 3;
  205. GLint third = (second + 1) % 3;
  206. GLfloat c = (color_flow % 256) / 256.0f;
  207. c = c * c * c;
  208. colors[i][first] = 1.0f - c;
  209. colors[i][second] = c;
  210. colors[i][third] = 0.0f;
  211. }
  212. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  213. glVertexPointer(2, GL_FLOAT, 0, verts);
  214. glColorPointer(3, GL_FLOAT, 0, colors);
  215. glEnableClientState(GL_VERTEX_ARRAY);
  216. glEnableClientState(GL_COLOR_ARRAY);
  217. glDrawArrays(GL_TRIANGLES, 0, 3);
  218. glDisableClientState(GL_VERTEX_ARRAY);
  219. glDisableClientState(GL_COLOR_ARRAY);
  220. }
  221. static void
  222. draw_textured_cube(void)
  223. {
  224. static const GLfloat verts[][2] = {
  225. { -4, -4 },
  226. { 4, -4 },
  227. { 4, 4 },
  228. { -4, 4 }
  229. };
  230. static const GLfloat colors[][4] = {
  231. { 1, 1, 1, 0.5 },
  232. { 1, 1, 1, 0.5 },
  233. { 1, 1, 1, 0.5 },
  234. { 1, 1, 1, 0.5 }
  235. };
  236. static const GLfloat texs[][2] = {
  237. { 0, 0 },
  238. { 1, 0 },
  239. { 1, 1 },
  240. { 0, 1 }
  241. };
  242. static const GLfloat xforms[6][4] = {
  243. { 0, 0, 1, 0 },
  244. { 90, 0, 1, 0 },
  245. { 180, 0, 1, 0 },
  246. { 270, 0, 1, 0 },
  247. { 90, 1, 0, 0 },
  248. { -90, 1, 0, 0 }
  249. };
  250. GLint i;
  251. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  252. if (blend) {
  253. glDisable(GL_DEPTH_TEST);
  254. glEnable(GL_BLEND);
  255. } else {
  256. glEnable(GL_DEPTH_TEST);
  257. glDisable(GL_BLEND);
  258. }
  259. glVertexPointer(2, GL_FLOAT, 0, verts);
  260. glColorPointer(4, GL_FLOAT, 0, colors);
  261. glTexCoordPointer(2, GL_FLOAT, 0, texs);
  262. glEnableClientState(GL_VERTEX_ARRAY);
  263. glEnableClientState(GL_COLOR_ARRAY);
  264. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  265. for (i = 0; i < 6; i++) {
  266. glPushMatrix();
  267. glRotatef(xforms[i][0], xforms[i][1], xforms[i][2], xforms[i][3]);
  268. glTranslatef(0, 0, 4.1);
  269. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  270. glPopMatrix();
  271. }
  272. glDisableClientState(GL_VERTEX_ARRAY);
  273. glDisableClientState(GL_COLOR_ARRAY);
  274. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  275. }
  276. static void
  277. draw(void)
  278. {
  279. use_pbuffer();
  280. draw_triangle();
  281. use_window();
  282. eglBindTexImage(dpy, surf_pbuf, EGL_BACK_BUFFER);
  283. glPushMatrix();
  284. glRotatef(view_rotx, 1, 0, 0);
  285. glRotatef(view_roty, 0, 1, 0);
  286. glRotatef(view_rotz, 0, 0, 1);
  287. draw_textured_cube();
  288. glPopMatrix();
  289. eglReleaseTexImage(dpy, surf_pbuf, EGL_BACK_BUFFER);
  290. }
  291. /* new window size or exposure */
  292. static void
  293. reshape(int width, int height)
  294. {
  295. GLfloat ar = (GLfloat) width / (GLfloat) height;
  296. use_window();
  297. glViewport(0, 0, (GLint) width, (GLint) height);
  298. glMatrixMode(GL_PROJECTION);
  299. glLoadIdentity();
  300. glFrustum(-ar, ar, -1, 1, 5.0, 60.0);
  301. glMatrixMode(GL_MODELVIEW);
  302. glLoadIdentity();
  303. glTranslatef(0.0, 0.0, -40.0);
  304. }
  305. static void
  306. event_loop(Display *x_dpy, Window win)
  307. {
  308. while (1) {
  309. int redraw = 1;
  310. if (XPending(x_dpy) > 0) {
  311. XEvent event;
  312. XNextEvent(x_dpy, &event);
  313. switch (event.type) {
  314. case Expose:
  315. redraw = 1;
  316. break;
  317. case ConfigureNotify:
  318. reshape(event.xconfigure.width, event.xconfigure.height);
  319. break;
  320. case KeyPress:
  321. {
  322. char buffer[10];
  323. int r, code;
  324. code = XLookupKeysym(&event.xkey, 0);
  325. if (code == XK_Left) {
  326. view_roty += 5.0;
  327. }
  328. else if (code == XK_Right) {
  329. view_roty -= 5.0;
  330. }
  331. else if (code == XK_Up) {
  332. view_rotx += 5.0;
  333. }
  334. else if (code == XK_Down) {
  335. view_rotx -= 5.0;
  336. }
  337. else if (code == XK_b) {
  338. blend = !blend;
  339. }
  340. else {
  341. r = XLookupString(&event.xkey, buffer, sizeof(buffer),
  342. NULL, NULL);
  343. if (buffer[0] == 27) {
  344. /* escape */
  345. return;
  346. }
  347. }
  348. }
  349. redraw = 1;
  350. break;
  351. default:
  352. ; /*no-op*/
  353. }
  354. }
  355. if (redraw) {
  356. view_rotx += 1.0;
  357. view_roty += 2.0;
  358. view_rotz += 1.5;
  359. color_flow += 20;
  360. draw();
  361. eglSwapBuffers(dpy, surf_win);
  362. }
  363. }
  364. }
  365. int
  366. main(int argc, char *argv[])
  367. {
  368. const int winWidth = 300, winHeight = 300;
  369. Display *x_dpy;
  370. Window win;
  371. char *dpyName = NULL;
  372. EGLint egl_major, egl_minor;
  373. const char *s;
  374. x_dpy = XOpenDisplay(dpyName);
  375. if (!x_dpy) {
  376. printf("Error: couldn't open display %s\n",
  377. dpyName ? dpyName : getenv("DISPLAY"));
  378. return -1;
  379. }
  380. dpy = eglGetDisplay(x_dpy);
  381. if (!dpy) {
  382. printf("Error: eglGetDisplay() failed\n");
  383. return -1;
  384. }
  385. if (!eglInitialize(dpy, &egl_major, &egl_minor)) {
  386. printf("Error: eglInitialize() failed\n");
  387. return -1;
  388. }
  389. s = eglQueryString(dpy, EGL_VERSION);
  390. printf("EGL_VERSION = %s\n", s);
  391. make_window(x_dpy, "color flow", 0, 0, winWidth, winHeight, &win);
  392. make_pbuffer(winWidth, winHeight);
  393. XMapWindow(x_dpy, win);
  394. reshape(winWidth, winHeight);
  395. event_loop(x_dpy, win);
  396. glDeleteTextures(1, &tex_pbuf);
  397. eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
  398. eglTerminate(dpy);
  399. XDestroyWindow(x_dpy, win);
  400. XCloseDisplay(x_dpy);
  401. return 0;
  402. }