PageRenderTime 55ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/mesa-demos-20101028/src/egl/opengles1/pbuffer.c

#
C | 608 lines | 466 code | 113 blank | 29 comment | 56 complexity | ae42093dd582b0b0ae2dfd07aa539ef9 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*
  2. * Copyright (C) 2008 Tunsgten Graphics,Inc. All Rights Reserved.
  3. */
  4. /*
  5. * Test EGL Pbuffers
  6. * Brian Paul
  7. * August 2008
  8. */
  9. #include <assert.h>
  10. #include <math.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <X11/Xlib.h>
  15. #include <X11/Xutil.h>
  16. #include <X11/keysym.h>
  17. #include <GLES/gl.h>
  18. #include <GLES/glext.h>
  19. #include <EGL/egl.h>
  20. static int WinWidth = 300, WinHeight = 300;
  21. static GLfloat view_rotx = 0.0, view_roty = 0.0, view_rotz = 0.0;
  22. static void
  23. Normal(GLfloat *n, GLfloat nx, GLfloat ny, GLfloat nz)
  24. {
  25. n[0] = nx;
  26. n[1] = ny;
  27. n[2] = nz;
  28. }
  29. static void
  30. Vertex(GLfloat *v, GLfloat vx, GLfloat vy, GLfloat vz)
  31. {
  32. v[0] = vx;
  33. v[1] = vy;
  34. v[2] = vz;
  35. }
  36. static void
  37. Texcoord(GLfloat *v, GLfloat s, GLfloat t)
  38. {
  39. v[0] = s;
  40. v[1] = t;
  41. }
  42. /* Borrowed from glut, adapted */
  43. static void
  44. draw_torus(GLfloat r, GLfloat R, GLint nsides, GLint rings)
  45. {
  46. int i, j;
  47. GLfloat theta, phi, theta1;
  48. GLfloat cosTheta, sinTheta;
  49. GLfloat cosTheta1, sinTheta1;
  50. GLfloat ringDelta, sideDelta;
  51. GLfloat varray[100][3], narray[100][3], tarray[100][2];
  52. int vcount;
  53. glVertexPointer(3, GL_FLOAT, 0, varray);
  54. glNormalPointer(GL_FLOAT, 0, narray);
  55. glTexCoordPointer(2, GL_FLOAT, 0, tarray);
  56. glEnableClientState(GL_VERTEX_ARRAY);
  57. glEnableClientState(GL_NORMAL_ARRAY);
  58. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  59. ringDelta = 2.0 * M_PI / rings;
  60. sideDelta = 2.0 * M_PI / nsides;
  61. theta = 0.0;
  62. cosTheta = 1.0;
  63. sinTheta = 0.0;
  64. for (i = rings - 1; i >= 0; i--) {
  65. theta1 = theta + ringDelta;
  66. cosTheta1 = cos(theta1);
  67. sinTheta1 = sin(theta1);
  68. vcount = 0; /* glBegin(GL_QUAD_STRIP); */
  69. phi = 0.0;
  70. for (j = nsides; j >= 0; j--) {
  71. GLfloat s0, s1, t;
  72. GLfloat cosPhi, sinPhi, dist;
  73. phi += sideDelta;
  74. cosPhi = cos(phi);
  75. sinPhi = sin(phi);
  76. dist = R + r * cosPhi;
  77. s0 = 20.0 * theta / (2.0 * M_PI);
  78. s1 = 20.0 * theta1 / (2.0 * M_PI);
  79. t = 8.0 * phi / (2.0 * M_PI);
  80. Normal(narray[vcount], cosTheta1 * cosPhi, -sinTheta1 * cosPhi, sinPhi);
  81. Texcoord(tarray[vcount], s1, t);
  82. Vertex(varray[vcount], cosTheta1 * dist, -sinTheta1 * dist, r * sinPhi);
  83. vcount++;
  84. Normal(narray[vcount], cosTheta * cosPhi, -sinTheta * cosPhi, sinPhi);
  85. Texcoord(tarray[vcount], s0, t);
  86. Vertex(varray[vcount], cosTheta * dist, -sinTheta * dist, r * sinPhi);
  87. vcount++;
  88. }
  89. /*glEnd();*/
  90. assert(vcount <= 100);
  91. glDrawArrays(GL_TRIANGLE_STRIP, 0, vcount);
  92. theta = theta1;
  93. cosTheta = cosTheta1;
  94. sinTheta = sinTheta1;
  95. }
  96. glDisableClientState(GL_VERTEX_ARRAY);
  97. glDisableClientState(GL_NORMAL_ARRAY);
  98. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  99. }
  100. static void
  101. draw(void)
  102. {
  103. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  104. glPushMatrix();
  105. glRotatef(view_rotx, 1, 0, 0);
  106. glRotatef(view_roty, 0, 1, 0);
  107. glRotatef(view_rotz, 0, 0, 1);
  108. glScalef(0.5, 0.5, 0.5);
  109. draw_torus(1.0, 3.0, 30, 60);
  110. glPopMatrix();
  111. glFinish();
  112. }
  113. /**
  114. * Draw to both the window and pbuffer and compare results.
  115. */
  116. static void
  117. draw_both(EGLDisplay egl_dpy, EGLSurface egl_surf, EGLSurface egl_pbuf,
  118. EGLContext egl_ctx)
  119. {
  120. unsigned *wbuf, *pbuf;
  121. int x = 100, y = 110;
  122. int i, dif;
  123. wbuf = (unsigned *) malloc(WinWidth * WinHeight * 4);
  124. pbuf = (unsigned *) malloc(WinWidth * WinHeight * 4);
  125. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  126. /* first draw to window */
  127. if (!eglMakeCurrent(egl_dpy, egl_surf, egl_surf, egl_ctx)) {
  128. printf("Error: eglMakeCurrent(window) failed\n");
  129. return;
  130. }
  131. draw();
  132. glReadPixels(0, 0, WinWidth, WinHeight, GL_RGBA, GL_UNSIGNED_BYTE, wbuf);
  133. printf("Window[%d,%d] = 0x%08x\n", x, y, wbuf[y*WinWidth+x]);
  134. eglSwapBuffers(egl_dpy, egl_surf);
  135. /* then draw to pbuffer */
  136. if (!eglMakeCurrent(egl_dpy, egl_pbuf, egl_pbuf, egl_ctx)) {
  137. printf("Error: eglMakeCurrent(pbuffer) failed\n");
  138. return;
  139. }
  140. draw();
  141. glReadPixels(0, 0, WinWidth, WinHeight, GL_RGBA, GL_UNSIGNED_BYTE, pbuf);
  142. printf("Pbuffer[%d,%d] = 0x%08x\n", x, y, pbuf[y*WinWidth+x]);
  143. /* compare renderings */
  144. for (dif = i = 0; i < WinWidth * WinHeight; i++) {
  145. if (wbuf[i] != pbuf[i]) {
  146. dif = 1;
  147. break;
  148. }
  149. }
  150. if (dif)
  151. printf("Difference at %d: 0x%08x vs. 0x%08x\n", i, wbuf[i], pbuf[i]);
  152. else
  153. printf("Window rendering matches Pbuffer rendering!\n");
  154. free(wbuf);
  155. free(pbuf);
  156. }
  157. /* new window size or exposure */
  158. static void
  159. reshape(int width, int height)
  160. {
  161. GLfloat ar = (GLfloat) width / (GLfloat) height;
  162. WinWidth = width;
  163. WinHeight = height;
  164. glViewport(0, 0, (GLint) width, (GLint) height);
  165. glMatrixMode(GL_PROJECTION);
  166. glLoadIdentity();
  167. #ifdef GL_VERSION_ES_CM_1_0
  168. glFrustumf(-ar, ar, -1, 1, 5.0, 60.0);
  169. #else
  170. glFrustum(-ar, ar, -1, 1, 5.0, 60.0);
  171. #endif
  172. glMatrixMode(GL_MODELVIEW);
  173. glLoadIdentity();
  174. glTranslatef(0.0, 0.0, -15.0);
  175. }
  176. static void
  177. make_texture(void)
  178. {
  179. #define SZ 64
  180. GLenum Filter = GL_LINEAR;
  181. GLubyte image[SZ][SZ][4];
  182. GLuint i, j;
  183. for (i = 0; i < SZ; i++) {
  184. for (j = 0; j < SZ; j++) {
  185. GLfloat d = (i - SZ/2) * (i - SZ/2) + (j - SZ/2) * (j - SZ/2);
  186. d = sqrt(d);
  187. if (d < SZ/3) {
  188. image[i][j][0] = 255;
  189. image[i][j][1] = 255;
  190. image[i][j][2] = 255;
  191. image[i][j][3] = 255;
  192. }
  193. else {
  194. image[i][j][0] = 127;
  195. image[i][j][1] = 127;
  196. image[i][j][2] = 127;
  197. image[i][j][3] = 255;
  198. }
  199. }
  200. }
  201. glActiveTexture(GL_TEXTURE0); /* unit 0 */
  202. glBindTexture(GL_TEXTURE_2D, 42);
  203. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, SZ, SZ, 0,
  204. GL_RGBA, GL_UNSIGNED_BYTE, image);
  205. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, Filter);
  206. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, Filter);
  207. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  208. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  209. #undef SZ
  210. }
  211. static void
  212. init(void)
  213. {
  214. static const GLfloat red[4] = {1, 0, 0, 0};
  215. static const GLfloat white[4] = {1.0, 1.0, 1.0, 1.0};
  216. static const GLfloat diffuse[4] = {0.7, 0.7, 0.7, 1.0};
  217. static const GLfloat specular[4] = {0.001, 0.001, 0.001, 1.0};
  218. static const GLfloat pos[4] = {20, 20, 50, 1};
  219. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red);
  220. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white);
  221. glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 9.0);
  222. glEnable(GL_LIGHTING);
  223. glEnable(GL_LIGHT0);
  224. glLightfv(GL_LIGHT0, GL_POSITION, pos);
  225. glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  226. glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
  227. glClearColor(0.4, 0.4, 0.4, 0.0);
  228. glEnable(GL_DEPTH_TEST);
  229. make_texture();
  230. glEnable(GL_TEXTURE_2D);
  231. }
  232. /*
  233. * Create an RGB, double-buffered X window.
  234. * Return the window and context handles.
  235. */
  236. static void
  237. make_x_window(Display *x_dpy, EGLDisplay egl_dpy,
  238. const char *name,
  239. int x, int y, int width, int height,
  240. Window *winRet,
  241. EGLContext *ctxRet,
  242. EGLSurface *surfRet)
  243. {
  244. static const EGLint attribs[] = {
  245. EGL_RED_SIZE, 1,
  246. EGL_GREEN_SIZE, 1,
  247. EGL_BLUE_SIZE, 1,
  248. EGL_DEPTH_SIZE, 1,
  249. EGL_NONE
  250. };
  251. int scrnum;
  252. XSetWindowAttributes attr;
  253. unsigned long mask;
  254. Window root;
  255. Window win;
  256. XVisualInfo *visInfo, visTemplate;
  257. int num_visuals;
  258. EGLContext ctx;
  259. EGLConfig config;
  260. EGLint num_configs;
  261. EGLint vid;
  262. scrnum = DefaultScreen( x_dpy );
  263. root = RootWindow( x_dpy, scrnum );
  264. if (!eglChooseConfig( egl_dpy, attribs, &config, 1, &num_configs)) {
  265. printf("Error: couldn't get an EGL visual config\n");
  266. exit(1);
  267. }
  268. assert(config);
  269. assert(num_configs > 0);
  270. if (!eglGetConfigAttrib(egl_dpy, config, EGL_NATIVE_VISUAL_ID, &vid)) {
  271. printf("Error: eglGetConfigAttrib() failed\n");
  272. exit(1);
  273. }
  274. /* The X window visual must match the EGL config */
  275. visTemplate.visualid = vid;
  276. visInfo = XGetVisualInfo(x_dpy, VisualIDMask, &visTemplate, &num_visuals);
  277. if (!visInfo) {
  278. printf("Error: couldn't get X visual\n");
  279. exit(1);
  280. }
  281. /* window attributes */
  282. attr.background_pixel = 0;
  283. attr.border_pixel = 0;
  284. attr.colormap = XCreateColormap( x_dpy, root, visInfo->visual, AllocNone);
  285. attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
  286. mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
  287. win = XCreateWindow( x_dpy, root, 0, 0, width, height,
  288. 0, visInfo->depth, InputOutput,
  289. visInfo->visual, mask, &attr );
  290. /* set hints and properties */
  291. {
  292. XSizeHints sizehints;
  293. sizehints.x = x;
  294. sizehints.y = y;
  295. sizehints.width = width;
  296. sizehints.height = height;
  297. sizehints.flags = USSize | USPosition;
  298. XSetNormalHints(x_dpy, win, &sizehints);
  299. XSetStandardProperties(x_dpy, win, name, name,
  300. None, (char **)NULL, 0, &sizehints);
  301. }
  302. eglBindAPI(EGL_OPENGL_ES_API);
  303. ctx = eglCreateContext(egl_dpy, config, EGL_NO_CONTEXT, NULL );
  304. if (!ctx) {
  305. printf("Error: eglCreateContext failed\n");
  306. exit(1);
  307. }
  308. *surfRet = eglCreateWindowSurface(egl_dpy, config, win, NULL);
  309. if (!*surfRet) {
  310. printf("Error: eglCreateWindowSurface failed\n");
  311. exit(1);
  312. }
  313. XFree(visInfo);
  314. *winRet = win;
  315. *ctxRet = ctx;
  316. }
  317. static EGLSurface
  318. make_pbuffer(Display *x_dpy, EGLDisplay egl_dpy, int width, int height)
  319. {
  320. static const EGLint config_attribs[] = {
  321. EGL_RED_SIZE, 1,
  322. EGL_GREEN_SIZE, 1,
  323. EGL_BLUE_SIZE, 1,
  324. EGL_DEPTH_SIZE, 1,
  325. EGL_NONE
  326. };
  327. EGLConfig config;
  328. EGLSurface pbuf;
  329. EGLint num_configs;
  330. EGLint pbuf_attribs[5];
  331. pbuf_attribs[0] = EGL_WIDTH;
  332. pbuf_attribs[1] = width;
  333. pbuf_attribs[2] = EGL_HEIGHT;
  334. pbuf_attribs[3] = height;
  335. pbuf_attribs[4] = EGL_NONE;
  336. if (!eglChooseConfig( egl_dpy, config_attribs, &config, 1, &num_configs)) {
  337. printf("Error: couldn't get an EGL config for pbuffer\n");
  338. exit(1);
  339. }
  340. pbuf = eglCreatePbufferSurface(egl_dpy, config, pbuf_attribs);
  341. return pbuf;
  342. }
  343. static void
  344. event_loop(Display *dpy, Window win,
  345. EGLDisplay egl_dpy, EGLSurface egl_surf, EGLSurface egl_pbuf,
  346. EGLContext egl_ctx)
  347. {
  348. int anim = 0;
  349. while (1) {
  350. int redraw = 0;
  351. if (!anim || XPending(dpy)) {
  352. XEvent event;
  353. XNextEvent(dpy, &event);
  354. switch (event.type) {
  355. case Expose:
  356. redraw = 1;
  357. break;
  358. case ConfigureNotify:
  359. if (event.xconfigure.window == win)
  360. reshape(event.xconfigure.width, event.xconfigure.height);
  361. break;
  362. case KeyPress:
  363. {
  364. char buffer[10];
  365. int r, code;
  366. code = XLookupKeysym(&event.xkey, 0);
  367. if (code == XK_Left) {
  368. view_roty += 5.0;
  369. }
  370. else if (code == XK_Right) {
  371. view_roty -= 5.0;
  372. }
  373. else if (code == XK_Up) {
  374. view_rotx += 5.0;
  375. }
  376. else if (code == XK_Down) {
  377. view_rotx -= 5.0;
  378. }
  379. else {
  380. r = XLookupString(&event.xkey, buffer, sizeof(buffer),
  381. NULL, NULL);
  382. if (buffer[0] == ' ') {
  383. anim = !anim;
  384. }
  385. else if (buffer[0] == 27) {
  386. /* escape */
  387. return;
  388. }
  389. }
  390. }
  391. redraw = 1;
  392. break;
  393. default:
  394. ; /*no-op*/
  395. }
  396. }
  397. if (anim) {
  398. view_rotx += 1.0;
  399. view_roty += 2.0;
  400. redraw = 1;
  401. }
  402. if (redraw) {
  403. draw_both(egl_dpy, egl_surf, egl_pbuf, egl_ctx);
  404. }
  405. }
  406. }
  407. static void
  408. usage(void)
  409. {
  410. printf("Usage:\n");
  411. printf(" -display <displayname> set the display to run on\n");
  412. printf(" -info display OpenGL renderer info\n");
  413. }
  414. int
  415. main(int argc, char *argv[])
  416. {
  417. Display *x_dpy;
  418. Window win;
  419. EGLSurface egl_surf, egl_pbuf;
  420. EGLContext egl_ctx;
  421. EGLDisplay egl_dpy;
  422. char *dpyName = NULL;
  423. GLboolean printInfo = GL_FALSE;
  424. EGLint egl_major, egl_minor;
  425. int i;
  426. const char *s;
  427. for (i = 1; i < argc; i++) {
  428. if (strcmp(argv[i], "-display") == 0) {
  429. dpyName = argv[i+1];
  430. i++;
  431. }
  432. else if (strcmp(argv[i], "-info") == 0) {
  433. printInfo = GL_TRUE;
  434. }
  435. else {
  436. usage();
  437. return -1;
  438. }
  439. }
  440. x_dpy = XOpenDisplay(dpyName);
  441. if (!x_dpy) {
  442. printf("Error: couldn't open display %s\n",
  443. dpyName ? dpyName : getenv("DISPLAY"));
  444. return -1;
  445. }
  446. egl_dpy = eglGetDisplay(x_dpy);
  447. if (!egl_dpy) {
  448. printf("Error: eglGetDisplay() failed\n");
  449. return -1;
  450. }
  451. if (!eglInitialize(egl_dpy, &egl_major, &egl_minor)) {
  452. printf("Error: eglInitialize() failed\n");
  453. return -1;
  454. }
  455. s = eglQueryString(egl_dpy, EGL_VERSION);
  456. printf("EGL_VERSION = %s\n", s);
  457. s = eglQueryString(egl_dpy, EGL_VENDOR);
  458. printf("EGL_VENDOR = %s\n", s);
  459. s = eglQueryString(egl_dpy, EGL_EXTENSIONS);
  460. printf("EGL_EXTENSIONS = %s\n", s);
  461. s = eglQueryString(egl_dpy, EGL_CLIENT_APIS);
  462. printf("EGL_CLIENT_APIS = %s\n", s);
  463. make_x_window(x_dpy, egl_dpy,
  464. "pbuffer", 0, 0, WinWidth, WinHeight,
  465. &win, &egl_ctx, &egl_surf);
  466. egl_pbuf = make_pbuffer(x_dpy, egl_dpy, WinWidth, WinHeight);
  467. if (!egl_pbuf) {
  468. printf("Error: eglCreatePBufferSurface() failed\n");
  469. return -1;
  470. }
  471. XMapWindow(x_dpy, win);
  472. if (!eglMakeCurrent(egl_dpy, egl_surf, egl_surf, egl_ctx)) {
  473. printf("Error: eglMakeCurrent() failed\n");
  474. return -1;
  475. }
  476. if (printInfo) {
  477. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  478. printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
  479. printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
  480. printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
  481. }
  482. init();
  483. /* Set initial projection/viewing transformation.
  484. * We can't be sure we'll get a ConfigureNotify event when the window
  485. * first appears.
  486. */
  487. reshape(WinWidth, WinHeight);
  488. event_loop(x_dpy, win, egl_dpy, egl_surf, egl_pbuf, egl_ctx);
  489. eglDestroyContext(egl_dpy, egl_ctx);
  490. eglDestroySurface(egl_dpy, egl_surf);
  491. eglTerminate(egl_dpy);
  492. XDestroyWindow(x_dpy, win);
  493. XCloseDisplay(x_dpy);
  494. return 0;
  495. }