test/testgles2.c
author Gabriel Jacobo <gabomdq@gmail.com>
Tue, 19 Nov 2013 10:56:38 -0300
changeset 8014 0a4b6dec7de5
child 8021 f96e12e0ade9
permissions -rw-r--r--
Adds testgles2

Source code copied from: https://github.com/fantasydr/sdl-2.0-gles (thanks!)

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