• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2   Copyright (r) 1997-2016 Sam Lantinga <slouken@libsdl.org>
3 
4   This software is provided 'as-is', without any express or implied
5   warranty.  In no event will the authors be held liable for any damages
6   arising from the use of this software.
7 
8   Permission is granted to anyone to use this software for any purpose,
9   including commercial applications, and to alter it and redistribute it
10   freely.
11 */
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <math.h>
16 
17 #ifdef __EMSCRIPTEN__
18 #include <emscripten/emscripten.h>
19 #endif
20 
21 #include "SDL_test_common.h"
22 
23 #if defined(__IPHONEOS__) || defined(__ANDROID__) || defined(__EMSCRIPTEN__) || defined(__NACL__)
24 #define HAVE_OPENGLES2
25 #endif
26 
27 #ifdef HAVE_OPENGLES2
28 
29 #include "SDL_opengles2.h"
30 
31 typedef struct GLES2_Context
32 {
33 #define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
34 #include "../src/render/opengles2/SDL_gles2funcs.h"
35 #undef SDL_PROC
36 } GLES2_Context;
37 
38 
39 static SDLTest_CommonState *state;
40 static SDL_GLContext *context = NULL;
41 static int depth = 16;
42 static GLES2_Context ctx;
43 
LoadContext(GLES2_Context * data)44 static int LoadContext(GLES2_Context * data)
45 {
46 #if SDL_VIDEO_DRIVER_UIKIT
47 #define __SDL_NOGETPROCADDR__
48 #elif SDL_VIDEO_DRIVER_ANDROID
49 #define __SDL_NOGETPROCADDR__
50 #elif SDL_VIDEO_DRIVER_PANDORA
51 #define __SDL_NOGETPROCADDR__
52 #endif
53 
54 #if defined __SDL_NOGETPROCADDR__
55 #define SDL_PROC(ret,func,params) data->func=func;
56 #else
57 #define SDL_PROC(ret,func,params) \
58     do { \
59         data->func = SDL_GL_GetProcAddress(#func); \
60         if ( ! data->func ) { \
61             return SDL_SetError("Couldn't load GLES2 function %s: %s\n", #func, SDL_GetError()); \
62         } \
63     } while ( 0 );
64 #endif /* __SDL_NOGETPROCADDR__ */
65 
66 #include "../src/render/opengles2/SDL_gles2funcs.h"
67 #undef SDL_PROC
68     return 0;
69 }
70 
71 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
72 static void
quit(int rc)73 quit(int rc)
74 {
75     int i;
76 
77     if (context != NULL) {
78         for (i = 0; i < state->num_windows; i++) {
79             if (context[i]) {
80                 SDL_GL_DeleteContext(context[i]);
81             }
82         }
83 
84         SDL_free(context);
85     }
86 
87     SDLTest_CommonQuit(state);
88     exit(rc);
89 }
90 
91 #define GL_CHECK(x) \
92         x; \
93         { \
94           GLenum glError = ctx.glGetError(); \
95           if(glError != GL_NO_ERROR) { \
96             SDL_Log("glGetError() = %i (0x%.8x) at line %i\n", glError, glError, __LINE__); \
97             quit(1); \
98           } \
99         }
100 
101 /*
102  * Simulates desktop's glRotatef. The matrix is returned in column-major
103  * order.
104  */
105 static void
rotate_matrix(float angle,float x,float y,float z,float * r)106 rotate_matrix(float angle, float x, float y, float z, float *r)
107 {
108     float radians, c, s, c1, u[3], length;
109     int i, j;
110 
111     radians = (float)(angle * M_PI) / 180.0f;
112 
113     c = SDL_cosf(radians);
114     s = SDL_sinf(radians);
115 
116     c1 = 1.0f - SDL_cosf(radians);
117 
118     length = (float)SDL_sqrt(x * x + y * y + z * z);
119 
120     u[0] = x / length;
121     u[1] = y / length;
122     u[2] = z / length;
123 
124     for (i = 0; i < 16; i++) {
125         r[i] = 0.0;
126     }
127 
128     r[15] = 1.0;
129 
130     for (i = 0; i < 3; i++) {
131         r[i * 4 + (i + 1) % 3] = u[(i + 2) % 3] * s;
132         r[i * 4 + (i + 2) % 3] = -u[(i + 1) % 3] * s;
133     }
134 
135     for (i = 0; i < 3; i++) {
136         for (j = 0; j < 3; j++) {
137             r[i * 4 + j] += c1 * u[i] * u[j] + (i == j ? c : 0.0f);
138         }
139     }
140 }
141 
142 /*
143  * Simulates gluPerspectiveMatrix
144  */
145 static void
perspective_matrix(float fovy,float aspect,float znear,float zfar,float * r)146 perspective_matrix(float fovy, float aspect, float znear, float zfar, float *r)
147 {
148     int i;
149     float f;
150 
151     f = 1.0f/SDL_tanf(fovy * 0.5f);
152 
153     for (i = 0; i < 16; i++) {
154         r[i] = 0.0;
155     }
156 
157     r[0] = f / aspect;
158     r[5] = f;
159     r[10] = (znear + zfar) / (znear - zfar);
160     r[11] = -1.0f;
161     r[14] = (2.0f * znear * zfar) / (znear - zfar);
162     r[15] = 0.0f;
163 }
164 
165 /*
166  * Multiplies lhs by rhs and writes out to r. All matrices are 4x4 and column
167  * major. In-place multiplication is supported.
168  */
169 static void
multiply_matrix(float * lhs,float * rhs,float * r)170 multiply_matrix(float *lhs, float *rhs, float *r)
171 {
172     int i, j, k;
173     float tmp[16];
174 
175     for (i = 0; i < 4; i++) {
176         for (j = 0; j < 4; j++) {
177             tmp[j * 4 + i] = 0.0;
178 
179             for (k = 0; k < 4; k++) {
180                 tmp[j * 4 + i] += lhs[k * 4 + i] * rhs[j * 4 + k];
181             }
182         }
183     }
184 
185     for (i = 0; i < 16; i++) {
186         r[i] = tmp[i];
187     }
188 }
189 
190 /*
191  * Create shader, load in source, compile, dump debug as necessary.
192  *
193  * shader: Pointer to return created shader ID.
194  * source: Passed-in shader source code.
195  * shader_type: Passed to GL, e.g. GL_VERTEX_SHADER.
196  */
197 void
process_shader(GLuint * shader,const char * source,GLint shader_type)198 process_shader(GLuint *shader, const char * source, GLint shader_type)
199 {
200     GLint status = GL_FALSE;
201     const char *shaders[1] = { NULL };
202     char buffer[1024];
203     GLsizei length;
204 
205     /* Create shader and load into GL. */
206     *shader = GL_CHECK(ctx.glCreateShader(shader_type));
207 
208     shaders[0] = source;
209 
210     GL_CHECK(ctx.glShaderSource(*shader, 1, shaders, NULL));
211 
212     /* Clean up shader source. */
213     shaders[0] = NULL;
214 
215     /* Try compiling the shader. */
216     GL_CHECK(ctx.glCompileShader(*shader));
217     GL_CHECK(ctx.glGetShaderiv(*shader, GL_COMPILE_STATUS, &status));
218 
219     /* Dump debug info (source and log) if compilation failed. */
220     if(status != GL_TRUE) {
221         ctx.glGetProgramInfoLog(*shader, sizeof(buffer), &length, &buffer[0]);
222         buffer[length] = '\0';
223         SDL_Log("Shader compilation failed: %s", buffer);fflush(stderr);
224         quit(-1);
225     }
226 }
227 
228 /* 3D data. Vertex range -0.5..0.5 in all axes.
229 * Z -0.5 is near, 0.5 is far. */
230 const float _vertices[] =
231 {
232     /* Front face. */
233     /* Bottom left */
234     -0.5,  0.5, -0.5,
235     0.5, -0.5, -0.5,
236     -0.5, -0.5, -0.5,
237     /* Top right */
238     -0.5,  0.5, -0.5,
239     0.5,  0.5, -0.5,
240     0.5, -0.5, -0.5,
241     /* Left face */
242     /* Bottom left */
243     -0.5,  0.5,  0.5,
244     -0.5, -0.5, -0.5,
245     -0.5, -0.5,  0.5,
246     /* Top right */
247     -0.5,  0.5,  0.5,
248     -0.5,  0.5, -0.5,
249     -0.5, -0.5, -0.5,
250     /* Top face */
251     /* Bottom left */
252     -0.5,  0.5,  0.5,
253     0.5,  0.5, -0.5,
254     -0.5,  0.5, -0.5,
255     /* Top right */
256     -0.5,  0.5,  0.5,
257     0.5,  0.5,  0.5,
258     0.5,  0.5, -0.5,
259     /* Right face */
260     /* Bottom left */
261     0.5,  0.5, -0.5,
262     0.5, -0.5,  0.5,
263     0.5, -0.5, -0.5,
264     /* Top right */
265     0.5,  0.5, -0.5,
266     0.5,  0.5,  0.5,
267     0.5, -0.5,  0.5,
268     /* Back face */
269     /* Bottom left */
270     0.5,  0.5,  0.5,
271     -0.5, -0.5,  0.5,
272     0.5, -0.5,  0.5,
273     /* Top right */
274     0.5,  0.5,  0.5,
275     -0.5,  0.5,  0.5,
276     -0.5, -0.5,  0.5,
277     /* Bottom face */
278     /* Bottom left */
279     -0.5, -0.5, -0.5,
280     0.5, -0.5,  0.5,
281     -0.5, -0.5,  0.5,
282     /* Top right */
283     -0.5, -0.5, -0.5,
284     0.5, -0.5, -0.5,
285     0.5, -0.5,  0.5,
286 };
287 
288 const float _colors[] =
289 {
290     /* Front face */
291     /* Bottom left */
292     1.0, 0.0, 0.0, /* red */
293     0.0, 0.0, 1.0, /* blue */
294     0.0, 1.0, 0.0, /* green */
295     /* Top right */
296     1.0, 0.0, 0.0, /* red */
297     1.0, 1.0, 0.0, /* yellow */
298     0.0, 0.0, 1.0, /* blue */
299     /* Left face */
300     /* Bottom left */
301     1.0, 1.0, 1.0, /* white */
302     0.0, 1.0, 0.0, /* green */
303     0.0, 1.0, 1.0, /* cyan */
304     /* Top right */
305     1.0, 1.0, 1.0, /* white */
306     1.0, 0.0, 0.0, /* red */
307     0.0, 1.0, 0.0, /* green */
308     /* Top face */
309     /* Bottom left */
310     1.0, 1.0, 1.0, /* white */
311     1.0, 1.0, 0.0, /* yellow */
312     1.0, 0.0, 0.0, /* red */
313     /* Top right */
314     1.0, 1.0, 1.0, /* white */
315     0.0, 0.0, 0.0, /* black */
316     1.0, 1.0, 0.0, /* yellow */
317     /* Right face */
318     /* Bottom left */
319     1.0, 1.0, 0.0, /* yellow */
320     1.0, 0.0, 1.0, /* magenta */
321     0.0, 0.0, 1.0, /* blue */
322     /* Top right */
323     1.0, 1.0, 0.0, /* yellow */
324     0.0, 0.0, 0.0, /* black */
325     1.0, 0.0, 1.0, /* magenta */
326     /* Back face */
327     /* Bottom left */
328     0.0, 0.0, 0.0, /* black */
329     0.0, 1.0, 1.0, /* cyan */
330     1.0, 0.0, 1.0, /* magenta */
331     /* Top right */
332     0.0, 0.0, 0.0, /* black */
333     1.0, 1.0, 1.0, /* white */
334     0.0, 1.0, 1.0, /* cyan */
335     /* Bottom face */
336     /* Bottom left */
337     0.0, 1.0, 0.0, /* green */
338     1.0, 0.0, 1.0, /* magenta */
339     0.0, 1.0, 1.0, /* cyan */
340     /* Top right */
341     0.0, 1.0, 0.0, /* green */
342     0.0, 0.0, 1.0, /* blue */
343     1.0, 0.0, 1.0, /* magenta */
344 };
345 
346 const char* _shader_vert_src =
347 " attribute vec4 av4position; "
348 " attribute vec3 av3color; "
349 " uniform mat4 mvp; "
350 " varying vec3 vv3color; "
351 " void main() { "
352 "    vv3color = av3color; "
353 "    gl_Position = mvp * av4position; "
354 " } ";
355 
356 const char* _shader_frag_src =
357 " precision lowp float; "
358 " varying vec3 vv3color; "
359 " void main() { "
360 "    gl_FragColor = vec4(vv3color, 1.0); "
361 " } ";
362 
363 typedef struct shader_data
364 {
365     GLuint shader_program, shader_frag, shader_vert;
366 
367     GLint attr_position;
368     GLint attr_color, attr_mvp;
369 
370     int angle_x, angle_y, angle_z;
371 
372 } shader_data;
373 
374 static void
Render(unsigned int width,unsigned int height,shader_data * data)375 Render(unsigned int width, unsigned int height, shader_data* data)
376 {
377     float matrix_rotate[16], matrix_modelview[16], matrix_perspective[16], matrix_mvp[16];
378 
379     /*
380     * Do some rotation with Euler angles. It is not a fixed axis as
381     * quaterions would be, but the effect is cool.
382     */
383     rotate_matrix((float)data->angle_x, 1.0f, 0.0f, 0.0f, matrix_modelview);
384     rotate_matrix((float)data->angle_y, 0.0f, 1.0f, 0.0f, matrix_rotate);
385 
386     multiply_matrix(matrix_rotate, matrix_modelview, matrix_modelview);
387 
388     rotate_matrix((float)data->angle_z, 0.0f, 1.0f, 0.0f, matrix_rotate);
389 
390     multiply_matrix(matrix_rotate, matrix_modelview, matrix_modelview);
391 
392     /* Pull the camera back from the cube */
393     matrix_modelview[14] -= 2.5;
394 
395     perspective_matrix(45.0f, (float)width/height, 0.01f, 100.0f, matrix_perspective);
396     multiply_matrix(matrix_perspective, matrix_modelview, matrix_mvp);
397 
398     GL_CHECK(ctx.glUniformMatrix4fv(data->attr_mvp, 1, GL_FALSE, matrix_mvp));
399 
400     data->angle_x += 3;
401     data->angle_y += 2;
402     data->angle_z += 1;
403 
404     if(data->angle_x >= 360) data->angle_x -= 360;
405     if(data->angle_x < 0) data->angle_x += 360;
406     if(data->angle_y >= 360) data->angle_y -= 360;
407     if(data->angle_y < 0) data->angle_y += 360;
408     if(data->angle_z >= 360) data->angle_z -= 360;
409     if(data->angle_z < 0) data->angle_z += 360;
410 
411     GL_CHECK(ctx.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT));
412     GL_CHECK(ctx.glDrawArrays(GL_TRIANGLES, 0, 36));
413 }
414 
415 int done;
416 Uint32 frames;
417 shader_data *datas;
418 
loop()419 void loop()
420 {
421     SDL_Event event;
422     int i;
423     int status;
424 
425     /* Check for events */
426     ++frames;
427     while (SDL_PollEvent(&event) && !done) {
428         switch (event.type) {
429         case SDL_WINDOWEVENT:
430             switch (event.window.event) {
431                 case SDL_WINDOWEVENT_RESIZED:
432                     for (i = 0; i < state->num_windows; ++i) {
433                         if (event.window.windowID == SDL_GetWindowID(state->windows[i])) {
434                             int w, h;
435                             status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
436                             if (status) {
437                                 SDL_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
438                                 break;
439                             }
440                             /* Change view port to the new window dimensions */
441                             SDL_GL_GetDrawableSize(state->windows[i], &w, &h);
442                             ctx.glViewport(0, 0, w, h);
443                             state->window_w = event.window.data1;
444                             state->window_h = event.window.data2;
445                             /* Update window content */
446                             Render(event.window.data1, event.window.data2, &datas[i]);
447                             SDL_GL_SwapWindow(state->windows[i]);
448                             break;
449                         }
450                     }
451                     break;
452             }
453         }
454         SDLTest_CommonEvent(state, &event, &done);
455     }
456     if (!done) {
457       for (i = 0; i < state->num_windows; ++i) {
458           status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
459           if (status) {
460               SDL_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
461 
462               /* Continue for next window */
463               continue;
464           }
465           Render(state->window_w, state->window_h, &datas[i]);
466           SDL_GL_SwapWindow(state->windows[i]);
467       }
468     }
469 #ifdef __EMSCRIPTEN__
470     else {
471         emscripten_cancel_main_loop();
472     }
473 #endif
474 }
475 
476 int
main(int argc,char * argv[])477 main(int argc, char *argv[])
478 {
479     int fsaa, accel;
480     int value;
481     int i;
482     SDL_DisplayMode mode;
483     Uint32 then, now;
484     int status;
485     shader_data *data;
486 
487     /* Initialize parameters */
488     fsaa = 0;
489     accel = 0;
490 
491     /* Initialize test framework */
492     state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
493     if (!state) {
494         return 1;
495     }
496     for (i = 1; i < argc;) {
497         int consumed;
498 
499         consumed = SDLTest_CommonArg(state, i);
500         if (consumed == 0) {
501             if (SDL_strcasecmp(argv[i], "--fsaa") == 0) {
502                 ++fsaa;
503                 consumed = 1;
504             } else if (SDL_strcasecmp(argv[i], "--accel") == 0) {
505                 ++accel;
506                 consumed = 1;
507             } else if (SDL_strcasecmp(argv[i], "--zdepth") == 0) {
508                 i++;
509                 if (!argv[i]) {
510                     consumed = -1;
511                 } else {
512                     depth = SDL_atoi(argv[i]);
513                     consumed = 1;
514                 }
515             } else {
516                 consumed = -1;
517             }
518         }
519         if (consumed < 0) {
520             SDL_Log ("Usage: %s %s [--fsaa] [--accel] [--zdepth %%d]\n", argv[0],
521                     SDLTest_CommonUsage(state));
522             quit(1);
523         }
524         i += consumed;
525     }
526 
527     /* Set OpenGL parameters */
528     state->window_flags |= SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_BORDERLESS;
529     state->gl_red_size = 5;
530     state->gl_green_size = 5;
531     state->gl_blue_size = 5;
532     state->gl_depth_size = depth;
533     state->gl_major_version = 2;
534     state->gl_minor_version = 0;
535     state->gl_profile_mask = SDL_GL_CONTEXT_PROFILE_ES;
536 
537     if (fsaa) {
538         state->gl_multisamplebuffers=1;
539         state->gl_multisamplesamples=fsaa;
540     }
541     if (accel) {
542         state->gl_accelerated=1;
543     }
544     if (!SDLTest_CommonInit(state)) {
545         quit(2);
546         return 0;
547     }
548 
549     context = (SDL_GLContext *)SDL_calloc(state->num_windows, sizeof(context));
550     if (context == NULL) {
551         SDL_Log("Out of memory!\n");
552         quit(2);
553     }
554 
555     /* Create OpenGL ES contexts */
556     for (i = 0; i < state->num_windows; i++) {
557         context[i] = SDL_GL_CreateContext(state->windows[i]);
558         if (!context[i]) {
559             SDL_Log("SDL_GL_CreateContext(): %s\n", SDL_GetError());
560             quit(2);
561         }
562     }
563 
564     /* Important: call this *after* creating the context */
565     if (LoadContext(&ctx) < 0) {
566         SDL_Log("Could not load GLES2 functions\n");
567         quit(2);
568         return 0;
569     }
570 
571 
572 
573     if (state->render_flags & SDL_RENDERER_PRESENTVSYNC) {
574         SDL_GL_SetSwapInterval(1);
575     } else {
576         SDL_GL_SetSwapInterval(0);
577     }
578 
579     SDL_GetCurrentDisplayMode(0, &mode);
580     SDL_Log("Screen bpp: %d\n", SDL_BITSPERPIXEL(mode.format));
581     SDL_Log("\n");
582     SDL_Log("Vendor     : %s\n", ctx.glGetString(GL_VENDOR));
583     SDL_Log("Renderer   : %s\n", ctx.glGetString(GL_RENDERER));
584     SDL_Log("Version    : %s\n", ctx.glGetString(GL_VERSION));
585     SDL_Log("Extensions : %s\n", ctx.glGetString(GL_EXTENSIONS));
586     SDL_Log("\n");
587 
588     status = SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
589     if (!status) {
590         SDL_Log("SDL_GL_RED_SIZE: requested %d, got %d\n", 5, value);
591     } else {
592         SDL_Log( "Failed to get SDL_GL_RED_SIZE: %s\n",
593                 SDL_GetError());
594     }
595     status = SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value);
596     if (!status) {
597         SDL_Log("SDL_GL_GREEN_SIZE: requested %d, got %d\n", 5, value);
598     } else {
599         SDL_Log( "Failed to get SDL_GL_GREEN_SIZE: %s\n",
600                 SDL_GetError());
601     }
602     status = SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value);
603     if (!status) {
604         SDL_Log("SDL_GL_BLUE_SIZE: requested %d, got %d\n", 5, value);
605     } else {
606         SDL_Log( "Failed to get SDL_GL_BLUE_SIZE: %s\n",
607                 SDL_GetError());
608     }
609     status = SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value);
610     if (!status) {
611         SDL_Log("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", depth, value);
612     } else {
613         SDL_Log( "Failed to get SDL_GL_DEPTH_SIZE: %s\n",
614                 SDL_GetError());
615     }
616     if (fsaa) {
617         status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value);
618         if (!status) {
619             SDL_Log("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
620         } else {
621             SDL_Log( "Failed to get SDL_GL_MULTISAMPLEBUFFERS: %s\n",
622                     SDL_GetError());
623         }
624         status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value);
625         if (!status) {
626             SDL_Log("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
627                    value);
628         } else {
629             SDL_Log( "Failed to get SDL_GL_MULTISAMPLESAMPLES: %s\n",
630                     SDL_GetError());
631         }
632     }
633     if (accel) {
634         status = SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value);
635         if (!status) {
636             SDL_Log("SDL_GL_ACCELERATED_VISUAL: requested 1, got %d\n", value);
637         } else {
638             SDL_Log( "Failed to get SDL_GL_ACCELERATED_VISUAL: %s\n",
639                     SDL_GetError());
640         }
641     }
642 
643     datas = (shader_data *)SDL_calloc(state->num_windows, sizeof(shader_data));
644 
645     /* Set rendering settings for each context */
646     for (i = 0; i < state->num_windows; ++i) {
647 
648         int w, h;
649         status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
650         if (status) {
651             SDL_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
652 
653             /* Continue for next window */
654             continue;
655         }
656         SDL_GL_GetDrawableSize(state->windows[i], &w, &h);
657         ctx.glViewport(0, 0, w, h);
658 
659         data = &datas[i];
660         data->angle_x = 0; data->angle_y = 0; data->angle_z = 0;
661 
662         /* Shader Initialization */
663         process_shader(&data->shader_vert, _shader_vert_src, GL_VERTEX_SHADER);
664         process_shader(&data->shader_frag, _shader_frag_src, GL_FRAGMENT_SHADER);
665 
666         /* Create shader_program (ready to attach shaders) */
667         data->shader_program = GL_CHECK(ctx.glCreateProgram());
668 
669         /* Attach shaders and link shader_program */
670         GL_CHECK(ctx.glAttachShader(data->shader_program, data->shader_vert));
671         GL_CHECK(ctx.glAttachShader(data->shader_program, data->shader_frag));
672         GL_CHECK(ctx.glLinkProgram(data->shader_program));
673 
674         /* Get attribute locations of non-fixed attributes like color and texture coordinates. */
675         data->attr_position = GL_CHECK(ctx.glGetAttribLocation(data->shader_program, "av4position"));
676         data->attr_color = GL_CHECK(ctx.glGetAttribLocation(data->shader_program, "av3color"));
677 
678         /* Get uniform locations */
679         data->attr_mvp = GL_CHECK(ctx.glGetUniformLocation(data->shader_program, "mvp"));
680 
681         GL_CHECK(ctx.glUseProgram(data->shader_program));
682 
683         /* Enable attributes for position, color and texture coordinates etc. */
684         GL_CHECK(ctx.glEnableVertexAttribArray(data->attr_position));
685         GL_CHECK(ctx.glEnableVertexAttribArray(data->attr_color));
686 
687         /* Populate attributes for position, color and texture coordinates etc. */
688         GL_CHECK(ctx.glVertexAttribPointer(data->attr_position, 3, GL_FLOAT, GL_FALSE, 0, _vertices));
689         GL_CHECK(ctx.glVertexAttribPointer(data->attr_color, 3, GL_FLOAT, GL_FALSE, 0, _colors));
690 
691         GL_CHECK(ctx.glEnable(GL_CULL_FACE));
692         GL_CHECK(ctx.glEnable(GL_DEPTH_TEST));
693     }
694 
695     /* Main render loop */
696     frames = 0;
697     then = SDL_GetTicks();
698     done = 0;
699 
700 #ifdef __EMSCRIPTEN__
701     emscripten_set_main_loop(loop, 0, 1);
702 #else
703     while (!done) {
704         loop();
705     }
706 #endif
707 
708     /* Print out some timing information */
709     now = SDL_GetTicks();
710     if (now > then) {
711         SDL_Log("%2.2f frames per second\n",
712                ((double) frames * 1000) / (now - then));
713     }
714 #if !defined(__ANDROID__) && !defined(__NACL__)
715     quit(0);
716 #endif
717     return 0;
718 }
719 
720 #else /* HAVE_OPENGLES2 */
721 
722 int
main(int argc,char * argv[])723 main(int argc, char *argv[])
724 {
725     SDL_Log("No OpenGL ES support on this system\n");
726     return 1;
727 }
728 
729 #endif /* HAVE_OPENGLES2 */
730 
731 /* vi: set ts=4 sw=4 expandtab: */
732