test/testgles2.c
author Gabriel Jacobo <gabomdq@gmail.com>
Fri, 22 Nov 2013 13:24:53 -0300
changeset 8021 f96e12e0ade9
parent 8014 0a4b6dec7de5
child 8027 21dcb24404c0
permissions -rw-r--r--
OpenGL ES support for Windows
gabomdq@8021
     1
/*
gabomdq@8021
     2
  Copyright (r) 1997-2011 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@8021
    19
#if defined(__IPHONEOS__) || defined(__ANDROID__)
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@8021
   198
gabomdq@8021
   199
    /* Create shader and load into GL. */
gabomdq@8021
   200
    *shader = GL_CHECK(ctx.glCreateShader(shader_type));
gabomdq@8021
   201
gabomdq@8021
   202
    shaders[0] = source;
gabomdq@8021
   203
gabomdq@8021
   204
    GL_CHECK(ctx.glShaderSource(*shader, 1, shaders, NULL));
gabomdq@8021
   205
gabomdq@8021
   206
    /* Clean up shader source. */
gabomdq@8021
   207
    shaders[0] = NULL;
gabomdq@8021
   208
gabomdq@8021
   209
    /* Try compiling the shader. */
gabomdq@8021
   210
    GL_CHECK(ctx.glCompileShader(*shader));
gabomdq@8021
   211
    GL_CHECK(ctx.glGetShaderiv(*shader, GL_COMPILE_STATUS, &status));
gabomdq@8021
   212
gabomdq@8021
   213
    // Dump debug info (source and log) if compilation failed.
gabomdq@8021
   214
    if(status != GL_TRUE) {
gabomdq@8021
   215
        SDL_Log("Shader compilation failed");
gabomdq@8021
   216
        quit(-1);
gabomdq@8021
   217
    }
gabomdq@8021
   218
}
gabomdq@8021
   219
gabomdq@8021
   220
/* 3D data. Vertex range -0.5..0.5 in all axes.
gabomdq@8021
   221
* Z -0.5 is near, 0.5 is far. */
gabomdq@8021
   222
const float _vertices[] =
gabomdq@8021
   223
{
gabomdq@8021
   224
    /* Front face. */
gabomdq@8021
   225
    /* Bottom left */
gabomdq@8021
   226
    -0.5,  0.5, -0.5,
gabomdq@8021
   227
    0.5, -0.5, -0.5,
gabomdq@8021
   228
    -0.5, -0.5, -0.5,
gabomdq@8021
   229
    /* Top right */
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
    /* Left face */
gabomdq@8021
   234
    /* Bottom left */
gabomdq@8021
   235
    -0.5,  0.5,  0.5,
gabomdq@8021
   236
    -0.5, -0.5, -0.5,
gabomdq@8021
   237
    -0.5, -0.5,  0.5,
gabomdq@8021
   238
    /* Top right */
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 face */
gabomdq@8021
   243
    /* Bottom left */
gabomdq@8021
   244
    -0.5,  0.5,  0.5,
gabomdq@8021
   245
    0.5,  0.5, -0.5,
gabomdq@8021
   246
    -0.5,  0.5, -0.5,
gabomdq@8021
   247
    /* Top right */
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
    /* Right face */
gabomdq@8021
   252
    /* Bottom left */
gabomdq@8021
   253
    0.5,  0.5, -0.5,
gabomdq@8021
   254
    0.5, -0.5,  0.5,
gabomdq@8021
   255
    0.5, -0.5, -0.5,
gabomdq@8021
   256
    /* Top right */
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
    /* Back face */
gabomdq@8021
   261
    /* Bottom left */
gabomdq@8021
   262
    0.5,  0.5,  0.5,
gabomdq@8021
   263
    -0.5, -0.5,  0.5,
gabomdq@8021
   264
    0.5, -0.5,  0.5,
gabomdq@8021
   265
    /* Top right */
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
    /* Bottom face */
gabomdq@8021
   270
    /* Bottom left */
gabomdq@8021
   271
    -0.5, -0.5, -0.5,
gabomdq@8021
   272
    0.5, -0.5,  0.5,
gabomdq@8021
   273
    -0.5, -0.5,  0.5,
gabomdq@8021
   274
    /* Top right */
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
};
gabomdq@8021
   279
