test/testshader.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 08 Apr 2011 13:03:26 -0700
changeset 5535 96594ac5fd1a
parent 5236 c7be6ca3a0b8
child 5641 7b94c53df15c
permissions -rw-r--r--
SDL 1.3 is now under the zlib license.
     1 /*
     2   Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     3 
     4   This software is provided 'as-is', without any express or implied
     5   warranty.  In no event will the authors be held liable for any damages
     6   arising from the use of this software.
     7 
     8   Permission is granted to anyone to use this software for any purpose,
     9   including commercial applications, and to alter it and redistribute it
    10   freely.
    11 */
    12 /* This is a simple example of using GLSL shaders with SDL */
    13 
    14 #include "SDL.h"
    15 
    16 #ifdef HAVE_OPENGL
    17 
    18 #include "SDL_opengl.h"
    19 
    20 
    21 static SDL_bool shaders_supported;
    22 static int      current_shader = 0;
    23 
    24 enum {
    25     SHADER_COLOR,
    26     SHADER_TEXTURE,
    27     SHADER_TEXCOORDS,
    28     NUM_SHADERS
    29 };
    30 
    31 typedef struct {
    32     GLuint program;
    33     GLuint vert_shader;
    34     GLuint frag_shader;
    35     const char *vert_source;
    36     const char *frag_source;
    37 } ShaderData;
    38 
    39 static ShaderData shaders[NUM_SHADERS] = {
    40 
    41     /* SHADER_COLOR */
    42     { 0, 0, 0,
    43         /* vertex shader */
    44 "varying vec4 v_color;\n"
    45 "\n"
    46 "void main()\n"
    47 "{\n"
    48 "    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
    49 "    v_color = gl_Color;\n"
    50 "}",
    51         /* fragment shader */
    52 "varying vec4 v_color;\n"
    53 "\n"
    54 "void main()\n"
    55 "{\n"
    56 "    gl_FragColor = v_color;\n"
    57 "}"
    58     },
    59 
    60     /* SHADER_TEXTURE */
    61     { 0, 0, 0,
    62         /* vertex shader */
    63 "varying vec4 v_color;\n"
    64 "varying vec2 v_texCoord;\n"
    65 "\n"
    66 "void main()\n"
    67 "{\n"
    68 "    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
    69 "    v_color = gl_Color;\n"
    70 "    v_texCoord = vec2(gl_MultiTexCoord0);\n"
    71 "}",
    72         /* fragment shader */
    73 "varying vec4 v_color;\n"
    74 "varying vec2 v_texCoord;\n"
    75 "uniform sampler2D tex0;\n"
    76 "\n"
    77 "void main()\n"
    78 "{\n"
    79 "    gl_FragColor = texture2D(tex0, v_texCoord) * v_color;\n"
    80 "}"
    81     },
    82 
    83     /* SHADER_TEXCOORDS */
    84     { 0, 0, 0,
    85         /* vertex shader */
    86 "varying vec2 v_texCoord;\n"
    87 "\n"
    88 "void main()\n"
    89 "{\n"
    90 "    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
    91 "    v_texCoord = vec2(gl_MultiTexCoord0);\n"
    92 "}",
    93         /* fragment shader */
    94 "varying vec2 v_texCoord;\n"
    95 "\n"
    96 "void main()\n"
    97 "{\n"
    98 "    vec4 color;\n"
    99 "    vec2 delta;\n"
   100 "    float dist;\n"
   101 "\n"
   102 "    delta = vec2(0.5, 0.5) - v_texCoord;\n"
   103 "    dist = dot(delta, delta);\n"
   104 "\n"
   105 "    color.r = v_texCoord.x;\n"
   106 "    color.g = v_texCoord.x * v_texCoord.y;\n"
   107 "    color.b = v_texCoord.y;\n"
   108 "    color.a = 1.0 - (dist * 4.0);\n"
   109 "    gl_FragColor = color;\n"
   110 "}"
   111     },
   112 };
   113     
   114 static PFNGLATTACHOBJECTARBPROC glAttachObjectARB;
   115 static PFNGLCOMPILESHADERARBPROC glCompileShaderARB;
   116 static PFNGLCREATEPROGRAMOBJECTARBPROC glCreateProgramObjectARB;
   117 static PFNGLCREATESHADEROBJECTARBPROC glCreateShaderObjectARB;
   118 static PFNGLDELETEOBJECTARBPROC glDeleteObjectARB;
   119 static PFNGLGETINFOLOGARBPROC glGetInfoLogARB;
   120 static PFNGLGETOBJECTPARAMETERIVARBPROC glGetObjectParameterivARB;
   121 static PFNGLGETUNIFORMLOCATIONARBPROC glGetUniformLocationARB;
   122 static PFNGLLINKPROGRAMARBPROC glLinkProgramARB;
   123 static PFNGLSHADERSOURCEARBPROC glShaderSourceARB;
   124 static PFNGLUNIFORM1IARBPROC glUniform1iARB;
   125 static PFNGLUSEPROGRAMOBJECTARBPROC glUseProgramObjectARB;
   126 
   127 static SDL_bool CompileShader(GLenum shader, const char *source)
   128 {
   129     GLint status;
   130 
   131     glShaderSourceARB(shader, 1, &source, NULL);
   132     glCompileShaderARB(shader);
   133     glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &status);
   134     if (status == 0) {
   135         GLint length;
   136         char *info;
   137 
   138         glGetObjectParameterivARB(shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &length);
   139         info = SDL_stack_alloc(char, length+1);
   140         glGetInfoLogARB(shader, length, NULL, info);
   141         fprintf(stderr, "Failed to compile shader:\n%s\n%s", source, info);
   142         SDL_stack_free(info);
   143 
   144         return SDL_FALSE;
   145     } else {
   146         return SDL_TRUE;
   147     }
   148 }
   149 
   150 static SDL_bool CompileShaderProgram(ShaderData *data)
   151 {
   152     const int num_tmus_bound = 4;
   153     int i;
   154     GLint location;
   155 
   156     glGetError();
   157 
   158     /* Create one program object to rule them all */
   159     data->program = glCreateProgramObjectARB();
   160 
   161     /* Create the vertex shader */
   162     data->vert_shader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
   163     if (!CompileShader(data->vert_shader, data->vert_source)) {
   164         return SDL_FALSE;
   165     }
   166 
   167     /* Create the fragment shader */
   168     data->frag_shader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
   169     if (!CompileShader(data->frag_shader, data->frag_source)) {
   170         return SDL_FALSE;
   171     }
   172 
   173     /* ... and in the darkness bind them */
   174     glAttachObjectARB(data->program, data->vert_shader);
   175     glAttachObjectARB(data->program, data->frag_shader);
   176     glLinkProgramARB(data->program);
   177 
   178     /* Set up some uniform variables */
   179     glUseProgramObjectARB(data->program);
   180     for (i = 0; i < num_tmus_bound; ++i) {
   181         char tex_name[5];
   182         SDL_snprintf(tex_name, SDL_arraysize(tex_name), "tex%d", i);
   183         location = glGetUniformLocationARB(data->program, tex_name);
   184         if (location >= 0) {
   185             glUniform1iARB(location, i);
   186         }
   187     }
   188     glUseProgramObjectARB(0);
   189  
   190     return (glGetError() == GL_NO_ERROR);
   191 }
   192 
   193 static void DestroyShaderProgram(ShaderData *data)
   194 {
   195     if (shaders_supported) {
   196         glDeleteObjectARB(data->vert_shader);
   197         glDeleteObjectARB(data->frag_shader);
   198         glDeleteObjectARB(data->program);
   199     }
   200 }
   201 
   202 static SDL_bool InitShaders()
   203 {
   204     int i;
   205 
   206     /* Check for shader support */
   207     shaders_supported = SDL_FALSE;
   208     if (SDL_GL_ExtensionSupported("GL_ARB_shader_objects") &&
   209         SDL_GL_ExtensionSupported("GL_ARB_shading_language_100") &&
   210         SDL_GL_ExtensionSupported("GL_ARB_vertex_shader") &&
   211         SDL_GL_ExtensionSupported("GL_ARB_fragment_shader")) {
   212         glAttachObjectARB = (PFNGLATTACHOBJECTARBPROC) SDL_GL_GetProcAddress("glAttachObjectARB");
   213         glCompileShaderARB = (PFNGLCOMPILESHADERARBPROC) SDL_GL_GetProcAddress("glCompileShaderARB");
   214         glCreateProgramObjectARB = (PFNGLCREATEPROGRAMOBJECTARBPROC) SDL_GL_GetProcAddress("glCreateProgramObjectARB");
   215         glCreateShaderObjectARB = (PFNGLCREATESHADEROBJECTARBPROC) SDL_GL_GetProcAddress("glCreateShaderObjectARB");
   216         glDeleteObjectARB = (PFNGLDELETEOBJECTARBPROC) SDL_GL_GetProcAddress("glDeleteObjectARB");
   217         glGetInfoLogARB = (PFNGLGETINFOLOGARBPROC) SDL_GL_GetProcAddress("glGetInfoLogARB");
   218         glGetObjectParameterivARB = (PFNGLGETOBJECTPARAMETERIVARBPROC) SDL_GL_GetProcAddress("glGetObjectParameterivARB");
   219         glGetUniformLocationARB = (PFNGLGETUNIFORMLOCATIONARBPROC) SDL_GL_GetProcAddress("glGetUniformLocationARB");
   220         glLinkProgramARB = (PFNGLLINKPROGRAMARBPROC) SDL_GL_GetProcAddress("glLinkProgramARB");
   221         glShaderSourceARB = (PFNGLSHADERSOURCEARBPROC) SDL_GL_GetProcAddress("glShaderSourceARB");
   222         glUniform1iARB = (PFNGLUNIFORM1IARBPROC) SDL_GL_GetProcAddress("glUniform1iARB");
   223         glUseProgramObjectARB = (PFNGLUSEPROGRAMOBJECTARBPROC) SDL_GL_GetProcAddress("glUseProgramObjectARB");
   224         if (glAttachObjectARB &&
   225             glCompileShaderARB &&
   226             glCreateProgramObjectARB &&
   227             glCreateShaderObjectARB &&
   228             glDeleteObjectARB &&
   229             glGetInfoLogARB &&
   230             glGetObjectParameterivARB &&
   231             glGetUniformLocationARB &&
   232             glLinkProgramARB &&
   233             glShaderSourceARB &&
   234             glUniform1iARB &&
   235             glUseProgramObjectARB) {
   236             shaders_supported = SDL_TRUE;
   237         }
   238     }
   239 
   240     if (!shaders_supported) {
   241         return SDL_FALSE;
   242     }
   243 
   244     /* Compile all the shaders */
   245     for (i = 0; i < NUM_SHADERS; ++i) {
   246         if (!CompileShaderProgram(&shaders[i])) {
   247             fprintf(stderr, "Unable to compile shader!\n");
   248             return SDL_FALSE;
   249         }
   250     }
   251 
   252     /* We're done! */
   253     return SDL_TRUE;
   254 }
   255 
   256 static void QuitShaders()
   257 {
   258     int i;
   259 
   260     for (i = 0; i < NUM_SHADERS; ++i) {
   261         DestroyShaderProgram(&shaders[i]);
   262     }
   263 }
   264 
   265 /* Quick utility function for texture creation */
   266 static int
   267 power_of_two(int input)
   268 {
   269     int value = 1;
   270 
   271     while (value < input) {
   272         value <<= 1;
   273     }
   274     return value;
   275 }
   276 
   277 GLuint
   278 SDL_GL_LoadTexture(SDL_Surface * surface, GLfloat * texcoord)
   279 {
   280     GLuint texture;
   281     int w, h;
   282     SDL_Surface *image;
   283     SDL_Rect area;
   284     Uint32 saved_flags;
   285     Uint8 saved_alpha;
   286 
   287     /* Use the surface width and height expanded to powers of 2 */
   288     w = power_of_two(surface->w);
   289     h = power_of_two(surface->h);
   290     texcoord[0] = 0.0f;         /* Min X */
   291     texcoord[1] = 0.0f;         /* Min Y */
   292     texcoord[2] = (GLfloat) surface->w / w;     /* Max X */
   293     texcoord[3] = (GLfloat) surface->h / h;     /* Max Y */
   294 
   295     image = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32,
   296 #if SDL_BYTEORDER == SDL_LIL_ENDIAN     /* OpenGL RGBA masks */
   297                                  0x000000FF,
   298                                  0x0000FF00, 0x00FF0000, 0xFF000000
   299 #else
   300                                  0xFF000000,
   301                                  0x00FF0000, 0x0000FF00, 0x000000FF
   302 #endif
   303         );
   304     if (image == NULL) {
   305         return 0;
   306     }
   307 
   308     /* Save the alpha blending attributes */
   309     saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
   310     SDL_GetSurfaceAlphaMod(surface, &saved_alpha);
   311     if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
   312         SDL_SetAlpha(surface, 0, 0);
   313     }
   314 
   315     /* Copy the surface into the GL texture image */
   316     area.x = 0;
   317     area.y = 0;
   318     area.w = surface->w;
   319     area.h = surface->h;
   320     SDL_BlitSurface(surface, &area, image, &area);
   321 
   322     /* Restore the alpha blending attributes */
   323     if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
   324         SDL_SetAlpha(surface, saved_flags, saved_alpha);
   325     }
   326 
   327     /* Create an OpenGL texture for the image */
   328     glGenTextures(1, &texture);
   329     glBindTexture(GL_TEXTURE_2D, texture);
   330     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
   331     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
   332     glTexImage2D(GL_TEXTURE_2D,
   333                  0,
   334                  GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
   335     SDL_FreeSurface(image);     /* No longer needed */
   336 
   337     return texture;
   338 }
   339 
   340 /* A general OpenGL initialization function.    Sets all of the initial parameters. */
   341 void InitGL(int Width, int Height)                    // We call this right after our OpenGL window is created.
   342 {
   343     GLdouble aspect;
   344 
   345     glViewport(0, 0, Width, Height);
   346     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);        // This Will Clear The Background Color To Black
   347     glClearDepth(1.0);                // Enables Clearing Of The Depth Buffer
   348     glDepthFunc(GL_LESS);                // The Type Of Depth Test To Do
   349     glEnable(GL_DEPTH_TEST);            // Enables Depth Testing
   350     glShadeModel(GL_SMOOTH);            // Enables Smooth Color Shading
   351 
   352     glMatrixMode(GL_PROJECTION);
   353     glLoadIdentity();                // Reset The Projection Matrix
   354 
   355     aspect = (GLdouble)Width / Height;
   356     glOrtho(-3.0, 3.0, -3.0 / aspect, 3.0 / aspect, 0.0, 1.0);
   357 
   358     glMatrixMode(GL_MODELVIEW);
   359 }
   360 
   361 /* The main drawing function. */
   362 void DrawGLScene(GLuint texture, GLfloat * texcoord)
   363 {
   364     /* Texture coordinate lookup, to make it simple */
   365     enum {
   366         MINX,
   367         MINY,
   368         MAXX,
   369         MAXY
   370     };
   371 
   372     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);        // Clear The Screen And The Depth Buffer
   373     glLoadIdentity();                // Reset The View
   374 
   375     glTranslatef(-1.5f,0.0f,0.0f);        // Move Left 1.5 Units
   376     
   377     // draw a triangle (in smooth coloring mode)
   378     glBegin(GL_POLYGON);                // start drawing a polygon
   379     glColor3f(1.0f,0.0f,0.0f);            // Set The Color To Red
   380     glVertex3f( 0.0f, 1.0f, 0.0f);        // Top
   381     glColor3f(0.0f,1.0f,0.0f);            // Set The Color To Green
   382     glVertex3f( 1.0f,-1.0f, 0.0f);        // Bottom Right
   383     glColor3f(0.0f,0.0f,1.0f);            // Set The Color To Blue
   384     glVertex3f(-1.0f,-1.0f, 0.0f);        // Bottom Left    
   385     glEnd();                    // we're done with the polygon (smooth color interpolation)    
   386 
   387     glTranslatef(3.0f,0.0f,0.0f);         // Move Right 3 Units
   388 
   389     // Enable blending
   390     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   391     glEnable(GL_BLEND);
   392     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   393 
   394     // draw a textured square (quadrilateral)
   395     glEnable(GL_TEXTURE_2D);
   396     glBindTexture(GL_TEXTURE_2D, texture);
   397     glColor3f(1.0f,1.0f,1.0f);
   398     if (shaders_supported) {
   399         glUseProgramObjectARB(shaders[current_shader].program);
   400     }
   401 
   402     glBegin(GL_QUADS);                // start drawing a polygon (4 sided)
   403     glTexCoord2f(texcoord[MINX], texcoord[MINY]);
   404     glVertex3f(-1.0f, 1.0f, 0.0f);        // Top Left
   405     glTexCoord2f(texcoord[MAXX], texcoord[MINY]);
   406     glVertex3f( 1.0f, 1.0f, 0.0f);        // Top Right
   407     glTexCoord2f(texcoord[MAXX], texcoord[MAXY]);
   408     glVertex3f( 1.0f,-1.0f, 0.0f);        // Bottom Right
   409     glTexCoord2f(texcoord[MINX], texcoord[MAXY]);
   410     glVertex3f(-1.0f,-1.0f, 0.0f);        // Bottom Left    
   411     glEnd();                    // done with the polygon
   412 
   413     if (shaders_supported) {
   414         glUseProgramObjectARB(0);
   415     }
   416     glDisable(GL_TEXTURE_2D);
   417 
   418     // swap buffers to display, since we're double buffered.
   419     SDL_GL_SwapBuffers();
   420 }
   421 
   422 int main(int argc, char **argv) 
   423 {    
   424     int done;
   425     SDL_Surface *surface;
   426     GLuint texture;
   427     GLfloat texcoords[4];
   428 
   429     /* Initialize SDL for video output */
   430     if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
   431         fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError());
   432         exit(1);
   433     }
   434 
   435     /* Create a 640x480 OpenGL screen */
   436     if ( SDL_SetVideoMode(640, 480, 0, SDL_OPENGL) == NULL ) {
   437         fprintf(stderr, "Unable to create OpenGL screen: %s\n", SDL_GetError());
   438         SDL_Quit();
   439         exit(2);
   440     }
   441 
   442     /* Set the title bar in environments that support it */
   443     SDL_WM_SetCaption("Shader Demo", NULL);
   444 
   445     surface = SDL_LoadBMP("icon.bmp");
   446     if ( ! surface ) {
   447         fprintf(stderr, "Unable to load icon.bmp: %s\n", SDL_GetError());
   448         SDL_Quit();
   449         exit(3);
   450     }
   451     texture = SDL_GL_LoadTexture(surface, texcoords);
   452     SDL_FreeSurface(surface);
   453 
   454     /* Loop, drawing and checking events */
   455     InitGL(640, 480);
   456     if (InitShaders()) {
   457         printf("Shaders supported, press SPACE to cycle them.\n");
   458     } else {
   459         printf("Shaders not supported!\n");
   460     }
   461     done = 0;
   462     while ( ! done ) {
   463         DrawGLScene(texture, texcoords);
   464 
   465         /* This could go in a separate function */
   466         { SDL_Event event;
   467             while ( SDL_PollEvent(&event) ) {
   468                 if ( event.type == SDL_QUIT ) {
   469                     done = 1;
   470                 }
   471                 if ( event.type == SDL_KEYDOWN ) {
   472                     if ( event.key.keysym.sym == SDLK_SPACE ) {
   473                         current_shader = (current_shader + 1) % NUM_SHADERS;
   474                     }
   475                     if ( event.key.keysym.sym == SDLK_ESCAPE ) {
   476                         done = 1;
   477                     }
   478                 }
   479             }
   480         }
   481     }
   482     QuitShaders();
   483     SDL_Quit();
   484     return 1;
   485 }
   486 
   487 #else /* HAVE_OPENGL */
   488 
   489 int
   490 main(int argc, char *argv[])
   491 {
   492     printf("No OpenGL support on this system\n");
   493     return 1;
   494 }
   495 
   496 #endif /* HAVE_OPENGL */
   497 
   498 /* vi: set ts=4 sw=4 expandtab: */