test/testgles.c
author Holmes Futrell <hfutrell@umail.ucsb.edu>
Fri, 15 Aug 2008 00:54:21 +0000
branchgsoc2008_iphone
changeset 2430 166821fa1591
permissions -rw-r--r--
A version of testgl2 written using OpenGL ES calls. Necessary because there's no glBegin/glEnd, etc.
hfutrell@2430
     1
#include <stdlib.h>
hfutrell@2430
     2
#include <stdio.h>
hfutrell@2430
     3
#include <string.h>
hfutrell@2430
     4
#include <math.h>
hfutrell@2430
     5
hfutrell@2430
     6
#include "common.h"
hfutrell@2430
     7
hfutrell@2430
     8
#ifdef __IPHONEOS__
hfutrell@2430
     9
#define HAVE_OPENGLES
hfutrell@2430
    10
#endif
hfutrell@2430
    11
hfutrell@2430
    12
#ifdef HAVE_OPENGLES
hfutrell@2430
    13
hfutrell@2430
    14
#include "SDL_opengles.h"
hfutrell@2430
    15
hfutrell@2430
    16
static CommonState *state;
hfutrell@2430
    17
static SDL_GLContext context;
hfutrell@2430
    18
hfutrell@2430
    19
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
hfutrell@2430
    20
static void
hfutrell@2430
    21
quit(int rc)
hfutrell@2430
    22
{
hfutrell@2430
    23
    if (context) {
hfutrell@2430
    24
        /* SDL_GL_MakeCurrent(0, NULL); *//* doesn't do anything */
hfutrell@2430
    25
        SDL_GL_DeleteContext(context);
hfutrell@2430
    26
    }
hfutrell@2430
    27
    CommonQuit(state);
hfutrell@2430
    28
    exit(rc);
hfutrell@2430
    29
}
hfutrell@2430
    30
hfutrell@2430
    31
static void
hfutrell@2430
    32
Render()
hfutrell@2430
    33
{
hfutrell@2430
    34
	static GLubyte color[8][4] = { {255, 0, 0, 0},
hfutrell@2430
    35
		{ 255,	0, 0, 255},
hfutrell@2430
    36
		{	0, 255, 0, 255},
hfutrell@2430
    37
		{	0, 255, 0, 255},
hfutrell@2430
    38
		{	0, 255, 0, 255},
hfutrell@2430
    39
		{ 255, 255, 255, 255},
hfutrell@2430
    40
		{ 255,	0, 255, 255},
hfutrell@2430
    41
		{	0,	0, 255, 255}
hfutrell@2430
    42
    };
hfutrell@2430
    43
    static GLfloat cube[8][3] = { {0.5, 0.5, -0.5},
hfutrell@2430
    44
		{0.5f, -0.5f, -0.5f},
hfutrell@2430
    45
		{-0.5f, -0.5f, -0.5f},
hfutrell@2430
    46
		{-0.5f, 0.5f, -0.5f},
hfutrell@2430
    47
		{-0.5f, 0.5f, 0.5f},
hfutrell@2430
    48
		{0.5f, 0.5f, 0.5f},
hfutrell@2430
    49
		{0.5f, -0.5f, 0.5f},
hfutrell@2430
    50
		{-0.5f, -0.5f, 0.5f}
hfutrell@2430
    51
    };
hfutrell@2430
    52
	static GLubyte indices[36] = { 0, 3, 4,\
hfutrell@2430
    53
								   4, 5, 0,\
hfutrell@2430
    54
								   0, 5, 6,\
hfutrell@2430
    55
								   6, 1, 0,\
hfutrell@2430
    56
								   6, 7, 2,\
hfutrell@2430
    57
								   2, 1, 6,\
hfutrell@2430
    58
								   7, 4, 3,\
hfutrell@2430
    59
								   3, 2, 7,\
hfutrell@2430
    60
								   5, 4, 7,\
hfutrell@2430
    61
								   7, 6, 5,\
hfutrell@2430
    62
								   2, 3, 1,\
hfutrell@2430
    63
								   3, 0, 1 };
hfutrell@2430
    64
	
hfutrell@2430
    65
	
hfutrell@2430
    66
    /* Do our drawing, too. */
hfutrell@2430
    67
    glClearColor(0.0, 0.0, 0.0, 1.0);
hfutrell@2430
    68
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
hfutrell@2430
    69
		
hfutrell@2430
    70
	/* Draw the cube */
hfutrell@2430
    71
	glColorPointer(4, GL_UNSIGNED_BYTE, 0, color);
hfutrell@2430
    72
	glEnableClientState(GL_COLOR_ARRAY);
hfutrell@2430
    73
	glVertexPointer(3, GL_FLOAT, 0, cube);
hfutrell@2430
    74
	glEnableClientState(GL_VERTEX_ARRAY);
hfutrell@2430
    75
	glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, indices);
