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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * Copyright (C) 2008 Tunsgten Graphics,Inc. All Rights Reserved.
  3. */
  4. /*
  5. * Test GL_OES_draw_texture
  6. * Brian Paul
  7. * August 2008
  8. */
  9. #define GL_GLEXT_PROTOTYPES
  10. #include <assert.h>
  11. #include <math.h>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <X11/Xlib.h>
  16. #include <X11/Xutil.h>
  17. #include <X11/keysym.h>
  18. #include <GLES/gl.h>
  19. #include <GLES/glext.h>
  20. #include <EGL/egl.h>
  21. static GLfloat view_posx = 10.0, view_posy = 20.0;
  22. static GLfloat width = 200, height = 200;
  23. static void
  24. draw(void)
  25. {
  26. glClear(GL_COLOR_BUFFER_BIT);
  27. glDrawTexfOES(view_posx, view_posy, 0.0, width, height);
  28. }
  29. /* new window size or exposure */
  30. static void
  31. reshape(int width, int height)
  32. {
  33. GLfloat ar = (GLfloat) width / (GLfloat) height;
  34. glViewport(0, 0, (GLint) width, (GLint) height);
  35. glMatrixMode(GL_PROJECTION);
  36. glLoadIdentity();
  37. #ifdef GL_VERSION_ES_CM_1_0
  38. glFrustumf(-ar, ar, -1, 1, 5.0, 60.0);
  39. #else
  40. glFrustum(-ar, ar, -1, 1, 5.0, 60.0);
  41. #endif
  42. glMatrixMode(GL_MODELVIEW);
  43. glLoadIdentity();
  44. glTranslatef(0.0, 0.0, -15.0);
  45. }
  46. static float
  47. dist(GLuint i, GLuint j, float x, float y)
  48. {
  49. return sqrt((i-x) * (i-x) + (j-y) * (j-y));
  50. }
  51. static void
  52. make_smile_texture(void)
  53. {
  54. #define SZ 128
  55. GLenum Filter = GL_LINEAR;
  56. GLubyte image[SZ][SZ][4];
  57. GLuint i, j;
  58. GLint cropRect[4];
  59. for (i = 0; i < SZ; i++) {
  60. for (j = 0; j < SZ; j++) {
  61. GLfloat d_mouth = dist(i, j, SZ/2, SZ/2);
  62. GLfloat d_rt_eye = dist(i, j, SZ*3/4, SZ*3/4);
  63. GLfloat d_lt_eye = dist(i, j, SZ*3/4, SZ*1/4);
  64. if (d_rt_eye < SZ / 8 || d_lt_eye < SZ / 8) {
  65. image[i][j][0] = 20;
  66. image[i][j][1] = 50;
  67. image[i][j][2] = 255;
  68. image[i][j][3] = 255;
  69. }
  70. else if (i < SZ/2 && d_mouth < SZ/3) {
  71. image[i][j][0] = 255;
  72. image[i][j][1] = 20;
  73. image[i][j][2] = 20;
  74. image[i][j][3] = 255;
  75. }
  76. else {
  77. image[i][j][0] = 200;
  78. image[i][j][1] = 200;
  79. image[i][j][2] = 200;
  80. image[i][j][3] = 255;
  81. }
  82. }
  83. }
  84. glActiveTexture(GL_TEXTURE0); /* unit 0 */
  85. glBindTexture(GL_TEXTURE_2D, 42);
  86. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, SZ, SZ, 0,
  87. GL_RGBA, GL_UNSIGNED_BYTE, image);
  88. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, Filter);
  89. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, Filter);
  90. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  91. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  92. cropRect[0] = 0;
  93. cropRect[1] = 0;
  94. cropRect[2] = SZ;
  95. cropRect[3] = SZ;
  96. glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect);
  97. #undef SZ
  98. }
  99. static void
  100. init(void)
  101. {
  102. const char *ext = (char *) glGetString(GL_EXTENSIONS);
  103. if (!strstr(ext, "GL_OES_draw_texture")) {
  104. fprintf(stderr, "Sorry, this program requires GL_OES_draw_texture");
  105. exit(1);
  106. }
  107. glClearColor(0.4, 0.4, 0.4, 0.0);
  108. make_smile_texture();
  109. glEnable(GL_TEXTURE_2D);
  110. }
  111. /*
  112. * Create an RGB, double-buffered X window.
  113. * Return the window and context handles.
  114. */
  115. static void
  116. make_x_window(Display *x_dpy, EGLDisplay egl_dpy,
  117. const char *name,
  118. int x, int y, int width, int height,
  119. Window *winRet,
  120. EGLContext *ctxRet,
  121. EGLSurface *surfRet)
  122. {
  123. static const EGLint attribs[] = {
  124. EGL_RED_SIZE, 1,
  125. EGL_GREEN_SIZE, 1,
  126. EGL_BLUE_SIZE, 1,
  127. EGL_NONE
  128. };
  129. int scrnum;
  130. XSetWindowAttributes attr;
  131. unsigned long mask;
  132. Window root;
  133. Window win;
  134. XVisualInfo *visInfo, visTemplate;
  135. int num_visuals;
  136. EGLContext ctx;
  137. EGLConfig config;
  138. EGLint num_configs;
  139. EGLint vid;
  140. scrnum = DefaultScreen( x_dpy );
  141. root = RootWindow( x_dpy, scrnum );
  142. if (!eglChooseConfig( egl_dpy, attribs, &config, 1, &num_configs)) {
  143. printf("Error: couldn't get an EGL visual config\n");
  144. exit(1);
  145. }
  146. assert(config);
  147. assert(num_configs > 0);
  148. if (!eglGetConfigAttrib(egl_dpy, config, EGL_NATIVE_VISUAL_ID, &vid)) {
  149. printf("Error: eglGetConfigAttrib() failed\n");
  150. exit(1);
  151. }
  152. /* The X window visual must match the EGL config */
  153. visTemplate.visualid = vid;
  154. visInfo = XGetVisualInfo(x_dpy, VisualIDMask, &visTemplate, &num_visuals);
  155. if (!visInfo) {
  156. printf("Error: couldn't get X visual\n");
  157. exit(1);
  158. }
  159. /* window attributes */
  160. attr.background_pixel = 0;
  161. attr.border_pixel = 0;
  162. attr.colormap = XCreateColormap( x_dpy, root, visInfo->visual, AllocNone);
  163. attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
  164. mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
  165. win = XCreateWindow( x_dpy, root, 0, 0, width, height,
  166. 0, visInfo->depth, InputOutput,
  167. visInfo->visual, mask, &attr );
  168. /* set hints and properties */
  169. {
  170. XSizeHints sizehints;
  171. sizehints.x = x;
  172. sizehints.y = y;
  173. sizehints.width = width;
  174. sizehints.height = height;
  175. sizehints.flags = USSize | USPosition;
  176. XSetNormalHints(x_dpy, win, &sizehints);
  177. XSetStandardProperties(x_dpy, win, name, name,
  178. None, (char **)NULL, 0, &sizehints);
  179. }
  180. eglBindAPI(EGL_OPENGL_ES_API);
  181. ctx = eglCreateContext(egl_dpy, config, EGL_NO_CONTEXT, NULL );
  182. if (!ctx) {
  183. printf("Error: eglCreateContext failed\n");
  184. exit(1);
  185. }
  186. *surfRet = eglCreateWindowSurface(egl_dpy, config, win, NULL);
  187. if (!*surfRet) {
  188. printf("Error: eglCreateWindowSurface failed\n");
  189. exit(1);
  190. }
  191. XFree(visInfo);
  192. *winRet = win;
  193. *ctxRet = ctx;
  194. }
  195. static void
  196. event_loop(Display *dpy, Window win,
  197. EGLDisplay egl_dpy, EGLSurface egl_surf)
  198. {
  199. int anim = 0;
  200. while (1) {
  201. int redraw = 0;
  202. if (!anim || XPending(dpy)) {
  203. XEvent event;
  204. XNextEvent(dpy, &event);
  205. switch (event.type) {
  206. case Expose:
  207. redraw = 1;
  208. break;
  209. case ConfigureNotify:
  210. reshape(event.xconfigure.width, event.xconfigure.height);
  211. break;
  212. case KeyPress:
  213. {
  214. char buffer[10];
  215. int r, code;
  216. code = XLookupKeysym(&event.xkey, 0);
  217. if (code == XK_Left) {
  218. view_posx -= 1.0;
  219. }
  220. else if (code == XK_Right) {
  221. view_posx += 1.0;
  222. }
  223. else if (code == XK_Up) {
  224. view_posy += 1.0;
  225. }
  226. else if (code == XK_Down) {
  227. view_posy -= 1.0;
  228. }
  229. else {
  230. r = XLookupString(&event.xkey, buffer, sizeof(buffer),
  231. NULL, NULL);
  232. if (buffer[0] == ' ') {
  233. anim = !anim;
  234. }
  235. else if (buffer[0] == 'w') {
  236. width -= 1.0f;
  237. }
  238. else if (buffer[0] == 'W') {
  239. width += 1.0f;
  240. }
  241. else if (buffer[0] == 'h') {
  242. height -= 1.0f;
  243. }
  244. else if (buffer[0] == 'H') {
  245. height += 1.0f;
  246. }
  247. else if (buffer[0] == 27) {
  248. /* escape */
  249. return;
  250. }
  251. }
  252. }
  253. redraw = 1;
  254. break;
  255. default:
  256. ; /*no-op*/
  257. }
  258. }
  259. if (anim) {
  260. view_posx += 1.0;
  261. view_posy += 2.0;
  262. redraw = 1;
  263. }
  264. if (redraw) {
  265. draw();
  266. eglSwapBuffers(egl_dpy, egl_surf);
  267. }
  268. }
  269. }
  270. static void
  271. usage(void)
  272. {
  273. printf("Usage:\n");
  274. printf(" -display <displayname> set the display to run on\n");
  275. printf(" -info display OpenGL renderer info\n");
  276. }
  277. int
  278. main(int argc, char *argv[])
  279. {
  280. const int winWidth = 400, winHeight = 300;
  281. Display *x_dpy;
  282. Window win;
  283. EGLSurface egl_surf;
  284. EGLContext egl_ctx;
  285. EGLDisplay egl_dpy;
  286. char *dpyName = NULL;
  287. GLboolean printInfo = GL_FALSE;
  288. EGLint egl_major, egl_minor;
  289. int i;
  290. const char *s;
  291. for (i = 1; i < argc; i++) {
  292. if (strcmp(argv[i], "-display") == 0) {
  293. dpyName = argv[i+1];
  294. i++;
  295. }
  296. else if (strcmp(argv[i], "-info") == 0) {
  297. printInfo = GL_TRUE;
  298. }
  299. else {
  300. usage();
  301. return -1;
  302. }
  303. }
  304. x_dpy = XOpenDisplay(dpyName);
  305. if (!x_dpy) {
  306. printf("Error: couldn't open display %s\n",
  307. dpyName ? dpyName : getenv("DISPLAY"));
  308. return -1;
  309. }
  310. egl_dpy = eglGetDisplay(x_dpy);
  311. if (!egl_dpy) {
  312. printf("Error: eglGetDisplay() failed\n");
  313. return -1;
  314. }
  315. if (!eglInitialize(egl_dpy, &egl_major, &egl_minor)) {
  316. printf("Error: eglInitialize() failed\n");
  317. return -1;
  318. }
  319. s = eglQueryString(egl_dpy, EGL_VERSION);
  320. printf("EGL_VERSION = %s\n", s);
  321. s = eglQueryString(egl_dpy, EGL_VENDOR);
  322. printf("EGL_VENDOR = %s\n", s);
  323. s = eglQueryString(egl_dpy, EGL_EXTENSIONS);
  324. printf("EGL_EXTENSIONS = %s\n", s);
  325. s = eglQueryString(egl_dpy, EGL_CLIENT_APIS);
  326. printf("EGL_CLIENT_APIS = %s\n", s);
  327. make_x_window(x_dpy, egl_dpy,
  328. "drawtex", 0, 0, winWidth, winHeight,
  329. &win, &egl_ctx, &egl_surf);
  330. XMapWindow(x_dpy, win);
  331. if (!eglMakeCurrent(egl_dpy, egl_surf, egl_surf, egl_ctx)) {
  332. printf("Error: eglMakeCurrent() failed\n");
  333. return -1;
  334. }
  335. if (printInfo) {
  336. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  337. printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
  338. printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
  339. printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
  340. }
  341. init();
  342. /* Set initial projection/viewing transformation.
  343. * We can't be sure we'll get a ConfigureNotify event when the window
  344. * first appears.
  345. */
  346. reshape(winWidth, winHeight);
  347. event_loop(x_dpy, win, egl_dpy, egl_surf);
  348. eglDestroyContext(egl_dpy, egl_ctx);
  349. eglDestroySurface(egl_dpy, egl_surf);
  350. eglTerminate(egl_dpy);
  351. XDestroyWindow(x_dpy, win);
  352. XCloseDisplay(x_dpy);
  353. return 0;
  354. }