test/testgl2.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 12 Aug 2007 16:26:10 +0000
changeset 2232 6630fefab312
parent 2228 bb67fd5ccfdb
child 2328 91e601d9df8b
permissions -rw-r--r--
indent
     1 #include <stdlib.h>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include <math.h>
     5 
     6 #include "common.h"
     7 
     8 #ifdef __MACOS__
     9 #define HAVE_OPENGL
    10 #endif
    11 
    12 #ifdef HAVE_OPENGL
    13 
    14 #include "SDL_opengl.h"
    15 
    16 /* Undefine this if you want a flat cube instead of a rainbow cube */
    17 #define SHADED_CUBE
    18 
    19 static CommonState *state;
    20 static SDL_GLContext context;
    21 
    22 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    23 static void
    24 quit(int rc)
    25 {
    26     if (context) {
    27         SDL_GL_MakeCurrent(0, NULL);
    28         SDL_GL_DeleteContext(context);
    29     }
    30     CommonQuit(state);
    31     exit(rc);
    32 }
    33 
    34 static void
    35 Render()
    36 {
    37     static float color[8][3] = {
    38         {1.0, 1.0, 0.0},
    39         {1.0, 0.0, 0.0},
    40         {0.0, 0.0, 0.0},
    41         {0.0, 1.0, 0.0},
    42         {0.0, 1.0, 1.0},
    43         {1.0, 1.0, 1.0},
    44         {1.0, 0.0, 1.0},
    45         {0.0, 0.0, 1.0}
    46     };
    47     static float cube[8][3] = {
    48         {0.5, 0.5, -0.5},
    49         {0.5, -0.5, -0.5},
    50         {-0.5, -0.5, -0.5},
    51         {-0.5, 0.5, -0.5},
    52         {-0.5, 0.5, 0.5},
    53         {0.5, 0.5, 0.5},
    54         {0.5, -0.5, 0.5},
    55         {-0.5, -0.5, 0.5}
    56     };
    57 
    58     /* Do our drawing, too. */
    59     glClearColor(0.0, 0.0, 0.0, 1.0);
    60     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    61 
    62     glBegin(GL_QUADS);
    63 
    64 #ifdef SHADED_CUBE
    65     glColor3fv(color[0]);
    66     glVertex3fv(cube[0]);
    67     glColor3fv(color[1]);
    68     glVertex3fv(cube[1]);
    69     glColor3fv(color[2]);
    70     glVertex3fv(cube[2]);
    71     glColor3fv(color[3]);
    72     glVertex3fv(cube[3]);
    73 
    74     glColor3fv(color[3]);
    75     glVertex3fv(cube[3]);
    76     glColor3fv(color[4]);
    77     glVertex3fv(cube[4]);
    78     glColor3fv(color[7]);
    79     glVertex3fv(cube[7]);
    80     glColor3fv(color[2]);
    81     glVertex3fv(cube[2]);
    82 
    83     glColor3fv(color[0]);
    84     glVertex3fv(cube[0]);
    85     glColor3fv(color[5]);
    86     glVertex3fv(cube[5]);
    87     glColor3fv(color[6]);
    88     glVertex3fv(cube[6]);
    89     glColor3fv(color[1]);
    90     glVertex3fv(cube[1]);
    91 
    92     glColor3fv(color[5]);
    93     glVertex3fv(cube[5]);
    94     glColor3fv(color[4]);
    95     glVertex3fv(cube[4]);
    96     glColor3fv(color[7]);
    97     glVertex3fv(cube[7]);
    98     glColor3fv(color[6]);
    99     glVertex3fv(cube[6]);
   100 
   101     glColor3fv(color[5]);
   102     glVertex3fv(cube[5]);
   103     glColor3fv(color[0]);
   104     glVertex3fv(cube[0]);
   105     glColor3fv(color[3]);
   106     glVertex3fv(cube[3]);
   107     glColor3fv(color[4]);
   108     glVertex3fv(cube[4]);
   109 
   110     glColor3fv(color[6]);
   111     glVertex3fv(cube[6]);
   112     glColor3fv(color[1]);
   113     glVertex3fv(cube[1]);
   114     glColor3fv(color[2]);
   115     glVertex3fv(cube[2]);
   116     glColor3fv(color[7]);
   117     glVertex3fv(cube[7]);
   118 #else /* flat cube */
   119     glColor3f(1.0, 0.0, 0.0);
   120     glVertex3fv(cube[0]);
   121     glVertex3fv(cube[1]);
   122     glVertex3fv(cube[2]);
   123     glVertex3fv(cube[3]);
   124 
   125     glColor3f(0.0, 1.0, 0.0);
   126     glVertex3fv(cube[3]);
   127     glVertex3fv(cube[4]);
   128     glVertex3fv(cube[7]);
   129     glVertex3fv(cube[2]);
   130 
   131     glColor3f(0.0, 0.0, 1.0);
   132     glVertex3fv(cube[0]);
   133     glVertex3fv(cube[5]);
   134     glVertex3fv(cube[6]);
   135     glVertex3fv(cube[1]);
   136 
   137     glColor3f(0.0, 1.0, 1.0);
   138     glVertex3fv(cube[5]);
   139     glVertex3fv(cube[4]);
   140     glVertex3fv(cube[7]);
   141     glVertex3fv(cube[6]);
   142 
   143     glColor3f(1.0, 1.0, 0.0);
   144     glVertex3fv(cube[5]);
   145     glVertex3fv(cube[0]);
   146     glVertex3fv(cube[3]);
   147     glVertex3fv(cube[4]);
   148 
   149     glColor3f(1.0, 0.0, 1.0);
   150     glVertex3fv(cube[6]);
   151     glVertex3fv(cube[1]);
   152     glVertex3fv(cube[2]);
   153     glVertex3fv(cube[7]);
   154 #endif /* SHADED_CUBE */
   155 
   156     glEnd();
   157 
   158     glMatrixMode(GL_MODELVIEW);
   159     glRotatef(5.0, 1.0, 1.0, 1.0);
   160 }
   161 
   162 int
   163 main(int argc, char *argv[])
   164 {
   165     int fsaa, accel;
   166     int value;
   167     int i, done;
   168     SDL_DisplayMode mode;
   169     SDL_Event event;
   170     Uint32 then, now, frames;
   171 
   172     /* Initialize parameters */
   173     fsaa = 0;
   174     accel = 0;
   175 
   176     /* Initialize test framework */
   177     state = CommonCreateState(argv, SDL_INIT_VIDEO);
   178     if (!state) {
   179         return 1;
   180     }
   181     for (i = 1; i < argc;) {
   182         int consumed;
   183 
   184         consumed = CommonArg(state, i);
   185         if (consumed == 0) {
   186             if (SDL_strcasecmp(argv[i], "--fsaa") == 0) {
   187                 ++fsaa;
   188                 consumed = 1;
   189             } else if (SDL_strcasecmp(argv[i], "--accel") == 0) {
   190                 ++accel;
   191                 consumed = 1;
   192             } else {
   193                 consumed = -1;
   194             }
   195         }
   196         if (consumed < 0) {
   197             fprintf(stderr, "Usage: %s %s [--fsaa] [--accel]\n", argv[0],
   198                     CommonUsage(state));
   199             quit(1);
   200         }
   201         i += consumed;
   202     }
   203 
   204     /* Set OpenGL parameters */
   205     state->window_flags |= SDL_WINDOW_OPENGL;
   206     SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
   207     SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
   208     SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
   209     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
   210     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
   211     if (fsaa) {
   212         SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
   213         SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, fsaa);
   214     }
   215     if (accel) {
   216         SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
   217     }
   218     if (!CommonInit(state)) {
   219         quit(2);
   220     }
   221 
   222     /* Create OpenGL context */
   223     context = SDL_GL_CreateContext(state->windows[0]);
   224     if (!context) {
   225         fprintf(stderr, "SDL_GL_CreateContext(): %s\n", SDL_GetError());
   226         quit(2);
   227     }
   228 
   229     if (state->render_flags & SDL_RENDERER_PRESENTVSYNC) {
   230         SDL_GL_SetSwapInterval(1);
   231     } else {
   232         SDL_GL_SetSwapInterval(0);
   233     }
   234 
   235     SDL_GetCurrentDisplayMode(&mode);
   236     printf("Screen BPP: %d\n", SDL_BITSPERPIXEL(mode.format));
   237     printf("\n");
   238     printf("Vendor     : %s\n", glGetString(GL_VENDOR));
   239     printf("Renderer   : %s\n", glGetString(GL_RENDERER));
   240     printf("Version    : %s\n", glGetString(GL_VERSION));
   241     printf("Extensions : %s\n", glGetString(GL_EXTENSIONS));
   242     printf("\n");
   243 
   244     SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
   245     printf("SDL_GL_RED_SIZE: requested %d, got %d\n", 5, value);
   246     SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value);
   247     printf("SDL_GL_GREEN_SIZE: requested %d, got %d\n", 5, value);
   248     SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value);
   249     printf("SDL_GL_BLUE_SIZE: requested %d, got %d\n", 5, value);
   250     SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value);
   251     printf("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", 16, value);
   252     SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &value);
   253     printf("SDL_GL_DOUBLEBUFFER: requested 1, got %d\n", value);
   254     if (fsaa) {
   255         SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value);
   256         printf("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
   257         SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value);
   258         printf("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
   259                value);
   260     }
   261     if (accel) {
   262         SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value);
   263         printf("SDL_GL_ACCELERATED_VISUAL: requested 1, got %d\n", value);
   264     }
   265 
   266     /* Set rendering settings */
   267     glMatrixMode(GL_PROJECTION);
   268     glLoadIdentity();
   269     glOrtho(-2.0, 2.0, -2.0, 2.0, -20.0, 20.0);
   270     glMatrixMode(GL_MODELVIEW);
   271     glLoadIdentity();
   272     glEnable(GL_DEPTH_TEST);
   273     glDepthFunc(GL_LESS);
   274     glShadeModel(GL_SMOOTH);
   275 
   276     /* Main render loop */
   277     frames = 0;
   278     then = SDL_GetTicks();
   279     done = 0;
   280     while (!done) {
   281         /* Check for events */
   282         ++frames;
   283         while (SDL_PollEvent(&event)) {
   284             CommonEvent(state, &event, &done);
   285         }
   286         for (i = 0; i < state->num_windows; ++i) {
   287             int w, h;
   288             SDL_GL_MakeCurrent(state->windows[i], context);
   289             SDL_GetWindowSize(state->windows[i], &w, &h);
   290             glViewport(0, 0, w, h);
   291             Render();
   292             SDL_GL_SwapWindow(state->windows[i]);
   293         }
   294     }
   295 
   296     /* Print out some timing information */
   297     now = SDL_GetTicks();
   298     if (now > then) {
   299         printf("%2.2f frames per second\n",
   300                ((double) frames * 1000) / (now - then));
   301     }
   302     quit(0);
   303     return 0;
   304 }
   305 
   306 #else /* HAVE_OPENGL */
   307 
   308 int
   309 main(int argc, char *argv[])
   310 {
   311     printf("No OpenGL support on this system\n");
   312     return 1;
   313 }
   314 
   315 #endif /* HAVE_OPENGL */