Mercurial > sdl-ios-xcode
annotate test/testshader.c @ 5237:74bc160186a8
Added a simple GLSL example using SDL
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Tue, 08 Feb 2011 22:11:16 -0800 |
parents | |
children | c7be6ca3a0b8 |
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 { |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
184 glDeleteObjectARB(data->vert_shader); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
185 glDeleteObjectARB(data->frag_shader); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
186 glDeleteObjectARB(data->program); |
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 |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
189 static SDL_bool InitShaders() |
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 int i; |
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 /* Check for shader support */ |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
194 shaders_supported = SDL_FALSE; |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
195 if (SDL_GL_ExtensionSupported("GL_ARB_shader_objects") && |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
196 SDL_GL_ExtensionSupported("GL_ARB_shading_language_100") && |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
197 SDL_GL_ExtensionSupported("GL_ARB_vertex_shader") && |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
198 SDL_GL_ExtensionSupported("GL_ARB_fragment_shader")) { |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
199 glAttachObjectARB = (PFNGLATTACHOBJECTARBPROC) SDL_GL_GetProcAddress("glAttachObjectARB"); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
200 glCompileShaderARB = (PFNGLCOMPILESHADERARBPROC) SDL_GL_GetProcAddress("glCompileShaderARB"); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
201 glCreateProgramObjectARB = (PFNGLCREATEPROGRAMOBJECTARBPROC) SDL_GL_GetProcAddress("glCreateProgramObjectARB"); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
202 glCreateShaderObjectARB = (PFNGLCREATESHADEROBJECTARBPROC) SDL_GL_GetProcAddress("glCreateShaderObjectARB"); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
203 glDeleteObjectARB = (PFNGLDELETEOBJECTARBPROC) SDL_GL_GetProcAddress("glDeleteObjectARB"); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
204 glGetInfoLogARB = (PFNGLGETINFOLOGARBPROC) SDL_GL_GetProcAddress("glGetInfoLogARB"); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
205 glGetObjectParameterivARB = (PFNGLGETOBJECTPARAMETERIVARBPROC) SDL_GL_GetProcAddress("glGetObjectParameterivARB"); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
206 glGetUniformLocationARB = (PFNGLGETUNIFORMLOCATIONARBPROC) SDL_GL_GetProcAddress("glGetUniformLocationARB"); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
207 glLinkProgramARB = (PFNGLLINKPROGRAMARBPROC) SDL_GL_GetProcAddress("glLinkProgramARB"); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
208 glShaderSourceARB = (PFNGLSHADERSOURCEARBPROC) SDL_GL_GetProcAddress("glShaderSourceARB"); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
209 glUniform1iARB = (PFNGLUNIFORM1IARBPROC) SDL_GL_GetProcAddress("glUniform1iARB"); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
210 glUseProgramObjectARB = (PFNGLUSEPROGRAMOBJECTARBPROC) SDL_GL_GetProcAddress("glUseProgramObjectARB"); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
211 if (glAttachObjectARB && |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
212 glCompileShaderARB && |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
213 glCreateProgramObjectARB && |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
214 glCreateShaderObjectARB && |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
215 glDeleteObjectARB && |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
216 glGetInfoLogARB && |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
217 glGetObjectParameterivARB && |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
218 glGetUniformLocationARB && |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
219 glLinkProgramARB && |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
220 glShaderSourceARB && |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
221 glUniform1iARB && |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
222 glUseProgramObjectARB) { |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
223 shaders_supported = SDL_TRUE; |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
224 } |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
225 } |
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 if (!shaders_supported) { |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
228 return SDL_FALSE; |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
229 } |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
230 |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
231 /* Compile all the shaders */ |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
232 for (i = 0; i < NUM_SHADERS; ++i) { |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
233 if (!CompileShaderProgram(&shaders[i])) { |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
234 fprintf(stderr, "Unable to compile shader!\n"); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
235 return SDL_FALSE; |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
236 } |
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 /* We're done! */ |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
240 return SDL_TRUE; |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
241 } |
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 static void QuitShaders() |
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 int i; |
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 for (i = 0; i < NUM_SHADERS; ++i) { |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
248 DestroyShaderProgram(&shaders[i]); |
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 /* Quick utility function for texture creation */ |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
253 static int |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
254 power_of_two(int input) |
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 int value = 1; |
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 while (value < input) { |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
259 value <<= 1; |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
260 } |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
261 return value; |
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 GLuint |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
265 SDL_GL_LoadTexture(SDL_Surface * surface, GLfloat * texcoord) |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
266 { |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
267 GLuint texture; |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
268 int w, h; |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
269 SDL_Surface *image; |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
270 SDL_Rect area; |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
271 Uint32 saved_flags; |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
272 Uint8 saved_alpha; |
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 /* 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
|
275 w = power_of_two(surface->w); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
276 h = power_of_two(surface->h); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
277 texcoord[0] = 0.0f; /* Min X */ |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
278 texcoord[1] = 0.0f; /* Min Y */ |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
279 texcoord[2] = (GLfloat) surface->w / w; /* Max X */ |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
280 texcoord[3] = (GLfloat) surface->h / h; /* Max Y */ |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
281 |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
282 image = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32, |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
283 #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
|
284 0x000000FF, |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
285 0x0000FF00, 0x00FF0000, 0xFF000000 |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
286 #else |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
287 0xFF000000, |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
288 0x00FF0000, 0x0000FF00, 0x000000FF |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
289 #endif |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
290 ); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
291 if (image == NULL) { |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
292 return 0; |
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 |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
295 /* Save the alpha blending attributes */ |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
296 saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
297 SDL_GetSurfaceAlphaMod(surface, &saved_alpha); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
298 if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) { |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
299 SDL_SetAlpha(surface, 0, 0); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
300 } |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
301 |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
302 /* Copy the surface into the GL texture image */ |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
303 area.x = 0; |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
304 area.y = 0; |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
305 area.w = surface->w; |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
306 area.h = surface->h; |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
307 SDL_BlitSurface(surface, &area, image, &area); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
308 |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
309 /* Restore the alpha blending attributes */ |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
310 if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) { |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
311 SDL_SetAlpha(surface, saved_flags, saved_alpha); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
312 } |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
313 |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
314 /* Create an OpenGL texture for the image */ |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
315 glGenTextures(1, &texture); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
316 glBindTexture(GL_TEXTURE_2D, texture); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
317 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
|
318 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
|
319 glTexImage2D(GL_TEXTURE_2D, |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
320 0, |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
321 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
|
322 SDL_FreeSurface(image); /* No longer needed */ |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
323 |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
324 return texture; |
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 |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
327 /* 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
|
328 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
|
329 { |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
330 GLdouble aspect; |
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 glViewport(0, 0, Width, Height); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
333 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
|
334 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
|
335 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
|
336 glEnable(GL_DEPTH_TEST); // Enables Depth Testing |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
337 glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading |
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 glMatrixMode(GL_PROJECTION); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
340 glLoadIdentity(); // Reset The Projection Matrix |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
341 |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
342 aspect = (GLdouble)Width / Height; |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
343 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
|
344 |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
345 glMatrixMode(GL_MODELVIEW); |
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 |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
348 /* The main drawing function. */ |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
349 void DrawGLScene(GLuint texture, GLfloat * texcoord) |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
350 { |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
351 /* Texture coordinate lookup, to make it simple */ |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
352 enum { |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
353 MINX, |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
354 MINY, |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
355 MAXX, |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
356 MAXY |
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 |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
359 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
|
360 glLoadIdentity(); // Reset The View |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
361 |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
362 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
|
363 |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
364 // draw a triangle (in smooth coloring mode) |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
365 glBegin(GL_POLYGON); // start drawing a polygon |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
366 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
|
367 glVertex3f( 0.0f, 1.0f, 0.0f); // Top |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
368 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
|
369 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
|
370 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
|
371 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
|
372 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
|
373 |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
374 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
|
375 |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
376 // Enable blending |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
377 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
|
378 glEnable(GL_BLEND); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
379 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
|
380 |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
381 // draw a textured square (quadrilateral) |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
382 glEnable(GL_TEXTURE_2D); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
383 glBindTexture(GL_TEXTURE_2D, texture); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
384 glColor3f(1.0f,1.0f,1.0f); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
385 if (shaders_supported) { |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
386 glUseProgramObjectARB(shaders[current_shader].program); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
387 } |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
388 |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
389 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
|
390 glTexCoord2f(texcoord[MINX], texcoord[MINY]); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
391 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
|
392 glTexCoord2f(texcoord[MAXX], 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 Right |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
394 glTexCoord2f(texcoord[MAXX], texcoord[MAXY]); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
395 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
|
396 glTexCoord2f(texcoord[MINX], 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 Left |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
398 glEnd(); // done with the polygon |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
399 |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
400 if (shaders_supported) { |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
401 glUseProgramObjectARB(0); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
402 } |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
403 glDisable(GL_TEXTURE_2D); |
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 // 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
|
406 SDL_GL_SwapBuffers(); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
407 } |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
408 |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
409 int main(int argc, char **argv) |
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 done; |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
412 SDL_Surface *surface; |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
413 GLuint texture; |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
414 GLfloat texcoords[4]; |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
415 |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
416 /* Initialize SDL for video output */ |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
417 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
418 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
|
419 exit(1); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
420 } |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
421 |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
422 /* Create a 640x480 OpenGL screen */ |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
423 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
|
424 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
|
425 SDL_Quit(); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
426 exit(2); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
427 } |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
428 |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
429 /* 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
|
430 SDL_WM_SetCaption("Shader Demo", NULL); |
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 surface = SDL_LoadBMP("icon.bmp"); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
433 if ( ! surface ) { |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
434 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
|
435 SDL_Quit(); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
436 exit(3); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
437 } |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
438 texture = SDL_GL_LoadTexture(surface, texcoords); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
439 SDL_FreeSurface(surface); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
440 |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
441 /* Loop, drawing and checking events */ |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
442 InitGL(640, 480); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
443 if (InitShaders()) { |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
444 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
|
445 } else { |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
446 printf("Shaders not supported!\n"); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
447 } |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
448 done = 0; |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
449 while ( ! done ) { |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
450 DrawGLScene(texture, texcoords); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
451 |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
452 /* This could go in a separate function */ |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
453 { SDL_Event event; |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
454 while ( SDL_PollEvent(&event) ) { |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
455 if ( event.type == SDL_QUIT ) { |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
456 done = 1; |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
457 } |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
458 if ( event.type == SDL_KEYDOWN ) { |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
459 if ( event.key.keysym.sym == SDLK_SPACE ) { |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
460 current_shader = (current_shader + 1) % NUM_SHADERS; |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
461 } |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
462 if ( event.key.keysym.sym == SDLK_ESCAPE ) { |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
463 done = 1; |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
464 } |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
465 } |
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 QuitShaders(); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
470 SDL_Quit(); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
471 return 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 |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
474 #else /* HAVE_OPENGL */ |
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 int |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
477 main(int argc, char *argv[]) |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
478 { |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
479 printf("No OpenGL support on this system\n"); |
74bc160186a8
Added a simple GLSL example using SDL
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
480 return 1; |
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 #endif /* HAVE_OPENGL */ |