1 /* boxed --- 3D bouncing balls that explode */
4 static const char sccsid[] = "@(#)boxed.c 0.9 01/09/26 xlockmore";
8 * Permission to use, copy, modify, and distribute this software and its
9 * documentation for any purpose and without fee is hereby granted,
10 * provided that the above copyright notice appear in all copies and that
11 * both that copyright notice and this permission notice appear in
12 * supporting documentation.
14 * This file is provided AS IS with no warranties of any kind. The author
15 * shall have no liability with respect to the infringement of copyrights,
16 * trade secrets or any patents by this file or any part thereof. In no
17 * event will the author be liable for any lost revenue or profits or
18 * other special, indirect and consequential damages.
22 * 2001: Written by Sander van Grieken <mailsander@gmx.net>
23 * as an OpenGL screensaver for the xscreensaver package.
24 * Lots of hardcoded values still in place. Also, there are some
25 * copy/paste leftovers from the gears hack. opts don't work.
27 * 2005: opts work. added options -balls, -ballsize, -explosion
29 * 2006: opts work. added option -decay
31 * 2008: opts work. added option -momentum
38 **----------------------------------------------------------------------------
40 **----------------------------------------------------------------------------
44 # define DEFAULTS "*delay: 20000 \n" \
45 "*showFPS: False \n" \
46 "*wireframe: False \n"
48 # define refresh_boxed 0
49 # define boxed_handle_event 0
50 # include "xlockmore.h" /* from the xscreensaver distribution */
51 #else /* !STANDALONE */
52 # include "xlock.h" /* from the xlockmore distribution */
53 #endif /* !STANDALONE */
57 # define DEF_SPEED "0.5"
58 # define DEF_BALLS "25"
59 # define DEF_BALLSIZE "2.0"
60 # define DEF_EXPLOSION "15.0"
61 # define DEF_DECAY "0.07"
62 # define DEF_MOMENTUM "0.6"
65 #define countof(x) (int)(sizeof((x))/sizeof((*x)))
67 #define rnd() (frand(1.0))
69 static GLfloat speed; /* jwz -- overall speed factor applied to all motion */
71 static GLfloat cfg_ballsize;
72 static GLfloat cfg_explosion;
73 static GLfloat cfg_decay;
74 static GLfloat cfg_momentum;
77 static XrmOptionDescRec opts[] = {
78 {"-speed", ".boxed.speed", XrmoptionSepArg, 0},
79 {"-balls", ".boxed.balls", XrmoptionSepArg, 0},
80 {"-ballsize", ".boxed.ballsize", XrmoptionSepArg, 0},
81 {"-explosion", ".boxed.explosion", XrmoptionSepArg, 0},
82 {"-decay", ".boxed.decay", XrmoptionSepArg, 0},
83 {"-momentum", ".boxed.momentum", XrmoptionSepArg, 0},
86 static argtype vars[] = {
87 {&speed, "speed", "Speed", DEF_SPEED, t_Float},
88 {&cfg_balls, "balls", "Balls", DEF_BALLS, t_Int},
89 {&cfg_ballsize, "ballsize", "Ball Size", DEF_BALLSIZE, t_Float},
90 {&cfg_explosion, "explosion", "Explosion", DEF_EXPLOSION, t_Float},
91 {&cfg_decay, "decay", "Explosion Decay", DEF_DECAY, t_Float},
92 {&cfg_momentum, "momentum", "Explosion Momentum", DEF_MOMENTUM, t_Float},
95 ENTRYPOINT ModeSpecOpt boxed_opts = {countof(opts), opts, countof(vars), vars, NULL};
99 ModStruct boxed_description = {
100 "boxed", "init_boxed", "draw_boxed", "release_boxed",
101 "draw_boxed", "init_boxed", NULL, &boxed_opts,
102 1000, 1, 2, 1, 4, 1.0, "",
103 "Shows GL's boxed balls", 0, NULL};
112 #define CAM_HEIGHT 100.0f
113 #define CAMDISTANCE_MIN 20.0
114 #define CAMDISTANCE_MAX 150.0
115 #define CAMDISTANCE_SPEED 1.5
117 /* rendering the sphere */
119 #define SPHERE_VERTICES (2+MESH_SIZE*MESH_SIZE*2)
120 #define SPHERE_INDICES ((MESH_SIZE*4 + MESH_SIZE*4*(MESH_SIZE-1))*3)
123 **-----------------------------------------------------------------------------
125 **-----------------------------------------------------------------------------
185 float cam_x_speed, cam_z_speed, cam_y_speed;
189 vectorf spherev[SPHERE_VERTICES];
190 GLint spherei[SPHERE_INDICES];
193 GLXContext *glx_context;
202 #define GLL_PATTERN 0
207 **----------------------------------------------------------------------------
209 **----------------------------------------------------------------------------
212 static boxedstruct *boxed = NULL;
216 **----------------------------------------------------------------------------
218 **----------------------------------------------------------------------------
224 static inline void addvectors(vectorf *dest, vectorf *s1, vectorf *s2)
226 dest->x = s1->x + s2->x;
227 dest->y = s1->y + s2->y;
228 dest->z = s1->z + s2->z;
234 static inline void subvectors(vectorf *dest, vectorf* s1, vectorf *s2)
236 dest->x = s1->x - s2->x;
237 dest->y = s1->y - s2->y;
238 dest->z = s1->z - s2->z;
242 * Multiply vector with scalar (scale vector)
244 static inline void scalevector(vectorf *dest, vectorf *source, GLfloat sc)
246 dest->x = source->x * sc;
247 dest->y = source->y * sc;
248 dest->z = source->z * sc;
254 static inline void copyvector(vectorf *dest, vectorf* source)
262 static inline GLfloat
263 dotproduct(vectorf * v1, vectorf * v2)
265 return v1->x * v2->x + v1->y * v2->y + v1->z * v2->z;
268 static inline GLfloat
269 squaremagnitude(vectorf * v)
271 return v->x * v->x + v->y * v->y + v->z * v->z;
274 static inline GLfloat
275 squaremagnitudehorz(vectorf * v)
277 return v->x * v->x + v->z * v->z;
283 * Generate the Sphere data
288 static void generatesphere(boxedstruct *gp)
290 float dj = M_PI/(MESH_SIZE+1.0f);
291 float di = M_PI/MESH_SIZE;
292 int v; /* vertex offset */
293 int ind; /* indices offset */
295 GLfloat r_y_plane, h_y_plane;
300 * generate the sphere data
301 * vertices 0 and 1 are the north and south poles
304 spherei = gp->spherei;
305 spherev = gp->spherev;
307 spherev[0].x = 0.0f; spherev[0].y =1.0f; spherev[0].z = 0.0f;
308 spherev[1].x = 0.0f; spherev[1].y =-1.0f; spherev[1].z = 0.0f;
310 for (j=0; j<MESH_SIZE; j++) {
311 r_y_plane = (float)sin((j+1) * dj);
312 h_y_plane = (float)cos((j+1) * dj);
313 for (i=0; i<MESH_SIZE*2; i++) {
314 si = 2+i+j*MESH_SIZE*2;
315 spherev[si].y = h_y_plane;
316 spherev[si].x = (float) sin(i * di) * r_y_plane;
317 spherev[si].z = (float) cos(i * di) * r_y_plane;
321 /* generate indices */
322 for (i=0; i<MESH_SIZE*2; i++) {
324 spherei[3*i+1] = i+2;
325 spherei[3*i+2] = i+3;
326 if (i==MESH_SIZE*2-1)
330 /* the middle strips */
331 for (j=0; j<MESH_SIZE-1; j++) {
333 ind = 3*MESH_SIZE*2 + j*6*MESH_SIZE*2;
334 for (i=0; i<MESH_SIZE*2; i++) {
335 spherei[6*i+ind] = v+i;
336 spherei[6*i+2+ind] = v+i+1;
337 spherei[6*i+1+ind] = v+i+MESH_SIZE*2;
339 spherei[6*i+ind+3] = v+i+MESH_SIZE*2;
340 spherei[6*i+2+ind+3] = v+i+1;
341 spherei[6*i+1+ind+3] = v+i+MESH_SIZE*2+1;
342 if (i==MESH_SIZE*2-1) {
343 spherei[6*i+2+ind] = v+i+1-2*MESH_SIZE;
344 spherei[6*i+2+ind+3] = v+i+1-2*MESH_SIZE;
345 spherei[6*i+1+ind+3] = v+i+MESH_SIZE*2+1-2*MESH_SIZE;
350 v = SPHERE_VERTICES-MESH_SIZE*2;
351 ind = SPHERE_INDICES-3*MESH_SIZE*2;
352 for (i=0; i<MESH_SIZE*2; i++) {
353 spherei[3*i+ind] = 1;
354 spherei[3*i+1+ind] = v+i+1;
355 spherei[3*i+2+ind] = v+i;
356 if (i==MESH_SIZE*2-1)
357 spherei[3*i+1+ind] = v;
368 static void createball(ball *newball)
370 float r=0.0f,g=0.0f,b=0.0f;
371 newball->loc.x = 5-10*rnd();
372 newball->loc.y = 35+20*rnd();
373 newball->loc.z = 5-10*rnd();
374 newball->dir.x = (0.5f-rnd()) * speed;
375 newball->dir.y = 0.0;
376 newball->dir.z = (0.5-rnd()) * speed;
377 newball->offside = 0;
378 newball->bounced = FALSE;
379 newball->radius = cfg_ballsize;
380 while (r+g+b < 1.8f ) {
381 newball->color.x = r=rnd();
382 newball->color.y = g=rnd();
383 newball->color.z = b=rnd();
385 newball->justcreated = TRUE;
388 /* Update position of each ball */
390 static void updateballs(ballman *bman)
393 vectorf dvect,richting,relspeed,influence;
396 for (b=0;b<bman->num_balls;b++) {
398 GLfloat gravity = 0.30f * speed;
401 bman->balls[b].dir.y -= gravity;
403 addvectors(&bman->balls[b].loc,&bman->balls[b].loc,&bman->balls[b].dir);
405 if (bman->balls[b].loc.y < bman->balls[b].radius) { /* ball onder bodem? (bodem @ y=0) */
406 if ((bman->balls[b].loc.x < -95.0) ||
407 (bman->balls[b].loc.x > 95.0) ||
408 (bman->balls[b].loc.z < -95.0) ||
409 (bman->balls[b].loc.z > 95.0)) {
410 if (bman->balls[b].loc.y < -1000.0)
411 createball(&bman->balls[b]);
413 bman->balls[b].loc.y = bman->balls[b].radius + (bman->balls[b].radius - bman->balls[b].loc.y);
414 bman->balls[b].dir.y = -bman->balls[b].dir.y;
415 if (bman->balls[b].offside) {
416 bman->balls[b].bounced = TRUE; /* temporary disable painting ball */
417 scalevector(&bman->balls[b].dir,&bman->balls[b].dir,0.80f);
418 if (squaremagnitude(&bman->balls[b].dir) < 0.08f) {
419 createball(&bman->balls[b]);
421 if (squaremagnitudehorz(&bman->balls[b].dir) < 0.005f) {
422 createball(&bman->balls[b]);
428 if (!bman->balls[b].offside) {
429 if (bman->balls[b].loc.x - bman->balls[b].radius < -20.0f) { /* x ondergrens */
430 if (bman->balls[b].loc.y > 41+bman->balls[b].radius) bman->balls[b].offside=1;
432 bman->balls[b].dir.x = -bman->balls[b].dir.x;
433 bman->balls[b].loc.x = -20.0f + bman->balls[b].radius;
436 if (bman->balls[b].loc.x + bman->balls[b].radius > 20.0f) { /* x bovengrens */
437 if (bman->balls[b].loc.y > 41+bman->balls[b].radius) bman->balls[b].offside=1;
439 bman->balls[b].dir.x = -bman->balls[b].dir.x;
440 bman->balls[b].loc.x = 20.0f - bman->balls[b].radius;
443 if (bman->balls[b].loc.z - bman->balls[b].radius < -20.0f) { /* z ondergrens */
444 if (bman->balls[b].loc.y > 41+bman->balls[b].radius) bman->balls[b].offside=1;
446 bman->balls[b].dir.z = -bman->balls[b].dir.z;
447 bman->balls[b].loc.z = -20.0f + bman->balls[b].radius;
450 if (bman->balls[b].loc.z + bman->balls[b].radius > 20.0f) { /* z bovengrens */
451 if (bman->balls[b].loc.y > 41+bman->balls[b].radius) bman->balls[b].offside=1;
453 bman->balls[b].dir.z = -bman->balls[b].dir.z;
454 bman->balls[b].loc.z = 20.0f - bman->balls[b].radius;
457 } /* end if !offside */
459 /* check voor stuiteren */
460 for (j=b+1;j<bman->num_balls;j++) {
461 squaredist = (bman->balls[b].radius * bman->balls[b].radius) + (bman->balls[j].radius * bman->balls[j].radius);
462 subvectors(&dvect,&bman->balls[b].loc,&bman->balls[j].loc);
463 if ( squaremagnitude(&dvect) < squaredist ) { /* balls b and j touch */
464 subvectors(&richting,&bman->balls[j].loc,&bman->balls[b].loc);
465 subvectors(&relspeed,&bman->balls[b].dir,&bman->balls[j].dir);
466 /* calc mutual influence direction and magnitude */
467 scalevector(&influence,&richting,(dotproduct(&richting,&relspeed)/squaremagnitude(&richting)));
469 subvectors(&bman->balls[b].dir,&bman->balls[b].dir,&influence);
470 addvectors(&bman->balls[j].dir,&bman->balls[j].dir,&influence);
471 addvectors(&bman->balls[b].loc,&bman->balls[b].loc,&bman->balls[b].dir);
472 addvectors(&bman->balls[j].loc,&bman->balls[j].loc,&bman->balls[j].dir);
474 subvectors(&dvect,&bman->balls[b].loc,&bman->balls[j].loc);
475 while (squaremagnitude(&dvect) < squaredist) {
476 addvectors(&bman->balls[b].loc,&bman->balls[b].loc,&bman->balls[b].dir);
477 addvectors(&bman->balls[j].loc,&bman->balls[j].loc,&bman->balls[j].dir);
478 subvectors(&dvect,&bman->balls[b].loc,&bman->balls[j].loc);
487 * explode ball into triangles
490 static void createtrisfromball(triman* tman, vectorf *spherev, GLint *spherei, int ind_num, ball *b)
497 vectorf avgdir,dvect,mvect;
499 tman->scalefac = b->radius;
500 copyvector(&tman->color,&b->color);
501 explosion = 1.0f + tman->explosion * 2.0 * rnd();
502 momentum = tman->momentum;
504 tman->num_tri = ind_num/3;
506 /* reserveer geheugen voor de poly's in een bal */
508 tman->tris = (tri *)malloc(tman->num_tri * sizeof(tri));
509 tman->vertices = (vectorf *)malloc(ind_num * sizeof(vectorf));
510 tman->normals = (vectorf *)malloc(ind_num/3 * sizeof(vectorf));
512 for (i=0; i<(tman->num_tri); i++) {
513 tman->tris[i].far = FALSE;
514 tman->tris[i].gone = FALSE;
516 /* kopieer elke poly apart naar een tri structure */
517 copyvector(&tman->vertices[pos+0],&spherev[spherei[pos+0]]);
518 copyvector(&tman->vertices[pos+1],&spherev[spherei[pos+1]]);
519 copyvector(&tman->vertices[pos+2],&spherev[spherei[pos+2]]);
520 /* Calculate average direction of shrapnel */
521 addvectors(&avgdir,&tman->vertices[pos+0],&tman->vertices[pos+1]);
522 addvectors(&avgdir,&avgdir,&tman->vertices[pos+2]);
523 scalevector(&avgdir,&avgdir,0.33333);
525 /* should normalize first, NYI */
526 copyvector(&tman->normals[i],&avgdir);
528 /* copy de lokatie */
529 addvectors(&tman->tris[i].loc,&b->loc,&avgdir);
530 /* en translate alle triangles terug naar hun eigen oorsprong */
531 tman->vertices[pos+0].x -= avgdir.x;
532 tman->vertices[pos+0].y -= avgdir.y;
533 tman->vertices[pos+0].z -= avgdir.z;
534 tman->vertices[pos+1].x -= avgdir.x;
535 tman->vertices[pos+1].y -= avgdir.y;
536 tman->vertices[pos+1].z -= avgdir.z;
537 tman->vertices[pos+2].x -= avgdir.x;
538 tman->vertices[pos+2].y -= avgdir.y;
539 tman->vertices[pos+2].z -= avgdir.z;
540 /* alwaar opschaling plaatsvindt */
541 scale = b->radius * 2;
542 scalevector(&tman->vertices[pos+0],&tman->vertices[pos+0],scale);
543 scalevector(&tman->vertices[pos+1],&tman->vertices[pos+1],scale);
544 scalevector(&tman->vertices[pos+2],&tman->vertices[pos+2],scale);
546 tman->vertices[pos+0].x += avgdir.x;
547 tman->vertices[pos+0].y += avgdir.y;
548 tman->vertices[pos+0].z += avgdir.z;
549 tman->vertices[pos+1].x += avgdir.x;
550 tman->vertices[pos+1].y += avgdir.y;
551 tman->vertices[pos+1].z += avgdir.z;
552 tman->vertices[pos+2].x += avgdir.x;
553 tman->vertices[pos+2].y += avgdir.y;
554 tman->vertices[pos+2].z += avgdir.z;
556 /* bereken nieuwe richting */
557 scalevector(&tman->tris[i].dir,&avgdir,explosion);
558 dvect.x = (0.1f - 0.2f*rnd());
559 dvect.y = (0.15f - 0.3f*rnd());
560 dvect.z = (0.1f - 0.2f*rnd());
561 addvectors(&tman->tris[i].dir,&tman->tris[i].dir,&dvect);
563 /* add ball's momentum to each piece of the exploded ball */
564 mvect.x = b->dir.x * momentum;
566 mvect.z = b->dir.z * momentum;
567 addvectors(&tman->tris[i].dir,&tman->tris[i].dir,&mvect);
573 * update position of each tri
576 static void updatetris(triman *t)
581 for (b=0;b<t->num_tri;b++) {
582 /* the exploded triangles disappear over time */
583 if (rnd() < t->decay) { t->tris[b].gone = TRUE; }
585 t->tris[b].dir.y -= (0.1f * speed);
587 addvectors(&t->tris[b].loc,&t->tris[b].loc,&t->tris[b].dir);
589 if (t->tris[b].far) continue;
590 if (t->tris[b].loc.y < 0) { /* onder bodem ? */
591 if ((t->tris[b].loc.x > -95.0f) &
592 (t->tris[b].loc.x < 95.0f) &
593 (t->tris[b].loc.z > -95.0f) &
594 (t->tris[b].loc.z < 95.0f)) { /* in veld */
595 t->tris[b].dir.y = -(t->tris[b].dir.y);
596 t->tris[b].loc.y = -t->tris[b].loc.y;
597 scalevector(&t->tris[b].dir,&t->tris[b].dir,0.80f); /* dampening */
600 t->tris[b].far = TRUE;
605 if ((t->tris[b].loc.x > -21.0f) &
606 (t->tris[b].loc.x < 21.0f) &
607 (t->tris[b].loc.z > -21.0f) &
608 (t->tris[b].loc.z < 21.0f)) { /* in box? */
610 xd = zd = 999.0f; /* big */
611 if ((t->tris[b].loc.x > -21.0f) &
612 (t->tris[b].loc.x < 0)) {
613 xd = t->tris[b].loc.x + 21.0f;
615 if ((t->tris[b].loc.x < 21.0f) &
616 (t->tris[b].loc.x > 0)) {
617 xd = 21.0f - t->tris[b].loc.x;
619 if ((t->tris[b].loc.z > -21.0f) &
620 (t->tris[b].loc.z < 0)) {
621 zd = t->tris[b].loc.z + 21.0f;
623 if ((t->tris[b].loc.z < 21.0f) &
624 (t->tris[b].loc.z > 0)) {
625 zd = 21.0f - t->tris[b].loc.z;
629 if (t->tris[b].dir.x < 0)
630 t->tris[b].loc.x += (21.0f - t->tris[b].loc.x);
632 t->tris[b].loc.x += (-21.0f - t->tris[b].loc.x);
633 t->tris[b].dir.x = -t->tris[b].dir.x;
636 if (t->tris[b].dir.z < 0)
637 t->tris[b].loc.z += (21.0f - t->tris[b].loc.z);
639 t->tris[b].loc.z += (-21.0f - t->tris[b].loc.z);
640 t->tris[b].dir.z = -t->tris[b].dir.z;
649 * free memory allocated by a tri manager
651 static void freetris(triman *t)
654 if (t->tris) free(t->tris);
655 if (t->vertices) free(t->vertices);
656 if (t->normals) free(t->normals);
666 *load defaults in config structure
668 static void setdefaultconfig(boxed_config *config)
670 cfg_balls = MAX(3,MIN(40,cfg_balls));
671 cfg_ballsize = MAX(1.0f,MIN(5.0f,cfg_ballsize));
672 cfg_explosion = MAX(0.0f,MIN(50.0f,cfg_explosion));
673 cfg_decay = MAX(0.0f,MIN(1.0f,cfg_decay));
674 cfg_momentum = MAX(0.0f,MIN(1.0f,cfg_momentum));
676 config->numballs = cfg_balls;
677 config->textures = TRUE;
678 config->transparent = FALSE;
679 config->explosion = cfg_explosion;
680 config->decay = cfg_decay;
681 config->momentum = cfg_momentum;
682 config->ballsize = cfg_ballsize;
683 config->camspeed = 35.0f;
690 static int drawfilledbox(boxedstruct *boxed, int wire)
692 /* draws texture filled box,
693 top is drawn using the entire texture,
694 the sides are drawn using the edge of the texture
699 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
701 glVertex3f(-1.0,1.0,1.0);
703 glVertex3f(1.0,1.0,1.0);
705 glVertex3f(1.0,-1.0,1.0);
707 glVertex3f(-1.0,-1.0,1.0);
711 glVertex3f(1.0,1.0,-1.0);
713 glVertex3f(-1.0,1.0,-1.0);
715 glVertex3f(-1.0,-1.0,-1.0);
717 glVertex3f(1.0,-1.0,-1.0);
721 glVertex3f(-1.0,1.0,1.0);
723 glVertex3f(-1.0,-1.0,1.0);
725 glVertex3f(-1.0,-1.0,-1.0);
727 glVertex3f(-1.0,1.0,-1.0);
731 glVertex3f(1.0,1.0,1.0);
733 glVertex3f(1.0,1.0,-1.0);
735 glVertex3f(1.0,-1.0,-1.0);
737 glVertex3f(1.0,-1.0,1.0);
740 glTexCoord2f(0.0,0.0);
741 glVertex3f(-1.0,1.0,1.0);
742 glTexCoord2f(0.0,1.0);
743 glVertex3f(-1.0,1.0,-1.0);
744 glTexCoord2f(1.0,1.0);
745 glVertex3f(1.0,1.0,-1.0);
746 glTexCoord2f(1.0,0.0);
747 glVertex3f(1.0,1.0,1.0);
751 glVertex3f(-1.0,-1.0,1.0);
753 glVertex3f(-1.0,-1.0,-1.0);
755 glVertex3f(1.0,-1.0,-1.0);
757 glVertex3f(1.0,-1.0,1.0);
766 * Draw a box made of lines
768 static int drawbox(boxedstruct *boxed)
772 glBegin(GL_LINE_STRIP);
773 glVertex3f(-1.0,1.0,1.0);
774 glVertex3f(-1.0,1.0,-1.0); polys++;
775 glVertex3f(1.0,1.0,-1.0); polys++;
776 glVertex3f(1.0,1.0,1.0); polys++;
777 glVertex3f(-1.0,1.0,1.0); polys++;
780 glBegin(GL_LINE_STRIP);
781 glVertex3f(-1.0,-1.0,1.0);
782 glVertex3f(1.0,-1.0,1.0); polys++;
783 glVertex3f(1.0,-1.0,-1.0); polys++;
784 glVertex3f(-1.0,-1.0,-1.0); polys++;
785 glVertex3f(-1.0,-1.0,1.0); polys++;
787 /* connect top & bottom */
789 glVertex3f(-1.0,1.0,1.0);
790 glVertex3f(-1.0,-1.0,1.0); polys++;
791 glVertex3f(1.0,1.0,1.0);
792 glVertex3f(1.0,-1.0,1.0); polys++;
793 glVertex3f(1.0,1.0,-1.0);
794 glVertex3f(1.0,-1.0,-1.0); polys++;
795 glVertex3f(-1.0,1.0,-1.0);
796 glVertex3f(-1.0,-1.0,-1.0); polys++;
806 static int drawball(boxedstruct *gp, ball *b, int wire)
810 GLint *spherei = gp->spherei;
811 vectorf *spherev = gp->spherev;
816 glTranslatef(b->loc.x,b->loc.y,b->loc.z);
817 glScalef(b->radius,b->radius,b->radius);
818 glColor3f(b->color.x,b->color.y,b->color.z);
822 glMaterialfv(GL_FRONT, GL_DIFFUSE, col);
826 glMaterialfv(GL_FRONT, GL_EMISSION,col);
828 if (!gp->gllists[GLL_BALL]) {
829 glNewList(gp->listobjects + GLL_BALL,GL_COMPILE);
830 cnt = SPHERE_INDICES/3;
831 for (i=0; i<cnt; i++) {
833 glBegin(wire ? GL_LINE_LOOP : GL_TRIANGLES);
834 glNormal3f(spherev[spherei[pos+0]].x,spherev[spherei[pos+0]].y,spherev[spherei[pos+0]].z);
835 glVertex3f(spherev[spherei[pos+0]].x,spherev[spherei[pos+0]].y,spherev[spherei[pos+0]].z);
836 glNormal3f(spherev[spherei[pos+1]].x,spherev[spherei[pos+1]].y,spherev[spherei[pos+1]].z);
837 gp->list_polys[GLL_BALL]++;
838 glVertex3f(spherev[spherei[pos+1]].x,spherev[spherei[pos+1]].y,spherev[spherei[pos+1]].z);
839 glNormal3f(spherev[spherei[pos+2]].x,spherev[spherei[pos+2]].y,spherev[spherei[pos+2]].z);
840 glVertex3f(spherev[spherei[pos+2]].x,spherev[spherei[pos+2]].y,spherev[spherei[pos+2]].z);
841 gp->list_polys[GLL_BALL]++;
845 gp->gllists[GLL_BALL] = 1;
847 glCallList(gp->listobjects + GLL_BALL);
848 polys += gp->list_polys[GLL_BALL];
857 * Draw all triangles in triman
859 static int drawtriman(triman *t, int wire)
863 vectorf *spherev = t->vertices;
867 glColor3f(t->color.x,t->color.y,t->color.z);
871 glMaterialfv(GL_FRONT, GL_DIFFUSE, col);
875 glMaterialfv(GL_FRONT, GL_EMISSION,col);
877 for (i=0; i<t->num_tri; i++) {
878 if (t->tris[i].gone == TRUE) { continue; }
881 glTranslatef(t->tris[i].loc.x,t->tris[i].loc.y,t->tris[i].loc.z);
882 glBegin(wire ? GL_LINE_LOOP : GL_TRIANGLES);
883 glNormal3f(t->normals[i].x,t->normals[i].y,t->normals[i].z);
884 glVertex3f(spherev[pos+0].x,spherev[pos+0].y,spherev[pos+0].z);
885 glVertex3f(spherev[pos+1].x,spherev[pos+1].y,spherev[pos+1].z);
886 glVertex3f(spherev[pos+2].x,spherev[pos+2].y,spherev[pos+2].z);
898 static int drawpattern(boxedstruct *gp)
901 if (!gp->gllists[GLL_PATTERN]) {
902 glNewList(gp->listobjects + GLL_PATTERN, GL_COMPILE);
904 glBegin(GL_LINE_STRIP);
905 glVertex3f(-25.0f, 0.0f, 35.0f);
906 glVertex3f(-15.0f, 0.0f, 35.0f); gp->list_polys[GLL_PATTERN]++;
907 glVertex3f(-5.0f, 0.0f, 25.0f); gp->list_polys[GLL_PATTERN]++;
908 glVertex3f(5.0f, 0.0f, 25.0f); gp->list_polys[GLL_PATTERN]++;
909 glVertex3f(15.0f, 0.0f, 35.0f); gp->list_polys[GLL_PATTERN]++;
910 glVertex3f(25.0f, 0.0f, 35.0f); gp->list_polys[GLL_PATTERN]++;
911 glVertex3f(35.0f, 0.0f, 25.0f); gp->list_polys[GLL_PATTERN]++;
912 glVertex3f(35.0f, 0.0f, 15.0f); gp->list_polys[GLL_PATTERN]++;
913 glVertex3f(25.0f, 0.0f, 5.0f); gp->list_polys[GLL_PATTERN]++;
914 glVertex3f(25.0f, 0.0f, -5.0f); gp->list_polys[GLL_PATTERN]++;
915 glVertex3f(35.0f, 0.0f, -15.0f); gp->list_polys[GLL_PATTERN]++;
916 glVertex3f(35.0f, 0.0f, -25.0f); gp->list_polys[GLL_PATTERN]++;
917 glVertex3f(25.0f, 0.0f, -35.0f); gp->list_polys[GLL_PATTERN]++;
918 glVertex3f(15.0f, 0.0f,-35.0f); gp->list_polys[GLL_PATTERN]++;
919 glVertex3f(5.0f, 0.0f, -25.0f); gp->list_polys[GLL_PATTERN]++;
920 glVertex3f(-5.0f, 0.0f, -25.0f); gp->list_polys[GLL_PATTERN]++;
921 glVertex3f(-15.0f, 0.0f,-35.0f); gp->list_polys[GLL_PATTERN]++;
922 glVertex3f(-25.0f, 0.0f,-35.0f); gp->list_polys[GLL_PATTERN]++;
923 glVertex3f(-35.0f, 0.0f, -25.0f); gp->list_polys[GLL_PATTERN]++;
924 glVertex3f(-35.0f, 0.0f, -15.0f); gp->list_polys[GLL_PATTERN]++;
925 glVertex3f(-25.0f, 0.0f, -5.0f); gp->list_polys[GLL_PATTERN]++;
926 glVertex3f(-25.0f, 0.0f, 5.0f); gp->list_polys[GLL_PATTERN]++;
927 glVertex3f(-35.0f, 0.0f, 15.0f); gp->list_polys[GLL_PATTERN]++;
928 glVertex3f(-35.0f, 0.0f, 25.0f); gp->list_polys[GLL_PATTERN]++;
929 glVertex3f(-25.0f, 0.0f, 35.0f); gp->list_polys[GLL_PATTERN]++;
932 glBegin(GL_LINE_STRIP);
933 glVertex3f(-5.0f, 0.0f, 15.0f);
934 glVertex3f(5.0f, 0.0f, 15.0f); gp->list_polys[GLL_PATTERN]++;
935 glVertex3f(15.0f, 0.0f, 5.0f); gp->list_polys[GLL_PATTERN]++;
936 glVertex3f(15.0f, 0.0f, -5.0f); gp->list_polys[GLL_PATTERN]++;
937 glVertex3f(5.0f, 0.0f, -15.0f); gp->list_polys[GLL_PATTERN]++;
938 glVertex3f(-5.0f, 0.0f, -15.0f); gp->list_polys[GLL_PATTERN]++;
939 glVertex3f(-15.0f, 0.0f, -5.0f); gp->list_polys[GLL_PATTERN]++;
940 glVertex3f(-15.0f, 0.0f, 5.0f); gp->list_polys[GLL_PATTERN]++;
941 glVertex3f(-5.0f, 0.0f, 15.0f); gp->list_polys[GLL_PATTERN]++;
945 gp->gllists[GLL_PATTERN] = 1;
947 glCallList(gp->listobjects + GLL_PATTERN);
948 polys += gp->list_polys[GLL_PATTERN];
956 * main rendering loop
958 static void draw(ModeInfo * mi)
960 boxedstruct *gp = &boxed[MI_SCREEN(mi)];
961 int wire = MI_IS_WIREFRAME (mi);
967 GLfloat dgray[4] = {0.3f, 0.3f, 0.3f, 1.0f};
968 GLfloat black[4] = {0.0f, 0.0f, 0.0f, 1.0f};
969 GLfloat lblue[4] = {0.4f,0.6f,1.0f };
971 GLfloat l0_ambient[] = {0.0, 0.0, 0.0, 1.0};
972 GLfloat l0_specular[] = {1.0, 1.0, 1.0, 1.0};
973 GLfloat l0_diffuse[] = {1.0, 1.0, 1.0, 1.0};
974 GLfloat l0_position[] = {0.0, 0.0, 0.0, 1.0}; /* w != 0 -> positional light */
975 GLfloat l1_ambient[] = {0.0, 0.0, 0.0, 1.0};
976 GLfloat l1_specular[] = {1.0, 1.0, 1.0, 1.0};
977 GLfloat l1_diffuse[] = {0.5, 0.5, 0.5, 1.0};
978 GLfloat l1_position[] = {0.0, 1.0, 0.0, 0.0}; /* w = 0 -> directional light */
980 mi->polygon_count = 0;
982 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
985 glRotatef(current_device_rotation(), 0, 0, 1);
988 gp->camtic += 0.01f + 0.01f * sin(gp->tic * speed);
990 /* rotate camera around (0,0,0), looking at (0,0,0), up is (0,1,0) */
991 dcam = CAMDISTANCE_MIN + (CAMDISTANCE_MAX - CAMDISTANCE_MIN) + (CAMDISTANCE_MAX - CAMDISTANCE_MIN)*cos((gp->camtic/CAMDISTANCE_SPEED) * speed);
992 v1.x = dcam * sin((gp->camtic/gp->cam_x_speed) * speed);
993 v1.z = dcam * cos((gp->camtic/gp->cam_z_speed) * speed);
994 v1.y = CAM_HEIGHT * sin((gp->camtic/gp->cam_y_speed) * speed) + 1.02 * CAM_HEIGHT;
995 gluLookAt(v1.x,v1.y,v1.z,0.0,0.0,0.0,0.0,1.0,0.0);
998 glLightfv(GL_LIGHT0, GL_AMBIENT, l0_ambient);
999 glLightfv(GL_LIGHT0, GL_DIFFUSE, l0_diffuse);
1000 glLightfv(GL_LIGHT0, GL_SPECULAR, l0_specular);
1001 glLightfv(GL_LIGHT0, GL_POSITION, l0_position);
1002 glLightfv(GL_LIGHT1, GL_AMBIENT, l1_ambient);
1003 glLightfv(GL_LIGHT1, GL_DIFFUSE, l1_diffuse);
1004 glLightfv(GL_LIGHT1, GL_SPECULAR, l1_specular);
1005 glLightfv(GL_LIGHT1, GL_POSITION, l1_position);
1006 glEnable(GL_LIGHT0);
1007 glEnable(GL_LIGHT1);
1011 glMaterialfv(GL_FRONT, GL_SPECULAR, black);
1012 glMaterialfv(GL_FRONT, GL_EMISSION, lblue);
1013 glMaterialfv(GL_FRONT,GL_AMBIENT,black);
1014 glMaterialf(GL_FRONT, GL_SHININESS, 5.0);
1018 /* draw ground grid */
1019 /* glDisable(GL_DEPTH_TEST); */
1020 glDisable(GL_LIGHTING);
1022 glColor3f(0.1,0.1,0.6);
1023 for (dx= -2; dx<3; dx++) {
1024 for (dz= -2; dz<3; dz++) {
1026 glTranslatef(dx*30.0f, 0.0f, dz*30.0f);
1032 /* Set drawing mode for the boxes */
1033 glEnable(GL_DEPTH_TEST);
1034 if (!wire) glEnable(GL_TEXTURE_2D);
1036 glColor3f(1.0,1.0,1.0);
1037 glScalef(20.0,0.25,20.0);
1038 glTranslatef(0.0,2.0,0.0);
1039 mi->polygon_count += drawfilledbox(gp, wire);
1041 glDisable(GL_TEXTURE_2D);
1044 glColor3f(0.2,0.5,0.2);
1045 glScalef(20.0,20.0,0.25);
1046 glTranslatef(0.0,1.0,81.0);
1047 mi->polygon_count += drawbox(gp);
1051 glColor3f(0.2,0.5,0.2);
1052 glScalef(20.0,20.0,0.25);
1053 glTranslatef(0.0,1.0,-81.0);
1054 mi->polygon_count += drawbox(gp);
1058 glColor3f(0.2,0.5,0.2);
1059 glScalef(.25,20.0,20.0);
1060 glTranslatef(-81.0,1.0,0.0);
1061 mi->polygon_count += drawbox(gp);
1065 glColor3f(0.2,0.5,0.2);
1066 glScalef(.25,20.0,20.0);
1067 glTranslatef(81.0,1.0,0.0);
1068 mi->polygon_count += drawbox(gp);
1072 glEnable(GL_LIGHTING);
1074 glMaterialfv(GL_FRONT, GL_DIFFUSE, dgray);
1075 glMaterialfv(GL_FRONT, GL_EMISSION, black); /* turn it off before painting the balls */
1078 /* move the balls and shrapnel */
1079 updateballs(&gp->bman);
1081 glFrontFace(GL_CCW);
1082 for (i=0;i<gp->bman.num_balls;i++) {
1083 if (gp->bman.balls[i].justcreated) {
1084 gp->bman.balls[i].justcreated = FALSE;
1085 freetris(&gp->tman[i]);
1087 if (gp->bman.balls[i].bounced) {
1088 if (gp->tman[i].vertices == NULL) {
1089 createtrisfromball(&gp->tman[i],gp->spherev,gp->spherei,SPHERE_INDICES,&gp->bman.balls[i]);
1091 updatetris(&gp->tman[i]);
1093 glDisable(GL_CULL_FACE);
1094 mi->polygon_count += drawtriman(&gp->tman[i], wire);
1095 if (!wire) glEnable(GL_CULL_FACE);
1097 mi->polygon_count += drawball(gp, &gp->bman.balls[i], wire);
1107 * new window size or exposure
1109 ENTRYPOINT void reshape_boxed(ModeInfo *mi, int width, int height)
1111 GLfloat h = (GLfloat) height / (GLfloat) width;
1113 glViewport(0, 0, (GLint) width, (GLint) height);
1114 glMatrixMode(GL_PROJECTION);
1116 gluPerspective(50.0,1/h,2.0,1000.0);
1117 glMatrixMode (GL_MODELVIEW);
1125 pinit(ModeInfo * mi)
1127 boxedstruct *gp = &boxed[MI_SCREEN(mi)];
1128 int wire = MI_IS_WIREFRAME (mi);
1132 char *texpixeltarget;
1134 glShadeModel(GL_SMOOTH);
1136 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1138 /* Load configuration */
1139 setdefaultconfig(&gp->config);
1141 /* give the decay parameter a better curve */
1142 if (gp->config.decay <= 0.8182) { gp->config.decay = gp->config.decay / 3; }
1143 else { gp->config.decay = (gp->config.decay - 0.75) * 4; }
1147 bman->balls = (ball *)malloc(gp->config.numballs * sizeof(ball));
1148 bman->num_balls = gp->config.numballs;
1149 bman->ballsize = gp->config.ballsize;
1150 bman->explosion = gp->config.explosion;
1152 gp->tman = (triman *)malloc(bman->num_balls * sizeof(triman));
1153 memset(gp->tman,0,bman->num_balls * sizeof(triman));
1155 for(i=0;i<bman->num_balls;i++) {
1156 gp->tman[i].explosion = (float) (((int)gp->config.explosion) / 15.0f );
1157 gp->tman[i].decay = gp->config.decay;
1158 gp->tman[i].momentum = gp->config.momentum;
1159 gp->tman[i].vertices = NULL;
1160 gp->tman[i].normals = NULL;
1161 gp->tman[i].tris = NULL;
1162 createball(&bman->balls[i]);
1163 bman->balls[i].loc.y *= rnd();
1169 glEnable(GL_CULL_FACE);
1170 glEnable(GL_LIGHTING);
1173 /* define cam path */
1174 gp->cam_x_speed = 1.0f/((float)gp->config.camspeed/50.0 + rnd()*((float)gp->config.camspeed/50.0));
1175 gp->cam_z_speed = 1.0f/((float)gp->config.camspeed/50.0 + rnd()*((float)gp->config.camspeed/50.0));
1176 gp->cam_y_speed = 1.0f/((float)gp->config.camspeed/250.0 + rnd()*((float)gp->config.camspeed/250.0));
1177 if (rnd() < 0.5f) gp->cam_x_speed = -gp->cam_x_speed;
1178 if (rnd() < 0.5f) gp->cam_z_speed = -gp->cam_z_speed;
1180 /* define initial cam position */
1181 gp->tic = gp->camtic = rnd() * 100.0f;
1183 /* define tex1 (bottom plate) */
1184 gp->tex1 = (char *)malloc(3*width*height*sizeof(GLuint));
1185 texpixels = 256*256; /*width*height;*/
1186 texpixeldata = header_data;
1187 texpixeltarget = gp->tex1;
1188 for (i=0; i < texpixels; i++) {
1189 HEADER_PIXEL(texpixeldata,texpixeltarget);
1190 texpixeltarget += 3;
1194 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
1198 i = gluBuild2DMipmaps(GL_TEXTURE_2D, 3, 256, 256,
1199 GL_RGB, GL_UNSIGNED_BYTE, gp->tex1);
1202 const char *s = (char *) gluErrorString (i);
1203 fprintf (stderr, "%s: error mipmapping texture: %s\n",
1204 progname, (s ? s : "(unknown)"));
1207 check_gl_error("mipmapping");
1209 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, 256, 256, 0,
1210 GL_RGB, GL_UNSIGNED_BYTE,
1212 check_gl_error("texture");
1215 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1216 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
1217 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
1218 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1219 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1226 init_boxed(ModeInfo * mi)
1228 int screen = MI_SCREEN(mi);
1230 /* Colormap cmap; */
1231 /* Boolean rgba, doublebuffer, cmap_installed; */
1234 if (boxed == NULL) {
1235 if ((boxed = (boxedstruct *) calloc(MI_NUM_SCREENS(mi),sizeof (boxedstruct))) == NULL) return;
1237 gp = &boxed[screen];
1238 gp->window = MI_WINDOW(mi);
1240 if ((gp->glx_context = init_GL(mi)) != NULL) {
1241 reshape_boxed(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
1242 glDrawBuffer(GL_BACK);
1243 if (!glIsList(gp->listobjects)) {
1244 gp->listobjects = glGenLists(3);
1257 draw_boxed(ModeInfo * mi)
1259 boxedstruct *gp = &boxed[MI_SCREEN(mi)];
1260 Display *display = MI_DISPLAY(mi);
1261 Window window = MI_WINDOW(mi);
1263 if (!gp->glx_context)
1266 glDrawBuffer(GL_BACK);
1268 glXMakeCurrent(display, window, *(gp->glx_context));
1271 if (mi->fps_p) do_fps (mi);
1273 glXSwapBuffers(display, window);
1277 release_boxed(ModeInfo * mi)
1281 if (boxed != NULL) {
1284 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
1285 boxedstruct *gp = &boxed[screen];
1287 if (gp->glx_context) {
1288 /* Display lists MUST be freed while their glXContext is current. */
1289 glXMakeCurrent(MI_DISPLAY(mi), gp->window, *(gp->glx_context));
1291 if (glIsList(gp->listobjects))
1292 glDeleteLists(gp->listobjects, 3);
1294 for (i=0;i<gp->bman.num_balls;i++) {
1295 if (gp->bman.balls[i].bounced) freetris(&gp->tman[i]);
1297 free (gp->bman.balls);
1304 (void) free((void *) boxed);
1311 XSCREENSAVER_MODULE ("Boxed", boxed)
1313 /*********************************************************/