test/testshader.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 25 Jul 2013 09:51:21 -0700
changeset 7517 965d57022c01
parent 7442 a65a1c17af2b
child 7639 9406b7dd2f2d
permissions -rw-r--r--
Updated the copyright year for the test programs
slouken@5535
     1
/*
slouken@7517
     2
  Copyright (C) 1997-2013 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
philipp@7442
    14
#include <stdio.h> /* for printf() */
slouken@5232
    15
#include "SDL.h"
slouken@5232
    16
slouken@5232
    17
#ifdef HAVE_OPENGL
slouken@5232
    18
slouken@5232
    19
#include "SDL_opengl.h"
slouken@5232
    20
slouken@5232
    21
slouken@5232
    22
static SDL_bool shaders_supported;
slouken@5232
    23
static int      current_shader = 0;
slouken@5232
    24
slouken@5232
    25
enum {
slouken@5232
    26
    SHADER_COLOR,
slouken@5232
    27
    SHADER_TEXTURE,
slouken@5232
    28
    SHADER_TEXCOORDS,
slouken@5232
    29
    NUM_SHADERS
slouken@5232
    30
};
slouken@5232
    31
slouken@5232
    32
typedef struct {
icculus@5641
    33
    GLhandleARB program;
icculus@5641
    34
    GLhandleARB vert_shader;
icculus@5641
    35
    GLhandleARB frag_shader;
slouken@5232
    36
    const char *vert_source;
slouken@5232
    37
    const char *frag_source;
slouken@5232
    38
} ShaderData;
slouken@5232
    39
slouken@5232
    40
static ShaderData shaders[NUM_SHADERS] = {
slouken@5232
    41
slouken@5232
    42
    /* SHADER_COLOR */
slouken@5232
    43
    { 0, 0, 0,
slouken@5232
    44
        /* vertex shader */
slouken@5232
    45
"varying vec4 v_color;\n"
slouken@5232
    46
"\n"
slouken@5232
    47
"void main()\n"
slouken@5232
    48
"{\n"
slouken@5232
    49
"    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
slouken@5232
    50
"    v_color = gl_Color;\n"
slouken@5232
    51
"}",
slouken@5232
    52
        /* fragment shader */
slouken@5232
    53
"varying vec4 v_color;\n"
slouken@5232
    54
"\n"
slouken@5232
    55
"void main()\n"
slouken@5232
    56
"{\n"
slouken@5232
    57
"    gl_FragColor = v_color;\n"
slouken@5232
    58
"}"
slouken@5232
    59
    },
slouken@5232
    60
slouken@5232
    61
    /* SHADER_TEXTURE */
slouken@5232
    62
    { 0, 0, 0,
slouken@5232
    63
        /* vertex shader */
slouken@5232
    64
"varying vec4 v_color;\n"
slouken@5232
    65
"varying vec2 v_texCoord;\n"
slouken@5232
    66
"\n"
slouken@5232
    67
"void main()\n"
slouken@5232
    68
"{\n"
slouken@5232
    69
"    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
slouken@5232
    70
"    v_color = gl_Color;\n"
slouken@5232
    71
"    v_texCoord = vec2(gl_MultiTexCoord0);\n"
slouken@5232
    72
"}",
slouken@5232
    73
        /* fragment shader */
slouken@5232
    74
"varying vec4 v_color;\n"
slouken@5232
    75
"varying vec2 v_texCoord;\n"
slouken@5232
    76
"uniform sampler2D tex0;\n"
slouken@5232
    77
"\n"
slouken@5232
    78
"void main()\n"
slouken@5232
    79
"{\n"
slouken@5232
    80
"    gl_FragColor = texture2D(tex0, v_texCoord) * v_color;\n"
slouken@5232
    81
"}"
slouken@5232
    82
    },
slouken@5232
    83
slouken@5232
    84
    /* SHADER_TEXCOORDS */
slouken@5232
    85
    { 0, 0, 0,
slouken@5232
    86
        /* vertex shader */
slouken@5232
    87
"varying vec2 v_texCoord;\n"
slouken@5232
    88
"\n"
slouken@5232
    89
"void main()\n"
slouken@5232
    90
"{\n"
slouken@5232
    91
"    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
slouken@5232
    92
"    v_texCoord = vec2(gl_MultiTexCoord0);\n"
slouken@5232
    93
"}",
slouken@5232
    94
        /* fragment shader */
slouken@5232
    95
"varying vec2 v_texCoord;\n"
slouken@5232
    96
"\n"
slouken@5232
    97
"void main()\n"
slouken@5232
    98
"{\n"
slouken@5232
    99
"    vec4 color;\n"
slouken@5232
   100
"    vec2 delta;\n"
slouken@5232
   101
"    float dist;\n"
slouken@5232
   102
"\n"
slouken@5232
   103
"    delta = vec2(0.5, 0.5) - v_texCoord;\n"
slouken@5232
   104
"    dist = dot(delta, delta);\n"
slouken@5232
   105
"\n"
slouken@5232
   106
"    color.r = v_texCoord.x;\n"
slouken@5232
   107
"    color.g = v_texCoord.x * v_texCoord.y;\n"
slouken@5232
   108
"    color.b = v_texCoord.y;\n"
slouken@5232
   109
"    color.a = 1.0 - (dist * 4.0);\n"
slouken@5232
   110
"    gl_FragColor = color;\n"
slouken@5232
   111
"}"
slouken@5232
   112
    },
slouken@5232
   113
};
slouken@7191
   114