gabomdq@8021
   280
const float _colors[] =
gabomdq@8021
   281
{
gabomdq@8021
   282
    /* Front face */
gabomdq@8021
   283
    /* Bottom left */
gabomdq@8021
   284
    1.0, 0.0, 0.0, /* red */
gabomdq@8021
   285
    0.0, 0.0, 1.0, /* blue */
gabomdq@8021
   286
    0.0, 1.0, 0.0, /* green */
gabomdq@8021
   287
    /* Top right */
gabomdq@8021
   288
    1.0, 0.0, 0.0, /* red */
gabomdq@8021
   289
    1.0, 1.0, 0.0, /* yellow */
gabomdq@8021
   290
    0.0, 0.0, 1.0, /* blue */
gabomdq@8021
   291
    /* Left face */
gabomdq@8021
   292
    /* Bottom left */
gabomdq@8021
   293
    1.0, 1.0, 1.0, /* white */
gabomdq@8021
   294
    0.0, 1.0, 0.0, /* green */
gabomdq@8021
   295
    0.0, 1.0, 1.0, /* cyan */
gabomdq@8021
   296
    /* Top right */
gabomdq@8021
   297
    1.0, 1.0, 1.0, /* white */
gabomdq@8021
   298
    1.0, 0.0, 0.0, /* red */
gabomdq@8021
   299
    0.0, 1.0, 0.0, /* green */
gabomdq@8021
   300
    /* Top face */
gabomdq@8021
   301
    /* Bottom left */
gabomdq@8021
   302
    1.0, 1.0, 1.0, /* white */
gabomdq@8021
   303
    1.0, 1.0, 0.0, /* yellow */
gabomdq@8021
   304
    1.0, 0.0, 0.0, /* red */
gabomdq@8021
   305
    /* Top right */
gabomdq@8021
   306
    1.0, 1.0, 1.0, /* white */
gabomdq@8021
   307
    0.0, 0.0, 0.0, /* black */
gabomdq@8021
   308
    1.0, 1.0, 0.0, /* yellow */
gabomdq@8021
   309
    /* Right face */
gabomdq@8021
   310
    /* Bottom left */
gabomdq@8021
   311
    1.0, 1.0, 0.0, /* yellow */
gabomdq@8021
   312
    1.0, 0.0, 1.0, /* magenta */
gabomdq@8021
   313
    0.0, 0.0, 1.0, /* blue */
gabomdq@8021
   314
    /* Top right */
gabomdq@8021
   315
    1.0, 1.0, 0.0, /* yellow */
gabomdq@8021
   316
    0.0, 0.0, 0.0, /* black */
gabomdq@8021
   317
    1.0, 0.0, 1.0, /* magenta */
gabomdq@8021
   318
    /* Back face */
gabomdq@8021
   319
    /* Bottom left */
gabomdq@8021
   320
    0.0, 0.0, 0.0, /* black */
gabomdq@8021
   321
    0.0, 1.0, 1.0, /* cyan */
gabomdq@8021
   322
    1.0, 0.0, 1.0, /* magenta */
gabomdq@8021
   323
    /* Top right */
gabomdq@8021
   324
    0.0, 0.0, 0.0, /* black */
gabomdq@8021
   325
    1.0, 1.0, 1.0, /* white */
gabomdq@8021
   326
    0.0, 1.0, 1.0, /* cyan */
gabomdq@8021
   327
    /* Bottom face */
gabomdq@8021
   328
    /* Bottom left */
gabomdq@8021
   329
    0.0, 1.0, 0.0, /* green */
gabomdq@8021
   330
    1.0, 0.0, 1.0, /* magenta */
gabomdq@8021
   331
    0.0, 1.0, 1.0, /* cyan */
gabomdq@8021
   332
    /* Top right */
gabomdq@8021
   333
    0.0, 1.0, 0.0, /* green */
gabomdq@8021
   334
    0.0, 0.0, 1.0, /* blue */
gabomdq@8021
   335
    1.0, 0.0, 1.0, /* magenta */
gabomdq@8021
   336
};
gabomdq@8021
   337
