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" \
33 #define countof(x) (sizeof((x))/sizeof((*x)))
35 #include "xlockmore.h"
39 #ifdef USE_GL /* whole file */
61 } bbox = {{14,14,20},{-14,-14,-20}};
71 GLfloat wh; /*width Height*/
75 static GLuint ballList;
76 static GLuint boxList;
77 Tdpos mo = { 1.0, 1.0, 1.0}; /*motion*/
78 Tdpos moh= {-1.0,-1.5,-1.5}; /*hold motion value*/
80 Tdpos bpos= {1.0, 1.0, 1.0};
83 static Side lside = {0, {0, 0, 0,}, MAX_COUNT, {1.0, 0.0, 0.0},{90,0,1,0},1,1};/*Red*/
84 static Side rside = {0, {0, 0, 0,}, MAX_COUNT, {0.0, 1.0, 0.0},{90,0,1,0},1,1};/*Green*/
85 static Side tside = {0, {0, 0, 0,}, MAX_COUNT, {0.0, 0.0, 1.0},{90,1,0,0},1,1};/*Blue*/
86 static Side bside = {0, {0, 0, 0,}, MAX_COUNT, {1.0, 0.5, 0.0},{90,1,0,0},1,1};/*Orange*/
87 static Side fside = {0, {0, 0, 0,}, MAX_COUNT, {1.0, 1.0, 0.0},{90,0,0,1},1,1};/*Yellow*/
88 static Side aside = {0, {0, 0, 0,}, MAX_COUNT, {0.5, 0.0, 1.0},{90,0,0,1},1,1};/*Purple*/
92 static float LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f };
93 static float LightPosition[]= { 20.0f, 100.0f, 20.0f, 1.0f };
95 static Bool do_dissolve;
97 static GLfloat des_amt = 1;
99 static XrmOptionDescRec opts[] = {
100 { "-boxsize", ".boxsize", XrmoptionSepArg, 0 },
101 { "-dissolve", ".dissolve", XrmoptionNoArg, "True" },
102 { "+dissolve", ".dissolve", XrmoptionNoArg, "False" },
103 { "-fade", ".fade", XrmoptionNoArg, "True" },
104 { "+fade", ".fade", XrmoptionNoArg, "False" }
108 static argtype vars[] = {
109 {&bscale.wh, "boxsize", "Boxsize", DEF_WH, t_Float},
110 {&do_dissolve, "dissolve", "Dissolve", DEF_DISSOLVE, t_Bool},
111 {&do_fade, "fade", "Fade", DEF_FADE, t_Bool},
114 ModeSpecOpt sws_opts = {countof(opts), opts, countof(vars), vars, NULL};
117 swap(GLfloat *a, GLfloat *b)
127 GLfloat j = 1+(random() % 2);
132 swap_mov(GLfloat *a, GLfloat *b)
144 cp_b_pos(Tdpos *s_pos){
153 if ((ball.x - ball.d) <= bbox.bottom.x){
155 lside.counter = MAX_COUNT;
157 lside.alpha_count = 0;
158 cp_b_pos(&lside.pos);
159 swap_mov(&mo.x,&moh.x);
161 if ((ball.x + ball.d) >= bbox.top.x){
163 rside.counter = MAX_COUNT;
165 rside.alpha_count = 0;
166 cp_b_pos(&rside.pos);
167 swap_mov(&mo.x,&moh.x);
174 if ((ball.y - ball.d) <= bbox.bottom.y){
176 bside.counter = MAX_COUNT;
178 bside.alpha_count = 0;
179 cp_b_pos(&bside.pos);
180 swap_mov(&mo.y,&moh.y);
182 if ((ball.y + ball.d) >= bbox.top.y){
184 tside.counter = MAX_COUNT;
186 tside.alpha_count = 0;
187 cp_b_pos(&tside.pos);
188 swap_mov(&mo.y,&moh.y);
195 if ((ball.z - ball.d) <= bbox.bottom.z){
197 aside.counter = MAX_COUNT;
199 aside.alpha_count = 0;
200 cp_b_pos(&aside.pos);
201 swap_mov(&mo.z,&moh.z);
203 if((ball.z + ball.d) >= bbox.top.z){
205 fside.counter = MAX_COUNT;
207 fside.alpha_count = 0;
208 cp_b_pos(&fside.pos);
209 swap_mov(&mo.z,&moh.z);
214 reshape_ball (ModeInfo *mi, int width, int height)
216 GLfloat h = (GLfloat) height / (GLfloat) width;
218 glViewport (0, 0, (GLint) width, (GLint) height);
219 glMatrixMode(GL_PROJECTION);
221 gluPerspective (30.0, 1/h, 1.0, 100.0);
223 glMatrixMode(GL_MODELVIEW);
225 gluLookAt( 0.0, 0.0, 40.0,
234 glBegin((wire)?GL_LINE_LOOP:GL_QUADS);
235 glNormal3f( 0.0f, -1.0f, 0.0f);
236 glVertex3f(-1.0f, -1.0f, -1.0f);
237 glVertex3f( 1.0f, -1.0f, -1.0f);
238 glVertex3f( 1.0f, -1.0f, 1.0f);
239 glVertex3f(-1.0f, -1.0f, 1.0f);
240 glNormal3f( 0.0f, 0.0f, 1.0f);
241 glVertex3f(-1.0f, -1.0f, 1.0f);
242 glVertex3f( 1.0f, -1.0f, 1.0f);
243 glVertex3f( 1.0f, 1.0f, 1.0f);
244 glVertex3f(-1.0f, 1.0f, 1.0f);
245 glNormal3f( 0.0f, 0.0f, -1.0f);
246 glVertex3f(-1.0f, -1.0f, -1.0f);
247 glVertex3f(-1.0f, 1.0f, -1.0f);
248 glVertex3f( 1.0f, 1.0f, -1.0f);
249 glVertex3f( 1.0f, -1.0f, -1.0f);
250 glNormal3f( 1.0f, 0.0f, 0.0f);
251 glVertex3f( 1.0f, -1.0f, -1.0f);
252 glVertex3f( 1.0f, 1.0f, -1.0f);
253 glVertex3f( 1.0f, 1.0f, 1.0f);
254 glVertex3f( 1.0f, -1.0f, 1.0f);
255 glNormal3f( -1.0f, 0.0f, 0.0f);
256 glVertex3f(-1.0f, -1.0f, -1.0f);
257 glVertex3f(-1.0f, -1.0f, 1.0f);
258 glVertex3f(-1.0f, 1.0f, 1.0f);
259 glVertex3f(-1.0f, 1.0f, -1.0f);
260 glNormal3f( 1.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);
269 init_ball (ModeInfo *mi)
271 #define SPHERE_SLICES 32 /* how densely to render spheres */
272 #define SPHERE_STACKS 16
273 int wire = MI_IS_WIREFRAME(mi);
275 sp = malloc(sizeof(*sp));
277 fprintf(stderr,"Could not allocate memory\n");
280 if( (bscale.wh < 1) ||
282 fprintf(stderr,"Boxsize out of range. Using default\n");
286 des_amt = bscale.wh / MAX_COUNT;
289 reshape_ball(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
290 ballList = glGenLists(1);
291 glNewList(ballList, GL_COMPILE);
292 unit_sphere (SPHERE_STACKS, SPHERE_SLICES, wire);
295 boxList = glGenLists(1);
296 glNewList(boxList, GL_COMPILE);
302 glEnable(GL_COLOR_MATERIAL);
303 glShadeModel(GL_SMOOTH);
304 glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
306 glEnable(GL_DEPTH_TEST);
307 glDepthFunc(GL_LEQUAL);
308 glEnable(GL_LIGHTING);
310 glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
311 glLightfv(GL_LIGHT1, GL_POSITION,LightPosition);
315 glDisable(GL_DEPTH_TEST);
320 CheckBoxPos(GLfloat bot_x, GLfloat top_x, GLfloat bot_y, GLfloat top_y)
322 /*Make sure it's inside of the bounding box*/
323 bpos.x = ((bpos.x - bscale.wh) < bot_x) ? bot_x + bscale.wh : bpos.x;
324 bpos.x = ((bpos.x + bscale.wh) > top_x) ? top_x - bscale.wh : bpos.x;
325 bpos.y = ((bpos.y - bscale.wh) < bot_y) ? bot_y + bscale.wh : bpos.y;
326 bpos.y = ((bpos.y + bscale.wh) > top_y) ? top_y - bscale.wh : bpos.y;
330 draw_ball (ModeInfo *mi)
332 Display *dpy = MI_DISPLAY(mi);
333 Window window = MI_WINDOW(mi);
335 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
341 glRotated(0.25,0,0,1);
342 glRotated(0.25,0,1,0);
343 glRotated(0.25,1,0,0);
347 glScalef(0.5,0.5,0.5);
351 glTranslatef(ball.x += mo.x,
356 glCallList(ballList);
363 bpos.x = lside.pos.z*-1;
364 bpos.y = lside.pos.y;
365 bpos.z = bbox.bottom.x - bscale.d;
367 CheckBoxPos(bbox.bottom.z,bbox.top.z,bbox.bottom.y,bbox.top.y);
372 bpos.x = rside.pos.z*-1;
373 bpos.y = rside.pos.y;
374 bpos.z = bbox.top.x + bscale.d;
376 CheckBoxPos(bbox.bottom.z,bbox.top.z,bbox.bottom.y,bbox.top.y);
381 bpos.x = tside.pos.x;
382 bpos.y = tside.pos.z;
383 bpos.z = bbox.bottom.y - bscale.d;
385 CheckBoxPos(bbox.bottom.x,bbox.top.x,bbox.bottom.z,bbox.top.z);
390 bpos.x = bside.pos.x;
391 bpos.y = bside.pos.z;
392 bpos.z = bbox.top.y + bscale.d;
394 CheckBoxPos(bbox.bottom.x,bbox.top.x,bbox.bottom.z,bbox.top.z);
399 bpos.x = fside.pos.y;
400 bpos.y = fside.pos.x*-1;
401 bpos.z = bbox.top.z + bscale.d;
403 CheckBoxPos(bbox.bottom.y,bbox.top.y,bbox.bottom.x,bbox.top.x);
408 bpos.x = aside.pos.y;
409 bpos.y = aside.pos.x*-1;
410 bpos.z = bbox.bottom.z + bscale.d;
412 CheckBoxPos(bbox.bottom.y,bbox.top.y,bbox.bottom.x,bbox.top.x);
418 glColor4f(sp->color[0],sp->color[1],sp->color[2],1-(ALPHA_AMT * sp->alpha_count));
420 glColor3fv(sp->color);
422 glBlendFunc(GL_SRC_ALPHA,GL_ONE);
424 glRotatef(sp->rot[0],sp->rot[1],sp->rot[2],sp->rot[3]);
425 glTranslatef(bpos.x,bpos.y,bpos.z);
427 glScalef(bscale.wh-(des_amt*sp->des_count),bscale.wh-(des_amt*sp->des_count),bscale.d);
429 glScalef(bscale.wh,bscale.wh,bscale.d);
447 glXSwapBuffers(dpy, window);