slouken@5232
   115
static PFNGLATTACHOBJECTARBPROC glAttachObjectARB;
slouken@5232
   116
static PFNGLCOMPILESHADERARBPROC glCompileShaderARB;
slouken@5232
   117
static PFNGLCREATEPROGRAMOBJECTARBPROC glCreateProgramObjectARB;
slouken@5232
   118
static PFNGLCREATESHADEROBJECTARBPROC glCreateShaderObjectARB;
slouken@5232
   119
static PFNGLDELETEOBJECTARBPROC glDeleteObjectARB;
slouken@5232
   120
static PFNGLGETINFOLOGARBPROC glGetInfoLogARB;
slouken@5232
   121
static PFNGLGETOBJECTPARAMETERIVARBPROC glGetObjectParameterivARB;
slouken@5232
   122
static PFNGLGETUNIFORMLOCATIONARBPROC glGetUniformLocationARB;
slouken@5232
   123
static PFNGLLINKPROGRAMARBPROC glLinkProgramARB;
slouken@5232
   124
static PFNGLSHADERSOURCEARBPROC glShaderSourceARB;
slouken@5232
   125
static PFNGLUNIFORM1IARBPROC glUniform1iARB;
slouken@5232
   126
static PFNGLUSEPROGRAMOBJECTARBPROC glUseProgramObjectARB;
slouken@5232
   127
icculus@5641
   128
static SDL_bool CompileShader(GLhandleARB shader, const char *source)
slouken@5232
   129
{
slouken@5232
   130
    GLint status;
slouken@5232
   131
slouken@5232
   132
    glShaderSourceARB(shader, 1, &source, NULL);
slouken@5232
   133
    glCompileShaderARB(shader);
slouken@5232
   134
    glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &status);
slouken@5232
   135
    if (status == 0) {
slouken@5232
   136
        GLint length;
slouken@5232
   137
        char *info;
slouken@5232
   138
slouken@5232
   139
        glGetObjectParameterivARB(shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &length);
slouken@5232
   140
        info = SDL_stack_alloc(char, length+1);
slouken@5232
   141
        glGetInfoLogARB(shader, length, NULL, info);
slouken@5232
   142
        fprintf(stderr, "Failed to compile shader:\n%s\n%s", source, info);
slouken@5232
   143
        SDL_stack_free(info);
slouken@5232
   144
slouken@5232
   145
        return SDL_FALSE;
slouken@5232
   146
    } else {
slouken@5232
   147
        return SDL_TRUE;
slouken@5232
   148
    }
slouken@5232
   149
}
slouken@5232
   150
slouken@5232
   151
