test/testgles2.c
changeset 8014 0a4b6dec7de5
child 8021 f96e12e0ade9
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testgles2.c	Tue Nov 19 10:56:38 2013 -0300
     1.3 @@ -0,0 +1,647 @@
     1.4 +/*
     1.5 +  Copyright (r) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     1.6 +
     1.7 +  This software is provided 'as-is', without any express or implied
     1.8 +  warranty.  In no event will the authors be held liable for any damages
     1.9 +  arising from the use of this software.
    1.10 +
    1.11 +  Permission is granted to anyone to use this software for any purpose,
    1.12 +  including commercial applications, and to alter it and redistribute it
    1.13 +  freely.
    1.14 +*/
    1.15 +#include <stdlib.h>
    1.16 +#include <stdio.h>
    1.17 +#include <string.h>
    1.18 +#include <math.h>
    1.19 +
    1.20 +#include "SDL_test_common.h"
    1.21 +
    1.22 +#if defined(__IPHONEOS__) || defined(__ANDROID__)
    1.23 +#define HAVE_OPENGLES2
    1.24 +#endif
    1.25 +
    1.26 +#ifdef HAVE_OPENGLES2
    1.27 +
    1.28 +#include "SDL_opengles2.h"
    1.29 +
    1.30 +static SDLTest_CommonState *state;
    1.31 +static SDL_GLContext *context = NULL;
    1.32 +static int depth = 16;
    1.33 +
    1.34 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    1.35 +static void
    1.36 +quit(int rc)
    1.37 +{
    1.38 +    int i;
    1.39 +
    1.40 +    if (context != NULL) {
    1.41 +        for (i = 0; i < state->num_windows; i++) {
    1.42 +            if (context[i]) {
    1.43 +                SDL_GL_DeleteContext(context[i]);
    1.44 +            }
    1.45 +        }
    1.46 +
    1.47 +        SDL_free(context);
    1.48 +    }
    1.49 +
    1.50 +    SDLTest_CommonQuit(state);
    1.51 +    exit(rc);
    1.52 +}
    1.53 +
    1.54 +#define GL_CHECK(x) \
    1.55 +        x; \
    1.56 +        { \
    1.57 +          GLenum glError = glGetError(); \
    1.58 +          if(glError != GL_NO_ERROR) { \
    1.59 +            fprintf(stderr, "glGetError() = %i (0x%.8x) at line %i\n", glError, glError, __LINE__); \
    1.60 +            quit(1); \
    1.61 +          } \
    1.62 +        }
    1.63 +
    1.64 +/* 
    1.65 + * Simulates desktop's glRotatef. The matrix is returned in column-major 
    1.66 + * order. 
    1.67 + */
    1.68 +static void
    1.69 +rotate_matrix(double angle, double x, double y, double z, float *r)
    1.70 +{
    1.71 +    double radians, c, s, c1, u[3], length;
    1.72 +    int i, j;
    1.73 +
    1.74 +    radians = (angle * M_PI) / 180.0;
    1.75 +
    1.76 +    c = cos(radians);
    1.77 +    s = sin(radians);
    1.78 +
    1.79 +    c1 = 1.0 - cos(radians);
    1.80 +
    1.81 +    length = sqrt(x * x + y * y + z * z);
    1.82 +
    1.83 +    u[0] = x / length;
    1.84 +    u[1] = y / length;
    1.85 +    u[2] = z / length;
    1.86 +
    1.87 +    for (i = 0; i < 16; i++) {
    1.88 +        r[i] = 0.0;
    1.89 +    }
    1.90 +
    1.91 +    r[15] = 1.0;
    1.92 +
    1.93 +    for (i = 0; i < 3; i++) {
    1.94 +        r[i * 4 + (i + 1) % 3] = u[(i + 2) % 3] * s;
    1.95 +        r[i * 4 + (i + 2) % 3] = -u[(i + 1) % 3] * s;
    1.96 +    }
    1.97 +
    1.98 +    for (i = 0; i < 3; i++) {
    1.99 +        for (j = 0; j < 3; j++) {
   1.100 +            r[i * 4 + j] += c1 * u[i] * u[j] + (i == j ? c : 0.0);
   1.101 +        }
   1.102 +    }
   1.103 +}
   1.104 +
   1.105 +/* 
   1.106 + * Simulates gluPerspectiveMatrix 
   1.107 + */
   1.108 +static void 
   1.109 +perspective_matrix(double fovy, double aspect, double znear, double zfar, float *r)
   1.110 +{
   1.111 +    int i;
   1.112 +    double f;
   1.113 +
   1.114 +    f = 1.0/tan(fovy * 0.5);
   1.115 +
   1.116 +    for (i = 0; i < 16; i++) {
   1.117 +        r[i] = 0.0;
   1.118 +    }
   1.119 +
   1.120 +    r[0] = f / aspect;
   1.121 +    r[5] = f;
   1.122 +    r[10] = (znear + zfar) / (znear - zfar);
   1.123 +    r[11] = -1.0;
   1.124 +    r[14] = (2.0 * znear * zfar) / (znear - zfar);
   1.125 +    r[15] = 0.0;
   1.126 +}
   1.127 +
   1.128 +/* 
   1.129 + * Multiplies lhs by rhs and writes out to r. All matrices are 4x4 and column
   1.130 + * major. In-place multiplication is supported.
   1.131 + */
   1.132 +static void
   1.133 +multiply_matrix(float *lhs, float *rhs, float *r)
   1.134 +{
   1.135 +	int i, j, k;
   1.136 +    float tmp[16];
   1.137 +
   1.138 +    for (i = 0; i < 4; i++) {
   1.139 +        for (j = 0; j < 4; j++) {
   1.140 +            tmp[j * 4 + i] = 0.0;
   1.141 +
   1.142 +            for (k = 0; k < 4; k++) {
   1.143 +                tmp[j * 4 + i] += lhs[k * 4 + i] * rhs[j * 4 + k];
   1.144 +            }
   1.145 +        }
   1.146 +    }
   1.147 +
   1.148 +    for (i = 0; i < 16; i++) {
   1.149 +        r[i] = tmp[i];
   1.150 +    }
   1.151 +}
   1.152 +
   1.153 +/* 
   1.154 + * Create shader, load in source, compile, dump debug as necessary.
   1.155 + *
   1.156 + * shader: Pointer to return created shader ID.
   1.157 + * source: Passed-in shader source code.
   1.158 + * shader_type: Passed to GL, e.g. GL_VERTEX_SHADER.
   1.159 + */
   1.160 +void 
   1.161 +process_shader(GLuint *shader, const char * source, GLint shader_type)
   1.162 +{
   1.163 +    GLint status;
   1.164 +    const char *shaders[1] = { NULL };
   1.165 +
   1.166 +    /* Create shader and load into GL. */
   1.167 +    *shader = GL_CHECK(glCreateShader(shader_type));
   1.168 +
   1.169 +    shaders[0] = source;
   1.170 +
   1.171 +    GL_CHECK(glShaderSource(*shader, 1, shaders, NULL));
   1.172 +
   1.173 +    /* Clean up shader source. */
   1.174 +    shaders[0] = NULL;
   1.175 +
   1.176 +    /* Try compiling the shader. */
   1.177 +    GL_CHECK(glCompileShader(*shader));
   1.178 +    GL_CHECK(glGetShaderiv(*shader, GL_COMPILE_STATUS, &status));
   1.179 +
   1.180 +    // Dump debug info (source and log) if compilation failed.
   1.181 +    if(status != GL_TRUE) {
   1.182 +        quit(-1);
   1.183 +    }
   1.184 +}
   1.185 +
   1.186 +/* 3D data. Vertex range -0.5..0.5 in all axes.
   1.187 +* Z -0.5 is near, 0.5 is far. */
   1.188 +const float _vertices[] =
   1.189 +{
   1.190 +    /* Front face. */
   1.191 +    /* Bottom left */
   1.192 +    -0.5,  0.5, -0.5,
   1.193 +    0.5, -0.5, -0.5,
   1.194 +    -0.5, -0.5, -0.5,
   1.195 +    /* Top right */
   1.196 +    -0.5,  0.5, -0.5,
   1.197 +    0.5,  0.5, -0.5,
   1.198 +    0.5, -0.5, -0.5,
   1.199 +    /* Left face */
   1.200 +    /* Bottom left */
   1.201 +    -0.5,  0.5,  0.5,
   1.202 +    -0.5, -0.5, -0.5,
   1.203 +    -0.5, -0.5,  0.5,
   1.204 +    /* Top right */
   1.205 +    -0.5,  0.5,  0.5,
   1.206 +    -0.5,  0.5, -0.5,
   1.207 +    -0.5, -0.5, -0.5,
   1.208 +    /* Top face */
   1.209 +    /* Bottom left */
   1.210 +    -0.5,  0.5,  0.5,
   1.211 +    0.5,  0.5, -0.5,
   1.212 +    -0.5,  0.5, -0.5,
   1.213 +    /* Top right */
   1.214 +    -0.5,  0.5,  0.5,
   1.215 +    0.5,  0.5,  0.5,
   1.216 +    0.5,  0.5, -0.5,
   1.217 +    /* Right face */
   1.218 +    /* Bottom left */
   1.219 +    0.5,  0.5, -0.5,
   1.220 +    0.5, -0.5,  0.5,
   1.221 +    0.5, -0.5, -0.5,
   1.222 +    /* Top right */
   1.223 +    0.5,  0.5, -0.5,
   1.224 +    0.5,  0.5,  0.5,
   1.225 +    0.5, -0.5,  0.5,
   1.226 +    /* Back face */
   1.227 +    /* Bottom left */
   1.228 +    0.5,  0.5,  0.5,
   1.229 +    -0.5, -0.5,  0.5,
   1.230 +    0.5, -0.5,  0.5,
   1.231 +    /* Top right */
   1.232 +    0.5,  0.5,  0.5,
   1.233 +    -0.5,  0.5,  0.5,
   1.234 +    -0.5, -0.5,  0.5,
   1.235 +    /* Bottom face */
   1.236 +    /* Bottom left */
   1.237 +    -0.5, -0.5, -0.5,
   1.238 +    0.5, -0.5,  0.5,
   1.239 +    -0.5, -0.5,  0.5,
   1.240 +    /* Top right */
   1.241 +    -0.5, -0.5, -0.5,
   1.242 +    0.5, -0.5, -0.5,
   1.243 +    0.5, -0.5,  0.5,
   1.244 +};
   1.245 +
   1.246 +const float _colors[] =
   1.247 +{
   1.248 +    /* Front face */
   1.249 +    /* Bottom left */
   1.250 +    1.0, 0.0, 0.0, /* red */
   1.251 +    0.0, 0.0, 1.0, /* blue */
   1.252 +    0.0, 1.0, 0.0, /* green */
   1.253 +    /* Top right */
   1.254 +    1.0, 0.0, 0.0, /* red */
   1.255 +    1.0, 1.0, 0.0, /* yellow */
   1.256 +    0.0, 0.0, 1.0, /* blue */
   1.257 +    /* Left face */
   1.258 +    /* Bottom left */
   1.259 +    1.0, 1.0, 1.0, /* white */
   1.260 +    0.0, 1.0, 0.0, /* green */
   1.261 +    0.0, 1.0, 1.0, /* cyan */
   1.262 +    /* Top right */
   1.263 +    1.0, 1.0, 1.0, /* white */
   1.264 +    1.0, 0.0, 0.0, /* red */
   1.265 +    0.0, 1.0, 0.0, /* green */
   1.266 +    /* Top face */
   1.267 +    /* Bottom left */
   1.268 +    1.0, 1.0, 1.0, /* white */
   1.269 +    1.0, 1.0, 0.0, /* yellow */
   1.270 +    1.0, 0.0, 0.0, /* red */
   1.271 +    /* Top right */
   1.272 +    1.0, 1.0, 1.0, /* white */
   1.273 +    0.0, 0.0, 0.0, /* black */
   1.274 +    1.0, 1.0, 0.0, /* yellow */
   1.275 +    /* Right face */
   1.276 +    /* Bottom left */
   1.277 +    1.0, 1.0, 0.0, /* yellow */
   1.278 +    1.0, 0.0, 1.0, /* magenta */
   1.279 +    0.0, 0.0, 1.0, /* blue */
   1.280 +    /* Top right */
   1.281 +    1.0, 1.0, 0.0, /* yellow */
   1.282 +    0.0, 0.0, 0.0, /* black */
   1.283 +    1.0, 0.0, 1.0, /* magenta */
   1.284 +    /* Back face */
   1.285 +    /* Bottom left */
   1.286 +    0.0, 0.0, 0.0, /* black */
   1.287 +    0.0, 1.0, 1.0, /* cyan */
   1.288 +    1.0, 0.0, 1.0, /* magenta */
   1.289 +    /* Top right */
   1.290 +    0.0, 0.0, 0.0, /* black */
   1.291 +    1.0, 1.0, 1.0, /* white */
   1.292 +    0.0, 1.0, 1.0, /* cyan */
   1.293 +    /* Bottom face */
   1.294 +    /* Bottom left */
   1.295 +    0.0, 1.0, 0.0, /* green */
   1.296 +    1.0, 0.0, 1.0, /* magenta */
   1.297 +    0.0, 1.0, 1.0, /* cyan */
   1.298 +    /* Top right */
   1.299 +    0.0, 1.0, 0.0, /* green */
   1.300 +    0.0, 0.0, 1.0, /* blue */
   1.301 +    1.0, 0.0, 1.0, /* magenta */
   1.302 +};
   1.303 +
   1.304 +const char* _shader_vert_src = 
   1.305 +" attribute vec4 av4position; "
   1.306 +" attribute vec3 av3color; "
   1.307 +" uniform mat4 mvp; "
   1.308 +" varying vec3 vv3color; "
   1.309 +" void main() { "
   1.310 +"    vv3color = av3color; "
   1.311 +"    gl_Position = mvp * av4position; "
   1.312 +" } ";
   1.313 +
   1.314 +const char* _shader_frag_src = 
   1.315 +" precision lowp float; "
   1.316 +" varying vec3 vv3color; "
   1.317 +" void main() { "
   1.318 +"    gl_FragColor = vec4(vv3color, 1.0); "
   1.319 +" } ";
   1.320 +
   1.321 +typedef struct shader_data
   1.322 +{
   1.323 +    GLuint shader_program, shader_frag, shader_vert;
   1.324 +
   1.325 +    GLint attr_position;
   1.326 +    GLint attr_color, attr_mvp;
   1.327 +
   1.328 +    int angle_x, angle_y, angle_z;
   1.329 +
   1.330 +} shader_data;
   1.331 +
   1.332 +static void
   1.333 +Render(unsigned int width, unsigned int height, shader_data* data)
   1.334 +{
   1.335 +    float matrix_rotate[16], matrix_modelview[16], matrix_perspective[16], matrix_mvp[16];
   1.336 +
   1.337 +    /* 
   1.338 +    * Do some rotation with Euler angles. It is not a fixed axis as
   1.339 +    * quaterions would be, but the effect is cool. 
   1.340 +    */
   1.341 +    rotate_matrix(data->angle_x, 1.0, 0.0, 0.0, matrix_modelview);
   1.342 +    rotate_matrix(data->angle_y, 0.0, 1.0, 0.0, matrix_rotate);
   1.343 +
   1.344 +    multiply_matrix(matrix_rotate, matrix_modelview, matrix_modelview);
   1.345 +
   1.346 +    rotate_matrix(data->angle_z, 0.0, 1.0, 0.0, matrix_rotate);
   1.347 +
   1.348 +    multiply_matrix(matrix_rotate, matrix_modelview, matrix_modelview);
   1.349 +
   1.350 +    /* Pull the camera back from the cube */
   1.351 +    matrix_modelview[14] -= 2.5;
   1.352 +
   1.353 +    perspective_matrix(45.0, (double)width/(double)height, 0.01, 100.0, matrix_perspective);
   1.354 +    multiply_matrix(matrix_perspective, matrix_modelview, matrix_mvp);
   1.355 +
   1.356 +    GL_CHECK(glUniformMatrix4fv(data->attr_mvp, 1, GL_FALSE, matrix_mvp));
   1.357 +
   1.358 +    data->angle_x += 3;
   1.359 +    data->angle_y += 2;
   1.360 +    data->angle_z += 1;
   1.361 +
   1.362 +    if(data->angle_x >= 360) data->angle_x -= 360;
   1.363 +    if(data->angle_x < 0) data->angle_x += 360;
   1.364 +    if(data->angle_y >= 360) data->angle_y -= 360;
   1.365 +    if(data->angle_y < 0) data->angle_y += 360;
   1.366 +    if(data->angle_z >= 360) data->angle_z -= 360;
   1.367 +    if(data->angle_z < 0) data->angle_z += 360;
   1.368 +
   1.369 +    GL_CHECK(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT));
   1.370 +    GL_CHECK(glDrawArrays(GL_TRIANGLES, 0, 36));
   1.371 +}
   1.372 +
   1.373 +int
   1.374 +main(int argc, char *argv[])
   1.375 +{
   1.376 +    int fsaa, accel;
   1.377 +    int value;
   1.378 +    int i, done;
   1.379 +    SDL_DisplayMode mode;
   1.380 +    SDL_Event event;
   1.381 +    Uint32 then, now, frames;
   1.382 +    int status;
   1.383 +    shader_data *datas, *data;
   1.384 +
   1.385 +    /* Initialize parameters */
   1.386 +    fsaa = 0;
   1.387 +    accel = 0;
   1.388 +
   1.389 +    /* Initialize test framework */
   1.390 +    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
   1.391 +    if (!state) {
   1.392 +        return 1;
   1.393 +    }
   1.394 +    for (i = 1; i < argc;) {
   1.395 +        int consumed;
   1.396 +
   1.397 +        consumed = SDLTest_CommonArg(state, i);
   1.398 +        if (consumed == 0) {
   1.399 +            if (SDL_strcasecmp(argv[i], "--fsaa") == 0) {
   1.400 +                ++fsaa;
   1.401 +                consumed = 1;
   1.402 +            } else if (SDL_strcasecmp(argv[i], "--accel") == 0) {
   1.403 +                ++accel;
   1.404 +                consumed = 1;
   1.405 +            } else if (SDL_strcasecmp(argv[i], "--zdepth") == 0) {
   1.406 +                i++;
   1.407 +                if (!argv[i]) {
   1.408 +                    consumed = -1;
   1.409 +                } else {
   1.410 +                    depth = SDL_atoi(argv[i]);
   1.411 +                    consumed = 1;
   1.412 +                }
   1.413 +            } else {
   1.414 +                consumed = -1;
   1.415 +            }
   1.416 +        }
   1.417 +        if (consumed < 0) {
   1.418 +            fprintf(stderr, "Usage: %s %s [--fsaa] [--accel] [--zdepth %%d]\n", argv[0],
   1.419 +                    SDLTest_CommonUsage(state));
   1.420 +            quit(1);
   1.421 +        }
   1.422 +        i += consumed;
   1.423 +    }
   1.424 +
   1.425 +    /* Set OpenGL parameters */
   1.426 +    state->window_flags |= SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_BORDERLESS;
   1.427 +    state->gl_red_size = 5;
   1.428 +    state->gl_green_size = 5;
   1.429 +    state->gl_blue_size = 5;
   1.430 +    state->gl_depth_size = depth;
   1.431 +    state->gl_major_version = 2;
   1.432 +    state->gl_minor_version = 0;
   1.433 +    state->gl_profile_mask = SDL_GL_CONTEXT_PROFILE_ES;
   1.434 +
   1.435 +    if (fsaa) {
   1.436 +        state->gl_multisamplebuffers=1;
   1.437 +        state->gl_multisamplesamples=fsaa;
   1.438 +    }
   1.439 +    if (accel) {
   1.440 +        state->gl_accelerated=1;
   1.441 +    }
   1.442 +    if (!SDLTest_CommonInit(state)) {
   1.443 +        return;
   1.444 +        quit(2);
   1.445 +    }
   1.446 +
   1.447 +    context = SDL_calloc(state->num_windows, sizeof(context));
   1.448 +    if (context == NULL) {
   1.449 +        fprintf(stderr, "Out of memory!\n");
   1.450 +        quit(2);
   1.451 +    }
   1.452 +    
   1.453 +    /* Create OpenGL ES contexts */
   1.454 +    for (i = 0; i < state->num_windows; i++) {
   1.455 +        context[i] = SDL_GL_CreateContext(state->windows[i]);
   1.456 +        if (!context[i]) {
   1.457 +            fprintf(stderr, "SDL_GL_CreateContext(): %s\n", SDL_GetError());
   1.458 +            quit(2);
   1.459 +        }
   1.460 +    }
   1.461 +
   1.462 +    if (state->render_flags & SDL_RENDERER_PRESENTVSYNC) {
   1.463 +        SDL_GL_SetSwapInterval(1);
   1.464 +    } else {
   1.465 +        SDL_GL_SetSwapInterval(0);
   1.466 +    }
   1.467 +
   1.468 +    SDL_GetCurrentDisplayMode(0, &mode);
   1.469 +    printf("Screen bpp: %d\n", SDL_BITSPERPIXEL(mode.format));
   1.470 +    printf("\n");
   1.471 +    printf("Vendor     : %s\n", glGetString(GL_VENDOR));
   1.472 +    printf("Renderer   : %s\n", glGetString(GL_RENDERER));
   1.473 +    printf("Version    : %s\n", glGetString(GL_VERSION));
   1.474 +    printf("Extensions : %s\n", glGetString(GL_EXTENSIONS));
   1.475 +    printf("\n");
   1.476 +
   1.477 +    status = SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
   1.478 +    if (!status) {
   1.479 +        printf("SDL_GL_RED_SIZE: requested %d, got %d\n", 5, value);
   1.480 +    } else {
   1.481 +        fprintf(stderr, "Failed to get SDL_GL_RED_SIZE: %s\n",
   1.482 +                SDL_GetError());
   1.483 +    }
   1.484 +    status = SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value);
   1.485 +    if (!status) {
   1.486 +        printf("SDL_GL_GREEN_SIZE: requested %d, got %d\n", 5, value);
   1.487 +    } else {
   1.488 +        fprintf(stderr, "Failed to get SDL_GL_GREEN_SIZE: %s\n",
   1.489 +                SDL_GetError());
   1.490 +    }
   1.491 +    status = SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value);
   1.492 +    if (!status) {
   1.493 +        printf("SDL_GL_BLUE_SIZE: requested %d, got %d\n", 5, value);
   1.494 +    } else {
   1.495 +        fprintf(stderr, "Failed to get SDL_GL_BLUE_SIZE: %s\n",
   1.496 +                SDL_GetError());
   1.497 +    }
   1.498 +    status = SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value);
   1.499 +    if (!status) {
   1.500 +        printf("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", depth, value);
   1.501 +    } else {
   1.502 +        fprintf(stderr, "Failed to get SDL_GL_DEPTH_SIZE: %s\n",
   1.503 +                SDL_GetError());
   1.504 +    }
   1.505 +    if (fsaa) {
   1.506 +        status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value);
   1.507 +        if (!status) {
   1.508 +            printf("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
   1.509 +        } else {
   1.510 +            fprintf(stderr, "Failed to get SDL_GL_MULTISAMPLEBUFFERS: %s\n",
   1.511 +                    SDL_GetError());
   1.512 +        }
   1.513 +        status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value);
   1.514 +        if (!status) {
   1.515 +            printf("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
   1.516 +                   value);
   1.517 +        } else {
   1.518 +            fprintf(stderr, "Failed to get SDL_GL_MULTISAMPLESAMPLES: %s\n",
   1.519 +                    SDL_GetError());
   1.520 +        }
   1.521 +    }
   1.522 +    if (accel) {
   1.523 +        status = SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value);
   1.524 +        if (!status) {
   1.525 +            printf("SDL_GL_ACCELERATED_VISUAL: requested 1, got %d\n", value);
   1.526 +        } else {
   1.527 +            fprintf(stderr, "Failed to get SDL_GL_ACCELERATED_VISUAL: %s\n",
   1.528 +                    SDL_GetError());
   1.529 +        }
   1.530 +    }
   1.531 +
   1.532 +    datas = SDL_calloc(state->num_windows, sizeof(shader_data));
   1.533 +
   1.534 +    /* Set rendering settings for each context */
   1.535 +    for (i = 0; i < state->num_windows; ++i) {
   1.536 +
   1.537 +        status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
   1.538 +        if (status) {
   1.539 +            printf("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
   1.540 +
   1.541 +            /* Continue for next window */
   1.542 +            continue;
   1.543 +        }
   1.544 +        glViewport(0, 0, state->window_w, state->window_h);
   1.545 +
   1.546 +        data = &datas[i];
   1.547 +        data->angle_x = 0; data->angle_y = 0; data->angle_z = 0;
   1.548 +
   1.549 +        /* Shader Initialization */
   1.550 +        process_shader(&data->shader_vert, _shader_vert_src, GL_VERTEX_SHADER);
   1.551 +        process_shader(&data->shader_frag, _shader_frag_src, GL_FRAGMENT_SHADER);
   1.552 +
   1.553 +        /* Create shader_program (ready to attach shaders) */
   1.554 +        data->shader_program = GL_CHECK(glCreateProgram());
   1.555 +
   1.556 +        /* Attach shaders and link shader_program */
   1.557 +        GL_CHECK(glAttachShader(data->shader_program, data->shader_vert));
   1.558 +        GL_CHECK(glAttachShader(data->shader_program, data->shader_frag));
   1.559 +        GL_CHECK(glLinkProgram(data->shader_program));
   1.560 +
   1.561 +        /* Get attribute locations of non-fixed attributes like color and texture coordinates. */
   1.562 +        data->attr_position = GL_CHECK(glGetAttribLocation(data->shader_program, "av4position"));
   1.563 +        data->attr_color = GL_CHECK(glGetAttribLocation(data->shader_program, "av3color"));
   1.564 +
   1.565 +        /* Get uniform locations */
   1.566 +        data->attr_mvp = GL_CHECK(glGetUniformLocation(data->shader_program, "mvp"));
   1.567 +
   1.568 +        GL_CHECK(glUseProgram(data->shader_program));
   1.569 +
   1.570 +        /* Enable attributes for position, color and texture coordinates etc. */
   1.571 +        GL_CHECK(glEnableVertexAttribArray(data->attr_position));
   1.572 +        GL_CHECK(glEnableVertexAttribArray(data->attr_color));
   1.573 +
   1.574 +        /* Populate attributes for position, color and texture coordinates etc. */
   1.575 +        GL_CHECK(glVertexAttribPointer(data->attr_position, 3, GL_FLOAT, GL_FALSE, 0, _vertices));
   1.576 +        GL_CHECK(glVertexAttribPointer(data->attr_color, 3, GL_FLOAT, GL_FALSE, 0, _colors));
   1.577 +
   1.578 +        GL_CHECK(glEnable(GL_CULL_FACE));
   1.579 +        GL_CHECK(glEnable(GL_DEPTH_TEST));
   1.580 +    }
   1.581 +
   1.582 +    /* Main render loop */
   1.583 +    frames = 0;
   1.584 +    then = SDL_GetTicks();
   1.585 +    done = 0;
   1.586 +    while (!done) {
   1.587 +        /* Check for events */
   1.588 +        ++frames;
   1.589 +        while (SDL_PollEvent(&event)) {
   1.590 +            switch (event.type) {
   1.591 +            case SDL_WINDOWEVENT:
   1.592 +                switch (event.window.event) {
   1.593 +                    case SDL_WINDOWEVENT_RESIZED:
   1.594 +                        for (i = 0; i < state->num_windows; ++i) {
   1.595 +                            if (event.window.windowID == SDL_GetWindowID(state->windows[i])) {
   1.596 +                                status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
   1.597 +                                if (status) {
   1.598 +                                    printf("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
   1.599 +                                    break;
   1.600 +                                }
   1.601 +                                /* Change view port to the new window dimensions */
   1.602 +                                glViewport(0, 0, event.window.data1, event.window.data2);
   1.603 +                                /* Update window content */
   1.604 +                                Render(event.window.data1, event.window.data2, &datas[i]);
   1.605 +                                SDL_GL_SwapWindow(state->windows[i]);
   1.606 +                                break;
   1.607 +                            }
   1.608 +                        }
   1.609 +                        break;
   1.610 +                }
   1.611 +            }
   1.612 +            SDLTest_CommonEvent(state, &event, &done);
   1.613 +        }
   1.614 +        for (i = 0; i < state->num_windows; ++i) {
   1.615 +            status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
   1.616 +            if (status) {
   1.617 +                printf("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
   1.618 +
   1.619 +                /* Continue for next window */
   1.620 +                continue;
   1.621 +            }
   1.622 +            Render(state->window_w, state->window_h, &datas[i]);
   1.623 +            SDL_GL_SwapWindow(state->windows[i]);
   1.624 +        }
   1.625 +    }
   1.626 +
   1.627 +    /* Print out some timing information */
   1.628 +    now = SDL_GetTicks();
   1.629 +    if (now > then) {
   1.630 +        printf("%2.2f frames per second\n",
   1.631 +               ((double) frames * 1000) / (now - then));
   1.632 +    }
   1.633 +#if !defined(__ANDROID__)    
   1.634 +    quit(0);
   1.635 +#endif    
   1.636 +    return 0;
   1.637 +}
   1.638 +
   1.639 +#else /* HAVE_OPENGLES2 */
   1.640 +
   1.641 +int
   1.642 +main(int argc, char *argv[])
   1.643 +{
   1.644 +    printf("No OpenGL ES support on this system\n");
   1.645 +    return 1;
   1.646 +}
   1.647 +
   1.648 +#endif /* HAVE_OPENGLES2 */
   1.649 +
   1.650 +/* vi: set ts=4 sw=4 expandtab: */