gabomdq@8021
   338
const char* _shader_vert_src = 
gabomdq@8021
   339
" attribute vec4 av4position; "
gabomdq@8021
   340
" attribute vec3 av3color; "
gabomdq@8021
   341
" uniform mat4 mvp; "
gabomdq@8021
   342
" varying vec3 vv3color; "
gabomdq@8021
   343
" void main() { "
gabomdq@8021
   344
"    vv3color = av3color; "
gabomdq@8021
   345
"    gl_Position = mvp * av4position; "
gabomdq@8021
   346
" } ";
gabomdq@8021
   347
gabomdq@8021
   348
const char* _shader_frag_src = 
gabomdq@8021
   349
" precision lowp float; "
gabomdq@8021
   350
" varying vec3 vv3color; "
gabomdq@8021
   351
" void main() { "
gabomdq@8021
   352
"    gl_FragColor = vec4(vv3color, 1.0); "
gabomdq@8021
   353
" } ";
gabomdq@8021
   354
gabomdq@8021
   355
typedef struct shader_data
gabomdq@8021
   356
{
gabomdq@8021
   357
    GLuint shader_program, shader_frag, shader_vert;
gabomdq@8021
   358
gabomdq@8021
   359
    GLint attr_position;
gabomdq@8021
   360
    GLint attr_color, attr_mvp;
gabomdq@8021
   361
gabomdq@8021
   362
    int angle_x, angle_y, angle_z;
gabomdq@8021
   363
gabomdq@8021
   364
} shader_data;
gabomdq@8021
   365
gabomdq@8021
   366
static void
gabomdq@8021
   367
Render(unsigned int width, unsigned int height, shader_data* data)
gabomdq@8021
   368
{
gabomdq@8021
   369
    float matrix_rotate[16], matrix_modelview[16], matrix_perspective[16], matrix_mvp[16];
gabomdq@8021
   370
gabomdq@8021
   371
    /* 
gabomdq@8021
   372
    * Do some rotation with Euler angles. It is not a fixed axis as
gabomdq@8021
   373
    * quaterions would be, but the effect is cool. 
gabomdq@8021
   374
    */
gabomdq@8021
   375
    rotate_matrix(data->angle_x, 1.0, 0.0, 0.0, matrix_modelview);
gabomdq@8021
   376
    rotate_matrix(data->angle_y, 0.0, 1.0, 0.0, matrix_rotate);
gabomdq@8021
   377
gabomdq@8021
   378
    multiply_matrix(matrix_rotate, matrix_modelview, matrix_modelview);
gabomdq@8021
   379
gabomdq@8021
   380
    rotate_matrix(data->angle_z, 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
    /* Pull the camera back from the cube */
gabomdq@8021
   385
    matrix_modelview[14] -= 2.5;
gabomdq@8021
   386
gabomdq@8021
   387
    perspective_matrix(45.0, (double)width/(double)height, 0.01, 100.0, matrix_perspective);
gabomdq@8021
   388
    multiply_matrix(matrix_perspective, matrix_modelview, matrix_mvp);
gabomdq@8021
   389
gabomdq@8021
   390
    GL_CHECK(ctx.glUniformMatrix4fv(data->attr_mvp, 1, GL_FALSE, matrix_mvp));
gabomdq@8021
   391
gabomdq@8021
   392
    data->angle_x += 3;
gabomdq@8021
   393
    data->angle_y += 2;
gabomdq@8021
   394
    data->angle_z += 1;
gabomdq@8021
   395
gabomdq@8021
   396
    if(data->angle_x >= 360) data->angle_x -= 360;
gabomdq@8021
   397
    if(data->angle_x < 0) data->angle_x += 360;
gabomdq@8021
   398
    if(data->angle_y >= 360) data->angle_y -= 360;
gabomdq@8021
   399
    if(data->angle_y < 0) data->angle_y += 360;
gabomdq@8021
   400
    if(data->angle_z >= 360) data->angle_z -= 360;
gabomdq@8021
   401
    if(data->angle_z < 0) data->angle_z += 360;
gabomdq@8021
   402
gabomdq@8021
   403
    GL_CHECK(ctx.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT));
gabomdq@8021
   404
    GL_CHECK(ctx.glDrawArrays(GL_TRIANGLES, 0, 36));
gabomdq@8021
   405
}
gabomdq@8021
   406