hfutrell@2430
    76
	
hfutrell@2430
    77
    glMatrixMode(GL_MODELVIEW);
hfutrell@2430
    78
    glRotatef(5.0, 1.0, 1.0, 1.0);
hfutrell@2430
    79
}
hfutrell@2430
    80
hfutrell@2430
    81
int
hfutrell@2430
    82
main(int argc, char *argv[])
hfutrell@2430
    83
{
hfutrell@2430
    84
    int fsaa, accel;
hfutrell@2430
    85
    int value;
hfutrell@2430
    86
    int i, done;
hfutrell@2430
    87
    SDL_DisplayMode mode;
hfutrell@2430
    88
    SDL_Event event;
hfutrell@2430
    89
    Uint32 then, now, frames;
hfutrell@2430
    90
	
hfutrell@2430
    91
    /* Initialize parameters */
hfutrell@2430
    92
    fsaa = 0;
hfutrell@2430
    93
    accel = 0;
hfutrell@2430
    94
	
hfutrell@2430
    95
    /* Initialize test framework */
hfutrell@2430
    96
    state = CommonCreateState(argv, SDL_INIT_VIDEO);
hfutrell@2430
    97
    if (!state) {
hfutrell@2430
    98
        return 1;
hfutrell@2430
    99
    }
hfutrell@2430
   100
    for (i = 1; i < argc;) {
hfutrell@2430
   101
        int consumed;
hfutrell@2430
   102
		
hfutrell@2430
   103
        consumed = CommonArg(state, i);
hfutrell@2430
   104
        if (consumed == 0) {
hfutrell@2430
   105
            if (SDL_strcasecmp(argv[i], "--fsaa") == 0) {
hfutrell@2430
   106
                ++fsaa;
hfutrell@2430
   107
                consumed = 1;
hfutrell@2430
   108
            } else if (SDL_strcasecmp(argv[i], "--accel") == 0) {
hfutrell@2430
   109
                ++accel;
hfutrell@2430
   110
                consumed = 1;
hfutrell@2430
   111
            } else {
hfutrell@2430
   112
                consumed = -1;
hfutrell@2430
   113
            }
hfutrell@2430
   114
        }
hfutrell@2430
   115
        if (consumed < 0) {
hfutrell@2430
   116
            fprintf(stderr, "Usage: %s %s [--fsaa] [--accel]\n", argv[0],
hfutrell@2430
   117
                    CommonUsage(state));
hfutrell@2430
   118
            quit(1);
hfutrell@2430
   119
        }
hfutrell@2430
   120
        i += consumed;
hfutrell@2430
   121
    }
hfutrell@2430
   122
	
hfutrell@2430
   123
    /* Set OpenGL parameters */
hfutrell@2430
   124
    state->window_flags |= SDL_WINDOW_OPENGL;
hfutrell@2430
   125
    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
hfutrell@2430
   126
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
hfutrell@2430
   127
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
hfutrell@2430
   128
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
hfutrell@2430
   129
    if (fsaa) {
hfutrell@2430
   130
        SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
hfutrell@2430
   131
        SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, fsaa);
hfutrell@2430
   132
    }
hfutrell@2430
   133
    if (accel) {
hfutrell@2430
   134
        SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
hfutrell@2430
   135
    }
hfutrell@2430
   136
    if (!CommonInit(state)) {
hfutrell@2430
   137
        quit(2);
hfutrell@2430
   138
    }
hfutrell@2430
   139
	
hfutrell@2430
   140
    /* Create OpenGL context */
hfutrell@2430
   141
    context = SDL_GL_CreateContext(state->windows[0]);
hfutrell@2430
   142
    if (!context) {
hfutrell@2430
   143
        fprintf(stderr, "SDL_GL_CreateContext(): %s\n", SDL_GetError());
hfutrell@2430
   144
        quit(2);
hfutrell@2430
   145
    }
hfutrell@2430
   146
	
hfutrell@2430
   147
    if (state->render_flags & SDL_RENDERER_PRESENTVSYNC) {
hfutrell@2430
   148
        SDL_GL_SetSwapInterval(1);
hfutrell@2430
   149
    } else {
hfutrell@2430
   150
        SDL_GL_SetSwapInterval(0);
hfutrell@2430
   151
    }
hfutrell@2430
   152
	
hfutrell@2430
   153
    SDL_GetCurrentDisplayMode(&mode);
hfutrell@2430
   154
    printf("Screen BPP: %d\n", SDL_BITSPERPIXEL(mode.format));
hfutrell@2430
   155
    printf("\n");
