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