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 #include <X11/Intrinsic.h>
17 extern XtAppContext app;
19 #define PROGCLASS "BlinkBox"
20 #define HACK_INIT init_ball
21 #define HACK_DRAW draw_ball
22 #define HACK_RESHAPE reshape_ball
23 #define sws_opts xlockmore_opts
25 #define ALPHA_AMT 0.05
27 /* this should be between 1 and 8 */
29 #define DEF_DISSOLVE "False"
30 #define DEF_FADE "True"
31 #define DEF_BLUR "True"
33 #define DEFAULTS "*delay: 30000 \n" \
34 "*wireframe: False \n" \
37 #define countof(x) (sizeof((x))/sizeof((*x)))
39 #include "xlockmore.h"
43 #ifdef USE_GL /* whole file */
65 } bbox = {{14,14,20},{-14,-14,-20}};
75 GLfloat wh; /*width Height*/
79 static GLuint ballList;
80 static GLuint boxList;
81 Tdpos mo = { 1.0, 1.0, 1.0}; /*motion*/
82 Tdpos moh= {-1.0,-1.5,-1.5}; /*hold motion value*/
84 Tdpos bpos= {1.0, 1.0, 1.0};
87 static Side lside = {0, {0, 0, 0,}, MAX_COUNT, {1.0, 0.0, 0.0},{90,0,1,0},1,1};/*Red*/
88 static Side rside = {0, {0, 0, 0,}, MAX_COUNT, {0.0, 1.0, 0.0},{90,0,1,0},1,1};/*Green*/
89 static Side tside = {0, {0, 0, 0,}, MAX_COUNT, {0.0, 0.0, 1.0},{90,1,0,0},1,1};/*Blue*/
90 static Side bside = {0, {0, 0, 0,}, MAX_COUNT, {1.0, 0.5, 0.0},{90,1,0,0},1,1};/*Orange*/
91 static Side fside = {0, {0, 0, 0,}, MAX_COUNT, {1.0, 1.0, 0.0},{90,0,0,1},1,1};/*Yellow*/
92 static Side aside = {0, {0, 0, 0,}, MAX_COUNT, {0.5, 0.0, 1.0},{90,0,0,1},1,1};/*Purple*/
96 static float LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f };
97 static float LightPosition[]= { 20.0f, 100.0f, 20.0f, 1.0f };
99 static Bool do_dissolve;
102 static GLfloat des_amt = 1;
104 static XrmOptionDescRec opts[] = {
105 { "-boxsize", ".boxsize", XrmoptionSepArg, 0 },
106 { "-dissolve", ".dissolve", XrmoptionNoArg, "True" },
107 { "+dissolve", ".dissolve", XrmoptionNoArg, "False" },
108 { "-fade", ".fade", XrmoptionNoArg, "True" },
109 { "+fade", ".fade", XrmoptionNoArg, "False" },
110 { "-blur", ".blur", XrmoptionNoArg, "True" },
111 { "+blur", ".blur", XrmoptionNoArg, "False" }
115 static argtype vars[] = {
116 {&bscale.wh, "boxsize", "Boxsize", DEF_WH, t_Float},
117 {&do_dissolve, "dissolve", "Dissolve", DEF_DISSOLVE, t_Bool},
118 {&do_fade, "fade", "Fade", DEF_FADE, t_Bool},
119 {&do_blur, "blur", "Blur", DEF_BLUR, t_Bool},
122 ModeSpecOpt sws_opts = {countof(opts), opts, countof(vars), vars, NULL};
125 swap(GLfloat *a, GLfloat *b)
135 GLfloat j = 1+(random() % 2);
140 swap_mov(GLfloat *a, GLfloat *b)
152 cp_b_pos(Tdpos *s_pos){
161 if ((ball.x - ball.d) <= bbox.bottom.x){
163 lside.counter = MAX_COUNT;
165 lside.alpha_count = 0;
166 cp_b_pos(&lside.pos);
167 swap_mov(&mo.x,&moh.x);
169 if ((ball.x + ball.d) >= bbox.top.x){
171 rside.counter = MAX_COUNT;
173 rside.alpha_count = 0;
174 cp_b_pos(&rside.pos);
175 swap_mov(&mo.x,&moh.x);
182 if ((ball.y - ball.d) <= bbox.bottom.y){
184 bside.counter = MAX_COUNT;
186 bside.alpha_count = 0;
187 cp_b_pos(&bside.pos);
188 swap_mov(&mo.y,&moh.y);
190 if ((ball.y + ball.d) >= bbox.top.y){
192 tside.counter = MAX_COUNT;
194 tside.alpha_count = 0;
195 cp_b_pos(&tside.pos);
196 swap_mov(&mo.y,&moh.y);
203 if ((ball.z - ball.d) <= bbox.bottom.z){
205 aside.counter = MAX_COUNT;
207 aside.alpha_count = 0;
208 cp_b_pos(&aside.pos);
209 swap_mov(&mo.z,&moh.z);
211 if((ball.z + ball.d) >= bbox.top.z){
213 fside.counter = MAX_COUNT;
215 fside.alpha_count = 0;
216 cp_b_pos(&fside.pos);
217 swap_mov(&mo.z,&moh.z);
222 reshape_ball (ModeInfo *mi, int width, int height)
224 GLfloat h = (GLfloat) height / (GLfloat) width;
226 glViewport (0, 0, (GLint) width, (GLint) height);
227 glMatrixMode(GL_PROJECTION);
229 gluPerspective (30.0, 1/h, 1.0, 100.0);
231 glMatrixMode(GL_MODELVIEW);
233 gluLookAt( 0.0, 0.0, 40.0,
242 glBegin((wire)?GL_LINE_LOOP:GL_QUADS);
243 glNormal3f( 0.0f, -1.0f, 0.0f);
244 glVertex3f(-1.0f, -1.0f, -1.0f);
245 glVertex3f( 1.0f, -1.0f, -1.0f);
246 glVertex3f( 1.0f, -1.0f, 1.0f);
247 glVertex3f(-1.0f, -1.0f, 1.0f);
248 glNormal3f( 0.0f, 0.0f, 1.0f);
249 glVertex3f(-1.0f, -1.0f, 1.0f);
250 glVertex3f( 1.0f, -1.0f, 1.0f);
251 glVertex3f( 1.0f, 1.0f, 1.0f);
252 glVertex3f(-1.0f, 1.0f, 1.0f);
253 glNormal3f( 0.0f, 0.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 glVertex3f( 1.0f, -1.0f, -1.0f);
258 glNormal3f( 1.0f, 0.0f, 0.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 glVertex3f( 1.0f, -1.0f, 1.0f);
263 glNormal3f( -1.0f, 0.0f, 0.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 glVertex3f(-1.0f, 1.0f, -1.0f);
268 glNormal3f( 1.0f, 1.0f, 0.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 glVertex3f( 1.0f, 1.0f, -1.0f);
277 init_ball (ModeInfo *mi)
279 #define SPHERE_SLICES 12 /* how densely to render spheres */
280 #define SPHERE_STACKS 16
281 int wire = MI_IS_WIREFRAME(mi);
283 sp = malloc(sizeof(*sp));
285 fprintf(stderr,"Could not allocate memory\n");
288 if( (bscale.wh < 1) ||
290 fprintf(stderr,"Boxsize out of range. Using default\n");
294 des_amt = bscale.wh / MAX_COUNT;
297 reshape_ball(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
298 ballList = glGenLists(1);
299 glNewList(ballList, GL_COMPILE);
300 unit_sphere (SPHERE_STACKS, SPHERE_SLICES, wire);
303 boxList = glGenLists(1);
304 glNewList(boxList, GL_COMPILE);
310 glEnable(GL_COLOR_MATERIAL);
311 glShadeModel(GL_SMOOTH);
312 glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
314 glEnable(GL_DEPTH_TEST);
315 glDepthFunc(GL_LEQUAL);
316 glEnable(GL_LIGHTING);
318 glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
319 glLightfv(GL_LIGHT1, GL_POSITION,LightPosition);
321 if (do_fade || do_blur) {
323 glDisable(GL_DEPTH_TEST);
328 CheckBoxPos(GLfloat bot_x, GLfloat top_x, GLfloat bot_y, GLfloat top_y)
330 /*Make sure it's inside of the bounding box*/
331 bpos.x = ((bpos.x - bscale.wh) < bot_x) ? bot_x + bscale.wh : bpos.x;
332 bpos.x = ((bpos.x + bscale.wh) > top_x) ? top_x - bscale.wh : bpos.x;
333 bpos.y = ((bpos.y - bscale.wh) < bot_y) ? bot_y + bscale.wh : bpos.y;
334 bpos.y = ((bpos.y + bscale.wh) > top_y) ? top_y - bscale.wh : bpos.y;
338 draw_ball (ModeInfo *mi)
340 Display *dpy = MI_DISPLAY(mi);
341 Window window = MI_WINDOW(mi);
343 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
349 glRotated(0.25,0,0,1);
350 glRotated(0.25,0,1,0);
351 glRotated(0.25,1,0,0);
355 glScalef(0.5,0.5,0.5);
360 if (!do_blur || MI_IS_WIREFRAME(mi)) {
361 glTranslatef(ball.x += mo.x,
366 glCallList(ballList);
370 # define blur_detail 24.0
371 float ball_alpha = 1 / blur_detail;
373 glBlendFunc(GL_SRC_ALPHA,GL_ONE);
374 glTranslatef(ball.x, ball.y, ball.z);
376 for (i = 0; i < blur_detail; ++i) {
377 glTranslatef(mo.x / blur_detail,
381 /* comment the following line for quick but boring linear blur */
382 ball_alpha = sin((M_PI / blur_detail) * i) / blur_detail;
384 glColor4f(1, 1, 1, ball_alpha);
387 glCallList(ballList);
388 glScalef(.5, .5, .5);
403 bpos.x = lside.pos.z*-1;
404 bpos.y = lside.pos.y;
405 bpos.z = bbox.bottom.x - bscale.d;
407 CheckBoxPos(bbox.bottom.z,bbox.top.z,bbox.bottom.y,bbox.top.y);
412 bpos.x = rside.pos.z*-1;
413 bpos.y = rside.pos.y;
414 bpos.z = bbox.top.x + bscale.d;
416 CheckBoxPos(bbox.bottom.z,bbox.top.z,bbox.bottom.y,bbox.top.y);
421 bpos.x = tside.pos.x;
422 bpos.y = tside.pos.z;
423 bpos.z = bbox.bottom.y - bscale.d;
425 CheckBoxPos(bbox.bottom.x,bbox.top.x,bbox.bottom.z,bbox.top.z);
430 bpos.x = bside.pos.x;
431 bpos.y = bside.pos.z;
432 bpos.z = bbox.top.y + bscale.d;
434 CheckBoxPos(bbox.bottom.x,bbox.top.x,bbox.bottom.z,bbox.top.z);
439 bpos.x = fside.pos.y;
440 bpos.y = fside.pos.x*-1;
441 bpos.z = bbox.top.z + bscale.d;
443 CheckBoxPos(bbox.bottom.y,bbox.top.y,bbox.bottom.x,bbox.top.x);
448 bpos.x = aside.pos.y;
449 bpos.y = aside.pos.x*-1;
450 bpos.z = bbox.bottom.z + bscale.d;
452 CheckBoxPos(bbox.bottom.y,bbox.top.y,bbox.bottom.x,bbox.top.x);
458 glColor4f(sp->color[0],sp->color[1],sp->color[2],1-(ALPHA_AMT * sp->alpha_count));
460 glColor3fv(sp->color);
462 glBlendFunc(GL_SRC_ALPHA,GL_ONE);
464 glRotatef(sp->rot[0],sp->rot[1],sp->rot[2],sp->rot[3]);
465 glTranslatef(bpos.x,bpos.y,bpos.z);
467 glScalef(bscale.wh-(des_amt*sp->des_count),bscale.wh-(des_amt*sp->des_count),bscale.d);
469 glScalef(bscale.wh,bscale.wh,bscale.d);
487 glXSwapBuffers(dpy, window);