1 /* glblur --- radial blur using GL textures
2 * Copyright (c) 2002-2004 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 #define DEFAULTS "*delay: 10000 \n" \
26 "*showFPS: False \n" \
29 # define refresh_glblur 0
30 # define release_glblur 0
32 #define countof(x) (sizeof((x))/sizeof((*x)))
35 #define ABS(n) ((n)<0?-(n):(n))
37 #define SIGNOF(n) ((n)<0?-1:1)
39 #include "xlockmore.h"
42 #include "gltrackball.h"
45 #ifdef USE_GL /* whole file */
47 #define DEF_SPIN "XYZ"
48 #define DEF_WANDER "True"
49 #define DEF_BLURSIZE "15"
51 typedef struct metaball metaball;
55 GLXContext *glx_context;
57 trackball_state *trackball;
60 GLuint obj_dlist0; /* east-west cube faces */
61 GLuint obj_dlist1; /* north-south cube faces */
62 GLuint obj_dlist2; /* up-down cube faces */
63 GLuint obj_dlist3; /* spikes coming out of the cube's corners */
64 GLuint scene_dlist1; /* the cube, rotated and translated */
65 GLuint scene_dlist2; /* the spikes, rotated and translated */
66 int scene_polys1; /* polygons in scene, not counting texture overlay */
67 int scene_polys2; /* polygons in scene, not counting texture overlay */
70 unsigned int *tex_data;
83 } glblur_configuration;
85 static glblur_configuration *bps = NULL;
88 static Bool do_wander;
91 static XrmOptionDescRec opts[] = {
92 { "-spin", ".spin", XrmoptionSepArg, 0 },
93 { "+spin", ".spin", XrmoptionNoArg, "" },
94 { "-blursize", ".blurSize", XrmoptionSepArg, 0 },
95 { "-wander", ".wander", XrmoptionNoArg, "True" },
96 { "+wander", ".wander", XrmoptionNoArg, "False" },
99 static argtype vars[] = {
100 {&do_spin, "spin", "Spin", DEF_SPIN, t_String},
101 {&do_wander, "wander", "Wander", DEF_WANDER, t_Bool},
102 {&blursize, "blurSize","BlurSize", DEF_BLURSIZE, t_Int},
105 ENTRYPOINT ModeSpecOpt glblur_opts = {countof(opts), opts, countof(vars), vars, NULL};
108 /* Window management, etc
111 reshape_glblur (ModeInfo *mi, int width, int height)
113 GLfloat h = (GLfloat) height / (GLfloat) width;
115 glViewport (0, 0, (GLint) width, (GLint) height);
117 glMatrixMode(GL_PROJECTION);
119 gluPerspective (30.0, 1/h, 1.0, 100.0);
121 glMatrixMode(GL_MODELVIEW);
123 gluLookAt( 0.0, 0.0, 8.0,
127 glClear(GL_COLOR_BUFFER_BIT);
132 /* Objects in the scene
136 generate_object (ModeInfo *mi)
138 glblur_configuration *bp = &bps[MI_SCREEN(mi)];
139 Bool wire = MI_IS_WIREFRAME (mi);
142 bp->scene_polys1 = 0;
143 bp->scene_polys2 = 0;
145 glNewList (bp->obj_dlist0, GL_COMPILE);
146 glBegin (wire ? GL_LINE_LOOP : GL_QUADS); /* front */
147 glNormal3f (0, 0, 1);
148 glTexCoord2f(1, 0); glVertex3f ( 0.5, -0.5, 0.5);
149 glTexCoord2f(0, 0); glVertex3f ( 0.5, 0.5, 0.5);
150 glTexCoord2f(0, 1); glVertex3f (-0.5, 0.5, 0.5);
151 glTexCoord2f(1, 1); glVertex3f (-0.5, -0.5, 0.5);
155 glBegin (wire ? GL_LINE_LOOP : GL_QUADS); /* back */
156 glNormal3f (0, 0, -1);
157 glTexCoord2f(0, 0); glVertex3f (-0.5, -0.5, -0.5);
158 glTexCoord2f(0, 1); glVertex3f (-0.5, 0.5, -0.5);
159 glTexCoord2f(1, 1); glVertex3f ( 0.5, 0.5, -0.5);
160 glTexCoord2f(1, 0); glVertex3f ( 0.5, -0.5, -0.5);
165 glNewList (bp->obj_dlist1, GL_COMPILE);
166 glBegin (wire ? GL_LINE_LOOP : GL_QUADS); /* left */
167 glNormal3f (-1, 0, 0);
168 glTexCoord2f(1, 1); glVertex3f (-0.5, 0.5, 0.5);
169 glTexCoord2f(1, 0); glVertex3f (-0.5, 0.5, -0.5);
170 glTexCoord2f(0, 0); glVertex3f (-0.5, -0.5, -0.5);
171 glTexCoord2f(0, 1); glVertex3f (-0.5, -0.5, 0.5);
175 glBegin (wire ? GL_LINE_LOOP : GL_QUADS); /* right */
176 glNormal3f (1, 0, 0);
177 glTexCoord2f(1, 1); glVertex3f ( 0.5, -0.5, -0.5);
178 glTexCoord2f(1, 0); glVertex3f ( 0.5, 0.5, -0.5);
179 glTexCoord2f(0, 0); glVertex3f ( 0.5, 0.5, 0.5);
180 glTexCoord2f(0, 1); glVertex3f ( 0.5, -0.5, 0.5);
185 glNewList (bp->obj_dlist2, GL_COMPILE);
186 glBegin (wire ? GL_LINE_LOOP : GL_QUADS); /* top */
187 glNormal3f (0, 1, 0);
188 glTexCoord2f(0, 0); glVertex3f ( 0.5, 0.5, 0.5);
189 glTexCoord2f(0, 1); glVertex3f ( 0.5, 0.5, -0.5);
190 glTexCoord2f(1, 1); glVertex3f (-0.5, 0.5, -0.5);
191 glTexCoord2f(1, 0); glVertex3f (-0.5, 0.5, 0.5);
195 glBegin (wire ? GL_LINE_LOOP : GL_QUADS); /* bottom */
196 glNormal3f (0, -1, 0);
197 glTexCoord2f(1, 0); glVertex3f (-0.5, -0.5, 0.5);
198 glTexCoord2f(0, 0); glVertex3f (-0.5, -0.5, -0.5);
199 glTexCoord2f(0, 1); glVertex3f ( 0.5, -0.5, -0.5);
200 glTexCoord2f(1, 1); glVertex3f ( 0.5, -0.5, 0.5);
205 glNewList (bp->obj_dlist3, GL_COMPILE);
208 glVertex3f(-s, 0, 0); glVertex3f(s, 0, 0); /* face spikes */
209 glVertex3f(0, -s, 0); glVertex3f(0, s, 0);
210 glVertex3f(0, 0, -s); glVertex3f(0, 0, s);
211 bp->scene_polys2 += 3;
216 glVertex3f(-s, -s, -s); glVertex3f( s, s, s); /* corner spikes */
217 glVertex3f(-s, -s, s); glVertex3f( s, s, -s);
218 glVertex3f(-s, s, -s); glVertex3f( s, -s, s);
219 glVertex3f( s, -s, -s); glVertex3f(-s, s, s);
220 bp->scene_polys2 += 4;
224 check_gl_error ("object generation");
229 init_texture (ModeInfo *mi)
231 glblur_configuration *bp = &bps[MI_SCREEN(mi)];
233 if (bp->tex_data) free (bp->tex_data);
237 bp->tex_data = (unsigned int *)
238 malloc (bp->tex_w * bp->tex_h * 4 * sizeof (unsigned int));
240 glGenTextures (1, &bp->texture);
241 glBindTexture (GL_TEXTURE_2D, bp->texture);
242 glTexImage2D (GL_TEXTURE_2D, 0, 4, 128, 128, 0,
244 /* GL_UNSIGNED_BYTE, */
245 GL_UNSIGNED_INT_8_8_8_8_REV,
247 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
248 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
253 render_scene_to_texture (ModeInfo *mi)
255 glblur_configuration *bp = &bps[MI_SCREEN(mi)];
257 glViewport (0, 0, bp->tex_w, bp->tex_h);
259 glCallList (bp->scene_dlist1);
260 glCallList (bp->scene_dlist2);
262 glBindTexture (GL_TEXTURE_2D, bp->texture);
263 glCopyTexImage2D (GL_TEXTURE_2D, 0, GL_LUMINANCE, 0, 0,
264 bp->tex_w, bp->tex_h, 0);
265 check_gl_error ("texture");
267 glViewport (0, 0, MI_WIDTH(mi), MI_HEIGHT(mi));
271 overlay_blur_texture (ModeInfo *mi)
273 glblur_configuration *bp = &bps[MI_SCREEN(mi)];
274 int w = MI_WIDTH (mi);
275 int h = MI_HEIGHT (mi);
276 int times = blursize;
278 GLfloat inc = 0.02 * (25.0 / times);
280 GLfloat spost = 0; /* starting texture coordinate offset */
281 GLfloat alpha_inc = 0.9 / times; /* transparency fade factor */
282 GLfloat alpha = 0.2; /* initial transparency */
284 glDisable (GL_TEXTURE_GEN_S);
285 glDisable (GL_TEXTURE_GEN_T);
287 glEnable (GL_TEXTURE_2D);
288 glDisable (GL_DEPTH_TEST);
289 glBlendFunc (GL_SRC_ALPHA,GL_ONE);
291 glBindTexture (GL_TEXTURE_2D, bp->texture);
294 /* switch to orthographic projection, saving both previous matrixes
295 on their respective stacks.
297 glMatrixMode (GL_PROJECTION);
300 glOrtho (0, w, h, 0, -1, 1);
301 glMatrixMode (GL_MODELVIEW);
306 alpha_inc = alpha / times;
308 mi->polygon_count = bp->scene_polys1 + bp->scene_polys2;
311 for (i = 0; i < times; i++)
313 glColor4f (1, 1, 1, alpha);
314 glTexCoord2f (0+spost, 1-spost); glVertex2f (0, 0);
315 glTexCoord2f (0+spost, 0+spost); glVertex2f (0, h);
316 glTexCoord2f (1-spost, 0+spost); glVertex2f (w, h);
317 glTexCoord2f (1-spost, 1-spost); glVertex2f (w, 0);
324 /* Switch back to perspective projection, restoring the saved matrixes
326 glMatrixMode (GL_PROJECTION);
328 glMatrixMode (GL_MODELVIEW);
331 glEnable (GL_DEPTH_TEST);
332 glDisable (GL_BLEND);
333 glDisable (GL_TEXTURE_2D);
334 glBindTexture (GL_TEXTURE_2D, 0);
339 /* Startup initialization
343 glblur_handle_event (ModeInfo *mi, XEvent *event)
345 glblur_configuration *bp = &bps[MI_SCREEN(mi)];
347 if (event->xany.type == ButtonPress &&
348 event->xbutton.button == Button1)
350 bp->button_down_p = True;
351 gltrackball_start (bp->trackball,
352 event->xbutton.x, event->xbutton.y,
353 MI_WIDTH (mi), MI_HEIGHT (mi));
356 else if (event->xany.type == ButtonRelease &&
357 event->xbutton.button == Button1)
359 bp->button_down_p = False;
362 else if (event->xany.type == ButtonPress &&
363 (event->xbutton.button == Button4 ||
364 event->xbutton.button == Button5))
366 gltrackball_mousewheel (bp->trackball, event->xbutton.button, 10,
367 !!event->xbutton.state);
370 else if (event->xany.type == MotionNotify &&
373 gltrackball_track (bp->trackball,
374 event->xmotion.x, event->xmotion.y,
375 MI_WIDTH (mi), MI_HEIGHT (mi));
384 init_glblur (ModeInfo *mi)
386 glblur_configuration *bp;
387 int wire = MI_IS_WIREFRAME(mi);
390 bps = (glblur_configuration *)
391 calloc (MI_NUM_SCREENS(mi), sizeof (glblur_configuration));
393 fprintf(stderr, "%s: out of memory\n", progname);
398 bp = &bps[MI_SCREEN(mi)];
400 bp->glx_context = init_GL(mi);
402 reshape_glblur (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
406 GLfloat gamb[4]= {0.2, 0.2, 0.2, 1.0};
407 GLfloat pos[4] = {0.0, 5.0, 10.0, 1.0};
408 GLfloat amb[4] = {0.2, 0.2, 0.2, 1.0};
409 GLfloat dif[4] = {0.3, 0.3, 0.3, 1.0};
410 GLfloat spc[4] = {0.8, 0.8, 0.8, 1.0};
413 glEnable(GL_LIGHTING);
416 glEnable(GL_DEPTH_TEST);
417 glEnable(GL_CULL_FACE);
418 glEnable(GL_NORMALIZE);
419 glShadeModel(GL_SMOOTH);
421 glLightModelfv (GL_LIGHT_MODEL_AMBIENT, gamb);
423 glLightfv(GL_LIGHT0, GL_POSITION, pos);
424 glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
425 glLightfv(GL_LIGHT0, GL_DIFFUSE, dif);
426 glLightfv(GL_LIGHT0, GL_SPECULAR, spc);
428 glEnable(GL_LIGHTING);
431 glMateriali(GL_FRONT, GL_SHININESS, shiny);
435 Bool spinx=False, spiny=False, spinz=False;
436 double spin_speed = 0.9;
437 double wander_speed = 0.06;
442 if (*s == 'x' || *s == 'X') spinx = True;
443 else if (*s == 'y' || *s == 'Y') spiny = True;
444 else if (*s == 'z' || *s == 'Z') spinz = True;
445 else if (*s == '0') ;
449 "%s: spin must contain only the characters X, Y, or Z (not \"%s\")\n",
456 bp->rot = make_rotator (spinx ? spin_speed : 0,
457 spiny ? spin_speed : 0,
458 spinz ? spin_speed : 0,
460 do_wander ? wander_speed : 0,
462 bp->trackball = gltrackball_init ();
465 if (blursize < 0) blursize = 0;
466 if (blursize > 200) blursize = 200;
469 bp->colors0 = (XColor *) calloc(bp->ncolors, sizeof(XColor));
470 bp->colors1 = (XColor *) calloc(bp->ncolors, sizeof(XColor));
471 bp->colors2 = (XColor *) calloc(bp->ncolors, sizeof(XColor));
472 bp->colors3 = (XColor *) calloc(bp->ncolors, sizeof(XColor));
473 make_smooth_colormap (0, 0, 0, bp->colors0, &bp->ncolors, False, 0, False);
474 make_smooth_colormap (0, 0, 0, bp->colors1, &bp->ncolors, False, 0, False);
475 make_smooth_colormap (0, 0, 0, bp->colors2, &bp->ncolors, False, 0, False);
476 make_smooth_colormap (0, 0, 0, bp->colors3, &bp->ncolors, False, 0, False);
479 bp->obj_dlist0 = glGenLists (1);
480 bp->obj_dlist1 = glGenLists (1);
481 bp->obj_dlist2 = glGenLists (1);
482 bp->obj_dlist3 = glGenLists (1);
483 bp->scene_dlist1 = glGenLists (1);
484 bp->scene_dlist2 = glGenLists (1);
487 generate_object (mi);
494 draw_glblur (ModeInfo *mi)
496 glblur_configuration *bp = &bps[MI_SCREEN(mi)];
497 Display *dpy = MI_DISPLAY(mi);
498 Window window = MI_WINDOW(mi);
500 GLfloat color0[4] = {0.0, 0.0, 0.0, 1.0};
501 GLfloat color1[4] = {0.0, 0.0, 0.0, 1.0};
502 GLfloat color2[4] = {0.0, 0.0, 0.0, 1.0};
503 GLfloat color3[4] = {0.0, 0.0, 0.0, 1.0};
504 GLfloat spec[4] = {1.0, 1.0, 1.0, 1.0};
510 if (!bp->glx_context)
513 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(bp->glx_context));
515 /* Decide what we're drawing
517 if (0 == (random() % 30))
519 bp->show_cube_p = (0 == (random() % 10));
520 bp->show_spikes_p = (0 == (random() % 20));
523 /* Select new colors for the various objects
525 color0[0] = bp->colors0[bp->ccolor].red / 65536.0;
526 color0[1] = bp->colors0[bp->ccolor].green / 65536.0;
527 color0[2] = bp->colors0[bp->ccolor].blue / 65536.0;
529 color1[0] = bp->colors1[bp->ccolor].red / 65536.0;
530 color1[1] = bp->colors1[bp->ccolor].green / 65536.0;
531 color1[2] = bp->colors1[bp->ccolor].blue / 65536.0;
533 color2[0] = bp->colors2[bp->ccolor].red / 65536.0;
534 color2[1] = bp->colors2[bp->ccolor].green / 65536.0;
535 color2[2] = bp->colors2[bp->ccolor].blue / 65536.0;
537 color3[0] = bp->colors3[bp->ccolor].red / 65536.0;
538 color3[1] = bp->colors3[bp->ccolor].green / 65536.0;
539 color3[2] = bp->colors3[bp->ccolor].blue / 65536.0;
542 if (bp->ccolor >= bp->ncolors) bp->ccolor = 0;
545 get_position (bp->rot, &px, &py, &pz, !bp->button_down_p);
546 get_rotation (bp->rot, &rx, &ry, &rz, !bp->button_down_p);
555 /* Generate scene_dlist1, which contains the box (not spikes),
556 rotated into position.
558 glNewList (bp->scene_dlist1, GL_COMPILE);
560 glMatrixMode (GL_MODELVIEW);
562 glTranslatef (px, py, pz);
563 gltrackball_rotate (bp->trackball);
564 glRotatef (rx, 1.0, 0.0, 0.0);
565 glRotatef (ry, 0.0, 1.0, 0.0);
566 glRotatef (rz, 0.0, 0.0, 1.0);
568 glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, spec);
570 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color0);
572 glCallList (bp->obj_dlist0);
574 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color1);
575 glCallList (bp->obj_dlist1);
577 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color2);
578 glCallList (bp->obj_dlist2);
585 /* Generate scene_dlist2, which contains the spikes (not box),
586 rotated into position.
588 glNewList (bp->scene_dlist2, GL_COMPILE);
590 glMatrixMode (GL_MODELVIEW);
592 glTranslatef (px, py, pz);
593 gltrackball_rotate (bp->trackball);
594 glRotatef (rx, 1.0, 0.0, 0.0);
595 glRotatef (ry, 0.0, 1.0, 0.0);
596 glRotatef (rz, 0.0, 0.0, 1.0);
598 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color3);
599 glCallList (bp->obj_dlist3);
606 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
608 render_scene_to_texture (mi);
610 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
612 if (bp->show_cube_p || bp->button_down_p)
614 glCallList (bp->scene_dlist1);
615 extra_polys += bp->scene_polys1;
617 if (bp->show_spikes_p || bp->button_down_p)
619 glCallList (bp->scene_dlist2);
620 extra_polys += bp->scene_polys2;
623 overlay_blur_texture (mi);
624 mi->polygon_count += extra_polys;
628 if (mi->fps_p) do_fps (mi);
631 glXSwapBuffers(dpy, window);
634 XSCREENSAVER_MODULE ("GLBlur", glblur)