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" \
17 "*suppressRotationAnimation: True\n" \
19 # define refresh_ball 0
20 # define release_ball 0
21 # define ball_handle_event 0
23 #define countof(x) (sizeof((x))/sizeof((*x)))
25 #include "xlockmore.h"
29 #ifdef USE_GL /* whole file */
32 #define ALPHA_AMT 0.05
34 /* this should be between 1 and 8 */
35 #define DEF_BOXSIZE "2"
36 #define DEF_DISSOLVE "False"
37 #define DEF_FADE "True"
38 #define DEF_BLUR "True"
68 GLfloat wh; /*width Height*/
72 static const struct Bounding_box bbox = {{14,14,20},{-14,-14,-20}};
75 GLXContext *glx_context;
82 Tdpos moh; /*hold motion value*/
101 static blinkboxstruct *blinkbox = (blinkboxstruct *) NULL;
105 static const float LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f };
106 static const float LightPosition[]= { 20.0f, 100.0f, 20.0f, 1.0f };
108 static Bool do_dissolve;
111 static float bscale_wh;
113 static XrmOptionDescRec opts[] = {
114 { "-boxsize", ".boxsize", XrmoptionSepArg, 0 },
115 { "-dissolve", ".dissolve", XrmoptionNoArg, "True" },
116 { "+dissolve", ".dissolve", XrmoptionNoArg, "False" },
117 { "-fade", ".fade", XrmoptionNoArg, "True" },
118 { "+fade", ".fade", XrmoptionNoArg, "False" },
119 { "-blur", ".blur", XrmoptionNoArg, "True" },
120 { "+blur", ".blur", XrmoptionNoArg, "False" }
124 static argtype vars[] = {
125 {&bscale_wh, "boxsize", "Boxsize", DEF_BOXSIZE, t_Float},
126 {&do_dissolve, "dissolve", "Dissolve", DEF_DISSOLVE, t_Bool},
127 {&do_fade, "fade", "Fade", DEF_FADE, t_Bool},
128 {&do_blur, "blur", "Blur", DEF_BLUR, t_Bool},
131 ENTRYPOINT ModeSpecOpt ball_opts = {countof(opts), opts, countof(vars), vars, NULL};
134 swap(GLfloat *a, GLfloat *b)
144 GLfloat j = 1+(random() % 2);
149 swap_mov(GLfloat *a, GLfloat *b)
161 cp_b_pos(blinkboxstruct *bp, Tdpos *s_pos)
163 s_pos->x = bp->ball.x;
164 s_pos->y = bp->ball.y;
165 s_pos->z = bp->ball.z;
169 hit_side(blinkboxstruct *bp)
171 if ((bp->ball.x - bp->ball.d) <= bbox.bottom.x){
173 bp->lside.counter = MAX_COUNT;
174 bp->lside.des_count = 1;
175 bp->lside.alpha_count = 0;
176 cp_b_pos(bp, &bp->lside.pos);
177 swap_mov(&bp->mo.x,&bp->moh.x);
179 if ((bp->ball.x + bp->ball.d) >= bbox.top.x){
181 bp->rside.counter = MAX_COUNT;
182 bp->rside.des_count = 1;
183 bp->rside.alpha_count = 0;
184 cp_b_pos(bp, &bp->rside.pos);
185 swap_mov(&bp->mo.x,&bp->moh.x);
190 hit_top_bottom(blinkboxstruct *bp)
192 if ((bp->ball.y - bp->ball.d) <= bbox.bottom.y){
194 bp->bside.counter = MAX_COUNT;
195 bp->bside.des_count = 1;
196 bp->bside.alpha_count = 0;
197 cp_b_pos(bp, &bp->bside.pos);
198 swap_mov(&bp->mo.y,&bp->moh.y);
200 if ((bp->ball.y + bp->ball.d) >= bbox.top.y){
202 bp->tside.counter = MAX_COUNT;
203 bp->tside.des_count = 1;
204 bp->tside.alpha_count = 0;
205 cp_b_pos(bp, &bp->tside.pos);
206 swap_mov(&bp->mo.y,&bp->moh.y);
211 hit_front_back(blinkboxstruct *bp)
213 if ((bp->ball.z - bp->ball.d) <= bbox.bottom.z){
215 bp->aside.counter = MAX_COUNT;
216 bp->aside.des_count = 1;
217 bp->aside.alpha_count = 0;
218 cp_b_pos(bp, &bp->aside.pos);
219 swap_mov(&bp->mo.z,&bp->moh.z);
221 if((bp->ball.z + bp->ball.d) >= bbox.top.z){
223 bp->fside.counter = MAX_COUNT;
224 bp->fside.des_count = 1;
225 bp->fside.alpha_count = 0;
226 cp_b_pos(bp, &bp->fside.pos);
227 swap_mov(&bp->mo.z,&bp->moh.z);
232 reshape_ball (ModeInfo *mi, int width, int height)
234 GLfloat h = (GLfloat) height / (GLfloat) width;
236 glViewport (0, 0, (GLint) width, (GLint) height);
237 glMatrixMode(GL_PROJECTION);
239 gluPerspective (30.0, 1/h, 1.0, 100.0);
241 glMatrixMode(GL_MODELVIEW);
243 gluLookAt( 0.0, 0.0, 40.0,
247 # ifdef HAVE_MOBILE /* Keep it the same relative size when rotated. */
249 int o = (int) current_device_rotation();
250 if (o != 0 && o != 180 && o != -180)
251 glScalef (1/h, 1/h, 1/h);
259 glBegin((wire)?GL_LINE_LOOP:GL_QUADS);
260 glNormal3f( 0.0f, -1.0f, 0.0f);
261 glVertex3f(-1.0f, -1.0f, -1.0f);
262 glVertex3f( 1.0f, -1.0f, -1.0f);
263 glVertex3f( 1.0f, -1.0f, 1.0f);
264 glVertex3f(-1.0f, -1.0f, 1.0f);
265 glNormal3f( 0.0f, 0.0f, 1.0f);
266 glVertex3f(-1.0f, -1.0f, 1.0f);
267 glVertex3f( 1.0f, -1.0f, 1.0f);
268 glVertex3f( 1.0f, 1.0f, 1.0f);
269 glVertex3f(-1.0f, 1.0f, 1.0f);
270 glNormal3f( 0.0f, 0.0f, -1.0f);
271 glVertex3f(-1.0f, -1.0f, -1.0f);
272 glVertex3f(-1.0f, 1.0f, -1.0f);
273 glVertex3f( 1.0f, 1.0f, -1.0f);
274 glVertex3f( 1.0f, -1.0f, -1.0f);
275 glNormal3f( 1.0f, 0.0f, 0.0f);
276 glVertex3f( 1.0f, -1.0f, -1.0f);
277 glVertex3f( 1.0f, 1.0f, -1.0f);
278 glVertex3f( 1.0f, 1.0f, 1.0f);
279 glVertex3f( 1.0f, -1.0f, 1.0f);
280 glNormal3f( -1.0f, 0.0f, 0.0f);
281 glVertex3f(-1.0f, -1.0f, -1.0f);
282 glVertex3f(-1.0f, -1.0f, 1.0f);
283 glVertex3f(-1.0f, 1.0f, 1.0f);
284 glVertex3f(-1.0f, 1.0f, -1.0f);
285 glNormal3f( 1.0f, 1.0f, 0.0f);
286 glVertex3f(-1.0f, 1.0f, -1.0f);
287 glVertex3f(-1.0f, 1.0f, 1.0f);
288 glVertex3f( 1.0f, 1.0f, 1.0f);
289 glVertex3f( 1.0f, 1.0f, -1.0f);
294 init_ball (ModeInfo *mi)
296 int wire = MI_IS_WIREFRAME(mi);
299 if(blinkbox == NULL) {
300 if((blinkbox = (blinkboxstruct *) calloc(MI_NUM_SCREENS(mi),
301 sizeof (blinkboxstruct))) == NULL)
304 bp = &blinkbox[MI_SCREEN(mi)];
306 if ((bp->glx_context = init_GL(mi)) != NULL) {
307 reshape_ball(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
308 glDrawBuffer(GL_BACK);
314 bp->bscale.wh = bscale_wh;
331 bp->lside.counter = MAX_COUNT;
332 bp->rside.counter = MAX_COUNT;
333 bp->tside.counter = MAX_COUNT;
334 bp->bside.counter = MAX_COUNT;
335 bp->fside.counter = MAX_COUNT;
336 bp->aside.counter = MAX_COUNT;
338 bp->lside.color[0] = 1;
339 bp->rside.color[1] = 1;
340 bp->tside.color[2] = 1;
342 bp->bside.color[0] = 1;
343 bp->bside.color[1] = 0.5;
345 bp->fside.color[0] = 1;
346 bp->fside.color[1] = 1;
348 bp->aside.color[0] = 0.5;
349 bp->aside.color[2] = 1;
351 bp->lside.rot[0] = 90;
352 bp->rside.rot[0] = 90;
353 bp->tside.rot[0] = 90;
354 bp->bside.rot[0] = 90;
355 bp->fside.rot[0] = 90;
356 bp->aside.rot[0] = 90;
358 bp->lside.rot[2] = 1;
359 bp->rside.rot[2] = 1;
360 bp->tside.rot[1] = 1;
361 bp->bside.rot[1] = 1;
362 bp->fside.rot[3] = 1;
363 bp->aside.rot[3] = 1;
365 bp->lside.des_count = 1;
366 bp->rside.des_count = 1;
367 bp->tside.des_count = 1;
368 bp->bside.des_count = 1;
369 bp->fside.des_count = 1;
370 bp->aside.des_count = 1;
372 bp->lside.alpha_count = 1;
373 bp->rside.alpha_count = 1;
374 bp->tside.alpha_count = 1;
375 bp->bside.alpha_count = 1;
376 bp->fside.alpha_count = 1;
377 bp->aside.alpha_count = 1;
380 #define SPHERE_SLICES 12 /* how densely to render spheres */
381 #define SPHERE_STACKS 16
383 bp->sp = malloc(sizeof(*bp->sp));
385 fprintf(stderr,"Could not allocate memory\n");
388 if( (bp->bscale.wh < 1) ||
389 (bp->bscale.wh > 8) ) {
390 fprintf(stderr,"Boxsize out of range. Using default\n");
394 bp->des_amt = bp->bscale.wh / MAX_COUNT;
397 reshape_ball(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
398 bp->ballList = glGenLists(1);
399 glNewList(bp->ballList, GL_COMPILE);
400 unit_sphere (SPHERE_STACKS, SPHERE_SLICES, wire);
403 bp->boxList = glGenLists(1);
404 glNewList(bp->boxList, GL_COMPILE);
410 glEnable(GL_COLOR_MATERIAL);
411 glShadeModel(GL_SMOOTH);
413 glEnable(GL_DEPTH_TEST);
414 glDepthFunc(GL_LEQUAL);
415 glEnable(GL_LIGHTING);
417 glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
418 glLightfv(GL_LIGHT1, GL_POSITION,LightPosition);
420 if (do_fade || do_blur) {
422 glDisable(GL_DEPTH_TEST);
427 CheckBoxPos(blinkboxstruct *bp,
428 GLfloat bot_x, GLfloat top_x, GLfloat bot_y, GLfloat top_y)
430 /*Make sure it's inside of the bounding box*/
431 bp->bpos.x = ((bp->bpos.x - bp->bscale.wh) < bot_x) ? bot_x + bp->bscale.wh : bp->bpos.x;
432 bp->bpos.x = ((bp->bpos.x + bp->bscale.wh) > top_x) ? top_x - bp->bscale.wh : bp->bpos.x;
433 bp->bpos.y = ((bp->bpos.y - bp->bscale.wh) < bot_y) ? bot_y + bp->bscale.wh : bp->bpos.y;
434 bp->bpos.y = ((bp->bpos.y + bp->bscale.wh) > top_y) ? top_y - bp->bscale.wh : bp->bpos.y;
438 draw_ball (ModeInfo *mi)
440 blinkboxstruct *bp = &blinkbox[MI_SCREEN(mi)];
442 Display *dpy = MI_DISPLAY(mi);
443 Window window = MI_WINDOW(mi);
446 if (! bp->glx_context)
448 mi->polygon_count = 0;
449 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(bp->glx_context));
451 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
457 glRotated(0.25,0,0,1);
458 glRotated(0.25,0,1,0);
459 glRotated(0.25,1,0,0);
463 glScalef(0.5,0.5,0.5);
468 if (!do_blur || MI_IS_WIREFRAME(mi)) {
469 glTranslatef(bp->ball.x += bp->mo.x,
470 bp->ball.y += bp->mo.y,
471 bp->ball.z += bp->mo.z);
474 glCallList(bp->ballList);
475 mi->polygon_count += SPHERE_SLICES*SPHERE_STACKS;
479 # define blur_detail 24.0
480 float ball_alpha = 1 / blur_detail;
482 glBlendFunc(GL_SRC_ALPHA,GL_ONE);
483 glTranslatef(bp->ball.x, bp->ball.y, bp->ball.z);
485 for (i = 0; i < blur_detail; ++i) {
486 glTranslatef(bp->mo.x / blur_detail,
487 bp->mo.y / blur_detail,
488 bp->mo.z / blur_detail);
490 /* comment the following line for quick but boring linear blur */
491 ball_alpha = sin((M_PI / blur_detail) * i) / blur_detail;
493 glColor4f(1, 1, 1, ball_alpha);
496 glCallList(bp->ballList);
497 mi->polygon_count += SPHERE_SLICES*SPHERE_STACKS;
498 glScalef(.5, .5, .5);
502 bp->ball.x += bp->mo.x;
503 bp->ball.y += bp->mo.y;
504 bp->ball.z += bp->mo.z;
513 bp->bpos.x = bp->lside.pos.z*-1;
514 bp->bpos.y = bp->lside.pos.y;
515 bp->bpos.z = bbox.bottom.x - bp->bscale.d;
517 CheckBoxPos(bp, bbox.bottom.z,bbox.top.z,bbox.bottom.y,bbox.top.y);
522 bp->bpos.x = bp->rside.pos.z*-1;
523 bp->bpos.y = bp->rside.pos.y;
524 bp->bpos.z = bbox.top.x + bp->bscale.d;
526 CheckBoxPos(bp, bbox.bottom.z,bbox.top.z,bbox.bottom.y,bbox.top.y);
531 bp->bpos.x = bp->tside.pos.x;
532 bp->bpos.y = bp->tside.pos.z;
533 bp->bpos.z = bbox.bottom.y - bp->bscale.d;
535 CheckBoxPos(bp, bbox.bottom.x,bbox.top.x,bbox.bottom.z,bbox.top.z);
540 bp->bpos.x = bp->bside.pos.x;
541 bp->bpos.y = bp->bside.pos.z;
542 bp->bpos.z = bbox.top.y + bp->bscale.d;
544 CheckBoxPos(bp, bbox.bottom.x,bbox.top.x,bbox.bottom.z,bbox.top.z);
549 bp->bpos.x = bp->fside.pos.y;
550 bp->bpos.y = bp->fside.pos.x*-1;
551 bp->bpos.z = bbox.top.z + bp->bscale.d;
553 CheckBoxPos(bp, bbox.bottom.y,bbox.top.y,bbox.bottom.x,bbox.top.x);
558 bp->bpos.x = bp->aside.pos.y;
559 bp->bpos.y = bp->aside.pos.x*-1;
560 bp->bpos.z = bbox.bottom.z + bp->bscale.d;
562 CheckBoxPos(bp, bbox.bottom.y,bbox.top.y,bbox.bottom.x,bbox.top.x);
568 glColor4f(bp->sp->color[0],bp->sp->color[1],bp->sp->color[2],1-(ALPHA_AMT * bp->sp->alpha_count));
570 glColor3fv(bp->sp->color);
572 glBlendFunc(GL_SRC_ALPHA,GL_ONE);
574 glRotatef(bp->sp->rot[0],bp->sp->rot[1],bp->sp->rot[2],bp->sp->rot[3]);
575 glTranslatef(bp->bpos.x,bp->bpos.y,bp->bpos.z);
577 glScalef(bp->bscale.wh-(bp->des_amt*bp->sp->des_count),bp->bscale.wh-(bp->des_amt*bp->sp->des_count),bp->bscale.d);
579 glScalef(bp->bscale.wh,bp->bscale.wh,bp->bscale.d);
581 glCallList(bp->boxList);
582 mi->polygon_count += 6;
586 bp->sp->alpha_count++;
597 if (mi->fps_p) do_fps (mi);
599 glXSwapBuffers(dpy, window);
603 XSCREENSAVER_MODULE_2 ("BlinkBox", blinkbox, ball)