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