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