Clone of mesa.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. /*
  2. * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included
  12. * in all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  18. * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. /*
  22. * Ported to X/EGL/GLES. XXX Actually, uses full OpenGL ATM.
  23. * Brian Paul
  24. * 30 May 2008
  25. */
  26. /*
  27. * Command line options:
  28. * -info print GL implementation information
  29. *
  30. */
  31. #define GL_GLEXT_PROTOTYPES
  32. #define EGL_EGLEXT_PROTOTYPES
  33. #include <math.h>
  34. #include <stdlib.h>
  35. #include <stdio.h>
  36. #include <string.h>
  37. #include <X11/Xlib.h>
  38. #include <X11/Xutil.h>
  39. #include <X11/keysym.h>
  40. #include <GL/gl.h>
  41. #include <EGL/egl.h>
  42. #include <EGL/eglext.h>
  43. #define BENCHMARK
  44. #ifdef BENCHMARK
  45. /* XXX this probably isn't very portable */
  46. #include <sys/time.h>
  47. #include <unistd.h>
  48. /* return current time (in seconds) */
  49. static double
  50. current_time(void)
  51. {
  52. struct timeval tv;
  53. #ifdef __VMS
  54. (void) gettimeofday(&tv, NULL );
  55. #else
  56. struct timezone tz;
  57. (void) gettimeofday(&tv, &tz);
  58. #endif
  59. return (double) tv.tv_sec + tv.tv_usec / 1000000.0;
  60. }
  61. #else /*BENCHMARK*/
  62. /* dummy */
  63. static double
  64. current_time(void)
  65. {
  66. /* update this function for other platforms! */
  67. static double t = 0.0;
  68. static int warn = 1;
  69. if (warn) {
  70. fprintf(stderr, "Warning: current_time() not implemented!!\n");
  71. warn = 0;
  72. }
  73. return t += 1.0;
  74. }
  75. #endif /*BENCHMARK*/
  76. #ifndef M_PI
  77. #define M_PI 3.14159265
  78. #endif
  79. static GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
  80. static GLint gear1, gear2, gear3;
  81. static GLfloat angle = 0.0;
  82. /*
  83. *
  84. * Draw a gear wheel. You'll probably want to call this function when
  85. * building a display list since we do a lot of trig here.
  86. *
  87. * Input: inner_radius - radius of hole at center
  88. * outer_radius - radius at center of teeth
  89. * width - width of gear
  90. * teeth - number of teeth
  91. * tooth_depth - depth of tooth
  92. */
  93. static void
  94. gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
  95. GLint teeth, GLfloat tooth_depth)
  96. {
  97. GLint i;
  98. GLfloat r0, r1, r2;
  99. GLfloat angle, da;
  100. GLfloat u, v, len;
  101. r0 = inner_radius;
  102. r1 = outer_radius - tooth_depth / 2.0;
  103. r2 = outer_radius + tooth_depth / 2.0;
  104. da = 2.0 * M_PI / teeth / 4.0;
  105. glShadeModel(GL_FLAT);
  106. glNormal3f(0.0, 0.0, 1.0);
  107. /* draw front face */
  108. glBegin(GL_QUAD_STRIP);
  109. for (i = 0; i <= teeth; i++) {
  110. angle = i * 2.0 * M_PI / teeth;
  111. glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
  112. glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
  113. if (i < teeth) {
  114. glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
  115. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  116. width * 0.5);
  117. }
  118. }
  119. glEnd();
  120. /* draw front sides of teeth */
  121. glBegin(GL_QUADS);
  122. da = 2.0 * M_PI / teeth / 4.0;
  123. for (i = 0; i < teeth; i++) {
  124. angle = i * 2.0 * M_PI / teeth;
  125. glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
  126. glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
  127. glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
  128. width * 0.5);
  129. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  130. width * 0.5);
  131. }
  132. glEnd();
  133. glNormal3f(0.0, 0.0, -1.0);
  134. /* draw back face */
  135. glBegin(GL_QUAD_STRIP);
  136. for (i = 0; i <= teeth; i++) {
  137. angle = i * 2.0 * M_PI / teeth;
  138. glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
  139. glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
  140. if (i < teeth) {
  141. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  142. -width * 0.5);
  143. glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
  144. }
  145. }
  146. glEnd();
  147. /* draw back sides of teeth */
  148. glBegin(GL_QUADS);
  149. da = 2.0 * M_PI / teeth / 4.0;
  150. for (i = 0; i < teeth; i++) {
  151. angle = i * 2.0 * M_PI / teeth;
  152. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  153. -width * 0.5);
  154. glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
  155. -width * 0.5);
  156. glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
  157. glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
  158. }
  159. glEnd();
  160. /* draw outward faces of teeth */
  161. glBegin(GL_QUAD_STRIP);
  162. for (i = 0; i < teeth; i++) {
  163. angle = i * 2.0 * M_PI / teeth;
  164. glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
  165. glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
  166. u = r2 * cos(angle + da) - r1 * cos(angle);
  167. v = r2 * sin(angle + da) - r1 * sin(angle);
  168. len = sqrt(u * u + v * v);
  169. u /= len;
  170. v /= len;
  171. glNormal3f(v, -u, 0.0);
  172. glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
  173. glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
  174. glNormal3f(cos(angle), sin(angle), 0.0);
  175. glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
  176. width * 0.5);
  177. glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
  178. -width * 0.5);
  179. u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
  180. v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
  181. glNormal3f(v, -u, 0.0);
  182. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  183. width * 0.5);
  184. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  185. -width * 0.5);
  186. glNormal3f(cos(angle), sin(angle), 0.0);
  187. }
  188. glVertex3f(r1 * cos(0), r1 * sin(0), width * 0.5);
  189. glVertex3f(r1 * cos(0), r1 * sin(0), -width * 0.5);
  190. glEnd();
  191. glShadeModel(GL_SMOOTH);
  192. /* draw inside radius cylinder */
  193. glBegin(GL_QUAD_STRIP);
  194. for (i = 0; i <= teeth; i++) {
  195. angle = i * 2.0 * M_PI / teeth;
  196. glNormal3f(-cos(angle), -sin(angle), 0.0);
  197. glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
  198. glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
  199. }
  200. glEnd();
  201. }
  202. static void
  203. draw(void)
  204. {
  205. glClearColor(0.2, 0.2, 0.2, 0.2);
  206. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  207. glPushMatrix();
  208. glRotatef(view_rotx, 1.0, 0.0, 0.0);
  209. glRotatef(view_roty, 0.0, 1.0, 0.0);
  210. glRotatef(view_rotz, 0.0, 0.0, 1.0);
  211. glPushMatrix();
  212. glTranslatef(-3.0, -2.0, 0.0);
  213. glRotatef(angle, 0.0, 0.0, 1.0);
  214. glCallList(gear1);
  215. glPopMatrix();
  216. glPushMatrix();
  217. glTranslatef(3.1, -2.0, 0.0);
  218. glRotatef(-2.0 * angle - 9.0, 0.0, 0.0, 1.0);
  219. glCallList(gear2);
  220. glPopMatrix();
  221. glPushMatrix();
  222. glTranslatef(-3.1, 4.2, 0.0);
  223. glRotatef(-2.0 * angle - 25.0, 0.0, 0.0, 1.0);
  224. glCallList(gear3);
  225. glPopMatrix();
  226. glPopMatrix();
  227. }
  228. /* new window size or exposure */
  229. static void
  230. reshape(int width, int height)
  231. {
  232. GLfloat ar = (GLfloat) width / (GLfloat) height;
  233. glViewport(0, 0, (GLint) width, (GLint) height);
  234. glMatrixMode(GL_PROJECTION);
  235. glLoadIdentity();
  236. glFrustum(-ar, ar, -1, 1, 5.0, 60.0);
  237. glMatrixMode(GL_MODELVIEW);
  238. glLoadIdentity();
  239. glTranslatef(0.0, 0.0, -40.0);
  240. }
  241. static void
  242. init(void)
  243. {
  244. static GLfloat pos[4] = { 5.0, 5.0, 10.0, 0.0 };
  245. static GLfloat red[4] = { 0.8, 0.1, 0.0, 1.0 };
  246. static GLfloat green[4] = { 0.0, 0.8, 0.2, 1.0 };
  247. static GLfloat blue[4] = { 0.2, 0.2, 1.0, 1.0 };
  248. glLightfv(GL_LIGHT0, GL_POSITION, pos);
  249. glEnable(GL_CULL_FACE);
  250. glEnable(GL_LIGHTING);
  251. glEnable(GL_LIGHT0);
  252. glEnable(GL_DEPTH_TEST);
  253. /* make the gears */
  254. gear1 = glGenLists(1);
  255. glNewList(gear1, GL_COMPILE);
  256. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
  257. gear(1.0, 4.0, 1.0, 20, 0.7);
  258. glEndList();
  259. gear2 = glGenLists(1);
  260. glNewList(gear2, GL_COMPILE);
  261. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
  262. gear(0.5, 2.0, 2.0, 10, 0.7);
  263. glEndList();
  264. gear3 = glGenLists(1);
  265. glNewList(gear3, GL_COMPILE);
  266. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
  267. gear(1.3, 2.0, 0.5, 10, 0.7);
  268. glEndList();
  269. glEnable(GL_NORMALIZE);
  270. }
  271. struct egl_manager {
  272. EGLNativeDisplayType xdpy;
  273. EGLNativeWindowType xwin;
  274. EGLNativePixmapType xpix;
  275. EGLDisplay dpy;
  276. EGLConfig conf;
  277. EGLContext ctx;
  278. EGLSurface win;
  279. EGLSurface pix;
  280. EGLSurface pbuf;
  281. EGLImageKHR image;
  282. EGLBoolean verbose;
  283. EGLint major, minor;
  284. GC gc;
  285. GLuint fbo;
  286. };
  287. static struct egl_manager *
  288. egl_manager_new(EGLNativeDisplayType xdpy, const EGLint *attrib_list,
  289. EGLBoolean verbose)
  290. {
  291. struct egl_manager *eman;
  292. const char *ver;
  293. EGLint num_conf;
  294. eman = calloc(1, sizeof(*eman));
  295. if (!eman)
  296. return NULL;
  297. eman->verbose = verbose;
  298. eman->xdpy = xdpy;
  299. eman->dpy = eglGetDisplay(eman->xdpy);
  300. if (eman->dpy == EGL_NO_DISPLAY) {
  301. printf("eglGetDisplay() failed\n");
  302. free(eman);
  303. return NULL;
  304. }
  305. if (!eglInitialize(eman->dpy, &eman->major, &eman->minor)) {
  306. printf("eglInitialize() failed\n");
  307. free(eman);
  308. return NULL;
  309. }
  310. ver = eglQueryString(eman->dpy, EGL_VERSION);
  311. printf("EGL_VERSION = %s\n", ver);
  312. if (!eglChooseConfig(eman->dpy, attrib_list, &eman->conf, 1, &num_conf) ||
  313. !num_conf) {
  314. printf("eglChooseConfig() failed\n");
  315. eglTerminate(eman->dpy);
  316. free(eman);
  317. return NULL;
  318. }
  319. eman->ctx = eglCreateContext(eman->dpy, eman->conf, EGL_NO_CONTEXT, NULL);
  320. if (eman->ctx == EGL_NO_CONTEXT) {
  321. printf("eglCreateContext() failed\n");
  322. eglTerminate(eman->dpy);
  323. free(eman);
  324. return NULL;
  325. }
  326. return eman;
  327. }
  328. static EGLBoolean
  329. egl_manager_create_window(struct egl_manager *eman, const char *name,
  330. EGLint w, EGLint h, EGLBoolean need_surface,
  331. EGLBoolean fullscreen, const EGLint *attrib_list)
  332. {
  333. XVisualInfo vinfo_template, *vinfo = NULL;
  334. EGLint val, num_vinfo;
  335. Window root;
  336. XSetWindowAttributes attrs;
  337. unsigned long mask;
  338. EGLint x = 0, y = 0;
  339. if (!eglGetConfigAttrib(eman->dpy, eman->conf,
  340. EGL_NATIVE_VISUAL_ID, &val)) {
  341. printf("eglGetConfigAttrib() failed\n");
  342. return EGL_FALSE;
  343. }
  344. if (val) {
  345. vinfo_template.visualid = (VisualID) val;
  346. vinfo = XGetVisualInfo(eman->xdpy, VisualIDMask, &vinfo_template, &num_vinfo);
  347. }
  348. /* try harder if window surface is not needed */
  349. if (!vinfo && !need_surface &&
  350. eglGetConfigAttrib(eman->dpy, eman->conf, EGL_BUFFER_SIZE, &val)) {
  351. if (val == 32)
  352. val = 24;
  353. vinfo_template.depth = val;
  354. vinfo = XGetVisualInfo(eman->xdpy, VisualDepthMask, &vinfo_template, &num_vinfo);
  355. }
  356. if (!vinfo) {
  357. printf("XGetVisualInfo() failed\n");
  358. return EGL_FALSE;
  359. }
  360. root = DefaultRootWindow(eman->xdpy);
  361. if (fullscreen) {
  362. x = y = 0;
  363. w = DisplayWidth(eman->xdpy, DefaultScreen(eman->xdpy));
  364. h = DisplayHeight(eman->xdpy, DefaultScreen(eman->xdpy));
  365. }
  366. /* window attributes */
  367. attrs.background_pixel = 0;
  368. attrs.border_pixel = 0;
  369. attrs.colormap = XCreateColormap(eman->xdpy, root, vinfo->visual, AllocNone);
  370. attrs.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
  371. attrs.override_redirect = fullscreen;
  372. mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask | CWOverrideRedirect;
  373. eman->xwin = XCreateWindow(eman->xdpy, root, x, y, w, h,
  374. 0, vinfo->depth, InputOutput,
  375. vinfo->visual, mask, &attrs);
  376. XFree(vinfo);
  377. /* set hints and properties */
  378. {
  379. XSizeHints sizehints;
  380. sizehints.x = x;
  381. sizehints.y = y;
  382. sizehints.width = w;
  383. sizehints.height = h;
  384. sizehints.flags = USSize | USPosition;
  385. XSetNormalHints(eman->xdpy, eman->xwin, &sizehints);
  386. XSetStandardProperties(eman->xdpy, eman->xwin, name, name,
  387. None, (char **)NULL, 0, &sizehints);
  388. }
  389. if (need_surface) {
  390. eman->win = eglCreateWindowSurface(eman->dpy, eman->conf,
  391. eman->xwin, attrib_list);
  392. if (eman->win == EGL_NO_SURFACE) {
  393. printf("eglCreateWindowSurface() failed\n");
  394. XDestroyWindow(eman->xdpy, eman->xwin);
  395. eman->xwin = None;
  396. return EGL_FALSE;
  397. }
  398. }
  399. eman->gc = XCreateGC(eman->xdpy, eman->xwin, 0, NULL);
  400. XMapWindow(eman->xdpy, eman->xwin);
  401. return EGL_TRUE;
  402. }
  403. static EGLBoolean
  404. egl_manager_create_pixmap(struct egl_manager *eman, EGLNativeWindowType xwin,
  405. EGLBoolean need_surface, const EGLint *attrib_list)
  406. {
  407. XWindowAttributes attrs;
  408. if (!XGetWindowAttributes(eman->xdpy, xwin, &attrs)) {
  409. printf("XGetWindowAttributes() failed\n");
  410. return EGL_FALSE;
  411. }
  412. eman->xpix = XCreatePixmap(eman->xdpy, xwin,
  413. attrs.width, attrs.height, attrs.depth);
  414. if (need_surface) {
  415. eman->pix = eglCreatePixmapSurface(eman->dpy, eman->conf,
  416. eman->xpix, attrib_list);
  417. if (eman->pix == EGL_NO_SURFACE) {
  418. printf("eglCreatePixmapSurface() failed\n");
  419. XFreePixmap(eman->xdpy, eman->xpix);
  420. eman->xpix = None;
  421. return EGL_FALSE;
  422. }
  423. }
  424. return EGL_TRUE;
  425. }
  426. static EGLBoolean
  427. egl_manager_create_pbuffer(struct egl_manager *eman, const EGLint *attrib_list)
  428. {
  429. eman->pbuf = eglCreatePbufferSurface(eman->dpy, eman->conf, attrib_list);
  430. if (eman->pbuf == EGL_NO_SURFACE) {
  431. printf("eglCreatePbufferSurface() failed\n");
  432. return EGL_FALSE;
  433. }
  434. return EGL_TRUE;
  435. }
  436. static void
  437. egl_manager_destroy(struct egl_manager *eman)
  438. {
  439. eglMakeCurrent(eman->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
  440. eglTerminate(eman->dpy);
  441. if (eman->xwin != None)
  442. XDestroyWindow(eman->xdpy, eman->xwin);
  443. if (eman->xpix != None)
  444. XFreePixmap(eman->xdpy, eman->xpix);
  445. XFreeGC(eman->xdpy, eman->gc);
  446. free(eman);
  447. }
  448. enum {
  449. GEARS_WINDOW,
  450. GEARS_PIXMAP,
  451. GEARS_PIXMAP_TEXTURE,
  452. GEARS_PBUFFER,
  453. GEARS_PBUFFER_TEXTURE,
  454. GEARS_RENDERBUFFER
  455. };
  456. static void
  457. texture_gears(struct egl_manager *eman, int surface_type)
  458. {
  459. static const GLint verts[12] =
  460. { -5, -6, -10, 5, -6, -10, -5, 4, 10, 5, 4, 10 };
  461. static const GLint tex_coords[8] = { 0, 0, 1, 0, 0, 1, 1, 1 };
  462. eglMakeCurrent(eman->dpy, eman->win, eman->win, eman->ctx);
  463. glClearColor(0, 0, 0, 0);
  464. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  465. glEnable(GL_TEXTURE_2D);
  466. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  467. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
  468. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  469. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  470. glEnableClientState(GL_VERTEX_ARRAY);
  471. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  472. glVertexPointer(3, GL_INT, 0, verts);
  473. glTexCoordPointer(2, GL_INT, 0, tex_coords);
  474. if (surface_type == GEARS_PBUFFER_TEXTURE)
  475. eglBindTexImage(eman->dpy, eman->pbuf, EGL_BACK_BUFFER);
  476. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  477. glDisableClientState(GL_VERTEX_ARRAY);
  478. glDisableClientState(GL_COLOR_ARRAY);
  479. glDisable(GL_TEXTURE_2D);
  480. if (surface_type == GEARS_PBUFFER_TEXTURE)
  481. eglReleaseTexImage(eman->dpy, eman->pbuf, EGL_BACK_BUFFER);
  482. eglSwapBuffers(eman->dpy, eman->win);
  483. }
  484. static void
  485. copy_gears(struct egl_manager *eman,
  486. EGLint tile_w, EGLint tile_h, EGLint w, EGLint h)
  487. {
  488. int x, y;
  489. eglWaitClient();
  490. for (x = 0; x < w; x += tile_w) {
  491. for (y = 0; y < h; y += tile_h) {
  492. XCopyArea(eman->xdpy, eman->xpix, eman->xwin, eman->gc,
  493. 0, 0, tile_w, tile_h, x, y);
  494. }
  495. }
  496. }
  497. static void
  498. event_loop(struct egl_manager *eman, EGLint surface_type, EGLint w, EGLint h)
  499. {
  500. int window_w = w, window_h = h;
  501. if (surface_type == EGL_PBUFFER_BIT)
  502. printf("there will be no screen update if "
  503. "eglCopyBuffers() is not implemented\n");
  504. while (1) {
  505. while (XPending(eman->xdpy) > 0) {
  506. XEvent event;
  507. XNextEvent(eman->xdpy, &event);
  508. switch (event.type) {
  509. case Expose:
  510. /* we'll redraw below */
  511. break;
  512. case ConfigureNotify:
  513. window_w = event.xconfigure.width;
  514. window_h = event.xconfigure.height;
  515. if (surface_type == EGL_WINDOW_BIT)
  516. reshape(window_w, window_h);
  517. break;
  518. case KeyPress:
  519. {
  520. char buffer[10];
  521. int r, code;
  522. code = XLookupKeysym(&event.xkey, 0);
  523. if (code == XK_Left) {
  524. view_roty += 5.0;
  525. }
  526. else if (code == XK_Right) {
  527. view_roty -= 5.0;
  528. }
  529. else if (code == XK_Up) {
  530. view_rotx += 5.0;
  531. }
  532. else if (code == XK_Down) {
  533. view_rotx -= 5.0;
  534. }
  535. else {
  536. r = XLookupString(&event.xkey, buffer, sizeof(buffer),
  537. NULL, NULL);
  538. if (buffer[0] == 27) {
  539. /* escape */
  540. return;
  541. }
  542. }
  543. }
  544. }
  545. }
  546. {
  547. static int frames = 0;
  548. static double tRot0 = -1.0, tRate0 = -1.0;
  549. double dt, t = current_time();
  550. if (tRot0 < 0.0)
  551. tRot0 = t;
  552. dt = t - tRot0;
  553. tRot0 = t;
  554. /* advance rotation for next frame */
  555. angle += 70.0 * dt; /* 70 degrees per second */
  556. if (angle > 3600.0)
  557. angle -= 3600.0;
  558. switch (surface_type) {
  559. case GEARS_WINDOW:
  560. draw();
  561. eglSwapBuffers(eman->dpy, eman->win);
  562. break;
  563. case GEARS_PBUFFER:
  564. draw();
  565. if (!eglCopyBuffers(eman->dpy, eman->pbuf, eman->xpix))
  566. break;
  567. copy_gears(eman, w, h, window_w, window_h);
  568. break;
  569. case GEARS_PBUFFER_TEXTURE:
  570. eglMakeCurrent(eman->dpy, eman->pbuf, eman->pbuf, eman->ctx);
  571. draw();
  572. texture_gears(eman, surface_type);
  573. break;
  574. case GEARS_PIXMAP:
  575. draw();
  576. copy_gears(eman, w, h, window_w, window_h);
  577. break;
  578. case GEARS_PIXMAP_TEXTURE:
  579. eglMakeCurrent(eman->dpy, eman->pix, eman->pix, eman->ctx);
  580. draw();
  581. texture_gears(eman, surface_type);
  582. break;
  583. case GEARS_RENDERBUFFER:
  584. glBindFramebuffer(GL_FRAMEBUFFER_EXT, eman->fbo);
  585. draw();
  586. glBindFramebuffer(GL_FRAMEBUFFER_EXT, 0);
  587. texture_gears(eman, surface_type);
  588. break;
  589. }
  590. frames++;
  591. if (tRate0 < 0.0)
  592. tRate0 = t;
  593. if (t - tRate0 >= 5.0) {
  594. GLfloat seconds = t - tRate0;
  595. GLfloat fps = frames / seconds;
  596. printf("%d frames in %3.1f seconds = %6.3f FPS\n", frames, seconds,
  597. fps);
  598. tRate0 = t;
  599. frames = 0;
  600. }
  601. }
  602. }
  603. }
  604. static void
  605. usage(void)
  606. {
  607. printf("Usage:\n");
  608. printf(" -display <displayname> set the display to run on\n");
  609. printf(" -fullscreen run in fullscreen mode\n");
  610. printf(" -info display OpenGL renderer info\n");
  611. printf(" -pixmap use pixmap surface\n");
  612. printf(" -pixmap-texture use pixmap surface and texture using EGLImage\n");
  613. printf(" -pbuffer use pbuffer surface and eglCopyBuffers\n");
  614. printf(" -pbuffer-texture use pbuffer surface and eglBindTexImage\n");
  615. printf(" -renderbuffer renderbuffer as EGLImage and bind as texture from\n");
  616. }
  617. static const char *names[] = {
  618. "window",
  619. "pixmap",
  620. "pixmap_texture",
  621. "pbuffer",
  622. "pbuffer_texture",
  623. "renderbuffer"
  624. };
  625. int
  626. main(int argc, char *argv[])
  627. {
  628. const int winWidth = 300, winHeight = 300;
  629. Display *x_dpy;
  630. char *dpyName = NULL;
  631. struct egl_manager *eman;
  632. EGLint attribs[] = {
  633. EGL_SURFACE_TYPE, EGL_WINDOW_BIT, /* may be changed later */
  634. EGL_RED_SIZE, 1,
  635. EGL_GREEN_SIZE, 1,
  636. EGL_BLUE_SIZE, 1,
  637. EGL_DEPTH_SIZE, 1,
  638. EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
  639. EGL_NONE
  640. };
  641. char win_title[] = "xeglgears (window/pixmap/pbuffer)";
  642. EGLint surface_type = GEARS_WINDOW;
  643. GLboolean printInfo = GL_FALSE;
  644. GLboolean fullscreen = GL_FALSE;
  645. EGLBoolean ret;
  646. GLuint texture, color_rb, depth_rb;
  647. int i;
  648. for (i = 1; i < argc; i++) {
  649. if (strcmp(argv[i], "-display") == 0) {
  650. dpyName = argv[i+1];
  651. i++;
  652. }
  653. else if (strcmp(argv[i], "-info") == 0) {
  654. printInfo = GL_TRUE;
  655. }
  656. else if (strcmp(argv[i], "-fullscreen") == 0) {
  657. fullscreen = GL_TRUE;
  658. }
  659. else if (strcmp(argv[i], "-pixmap") == 0) {
  660. surface_type = GEARS_PIXMAP;
  661. attribs[1] = EGL_PIXMAP_BIT;
  662. }
  663. else if (strcmp(argv[i], "-pixmap-texture") == 0) {
  664. surface_type = GEARS_PIXMAP_TEXTURE;
  665. attribs[1] = EGL_PIXMAP_BIT;
  666. }
  667. else if (strcmp(argv[i], "-pbuffer") == 0) {
  668. surface_type = GEARS_PBUFFER;
  669. attribs[1] = EGL_PBUFFER_BIT;
  670. }
  671. else if (strcmp(argv[i], "-pbuffer-texture") == 0) {
  672. surface_type = GEARS_PBUFFER_TEXTURE;
  673. attribs[1] = EGL_PBUFFER_BIT;
  674. }
  675. else if (strcmp(argv[i], "-renderbuffer") == 0) {
  676. surface_type = GEARS_RENDERBUFFER;
  677. }
  678. else {
  679. usage();
  680. return -1;
  681. }
  682. }
  683. x_dpy = XOpenDisplay(dpyName);
  684. if (!x_dpy) {
  685. printf("Error: couldn't open display %s\n",
  686. dpyName ? dpyName : getenv("DISPLAY"));
  687. return -1;
  688. }
  689. eglBindAPI(EGL_OPENGL_API);
  690. eman = egl_manager_new(x_dpy, attribs, printInfo);
  691. if (!eman) {
  692. XCloseDisplay(x_dpy);
  693. return -1;
  694. }
  695. snprintf(win_title, sizeof(win_title),
  696. "xeglgears (%s)", names[surface_type]);
  697. ret = egl_manager_create_window(eman, win_title, winWidth, winHeight,
  698. EGL_TRUE, fullscreen, NULL);
  699. if (!ret)
  700. return -1;
  701. /* create surface(s) */
  702. switch (surface_type) {
  703. case GEARS_WINDOW:
  704. if (ret)
  705. ret = eglMakeCurrent(eman->dpy, eman->win, eman->win, eman->ctx);
  706. break;
  707. case GEARS_PIXMAP:
  708. case GEARS_PIXMAP_TEXTURE:
  709. ret = egl_manager_create_pixmap(eman, eman->xwin, EGL_TRUE, NULL);
  710. if (surface_type == GEARS_PIXMAP_TEXTURE)
  711. eman->image = eglCreateImageKHR (eman->dpy, eman->ctx,
  712. EGL_NATIVE_PIXMAP_KHR,
  713. (EGLClientBuffer) eman->xpix, NULL);
  714. if (ret)
  715. ret = eglMakeCurrent(eman->dpy, eman->pix, eman->pix, eman->ctx);
  716. break;
  717. case GEARS_PBUFFER:
  718. case GEARS_PBUFFER_TEXTURE:
  719. {
  720. EGLint pbuf_attribs[] = {
  721. EGL_WIDTH, winWidth,
  722. EGL_HEIGHT, winHeight,
  723. EGL_TEXTURE_FORMAT, EGL_TEXTURE_RGB,
  724. EGL_TEXTURE_TARGET, EGL_TEXTURE_2D,
  725. EGL_NONE
  726. };
  727. ret = (egl_manager_create_pixmap(eman, eman->xwin,
  728. EGL_TRUE, NULL) &&
  729. egl_manager_create_pbuffer(eman, pbuf_attribs));
  730. if (ret)
  731. ret = eglMakeCurrent(eman->dpy, eman->pbuf, eman->pbuf, eman->ctx);
  732. }
  733. break;
  734. case GEARS_RENDERBUFFER:
  735. ret = eglMakeCurrent(eman->dpy, eman->win, eman->win, eman->ctx);
  736. if (ret == EGL_FALSE)
  737. printf("failed to make context current\n");
  738. glGenFramebuffers(1, &eman->fbo);
  739. glBindFramebuffer(GL_FRAMEBUFFER_EXT, eman->fbo);
  740. glGenRenderbuffers(1, &color_rb);
  741. glBindRenderbuffer(GL_RENDERBUFFER_EXT, color_rb);
  742. glRenderbufferStorage(GL_RENDERBUFFER_EXT, GL_RGBA, winWidth, winHeight);
  743. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT,
  744. GL_COLOR_ATTACHMENT0_EXT,
  745. GL_RENDERBUFFER_EXT,
  746. color_rb);
  747. eman->image = eglCreateImageKHR(eman->dpy, eman->ctx,
  748. EGL_GL_RENDERBUFFER_KHR,
  749. (EGLClientBuffer) color_rb, NULL);
  750. glGenRenderbuffers(1, &depth_rb);
  751. glBindRenderbuffer(GL_RENDERBUFFER_EXT, depth_rb);
  752. glRenderbufferStorage(GL_RENDERBUFFER_EXT,
  753. GL_DEPTH_COMPONENT, winWidth, winHeight);
  754. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT,
  755. GL_DEPTH_ATTACHMENT_EXT,
  756. GL_RENDERBUFFER_EXT,
  757. depth_rb);
  758. if (glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT) != GL_FRAMEBUFFER_COMPLETE) {
  759. printf("framebuffer not complete\n");
  760. exit(1);
  761. }
  762. break;
  763. default:
  764. ret = EGL_FALSE;
  765. break;
  766. }
  767. switch (surface_type) {
  768. case GEARS_PIXMAP_TEXTURE:
  769. case GEARS_RENDERBUFFER:
  770. glGenTextures(1, &texture);
  771. glBindTexture(GL_TEXTURE_2D, texture);
  772. glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, eman->image);
  773. break;
  774. case GEARS_PBUFFER_TEXTURE:
  775. glGenTextures(1, &texture);
  776. glBindTexture(GL_TEXTURE_2D, texture);
  777. break;
  778. }
  779. if (!ret) {
  780. egl_manager_destroy(eman);
  781. XCloseDisplay(x_dpy);
  782. return -1;
  783. }
  784. if (printInfo) {
  785. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  786. printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
  787. printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
  788. }
  789. init();
  790. /* Set initial projection/viewing transformation.
  791. * We can't be sure we'll get a ConfigureNotify event when the window
  792. * first appears.
  793. */
  794. reshape(winWidth, winHeight);
  795. event_loop(eman, surface_type, winWidth, winHeight);
  796. glDeleteLists(gear1, 1);
  797. glDeleteLists(gear2, 1);
  798. glDeleteLists(gear3, 1);
  799. egl_manager_destroy(eman);
  800. XCloseDisplay(x_dpy);
  801. return 0;
  802. }