gabomdq@8021
   407
int
gabomdq@8021
   408
main(int argc, char *argv[])
gabomdq@8021
   409
{
gabomdq@8021
   410
    int fsaa, accel;
gabomdq@8021
   411
    int value;
gabomdq@8021
   412
    int i, done;
gabomdq@8021
   413
    SDL_DisplayMode mode;
gabomdq@8021
   414
    SDL_Event event;
gabomdq@8021
   415
    Uint32 then, now, frames;
gabomdq@8021
   416
    int status;
gabomdq@8021
   417
    shader_data *datas, *data;
gabomdq@8021
   418
gabomdq@8021
   419
    /* Initialize parameters */
gabomdq@8021
   420
    fsaa = 0;
gabomdq@8021
   421
    accel = 0;
gabomdq@8021
   422
gabomdq@8021
   423
    /* Initialize test framework */
gabomdq@8021
   424
    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
gabomdq@8021
   425
    if (!state) {
gabomdq@8021
   426
        return 1;
gabomdq@8021
   427
    }
gabomdq@8021
   428
    for (i = 1; i < argc;) {
gabomdq@8021
   429
        int consumed;
gabomdq@8021
   430
gabomdq@8021
   431
        consumed = SDLTest_CommonArg(state, i);
gabomdq@8021
   432
        if (consumed == 0) {
gabomdq@8021
   433
            if (SDL_strcasecmp(argv[i], "--fsaa") == 0) {
gabomdq@8021
   434
                ++fsaa;
gabomdq@8021
   435
                consumed = 1;
gabomdq@8021
   436
            } else if (SDL_strcasecmp(argv[i], "--accel") == 0) {
gabomdq@8021
   437
                ++accel;
gabomdq@8021
   438
                consumed = 1;
gabomdq@8021
   439
            } else if (SDL_strcasecmp(argv[i], "--zdepth") == 0) {
gabomdq@8021
   440
                i++;
gabomdq@8021
   441
                if (!argv[i]) {
gabomdq@8021
   442
                    consumed = -1;
gabomdq@8021
   443
                } else {
gabomdq@8021
   444
                    depth = SDL_atoi(argv[i]);
gabomdq@8021
   445
                    consumed = 1;
gabomdq@8021
   446
                }
gabomdq@8021
   447
            } else {
gabomdq@8021
   448
                consumed = -1;
gabomdq@8021
   449
            }
gabomdq@8021
   450
        }
gabomdq@8021
   451
        if (consumed < 0) {
gabomdq@8021
   452
            SDL_Log ("Usage: %s %s [--fsaa] [--accel] [--zdepth %%d]\n", argv[0],
gabomdq@8021
   453
                    SDLTest_CommonUsage(state));
gabomdq@8021
   454
            quit(1);
gabomdq@8021
   455
        }
gabomdq@8021
   456
        i += consumed;
gabomdq@8021
   457
    }
gabomdq@8021
   458
gabomdq@8021
   459
    /* Set OpenGL parameters */
gabomdq@8021
   460
    state->window_flags |= SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_BORDERLESS;
gabomdq@8021
   461
    state->gl_red_size = 5;
gabomdq@8021
   462
    state->gl_green_size = 5;
gabomdq@8021
   463
    state->gl_blue_size = 5;
gabomdq@8021
   464
    state->gl_depth_size = depth;
gabomdq@8021
   465
    state->gl_major_version = 2;
gabomdq@8021
   466
    state->gl_minor_version = 0;
gabomdq@8021
   467
    state->gl_profile_mask = SDL_GL_CONTEXT_PROFILE_ES;
gabomdq@8021
   468
gabomdq@8021
   469
    if (fsaa) {
gabomdq@8021
   470
        state->gl_multisamplebuffers=1;
gabomdq@8021
   471
        state->gl_multisamplesamples=fsaa;
gabomdq@8021
   472
    }
gabomdq@8021
   473
    if (accel) {
gabomdq@8021
   474
        state->gl_accelerated=1;
gabomdq@8021
   475
    }
gabomdq@8021
   476
    if (!SDLTest_CommonInit(state)) {
gabomdq@8021
   477
        quit(2);
gabomdq@8021
   478
        return 0;
gabomdq@8021
   479
    }
gabomdq@8021
   480
gabomdq@8021
   481
    context = SDL_calloc(state->num_windows, sizeof(context));
gabomdq@8021
   482
    if (context == NULL) {
gabomdq@8021
   483
        SDL_Log("Out of memory!\n");
gabomdq@8021
   484
        quit(2);
gabomdq@8021
   485
    }
gabomdq@8021
   486
    
gabomdq@8021
   487
    /* Create OpenGL ES contexts */
gabomdq@8021
   488
    for (i = 0; i < state->num_windows; i++) {
gabomdq@8021
   489
        context[i] = SDL_GL_CreateContext(state->windows[i]);
gabomdq@8021
   490
        if (!context[i]) {
gabomdq@8021
   491
            SDL_Log("SDL_GL_CreateContext(): %s\n", SDL_GetError());
gabomdq@8021
   492
            quit(2);
gabomdq@8021
   493
        }
gabomdq@8021
   494
    }
gabomdq@8021
   495
gabomdq@8021
   496
    /* Important: call this *after* creating the context */
gabomdq@8021
   497
    if (LoadContext(&ctx) < 0) {
gabomdq@8021
   498
        SDL_Log("Could not load GLES2 functions\n");
gabomdq@8021
   499
        quit(2);
gabomdq@8021
   500
        return 0;
gabomdq@8021
   501
    }
gabomdq@8021
   502
gabomdq@8021
   503
gabomdq@8021
   504
gabomdq@8021
   505
    if (state->render_flags & SDL_RENDERER_PRESENTVSYNC) {
gabomdq@8021
   506
        SDL_GL_SetSwapInterval(1);
gabomdq@8021
   507
    } else {
gabomdq@8021
   508
        SDL_GL_SetSwapInterval(0);
gabomdq@8021
   509
    }
gabomdq@8021
   510
gabomdq@8021
   511
    SDL_GetCurrentDisplayMode(0, &mode);
gabomdq@8021
   512
    SDL_Log("Screen bpp: %d\n", SDL_BITSPERPIXEL(mode.format));
gabomdq@8021
   513
    SDL_Log("\n");
gabomdq@8021
   514
    SDL_Log("Vendor     : %s\n", ctx.glGetString(GL_VENDOR));
gabomdq@8021
   515
    SDL_Log("Renderer   : %s\n", ctx.glGetString(GL_RENDERER));
gabomdq@8021
   516
    SDL_Log("Version    : %s\n", ctx.glGetString(GL_VERSION));
gabomdq@8021
   517
    SDL_Log("Extensions : %s\n", ctx.glGetString(GL_EXTENSIONS));
gabomdq@8021
   518
    SDL_Log("\n");
gabomdq@8021
   519
gabomdq@8021
   520
    status = SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
gabomdq@8021
   521
    if (!status) {
gabomdq@8021
   522
        SDL_Log("SDL_GL_RED_SIZE: requested %d, got %d\n", 5, value);
gabomdq@8021
   523
    } else {
gabomdq@8021
   524
        SDL_Log( "Failed to get SDL_GL_RED_SIZE: %s\n",
gabomdq@8021
   525
                SDL_GetError());
gabomdq@8021
   526
    }
gabomdq@8021
   527
    status = SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value);
