Adds testgles2
authorGabriel Jacobo <gabomdq@gmail.com>
Tue, 19 Nov 2013 10:56:38 -0300
changeset 80140a4b6dec7de5
parent 8013 109299fe73ad
child 8015 ba26f042e36d
Adds testgles2

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

Minor fixes, configure script changes done by me.
android-project/jni/src/Android.mk
test/Makefile.in
test/configure
test/configure.in
test/testgles2.c
     1.1 --- a/android-project/jni/src/Android.mk	Tue Nov 19 10:00:05 2013 -0300
     1.2 +++ b/android-project/jni/src/Android.mk	Tue Nov 19 10:56:38 2013 -0300
     1.3 @@ -14,6 +14,6 @@
     1.4  
     1.5  LOCAL_SHARED_LIBRARIES := SDL2
     1.6  
     1.7 -LOCAL_LDLIBS := -lGLESv1_CM -llog
     1.8 +LOCAL_LDLIBS := -lGLESv1_CM -lGLESv2 -llog
     1.9  
    1.10  include $(BUILD_SHARED_LIBRARY)
     2.1 --- a/test/Makefile.in	Tue Nov 19 10:00:05 2013 -0300
     2.2 +++ b/test/Makefile.in	Tue Nov 19 10:56:38 2013 -0300
     2.3 @@ -21,6 +21,7 @@
     2.4  	testgesture$(EXE) \
     2.5  	testgl2$(EXE) \
     2.6  	testgles$(EXE) \
     2.7 +	testgles2$(EXE) \
     2.8  	testhaptic$(EXE) \
     2.9  	testrumble$(EXE) \
    2.10  	testthread$(EXE) \
    2.11 @@ -131,6 +132,9 @@
    2.12  testgles$(EXE): $(srcdir)/testgles.c
    2.13  	$(CC) -o $@ $^ $(CFLAGS) $(LIBS) @GLESLIB@ @MATHLIB@
    2.14  
    2.15 +testgles2$(EXE): $(srcdir)/testgles2.c
    2.16 +	$(CC) -o $@ $^ $(CFLAGS) $(LIBS) @GLES2LIB@ @MATHLIB@
    2.17 +
    2.18  testhaptic$(EXE): $(srcdir)/testhaptic.c
    2.19  	$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
    2.20  
     3.1 --- a/test/configure	Tue Nov 19 10:00:05 2013 -0300
     3.2 +++ b/test/configure	Tue Nov 19 10:56:38 2013 -0300
     3.3 @@ -587,6 +587,7 @@
     3.4  SDL_TTF_LIB
     3.5  XLIB
     3.6  GLESLIB
     3.7 +GLES2LIB
     3.8  GLLIB
     3.9  CPP
    3.10  XMKMF
    3.11 @@ -3860,12 +3861,49 @@
    3.12  { $as_echo "$as_me:${as_lineno-$LINENO}: result: $have_opengles" >&5
    3.13  $as_echo "$have_opengles" >&6; }
    3.14  
    3.15 +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for OpenGL ES2 support" >&5
    3.16 +$as_echo_n "checking for OpenGL ES2 support... " >&6; }
    3.17 +have_opengles2=no
    3.18 +cat confdefs.h - <<_ACEOF >conftest.$ac_ext
    3.19 +/* end confdefs.h.  */
    3.20 +
    3.21 + #if defined (__IPHONEOS__)
    3.22 +    #include <OpenGLES/ES2/gl.h>
    3.23 +    #include <OpenGLES/ES2/glext.h>
    3.24 + #else
    3.25 +    #include <GLES2/gl2.h>
    3.26 +    #include <GLES2/gl2ext.h>
    3.27 + #endif
    3.28 +
    3.29 +int
    3.30 +main ()
    3.31 +{
    3.32 +
    3.33 +
    3.34 +  ;
    3.35 +  return 0;
    3.36 +}
    3.37 +_ACEOF
    3.38 +if ac_fn_c_try_compile "$LINENO"; then :
    3.39 +
    3.40 +have_opengles2=yes
    3.41 +
    3.42 +fi
    3.43 +rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
    3.44 +{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $have_opengles2" >&5
    3.45 +$as_echo "$have_opengles2" >&6; }
    3.46 +
    3.47  GLLIB=""
    3.48  GLESLIB=""
    3.49 +GLES2LIB=""
    3.50  if test x$have_opengles = xyes; then
    3.51      CFLAGS="$CFLAGS -DHAVE_OPENGLES"
    3.52      GLESLIB="$XPATH -lGLESv1_CM"
    3.53  fi
    3.54 +if test x$have_opengles2 = xyes; then
    3.55 +    CFLAGS="$CFLAGS -DHAVE_OPENGLES2"
    3.56 +    GLES2LIB="$XPATH -lGLESv2"
    3.57 +fi
    3.58  if test x$have_opengl = xyes; then
    3.59      CFLAGS="$CFLAGS -DHAVE_OPENGL"
    3.60      GLLIB="$XPATH $SYS_GL_LIBS"
     4.1 --- a/test/configure.in	Tue Nov 19 10:00:05 2013 -0300
     4.2 +++ b/test/configure.in	Tue Nov 19 10:56:38 2013 -0300
     4.3 @@ -135,12 +135,34 @@
     4.4  ])
     4.5  AC_MSG_RESULT($have_opengles)
     4.6  
     4.7 +dnl Check for OpenGL ES2
     4.8 +AC_MSG_CHECKING(for OpenGL ES2 support)
     4.9 +have_opengles2=no
    4.10 +AC_TRY_COMPILE([
    4.11 + #if defined (__IPHONEOS__)
    4.12 +    #include <OpenGLES/ES2/gl.h>
    4.13 +    #include <OpenGLES/ES2/glext.h>
    4.14 + #else
    4.15 +    #include <GLES2/gl2.h>
    4.16 +    #include <GLES2/gl2ext.h>
    4.17 + #endif
    4.18 +],[
    4.19 +],[
    4.20 +have_opengles2=yes
    4.21 +])
    4.22 +AC_MSG_RESULT($have_opengles2)
    4.23 +
    4.24  GLLIB=""
    4.25  GLESLIB=""
    4.26 +GLES2LIB=""
    4.27  if test x$have_opengles = xyes; then
    4.28      CFLAGS="$CFLAGS -DHAVE_OPENGLES"
    4.29      GLESLIB="$XPATH -lGLESv1_CM"
    4.30  fi
    4.31 +if test x$have_opengles2 = xyes; then
    4.32 +    CFLAGS="$CFLAGS -DHAVE_OPENGLES2"
    4.33 +    GLES2LIB="$XPATH -lGLESv2"
    4.34 +fi
    4.35  if test x$have_opengl = xyes; then
    4.36      CFLAGS="$CFLAGS -DHAVE_OPENGL"
    4.37      GLLIB="$XPATH $SYS_GL_LIBS"
    4.38 @@ -148,6 +170,7 @@
    4.39  
    4.40  AC_SUBST(GLLIB)
    4.41  AC_SUBST(GLESLIB)
    4.42 +AC_SUBST(GLES2LIB)
    4.43  AC_SUBST(XLIB)
    4.44  
    4.45  dnl Check for SDL_ttf
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/test/testgles2.c	Tue Nov 19 10:56:38 2013 -0300
     5.3 @@ -0,0 +1,647 @@
     5.4 +/*
     5.5 +  Copyright (r) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     5.6 +
     5.7 +  This software is provided 'as-is', without any express or implied
     5.8 +  warranty.  In no event will the authors be held liable for any damages
     5.9 +  arising from the use of this software.
    5.10 +
    5.11 +  Permission is granted to anyone to use this software for any purpose,
    5.12 +  including commercial applications, and to alter it and redistribute it
    5.13 +  freely.
    5.14 +*/
    5.15 +#include <stdlib.h>
    5.16 +#include <stdio.h>
    5.17 +#include <string.h>
    5.18 +#include <math.h>
    5.19 +
    5.20 +#include "SDL_test_common.h"
    5.21 +
    5.22 +#if defined(__IPHONEOS__) || defined(__ANDROID__)
    5.23 +#define HAVE_OPENGLES2
    5.24 +#endif
    5.25 +
    5.26 +#ifdef HAVE_OPENGLES2
    5.27 +
    5.28 +#include "SDL_opengles2.h"
    5.29 +
    5.30 +static SDLTest_CommonState *state;
    5.31 +static SDL_GLContext *context = NULL;
    5.32 +static int depth = 16;
    5.33 +
    5.34 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    5.35 +static void
    5.36 +quit(int rc)
    5.37 +{
    5.38 +    int i;
    5.39 +
    5.40 +    if (context != NULL) {
    5.41 +        for (i = 0; i < state->num_windows; i++) {
    5.42 +            if (context[i]) {
    5.43 +                SDL_GL_DeleteContext(context[i]);
    5.44 +            }
    5.45 +        }
    5.46 +
    5.47 +        SDL_free(context);
    5.48 +    }
    5.49 +
    5.50 +    SDLTest_CommonQuit(state);
    5.51 +    exit(rc);
    5.52 +}
    5.53 +
    5.54 +#define GL_CHECK(x) \
    5.55 +        x; \
    5.56 +        { \
    5.57 +          GLenum glError = glGetError(); \
    5.58 +          if(glError != GL_NO_ERROR) { \
    5.59 +            fprintf(stderr, "glGetError() = %i (0x%.8x) at line %i\n", glError, glError, __LINE__); \
    5.60 +            quit(1); \
    5.61 +          } \
    5.62 +        }
    5.63 +
    5.64 +/* 
    5.65 + * Simulates desktop's glRotatef. The matrix is returned in column-major 
    5.66 + * order. 
    5.67 + */
    5.68 +static void
    5.69 +rotate_matrix(double angle, double x, double y, double z, float *r)
    5.70 +{
    5.71 +    double radians, c, s, c1, u[3], length;
    5.72 +    int i, j;
    5.73 +
    5.74 +    radians = (angle * M_PI) / 180.0;
    5.75 +
    5.76 +    c = cos(radians);
    5.77 +    s = sin(radians);
    5.78 +
    5.79 +    c1 = 1.0 - cos(radians);
    5.80 +
    5.81 +    length = sqrt(x * x + y * y + z * z);
    5.82 +
    5.83 +    u[0] = x / length;
    5.84 +    u[1] = y / length;
    5.85 +    u[2] = z / length;
    5.86 +
    5.87 +    for (i = 0; i < 16; i++) {
    5.88 +        r[i] = 0.0;
    5.89 +    }
    5.90 +
    5.91 +    r[15] = 1.0;
    5.92 +
    5.93 +    for (i = 0; i < 3; i++) {
    5.94 +        r[i * 4 + (i + 1) % 3] = u[(i + 2) % 3] * s;
    5.95 +        r[i * 4 + (i + 2) % 3] = -u[(i + 1) % 3] * s;
    5.96 +    }
    5.97 +
    5.98 +    for (i = 0; i < 3; i++) {
    5.99 +        for (j = 0; j < 3; j++) {
   5.100 +            r[i * 4 + j] += c1 * u[i] * u[j] + (i == j ? c : 0.0);
   5.101 +        }
   5.102 +    }
   5.103 +}
   5.104 +
   5.105 +/* 
   5.106 + * Simulates gluPerspectiveMatrix 
   5.107 + */
   5.108 +static void 
   5.109 +perspective_matrix(double fovy, double aspect, double znear, double zfar, float *r)
   5.110 +{
   5.111 +    int i;
   5.112 +    double f;
   5.113 +
   5.114 +    f = 1.0/tan(fovy * 0.5);
   5.115 +
   5.116 +    for (i = 0; i < 16; i++) {
   5.117 +        r[i] = 0.0;
   5.118 +    }
   5.119 +
   5.120 +    r[0] = f / aspect;
   5.121 +    r[5] = f;
   5.122 +    r[10] = (znear + zfar) / (znear - zfar);
   5.123 +    r[11] = -1.0;
   5.124 +    r[14] = (2.0 * znear * zfar) / (znear - zfar);
   5.125 +    r[15] = 0.0;
   5.126 +}
   5.127 +
   5.128 +/* 
   5.129 + * Multiplies lhs by rhs and writes out to r. All matrices are 4x4 and column
   5.130 + * major. In-place multiplication is supported.
   5.131 + */
   5.132 +static void
   5.133 +multiply_matrix(float *lhs, float *rhs, float *r)
   5.134 +{
   5.135 +	int i, j, k;
   5.136 +    float tmp[16];
   5.137 +
   5.138 +    for (i = 0; i < 4; i++) {
   5.139 +        for (j = 0; j < 4; j++) {
   5.140 +            tmp[j * 4 + i] = 0.0;
   5.141 +
   5.142 +            for (k = 0; k < 4; k++) {
   5.143 +                tmp[j * 4 + i] += lhs[k * 4 + i] * rhs[j * 4 + k];
   5.144 +            }
   5.145 +        }
   5.146 +    }
   5.147 +
   5.148 +    for (i = 0; i < 16; i++) {
   5.149 +        r[i] = tmp[i];
   5.150 +    }
   5.151 +}
   5.152 +
   5.153 +/* 
   5.154 + * Create shader, load in source, compile, dump debug as necessary.
   5.155 + *
   5.156 + * shader: Pointer to return created shader ID.
   5.157 + * source: Passed-in shader source code.
   5.158 + * shader_type: Passed to GL, e.g. GL_VERTEX_SHADER.
   5.159 + */
   5.160 +void 
   5.161 +process_shader(GLuint *shader, const char * source, GLint shader_type)
   5.162 +{
   5.163 +    GLint status;
   5.164 +    const char *shaders[1] = { NULL };
   5.165 +
   5.166 +    /* Create shader and load into GL. */
   5.167 +    *shader = GL_CHECK(glCreateShader(shader_type));
   5.168 +
   5.169 +    shaders[0] = source;
   5.170 +
   5.171 +    GL_CHECK(glShaderSource(*shader, 1, shaders, NULL));
   5.172 +
   5.173 +    /* Clean up shader source. */
   5.174 +    shaders[0] = NULL;
   5.175 +
   5.176 +    /* Try compiling the shader. */
   5.177 +    GL_CHECK(glCompileShader(*shader));
   5.178 +    GL_CHECK(glGetShaderiv(*shader, GL_COMPILE_STATUS, &status));
   5.179 +
   5.180 +    // Dump debug info (source and log) if compilation failed.
   5.181 +    if(status != GL_TRUE) {
   5.182 +        quit(-1);
   5.183 +    }
   5.184 +}
   5.185 +
   5.186 +/* 3D data. Vertex range -0.5..0.5 in all axes.
   5.187 +* Z -0.5 is near, 0.5 is far. */
   5.188 +const float _vertices[] =
   5.189 +{
   5.190 +    /* Front face. */
   5.191 +    /* Bottom left */
   5.192 +    -0.5,  0.5, -0.5,
   5.193 +    0.5, -0.5, -0.5,
   5.194 +    -0.5, -0.5, -0.5,
   5.195 +    /* Top right */
   5.196 +    -0.5,  0.5, -0.5,
   5.197 +    0.5,  0.5, -0.5,
   5.198 +    0.5, -0.5, -0.5,
   5.199 +    /* Left face */
   5.200 +    /* Bottom left */
   5.201 +    -0.5,  0.5,  0.5,
   5.202 +    -0.5, -0.5, -0.5,
   5.203 +    -0.5, -0.5,  0.5,
   5.204 +    /* Top right */
   5.205 +    -0.5,  0.5,  0.5,
   5.206 +    -0.5,  0.5, -0.5,
   5.207 +    -0.5, -0.5, -0.5,
   5.208 +    /* Top face */
   5.209 +    /* Bottom left */
   5.210 +    -0.5,  0.5,  0.5,
   5.211 +    0.5,  0.5, -0.5,
   5.212 +    -0.5,  0.5, -0.5,
   5.213 +    /* Top right */
   5.214 +    -0.5,  0.5,  0.5,
   5.215 +    0.5,  0.5,  0.5,
   5.216 +    0.5,  0.5, -0.5,
   5.217 +    /* Right face */
   5.218 +    /* Bottom left */
   5.219 +    0.5,  0.5, -0.5,
   5.220 +    0.5, -0.5,  0.5,
   5.221 +    0.5, -0.5, -0.5,
   5.222 +    /* Top right */
   5.223 +    0.5,  0.5, -0.5,
   5.224 +    0.5,  0.5,  0.5,
   5.225 +    0.5, -0.5,  0.5,
   5.226 +    /* Back face */
   5.227 +    /* Bottom left */
   5.228 +    0.5,  0.5,  0.5,
   5.229 +    -0.5, -0.5,  0.5,
   5.230 +    0.5, -0.5,  0.5,
   5.231 +    /* Top right */
   5.232 +    0.5,  0.5,  0.5,
   5.233 +    -0.5,  0.5,  0.5,
   5.234 +    -0.5, -0.5,  0.5,
   5.235 +    /* Bottom face */
   5.236 +    /* Bottom left */
   5.237 +    -0.5, -0.5, -0.5,
   5.238 +    0.5, -0.5,  0.5,
   5.239 +    -0.5, -0.5,  0.5,
   5.240 +    /* Top right */
   5.241 +    -0.5, -0.5, -0.5,
   5.242 +    0.5, -0.5, -0.5,
   5.243 +    0.5, -0.5,  0.5,
   5.244 +};
   5.245 +
   5.246 +const float _colors[] =
   5.247 +{
   5.248 +    /* Front face */
   5.249 +    /* Bottom left */
   5.250 +    1.0, 0.0, 0.0, /* red */
   5.251 +    0.0, 0.0, 1.0, /* blue */
   5.252 +    0.0, 1.0, 0.0, /* green */
   5.253 +    /* Top right */
   5.254 +    1.0, 0.0, 0.0, /* red */
   5.255 +    1.0, 1.0, 0.0, /* yellow */
   5.256 +    0.0, 0.0, 1.0, /* blue */
   5.257 +    /* Left face */
   5.258 +    /* Bottom left */
   5.259 +    1.0, 1.0, 1.0, /* white */
   5.260 +    0.0, 1.0, 0.0, /* green */
   5.261 +    0.0, 1.0, 1.0, /* cyan */
   5.262 +    /* Top right */
   5.263 +    1.0, 1.0, 1.0, /* white */
   5.264 +    1.0, 0.0, 0.0, /* red */
   5.265 +    0.0, 1.0, 0.0, /* green */
   5.266 +    /* Top face */
   5.267 +    /* Bottom left */
   5.268 +    1.0, 1.0, 1.0, /* white */
   5.269 +    1.0, 1.0, 0.0, /* yellow */
   5.270 +    1.0, 0.0, 0.0, /* red */
   5.271 +    /* Top right */
   5.272 +    1.0, 1.0, 1.0, /* white */
   5.273 +    0.0, 0.0, 0.0, /* black */
   5.274 +    1.0, 1.0, 0.0, /* yellow */
   5.275 +    /* Right face */
   5.276 +    /* Bottom left */
   5.277 +    1.0, 1.0, 0.0, /* yellow */
   5.278 +    1.0, 0.0, 1.0, /* magenta */
   5.279 +    0.0, 0.0, 1.0, /* blue */
   5.280 +    /* Top right */
   5.281 +    1.0, 1.0, 0.0, /* yellow */
   5.282 +    0.0, 0.0, 0.0, /* black */
   5.283 +    1.0, 0.0, 1.0, /* magenta */
   5.284 +    /* Back face */
   5.285 +    /* Bottom left */
   5.286 +    0.0, 0.0, 0.0, /* black */
   5.287 +    0.0, 1.0, 1.0, /* cyan */
   5.288 +    1.0, 0.0, 1.0, /* magenta */
   5.289 +    /* Top right */
   5.290 +    0.0, 0.0, 0.0, /* black */
   5.291 +    1.0, 1.0, 1.0, /* white */
   5.292 +    0.0, 1.0, 1.0, /* cyan */
   5.293 +    /* Bottom face */
   5.294 +    /* Bottom left */
   5.295 +    0.0, 1.0, 0.0, /* green */
   5.296 +    1.0, 0.0, 1.0, /* magenta */
   5.297 +    0.0, 1.0, 1.0, /* cyan */
   5.298 +    /* Top right */
   5.299 +    0.0, 1.0, 0.0, /* green */
   5.300 +    0.0, 0.0, 1.0, /* blue */
   5.301 +    1.0, 0.0, 1.0, /* magenta */
   5.302 +};
   5.303 +
   5.304 +const char* _shader_vert_src = 
   5.305 +" attribute vec4 av4position; "
   5.306 +" attribute vec3 av3color; "
   5.307 +" uniform mat4 mvp; "
   5.308 +" varying vec3 vv3color; "
   5.309 +" void main() { "
   5.310 +"    vv3color = av3color; "
   5.311 +"    gl_Position = mvp * av4position; "
   5.312 +" } ";
   5.313 +
   5.314 +const char* _shader_frag_src = 
   5.315 +" precision lowp float; "
   5.316 +" varying vec3 vv3color; "
   5.317 +" void main() { "
   5.318 +"    gl_FragColor = vec4(vv3color, 1.0); "
   5.319 +" } ";
   5.320 +
   5.321 +typedef struct shader_data
   5.322 +{
   5.323 +    GLuint shader_program, shader_frag, shader_vert;
   5.324 +
   5.325 +    GLint attr_position;
   5.326 +    GLint attr_color, attr_mvp;
   5.327 +
   5.328 +    int angle_x, angle_y, angle_z;
   5.329 +
   5.330 +} shader_data;
   5.331 +
   5.332 +static void
   5.333 +Render(unsigned int width, unsigned int height, shader_data* data)
   5.334 +{
   5.335 +    float matrix_rotate[16], matrix_modelview[16], matrix_perspective[16], matrix_mvp[16];
   5.336 +
   5.337 +    /* 
   5.338 +    * Do some rotation with Euler angles. It is not a fixed axis as
   5.339 +    * quaterions would be, but the effect is cool. 
   5.340 +    */
   5.341 +    rotate_matrix(data->angle_x, 1.0, 0.0, 0.0, matrix_modelview);
   5.342 +    rotate_matrix(data->angle_y, 0.0, 1.0, 0.0, matrix_rotate);
   5.343 +
   5.344 +    multiply_matrix(matrix_rotate, matrix_modelview, matrix_modelview);
   5.345 +
   5.346 +    rotate_matrix(data->angle_z, 0.0, 1.0, 0.0, matrix_rotate);
   5.347 +
   5.348 +    multiply_matrix(matrix_rotate, matrix_modelview, matrix_modelview);
   5.349 +
   5.350 +    /* Pull the camera back from the cube */
   5.351 +    matrix_modelview[14] -= 2.5;
   5.352 +
   5.353 +    perspective_matrix(45.0, (double)width/(double)height, 0.01, 100.0, matrix_perspective);
   5.354 +    multiply_matrix(matrix_perspective, matrix_modelview, matrix_mvp);
   5.355 +
   5.356 +    GL_CHECK(glUniformMatrix4fv(data->attr_mvp, 1, GL_FALSE, matrix_mvp));
   5.357 +
   5.358 +    data->angle_x += 3;
   5.359 +    data->angle_y += 2;
   5.360 +    data->angle_z += 1;
   5.361 +
   5.362 +    if(data->angle_x >= 360) data->angle_x -= 360;
   5.363 +    if(data->angle_x < 0) data->angle_x += 360;
   5.364 +    if(data->angle_y >= 360) data->angle_y -= 360;
   5.365 +    if(data->angle_y < 0) data->angle_y += 360;
   5.366 +    if(data->angle_z >= 360) data->angle_z -= 360;
   5.367 +    if(data->angle_z < 0) data->angle_z += 360;
   5.368 +
   5.369 +    GL_CHECK(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT));
   5.370 +    GL_CHECK(glDrawArrays(GL_TRIANGLES, 0, 36));
   5.371 +}
   5.372 +
   5.373 +int
   5.374 +main(int argc, char *argv[])
   5.375 +{
   5.376 +    int fsaa, accel;
   5.377 +    int value;
   5.378 +    int i, done;
   5.379 +    SDL_DisplayMode mode;
   5.380 +    SDL_Event event;
   5.381 +    Uint32 then, now, frames;
   5.382 +    int status;
   5.383 +    shader_data *datas, *data;
   5.384 +
   5.385 +    /* Initialize parameters */
   5.386 +    fsaa = 0;
   5.387 +    accel = 0;
   5.388 +
   5.389 +    /* Initialize test framework */
   5.390 +    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
   5.391 +    if (!state) {
   5.392 +        return 1;
   5.393 +    }
   5.394 +    for (i = 1; i < argc;) {
   5.395 +        int consumed;
   5.396 +
   5.397 +        consumed = SDLTest_CommonArg(state, i);
   5.398 +        if (consumed == 0) {
   5.399 +            if (SDL_strcasecmp(argv[i], "--fsaa") == 0) {
   5.400 +                ++fsaa;
   5.401 +                consumed = 1;
   5.402 +            } else if (SDL_strcasecmp(argv[i], "--accel") == 0) {
   5.403 +                ++accel;
   5.404 +                consumed = 1;
   5.405 +            } else if (SDL_strcasecmp(argv[i], "--zdepth") == 0) {
   5.406 +                i++;
   5.407 +                if (!argv[i]) {
   5.408 +                    consumed = -1;
   5.409 +                } else {
   5.410 +                    depth = SDL_atoi(argv[i]);
   5.411 +                    consumed = 1;
   5.412 +                }
   5.413 +            } else {
   5.414 +                consumed = -1;
   5.415 +            }
   5.416 +        }
   5.417 +        if (consumed < 0) {
   5.418 +            fprintf(stderr, "Usage: %s %s [--fsaa] [--accel] [--zdepth %%d]\n", argv[0],
   5.419 +                    SDLTest_CommonUsage(state));
   5.420 +            quit(1);
   5.421 +        }
   5.422 +        i += consumed;
   5.423 +    }
   5.424 +
   5.425 +    /* Set OpenGL parameters */
   5.426 +    state->window_flags |= SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_BORDERLESS;
   5.427 +    state->gl_red_size = 5;
   5.428 +    state->gl_green_size = 5;
   5.429 +    state->gl_blue_size = 5;
   5.430 +    state->gl_depth_size = depth;
   5.431 +    state->gl_major_version = 2;
   5.432 +    state->gl_minor_version = 0;
   5.433 +    state->gl_profile_mask = SDL_GL_CONTEXT_PROFILE_ES;
   5.434 +
   5.435 +    if (fsaa) {
   5.436 +        state->gl_multisamplebuffers=1;
   5.437 +        state->gl_multisamplesamples=fsaa;
   5.438 +    }
   5.439 +    if (accel) {
   5.440 +        state->gl_accelerated=1;
   5.441 +    }
   5.442 +    if (!SDLTest_CommonInit(state)) {
   5.443 +        return;
   5.444 +        quit(2);
   5.445 +    }
   5.446 +
   5.447 +    context = SDL_calloc(state->num_windows, sizeof(context));
   5.448 +    if (context == NULL) {
   5.449 +        fprintf(stderr, "Out of memory!\n");
   5.450 +        quit(2);
   5.451 +    }
   5.452 +    
   5.453 +    /* Create OpenGL ES contexts */
   5.454 +    for (i = 0; i < state->num_windows; i++) {
   5.455 +        context[i] = SDL_GL_CreateContext(state->windows[i]);
   5.456 +        if (!context[i]) {
   5.457 +            fprintf(stderr, "SDL_GL_CreateContext(): %s\n", SDL_GetError());
   5.458 +            quit(2);
   5.459 +        }
   5.460 +    }
   5.461 +
   5.462 +    if (state->render_flags & SDL_RENDERER_PRESENTVSYNC) {
   5.463 +        SDL_GL_SetSwapInterval(1);
   5.464 +    } else {
   5.465 +        SDL_GL_SetSwapInterval(0);
   5.466 +    }
   5.467 +
   5.468 +    SDL_GetCurrentDisplayMode(0, &mode);
   5.469 +    printf("Screen bpp: %d\n", SDL_BITSPERPIXEL(mode.format));
   5.470 +    printf("\n");
   5.471 +    printf("Vendor     : %s\n", glGetString(GL_VENDOR));
   5.472 +    printf("Renderer   : %s\n", glGetString(GL_RENDERER));
   5.473 +    printf("Version    : %s\n", glGetString(GL_VERSION));
   5.474 +    printf("Extensions : %s\n", glGetString(GL_EXTENSIONS));
   5.475 +    printf("\n");
   5.476 +
   5.477 +    status = SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
   5.478 +    if (!status) {
   5.479 +        printf("SDL_GL_RED_SIZE: requested %d, got %d\n", 5, value);
   5.480 +    } else {
   5.481 +        fprintf(stderr, "Failed to get SDL_GL_RED_SIZE: %s\n",
   5.482 +                SDL_GetError());
   5.483 +    }
   5.484 +    status = SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value);
   5.485 +    if (!status) {
   5.486 +        printf("SDL_GL_GREEN_SIZE: requested %d, got %d\n", 5, value);
   5.487 +    } else {
   5.488 +        fprintf(stderr, "Failed to get SDL_GL_GREEN_SIZE: %s\n",
   5.489 +                SDL_GetError());
   5.490 +    }
   5.491 +    status = SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value);
   5.492 +    if (!status) {
   5.493 +        printf("SDL_GL_BLUE_SIZE: requested %d, got %d\n", 5, value);
   5.494 +    } else {
   5.495 +        fprintf(stderr, "Failed to get SDL_GL_BLUE_SIZE: %s\n",
   5.496 +                SDL_GetError());
   5.497 +    }
   5.498 +    status = SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value);
   5.499 +    if (!status) {
   5.500 +        printf("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", depth, value);
   5.501 +    } else {
   5.502 +        fprintf(stderr, "Failed to get SDL_GL_DEPTH_SIZE: %s\n",
   5.503 +                SDL_GetError());
   5.504 +    }
   5.505 +    if (fsaa) {
   5.506 +        status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value);
   5.507 +        if (!status) {
   5.508 +            printf("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
   5.509 +        } else {
   5.510 +            fprintf(stderr, "Failed to get SDL_GL_MULTISAMPLEBUFFERS: %s\n",
   5.511 +                    SDL_GetError());
   5.512 +        }
   5.513 +        status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value);
   5.514 +        if (!status) {
   5.515 +            printf("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
   5.516 +                   value);
   5.517 +        } else {
   5.518 +            fprintf(stderr, "Failed to get SDL_GL_MULTISAMPLESAMPLES: %s\n",
   5.519 +                    SDL_GetError());
   5.520 +        }
   5.521 +    }
   5.522 +    if (accel) {
   5.523 +        status = SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value);
   5.524 +        if (!status) {
   5.525 +            printf("SDL_GL_ACCELERATED_VISUAL: requested 1, got %d\n", value);
   5.526 +        } else {
   5.527 +            fprintf(stderr, "Failed to get SDL_GL_ACCELERATED_VISUAL: %s\n",
   5.528 +                    SDL_GetError());
   5.529 +        }
   5.530 +    }
   5.531 +
   5.532 +    datas = SDL_calloc(state->num_windows, sizeof(shader_data));
   5.533 +
   5.534 +    /* Set rendering settings for each context */
   5.535 +    for (i = 0; i < state->num_windows; ++i) {
   5.536 +
   5.537 +        status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
   5.538 +        if (status) {
   5.539 +            printf("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
   5.540 +
   5.541 +            /* Continue for next window */
   5.542 +            continue;
   5.543 +        }
   5.544 +        glViewport(0, 0, state->window_w, state->window_h);
   5.545 +
   5.546 +        data = &datas[i];
   5.547 +        data->angle_x = 0; data->angle_y = 0; data->angle_z = 0;
   5.548 +
   5.549 +        /* Shader Initialization */
   5.550 +        process_shader(&data->shader_vert, _shader_vert_src, GL_VERTEX_SHADER);
   5.551 +        process_shader(&data->shader_frag, _shader_frag_src, GL_FRAGMENT_SHADER);
   5.552 +
   5.553 +        /* Create shader_program (ready to attach shaders) */
   5.554 +        data->shader_program = GL_CHECK(glCreateProgram());
   5.555 +
   5.556 +        /* Attach shaders and link shader_program */
   5.557 +        GL_CHECK(glAttachShader(data->shader_program, data->shader_vert));
   5.558 +        GL_CHECK(glAttachShader(data->shader_program, data->shader_frag));
   5.559 +        GL_CHECK(glLinkProgram(data->shader_program));
   5.560 +
   5.561 +        /* Get attribute locations of non-fixed attributes like color and texture coordinates. */
   5.562 +        data->attr_position = GL_CHECK(glGetAttribLocation(data->shader_program, "av4position"));
   5.563 +        data->attr_color = GL_CHECK(glGetAttribLocation(data->shader_program, "av3color"));
   5.564 +
   5.565 +        /* Get uniform locations */
   5.566 +        data->attr_mvp = GL_CHECK(glGetUniformLocation(data->shader_program, "mvp"));
   5.567 +
   5.568 +        GL_CHECK(glUseProgram(data->shader_program));
   5.569 +
   5.570 +        /* Enable attributes for position, color and texture coordinates etc. */
   5.571 +        GL_CHECK(glEnableVertexAttribArray(data->attr_position));
   5.572 +        GL_CHECK(glEnableVertexAttribArray(data->attr_color));
   5.573 +
   5.574 +        /* Populate attributes for position, color and texture coordinates etc. */
   5.575 +        GL_CHECK(glVertexAttribPointer(data->attr_position, 3, GL_FLOAT, GL_FALSE, 0, _vertices));
   5.576 +        GL_CHECK(glVertexAttribPointer(data->attr_color, 3, GL_FLOAT, GL_FALSE, 0, _colors));
   5.577 +
   5.578 +        GL_CHECK(glEnable(GL_CULL_FACE));
   5.579 +        GL_CHECK(glEnable(GL_DEPTH_TEST));
   5.580 +    }
   5.581 +
   5.582 +    /* Main render loop */
   5.583 +    frames = 0;
   5.584 +    then = SDL_GetTicks();
   5.585 +    done = 0;
   5.586 +    while (!done) {
   5.587 +        /* Check for events */
   5.588 +        ++frames;
   5.589 +        while (SDL_PollEvent(&event)) {
   5.590 +            switch (event.type) {
   5.591 +            case SDL_WINDOWEVENT:
   5.592 +                switch (event.window.event) {
   5.593 +                    case SDL_WINDOWEVENT_RESIZED:
   5.594 +                        for (i = 0; i < state->num_windows; ++i) {
   5.595 +                            if (event.window.windowID == SDL_GetWindowID(state->windows[i])) {
   5.596 +                                status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
   5.597 +                                if (status) {
   5.598 +                                    printf("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
   5.599 +                                    break;
   5.600 +                                }
   5.601 +                                /* Change view port to the new window dimensions */
   5.602 +                                glViewport(0, 0, event.window.data1, event.window.data2);
   5.603 +                                /* Update window content */
   5.604 +                                Render(event.window.data1, event.window.data2, &datas[i]);
   5.605 +                                SDL_GL_SwapWindow(state->windows[i]);
   5.606 +                                break;
   5.607 +                            }
   5.608 +                        }
   5.609 +                        break;
   5.610 +                }
   5.611 +            }
   5.612 +            SDLTest_CommonEvent(state, &event, &done);
   5.613 +        }
   5.614 +        for (i = 0; i < state->num_windows; ++i) {
   5.615 +            status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
   5.616 +            if (status) {
   5.617 +                printf("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
   5.618 +
   5.619 +                /* Continue for next window */
   5.620 +                continue;
   5.621 +            }
   5.622 +            Render(state->window_w, state->window_h, &datas[i]);
   5.623 +            SDL_GL_SwapWindow(state->windows[i]);
   5.624 +        }
   5.625 +    }
   5.626 +
   5.627 +    /* Print out some timing information */
   5.628 +    now = SDL_GetTicks();
   5.629 +    if (now > then) {
   5.630 +        printf("%2.2f frames per second\n",
   5.631 +               ((double) frames * 1000) / (now - then));
   5.632 +    }
   5.633 +#if !defined(__ANDROID__)    
   5.634 +    quit(0);
   5.635 +#endif    
   5.636 +    return 0;
   5.637 +}
   5.638 +
   5.639 +#else /* HAVE_OPENGLES2 */
   5.640 +
   5.641 +int
   5.642 +main(int argc, char *argv[])
   5.643 +{
   5.644 +    printf("No OpenGL ES support on this system\n");
   5.645 +    return 1;
   5.646 +}
   5.647 +
   5.648 +#endif /* HAVE_OPENGLES2 */
   5.649 +
   5.650 +/* vi: set ts=4 sw=4 expandtab: */