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