Merge remote branch 'origin/7.8'
[mesa.git] / progs / egl / opengles2 / tri.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 **************************************************************************/
7
8 /*
9 * Draw a triangle with X/EGL and OpenGL ES 2.x
10 */
11
12 #define USE_FULL_GL 0
13
14
15
16 #include <assert.h>
17 #include <math.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <X11/Xlib.h>
22 #include <X11/Xutil.h>
23 #include <X11/keysym.h>
24 #if USE_FULL_GL
25 #include <GL/gl.h> /* use full OpenGL */
26 #else
27 #include <GLES2/gl2.h> /* use OpenGL ES 2.x */
28 #endif
29 #include <EGL/egl.h>
30
31
32 #define FLOAT_TO_FIXED(X) ((X) * 65535.0)
33
34
35
36 static GLfloat view_rotx = 0.0, view_roty = 0.0;
37
38 static GLint u_matrix = -1;
39 static GLint attr_pos = 0, attr_color = 1;
40
41
42 static void
43 make_z_rot_matrix(GLfloat angle, GLfloat *m)
44 {
45 float c = cos(angle * M_PI / 180.0);
46 float s = sin(angle * M_PI / 180.0);
47 int i;
48 for (i = 0; i < 16; i++)
49 m[i] = 0.0;
50 m[0] = m[5] = m[10] = m[15] = 1.0;
51
52 m[0] = c;
53 m[1] = s;
54 m[4] = -s;
55 m[5] = c;
56 }
57
58 static void
59 make_scale_matrix(GLfloat xs, GLfloat ys, GLfloat zs, GLfloat *m)
60 {
61 int i;
62 for (i = 0; i < 16; i++)
63 m[i] = 0.0;
64 m[0] = xs;
65 m[5] = ys;
66 m[10] = zs;
67 m[15] = 1.0;
68 }
69
70
71 static void
72 mul_matrix(GLfloat *prod, const GLfloat *a, const GLfloat *b)
73 {
74 #define A(row,col) a[(col<<2)+row]
75 #define B(row,col) b[(col<<2)+row]
76 #define P(row,col) p[(col<<2)+row]
77 GLfloat p[16];
78 GLint i;
79 for (i = 0; i < 4; i++) {
80 const GLfloat ai0=A(i,0), ai1=A(i,1), ai2=A(i,2), ai3=A(i,3);
81 P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0) + ai3 * B(3,0);
82 P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1) + ai3 * B(3,1);
83 P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2) + ai3 * B(3,2);
84 P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3 * B(3,3);
85 }
86 memcpy(prod, p, sizeof(p));
87 #undef A
88 #undef B
89 #undef PROD
90 }
91
92
93 static void
94 draw(void)
95 {
96 static const GLfloat verts[3][2] = {
97 { -1, -1 },
98 { 1, -1 },
99 { 0, 1 }
100 };
101 static const GLfloat colors[3][3] = {
102 { 1, 0, 0 },
103 { 0, 1, 0 },
104 { 0, 0, 1 }
105 };
106 GLfloat mat[16], rot[16], scale[16];
107
108 /* Set modelview/projection matrix */
109 make_z_rot_matrix(view_rotx, rot);
110 make_scale_matrix(0.5, 0.5, 0.5, scale);
111 mul_matrix(mat, rot, scale);
112 glUniformMatrix4fv(u_matrix, 1, GL_FALSE, mat);
113
114 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
115
116 {
117 glVertexAttribPointer(attr_pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
118 glVertexAttribPointer(attr_color, 3, GL_FLOAT, GL_FALSE, 0, colors);
119 glEnableVertexAttribArray(attr_pos);
120 glEnableVertexAttribArray(attr_color);
121
122 glDrawArrays(GL_TRIANGLES, 0, 3);
123
124 glDisableVertexAttribArray(attr_pos);
125 glDisableVertexAttribArray(attr_color);
126 }
127 }
128
129
130 /* new window size or exposure */
131 static void
132 reshape(int width, int height)
133 {
134 glViewport(0, 0, (GLint) width, (GLint) height);
135 }
136
137
138 static void
139 create_shaders(void)
140 {
141 static const char *fragShaderText =
142 "varying vec4 v_color;\n"
143 "void main() {\n"
144 " gl_FragColor = v_color;\n"
145 "}\n";
146 static const char *vertShaderText =
147 "uniform mat4 modelviewProjection;\n"
148 "attribute vec4 pos;\n"
149 "attribute vec4 color;\n"
150 "varying vec4 v_color;\n"
151 "void main() {\n"
152 " gl_Position = modelviewProjection * pos;\n"
153 " v_color = color;\n"
154 "}\n";
155
156 GLuint fragShader, vertShader, program;
157 GLint stat;
158
159 fragShader = glCreateShader(GL_FRAGMENT_SHADER);
160 glShaderSource(fragShader, 1, (const char **) &fragShaderText, NULL);
161 glCompileShader(fragShader);
162 glGetShaderiv(fragShader, GL_COMPILE_STATUS, &stat);
163 if (!stat) {
164 printf("Error: fragment shader did not compile!\n");
165 exit(1);
166 }
167
168 vertShader = glCreateShader(GL_VERTEX_SHADER);
169 glShaderSource(vertShader, 1, (const char **) &vertShaderText, NULL);
170 glCompileShader(vertShader);
171 glGetShaderiv(vertShader, GL_COMPILE_STATUS, &stat);
172 if (!stat) {
173 printf("Error: vertex shader did not compile!\n");
174 exit(1);
175 }
176
177 program = glCreateProgram();
178 glAttachShader(program, fragShader);
179 glAttachShader(program, vertShader);
180 glLinkProgram(program);
181
182 glGetProgramiv(program, GL_LINK_STATUS, &stat);
183 if (!stat) {
184 char log[1000];
185 GLsizei len;
186 glGetProgramInfoLog(program, 1000, &len, log);
187 printf("Error: linking:\n%s\n", log);
188 exit(1);
189 }
190
191 glUseProgram(program);
192
193 if (1) {
194 /* test setting attrib locations */
195 glBindAttribLocation(program, attr_pos, "pos");
196 glBindAttribLocation(program, attr_color, "color");
197 glLinkProgram(program); /* needed to put attribs into effect */
198 }
199 else {
200 /* test automatic attrib locations */
201 attr_pos = glGetAttribLocation(program, "pos");
202 attr_color = glGetAttribLocation(program, "color");
203 }
204
205 u_matrix = glGetUniformLocation(program, "modelviewProjection");
206 printf("Uniform modelviewProjection at %d\n", u_matrix);
207 printf("Attrib pos at %d\n", attr_pos);
208 printf("Attrib color at %d\n", attr_color);
209 }
210
211
212 static void
213 init(void)
214 {
215 typedef void (*proc)();
216
217 #if 1 /* test code */
218 proc p = eglGetProcAddress("glMapBufferOES");
219 assert(p);
220 #endif
221
222 glClearColor(0.4, 0.4, 0.4, 0.0);
223
224 create_shaders();
225 }
226
227
228 /*
229 * Create an RGB, double-buffered X window.
230 * Return the window and context handles.
231 */
232 static void
233 make_x_window(Display *x_dpy, EGLDisplay egl_dpy,
234 const char *name,
235 int x, int y, int width, int height,
236 Window *winRet,
237 EGLContext *ctxRet,
238 EGLSurface *surfRet)
239 {
240 static const EGLint attribs[] = {
241 EGL_RED_SIZE, 1,
242 EGL_GREEN_SIZE, 1,
243 EGL_BLUE_SIZE, 1,
244 EGL_DEPTH_SIZE, 1,
245 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
246 EGL_NONE
247 };
248 static const EGLint ctx_attribs[] = {
249 EGL_CONTEXT_CLIENT_VERSION, 2,
250 EGL_NONE
251 };
252 int scrnum;
253 XSetWindowAttributes attr;
254 unsigned long mask;
255 Window root;
256 Window win;
257 XVisualInfo *visInfo, visTemplate;
258 int num_visuals;
259 EGLContext ctx;
260 EGLConfig config;
261 EGLint num_configs;
262 EGLint vid;
263
264 scrnum = DefaultScreen( x_dpy );
265 root = RootWindow( x_dpy, scrnum );
266
267 if (!eglChooseConfig( egl_dpy, attribs, &config, 1, &num_configs)) {
268 printf("Error: couldn't get an EGL visual config\n");
269 exit(1);
270 }
271
272 assert(config);
273 assert(num_configs > 0);
274
275 if (!eglGetConfigAttrib(egl_dpy, config, EGL_NATIVE_VISUAL_ID, &vid)) {
276 printf("Error: eglGetConfigAttrib() failed\n");
277 exit(1);
278 }
279
280 /* The X window visual must match the EGL config */
281 visTemplate.visualid = vid;
282 visInfo = XGetVisualInfo(x_dpy, VisualIDMask, &visTemplate, &num_visuals);
283 if (!visInfo) {
284 printf("Error: couldn't get X visual\n");
285 exit(1);
286 }
287
288 /* window attributes */
289 attr.background_pixel = 0;
290 attr.border_pixel = 0;
291 attr.colormap = XCreateColormap( x_dpy, root, visInfo->visual, AllocNone);
292 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
293 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
294
295 win = XCreateWindow( x_dpy, root, 0, 0, width, height,
296 0, visInfo->depth, InputOutput,
297 visInfo->visual, mask, &attr );
298
299 /* set hints and properties */
300 {
301 XSizeHints sizehints;
302 sizehints.x = x;
303 sizehints.y = y;
304 sizehints.width = width;
305 sizehints.height = height;
306 sizehints.flags = USSize | USPosition;
307 XSetNormalHints(x_dpy, win, &sizehints);
308 XSetStandardProperties(x_dpy, win, name, name,
309 None, (char **)NULL, 0, &sizehints);
310 }
311
312 #if USE_FULL_GL /* XXX fix this when eglBindAPI() works */
313 eglBindAPI(EGL_OPENGL_API);
314 #else
315 eglBindAPI(EGL_OPENGL_ES_API);
316 #endif
317
318 ctx = eglCreateContext(egl_dpy, config, EGL_NO_CONTEXT, ctx_attribs );
319 if (!ctx) {
320 printf("Error: eglCreateContext failed\n");
321 exit(1);
322 }
323
324 /* test eglQueryContext() */
325 {
326 EGLint val;
327 eglQueryContext(egl_dpy, ctx, EGL_CONTEXT_CLIENT_VERSION, &val);
328 assert(val == 2);
329 }
330
331 *surfRet = eglCreateWindowSurface(egl_dpy, config, win, NULL);
332 if (!*surfRet) {
333 printf("Error: eglCreateWindowSurface failed\n");
334 exit(1);
335 }
336
337 /* sanity checks */
338 {
339 EGLint val;
340 eglQuerySurface(egl_dpy, *surfRet, EGL_WIDTH, &val);
341 assert(val == width);
342 eglQuerySurface(egl_dpy, *surfRet, EGL_HEIGHT, &val);
343 assert(val == height);
344 assert(eglGetConfigAttrib(egl_dpy, config, EGL_SURFACE_TYPE, &val));
345 assert(val & EGL_WINDOW_BIT);
346 }
347
348 XFree(visInfo);
349
350 *winRet = win;
351 *ctxRet = ctx;
352 }
353
354
355 static void
356 event_loop(Display *dpy, Window win,
357 EGLDisplay egl_dpy, EGLSurface egl_surf)
358 {
359 while (1) {
360 int redraw = 0;
361 XEvent event;
362
363 XNextEvent(dpy, &event);
364
365 switch (event.type) {
366 case Expose:
367 redraw = 1;
368 break;
369 case ConfigureNotify:
370 reshape(event.xconfigure.width, event.xconfigure.height);
371 break;
372 case KeyPress:
373 {
374 char buffer[10];
375 int r, code;
376 code = XLookupKeysym(&event.xkey, 0);
377 if (code == XK_Left) {
378 view_roty += 5.0;
379 }
380 else if (code == XK_Right) {
381 view_roty -= 5.0;
382 }
383 else if (code == XK_Up) {
384 view_rotx += 5.0;
385 }
386 else if (code == XK_Down) {
387 view_rotx -= 5.0;
388 }
389 else {
390 r = XLookupString(&event.xkey, buffer, sizeof(buffer),
391 NULL, NULL);
392 if (buffer[0] == 27) {
393 /* escape */
394 return;
395 }
396 }
397 }
398 redraw = 1;
399 break;
400 default:
401 ; /*no-op*/
402 }
403
404 if (redraw) {
405 draw();
406 eglSwapBuffers(egl_dpy, egl_surf);
407 }
408 }
409 }
410
411
412 static void
413 usage(void)
414 {
415 printf("Usage:\n");
416 printf(" -display <displayname> set the display to run on\n");
417 printf(" -info display OpenGL renderer info\n");
418 }
419
420
421 int
422 main(int argc, char *argv[])
423 {
424 const int winWidth = 300, winHeight = 300;
425 Display *x_dpy;
426 Window win;
427 EGLSurface egl_surf;
428 EGLContext egl_ctx;
429 EGLDisplay egl_dpy;
430 char *dpyName = NULL;
431 GLboolean printInfo = GL_FALSE;
432 EGLint egl_major, egl_minor;
433 int i;
434 const char *s;
435
436 for (i = 1; i < argc; i++) {
437 if (strcmp(argv[i], "-display") == 0) {
438 dpyName = argv[i+1];
439 i++;
440 }
441 else if (strcmp(argv[i], "-info") == 0) {
442 printInfo = GL_TRUE;
443 }
444 else {
445 usage();
446 return -1;
447 }
448 }
449
450 x_dpy = XOpenDisplay(dpyName);
451 if (!x_dpy) {
452 printf("Error: couldn't open display %s\n",
453 dpyName ? dpyName : getenv("DISPLAY"));
454 return -1;
455 }
456
457 egl_dpy = eglGetDisplay(x_dpy);
458 if (!egl_dpy) {
459 printf("Error: eglGetDisplay() failed\n");
460 return -1;
461 }
462
463 if (!eglInitialize(egl_dpy, &egl_major, &egl_minor)) {
464 printf("Error: eglInitialize() failed\n");
465 return -1;
466 }
467
468 s = eglQueryString(egl_dpy, EGL_VERSION);
469 printf("EGL_VERSION = %s\n", s);
470
471 s = eglQueryString(egl_dpy, EGL_VENDOR);
472 printf("EGL_VENDOR = %s\n", s);
473
474 s = eglQueryString(egl_dpy, EGL_EXTENSIONS);
475 printf("EGL_EXTENSIONS = %s\n", s);
476
477 s = eglQueryString(egl_dpy, EGL_CLIENT_APIS);
478 printf("EGL_CLIENT_APIS = %s\n", s);
479
480 make_x_window(x_dpy, egl_dpy,
481 "OpenGL ES 2.x tri", 0, 0, winWidth, winHeight,
482 &win, &egl_ctx, &egl_surf);
483
484 XMapWindow(x_dpy, win);
485 if (!eglMakeCurrent(egl_dpy, egl_surf, egl_surf, egl_ctx)) {
486 printf("Error: eglMakeCurrent() failed\n");
487 return -1;
488 }
489
490 if (printInfo) {
491 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
492 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
493 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
494 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
495 }
496
497 init();
498
499 /* Set initial projection/viewing transformation.
500 * We can't be sure we'll get a ConfigureNotify event when the window
501 * first appears.
502 */
503 reshape(winWidth, winHeight);
504
505 event_loop(x_dpy, win, egl_dpy, egl_surf);
506
507 eglDestroyContext(egl_dpy, egl_ctx);
508 eglDestroySurface(egl_dpy, egl_surf);
509 eglTerminate(egl_dpy);
510
511
512 XDestroyWindow(x_dpy, win);
513 XCloseDisplay(x_dpy);
514
515 return 0;
516 }