gabomdq@8021
   528
    if (!status) {
gabomdq@8021
   529
        SDL_Log("SDL_GL_GREEN_SIZE: requested %d, got %d\n", 5, value);
gabomdq@8021
   530
    } else {
gabomdq@8021
   531
        SDL_Log( "Failed to get SDL_GL_GREEN_SIZE: %s\n",
gabomdq@8021
   532
                SDL_GetError());
gabomdq@8021
   533
    }
gabomdq@8021
   534
    status = SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value);
gabomdq@8021
   535
    if (!status) {
gabomdq@8021
   536
        SDL_Log("SDL_GL_BLUE_SIZE: requested %d, got %d\n", 5, value);
gabomdq@8021
   537
    } else {
gabomdq@8021
   538
        SDL_Log( "Failed to get SDL_GL_BLUE_SIZE: %s\n",
gabomdq@8021
   539
                SDL_GetError());
gabomdq@8021
   540
    }
gabomdq@8021
   541
    status = SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value);
gabomdq@8021
   542
    if (!status) {
gabomdq@8021
   543
        SDL_Log("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", depth, value);
gabomdq@8021
   544
    } else {
gabomdq@8021
   545
        SDL_Log( "Failed to get SDL_GL_DEPTH_SIZE: %s\n",
gabomdq@8021
   546
                SDL_GetError());
gabomdq@8021
   547
    }
