Added a simple GLSL example using SDL
authorSam Lantinga <slouken@libsdl.org>
Tue, 08 Feb 2011 22:11:16 -0800
changeset 523274bc160186a8
parent 5231 e82908c86c9c
child 5233 ce4f91138031
Added a simple GLSL example using SDL
test/Makefile.in
test/testshader.c
     1.1 --- a/test/Makefile.in	Tue Feb 08 20:31:39 2011 -0800
     1.2 +++ b/test/Makefile.in	Tue Feb 08 22:11:16 2011 -0800
     1.3 @@ -7,7 +7,56 @@
     1.4  CFLAGS  = @CFLAGS@
     1.5  LIBS	= @LIBS@
     1.6  
     1.7 -TARGETS = checkkeys$(EXE) graywin$(EXE) loopwave$(EXE) testalpha$(EXE) testatomic$(EXE) testaudioinfo$(EXE) testbitmap$(EXE) testblitspeed$(EXE) testcursor$(EXE) testdraw2$(EXE) testdyngles$(EXE) testdyngl$(EXE) testerror$(EXE) testfile$(EXE) testfill$(EXE) testgamma$(EXE) testgl2$(EXE) testgles$(EXE) testgl$(EXE) testhaptic$(EXE) testhread$(EXE) testiconv$(EXE) testime$(EXE) testintersections$(EXE) testjoystick$(EXE) testkeys$(EXE) testloadso$(EXE) testlock$(EXE) testmultiaudio$(EXE) testoverlay2$(EXE) testoverlay$(EXE) testplatform$(EXE) testpower$(EXE) testresample$(EXE) testsem$(EXE) testshape$(EXE) testsprite2$(EXE) testsprite$(EXE) testspriteminimal$(EXE) testtimer$(EXE) testver$(EXE) testvidinfo$(EXE) testwin$(EXE) testwm2$(EXE) testwm$(EXE) threadwin$(EXE) torturethread$(EXE) testgesture$(EXE)
     1.8 +TARGETS = \
     1.9 +	checkkeys$(EXE) \
    1.10 +	graywin$(EXE) \
    1.11 +	loopwave$(EXE) \
    1.12 +	testalpha$(EXE) \
    1.13 +	testatomic$(EXE) \
    1.14 +	testaudioinfo$(EXE) \
    1.15 +	testbitmap$(EXE) \
    1.16 +	testblitspeed$(EXE) \
    1.17 +	testcursor$(EXE) \
    1.18 +	testdraw2$(EXE) \
    1.19 +	testdyngl$(EXE) \
    1.20 +	testdyngles$(EXE) \
    1.21 +	testerror$(EXE) \
    1.22 +	testfile$(EXE) \
    1.23 +	testfill$(EXE) \
    1.24 +	testgamma$(EXE) \
    1.25 +	testgesture$(EXE) \
    1.26 +	testgl$(EXE) \
    1.27 +	testgl2$(EXE) \
    1.28 +	testgles$(EXE) \
    1.29 +	testhaptic$(EXE) \
    1.30 +	testhread$(EXE) \
    1.31 +	testiconv$(EXE) \
    1.32 +	testime$(EXE) \
    1.33 +	testintersections$(EXE) \
    1.34 +	testjoystick$(EXE) \
    1.35 +	testkeys$(EXE) \
    1.36 +	testloadso$(EXE) \
    1.37 +	testlock$(EXE) \
    1.38 +	testmultiaudio$(EXE) \
    1.39 +	testoverlay$(EXE) \
    1.40 +	testoverlay2$(EXE) \
    1.41 +	testplatform$(EXE) \
    1.42 +	testpower$(EXE) \
    1.43 +	testresample$(EXE) \
    1.44 +	testsem$(EXE) \
    1.45 +	testshader$(EXE) \
    1.46 +	testshape$(EXE) \
    1.47 +	testsprite$(EXE) \
    1.48 +	testsprite2$(EXE) \
    1.49 +	testspriteminimal$(EXE) \
    1.50 +	testtimer$(EXE) \
    1.51 +	testver$(EXE) \
    1.52 +	testvidinfo$(EXE) \
    1.53 +	testwin$(EXE) \
    1.54 +	testwm$(EXE) \
    1.55 +	testwm2$(EXE) \
    1.56 +	threadwin$(EXE) \
    1.57 +	torturethread$(EXE) \
    1.58  
    1.59  all: Makefile $(TARGETS)
    1.60  
    1.61 @@ -107,6 +156,9 @@
    1.62  testsem$(EXE): $(srcdir)/testsem.c
    1.63  	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    1.64  
    1.65 +testshader$(EXE): $(srcdir)/testshader.c
    1.66 +	$(CC) -o $@ $(srcdir)/testshader.c $(CFLAGS) $(LIBS) @GLLIB@ @MATHLIB@
    1.67 +
    1.68  testshape$(EXE): $(srcdir)/testshape.c
    1.69  	$(CC) -o $@ $? -std=c99 $(CFLAGS) $(LIBS)
    1.70  
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/testshader.c	Tue Feb 08 22:11:16 2011 -0800
     2.3 @@ -0,0 +1,483 @@
     2.4 +/* This is a simple example of using GLSL shaders with SDL */
     2.5 +
     2.6 +#include "SDL.h"
     2.7 +
     2.8 +#ifdef HAVE_OPENGL
     2.9 +
    2.10 +#include "SDL_opengl.h"
    2.11 +
    2.12 +
    2.13 +static SDL_bool shaders_supported;
    2.14 +static int      current_shader = 0;
    2.15 +
    2.16 +enum {
    2.17 +    SHADER_COLOR,
    2.18 +    SHADER_TEXTURE,
    2.19 +    SHADER_TEXCOORDS,
    2.20 +    NUM_SHADERS
    2.21 +};
    2.22 +
    2.23 +typedef struct {
    2.24 +    GLuint program;
    2.25 +    GLuint vert_shader;
    2.26 +    GLuint frag_shader;
    2.27 +    const char *vert_source;
    2.28 +    const char *frag_source;
    2.29 +} ShaderData;
    2.30 +
    2.31 +static ShaderData shaders[NUM_SHADERS] = {
    2.32 +
    2.33 +    /* SHADER_COLOR */
    2.34 +    { 0, 0, 0,
    2.35 +        /* vertex shader */
    2.36 +"varying vec4 v_color;\n"
    2.37 +"\n"
    2.38 +"void main()\n"
    2.39 +"{\n"
    2.40 +"    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
    2.41 +"    v_color = gl_Color;\n"
    2.42 +"}",
    2.43 +        /* fragment shader */
    2.44 +"varying vec4 v_color;\n"
    2.45 +"\n"
    2.46 +"void main()\n"
    2.47 +"{\n"
    2.48 +"    gl_FragColor = v_color;\n"
    2.49 +"}"
    2.50 +    },
    2.51 +
    2.52 +    /* SHADER_TEXTURE */
    2.53 +    { 0, 0, 0,
    2.54 +        /* vertex shader */
    2.55 +"varying vec4 v_color;\n"
    2.56 +"varying vec2 v_texCoord;\n"
    2.57 +"\n"
    2.58 +"void main()\n"
    2.59 +"{\n"
    2.60 +"    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
    2.61 +"    v_color = gl_Color;\n"
    2.62 +"    v_texCoord = vec2(gl_MultiTexCoord0);\n"
    2.63 +"}",
    2.64 +        /* fragment shader */
    2.65 +"varying vec4 v_color;\n"
    2.66 +"varying vec2 v_texCoord;\n"
    2.67 +"uniform sampler2D tex0;\n"
    2.68 +"\n"
    2.69 +"void main()\n"
    2.70 +"{\n"
    2.71 +"    gl_FragColor = texture2D(tex0, v_texCoord) * v_color;\n"
    2.72 +"}"
    2.73 +    },
    2.74 +
    2.75 +    /* SHADER_TEXCOORDS */
    2.76 +    { 0, 0, 0,
    2.77 +        /* vertex shader */
    2.78 +"varying vec2 v_texCoord;\n"
    2.79 +"\n"
    2.80 +"void main()\n"
    2.81 +"{\n"
    2.82 +"    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
    2.83 +"    v_texCoord = vec2(gl_MultiTexCoord0);\n"
    2.84 +"}",
    2.85 +        /* fragment shader */
    2.86 +"varying vec2 v_texCoord;\n"
    2.87 +"\n"
    2.88 +"void main()\n"
    2.89 +"{\n"
    2.90 +"    vec4 color;\n"
    2.91 +"    vec2 delta;\n"
    2.92 +"    float dist;\n"
    2.93 +"\n"
    2.94 +"    delta = vec2(0.5, 0.5) - v_texCoord;\n"
    2.95 +"    dist = dot(delta, delta);\n"
    2.96 +"\n"
    2.97 +"    color.r = v_texCoord.x;\n"
    2.98 +"    color.g = v_texCoord.x * v_texCoord.y;\n"
    2.99 +"    color.b = v_texCoord.y;\n"
   2.100 +"    color.a = 1.0 - (dist * 4.0);\n"
   2.101 +"    gl_FragColor = color;\n"
   2.102 +"}"
   2.103 +    },
   2.104 +};
   2.105 +    
   2.106 +static PFNGLATTACHOBJECTARBPROC glAttachObjectARB;
   2.107 +static PFNGLCOMPILESHADERARBPROC glCompileShaderARB;
   2.108 +static PFNGLCREATEPROGRAMOBJECTARBPROC glCreateProgramObjectARB;
   2.109 +static PFNGLCREATESHADEROBJECTARBPROC glCreateShaderObjectARB;
   2.110 +static PFNGLDELETEOBJECTARBPROC glDeleteObjectARB;
   2.111 +static PFNGLGETINFOLOGARBPROC glGetInfoLogARB;
   2.112 +static PFNGLGETOBJECTPARAMETERIVARBPROC glGetObjectParameterivARB;
   2.113 +static PFNGLGETUNIFORMLOCATIONARBPROC glGetUniformLocationARB;
   2.114 +static PFNGLLINKPROGRAMARBPROC glLinkProgramARB;
   2.115 +static PFNGLSHADERSOURCEARBPROC glShaderSourceARB;
   2.116 +static PFNGLUNIFORM1IARBPROC glUniform1iARB;
   2.117 +static PFNGLUSEPROGRAMOBJECTARBPROC glUseProgramObjectARB;
   2.118 +
   2.119 +static SDL_bool CompileShader(GLenum shader, const char *source)
   2.120 +{
   2.121 +    GLint status;
   2.122 +
   2.123 +    glShaderSourceARB(shader, 1, &source, NULL);
   2.124 +    glCompileShaderARB(shader);
   2.125 +    glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &status);
   2.126 +    if (status == 0) {
   2.127 +        GLint length;
   2.128 +        char *info;
   2.129 +
   2.130 +        glGetObjectParameterivARB(shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &length);
   2.131 +        info = SDL_stack_alloc(char, length+1);
   2.132 +        glGetInfoLogARB(shader, length, NULL, info);
   2.133 +        fprintf(stderr, "Failed to compile shader:\n%s\n%s", source, info);
   2.134 +        SDL_stack_free(info);
   2.135 +
   2.136 +        return SDL_FALSE;
   2.137 +    } else {
   2.138 +        return SDL_TRUE;
   2.139 +    }
   2.140 +}
   2.141 +
   2.142 +static SDL_bool CompileShaderProgram(ShaderData *data)
   2.143 +{
   2.144 +    const int num_tmus_bound = 4;
   2.145 +    int i;
   2.146 +    GLint location;
   2.147 +
   2.148 +    glGetError();
   2.149 +
   2.150 +    /* Create one program object to rule them all */
   2.151 +    data->program = glCreateProgramObjectARB();
   2.152 +
   2.153 +    /* Create the vertex shader */
   2.154 +    data->vert_shader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
   2.155 +    if (!CompileShader(data->vert_shader, data->vert_source)) {
   2.156 +        return SDL_FALSE;
   2.157 +    }
   2.158 +
   2.159 +    /* Create the fragment shader */
   2.160 +    data->frag_shader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
   2.161 +    if (!CompileShader(data->frag_shader, data->frag_source)) {
   2.162 +        return SDL_FALSE;
   2.163 +    }
   2.164 +
   2.165 +    /* ... and in the darkness bind them */
   2.166 +    glAttachObjectARB(data->program, data->vert_shader);
   2.167 +    glAttachObjectARB(data->program, data->frag_shader);
   2.168 +    glLinkProgramARB(data->program);
   2.169 +
   2.170 +    /* Set up some uniform variables */
   2.171 +    glUseProgramObjectARB(data->program);
   2.172 +    for (i = 0; i < num_tmus_bound; ++i) {
   2.173 +        char tex_name[5];
   2.174 +        SDL_snprintf(tex_name, SDL_arraysize(tex_name), "tex%d", i);
   2.175 +        location = glGetUniformLocationARB(data->program, tex_name);
   2.176 +        if (location >= 0) {
   2.177 +            glUniform1iARB(location, i);
   2.178 +        }
   2.179 +    }
   2.180 +    glUseProgramObjectARB(0);
   2.181 + 
   2.182 +    return (glGetError() == GL_NO_ERROR);
   2.183 +}
   2.184 +
   2.185 +static void DestroyShaderProgram(ShaderData *data)
   2.186 +{
   2.187 +    glDeleteObjectARB(data->vert_shader);
   2.188 +    glDeleteObjectARB(data->frag_shader);
   2.189 +    glDeleteObjectARB(data->program);
   2.190 +}
   2.191 +
   2.192 +static SDL_bool InitShaders()
   2.193 +{
   2.194 +    int i;
   2.195 +
   2.196 +    /* Check for shader support */
   2.197 +    shaders_supported = SDL_FALSE;
   2.198 +    if (SDL_GL_ExtensionSupported("GL_ARB_shader_objects") &&
   2.199 +        SDL_GL_ExtensionSupported("GL_ARB_shading_language_100") &&
   2.200 +        SDL_GL_ExtensionSupported("GL_ARB_vertex_shader") &&
   2.201 +        SDL_GL_ExtensionSupported("GL_ARB_fragment_shader")) {
   2.202 +        glAttachObjectARB = (PFNGLATTACHOBJECTARBPROC) SDL_GL_GetProcAddress("glAttachObjectARB");
   2.203 +        glCompileShaderARB = (PFNGLCOMPILESHADERARBPROC) SDL_GL_GetProcAddress("glCompileShaderARB");
   2.204 +        glCreateProgramObjectARB = (PFNGLCREATEPROGRAMOBJECTARBPROC) SDL_GL_GetProcAddress("glCreateProgramObjectARB");
   2.205 +        glCreateShaderObjectARB = (PFNGLCREATESHADEROBJECTARBPROC) SDL_GL_GetProcAddress("glCreateShaderObjectARB");
   2.206 +        glDeleteObjectARB = (PFNGLDELETEOBJECTARBPROC) SDL_GL_GetProcAddress("glDeleteObjectARB");
   2.207 +        glGetInfoLogARB = (PFNGLGETINFOLOGARBPROC) SDL_GL_GetProcAddress("glGetInfoLogARB");
   2.208 +        glGetObjectParameterivARB = (PFNGLGETOBJECTPARAMETERIVARBPROC) SDL_GL_GetProcAddress("glGetObjectParameterivARB");
   2.209 +        glGetUniformLocationARB = (PFNGLGETUNIFORMLOCATIONARBPROC) SDL_GL_GetProcAddress("glGetUniformLocationARB");
   2.210 +        glLinkProgramARB = (PFNGLLINKPROGRAMARBPROC) SDL_GL_GetProcAddress("glLinkProgramARB");
   2.211 +        glShaderSourceARB = (PFNGLSHADERSOURCEARBPROC) SDL_GL_GetProcAddress("glShaderSourceARB");
   2.212 +        glUniform1iARB = (PFNGLUNIFORM1IARBPROC) SDL_GL_GetProcAddress("glUniform1iARB");
   2.213 +        glUseProgramObjectARB = (PFNGLUSEPROGRAMOBJECTARBPROC) SDL_GL_GetProcAddress("glUseProgramObjectARB");
   2.214 +        if (glAttachObjectARB &&
   2.215 +            glCompileShaderARB &&
   2.216 +            glCreateProgramObjectARB &&
   2.217 +            glCreateShaderObjectARB &&
   2.218 +            glDeleteObjectARB &&
   2.219 +            glGetInfoLogARB &&
   2.220 +            glGetObjectParameterivARB &&
   2.221 +            glGetUniformLocationARB &&
   2.222 +            glLinkProgramARB &&
   2.223 +            glShaderSourceARB &&
   2.224 +            glUniform1iARB &&
   2.225 +            glUseProgramObjectARB) {
   2.226 +            shaders_supported = SDL_TRUE;
   2.227 +        }
   2.228 +    }
   2.229 +
   2.230 +    if (!shaders_supported) {
   2.231 +        return SDL_FALSE;
   2.232 +    }
   2.233 +
   2.234 +    /* Compile all the shaders */
   2.235 +    for (i = 0; i < NUM_SHADERS; ++i) {
   2.236 +        if (!CompileShaderProgram(&shaders[i])) {
   2.237 +            fprintf(stderr, "Unable to compile shader!\n");
   2.238 +            return SDL_FALSE;
   2.239 +        }
   2.240 +    }
   2.241 +
   2.242 +    /* We're done! */
   2.243 +    return SDL_TRUE;
   2.244 +}
   2.245 +
   2.246 +static void QuitShaders()
   2.247 +{
   2.248 +    int i;
   2.249 +
   2.250 +    for (i = 0; i < NUM_SHADERS; ++i) {
   2.251 +        DestroyShaderProgram(&shaders[i]);
   2.252 +    }
   2.253 +}
   2.254 +
   2.255 +/* Quick utility function for texture creation */
   2.256 +static int
   2.257 +power_of_two(int input)
   2.258 +{
   2.259 +    int value = 1;
   2.260 +
   2.261 +    while (value < input) {
   2.262 +        value <<= 1;
   2.263 +    }
   2.264 +    return value;
   2.265 +}
   2.266 +
   2.267 +GLuint
   2.268 +SDL_GL_LoadTexture(SDL_Surface * surface, GLfloat * texcoord)
   2.269 +{
   2.270 +    GLuint texture;
   2.271 +    int w, h;
   2.272 +    SDL_Surface *image;
   2.273 +    SDL_Rect area;
   2.274 +    Uint32 saved_flags;
   2.275 +    Uint8 saved_alpha;
   2.276 +
   2.277 +    /* Use the surface width and height expanded to powers of 2 */
   2.278 +    w = power_of_two(surface->w);
   2.279 +    h = power_of_two(surface->h);
   2.280 +    texcoord[0] = 0.0f;         /* Min X */
   2.281 +    texcoord[1] = 0.0f;         /* Min Y */
   2.282 +    texcoord[2] = (GLfloat) surface->w / w;     /* Max X */
   2.283 +    texcoord[3] = (GLfloat) surface->h / h;     /* Max Y */
   2.284 +
   2.285 +    image = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32,
   2.286 +#if SDL_BYTEORDER == SDL_LIL_ENDIAN     /* OpenGL RGBA masks */
   2.287 +                                 0x000000FF,
   2.288 +                                 0x0000FF00, 0x00FF0000, 0xFF000000
   2.289 +#else
   2.290 +                                 0xFF000000,
   2.291 +                                 0x00FF0000, 0x0000FF00, 0x000000FF
   2.292 +#endif
   2.293 +        );
   2.294 +    if (image == NULL) {
   2.295 +        return 0;
   2.296 +    }
   2.297 +
   2.298 +    /* Save the alpha blending attributes */
   2.299 +    saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
   2.300 +    SDL_GetSurfaceAlphaMod(surface, &saved_alpha);
   2.301 +    if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
   2.302 +        SDL_SetAlpha(surface, 0, 0);
   2.303 +    }
   2.304 +
   2.305 +    /* Copy the surface into the GL texture image */
   2.306 +    area.x = 0;
   2.307 +    area.y = 0;
   2.308 +    area.w = surface->w;
   2.309 +    area.h = surface->h;
   2.310 +    SDL_BlitSurface(surface, &area, image, &area);
   2.311 +
   2.312 +    /* Restore the alpha blending attributes */
   2.313 +    if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
   2.314 +        SDL_SetAlpha(surface, saved_flags, saved_alpha);
   2.315 +    }
   2.316 +
   2.317 +    /* Create an OpenGL texture for the image */
   2.318 +    glGenTextures(1, &texture);
   2.319 +    glBindTexture(GL_TEXTURE_2D, texture);
   2.320 +    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
   2.321 +    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
   2.322 +    glTexImage2D(GL_TEXTURE_2D,
   2.323 +                 0,
   2.324 +                 GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
   2.325 +    SDL_FreeSurface(image);     /* No longer needed */
   2.326 +
   2.327 +    return texture;
   2.328 +}
   2.329 +
   2.330 +/* A general OpenGL initialization function.    Sets all of the initial parameters. */
   2.331 +void InitGL(int Width, int Height)                    // We call this right after our OpenGL window is created.
   2.332 +{
   2.333 +    GLdouble aspect;
   2.334 +
   2.335 +    glViewport(0, 0, Width, Height);
   2.336 +    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);        // This Will Clear The Background Color To Black
   2.337 +    glClearDepth(1.0);                // Enables Clearing Of The Depth Buffer
   2.338 +    glDepthFunc(GL_LESS);                // The Type Of Depth Test To Do
   2.339 +    glEnable(GL_DEPTH_TEST);            // Enables Depth Testing
   2.340 +    glShadeModel(GL_SMOOTH);            // Enables Smooth Color Shading
   2.341 +
   2.342 +    glMatrixMode(GL_PROJECTION);
   2.343 +    glLoadIdentity();                // Reset The Projection Matrix
   2.344 +
   2.345 +    aspect = (GLdouble)Width / Height;
   2.346 +    glOrtho(-3.0, 3.0, -3.0 / aspect, 3.0 / aspect, 0.0, 1.0);
   2.347 +
   2.348 +    glMatrixMode(GL_MODELVIEW);
   2.349 +}
   2.350 +
   2.351 +/* The main drawing function. */
   2.352 +void DrawGLScene(GLuint texture, GLfloat * texcoord)
   2.353 +{
   2.354 +    /* Texture coordinate lookup, to make it simple */
   2.355 +    enum {
   2.356 +        MINX,
   2.357 +        MINY,
   2.358 +        MAXX,
   2.359 +        MAXY
   2.360 +    };
   2.361 +
   2.362 +    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);        // Clear The Screen And The Depth Buffer
   2.363 +    glLoadIdentity();                // Reset The View
   2.364 +
   2.365 +    glTranslatef(-1.5f,0.0f,0.0f);        // Move Left 1.5 Units
   2.366 +    
   2.367 +    // draw a triangle (in smooth coloring mode)
   2.368 +    glBegin(GL_POLYGON);                // start drawing a polygon
   2.369 +    glColor3f(1.0f,0.0f,0.0f);            // Set The Color To Red
   2.370 +    glVertex3f( 0.0f, 1.0f, 0.0f);        // Top
   2.371 +    glColor3f(0.0f,1.0f,0.0f);            // Set The Color To Green
   2.372 +    glVertex3f( 1.0f,-1.0f, 0.0f);        // Bottom Right
   2.373 +    glColor3f(0.0f,0.0f,1.0f);            // Set The Color To Blue
   2.374 +    glVertex3f(-1.0f,-1.0f, 0.0f);        // Bottom Left    
   2.375 +    glEnd();                    // we're done with the polygon (smooth color interpolation)    
   2.376 +
   2.377 +    glTranslatef(3.0f,0.0f,0.0f);         // Move Right 3 Units
   2.378 +
   2.379 +    // Enable blending
   2.380 +    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   2.381 +    glEnable(GL_BLEND);
   2.382 +    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   2.383 +
   2.384 +    // draw a textured square (quadrilateral)
   2.385 +    glEnable(GL_TEXTURE_2D);
   2.386 +    glBindTexture(GL_TEXTURE_2D, texture);
   2.387 +    glColor3f(1.0f,1.0f,1.0f);
   2.388 +    if (shaders_supported) {
   2.389 +        glUseProgramObjectARB(shaders[current_shader].program);
   2.390 +    }
   2.391 +
   2.392 +    glBegin(GL_QUADS);                // start drawing a polygon (4 sided)
   2.393 +    glTexCoord2f(texcoord[MINX], texcoord[MINY]);
   2.394 +    glVertex3f(-1.0f, 1.0f, 0.0f);        // Top Left
   2.395 +    glTexCoord2f(texcoord[MAXX], texcoord[MINY]);
   2.396 +    glVertex3f( 1.0f, 1.0f, 0.0f);        // Top Right
   2.397 +    glTexCoord2f(texcoord[MAXX], texcoord[MAXY]);
   2.398 +    glVertex3f( 1.0f,-1.0f, 0.0f);        // Bottom Right
   2.399 +    glTexCoord2f(texcoord[MINX], texcoord[MAXY]);
   2.400 +    glVertex3f(-1.0f,-1.0f, 0.0f);        // Bottom Left    
   2.401 +    glEnd();                    // done with the polygon
   2.402 +
   2.403 +    if (shaders_supported) {
   2.404 +        glUseProgramObjectARB(0);
   2.405 +    }
   2.406 +    glDisable(GL_TEXTURE_2D);
   2.407 +
   2.408 +    // swap buffers to display, since we're double buffered.
   2.409 +    SDL_GL_SwapBuffers();
   2.410 +}
   2.411 +
   2.412 +int main(int argc, char **argv) 
   2.413 +{    
   2.414 +    int done;
   2.415 +    SDL_Surface *surface;
   2.416 +    GLuint texture;
   2.417 +    GLfloat texcoords[4];
   2.418 +
   2.419 +    /* Initialize SDL for video output */
   2.420 +    if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
   2.421 +        fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError());
   2.422 +        exit(1);
   2.423 +    }
   2.424 +
   2.425 +    /* Create a 640x480 OpenGL screen */
   2.426 +    if ( SDL_SetVideoMode(640, 480, 0, SDL_OPENGL) == NULL ) {
   2.427 +        fprintf(stderr, "Unable to create OpenGL screen: %s\n", SDL_GetError());
   2.428 +        SDL_Quit();
   2.429 +        exit(2);
   2.430 +    }
   2.431 +
   2.432 +    /* Set the title bar in environments that support it */
   2.433 +    SDL_WM_SetCaption("Shader Demo", NULL);
   2.434 +
   2.435 +    surface = SDL_LoadBMP("icon.bmp");
   2.436 +    if ( ! surface ) {
   2.437 +        fprintf(stderr, "Unable to load icon.bmp: %s\n", SDL_GetError());
   2.438 +        SDL_Quit();
   2.439 +        exit(3);
   2.440 +    }
   2.441 +    texture = SDL_GL_LoadTexture(surface, texcoords);
   2.442 +    SDL_FreeSurface(surface);
   2.443 +
   2.444 +    /* Loop, drawing and checking events */
   2.445 +    InitGL(640, 480);
   2.446 +    if (InitShaders()) {
   2.447 +        printf("Shaders supported, press SPACE to cycle them.\n");
   2.448 +    } else {
   2.449 +        printf("Shaders not supported!\n");
   2.450 +    }
   2.451 +    done = 0;
   2.452 +    while ( ! done ) {
   2.453 +        DrawGLScene(texture, texcoords);
   2.454 +
   2.455 +        /* This could go in a separate function */
   2.456 +        { SDL_Event event;
   2.457 +            while ( SDL_PollEvent(&event) ) {
   2.458 +                if ( event.type == SDL_QUIT ) {
   2.459 +                    done = 1;
   2.460 +                }
   2.461 +                if ( event.type == SDL_KEYDOWN ) {
   2.462 +                    if ( event.key.keysym.sym == SDLK_SPACE ) {
   2.463 +                        current_shader = (current_shader + 1) % NUM_SHADERS;
   2.464 +                    }
   2.465 +                    if ( event.key.keysym.sym == SDLK_ESCAPE ) {
   2.466 +                        done = 1;
   2.467 +                    }
   2.468 +                }
   2.469 +            }
   2.470 +        }
   2.471 +    }
   2.472 +    QuitShaders();
   2.473 +    SDL_Quit();
   2.474 +    return 1;
   2.475 +}
   2.476 +
   2.477 +#else /* HAVE_OPENGL */
   2.478 +
   2.479 +int
   2.480 +main(int argc, char *argv[])
   2.481 +{
   2.482 +    printf("No OpenGL support on this system\n");
   2.483 +    return 1;
   2.484 +}
   2.485 +
   2.486 +#endif /* HAVE_OPENGL */