1 /* glblur --- radial blur using GL textures
2 * Copyright (c) 2002 Jamie Zawinski <jwz@jwz.org>
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation. No representations are made about the suitability of this
9 * software for any purpose. It is provided "as is" without express or
12 * This program draws a box and a few line segments, and generates a flowing
13 * radial blur outward from it -- this causes flowing field effects.
14 * It does this by rendering the scene into a small texture, then repeatedly
15 * rendering increasingly-enlarged and increasingly-transparent versions of
16 * that texture onto the frame buffer.
18 * As such, it's quite graphics intensive -- don't bother trying to run this
19 * if you don't have hardware-accelerated texture support.
21 * Inspired by Dario Corno's Radial Blur tutorial:
22 * http://nehe.gamedev.net/tutorials/lesson.asp?l=36
25 #include <X11/Intrinsic.h>
27 extern XtAppContext app;
29 #define PROGCLASS "GLBlur"
30 #define HACK_INIT init_glblur
31 #define HACK_DRAW draw_glblur
32 #define HACK_RESHAPE reshape_glblur
33 #define HACK_HANDLE_EVENT glblur_handle_event
34 #define EVENT_MASK PointerMotionMask
35 #define sws_opts xlockmore_opts
37 #define DEF_SPIN "XYZ"
38 #define DEF_WANDER "True"
39 #define DEF_BLURSIZE "15"
41 #define DEFAULTS "*delay: 10000 \n" \
42 "*showFPS: False \n" \
43 "*fpsSolid: True \n" \
44 "*wireframe: False \n" \
45 "*spin: " DEF_SPIN "\n" \
46 "*wander: " DEF_WANDER "\n" \
47 "*blurSize: " DEF_BLURSIZE "\n" \
51 #define countof(x) (sizeof((x))/sizeof((*x)))
54 #define ABS(n) ((n)<0?-(n):(n))
56 #define SIGNOF(n) ((n)<0?-1:1)
58 #include "xlockmore.h"
61 #include "gltrackball.h"
64 #ifdef USE_GL /* whole file */
69 typedef struct metaball metaball;
73 GLXContext *glx_context;
75 trackball_state *trackball;
78 GLuint obj_dlist0; /* east-west cube faces */
79 GLuint obj_dlist1; /* north-south cube faces */
80 GLuint obj_dlist2; /* up-down cube faces */
81 GLuint obj_dlist3; /* spikes coming out of the cube's corners */
82 GLuint scene_dlist1; /* the cube, rotated and translated */
83 GLuint scene_dlist2; /* the spikes, rotated and translated */
84 int scene_polys1; /* polygons in scene, not counting texture overlay */
85 int scene_polys2; /* polygons in scene, not counting texture overlay */
88 unsigned int *tex_data;
101 } glblur_configuration;
103 static glblur_configuration *bps = NULL;
105 static char *do_spin;
106 static Bool do_wander;
109 static XrmOptionDescRec opts[] = {
110 { "-spin", ".spin", XrmoptionSepArg, 0 },
111 { "+spin", ".spin", XrmoptionNoArg, "" },
112 { "-blursize", ".blurSize", XrmoptionSepArg, 0 },
113 { "-wander", ".wander", XrmoptionNoArg, "True" },
114 { "+wander", ".wander", XrmoptionNoArg, "False" },
117 static argtype vars[] = {
118 {(caddr_t *) &do_spin, "spin", "Spin", DEF_SPIN, t_String},
119 {(caddr_t *) &do_wander, "wander", "Wander", DEF_WANDER, t_Bool},
120 {(caddr_t *) &blursize, "blurSize","BlurSize", DEF_BLURSIZE, t_Int},
123 ModeSpecOpt sws_opts = {countof(opts), opts, countof(vars), vars, NULL};
126 /* Window management, etc
129 reshape_glblur (ModeInfo *mi, int width, int height)
131 GLfloat h = (GLfloat) height / (GLfloat) width;
133 glViewport (0, 0, (GLint) width, (GLint) height);
135 glMatrixMode(GL_PROJECTION);
137 gluPerspective (30.0, 1/h, 1.0, 100.0);
139 glMatrixMode(GL_MODELVIEW);
141 gluLookAt( 0.0, 0.0, 8.0,
145 glClear(GL_COLOR_BUFFER_BIT);
150 /* Objects in the scene
154 generate_object (ModeInfo *mi)
156 glblur_configuration *bp = &bps[MI_SCREEN(mi)];
157 Bool wire = MI_IS_WIREFRAME (mi);
160 bp->scene_polys1 = 0;
161 bp->scene_polys2 = 0;
163 glNewList (bp->obj_dlist0, GL_COMPILE);
164 glBegin (wire ? GL_LINE_LOOP : GL_QUADS); /* front */
165 glNormal3f (0, 0, 1);
166 glTexCoord2f(1, 0); glVertex3f ( 0.5, -0.5, 0.5);
167 glTexCoord2f(0, 0); glVertex3f ( 0.5, 0.5, 0.5);
168 glTexCoord2f(0, 1); glVertex3f (-0.5, 0.5, 0.5);
169 glTexCoord2f(1, 1); glVertex3f (-0.5, -0.5, 0.5);
173 glBegin (wire ? GL_LINE_LOOP : GL_QUADS); /* back */
174 glNormal3f (0, 0, -1);
175 glTexCoord2f(0, 0); glVertex3f (-0.5, -0.5, -0.5);
176 glTexCoord2f(0, 1); glVertex3f (-0.5, 0.5, -0.5);
177 glTexCoord2f(1, 1); glVertex3f ( 0.5, 0.5, -0.5);
178 glTexCoord2f(1, 0); glVertex3f ( 0.5, -0.5, -0.5);
183 glNewList (bp->obj_dlist1, GL_COMPILE);
184 glBegin (wire ? GL_LINE_LOOP : GL_QUADS); /* left */
185 glNormal3f (-1, 0, 0);
186 glTexCoord2f(1, 1); glVertex3f (-0.5, 0.5, 0.5);
187 glTexCoord2f(1, 0); glVertex3f (-0.5, 0.5, -0.5);
188 glTexCoord2f(0, 0); glVertex3f (-0.5, -0.5, -0.5);
189 glTexCoord2f(0, 1); glVertex3f (-0.5, -0.5, 0.5);
193 glBegin (wire ? GL_LINE_LOOP : GL_QUADS); /* right */
194 glNormal3f (1, 0, 0);
195 glTexCoord2f(1, 1); glVertex3f ( 0.5, -0.5, -0.5);
196 glTexCoord2f(1, 0); glVertex3f ( 0.5, 0.5, -0.5);
197 glTexCoord2f(0, 0); glVertex3f ( 0.5, 0.5, 0.5);
198 glTexCoord2f(0, 1); glVertex3f ( 0.5, -0.5, 0.5);
203 glNewList (bp->obj_dlist2, GL_COMPILE);
204 glBegin (wire ? GL_LINE_LOOP : GL_QUADS); /* top */
205 glNormal3f (0, 1, 0);
206 glTexCoord2f(0, 0); glVertex3f ( 0.5, 0.5, 0.5);
207 glTexCoord2f(0, 1); glVertex3f ( 0.5, 0.5, -0.5);
208 glTexCoord2f(1, 1); glVertex3f (-0.5, 0.5, -0.5);
209 glTexCoord2f(1, 0); glVertex3f (-0.5, 0.5, 0.5);
213 glBegin (wire ? GL_LINE_LOOP : GL_QUADS); /* bottom */
214 glNormal3f (0, -1, 0);
215 glTexCoord2f(1, 0); glVertex3f (-0.5, -0.5, 0.5);
216 glTexCoord2f(0, 0); glVertex3f (-0.5, -0.5, -0.5);
217 glTexCoord2f(0, 1); glVertex3f ( 0.5, -0.5, -0.5);
218 glTexCoord2f(1, 1); glVertex3f ( 0.5, -0.5, 0.5);
223 glNewList (bp->obj_dlist3, GL_COMPILE);
226 glVertex3f(-s, 0, 0); glVertex3f(s, 0, 0); /* face spikes */
227 glVertex3f(0, -s, 0); glVertex3f(0, s, 0);
228 glVertex3f(0, 0, -s); glVertex3f(0, 0, s);
229 bp->scene_polys2 += 3;
234 glVertex3f(-s, -s, -s); glVertex3f( s, s, s); /* corner spikes */
235 glVertex3f(-s, -s, s); glVertex3f( s, s, -s);
236 glVertex3f(-s, s, -s); glVertex3f( s, -s, s);
237 glVertex3f( s, -s, -s); glVertex3f(-s, s, s);
238 bp->scene_polys2 += 4;
242 check_gl_error ("object generation");
247 init_texture (ModeInfo *mi)
249 glblur_configuration *bp = &bps[MI_SCREEN(mi)];
251 if (bp->tex_data) free (bp->tex_data);
255 bp->tex_data = (unsigned int *)
256 malloc (bp->tex_w * bp->tex_h * 4 * sizeof (unsigned int));
258 glGenTextures (1, &bp->texture);
259 glBindTexture (GL_TEXTURE_2D, bp->texture);
260 glTexImage2D (GL_TEXTURE_2D, 0, 4, 128, 128, 0,
261 GL_RGBA, GL_UNSIGNED_BYTE, bp->tex_data);
262 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
263 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
268 render_scene_to_texture (ModeInfo *mi)
270 glblur_configuration *bp = &bps[MI_SCREEN(mi)];
272 glViewport (0, 0, bp->tex_w, bp->tex_h);
274 glCallList (bp->scene_dlist1);
275 glCallList (bp->scene_dlist2);
277 glBindTexture (GL_TEXTURE_2D, bp->texture);
278 glCopyTexImage2D (GL_TEXTURE_2D, 0, GL_LUMINANCE, 0, 0,
279 bp->tex_w, bp->tex_h, 0);
280 check_gl_error ("texture");
282 glViewport (0, 0, MI_WIDTH(mi), MI_HEIGHT(mi));
286 overlay_blur_texture (ModeInfo *mi)
288 glblur_configuration *bp = &bps[MI_SCREEN(mi)];
289 int w = MI_WIDTH (mi);
290 int h = MI_HEIGHT (mi);
291 int times = blursize;
293 GLfloat inc = 0.02 * (25.0 / times);
295 GLfloat spost = 0; /* starting texture coordinate offset */
296 GLfloat alpha_inc = 0.9 / times; /* transparency fade factor */
297 GLfloat alpha = 0.2; /* initial transparency */
299 glDisable (GL_TEXTURE_GEN_S);
300 glDisable (GL_TEXTURE_GEN_T);
302 glEnable (GL_TEXTURE_2D);
303 glDisable (GL_DEPTH_TEST);
304 glBlendFunc (GL_SRC_ALPHA,GL_ONE);
306 glBindTexture (GL_TEXTURE_2D, bp->texture);
309 /* switch to orthographic projection, saving both previous matrixes
310 on their respective stacks.
312 glMatrixMode (GL_PROJECTION);
315 glOrtho (0, w, h, 0, -1, 1);
316 glMatrixMode (GL_MODELVIEW);
321 alpha_inc = alpha / times;
323 mi->polygon_count = bp->scene_polys1 + bp->scene_polys2;
326 for (i = 0; i < times; i++)
328 glColor4f (1, 1, 1, alpha);
329 glTexCoord2f (0+spost, 1-spost); glVertex2f (0, 0);
330 glTexCoord2f (0+spost, 0+spost); glVertex2f (0, h);
331 glTexCoord2f (1-spost, 0+spost); glVertex2f (w, h);
332 glTexCoord2f (1-spost, 1-spost); glVertex2f (w, 0);
339 /* Switch back to perspective projection, restoring the saved matrixes
341 glMatrixMode (GL_PROJECTION);
343 glMatrixMode (GL_MODELVIEW);
346 glEnable (GL_DEPTH_TEST);
347 glDisable (GL_BLEND);
348 glDisable (GL_TEXTURE_2D);
349 glBindTexture (GL_TEXTURE_2D, 0);
354 /* Startup initialization
358 glblur_handle_event (ModeInfo *mi, XEvent *event)
360 glblur_configuration *bp = &bps[MI_SCREEN(mi)];
362 if (event->xany.type == ButtonPress &&
363 event->xbutton.button & Button1)
365 bp->button_down_p = True;
366 gltrackball_start (bp->trackball,
367 event->xbutton.x, event->xbutton.y,
368 MI_WIDTH (mi), MI_HEIGHT (mi));
371 else if (event->xany.type == ButtonRelease &&
372 event->xbutton.button & Button1)
374 bp->button_down_p = False;
377 else if (event->xany.type == MotionNotify &&
380 gltrackball_track (bp->trackball,
381 event->xmotion.x, event->xmotion.y,
382 MI_WIDTH (mi), MI_HEIGHT (mi));
391 init_glblur (ModeInfo *mi)
393 glblur_configuration *bp;
394 int wire = MI_IS_WIREFRAME(mi);
397 bps = (glblur_configuration *)
398 calloc (MI_NUM_SCREENS(mi), sizeof (glblur_configuration));
400 fprintf(stderr, "%s: out of memory\n", progname);
405 bp = &bps[MI_SCREEN(mi)];
407 bp->glx_context = init_GL(mi);
409 reshape_glblur (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
413 GLfloat gamb[4]= {0.2, 0.2, 0.2, 1.0};
414 GLfloat pos[4] = {0.0, 5.0, 10.0, 1.0};
415 GLfloat amb[4] = {0.2, 0.2, 0.2, 1.0};
416 GLfloat dif[4] = {0.3, 0.3, 0.3, 1.0};
417 GLfloat spc[4] = {0.8, 0.8, 0.8, 1.0};
420 glEnable(GL_LIGHTING);
423 glEnable(GL_DEPTH_TEST);
424 glEnable(GL_CULL_FACE);
425 glEnable(GL_NORMALIZE);
426 glShadeModel(GL_SMOOTH);
428 glLightModelfv (GL_LIGHT_MODEL_AMBIENT, gamb);
430 glLightfv(GL_LIGHT0, GL_POSITION, pos);
431 glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
432 glLightfv(GL_LIGHT0, GL_DIFFUSE, dif);
433 glLightfv(GL_LIGHT0, GL_SPECULAR, spc);
435 glEnable(GL_LIGHTING);
438 glMateriali(GL_FRONT, GL_SHININESS, shiny);
442 Bool spinx=False, spiny=False, spinz=False;
443 double spin_speed = 0.9;
444 double wander_speed = 0.06;
449 if (*s == 'x' || *s == 'X') spinx = True;
450 else if (*s == 'y' || *s == 'Y') spiny = True;
451 else if (*s == 'z' || *s == 'Z') spinz = True;
455 "%s: spin must contain only the characters X, Y, or Z (not \"%s\")\n",
462 bp->rot = make_rotator (spinx ? spin_speed : 0,
463 spiny ? spin_speed : 0,
464 spinz ? spin_speed : 0,
466 do_wander ? wander_speed : 0,
468 bp->trackball = gltrackball_init ();
471 if (blursize < 0) blursize = 0;
472 if (blursize > 200) blursize = 200;
475 bp->colors0 = (XColor *) calloc(bp->ncolors, sizeof(XColor));
476 bp->colors1 = (XColor *) calloc(bp->ncolors, sizeof(XColor));
477 bp->colors2 = (XColor *) calloc(bp->ncolors, sizeof(XColor));
478 bp->colors3 = (XColor *) calloc(bp->ncolors, sizeof(XColor));
479 make_smooth_colormap (0, 0, 0, bp->colors0, &bp->ncolors, False, 0, False);
480 make_smooth_colormap (0, 0, 0, bp->colors1, &bp->ncolors, False, 0, False);
481 make_smooth_colormap (0, 0, 0, bp->colors2, &bp->ncolors, False, 0, False);
482 make_smooth_colormap (0, 0, 0, bp->colors3, &bp->ncolors, False, 0, False);
485 bp->obj_dlist0 = glGenLists (1);
486 bp->obj_dlist1 = glGenLists (1);
487 bp->obj_dlist2 = glGenLists (1);
488 bp->obj_dlist3 = glGenLists (1);
489 bp->scene_dlist1 = glGenLists (1);
490 bp->scene_dlist2 = glGenLists (1);
493 generate_object (mi);
500 draw_glblur (ModeInfo *mi)
502 glblur_configuration *bp = &bps[MI_SCREEN(mi)];
503 Display *dpy = MI_DISPLAY(mi);
504 Window window = MI_WINDOW(mi);
506 static GLfloat color0[4] = {0.0, 0.0, 0.0, 1.0};
507 static GLfloat color1[4] = {0.0, 0.0, 0.0, 1.0};
508 static GLfloat color2[4] = {0.0, 0.0, 0.0, 1.0};
509 static GLfloat color3[4] = {0.0, 0.0, 0.0, 1.0};
510 static GLfloat spec[4] = {1.0, 1.0, 1.0, 1.0};
516 if (!bp->glx_context)
519 /* Decide what we're drawing
521 if (0 == (random() % 30))
523 bp->show_cube_p = (0 == (random() % 10));
524 bp->show_spikes_p = (0 == (random() % 20));
527 /* Select new colors for the various objects
529 color0[0] = bp->colors0[bp->ccolor].red / 65536.0;
530 color0[1] = bp->colors0[bp->ccolor].green / 65536.0;
531 color0[2] = bp->colors0[bp->ccolor].blue / 65536.0;
533 color1[0] = bp->colors1[bp->ccolor].red / 65536.0;
534 color1[1] = bp->colors1[bp->ccolor].green / 65536.0;
535 color1[2] = bp->colors1[bp->ccolor].blue / 65536.0;
537 color2[0] = bp->colors2[bp->ccolor].red / 65536.0;
538 color2[1] = bp->colors2[bp->ccolor].green / 65536.0;
539 color2[2] = bp->colors2[bp->ccolor].blue / 65536.0;
541 color3[0] = bp->colors3[bp->ccolor].red / 65536.0;
542 color3[1] = bp->colors3[bp->ccolor].green / 65536.0;
543 color3[2] = bp->colors3[bp->ccolor].blue / 65536.0;
546 if (bp->ccolor >= bp->ncolors) bp->ccolor = 0;
549 get_position (bp->rot, &px, &py, &pz, !bp->button_down_p);
550 get_rotation (bp->rot, &rx, &ry, &rz, !bp->button_down_p);
559 /* Generate scene_dlist1, which contains the box (not spikes),
560 rotated into position.
562 glNewList (bp->scene_dlist1, GL_COMPILE);
564 glMatrixMode (GL_MODELVIEW);
566 glTranslatef (px, py, pz);
567 gltrackball_rotate (bp->trackball);
568 glRotatef (rx, 1.0, 0.0, 0.0);
569 glRotatef (ry, 0.0, 1.0, 0.0);
570 glRotatef (rz, 0.0, 0.0, 1.0);
572 glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, spec);
574 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color0);
575 glCallList (bp->obj_dlist0);
577 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color1);
578 glCallList (bp->obj_dlist1);
580 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color2);
581 glCallList (bp->obj_dlist2);
588 /* Generate scene_dlist2, which contains the spikes (not box),
589 rotated into position.
591 glNewList (bp->scene_dlist2, GL_COMPILE);
593 glMatrixMode (GL_MODELVIEW);
595 glTranslatef (px, py, pz);
596 gltrackball_rotate (bp->trackball);
597 glRotatef (rx, 1.0, 0.0, 0.0);
598 glRotatef (ry, 0.0, 1.0, 0.0);
599 glRotatef (rz, 0.0, 0.0, 1.0);
601 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color3);
602 glCallList (bp->obj_dlist3);
609 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
611 render_scene_to_texture (mi);
613 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
615 if (bp->show_cube_p || bp->button_down_p)
617 glCallList (bp->scene_dlist1);
618 extra_polys += bp->scene_polys1;
620 if (bp->show_spikes_p || bp->button_down_p)
622 glCallList (bp->scene_dlist2);
623 extra_polys += bp->scene_polys2;
626 overlay_blur_texture (mi);
627 mi->polygon_count += extra_polys;
631 if (mi->fps_p) do_fps (mi);
634 glXSwapBuffers(dpy, window);