gabomdq@8021
   548
    if (fsaa) {
gabomdq@8021
   549
        status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value);
gabomdq@8021
   550
        if (!status) {
gabomdq@8021
   551
            SDL_Log("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
gabomdq@8021
   552
        } else {
gabomdq@8021
   553
            SDL_Log( "Failed to get SDL_GL_MULTISAMPLEBUFFERS: %s\n",
gabomdq@8021
   554
                    SDL_GetError());
gabomdq@8021
   555
        }
gabomdq@8021
   556
        status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value);
gabomdq@8021
   557
        if (!status) {
gabomdq@8021
   558
            SDL_Log("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
gabomdq@8021
   559
                   value);
gabomdq@8021
   560
        } else {
gabomdq@8021
   561
            SDL_Log( "Failed to get SDL_GL_MULTISAMPLESAMPLES: %s\n",
gabomdq@8021
   562
                    SDL_GetError());
gabomdq@8021
   563
        }
gabomdq@8021
   564
    }
gabomdq@8021
   565
    if (accel) {
gabomdq@8021
   566
        status = SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value);
gabomdq@8021
   567
        if (!status) {
gabomdq@8021
   568
            SDL_Log("SDL_GL_ACCELERATED_VISUAL: requested 1, got %d\n", value);
gabomdq@8021
   569
        } else {
gabomdq@8021
   570
            SDL_Log( "Failed to get SDL_GL_ACCELERATED_VISUAL: %s\n",
gabomdq@8021
   571
                    SDL_GetError());
gabomdq@8021
   572
        }
gabomdq@8021
   573
    }
gabomdq@8021
   574
gabomdq@8021
   575
    datas = SDL_calloc(state->num_windows, sizeof(shader_data));
