1 /* blinkbox, Copyright (c) 2003 Jeremy English <jenglish@myself.com>
3 * Permission to use, copy, modify, distribute, and sell this software and its
4 * documentation for any purpose is hereby granted without fee, provided that
5 * the above copyright notice appear in all copies and that both that
6 * copyright notice and this permission notice appear in supporting
7 * documentation. No representations are made about the suitability of this
8 * software for any purpose. It is provided "as is" without express or
12 /* motion blur added March 2005 by John Boero <jlboero@cs.uwm.edu>
15 #define DEFAULTS "*delay: 30000 \n" \
16 "*wireframe: False \n" \
18 # define refresh_ball 0
19 # define release_ball 0
20 # define ball_handle_event 0
22 #define countof(x) (sizeof((x))/sizeof((*x)))
24 #include "xlockmore.h"
28 #ifdef USE_GL /* whole file */
31 #define ALPHA_AMT 0.05
33 /* this should be between 1 and 8 */
35 #define DEF_DISSOLVE "False"
36 #define DEF_FADE "True"
37 #define DEF_BLUR "True"
67 GLfloat wh; /*width Height*/
71 static const struct Bounding_box bbox = {{14,14,20},{-14,-14,-20}};
74 GLXContext *glx_context;
81 Tdpos moh; /*hold motion value*/
100 static blinkboxstruct *blinkbox = (blinkboxstruct *) NULL;
104 static const float LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f };
105 static const float LightPosition[]= { 20.0f, 100.0f, 20.0f, 1.0f };
107 static Bool do_dissolve;
110 static float bscale_wh;
112 static XrmOptionDescRec opts[] = {
113 { "-boxsize", ".boxsize", XrmoptionSepArg, 0 },
114 { "-dissolve", ".dissolve", XrmoptionNoArg, "True" },
115 { "+dissolve", ".dissolve", XrmoptionNoArg, "False" },
116 { "-fade", ".fade", XrmoptionNoArg, "True" },
117 { "+fade", ".fade", XrmoptionNoArg, "False" },
118 { "-blur", ".blur", XrmoptionNoArg, "True" },
119 { "+blur", ".blur", XrmoptionNoArg, "False" }
123 static argtype vars[] = {
124 {&bscale_wh, "boxsize", "Boxsize", DEF_WH, t_Float},
125 {&do_dissolve, "dissolve", "Dissolve", DEF_DISSOLVE, t_Bool},
126 {&do_fade, "fade", "Fade", DEF_FADE, t_Bool},
127 {&do_blur, "blur", "Blur", DEF_BLUR, t_Bool},
130 ENTRYPOINT ModeSpecOpt ball_opts = {countof(opts), opts, countof(vars), vars, NULL};
133 swap(GLfloat *a, GLfloat *b)
143 GLfloat j = 1+(random() % 2);
148 swap_mov(GLfloat *a, GLfloat *b)
160 cp_b_pos(blinkboxstruct *bp, Tdpos *s_pos)
162 s_pos->x = bp->ball.x;
163 s_pos->y = bp->ball.y;
164 s_pos->z = bp->ball.z;
168 hit_side(blinkboxstruct *bp)
170 if ((bp->ball.x - bp->ball.d) <= bbox.bottom.x){
172 bp->lside.counter = MAX_COUNT;
173 bp->lside.des_count = 1;
174 bp->lside.alpha_count = 0;
175 cp_b_pos(bp, &bp->lside.pos);
176 swap_mov(&bp->mo.x,&bp->moh.x);
178 if ((bp->ball.x + bp->ball.d) >= bbox.top.x){
180 bp->rside.counter = MAX_COUNT;
181 bp->rside.des_count = 1;
182 bp->rside.alpha_count = 0;
183 cp_b_pos(bp, &bp->rside.pos);
184 swap_mov(&bp->mo.x,&bp->moh.x);
189 hit_top_bottom(blinkboxstruct *bp)
191 if ((bp->ball.y - bp->ball.d) <= bbox.bottom.y){
193 bp->bside.counter = MAX_COUNT;
194 bp->bside.des_count = 1;
195 bp->bside.alpha_count = 0;
196 cp_b_pos(bp, &bp->bside.pos);
197 swap_mov(&bp->mo.y,&bp->moh.y);
199 if ((bp->ball.y + bp->ball.d) >= bbox.top.y){
201 bp->tside.counter = MAX_COUNT;
202 bp->tside.des_count = 1;
203 bp->tside.alpha_count = 0;
204 cp_b_pos(bp, &bp->tside.pos);
205 swap_mov(&bp->mo.y,&bp->moh.y);
210 hit_front_back(blinkboxstruct *bp)
212 if ((bp->ball.z - bp->ball.d) <= bbox.bottom.z){
214 bp->aside.counter = MAX_COUNT;
215 bp->aside.des_count = 1;
216 bp->aside.alpha_count = 0;
217 cp_b_pos(bp, &bp->aside.pos);
218 swap_mov(&bp->mo.z,&bp->moh.z);
220 if((bp->ball.z + bp->ball.d) >= bbox.top.z){
222 bp->fside.counter = MAX_COUNT;
223 bp->fside.des_count = 1;
224 bp->fside.alpha_count = 0;
225 cp_b_pos(bp, &bp->fside.pos);
226 swap_mov(&bp->mo.z,&bp->moh.z);
231 reshape_ball (ModeInfo *mi, int width, int height)
233 GLfloat h = (GLfloat) height / (GLfloat) width;
235 glViewport (0, 0, (GLint) width, (GLint) height);
236 glMatrixMode(GL_PROJECTION);
238 gluPerspective (30.0, 1/h, 1.0, 100.0);
240 glMatrixMode(GL_MODELVIEW);
242 gluLookAt( 0.0, 0.0, 40.0,
251 glBegin((wire)?GL_LINE_LOOP:GL_QUADS);
252 glNormal3f( 0.0f, -1.0f, 0.0f);
253 glVertex3f(-1.0f, -1.0f, -1.0f);
254 glVertex3f( 1.0f, -1.0f, -1.0f);
255 glVertex3f( 1.0f, -1.0f, 1.0f);
256 glVertex3f(-1.0f, -1.0f, 1.0f);
257 glNormal3f( 0.0f, 0.0f, 1.0f);
258 glVertex3f(-1.0f, -1.0f, 1.0f);
259 glVertex3f( 1.0f, -1.0f, 1.0f);
260 glVertex3f( 1.0f, 1.0f, 1.0f);
261 glVertex3f(-1.0f, 1.0f, 1.0f);
262 glNormal3f( 0.0f, 0.0f, -1.0f);
263 glVertex3f(-1.0f, -1.0f, -1.0f);
264 glVertex3f(-1.0f, 1.0f, -1.0f);
265 glVertex3f( 1.0f, 1.0f, -1.0f);
266 glVertex3f( 1.0f, -1.0f, -1.0f);
267 glNormal3f( 1.0f, 0.0f, 0.0f);
268 glVertex3f( 1.0f, -1.0f, -1.0f);
269 glVertex3f( 1.0f, 1.0f, -1.0f);
270 glVertex3f( 1.0f, 1.0f, 1.0f);
271 glVertex3f( 1.0f, -1.0f, 1.0f);
272 glNormal3f( -1.0f, 0.0f, 0.0f);
273 glVertex3f(-1.0f, -1.0f, -1.0f);
274 glVertex3f(-1.0f, -1.0f, 1.0f);
275 glVertex3f(-1.0f, 1.0f, 1.0f);
276 glVertex3f(-1.0f, 1.0f, -1.0f);
277 glNormal3f( 1.0f, 1.0f, 0.0f);
278 glVertex3f(-1.0f, 1.0f, -1.0f);
279 glVertex3f(-1.0f, 1.0f, 1.0f);
280 glVertex3f( 1.0f, 1.0f, 1.0f);
281 glVertex3f( 1.0f, 1.0f, -1.0f);
286 init_ball (ModeInfo *mi)
288 int wire = MI_IS_WIREFRAME(mi);
291 if(blinkbox == NULL) {
292 if((blinkbox = (blinkboxstruct *) calloc(MI_NUM_SCREENS(mi),
293 sizeof (blinkboxstruct))) == NULL)
296 bp = &blinkbox[MI_SCREEN(mi)];
298 if ((bp->glx_context = init_GL(mi)) != NULL) {
299 reshape_ball(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
300 glDrawBuffer(GL_BACK);
306 bp->bscale.wh = bscale_wh;
323 bp->lside.counter = MAX_COUNT;
324 bp->rside.counter = MAX_COUNT;
325 bp->tside.counter = MAX_COUNT;
326 bp->bside.counter = MAX_COUNT;
327 bp->fside.counter = MAX_COUNT;
328 bp->aside.counter = MAX_COUNT;
330 bp->lside.color[0] = 1;
331 bp->rside.color[1] = 1;
332 bp->tside.color[2] = 1;
334 bp->bside.color[0] = 1;
335 bp->bside.color[1] = 0.5;
337 bp->fside.color[0] = 1;
338 bp->fside.color[1] = 1;
340 bp->aside.color[0] = 0.5;
341 bp->aside.color[2] = 1;
343 bp->lside.rot[0] = 90;
344 bp->rside.rot[0] = 90;
345 bp->tside.rot[0] = 90;
346 bp->bside.rot[0] = 90;
347 bp->fside.rot[0] = 90;
348 bp->aside.rot[0] = 90;
350 bp->lside.rot[2] = 1;
351 bp->rside.rot[2] = 1;
352 bp->tside.rot[1] = 1;
353 bp->bside.rot[1] = 1;
354 bp->fside.rot[3] = 1;
355 bp->aside.rot[3] = 1;
357 bp->lside.des_count = 1;
358 bp->rside.des_count = 1;
359 bp->tside.des_count = 1;
360 bp->bside.des_count = 1;
361 bp->fside.des_count = 1;
362 bp->aside.des_count = 1;
364 bp->lside.alpha_count = 1;
365 bp->rside.alpha_count = 1;
366 bp->tside.alpha_count = 1;
367 bp->bside.alpha_count = 1;
368 bp->fside.alpha_count = 1;
369 bp->aside.alpha_count = 1;
372 #define SPHERE_SLICES 12 /* how densely to render spheres */
373 #define SPHERE_STACKS 16
375 bp->sp = malloc(sizeof(*bp->sp));
377 fprintf(stderr,"Could not allocate memory\n");
380 if( (bp->bscale.wh < 1) ||
381 (bp->bscale.wh > 8) ) {
382 fprintf(stderr,"Boxsize out of range. Using default\n");
386 bp->des_amt = bp->bscale.wh / MAX_COUNT;
389 bp->glx_context = init_GL(mi);
391 reshape_ball(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
392 bp->ballList = glGenLists(1);
393 glNewList(bp->ballList, GL_COMPILE);
394 unit_sphere (SPHERE_STACKS, SPHERE_SLICES, wire);
397 bp->boxList = glGenLists(1);
398 glNewList(bp->boxList, GL_COMPILE);
404 glEnable(GL_COLOR_MATERIAL);
405 glShadeModel(GL_SMOOTH);
406 glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
408 glEnable(GL_DEPTH_TEST);
409 glDepthFunc(GL_LEQUAL);
410 glEnable(GL_LIGHTING);
412 glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
413 glLightfv(GL_LIGHT1, GL_POSITION,LightPosition);
415 if (do_fade || do_blur) {
417 glDisable(GL_DEPTH_TEST);
422 CheckBoxPos(blinkboxstruct *bp,
423 GLfloat bot_x, GLfloat top_x, GLfloat bot_y, GLfloat top_y)
425 /*Make sure it's inside of the bounding box*/
426 bp->bpos.x = ((bp->bpos.x - bp->bscale.wh) < bot_x) ? bot_x + bp->bscale.wh : bp->bpos.x;
427 bp->bpos.x = ((bp->bpos.x + bp->bscale.wh) > top_x) ? top_x - bp->bscale.wh : bp->bpos.x;
428 bp->bpos.y = ((bp->bpos.y - bp->bscale.wh) < bot_y) ? bot_y + bp->bscale.wh : bp->bpos.y;
429 bp->bpos.y = ((bp->bpos.y + bp->bscale.wh) > top_y) ? top_y - bp->bscale.wh : bp->bpos.y;
433 draw_ball (ModeInfo *mi)
435 blinkboxstruct *bp = &blinkbox[MI_SCREEN(mi)];
437 Display *dpy = MI_DISPLAY(mi);
438 Window window = MI_WINDOW(mi);
441 if (! bp->glx_context)
443 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(bp->glx_context));
445 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
451 glRotated(0.25,0,0,1);
452 glRotated(0.25,0,1,0);
453 glRotated(0.25,1,0,0);
457 glScalef(0.5,0.5,0.5);
462 if (!do_blur || MI_IS_WIREFRAME(mi)) {
463 glTranslatef(bp->ball.x += bp->mo.x,
464 bp->ball.y += bp->mo.y,
465 bp->ball.z += bp->mo.z);
468 glCallList(bp->ballList);
472 # define blur_detail 24.0
473 float ball_alpha = 1 / blur_detail;
475 glBlendFunc(GL_SRC_ALPHA,GL_ONE);
476 glTranslatef(bp->ball.x, bp->ball.y, bp->ball.z);
478 for (i = 0; i < blur_detail; ++i) {
479 glTranslatef(bp->mo.x / blur_detail,
480 bp->mo.y / blur_detail,
481 bp->mo.z / blur_detail);
483 /* comment the following line for quick but boring linear blur */
484 ball_alpha = sin((M_PI / blur_detail) * i) / blur_detail;
486 glColor4f(1, 1, 1, ball_alpha);
489 glCallList(bp->ballList);
490 glScalef(.5, .5, .5);
494 bp->ball.x += bp->mo.x;
495 bp->ball.y += bp->mo.y;
496 bp->ball.z += bp->mo.z;
505 bp->bpos.x = bp->lside.pos.z*-1;
506 bp->bpos.y = bp->lside.pos.y;
507 bp->bpos.z = bbox.bottom.x - bp->bscale.d;
509 CheckBoxPos(bp, bbox.bottom.z,bbox.top.z,bbox.bottom.y,bbox.top.y);
514 bp->bpos.x = bp->rside.pos.z*-1;
515 bp->bpos.y = bp->rside.pos.y;
516 bp->bpos.z = bbox.top.x + bp->bscale.d;
518 CheckBoxPos(bp, bbox.bottom.z,bbox.top.z,bbox.bottom.y,bbox.top.y);
523 bp->bpos.x = bp->tside.pos.x;
524 bp->bpos.y = bp->tside.pos.z;
525 bp->bpos.z = bbox.bottom.y - bp->bscale.d;
527 CheckBoxPos(bp, bbox.bottom.x,bbox.top.x,bbox.bottom.z,bbox.top.z);
532 bp->bpos.x = bp->bside.pos.x;
533 bp->bpos.y = bp->bside.pos.z;
534 bp->bpos.z = bbox.top.y + bp->bscale.d;
536 CheckBoxPos(bp, bbox.bottom.x,bbox.top.x,bbox.bottom.z,bbox.top.z);
541 bp->bpos.x = bp->fside.pos.y;
542 bp->bpos.y = bp->fside.pos.x*-1;
543 bp->bpos.z = bbox.top.z + bp->bscale.d;
545 CheckBoxPos(bp, bbox.bottom.y,bbox.top.y,bbox.bottom.x,bbox.top.x);
550 bp->bpos.x = bp->aside.pos.y;
551 bp->bpos.y = bp->aside.pos.x*-1;
552 bp->bpos.z = bbox.bottom.z + bp->bscale.d;
554 CheckBoxPos(bp, bbox.bottom.y,bbox.top.y,bbox.bottom.x,bbox.top.x);
560 glColor4f(bp->sp->color[0],bp->sp->color[1],bp->sp->color[2],1-(ALPHA_AMT * bp->sp->alpha_count));
562 glColor3fv(bp->sp->color);
564 glBlendFunc(GL_SRC_ALPHA,GL_ONE);
566 glRotatef(bp->sp->rot[0],bp->sp->rot[1],bp->sp->rot[2],bp->sp->rot[3]);
567 glTranslatef(bp->bpos.x,bp->bpos.y,bp->bpos.z);
569 glScalef(bp->bscale.wh-(bp->des_amt*bp->sp->des_count),bp->bscale.wh-(bp->des_amt*bp->sp->des_count),bp->bscale.d);
571 glScalef(bp->bscale.wh,bp->bscale.wh,bp->bscale.d);
573 glCallList(bp->boxList);
577 bp->sp->alpha_count++;
589 glXSwapBuffers(dpy, window);
593 XSCREENSAVER_MODULE_2 ("BlinkBox", blinkbox, ball)