test/testgl2.c
changeset 8062 4fc5f66d63cc
parent 7787 e6f3e8fc96ea
child 8149 681eb46b8ac4
equal deleted inserted replaced
8061:a5f8137ccf01 8062:4fc5f66d63cc
    22 
    22 
    23 #ifdef HAVE_OPENGL
    23 #ifdef HAVE_OPENGL
    24 
    24 
    25 #include "SDL_opengl.h"
    25 #include "SDL_opengl.h"
    26 
    26 
       
    27 typedef struct GL_Context
       
    28 {
       
    29 #define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
       
    30 #include "../src/render/opengl/SDL_glfuncs.h"
       
    31 #undef SDL_PROC
       
    32 } GL_Context;
       
    33 
       
    34 
    27 /* Undefine this if you want a flat cube instead of a rainbow cube */
    35 /* Undefine this if you want a flat cube instead of a rainbow cube */
    28 #define SHADED_CUBE
    36 #define SHADED_CUBE
    29 
    37 
    30 static SDLTest_CommonState *state;
    38 static SDLTest_CommonState *state;
    31 static SDL_GLContext context;
    39 static SDL_GLContext context;
       
    40 static GL_Context ctx;
       
    41 
       
    42 static int LoadContext(GL_Context * data)
       
    43 {
       
    44 #if SDL_VIDEO_DRIVER_UIKIT
       
    45 #define __SDL_NOGETPROCADDR__
       
    46 #elif SDL_VIDEO_DRIVER_ANDROID
       
    47 #define __SDL_NOGETPROCADDR__
       
    48 #elif SDL_VIDEO_DRIVER_PANDORA
       
    49 #define __SDL_NOGETPROCADDR__
       
    50 #endif
       
    51 
       
    52 #if defined __SDL_NOGETPROCADDR__
       
    53 #define SDL_PROC(ret,func,params) data->func=func;
       
    54 #else
       
    55 #define SDL_PROC(ret,func,params) \
       
    56     do { \
       
    57         data->func = SDL_GL_GetProcAddress(#func); \
       
    58         if ( ! data->func ) { \
       
    59             return SDL_SetError("Couldn't load GL function %s: %s\n", #func, SDL_GetError()); \
       
    60         } \
       
    61     } while ( 0 );
       
    62 #endif /* _SDL_NOGETPROCADDR_ */
       
    63 
       
    64 #include "../src/render/opengl/SDL_glfuncs.h"
       
    65 #undef SDL_PROC
       
    66     return 0;
       
    67 }
       
    68 
    32 
    69 
    33 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    70 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    34 static void
    71 static void
    35 quit(int rc)
    72 quit(int rc)
    36 {
    73 {
    65         {0.5, -0.5, 0.5},
   102         {0.5, -0.5, 0.5},
    66         {-0.5, -0.5, 0.5}
   103         {-0.5, -0.5, 0.5}
    67     };
   104     };
    68 
   105 
    69     /* Do our drawing, too. */
   106     /* Do our drawing, too. */
    70     glClearColor(0.0, 0.0, 0.0, 1.0);
   107     ctx.glClearColor(0.0, 0.0, 0.0, 1.0);
    71     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   108     ctx.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    72 
   109 
    73     glBegin(GL_QUADS);
   110     ctx.glBegin(GL_QUADS);
    74 
   111 
    75 #ifdef SHADED_CUBE
   112 #ifdef SHADED_CUBE
    76     glColor3fv(color[0]);
   113     ctx.glColor3fv(color[0]);
    77     glVertex3fv(cube[0]);
   114     ctx.glVertex3fv(cube[0]);
    78     glColor3fv(color[1]);
   115     ctx.glColor3fv(color[1]);
    79     glVertex3fv(cube[1]);
   116     ctx.glVertex3fv(cube[1]);
    80     glColor3fv(color[2]);
   117     ctx.glColor3fv(color[2]);
    81     glVertex3fv(cube[2]);
   118     ctx.glVertex3fv(cube[2]);
    82     glColor3fv(color[3]);
   119     ctx.glColor3fv(color[3]);
    83     glVertex3fv(cube[3]);
   120     ctx.glVertex3fv(cube[3]);
    84 
   121 
    85     glColor3fv(color[3]);
   122     ctx.glColor3fv(color[3]);
    86     glVertex3fv(cube[3]);
   123     ctx.glVertex3fv(cube[3]);
    87     glColor3fv(color[4]);
   124     ctx.glColor3fv(color[4]);
    88     glVertex3fv(cube[4]);
   125     ctx.glVertex3fv(cube[4]);
    89     glColor3fv(color[7]);
   126     ctx.glColor3fv(color[7]);
    90     glVertex3fv(cube[7]);
   127     ctx.glVertex3fv(cube[7]);
    91     glColor3fv(color[2]);
   128     ctx.glColor3fv(color[2]);
    92     glVertex3fv(cube[2]);
   129     ctx.glVertex3fv(cube[2]);
    93 
   130 
    94     glColor3fv(color[0]);
   131     ctx.glColor3fv(color[0]);
    95     glVertex3fv(cube[0]);
   132     ctx.glVertex3fv(cube[0]);
    96     glColor3fv(color[5]);
   133     ctx.glColor3fv(color[5]);
    97     glVertex3fv(cube[5]);
   134     ctx.glVertex3fv(cube[5]);
    98     glColor3fv(color[6]);
   135     ctx.glColor3fv(color[6]);
    99     glVertex3fv(cube[6]);
   136     ctx.glVertex3fv(cube[6]);
   100     glColor3fv(color[1]);
   137     ctx.glColor3fv(color[1]);
   101     glVertex3fv(cube[1]);
   138     ctx.glVertex3fv(cube[1]);
   102 
   139 
   103     glColor3fv(color[5]);
   140     ctx.glColor3fv(color[5]);
   104     glVertex3fv(cube[5]);
   141     ctx.glVertex3fv(cube[5]);
   105     glColor3fv(color[4]);
   142     ctx.glColor3fv(color[4]);
   106     glVertex3fv(cube[4]);
   143     ctx.glVertex3fv(cube[4]);
   107     glColor3fv(color[7]);
   144     ctx.glColor3fv(color[7]);
   108     glVertex3fv(cube[7]);
   145     ctx.glVertex3fv(cube[7]);
   109     glColor3fv(color[6]);
   146     ctx.glColor3fv(color[6]);
   110     glVertex3fv(cube[6]);
   147     ctx.glVertex3fv(cube[6]);
   111 
   148 
   112     glColor3fv(color[5]);
   149     ctx.glColor3fv(color[5]);
   113     glVertex3fv(cube[5]);
   150     ctx.glVertex3fv(cube[5]);
   114     glColor3fv(color[0]);
   151     ctx.glColor3fv(color[0]);
   115     glVertex3fv(cube[0]);
   152     ctx.glVertex3fv(cube[0]);
   116     glColor3fv(color[3]);
   153     ctx.glColor3fv(color[3]);
   117     glVertex3fv(cube[3]);
   154     ctx.glVertex3fv(cube[3]);
   118     glColor3fv(color[4]);
   155     ctx.glColor3fv(color[4]);
   119     glVertex3fv(cube[4]);
   156     ctx.glVertex3fv(cube[4]);
   120 
   157 
   121     glColor3fv(color[6]);
   158     ctx.glColor3fv(color[6]);
   122     glVertex3fv(cube[6]);
   159     ctx.glVertex3fv(cube[6]);
   123     glColor3fv(color[1]);
   160     ctx.glColor3fv(color[1]);
   124     glVertex3fv(cube[1]);
   161     ctx.glVertex3fv(cube[1]);
   125     glColor3fv(color[2]);
   162     ctx.glColor3fv(color[2]);
   126     glVertex3fv(cube[2]);
   163     ctx.glVertex3fv(cube[2]);
   127     glColor3fv(color[7]);
   164     ctx.glColor3fv(color[7]);
   128     glVertex3fv(cube[7]);
   165     ctx.glVertex3fv(cube[7]);
   129 #else /* flat cube */
   166 #else /* flat cube */
   130     glColor3f(1.0, 0.0, 0.0);
   167     ctx.glColor3f(1.0, 0.0, 0.0);
   131     glVertex3fv(cube[0]);
   168     ctx.glVertex3fv(cube[0]);
   132     glVertex3fv(cube[1]);
   169     ctx.glVertex3fv(cube[1]);
   133     glVertex3fv(cube[2]);
   170     ctx.glVertex3fv(cube[2]);
   134     glVertex3fv(cube[3]);
   171     ctx.glVertex3fv(cube[3]);
   135 
   172 
   136     glColor3f(0.0, 1.0, 0.0);
   173     ctx.glColor3f(0.0, 1.0, 0.0);
   137     glVertex3fv(cube[3]);
   174     ctx.glVertex3fv(cube[3]);
   138     glVertex3fv(cube[4]);
   175     ctx.glVertex3fv(cube[4]);
   139     glVertex3fv(cube[7]);
   176     ctx.glVertex3fv(cube[7]);
   140     glVertex3fv(cube[2]);
   177     ctx.glVertex3fv(cube[2]);
   141 
   178 
   142     glColor3f(0.0, 0.0, 1.0);
   179     ctx.glColor3f(0.0, 0.0, 1.0);
   143     glVertex3fv(cube[0]);
   180     ctx.glVertex3fv(cube[0]);
   144     glVertex3fv(cube[5]);
   181     ctx.glVertex3fv(cube[5]);
   145     glVertex3fv(cube[6]);
   182     ctx.glVertex3fv(cube[6]);
   146     glVertex3fv(cube[1]);
   183     ctx.glVertex3fv(cube[1]);
   147 
   184 
   148     glColor3f(0.0, 1.0, 1.0);
   185     ctx.glColor3f(0.0, 1.0, 1.0);
   149     glVertex3fv(cube[5]);
   186     ctx.glVertex3fv(cube[5]);
   150     glVertex3fv(cube[4]);
   187     ctx.glVertex3fv(cube[4]);
   151     glVertex3fv(cube[7]);
   188     ctx.glVertex3fv(cube[7]);
   152     glVertex3fv(cube[6]);
   189     ctx.glVertex3fv(cube[6]);
   153 
   190 
   154     glColor3f(1.0, 1.0, 0.0);
   191     ctx.glColor3f(1.0, 1.0, 0.0);
   155     glVertex3fv(cube[5]);
   192     ctx.glVertex3fv(cube[5]);
   156     glVertex3fv(cube[0]);
   193     ctx.glVertex3fv(cube[0]);
   157     glVertex3fv(cube[3]);
   194     ctx.glVertex3fv(cube[3]);
   158     glVertex3fv(cube[4]);
   195     ctx.glVertex3fv(cube[4]);
   159 
   196 
   160     glColor3f(1.0, 0.0, 1.0);
   197     ctx.glColor3f(1.0, 0.0, 1.0);
   161     glVertex3fv(cube[6]);
   198     ctx.glVertex3fv(cube[6]);
   162     glVertex3fv(cube[1]);
   199     ctx.glVertex3fv(cube[1]);
   163     glVertex3fv(cube[2]);
   200     ctx.glVertex3fv(cube[2]);
   164     glVertex3fv(cube[7]);
   201     ctx.glVertex3fv(cube[7]);
   165 #endif /* SHADED_CUBE */
   202 #endif /* SHADED_CUBE */
   166 
   203 
   167     glEnd();
   204     ctx.glEnd();
   168 
   205 
   169     glMatrixMode(GL_MODELVIEW);
   206     ctx.glMatrixMode(GL_MODELVIEW);
   170     glRotatef(5.0, 1.0, 1.0, 1.0);
   207     ctx.glRotatef(5.0, 1.0, 1.0, 1.0);
   171 }
   208 }
   172 
   209 
   173 int
   210 int
   174 main(int argc, char *argv[])
   211 main(int argc, char *argv[])
   175 {
   212 {
   240     context = SDL_GL_CreateContext(state->windows[0]);
   277     context = SDL_GL_CreateContext(state->windows[0]);
   241     if (!context) {
   278     if (!context) {
   242         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_GL_CreateContext(): %s\n", SDL_GetError());
   279         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_GL_CreateContext(): %s\n", SDL_GetError());
   243         quit(2);
   280         quit(2);
   244     }
   281     }
       
   282     
       
   283     /* Important: call this *after* creating the context */
       
   284     if (LoadContext(&ctx) < 0) {
       
   285         SDL_Log("Could not load GL functions\n");
       
   286         quit(2);
       
   287         return 0;
       
   288     }
   245 
   289 
   246     if (state->render_flags & SDL_RENDERER_PRESENTVSYNC) {
   290     if (state->render_flags & SDL_RENDERER_PRESENTVSYNC) {
   247         /* try late-swap-tearing first. If not supported, try normal vsync. */
   291         /* try late-swap-tearing first. If not supported, try normal vsync. */
   248         if (SDL_GL_SetSwapInterval(-1) == -1) {
   292         if (SDL_GL_SetSwapInterval(-1) == -1) {
   249             SDL_GL_SetSwapInterval(1);
   293             SDL_GL_SetSwapInterval(1);
   258     SDL_GetWindowSize(state->windows[0], &dw, &dh);
   302     SDL_GetWindowSize(state->windows[0], &dw, &dh);
   259     SDL_Log("Window Size   : %d,%d\n", dw, dh);
   303     SDL_Log("Window Size   : %d,%d\n", dw, dh);
   260     SDL_GL_GetDrawableSize(state->windows[0], &dw, &dh);
   304     SDL_GL_GetDrawableSize(state->windows[0], &dw, &dh);
   261     SDL_Log("Draw Size     : %d,%d\n", dw, dh);
   305     SDL_Log("Draw Size     : %d,%d\n", dw, dh);
   262     SDL_Log("\n");
   306     SDL_Log("\n");
   263     SDL_Log("Vendor        : %s\n", glGetString(GL_VENDOR));
   307     SDL_Log("Vendor        : %s\n", ctx.glGetString(GL_VENDOR));
   264     SDL_Log("Renderer      : %s\n", glGetString(GL_RENDERER));
   308     SDL_Log("Renderer      : %s\n", ctx.glGetString(GL_RENDERER));
   265     SDL_Log("Version       : %s\n", glGetString(GL_VERSION));
   309     SDL_Log("Version       : %s\n", ctx.glGetString(GL_VERSION));
   266     SDL_Log("Extensions    : %s\n", glGetString(GL_EXTENSIONS));
   310     SDL_Log("Extensions    : %s\n", ctx.glGetString(GL_EXTENSIONS));
   267     SDL_Log("\n");
   311     SDL_Log("\n");
   268 
   312 
   269     status = SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
   313     status = SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
   270     if (!status) {
   314     if (!status) {
   271         SDL_Log("SDL_GL_RED_SIZE: requested %d, got %d\n", 5, value);
   315         SDL_Log("SDL_GL_RED_SIZE: requested %d, got %d\n", 5, value);
   317                    SDL_GetError());
   361                    SDL_GetError());
   318         }
   362         }
   319     }
   363     }
   320 
   364 
   321     /* Set rendering settings */
   365     /* Set rendering settings */
   322     glMatrixMode(GL_PROJECTION);
   366     ctx.glMatrixMode(GL_PROJECTION);
   323     glLoadIdentity();
   367     ctx.glLoadIdentity();
   324     glOrtho(-2.0, 2.0, -2.0, 2.0, -20.0, 20.0);
   368     ctx.glOrtho(-2.0, 2.0, -2.0, 2.0, -20.0, 20.0);
   325     glMatrixMode(GL_MODELVIEW);
   369     ctx.glMatrixMode(GL_MODELVIEW);
   326     glLoadIdentity();
   370     ctx.glLoadIdentity();
   327     glEnable(GL_DEPTH_TEST);
   371     ctx.glEnable(GL_DEPTH_TEST);
   328     glDepthFunc(GL_LESS);
   372     ctx.glDepthFunc(GL_LESS);
   329     glShadeModel(GL_SMOOTH);
   373     ctx.glShadeModel(GL_SMOOTH);
   330     
   374     
   331     /* Main render loop */
   375     /* Main render loop */
   332     frames = 0;
   376     frames = 0;
   333     then = SDL_GetTicks();
   377     then = SDL_GetTicks();
   334     done = 0;
   378     done = 0;
   342             int w, h;
   386             int w, h;
   343             if (state->windows[i] == NULL)
   387             if (state->windows[i] == NULL)
   344                 continue;
   388                 continue;
   345             SDL_GL_MakeCurrent(state->windows[i], context);
   389             SDL_GL_MakeCurrent(state->windows[i], context);
   346             SDL_GL_GetDrawableSize(state->windows[i], &w, &h);
   390             SDL_GL_GetDrawableSize(state->windows[i], &w, &h);
   347             glViewport(0, 0, w, h);
   391             ctx.glViewport(0, 0, w, h);
   348             Render();
   392             Render();
   349             SDL_GL_SwapWindow(state->windows[i]);
   393             SDL_GL_SwapWindow(state->windows[i]);
   350         }
   394         }
   351     }
   395     }
   352 
   396