gabomdq@8021
   576
gabomdq@8021
   577
    /* Set rendering settings for each context */
gabomdq@8021
   578
    for (i = 0; i < state->num_windows; ++i) {
gabomdq@8021
   579
gabomdq@8021
   580
        status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
gabomdq@8021
   581
        if (status) {
gabomdq@8021
   582
            SDL_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
gabomdq@8021
   583
gabomdq@8021
   584
            /* Continue for next window */
gabomdq@8021
   585
            continue;
gabomdq@8021
   586
        }
gabomdq@8021
   587
        ctx.glViewport(0, 0, state->window_w, state->window_h);
gabomdq@8021
   588
gabomdq@8021
   589
        data = &datas[i];
gabomdq@8021
   590
        data->angle_x = 0; data->angle_y = 0; data->angle_z = 0;
gabomdq@8021
   591
gabomdq@8021
   592
        /* Shader Initialization */
gabomdq@8021
   593
        process_shader(&data->shader_vert, _shader_vert_src, GL_VERTEX_SHADER);
gabomdq@8021
   594
        process_shader(&data->shader_frag, _shader_frag_src, GL_FRAGMENT_SHADER);
gabomdq@8021
   595
gabomdq@8021
   596
        /* Create shader_program (ready to attach shaders) */
gabomdq@8021
   597
        data->shader_program = GL_CHECK(ctx.glCreateProgram());
gabomdq@8021
   598
gabomdq@8021
   599
        /* Attach shaders and link shader_program */
gabomdq@8021
   600
        GL_CHECK(ctx.glAttachShader(data->shader_program, data->shader_vert));
gabomdq@8021
   601
        GL_CHECK(ctx.glAttachShader(data->shader_program, data->shader_frag));
gabomdq@8021
   602
        GL_CHECK(ctx.glLinkProgram(data->shader_program));
gabomdq@8021
   603
gabomdq@8021
   604
        /* Get attribute locations of non-fixed attributes like color and texture coordinates. */
gabomdq@8021
   605
        data->attr_position = GL_CHECK(ctx.glGetAttribLocation(data->shader_program, "av4position"));
gabomdq@8021
   606
        data->attr_color = GL_CHECK(ctx.glGetAttribLocation(data->shader_program, "av3color"));
gabomdq@8021
   607
gabomdq@8021
   608
        /* Get uniform locations */
gabomdq@8021
   609
        data->attr_mvp = GL_CHECK(ctx.glGetUniformLocation(data->shader_program, "mvp"));
gabomdq@8021
   610
gabomdq@8021
   611
        GL_CHECK(ctx.glUseProgram(data->shader_program));
gabomdq@8021
   612
gabomdq@8021
   613
        /* Enable attributes for position, color and texture coordinates etc. */
gabomdq@8021
   614
        GL_CHECK(ctx.glEnableVertexAttribArray(data->attr_position));
gabomdq@8021
   615
        GL_CHECK(ctx.glEnableVertexAttribArray(data->attr_color));
gabomdq@8021
   616
gabomdq@8021
   617
        /* Populate attributes for position, color and texture coordinates etc. */
gabomdq@8021
   618
        GL_CHECK(ctx.glVertexAttribPointer(data->attr_position, 3, GL_FLOAT, GL_FALSE, 0, _vertices));
gabomdq@8021
   619
        GL_CHECK(ctx.glVertexAttribPointer(data->attr_color, 3, GL_FLOAT, GL_FALSE, 0, _colors));
gabomdq@8021
   620
gabomdq@8021
   621
        GL_CHECK(ctx.glEnable(GL_CULL_FACE));
gabomdq@8021
   622
        GL_CHECK(ctx.glEnable(GL_DEPTH_TEST));
gabomdq@8021
   623
    }
gabomdq@8021
   624
gabomdq@8021
   625
    /* Main render loop */
gabomdq@8021
   626
    frames = 0;
