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