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