gabomdq@8021
   627
    then = SDL_GetTicks();
gabomdq@8021
   628
    done = 0;
gabomdq@8021
   629
    while (!done) {
gabomdq@8021
   630
        /* Check for events */
gabomdq@8021
   631
        ++frames;
gabomdq@8021
   632
        while (SDL_PollEvent(&event)) {
gabomdq@8021
   633
            switch (event.type) {
gabomdq@8021
   634
            case SDL_WINDOWEVENT:
gabomdq@8021
   635
                switch (event.window.event) {
gabomdq@8021
   636
                    case SDL_WINDOWEVENT_RESIZED:
gabomdq@8021
   637
                        for (i = 0; i < state->num_windows; ++i) {
gabomdq@8021
   638
                            if (event.window.windowID == SDL_GetWindowID(state->windows[i])) {
gabomdq@8021
   639
                                status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
gabomdq@8021
   640
                                if (status) {
gabomdq@8021
   641
                                    SDL_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
gabomdq@8021
   642
                                    break;
gabomdq@8021
   643
                                }
gabomdq@8021
   644
                                /* Change view port to the new window dimensions */
gabomdq@8021
   645
                                ctx.glViewport(0, 0, event.window.data1, event.window.data2);
gabomdq@8021
   646
                                /* Update window content */
gabomdq@8021
   647
                                Render(event.window.data1, event.window.data2, &datas[i]);
gabomdq@8021
   648
                                SDL_GL_SwapWindow(state->windows[i]);
gabomdq@8021
   649
                                break;
gabomdq@8021
   650
                            }
gabomdq@8021
   651
                        }
gabomdq@8021
   652
                        break;
gabomdq@8021
   653
                }
gabomdq@8021
   654
            }
gabomdq@8021
   655
            SDLTest_CommonEvent(state, &event, &done);
gabomdq@8021
   656
        }
gabomdq@8021
   657
        for (i = 0; i < state->num_windows; ++i) {
gabomdq@8021
   658
            status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
gabomdq@8021
   659
            if (status) {
gabomdq@8021
   660
                SDL_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
gabomdq@8021
   661
gabomdq@8021
   662
                /* Continue for next window */
gabomdq@8021
   663
                continue;
gabomdq@8021
   664
            }
gabomdq@8021
   665
            Render(state->window_w, state->window_h, &datas[i]);
gabomdq@8021
   666
            SDL_GL_SwapWindow(state->windows[i]);
gabomdq@8021
   667
        }
gabomdq@8021
   668
    }
gabomdq@8021
   669
gabomdq@8021
   670
    /* Print out some timing information */
gabomdq@8021
   671
    now = SDL_GetTicks();
gabomdq@8021
   672
    if (now > then) {
gabomdq@8021
   673
        SDL_Log("%2.2f frames per second\n",
gabomdq@8021
   674
               ((double) frames * 1000) / (now - then));
gabomdq@8021
   675
    }
gabomdq@8021
   676
#if !defined(__ANDROID__)    
gabomdq@8021
   677
    quit(0);
gabomdq@8021
   678
    return 0;
gabomdq@8021
   679
#endif    
gabomdq@8021
   680
    return 0;
gabomdq@8021
   681
}
gabomdq@8021
   682
gabomdq@8021
   683
#else /* HAVE_OPENGLES2 */
gabomdq@8021
   684
gabomdq@8021
   685
int
gabomdq@8021
   686
main(int argc, char *argv[])
gabomdq@8021
   687
{
gabomdq@8021
   688
    SDL_Log("No OpenGL ES support on this system\n");
gabomdq@8021
   689
    return 1;
gabomdq@8021
   690
}
gabomdq@8021
   691
gabomdq@8021
   692
#endif /* HAVE_OPENGLES2 */
gabomdq@8021
   693
gabomdq@8021
   694
/* vi: set ts=4 sw=4 expandtab: */