static SDL_bool CompileShaderProgram(ShaderData *data)
slouken@5232
   152
{
slouken@5232
   153
    const int num_tmus_bound = 4;
slouken@5232
   154
    int i;
slouken@5232
   155
    GLint location;
slouken@5232
   156
slouken@5232
   157
    glGetError();
slouken@5232
   158
slouken@5232
   159
    /* Create one program object to rule them all */
slouken@5232
   160
    data->program = glCreateProgramObjectARB();
slouken@5232
   161
slouken@5232
   162
    /* Create the vertex shader */
slouken@5232
   163
    data->vert_shader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
slouken@5232
   164
    if (!CompileShader(data->vert_shader, data->vert_source)) {
slouken@5232
   165
        return SDL_FALSE;
slouken@5232
   166
    }
slouken@5232
   167
slouken@5232
   168
    /* Create the fragment shader */
slouken@5232
   169
    data->frag_shader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
slouken@5232
   170
    if (!CompileShader(data->frag_shader, data->frag_source)) {
slouken@5232
   171
        return SDL_FALSE;
slouken@5232
   172
    }
slouken@5232
   173
slouken@5232
   174
    /* ... and in the darkness bind them */
slouken@5232
   175
    glAttachObjectARB(data->program, data->vert_shader);
slouken@5232
   176
    glAttachObjectARB(data->program, data->frag_shader);
slouken@5232
   177
    glLinkProgramARB(data->program);
slouken@5232
   178
slouken@5232
   179
    /* Set up some uniform variables */
slouken@5232
   180
    glUseProgramObjectARB(data->program);
slouken@5232
   181
    for (i = 0; i < num_tmus_bound; ++i) {
slouken@5232
   182
        char tex_name[5];
slouken@5232
   183
        SDL_snprintf(tex_name, SDL_arraysize(tex_name), "tex%d", i);
slouken@5232
   184
        location = glGetUniformLocationARB(data->program, tex_name);
slouken@5232
   185
        if (location >= 0) {
slouken@5232
   186
            glUniform1iARB(location, i);
slouken@5232
   187
        }
slouken@5232
   188
    }
slouken@5232
   189
    glUseProgramObjectARB(0);
slouken@7191
   190
slouken@5232
   191
    return (glGetError() == GL_NO_ERROR);
slouken@5232
   192
}
slouken@5232
   193
slouken@5232
   194
static void DestroyShaderProgram(ShaderData *data)
slouken@5232
   195
{
slouken@5236
   196
    if (shaders_supported) {
slouken@5236
   197
        glDeleteObjectARB(data->vert_shader);
slouken@5236
   198
        glDeleteObjectARB(data->frag_shader);
slouken@5236
   199
        glDeleteObjectARB(data->program);
slouken@5236
   200
    }
slouken@5232
   201
}
slouken@5232
   202
slouken@5232
   203
