1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
6 **************************************************************************/
9 * Draw a triangle with X/EGL and OpenGL ES 2.x
22 #include <X11/Xutil.h>
23 #include <X11/keysym.h>
25 #include <GL/gl.h> /* use full OpenGL */
27 #include <GLES2/gl2.h> /* use OpenGL ES 2.x */
32 #define FLOAT_TO_FIXED(X) ((X) * 65535.0)
36 static GLfloat view_rotx = 0.0, view_roty = 0.0;
38 static GLint u_matrix = -1;
39 static GLint attr_pos = 0, attr_color = 1;
43 make_z_rot_matrix(GLfloat angle, GLfloat *m)
45 float c = cos(angle * M_PI / 180.0);
46 float s = sin(angle * M_PI / 180.0);
48 for (i = 0; i < 16; i++)
50 m[0] = m[5] = m[10] = m[15] = 1.0;
59 make_scale_matrix(GLfloat xs, GLfloat ys, GLfloat zs, GLfloat *m)
62 for (i = 0; i < 16; i++)
72 mul_matrix(GLfloat *prod, const GLfloat *a, const GLfloat *b)
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]
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);
86 memcpy(prod, p, sizeof(p));
96 static const GLfloat verts[3][2] = {
101 static const GLfloat colors[3][3] = {
106 GLfloat mat[16], rot[16], scale[16];
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);
114 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
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);
122 glDrawArrays(GL_TRIANGLES, 0, 3);
124 glDisableVertexAttribArray(attr_pos);
125 glDisableVertexAttribArray(attr_color);
130 /* new window size or exposure */
132 reshape(int width, int height)
134 glViewport(0, 0, (GLint) width, (GLint) height);
141 static const char *fragShaderText =
142 "varying vec4 v_color;\n"
144 " gl_FragColor = v_color;\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"
152 " gl_Position = modelviewProjection * pos;\n"
153 " v_color = color;\n"
156 GLuint fragShader, vertShader, program;
159 fragShader = glCreateShader(GL_FRAGMENT_SHADER);
160 glShaderSource(fragShader, 1, (const char **) &fragShaderText, NULL);
161 glCompileShader(fragShader);
162 glGetShaderiv(fragShader, GL_COMPILE_STATUS, &stat);
164 printf("Error: fragment shader did not compile!\n");
168 vertShader = glCreateShader(GL_VERTEX_SHADER);
169 glShaderSource(vertShader, 1, (const char **) &vertShaderText, NULL);
170 glCompileShader(vertShader);
171 glGetShaderiv(vertShader, GL_COMPILE_STATUS, &stat);
173 printf("Error: vertex shader did not compile!\n");
177 program = glCreateProgram();
178 glAttachShader(program, fragShader);
179 glAttachShader(program, vertShader);
180 glLinkProgram(program);
182 glGetProgramiv(program, GL_LINK_STATUS, &stat);
186 glGetProgramInfoLog(program, 1000, &len, log);
187 printf("Error: linking:\n%s\n", log);
191 glUseProgram(program);
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 */
200 /* test automatic attrib locations */
201 attr_pos = glGetAttribLocation(program, "pos");
202 attr_color = glGetAttribLocation(program, "color");
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);
215 typedef void (*proc)();
217 #if 1 /* test code */
218 proc p = eglGetProcAddress("glMapBufferOES");
222 glClearColor(0.4, 0.4, 0.4, 0.0);
229 * Create an RGB, double-buffered X window.
230 * Return the window and context handles.
233 make_x_window(Display *x_dpy, EGLDisplay egl_dpy,
235 int x, int y, int width, int height,
240 static const EGLint attribs[] = {
245 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
249 static const EGLint ctx_attribs[] = {
253 static const EGLint ctx_attribs[] = {
254 EGL_CONTEXT_CLIENT_VERSION, 2,
260 XSetWindowAttributes attr;
264 XVisualInfo *visInfo, visTemplate;
271 scrnum = DefaultScreen( x_dpy );
272 root = RootWindow( x_dpy, scrnum );
274 if (!eglChooseConfig( egl_dpy, attribs, &config, 1, &num_configs)) {
275 printf("Error: couldn't get an EGL visual config\n");
280 assert(num_configs > 0);
282 if (!eglGetConfigAttrib(egl_dpy, config, EGL_NATIVE_VISUAL_ID, &vid)) {
283 printf("Error: eglGetConfigAttrib() failed\n");
287 /* The X window visual must match the EGL config */
288 visTemplate.visualid = vid;
289 visInfo = XGetVisualInfo(x_dpy, VisualIDMask, &visTemplate, &num_visuals);
291 printf("Error: couldn't get X visual\n");
295 /* window attributes */
296 attr.background_pixel = 0;
297 attr.border_pixel = 0;
298 attr.colormap = XCreateColormap( x_dpy, root, visInfo->visual, AllocNone);
299 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
300 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
302 win = XCreateWindow( x_dpy, root, 0, 0, width, height,
303 0, visInfo->depth, InputOutput,
304 visInfo->visual, mask, &attr );
306 /* set hints and properties */
308 XSizeHints sizehints;
311 sizehints.width = width;
312 sizehints.height = height;
313 sizehints.flags = USSize | USPosition;
314 XSetNormalHints(x_dpy, win, &sizehints);
315 XSetStandardProperties(x_dpy, win, name, name,
316 None, (char **)NULL, 0, &sizehints);
319 #if USE_FULL_GL /* XXX fix this when eglBindAPI() works */
320 eglBindAPI(EGL_OPENGL_API);
322 eglBindAPI(EGL_OPENGL_ES_API);
325 ctx = eglCreateContext(egl_dpy, config, EGL_NO_CONTEXT, ctx_attribs );
327 printf("Error: eglCreateContext failed\n");
332 /* test eglQueryContext() */
335 eglQueryContext(egl_dpy, ctx, EGL_CONTEXT_CLIENT_VERSION, &val);
340 *surfRet = eglCreateWindowSurface(egl_dpy, config, win, NULL);
342 printf("Error: eglCreateWindowSurface failed\n");
349 eglQuerySurface(egl_dpy, *surfRet, EGL_WIDTH, &val);
350 assert(val == width);
351 eglQuerySurface(egl_dpy, *surfRet, EGL_HEIGHT, &val);
352 assert(val == height);
353 assert(eglGetConfigAttrib(egl_dpy, config, EGL_SURFACE_TYPE, &val));
354 assert(val & EGL_WINDOW_BIT);
365 event_loop(Display *dpy, Window win,
366 EGLDisplay egl_dpy, EGLSurface egl_surf)
372 XNextEvent(dpy, &event);
374 switch (event.type) {
378 case ConfigureNotify:
379 reshape(event.xconfigure.width, event.xconfigure.height);
385 code = XLookupKeysym(&event.xkey, 0);
386 if (code == XK_Left) {
389 else if (code == XK_Right) {
392 else if (code == XK_Up) {
395 else if (code == XK_Down) {
399 r = XLookupString(&event.xkey, buffer, sizeof(buffer),
401 if (buffer[0] == 27) {
415 eglSwapBuffers(egl_dpy, egl_surf);
426 printf(" -display <displayname> set the display to run on\n");
427 printf(" -info display OpenGL renderer info\n");
432 main(int argc, char *argv[])
434 const int winWidth = 300, winHeight = 300;
440 char *dpyName = NULL;
441 GLboolean printInfo = GL_FALSE;
442 EGLint egl_major, egl_minor;
446 for (i = 1; i < argc; i++) {
447 if (strcmp(argv[i], "-display") == 0) {
451 else if (strcmp(argv[i], "-info") == 0) {
460 x_dpy = XOpenDisplay(dpyName);
462 printf("Error: couldn't open display %s\n",
463 dpyName ? dpyName : getenv("DISPLAY"));
467 egl_dpy = eglGetDisplay(x_dpy);
469 printf("Error: eglGetDisplay() failed\n");
473 if (!eglInitialize(egl_dpy, &egl_major, &egl_minor)) {
474 printf("Error: eglInitialize() failed\n");
478 s = eglQueryString(egl_dpy, EGL_VERSION);
479 printf("EGL_VERSION = %s\n", s);
481 s = eglQueryString(egl_dpy, EGL_VENDOR);
482 printf("EGL_VENDOR = %s\n", s);
484 s = eglQueryString(egl_dpy, EGL_EXTENSIONS);
485 printf("EGL_EXTENSIONS = %s\n", s);
487 s = eglQueryString(egl_dpy, EGL_CLIENT_APIS);
488 printf("EGL_CLIENT_APIS = %s\n", s);
490 make_x_window(x_dpy, egl_dpy,
491 "OpenGL ES 2.x tri", 0, 0, winWidth, winHeight,
492 &win, &egl_ctx, &egl_surf);
494 XMapWindow(x_dpy, win);
495 if (!eglMakeCurrent(egl_dpy, egl_surf, egl_surf, egl_ctx)) {
496 printf("Error: eglMakeCurrent() failed\n");
501 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
502 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
503 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
504 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
509 /* Set initial projection/viewing transformation.
510 * We can't be sure we'll get a ConfigureNotify event when the window
513 reshape(winWidth, winHeight);
515 event_loop(x_dpy, win, egl_dpy, egl_surf);
517 eglDestroyContext(egl_dpy, egl_ctx);
518 eglDestroySurface(egl_dpy, egl_surf);
519 eglTerminate(egl_dpy);
522 XDestroyWindow(x_dpy, win);
523 XCloseDisplay(x_dpy);