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 #include <X11/Intrinsic.h>
14 extern XtAppContext app;
16 #define PROGCLASS "BlinkBox"
17 #define HACK_INIT init_ball
18 #define HACK_DRAW draw_ball
19 #define HACK_RESHAPE reshape_ball
20 #define sws_opts xlockmore_opts
22 #define ALPHA_AMT 0.05
24 /* this should be between 1 and 8 */
26 #define DEF_DISSOLVE "False"
27 #define DEF_FADE "True"
29 #define DEFAULTS "*delay: 30000 \n" \
30 "*wireframe: False \n" \
31 "*boxsize: " DEF_WH "\n" \
32 "*dissolve: " DEF_DISSOLVE "\n" \
33 "*fade: " DEF_FADE "\n" \
36 #define countof(x) (sizeof((x))/sizeof((*x)))
38 #include "xlockmore.h"
42 #ifdef USE_GL /* whole file */
64 } bbox = {{14,14,20},{-14,-14,-20}};
74 GLfloat wh; /*width Height*/
78 static GLuint ballList;
79 static GLuint boxList;
80 Tdpos mo = { 1.0, 1.0, 1.0}; /*motion*/
81 Tdpos moh= {-1.0,-1.5,-1.5}; /*hold motion value*/
83 Tdpos bpos= {1.0, 1.0, 1.0};
86 static Side lside = {0, {0, 0, 0,}, MAX_COUNT, {1.0, 0.0, 0.0},{90,0,1,0},1,1};/*Red*/
87 static Side rside = {0, {0, 0, 0,}, MAX_COUNT, {0.0, 1.0, 0.0},{90,0,1,0},1,1};/*Green*/
88 static Side tside = {0, {0, 0, 0,}, MAX_COUNT, {0.0, 0.0, 1.0},{90,1,0,0},1,1};/*Blue*/
89 static Side bside = {0, {0, 0, 0,}, MAX_COUNT, {1.0, 0.5, 0.0},{90,1,0,0},1,1};/*Orange*/
90 static Side fside = {0, {0, 0, 0,}, MAX_COUNT, {1.0, 1.0, 0.0},{90,0,0,1},1,1};/*Yellow*/
91 static Side aside = {0, {0, 0, 0,}, MAX_COUNT, {0.5, 0.0, 1.0},{90,0,0,1},1,1};/*Purple*/
95 static float LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f };
96 static float LightPosition[]= { 20.0f, 100.0f, 20.0f, 1.0f };
98 static Bool do_dissolve;
100 static GLfloat des_amt = 1;
102 static XrmOptionDescRec opts[] = {
103 { "-boxsize", ".boxsize", XrmoptionSepArg, 0 },
104 { "-dissolve", ".dissolve", XrmoptionNoArg, "True" },
105 { "+dissolve", ".dissolve", XrmoptionNoArg, "False" },
106 { "-fade", ".fade", XrmoptionNoArg, "True" },
107 { "+fade", ".fade", XrmoptionNoArg, "False" }
111 static argtype vars[] = {
112 {&bscale.wh, "boxsize", "Boxsize", DEF_WH, t_Float},
113 {&do_dissolve, "dissolve", "Dissolve", DEF_DISSOLVE, t_Bool},
114 {&do_fade, "fade", "Fade", DEF_FADE, t_Bool},
117 ModeSpecOpt sws_opts = {countof(opts), opts, countof(vars), vars, NULL};
120 swap(GLfloat *a, GLfloat *b)
130 GLfloat j = 1+(random() % 2);
135 swap_mov(GLfloat *a, GLfloat *b)
147 cp_b_pos(Tdpos *s_pos){
156 if ((ball.x - ball.d) <= bbox.bottom.x){
158 lside.counter = MAX_COUNT;
160 lside.alpha_count = 0;
161 cp_b_pos(&lside.pos);
162 swap_mov(&mo.x,&moh.x);
164 if ((ball.x + ball.d) >= bbox.top.x){
166 rside.counter = MAX_COUNT;
168 rside.alpha_count = 0;
169 cp_b_pos(&rside.pos);
170 swap_mov(&mo.x,&moh.x);
177 if ((ball.y - ball.d) <= bbox.bottom.y){
179 bside.counter = MAX_COUNT;
181 bside.alpha_count = 0;
182 cp_b_pos(&bside.pos);
183 swap_mov(&mo.y,&moh.y);
185 if ((ball.y + ball.d) >= bbox.top.y){
187 tside.counter = MAX_COUNT;
189 tside.alpha_count = 0;
190 cp_b_pos(&tside.pos);
191 swap_mov(&mo.y,&moh.y);
198 if ((ball.z - ball.d) <= bbox.bottom.z){
200 aside.counter = MAX_COUNT;
202 aside.alpha_count = 0;
203 cp_b_pos(&aside.pos);
204 swap_mov(&mo.z,&moh.z);
206 if((ball.z + ball.d) >= bbox.top.z){
208 fside.counter = MAX_COUNT;
210 fside.alpha_count = 0;
211 cp_b_pos(&fside.pos);
212 swap_mov(&mo.z,&moh.z);
217 reshape_ball (ModeInfo *mi, int width, int height)
219 GLfloat h = (GLfloat) height / (GLfloat) width;
221 glViewport (0, 0, (GLint) width, (GLint) height);
222 glMatrixMode(GL_PROJECTION);
224 gluPerspective (30.0, 1/h, 1.0, 100.0);
226 glMatrixMode(GL_MODELVIEW);
228 gluLookAt( 0.0, 0.0, 40.0,
237 glBegin((wire)?GL_LINE_LOOP:GL_QUADS);
238 glNormal3f( 0.0f, -1.0f, 0.0f);
239 glVertex3f(-1.0f, -1.0f, -1.0f);
240 glVertex3f( 1.0f, -1.0f, -1.0f);
241 glVertex3f( 1.0f, -1.0f, 1.0f);
242 glVertex3f(-1.0f, -1.0f, 1.0f);
243 glNormal3f( 0.0f, 0.0f, 1.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( 1.0f, 0.0f, 0.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, 1.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);
272 init_ball (ModeInfo *mi)
274 #define SPHERE_SLICES 32 /* how densely to render spheres */
275 #define SPHERE_STACKS 16
276 int wire = MI_IS_WIREFRAME(mi);
278 sp = malloc(sizeof(*sp));
280 fprintf(stderr,"Could not allocate memory\n");
283 if( (bscale.wh < 1) ||
285 fprintf(stderr,"Boxsize out of range. Using default\n");
289 des_amt = bscale.wh / MAX_COUNT;
292 reshape_ball(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
293 ballList = glGenLists(1);
294 glNewList(ballList, GL_COMPILE);
295 unit_sphere (SPHERE_STACKS, SPHERE_SLICES, wire);
298 boxList = glGenLists(1);
299 glNewList(boxList, GL_COMPILE);
305 glEnable(GL_COLOR_MATERIAL);
306 glShadeModel(GL_SMOOTH);
307 glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
309 glEnable(GL_DEPTH_TEST);
310 glDepthFunc(GL_LEQUAL);
311 glEnable(GL_LIGHTING);
313 glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
314 glLightfv(GL_LIGHT1, GL_POSITION,LightPosition);
318 glDisable(GL_DEPTH_TEST);
323 CheckBoxPos(GLfloat bot_x, GLfloat top_x, GLfloat bot_y, GLfloat top_y)
325 /*Make sure it's inside of the bounding box*/
326 bpos.x = ((bpos.x - bscale.wh) < bot_x) ? bot_x + bscale.wh : bpos.x;
327 bpos.x = ((bpos.x + bscale.wh) > top_x) ? top_x - bscale.wh : bpos.x;
328 bpos.y = ((bpos.y - bscale.wh) < bot_y) ? bot_y + bscale.wh : bpos.y;
329 bpos.y = ((bpos.y + bscale.wh) > top_y) ? top_y - bscale.wh : bpos.y;
333 draw_ball (ModeInfo *mi)
335 Display *dpy = MI_DISPLAY(mi);
336 Window window = MI_WINDOW(mi);
338 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
344 glRotated(0.25,0,0,1);
345 glRotated(0.25,0,1,0);
346 glRotated(0.25,1,0,0);
350 glScalef(0.5,0.5,0.5);
354 glTranslatef(ball.x += mo.x,
359 glCallList(ballList);
366 bpos.x = lside.pos.z*-1;
367 bpos.y = lside.pos.y;
368 bpos.z = bbox.bottom.x - bscale.d;
370 CheckBoxPos(bbox.bottom.z,bbox.top.z,bbox.bottom.y,bbox.top.y);
375 bpos.x = rside.pos.z*-1;
376 bpos.y = rside.pos.y;
377 bpos.z = bbox.top.x + bscale.d;
379 CheckBoxPos(bbox.bottom.z,bbox.top.z,bbox.bottom.y,bbox.top.y);
384 bpos.x = tside.pos.x;
385 bpos.y = tside.pos.z;
386 bpos.z = bbox.bottom.y - bscale.d;
388 CheckBoxPos(bbox.bottom.x,bbox.top.x,bbox.bottom.z,bbox.top.z);
393 bpos.x = bside.pos.x;
394 bpos.y = bside.pos.z;
395 bpos.z = bbox.top.y + bscale.d;
397 CheckBoxPos(bbox.bottom.x,bbox.top.x,bbox.bottom.z,bbox.top.z);
402 bpos.x = fside.pos.y;
403 bpos.y = fside.pos.x*-1;
404 bpos.z = bbox.top.z + bscale.d;
406 CheckBoxPos(bbox.bottom.y,bbox.top.y,bbox.bottom.x,bbox.top.x);
411 bpos.x = aside.pos.y;
412 bpos.y = aside.pos.x*-1;
413 bpos.z = bbox.bottom.z + bscale.d;
415 CheckBoxPos(bbox.bottom.y,bbox.top.y,bbox.bottom.x,bbox.top.x);
421 glColor4f(sp->color[0],sp->color[1],sp->color[2],1-(ALPHA_AMT * sp->alpha_count));
423 glColor3fv(sp->color);
425 glBlendFunc(GL_SRC_ALPHA,GL_ONE);
427 glRotatef(sp->rot[0],sp->rot[1],sp->rot[2],sp->rot[3]);
428 glTranslatef(bpos.x,bpos.y,bpos.z);
430 glScalef(bscale.wh-(des_amt*sp->des_count),bscale.wh-(des_amt*sp->des_count),bscale.d);
432 glScalef(bscale.wh,bscale.wh,bscale.d);
450 glXSwapBuffers(dpy, window);