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: 15000 \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 "20"
59 # define DEF_BALLSIZE "3.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 80.0f
113 #define CAMDISTANCE_MIN 35.0
114 #define CAMDISTANCE_MAX 150.0
115 #define CAMDISTANCE_SPEED 1.5
116 #define LOOKAT_R 30.0
118 /* rendering the sphere */
120 #define SPHERE_VERTICES (2+MESH_SIZE*MESH_SIZE*2)
121 #define SPHERE_INDICES ((MESH_SIZE*4 + MESH_SIZE*4*(MESH_SIZE-1))*3)
124 **-----------------------------------------------------------------------------
126 **-----------------------------------------------------------------------------
186 float cam_x_speed, cam_z_speed, cam_y_speed;
190 vectorf spherev[SPHERE_VERTICES];
191 GLint spherei[SPHERE_INDICES];
194 GLXContext *glx_context;
203 #define GLL_PATTERN 0
208 **----------------------------------------------------------------------------
210 **----------------------------------------------------------------------------
213 static boxedstruct *boxed = NULL;
217 **----------------------------------------------------------------------------
219 **----------------------------------------------------------------------------
225 static inline void addvectors(vectorf *dest, vectorf *s1, vectorf *s2)
227 dest->x = s1->x + s2->x;
228 dest->y = s1->y + s2->y;
229 dest->z = s1->z + s2->z;
235 static inline void subvectors(vectorf *dest, vectorf* s1, vectorf *s2)
237 dest->x = s1->x - s2->x;
238 dest->y = s1->y - s2->y;
239 dest->z = s1->z - s2->z;
243 * Multiply vector with scalar (scale vector)
245 static inline void scalevector(vectorf *dest, vectorf *source, GLfloat sc)
247 dest->x = source->x * sc;
248 dest->y = source->y * sc;
249 dest->z = source->z * sc;
255 static inline void copyvector(vectorf *dest, vectorf* source)
263 static inline GLfloat
264 dotproduct(vectorf * v1, vectorf * v2)
266 return v1->x * v2->x + v1->y * v2->y + v1->z * v2->z;
269 static inline GLfloat
270 squaremagnitude(vectorf * v)
272 return v->x * v->x + v->y * v->y + v->z * v->z;
275 static inline GLfloat
276 squaremagnitudehorz(vectorf * v)
278 return v->x * v->x + v->z * v->z;
284 * Generate the Sphere data
289 static void generatesphere(boxedstruct *gp)
291 float dj = M_PI/(MESH_SIZE+1.0f);
292 float di = M_PI/MESH_SIZE;
293 int v; /* vertex offset */
294 int ind; /* indices offset */
296 GLfloat r_y_plane, h_y_plane;
301 * generate the sphere data
302 * vertices 0 and 1 are the north and south poles
305 spherei = gp->spherei;
306 spherev = gp->spherev;
308 spherev[0].x = 0.0f; spherev[0].y =1.0f; spherev[0].z = 0.0f;
309 spherev[1].x = 0.0f; spherev[1].y =-1.0f; spherev[1].z = 0.0f;
311 for (j=0; j<MESH_SIZE; j++) {
312 r_y_plane = (float)sin((j+1) * dj);
313 h_y_plane = (float)cos((j+1) * dj);
314 for (i=0; i<MESH_SIZE*2; i++) {
315 si = 2+i+j*MESH_SIZE*2;
316 spherev[si].y = h_y_plane;
317 spherev[si].x = (float) sin(i * di) * r_y_plane;
318 spherev[si].z = (float) cos(i * di) * r_y_plane;
322 /* generate indices */
323 for (i=0; i<MESH_SIZE*2; i++) {
325 spherei[3*i+1] = i+2;
326 spherei[3*i+2] = i+3;
327 if (i==MESH_SIZE*2-1)
331 /* the middle strips */
332 for (j=0; j<MESH_SIZE-1; j++) {
334 ind = 3*MESH_SIZE*2 + j*6*MESH_SIZE*2;
335 for (i=0; i<MESH_SIZE*2; i++) {
336 spherei[6*i+ind] = v+i;
337 spherei[6*i+2+ind] = v+i+1;
338 spherei[6*i+1+ind] = v+i+MESH_SIZE*2;
340 spherei[6*i+ind+3] = v+i+MESH_SIZE*2;
341 spherei[6*i+2+ind+3] = v+i+1;
342 spherei[6*i+1+ind+3] = v+i+MESH_SIZE*2+1;
343 if (i==MESH_SIZE*2-1) {
344 spherei[6*i+2+ind] = v+i+1-2*MESH_SIZE;
345 spherei[6*i+2+ind+3] = v+i+1-2*MESH_SIZE;
346 spherei[6*i+1+ind+3] = v+i+MESH_SIZE*2+1-2*MESH_SIZE;
351 v = SPHERE_VERTICES-MESH_SIZE*2;
352 ind = SPHERE_INDICES-3*MESH_SIZE*2;
353 for (i=0; i<MESH_SIZE*2; i++) {
354 spherei[3*i+ind] = 1;
355 spherei[3*i+1+ind] = v+i+1;
356 spherei[3*i+2+ind] = v+i;
357 if (i==MESH_SIZE*2-1)
358 spherei[3*i+1+ind] = v;
369 static void createball(ball *newball)
371 float r=0.0f,g=0.0f,b=0.0f;
372 newball->loc.x = 5-10*rnd();
373 newball->loc.y = 35+20*rnd();
374 newball->loc.z = 5-10*rnd();
375 newball->dir.x = (0.5f-rnd()) * speed;
376 newball->dir.y = 0.0;
377 newball->dir.z = (0.5-rnd()) * speed;
378 newball->offside = 0;
379 newball->bounced = FALSE;
380 newball->radius = cfg_ballsize;
381 while (r+g+b < 1.8f ) {
382 newball->color.x = r=rnd();
383 newball->color.y = g=rnd();
384 newball->color.z = b=rnd();
386 newball->justcreated = TRUE;
389 /* Update position of each ball */
391 static void updateballs(ballman *bman)
394 vectorf dvect,richting,relspeed,influence;
397 for (b=0;b<bman->num_balls;b++) {
399 GLfloat gravity = 0.30f * speed;
402 bman->balls[b].dir.y -= gravity;
404 addvectors(&bman->balls[b].loc,&bman->balls[b].loc,&bman->balls[b].dir);
406 if (bman->balls[b].loc.y < bman->balls[b].radius) { /* ball onder bodem? (bodem @ y=0) */
407 if ((bman->balls[b].loc.x < -95.0) ||
408 (bman->balls[b].loc.x > 95.0) ||
409 (bman->balls[b].loc.z < -95.0) ||
410 (bman->balls[b].loc.z > 95.0)) {
411 if (bman->balls[b].loc.y < -2000.0)
412 createball(&bman->balls[b]);
414 bman->balls[b].loc.y = bman->balls[b].radius + (bman->balls[b].radius - bman->balls[b].loc.y);
415 bman->balls[b].dir.y = -bman->balls[b].dir.y;
416 if (bman->balls[b].offside) {
417 bman->balls[b].bounced = TRUE; /* temporary disable painting ball */
418 scalevector(&bman->balls[b].dir,&bman->balls[b].dir,0.80f);
419 if (squaremagnitude(&bman->balls[b].dir) < 0.08f) {
420 createball(&bman->balls[b]);
422 if (squaremagnitudehorz(&bman->balls[b].dir) < 0.005f) {
423 createball(&bman->balls[b]);
429 if (!bman->balls[b].offside) {
430 if (bman->balls[b].loc.x - bman->balls[b].radius < -20.0f) { /* x ondergrens */
431 if (bman->balls[b].loc.y > 41+bman->balls[b].radius) bman->balls[b].offside=1;
433 bman->balls[b].dir.x = -bman->balls[b].dir.x;
434 bman->balls[b].loc.x = -20.0f + bman->balls[b].radius;
437 if (bman->balls[b].loc.x + bman->balls[b].radius > 20.0f) { /* x bovengrens */
438 if (bman->balls[b].loc.y > 41+bman->balls[b].radius) bman->balls[b].offside=1;
440 bman->balls[b].dir.x = -bman->balls[b].dir.x;
441 bman->balls[b].loc.x = 20.0f - bman->balls[b].radius;
444 if (bman->balls[b].loc.z - bman->balls[b].radius < -20.0f) { /* z ondergrens */
445 if (bman->balls[b].loc.y > 41+bman->balls[b].radius) bman->balls[b].offside=1;
447 bman->balls[b].dir.z = -bman->balls[b].dir.z;
448 bman->balls[b].loc.z = -20.0f + bman->balls[b].radius;
451 if (bman->balls[b].loc.z + bman->balls[b].radius > 20.0f) { /* z bovengrens */
452 if (bman->balls[b].loc.y > 41+bman->balls[b].radius) bman->balls[b].offside=1;
454 bman->balls[b].dir.z = -bman->balls[b].dir.z;
455 bman->balls[b].loc.z = 20.0f - bman->balls[b].radius;
458 } /* end if !offside */
460 /* check voor stuiteren */
461 for (j=b+1;j<bman->num_balls;j++) {
462 squaredist = (bman->balls[b].radius * bman->balls[b].radius) + (bman->balls[j].radius * bman->balls[j].radius);
463 subvectors(&dvect,&bman->balls[b].loc,&bman->balls[j].loc);
464 if ( squaremagnitude(&dvect) < squaredist ) { /* balls b and j touch */
465 subvectors(&richting,&bman->balls[j].loc,&bman->balls[b].loc);
466 subvectors(&relspeed,&bman->balls[b].dir,&bman->balls[j].dir);
467 /* calc mutual influence direction and magnitude */
468 scalevector(&influence,&richting,(dotproduct(&richting,&relspeed)/squaremagnitude(&richting)));
470 subvectors(&bman->balls[b].dir,&bman->balls[b].dir,&influence);
471 addvectors(&bman->balls[j].dir,&bman->balls[j].dir,&influence);
472 addvectors(&bman->balls[b].loc,&bman->balls[b].loc,&bman->balls[b].dir);
473 addvectors(&bman->balls[j].loc,&bman->balls[j].loc,&bman->balls[j].dir);
475 subvectors(&dvect,&bman->balls[b].loc,&bman->balls[j].loc);
476 while (squaremagnitude(&dvect) < squaredist) {
477 addvectors(&bman->balls[b].loc,&bman->balls[b].loc,&bman->balls[b].dir);
478 addvectors(&bman->balls[j].loc,&bman->balls[j].loc,&bman->balls[j].dir);
479 subvectors(&dvect,&bman->balls[b].loc,&bman->balls[j].loc);
488 * explode ball into triangles
491 static void createtrisfromball(triman* tman, vectorf *spherev, GLint *spherei, int ind_num, ball *b)
498 vectorf avgdir,dvect,mvect;
500 tman->scalefac = b->radius;
501 copyvector(&tman->color,&b->color);
502 explosion = 1.0f + tman->explosion * 2.0 * rnd();
503 momentum = tman->momentum;
505 tman->num_tri = ind_num/3;
507 /* reserveer geheugen voor de poly's in een bal */
509 tman->tris = (tri *)malloc(tman->num_tri * sizeof(tri));
510 tman->vertices = (vectorf *)malloc(ind_num * sizeof(vectorf));
511 tman->normals = (vectorf *)malloc(ind_num/3 * sizeof(vectorf));
513 for (i=0; i<(tman->num_tri); i++) {
514 tman->tris[i].far = FALSE;
515 tman->tris[i].gone = 0;
517 /* kopieer elke poly apart naar een tri structure */
518 copyvector(&tman->vertices[pos+0],&spherev[spherei[pos+0]]);
519 copyvector(&tman->vertices[pos+1],&spherev[spherei[pos+1]]);
520 copyvector(&tman->vertices[pos+2],&spherev[spherei[pos+2]]);
521 /* Calculate average direction of shrapnel */
522 addvectors(&avgdir,&tman->vertices[pos+0],&tman->vertices[pos+1]);
523 addvectors(&avgdir,&avgdir,&tman->vertices[pos+2]);
524 scalevector(&avgdir,&avgdir,0.33333);
526 /* should normalize first, NYI */
527 copyvector(&tman->normals[i],&avgdir);
529 /* copy de lokatie */
530 addvectors(&tman->tris[i].loc,&b->loc,&avgdir);
531 /* en translate alle triangles terug naar hun eigen oorsprong */
532 tman->vertices[pos+0].x -= avgdir.x;
533 tman->vertices[pos+0].y -= avgdir.y;
534 tman->vertices[pos+0].z -= avgdir.z;
535 tman->vertices[pos+1].x -= avgdir.x;
536 tman->vertices[pos+1].y -= avgdir.y;
537 tman->vertices[pos+1].z -= avgdir.z;
538 tman->vertices[pos+2].x -= avgdir.x;
539 tman->vertices[pos+2].y -= avgdir.y;
540 tman->vertices[pos+2].z -= avgdir.z;
541 /* alwaar opschaling plaatsvindt */
542 scale = b->radius * 2;
543 scalevector(&tman->vertices[pos+0],&tman->vertices[pos+0],scale);
544 scalevector(&tman->vertices[pos+1],&tman->vertices[pos+1],scale);
545 scalevector(&tman->vertices[pos+2],&tman->vertices[pos+2],scale);
547 tman->vertices[pos+0].x += avgdir.x;
548 tman->vertices[pos+0].y += avgdir.y;
549 tman->vertices[pos+0].z += avgdir.z;
550 tman->vertices[pos+1].x += avgdir.x;
551 tman->vertices[pos+1].y += avgdir.y;
552 tman->vertices[pos+1].z += avgdir.z;
553 tman->vertices[pos+2].x += avgdir.x;
554 tman->vertices[pos+2].y += avgdir.y;
555 tman->vertices[pos+2].z += avgdir.z;
557 /* bereken nieuwe richting */
558 scalevector(&tman->tris[i].dir,&avgdir,explosion);
559 dvect.x = (0.1f - 0.2f*rnd());
560 dvect.y = (0.15f - 0.3f*rnd());
561 dvect.z = (0.1f - 0.2f*rnd());
562 addvectors(&tman->tris[i].dir,&tman->tris[i].dir,&dvect);
564 /* add ball's momentum to each piece of the exploded ball */
565 mvect.x = b->dir.x * momentum;
567 mvect.z = b->dir.z * momentum;
568 addvectors(&tman->tris[i].dir,&tman->tris[i].dir,&mvect);
574 * update position of each tri
577 static void updatetris(triman *t)
582 for (b=0;b<t->num_tri;b++) {
583 /* the exploded triangles disappear over time */
584 if (rnd() < t->decay) {
585 if (t->tris[b].gone == 0)
589 t->tris[b].dir.y -= (0.1f * speed);
591 addvectors(&t->tris[b].loc,&t->tris[b].loc,&t->tris[b].dir);
593 if (t->tris[b].far) continue;
594 if (t->tris[b].loc.y < 0) { /* onder bodem ? */
595 if ((t->tris[b].loc.x > -95.0f) &
596 (t->tris[b].loc.x < 95.0f) &
597 (t->tris[b].loc.z > -95.0f) &
598 (t->tris[b].loc.z < 95.0f)) { /* in veld */
599 t->tris[b].dir.y = -(t->tris[b].dir.y);
600 t->tris[b].loc.y = -t->tris[b].loc.y;
601 scalevector(&t->tris[b].dir,&t->tris[b].dir,0.80f); /* dampening */
604 t->tris[b].far = TRUE;
609 if ((t->tris[b].loc.x > -21.0f) &
610 (t->tris[b].loc.x < 21.0f) &
611 (t->tris[b].loc.z > -21.0f) &
612 (t->tris[b].loc.z < 21.0f)) { /* in box? */
614 xd = zd = 999.0f; /* big */
615 if ((t->tris[b].loc.x > -21.0f) &
616 (t->tris[b].loc.x < 0)) {
617 xd = t->tris[b].loc.x + 21.0f;
619 if ((t->tris[b].loc.x < 21.0f) &
620 (t->tris[b].loc.x > 0)) {
621 xd = 21.0f - t->tris[b].loc.x;
623 if ((t->tris[b].loc.z > -21.0f) &
624 (t->tris[b].loc.z < 0)) {
625 zd = t->tris[b].loc.z + 21.0f;
627 if ((t->tris[b].loc.z < 21.0f) &
628 (t->tris[b].loc.z > 0)) {
629 zd = 21.0f - t->tris[b].loc.z;
633 if (t->tris[b].dir.x < 0)
634 t->tris[b].loc.x += (21.0f - t->tris[b].loc.x);
636 t->tris[b].loc.x += (-21.0f - t->tris[b].loc.x);
637 t->tris[b].dir.x = -t->tris[b].dir.x;
640 if (t->tris[b].dir.z < 0)
641 t->tris[b].loc.z += (21.0f - t->tris[b].loc.z);
643 t->tris[b].loc.z += (-21.0f - t->tris[b].loc.z);
644 t->tris[b].dir.z = -t->tris[b].dir.z;
653 * free memory allocated by a tri manager
655 static void freetris(triman *t)
658 if (t->tris) free(t->tris);
659 if (t->vertices) free(t->vertices);
660 if (t->normals) free(t->normals);
670 *load defaults in config structure
672 static void setdefaultconfig(boxed_config *config)
674 cfg_balls = MAX(3,MIN(40,cfg_balls));
675 cfg_ballsize = MAX(1.0f,MIN(5.0f,cfg_ballsize));
676 cfg_explosion = MAX(0.0f,MIN(50.0f,cfg_explosion));
677 cfg_decay = MAX(0.02f,MIN(0.90f,cfg_decay));
678 cfg_momentum = MAX(0.0f,MIN(1.0f,cfg_momentum));
680 config->numballs = cfg_balls;
681 config->textures = TRUE;
682 config->transparent = FALSE;
683 config->explosion = cfg_explosion;
684 config->decay = cfg_decay;
685 config->momentum = cfg_momentum;
686 config->ballsize = cfg_ballsize;
687 config->camspeed = 35.0f;
694 static int drawfilledbox(boxedstruct *boxed, int wire)
696 /* draws texture filled box,
697 top is drawn using the entire texture,
698 the sides are drawn using the edge of the texture
703 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
705 glVertex3f(-1.0,1.0,1.0);
707 glVertex3f(1.0,1.0,1.0);
709 glVertex3f(1.0,-1.0,1.0);
711 glVertex3f(-1.0,-1.0,1.0);
715 glVertex3f(1.0,1.0,-1.0);
717 glVertex3f(-1.0,1.0,-1.0);
719 glVertex3f(-1.0,-1.0,-1.0);
721 glVertex3f(1.0,-1.0,-1.0);
725 glVertex3f(-1.0,1.0,1.0);
727 glVertex3f(-1.0,-1.0,1.0);
729 glVertex3f(-1.0,-1.0,-1.0);
731 glVertex3f(-1.0,1.0,-1.0);
735 glVertex3f(1.0,1.0,1.0);
737 glVertex3f(1.0,1.0,-1.0);
739 glVertex3f(1.0,-1.0,-1.0);
741 glVertex3f(1.0,-1.0,1.0);
744 glTexCoord2f(0.0,0.0);
745 glVertex3f(-1.0,1.0,1.0);
746 glTexCoord2f(0.0,1.0);
747 glVertex3f(-1.0,1.0,-1.0);
748 glTexCoord2f(1.0,1.0);
749 glVertex3f(1.0,1.0,-1.0);
750 glTexCoord2f(1.0,0.0);
751 glVertex3f(1.0,1.0,1.0);
755 glVertex3f(-1.0,-1.0,1.0);
757 glVertex3f(-1.0,-1.0,-1.0);
759 glVertex3f(1.0,-1.0,-1.0);
761 glVertex3f(1.0,-1.0,1.0);
770 * Draw a box made of lines
772 static int drawbox(boxedstruct *boxed)
776 glBegin(GL_LINE_STRIP);
777 glVertex3f(-1.0,1.0,1.0);
778 glVertex3f(-1.0,1.0,-1.0); polys++;
779 glVertex3f(1.0,1.0,-1.0); polys++;
780 glVertex3f(1.0,1.0,1.0); polys++;
781 glVertex3f(-1.0,1.0,1.0); polys++;
784 glBegin(GL_LINE_STRIP);
785 glVertex3f(-1.0,-1.0,1.0);
786 glVertex3f(1.0,-1.0,1.0); polys++;
787 glVertex3f(1.0,-1.0,-1.0); polys++;
788 glVertex3f(-1.0,-1.0,-1.0); polys++;
789 glVertex3f(-1.0,-1.0,1.0); polys++;
791 /* connect top & bottom */
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++;
797 glVertex3f(1.0,1.0,-1.0);
798 glVertex3f(1.0,-1.0,-1.0); polys++;
799 glVertex3f(-1.0,1.0,-1.0);
800 glVertex3f(-1.0,-1.0,-1.0); polys++;
810 static int drawball(boxedstruct *gp, ball *b, int wire)
814 GLint *spherei = gp->spherei;
815 vectorf *spherev = gp->spherev;
820 glTranslatef(b->loc.x,b->loc.y,b->loc.z);
821 glScalef(b->radius,b->radius,b->radius);
822 glColor3f(b->color.x,b->color.y,b->color.z);
826 glMaterialfv(GL_FRONT, GL_DIFFUSE, col);
830 glMaterialfv(GL_FRONT, GL_EMISSION,col);
832 if (!gp->gllists[GLL_BALL]) {
833 glNewList(gp->listobjects + GLL_BALL,GL_COMPILE);
834 glBegin(wire ? GL_LINES : GL_TRIANGLES);
835 cnt = SPHERE_INDICES/3;
836 for (i=0; i<cnt; i++) {
838 glNormal3f(spherev[spherei[pos+0]].x,spherev[spherei[pos+0]].y,spherev[spherei[pos+0]].z);
839 glVertex3f(spherev[spherei[pos+0]].x,spherev[spherei[pos+0]].y,spherev[spherei[pos+0]].z);
840 glNormal3f(spherev[spherei[pos+1]].x,spherev[spherei[pos+1]].y,spherev[spherei[pos+1]].z);
841 gp->list_polys[GLL_BALL]++;
842 glVertex3f(spherev[spherei[pos+1]].x,spherev[spherei[pos+1]].y,spherev[spherei[pos+1]].z);
844 glVertex3f(spherev[spherei[pos+1]].x,spherev[spherei[pos+1]].y,spherev[spherei[pos+1]].z);
845 glNormal3f(spherev[spherei[pos+2]].x,spherev[spherei[pos+2]].y,spherev[spherei[pos+2]].z);
846 glVertex3f(spherev[spherei[pos+2]].x,spherev[spherei[pos+2]].y,spherev[spherei[pos+2]].z);
847 gp->list_polys[GLL_BALL]++;
851 gp->gllists[GLL_BALL] = 1;
853 glCallList(gp->listobjects + GLL_BALL);
854 polys += gp->list_polys[GLL_BALL];
863 * Draw a single triangle
865 static void drawtri(triman *t, int wire, int i)
867 const vectorf *spherev = t->vertices + i*3;
868 const vectorf *loc = &t->tris[i].loc;
870 glNormal3f(t->normals[i].x,t->normals[i].y,t->normals[i].z);
871 glVertex3f(spherev[0].x+loc->x,spherev[0].y+loc->y,spherev[0].z+loc->z);
872 glVertex3f(spherev[1].x+loc->x,spherev[1].y+loc->y,spherev[1].z+loc->z);
874 glVertex3f(spherev[1].x+loc->x,spherev[1].y+loc->y,spherev[1].z+loc->z);
875 glVertex3f(spherev[2].x+loc->x,spherev[2].y+loc->y,spherev[2].z+loc->z);
877 glVertex3f(spherev[2].x+loc->x,spherev[2].y+loc->y,spherev[2].z+loc->z);
878 glVertex3f(spherev[0].x+loc->x,spherev[0].y+loc->y,spherev[0].z+loc->z);
884 * Draw all triangles in triman
886 static int drawtriman(triman *t, int wire)
893 glColor3f(t->color.x,t->color.y,t->color.z);
897 glMaterialfv(GL_FRONT, GL_DIFFUSE, col);
901 glMaterialfv(GL_FRONT, GL_EMISSION,col);
902 glBegin(wire ? GL_LINES : GL_TRIANGLES);
904 for (i=0; i<t->num_tri; i++) {
905 if (t->tris[i].gone > 3) { continue; }
906 if (t->tris[i].gone > 0) {
907 glColor3f(t->color.x,t->color.y,t->color.z);
911 glMaterialfv(GL_FRONT, GL_DIFFUSE, col);
915 glMaterialfv(GL_FRONT, GL_EMISSION,col);
920 glColor3f(t->color.x,t->color.y,t->color.z);
924 glMaterialfv(GL_FRONT, GL_DIFFUSE, col);
928 glMaterialfv(GL_FRONT, GL_EMISSION,col);
945 static int drawpattern(boxedstruct *gp)
948 if (!gp->gllists[GLL_PATTERN]) {
949 glNewList(gp->listobjects + GLL_PATTERN, GL_COMPILE);
951 glBegin(GL_LINE_STRIP);
952 glVertex3f(-25.0f, 0.0f, 35.0f);
953 glVertex3f(-15.0f, 0.0f, 35.0f); gp->list_polys[GLL_PATTERN]++;
954 glVertex3f(-5.0f, 0.0f, 25.0f); gp->list_polys[GLL_PATTERN]++;
955 glVertex3f(5.0f, 0.0f, 25.0f); gp->list_polys[GLL_PATTERN]++;
956 glVertex3f(15.0f, 0.0f, 35.0f); gp->list_polys[GLL_PATTERN]++;
957 glVertex3f(25.0f, 0.0f, 35.0f); gp->list_polys[GLL_PATTERN]++;
958 glVertex3f(35.0f, 0.0f, 25.0f); gp->list_polys[GLL_PATTERN]++;
959 glVertex3f(35.0f, 0.0f, 15.0f); gp->list_polys[GLL_PATTERN]++;
960 glVertex3f(25.0f, 0.0f, 5.0f); gp->list_polys[GLL_PATTERN]++;
961 glVertex3f(25.0f, 0.0f, -5.0f); gp->list_polys[GLL_PATTERN]++;
962 glVertex3f(35.0f, 0.0f, -15.0f); gp->list_polys[GLL_PATTERN]++;
963 glVertex3f(35.0f, 0.0f, -25.0f); gp->list_polys[GLL_PATTERN]++;
964 glVertex3f(25.0f, 0.0f, -35.0f); gp->list_polys[GLL_PATTERN]++;
965 glVertex3f(15.0f, 0.0f,-35.0f); gp->list_polys[GLL_PATTERN]++;
966 glVertex3f(5.0f, 0.0f, -25.0f); gp->list_polys[GLL_PATTERN]++;
967 glVertex3f(-5.0f, 0.0f, -25.0f); gp->list_polys[GLL_PATTERN]++;
968 glVertex3f(-15.0f, 0.0f,-35.0f); gp->list_polys[GLL_PATTERN]++;
969 glVertex3f(-25.0f, 0.0f,-35.0f); gp->list_polys[GLL_PATTERN]++;
970 glVertex3f(-35.0f, 0.0f, -25.0f); gp->list_polys[GLL_PATTERN]++;
971 glVertex3f(-35.0f, 0.0f, -15.0f); gp->list_polys[GLL_PATTERN]++;
972 glVertex3f(-25.0f, 0.0f, -5.0f); gp->list_polys[GLL_PATTERN]++;
973 glVertex3f(-25.0f, 0.0f, 5.0f); gp->list_polys[GLL_PATTERN]++;
974 glVertex3f(-35.0f, 0.0f, 15.0f); gp->list_polys[GLL_PATTERN]++;
975 glVertex3f(-35.0f, 0.0f, 25.0f); gp->list_polys[GLL_PATTERN]++;
976 glVertex3f(-25.0f, 0.0f, 35.0f); gp->list_polys[GLL_PATTERN]++;
979 glBegin(GL_LINE_STRIP);
980 glVertex3f(-5.0f, 0.0f, 15.0f);
981 glVertex3f(5.0f, 0.0f, 15.0f); gp->list_polys[GLL_PATTERN]++;
982 glVertex3f(15.0f, 0.0f, 5.0f); gp->list_polys[GLL_PATTERN]++;
983 glVertex3f(15.0f, 0.0f, -5.0f); gp->list_polys[GLL_PATTERN]++;
984 glVertex3f(5.0f, 0.0f, -15.0f); gp->list_polys[GLL_PATTERN]++;
985 glVertex3f(-5.0f, 0.0f, -15.0f); gp->list_polys[GLL_PATTERN]++;
986 glVertex3f(-15.0f, 0.0f, -5.0f); gp->list_polys[GLL_PATTERN]++;
987 glVertex3f(-15.0f, 0.0f, 5.0f); gp->list_polys[GLL_PATTERN]++;
988 glVertex3f(-5.0f, 0.0f, 15.0f); gp->list_polys[GLL_PATTERN]++;
992 gp->gllists[GLL_PATTERN] = 1;
994 glCallList(gp->listobjects + GLL_PATTERN);
995 polys += gp->list_polys[GLL_PATTERN];
1003 * main rendering loop
1005 static void draw(ModeInfo * mi)
1007 boxedstruct *gp = &boxed[MI_SCREEN(mi)];
1008 int wire = MI_IS_WIREFRAME (mi);
1014 GLfloat dgray[4] = {0.3f, 0.3f, 0.3f, 1.0f};
1015 GLfloat black[4] = {0.0f, 0.0f, 0.0f, 1.0f};
1016 GLfloat lblue[4] = {0.4f,0.6f,1.0f };
1018 GLfloat l0_ambient[] = {0.0, 0.0, 0.0, 1.0};
1019 GLfloat l0_specular[] = {1.0, 1.0, 1.0, 1.0};
1020 GLfloat l0_diffuse[] = {1.0, 1.0, 1.0, 1.0};
1021 GLfloat l0_position[] = {0.0, 0.0, 0.0, 1.0}; /* w != 0 -> positional light */
1022 GLfloat l1_ambient[] = {0.0, 0.0, 0.0, 1.0};
1023 GLfloat l1_specular[] = {1.0, 1.0, 1.0, 1.0};
1024 GLfloat l1_diffuse[] = {0.5, 0.5, 0.5, 1.0};
1025 GLfloat l1_position[] = {0.0, 1.0, 0.0, 0.0}; /* w = 0 -> directional light */
1027 mi->polygon_count = 0;
1029 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1032 glRotatef(current_device_rotation(), 0, 0, 1);
1035 gp->camtic += 0.01f + 0.01f * sin(gp->tic * speed);
1037 /* rotate camera around (0,0,0), looking at (0,0,0), up is (0,1,0) */
1038 r = CAMDISTANCE_MIN + (CAMDISTANCE_MAX - CAMDISTANCE_MIN) + (CAMDISTANCE_MAX - CAMDISTANCE_MIN)*cos((gp->camtic/CAMDISTANCE_SPEED) * speed);
1039 v1.x = r * sin((gp->camtic/gp->cam_x_speed) * speed);
1040 v1.z = r * cos((gp->camtic/gp->cam_x_speed) * speed);
1041 v1.y = CAM_HEIGHT * sin((gp->camtic/gp->cam_y_speed) * speed) + 1.02 * CAM_HEIGHT;
1043 v2.x = LOOKAT_R * sin((gp->camtic/(gp->cam_x_speed * 5.0f)) * speed);
1044 v2.z = LOOKAT_R * cos((gp->camtic/(gp->cam_x_speed * 5.0f)) * speed);
1045 v2.y = (CAM_HEIGHT * sin((gp->camtic/gp->cam_y_speed) * speed) + 1.02 * CAM_HEIGHT)/10.0;
1047 gluLookAt(v1.x,v1.y,v1.z,v2.x,v2.y,v2.x,0.0,1.0,0.0);
1050 glLightfv(GL_LIGHT0, GL_AMBIENT, l0_ambient);
1051 glLightfv(GL_LIGHT0, GL_DIFFUSE, l0_diffuse);
1052 glLightfv(GL_LIGHT0, GL_SPECULAR, l0_specular);
1053 glLightfv(GL_LIGHT0, GL_POSITION, l0_position);
1054 glLightfv(GL_LIGHT1, GL_AMBIENT, l1_ambient);
1055 glLightfv(GL_LIGHT1, GL_DIFFUSE, l1_diffuse);
1056 glLightfv(GL_LIGHT1, GL_SPECULAR, l1_specular);
1057 glLightfv(GL_LIGHT1, GL_POSITION, l1_position);
1058 glEnable(GL_LIGHT0);
1059 glEnable(GL_LIGHT1);
1063 glMaterialfv(GL_FRONT, GL_SPECULAR, black);
1064 glMaterialfv(GL_FRONT, GL_EMISSION, lblue);
1065 glMaterialfv(GL_FRONT,GL_AMBIENT,black);
1066 glMaterialf(GL_FRONT, GL_SHININESS, 5.0);
1070 /* draw ground grid */
1071 /* glDisable(GL_DEPTH_TEST); */
1072 glDisable(GL_LIGHTING);
1074 glColor3f(0.1,0.1,0.6);
1075 for (dx= -2; dx<3; dx++) {
1076 for (dz= -2; dz<3; dz++) {
1078 glTranslatef(dx*30.0f, 0.0f, dz*30.0f);
1084 /* Set drawing mode for the boxes */
1085 glEnable(GL_DEPTH_TEST);
1086 if (!wire) glEnable(GL_TEXTURE_2D);
1088 glColor3f(1.0,1.0,1.0);
1089 glScalef(20.0,0.25,20.0);
1090 glTranslatef(0.0,2.0,0.0);
1091 mi->polygon_count += drawfilledbox(gp, wire);
1093 glDisable(GL_TEXTURE_2D);
1096 glColor3f(0.2,0.5,0.2);
1097 glScalef(20.0,20.0,0.25);
1098 glTranslatef(0.0,1.0,81.0);
1099 mi->polygon_count += drawbox(gp);
1103 glColor3f(0.2,0.5,0.2);
1104 glScalef(20.0,20.0,0.25);
1105 glTranslatef(0.0,1.0,-81.0);
1106 mi->polygon_count += drawbox(gp);
1110 glColor3f(0.2,0.5,0.2);
1111 glScalef(.25,20.0,20.0);
1112 glTranslatef(-81.0,1.0,0.0);
1113 mi->polygon_count += drawbox(gp);
1117 glColor3f(0.2,0.5,0.2);
1118 glScalef(.25,20.0,20.0);
1119 glTranslatef(81.0,1.0,0.0);
1120 mi->polygon_count += drawbox(gp);
1124 glEnable(GL_LIGHTING);
1126 glMaterialfv(GL_FRONT, GL_DIFFUSE, dgray);
1127 glMaterialfv(GL_FRONT, GL_EMISSION, black); /* turn it off before painting the balls */
1130 /* move the balls and shrapnel */
1131 updateballs(&gp->bman);
1133 glFrontFace(GL_CCW);
1134 for (i=0;i<gp->bman.num_balls;i++) {
1135 if (gp->bman.balls[i].justcreated) {
1136 gp->bman.balls[i].justcreated = FALSE;
1137 freetris(&gp->tman[i]);
1139 if (gp->bman.balls[i].bounced) {
1140 if (gp->tman[i].vertices == NULL) {
1141 createtrisfromball(&gp->tman[i],gp->spherev,gp->spherei,SPHERE_INDICES,&gp->bman.balls[i]);
1143 updatetris(&gp->tman[i]);
1145 glDisable(GL_CULL_FACE);
1146 mi->polygon_count += drawtriman(&gp->tman[i], wire);
1147 if (!wire) glEnable(GL_CULL_FACE);
1149 mi->polygon_count += drawball(gp, &gp->bman.balls[i], wire);
1159 * new window size or exposure
1161 ENTRYPOINT void reshape_boxed(ModeInfo *mi, int width, int height)
1163 GLfloat h = (GLfloat) height / (GLfloat) width;
1165 glViewport(0, 0, (GLint) width, (GLint) height);
1166 glMatrixMode(GL_PROJECTION);
1168 gluPerspective(50.0,1/h,2.0,1000.0);
1169 glMatrixMode (GL_MODELVIEW);
1177 pinit(ModeInfo * mi)
1179 boxedstruct *gp = &boxed[MI_SCREEN(mi)];
1180 int wire = MI_IS_WIREFRAME (mi);
1184 char *texpixeltarget;
1186 glShadeModel(GL_SMOOTH);
1188 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1190 /* Load configuration */
1191 setdefaultconfig(&gp->config);
1193 /* give the decay parameter a better curve */
1194 if (gp->config.decay <= 0.8182) { gp->config.decay = gp->config.decay / 3; }
1195 else { gp->config.decay = (gp->config.decay - 0.75) * 4; }
1199 bman->balls = (ball *)malloc(gp->config.numballs * sizeof(ball));
1200 bman->num_balls = gp->config.numballs;
1201 bman->ballsize = gp->config.ballsize;
1202 bman->explosion = gp->config.explosion;
1204 gp->tman = (triman *)malloc(bman->num_balls * sizeof(triman));
1205 memset(gp->tman,0,bman->num_balls * sizeof(triman));
1207 for(i=0;i<bman->num_balls;i++) {
1208 gp->tman[i].explosion = (float) (((int)gp->config.explosion) / 15.0f );
1209 gp->tman[i].decay = gp->config.decay;
1210 gp->tman[i].momentum = gp->config.momentum;
1211 gp->tman[i].vertices = NULL;
1212 gp->tman[i].normals = NULL;
1213 gp->tman[i].tris = NULL;
1214 createball(&bman->balls[i]);
1215 bman->balls[i].loc.y *= rnd();
1221 glEnable(GL_CULL_FACE);
1222 glEnable(GL_LIGHTING);
1225 /* define cam path */
1226 gp->cam_x_speed = 1.0f/((float)gp->config.camspeed/50.0 + rnd()*((float)gp->config.camspeed/50.0));
1227 gp->cam_z_speed = 1.0f/((float)gp->config.camspeed/50.0 + rnd()*((float)gp->config.camspeed/50.0));
1228 gp->cam_y_speed = 1.0f/((float)gp->config.camspeed/250.0 + rnd()*((float)gp->config.camspeed/250.0));
1229 if (rnd() < 0.5f) gp->cam_x_speed = -gp->cam_x_speed;
1230 if (rnd() < 0.5f) gp->cam_z_speed = -gp->cam_z_speed;
1232 /* define initial cam position */
1233 gp->tic = gp->camtic = rnd() * 100.0f;
1235 /* define tex1 (bottom plate) */
1236 gp->tex1 = (char *)malloc(3*width*height*sizeof(GLuint));
1237 texpixels = 256*256; /*width*height;*/
1238 texpixeldata = header_data;
1239 texpixeltarget = gp->tex1;
1240 for (i=0; i < texpixels; i++) {
1241 HEADER_PIXEL(texpixeldata,texpixeltarget);
1242 texpixeltarget += 3;
1246 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
1250 i = gluBuild2DMipmaps(GL_TEXTURE_2D, 3, 256, 256,
1251 GL_RGB, GL_UNSIGNED_BYTE, gp->tex1);
1254 const char *s = (char *) gluErrorString (i);
1255 fprintf (stderr, "%s: error mipmapping texture: %s\n",
1256 progname, (s ? s : "(unknown)"));
1259 check_gl_error("mipmapping");
1261 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, 256, 256, 0,
1262 GL_RGB, GL_UNSIGNED_BYTE,
1264 check_gl_error("texture");
1267 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1268 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
1269 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
1270 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1271 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1278 init_boxed(ModeInfo * mi)
1280 int screen = MI_SCREEN(mi);
1282 /* Colormap cmap; */
1283 /* Boolean rgba, doublebuffer, cmap_installed; */
1286 if (boxed == NULL) {
1287 if ((boxed = (boxedstruct *) calloc(MI_NUM_SCREENS(mi),sizeof (boxedstruct))) == NULL) return;
1289 gp = &boxed[screen];
1290 gp->window = MI_WINDOW(mi);
1292 if ((gp->glx_context = init_GL(mi)) != NULL) {
1293 reshape_boxed(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
1294 glDrawBuffer(GL_BACK);
1295 if (!glIsList(gp->listobjects)) {
1296 gp->listobjects = glGenLists(3);
1309 draw_boxed(ModeInfo * mi)
1311 boxedstruct *gp = &boxed[MI_SCREEN(mi)];
1312 Display *display = MI_DISPLAY(mi);
1313 Window window = MI_WINDOW(mi);
1315 if (!gp->glx_context)
1318 glDrawBuffer(GL_BACK);
1320 glXMakeCurrent(display, window, *(gp->glx_context));
1323 if (mi->fps_p) do_fps (mi);
1325 glXSwapBuffers(display, window);
1329 release_boxed(ModeInfo * mi)
1333 if (boxed != NULL) {
1336 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
1337 boxedstruct *gp = &boxed[screen];
1339 if (gp->glx_context) {
1340 /* Display lists MUST be freed while their glXContext is current. */
1341 glXMakeCurrent(MI_DISPLAY(mi), gp->window, *(gp->glx_context));
1343 if (glIsList(gp->listobjects))
1344 glDeleteLists(gp->listobjects, 3);
1346 for (i=0;i<gp->bman.num_balls;i++) {
1347 if (gp->bman.balls[i].bounced) freetris(&gp->tman[i]);
1349 free (gp->bman.balls);
1356 (void) free((void *) boxed);
1363 XSCREENSAVER_MODULE ("Boxed", boxed)
1365 /*********************************************************/