hfutrell@2430
   156
    printf("Vendor     : %s\n", glGetString(GL_VENDOR));
hfutrell@2430
   157
    printf("Renderer   : %s\n", glGetString(GL_RENDERER));
hfutrell@2430
   158
    printf("Version    : %s\n", glGetString(GL_VERSION));
hfutrell@2430
   159
    printf("Extensions : %s\n", glGetString(GL_EXTENSIONS));
hfutrell@2430
   160
    printf("\n");
hfutrell@2430
   161
	
hfutrell@2430
   162
    SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
hfutrell@2430
   163
    printf("SDL_GL_RED_SIZE: requested %d, got %d\n", 5, value);
hfutrell@2430
   164
    SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value);
hfutrell@2430
   165
    printf("SDL_GL_GREEN_SIZE: requested %d, got %d\n", 5, value);
hfutrell@2430
   166
    SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value);
hfutrell@2430
   167
    printf("SDL_GL_BLUE_SIZE: requested %d, got %d\n", 5, value);
hfutrell@2430
   168
    SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value);
hfutrell@2430
   169
    printf("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", 16, value);
hfutrell@2430
   170
    if (fsaa) {
hfutrell@2430
   171
        SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value);
hfutrell@2430
   172
        printf("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
hfutrell@2430
   173
        SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value);
hfutrell@2430
   174
        printf("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
hfutrell@2430
   175
               value);
hfutrell@2430
   176
    }
hfutrell@2430
   177
    if (accel) {
hfutrell@2430
   178
        SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value);
hfutrell@2430
   179
        printf("SDL_GL_ACCELERATED_VISUAL: requested 1, got %d\n", value);
hfutrell@2430
   180
    }
hfutrell@2430
   181
	
hfutrell@2430
   182
    /* Set rendering settings */
hfutrell@2430
   183
    glMatrixMode(GL_PROJECTION);
hfutrell@2430
   184
    glLoadIdentity();
hfutrell@2430
   185
    glOrthof(-2.0, 2.0, -2.0, 2.0, -20.0, 20.0);
hfutrell@2430
   186
    glMatrixMode(GL_MODELVIEW);
hfutrell@2430
   187
    glLoadIdentity();
hfutrell@2430
   188
    glEnable(GL_DEPTH_TEST);
hfutrell@2430
   189
    glDepthFunc(GL_LESS);
hfutrell@2430
   190
    glShadeModel(GL_SMOOTH);
hfutrell@2430
   191
	
hfutrell@2430
   192
    /* Main render loop */
hfutrell@2430
   193
    frames = 0;
hfutrell@2430
   194
    then = SDL_GetTicks();
hfutrell@2430
   195
    done = 0;
hfutrell@2430
   196
    while (!done) {
hfutrell@2430
   197
        /* Check for events */
hfutrell@2430
   198
        ++frames;
hfutrell@2430
   199
        while (SDL_PollEvent(&event)) {
hfutrell@2430
   200
            CommonEvent(state, &event, &done);
hfutrell@2430
   201
        }
hfutrell@2430
   202
        for (i = 0; i < state->num_windows; ++i) {
hfutrell@2430
   203
            int w, h;
hfutrell@2430
   204
            SDL_GL_MakeCurrent(state->windows[i], context);
hfutrell@2430
   205
            SDL_GetWindowSize(state->windows[i], &w, &h);
hfutrell@2430
   206
            glViewport(0, 0, w, h);
hfutrell@2430
   207
            Render();
hfutrell@2430
   208
            SDL_GL_SwapWindow(state->windows[i]);
hfutrell@2430
   209
        }
hfutrell@2430
   210
    }
hfutrell@2430
   211
	
hfutrell@2430
   212
    /* Print out some timing information */
hfutrell@2430
   213
    now = SDL_GetTicks();
hfutrell@2430
   214
    if (now > then) {
hfutrell@2430
   215
        printf("%2.2f frames per second\n",
hfutrell@2430
   216
               ((double) frames * 1000) / (now - then));
hfutrell@2430
   217
    }
hfutrell@2430
   218
    quit(0);
hfutrell@2430
   219
    return 0;
hfutrell@2430
   220
}
hfutrell@2430
   221
hfutrell@2430
   222
#else /* HAVE_OPENGLES */
hfutrell@2430
   223
hfutrell@2430
   224
int
hfutrell@2430
   225
main(int argc, char *argv[])
hfutrell@2430
   226
{
hfutrell@2430
   227
    printf("No OpenGL ES support on this system\n");
hfutrell@2430
   228
    return 1;
hfutrell@2430
   229
}
hfutrell@2430
   230
hfutrell@2430
   231
#endif /* HAVE_OPENGLES */