annotate test/testshader.c @ 5266:595814f561f7

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