static SDL_bool InitShaders()
slouken@5232
   204
{
slouken@5232
   205
    int i;
slouken@5232
   206
slouken@5232
   207
    /* Check for shader support */
slouken@5232
   208
    shaders_supported = SDL_FALSE;
slouken@5232
   209
    if (SDL_GL_ExtensionSupported("GL_ARB_shader_objects") &&
slouken@5232
   210
        SDL_GL_ExtensionSupported("GL_ARB_shading_language_100") &&
slouken@5232
   211
        SDL_GL_ExtensionSupported("GL_ARB_vertex_shader") &&
slouken@5232
   212
        SDL_GL_ExtensionSupported("GL_ARB_fragment_shader")) {
slouken@5232
   213
        glAttachObjectARB = (PFNGLATTACHOBJECTARBPROC) SDL_GL_GetProcAddress("glAttachObjectARB");
slouken@5232
   214
        glCompileShaderARB = (PFNGLCOMPILESHADERARBPROC) SDL_GL_GetProcAddress("glCompileShaderARB");
slouken@5232
   215
        glCreateProgramObjectARB = (PFNGLCREATEPROGRAMOBJECTARBPROC) SDL_GL_GetProcAddress("glCreateProgramObjectARB");
slouken@5232
   216
        glCreateShaderObjectARB = (PFNGLCREATESHADEROBJECTARBPROC) SDL_GL_GetProcAddress("glCreateShaderObjectARB");
slouken@5232
   217
        glDeleteObjectARB = (PFNGLDELETEOBJECTARBPROC) SDL_GL_GetProcAddress("glDeleteObjectARB");
slouken@5232
   218
        glGetInfoLogARB = (PFNGLGETINFOLOGARBPROC) SDL_GL_GetProcAddress("glGetInfoLogARB");
slouken@5232
   219
        glGetObjectParameterivARB = (PFNGLGETOBJECTPARAMETERIVARBPROC) SDL_GL_GetProcAddress("glGetObjectParameterivARB");
slouken@5232
   220
        glGetUniformLocationARB = (PFNGLGETUNIFORMLOCATIONARBPROC) SDL_GL_GetProcAddress("glGetUniformLocationARB");
slouken@5232
   221
        glLinkProgramARB = (PFNGLLINKPROGRAMARBPROC) SDL_GL_GetProcAddress("glLinkProgramARB");
slouken@5232
   222
        glShaderSourceARB = (PFNGLSHADERSOURCEARBPROC) SDL_GL_GetProcAddress("glShaderSourceARB");
slouken@5232
   223
        glUniform1iARB = (PFNGLUNIFORM1IARBPROC) SDL_GL_GetProcAddress("glUniform1iARB");
slouken@5232
   224
        glUseProgramObjectARB = (PFNGLUSEPROGRAMOBJECTARBPROC) SDL_GL_GetProcAddress("glUseProgramObjectARB");
slouken@5232
   225
        if (glAttachObjectARB &&
slouken@5232
   226
            glCompileShaderARB &&
slouken@5232
   227
            glCreateProgramObjectARB &&
slouken@5232
   228
            glCreateShaderObjectARB &&
slouken@5232
   229
            glDeleteObjectARB &&
slouken@5232
   230
            glGetInfoLogARB &&
slouken@5232
   231
            glGetObjectParameterivARB &&
slouken@5232
   232
            glGetUniformLocationARB &&
slouken@5232
   233
            glLinkProgramARB &&
slouken@5232
   234
            glShaderSourceARB &&
slouken@5232
   235
            glUniform1iARB &&
slouken@5232
   236
            glUseProgramObjectARB) {
slouken@5232
   237
            shaders_supported = SDL_TRUE;
slouken@5232
   238
        }
slouken@5232
   239
    }
slouken@5232
   240
slouken@5232
   241
    if (!shaders_supported) {
slouken@5232
   242
        return SDL_FALSE;
slouken@5232
   243
    }
slouken@5232
   244
slouken@5232
   245
    /* Compile all the shaders */
slouken@5232
   246
    for (i = 0; i < NUM_SHADERS; ++i) {
slouken@5232
   247
        if (!CompileShaderProgram(&shaders[i])) {
slouken@5232
   248
            fprintf(stderr, "Unable to compile shader!\n");
slouken@5232
   249
            return SDL_FALSE;
slouken@5232
   250
        }
slouken@5232
   251
    }
slouken@5232
   252
slouken@5232
   253
    /* We're done! */
slouken@5232
   254
    return SDL_TRUE;
slouken@5232
   255
}
slouken@5232
   256
slouken@5232
   257
static void QuitShaders()
slouken@5232
   258
{
slouken@5232
   259
    int i;
slouken@5232
   260
slouken@5232
   261
    for (i = 0; i < NUM_SHADERS; ++i) {
slouken@5232
   262
        DestroyShaderProgram(&shaders[i]);
slouken@5232
   263
    }
slouken@5232
   264
}
slouken@5232
   265
slouken@5232
   266
/* Quick utility function for texture creation */
slouken@5232
   267
static int
slouken@5232
   268
power_of_two(int input)
slouken@5232
   269
{
slouken@5232
   270
    int value = 1;
slouken@5232
   271
slouken@5232
   272
    while (value < input) {
slouken@5232
   273
        value <<= 1;
slouken@5232
   274
    }
slouken@5232
   275
    return value;
slouken@5232
   276
}
slouken@5232
   277
slouken@5232
   278
GLuint
slouken@5232
   279
SDL_GL_LoadTexture(SDL_Surface * surface, GLfloat * texcoord)
slouken@5232
   280
{
slouken@5232
   281
    GLuint texture;
slouken@5232
   282
    int w, h;
slouken@5232
   283
    SDL_Surface *image;
slouken@5232
   284
    SDL_Rect area;
slouken@7324
   285
    SDL_BlendMode saved_mode;
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@7324
   309
    SDL_GetSurfaceBlendMode(surface, &saved_mode);
slouken@7324
   310
    SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_NONE);
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@7324
   320
    SDL_SetSurfaceBlendMode(surface, saved_mode);
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: */