/[pcsx2_0.9.7]/trunk/3rdparty/SDL-1.3.0-5387/test/testshader.c
ViewVC logotype

Contents of /trunk/3rdparty/SDL-1.3.0-5387/test/testshader.c

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22