test/testgles2.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 19 Feb 2015 13:42:56 -0500
changeset 9361 17a2821b451c
parent 9278 8900afb78a19
child 9606 ec24a5add229
permissions -rw-r--r--
Some corrections to the SDL2.spec.in (thanks, Simone!).

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