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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * Copyright (C) 2008 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. * Draw a triangle with X/EGL.
  23. * Brian Paul
  24. * 3 June 2008
  25. */
  26. #include <assert.h>
  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> /* using full OpenGL for now */
  35. #include <GLES/egl.h>
  36. static GLfloat view_rotx = 0.0, view_roty = 0.0, view_rotz = 0.0;
  37. static void
  38. draw(void)
  39. {
  40. static const GLfloat verts[3][2] = {
  41. { -1, -1 },
  42. { 1, -1 },
  43. { 0, 1 }
  44. };
  45. static const GLfloat colors[3][3] = {
  46. { 1, 0, 0 },
  47. { 0, 1, 0 },
  48. { 0, 0, 1 }
  49. };
  50. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  51. glPushMatrix();
  52. glRotatef(view_rotx, 1, 0, 0);
  53. glRotatef(view_roty, 0, 1, 0);
  54. glRotatef(view_rotz, 0, 0, 1);
  55. {
  56. glVertexPointer(2, GL_FLOAT, 0, verts);
  57. glColorPointer(3, GL_FLOAT, 0, colors);
  58. glEnableClientState(GL_VERTEX_ARRAY);
  59. glEnableClientState(GL_COLOR_ARRAY);
  60. glDrawArrays(GL_TRIANGLES, 0, 3);
  61. glDisableClientState(GL_VERTEX_ARRAY);
  62. glDisableClientState(GL_COLOR_ARRAY);
  63. }
  64. glPopMatrix();
  65. }
  66. /* new window size or exposure */
  67. static void
  68. reshape(int width, int height)
  69. {
  70. GLfloat ar = (GLfloat) width / (GLfloat) height;
  71. glViewport(0, 0, (GLint) width, (GLint) height);
  72. glMatrixMode(GL_PROJECTION);
  73. glLoadIdentity();
  74. glFrustum(-ar, ar, -1, 1, 5.0, 60.0);
  75. glMatrixMode(GL_MODELVIEW);
  76. glLoadIdentity();
  77. glTranslatef(0.0, 0.0, -10.0);
  78. }
  79. static void
  80. init(void)
  81. {
  82. glClearColor(0.4, 0.4, 0.4, 0.0);
  83. }
  84. /*
  85. * Create an RGB, double-buffered X window.
  86. * Return the window and context handles.
  87. */
  88. static void
  89. make_x_window(Display *x_dpy, EGLDisplay egl_dpy,
  90. const char *name,
  91. int x, int y, int width, int height,
  92. Window *winRet,
  93. EGLContext *ctxRet,
  94. EGLSurface *surfRet)
  95. {
  96. static const EGLint attribs[] = {
  97. EGL_RED_SIZE, 1,
  98. EGL_GREEN_SIZE, 1,
  99. EGL_BLUE_SIZE, 1,
  100. EGL_DEPTH_SIZE, 1,
  101. EGL_NONE
  102. };
  103. int scrnum;
  104. XSetWindowAttributes attr;
  105. unsigned long mask;
  106. Window root;
  107. Window win;
  108. XVisualInfo *visInfo, visTemplate;
  109. int num_visuals;
  110. EGLContext ctx;
  111. EGLConfig config;
  112. EGLint num_configs;
  113. EGLint vid;
  114. scrnum = DefaultScreen( x_dpy );
  115. root = RootWindow( x_dpy, scrnum );
  116. if (!eglChooseConfig( egl_dpy, attribs, &config, 1, &num_configs)) {
  117. printf("Error: couldn't get an EGL visual config\n");
  118. exit(1);
  119. }
  120. assert(config);
  121. assert(num_configs > 0);
  122. if (!eglGetConfigAttrib(egl_dpy, config, EGL_NATIVE_VISUAL_ID, &vid)) {
  123. printf("Error: eglGetConfigAttrib() failed\n");
  124. exit(1);
  125. }
  126. /* The X window visual must match the EGL config */
  127. visTemplate.visualid = vid;
  128. visInfo = XGetVisualInfo(x_dpy, VisualIDMask, &visTemplate, &num_visuals);
  129. if (!visInfo) {
  130. printf("Error: couldn't get X visual\n");
  131. exit(1);
  132. }
  133. /* window attributes */
  134. attr.background_pixel = 0;
  135. attr.border_pixel = 0;
  136. attr.colormap = XCreateColormap( x_dpy, root, visInfo->visual, AllocNone);
  137. attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
  138. mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
  139. win = XCreateWindow( x_dpy, root, 0, 0, width, height,
  140. 0, visInfo->depth, InputOutput,
  141. visInfo->visual, mask, &attr );
  142. /* set hints and properties */
  143. {
  144. XSizeHints sizehints;
  145. sizehints.x = x;
  146. sizehints.y = y;
  147. sizehints.width = width;
  148. sizehints.height = height;
  149. sizehints.flags = USSize | USPosition;
  150. XSetNormalHints(x_dpy, win, &sizehints);
  151. XSetStandardProperties(x_dpy, win, name, name,
  152. None, (char **)NULL, 0, &sizehints);
  153. }
  154. eglBindAPI(EGL_OPENGL_API);
  155. ctx = eglCreateContext(egl_dpy, config, EGL_NO_CONTEXT, NULL );
  156. if (!ctx) {
  157. printf("Error: glXCreateContext failed\n");
  158. exit(1);
  159. }
  160. *surfRet = eglCreateWindowSurface(egl_dpy, config, win, NULL);
  161. if (!*surfRet) {
  162. printf("Error: eglCreateWindowSurface failed\n");
  163. exit(1);
  164. }
  165. XFree(visInfo);
  166. *winRet = win;
  167. *ctxRet = ctx;
  168. }
  169. static void
  170. event_loop(Display *dpy, Window win,
  171. EGLDisplay egl_dpy, EGLSurface egl_surf)
  172. {
  173. while (1) {
  174. int redraw = 0;
  175. XEvent event;
  176. XNextEvent(dpy, &event);
  177. switch (event.type) {
  178. case Expose:
  179. redraw = 1;
  180. break;
  181. case ConfigureNotify:
  182. reshape(event.xconfigure.width, event.xconfigure.height);
  183. break;
  184. case KeyPress:
  185. {
  186. char buffer[10];
  187. int r, code;
  188. code = XLookupKeysym(&event.xkey, 0);
  189. if (code == XK_Left) {
  190. view_roty += 5.0;
  191. }
  192. else if (code == XK_Right) {
  193. view_roty -= 5.0;
  194. }
  195. else if (code == XK_Up) {
  196. view_rotx += 5.0;
  197. }
  198. else if (code == XK_Down) {
  199. view_rotx -= 5.0;
  200. }
  201. else {
  202. r = XLookupString(&event.xkey, buffer, sizeof(buffer),
  203. NULL, NULL);
  204. if (buffer[0] == 27) {
  205. /* escape */
  206. return;
  207. }
  208. }
  209. }
  210. redraw = 1;
  211. break;
  212. default:
  213. ; /*no-op*/
  214. }
  215. if (redraw) {
  216. draw();
  217. eglSwapBuffers(egl_dpy, egl_surf);
  218. }
  219. }
  220. }
  221. static void
  222. usage(void)
  223. {
  224. printf("Usage:\n");
  225. printf(" -display <displayname> set the display to run on\n");
  226. printf(" -info display OpenGL renderer info\n");
  227. }
  228. int
  229. main(int argc, char *argv[])
  230. {
  231. const int winWidth = 300, winHeight = 300;
  232. Display *x_dpy;
  233. Window win;
  234. EGLSurface egl_surf;
  235. EGLContext egl_ctx;
  236. EGLDisplay egl_dpy;
  237. char *dpyName = NULL;
  238. GLboolean printInfo = GL_FALSE;
  239. EGLint egl_major, egl_minor;
  240. int i;
  241. const char *s;
  242. for (i = 1; i < argc; i++) {
  243. if (strcmp(argv[i], "-display") == 0) {
  244. dpyName = argv[i+1];
  245. i++;
  246. }
  247. else if (strcmp(argv[i], "-info") == 0) {
  248. printInfo = GL_TRUE;
  249. }
  250. else {
  251. usage();
  252. return -1;
  253. }
  254. }
  255. x_dpy = XOpenDisplay(dpyName);
  256. if (!x_dpy) {
  257. printf("Error: couldn't open display %s\n",
  258. dpyName ? dpyName : getenv("DISPLAY"));
  259. return -1;
  260. }
  261. egl_dpy = eglGetDisplay(x_dpy);
  262. if (!egl_dpy) {
  263. printf("Error: eglGetDisplay() failed\n");
  264. return -1;
  265. }
  266. if (!eglInitialize(egl_dpy, &egl_major, &egl_minor)) {
  267. printf("Error: eglInitialize() failed\n");
  268. return -1;
  269. }
  270. s = eglQueryString(egl_dpy, EGL_VERSION);
  271. printf("EGL_VERSION = %s\n", s);
  272. make_x_window(x_dpy, egl_dpy,
  273. "xegl_tri", 0, 0, winWidth, winHeight,
  274. &win, &egl_ctx, &egl_surf);
  275. XMapWindow(x_dpy, win);
  276. if (!eglMakeCurrent(egl_dpy, egl_surf, egl_surf, egl_ctx)) {
  277. printf("Error: eglMakeCurrent() failed\n");
  278. return -1;
  279. }
  280. if (printInfo) {
  281. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  282. printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
  283. printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
  284. }
  285. init();
  286. /* Set initial projection/viewing transformation.
  287. * We can't be sure we'll get a ConfigureNotify event when the window
  288. * first appears.
  289. */
  290. reshape(winWidth, winHeight);
  291. event_loop(x_dpy, win, egl_dpy, egl_surf);
  292. eglDestroyContext(egl_dpy, egl_ctx);
  293. eglDestroySurface(egl_dpy, egl_surf);
  294. eglTerminate(egl_dpy);
  295. XDestroyWindow(x_dpy, win);
  296. XCloseDisplay(x_dpy);
  297. return 0;
  298. }