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 cnt = SPHERE_INDICES/3;
835 for (i=0; i<cnt; i++) {
837 glBegin(wire ? GL_LINE_LOOP : GL_TRIANGLES);
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);
843 glNormal3f(spherev[spherei[pos+2]].x,spherev[spherei[pos+2]].y,spherev[spherei[pos+2]].z);
844 glVertex3f(spherev[spherei[pos+2]].x,spherev[spherei[pos+2]].y,spherev[spherei[pos+2]].z);
845 gp->list_polys[GLL_BALL]++;
849 gp->gllists[GLL_BALL] = 1;
851 glCallList(gp->listobjects + GLL_BALL);
852 polys += gp->list_polys[GLL_BALL];
861 * Draw all triangles in triman
863 static int drawtriman(triman *t, int wire)
867 vectorf *spherev = t->vertices;
871 glColor3f(t->color.x,t->color.y,t->color.z);
875 glMaterialfv(GL_FRONT, GL_DIFFUSE, col);
879 glMaterialfv(GL_FRONT, GL_EMISSION,col);
881 for (i=0; i<t->num_tri; i++) {
882 if (t->tris[i].gone > 3) { continue; }
883 if (t->tris[i].gone > 0) {
884 glColor3f(t->color.x,t->color.y,t->color.z);
888 glMaterialfv(GL_FRONT, GL_DIFFUSE, col);
892 glMaterialfv(GL_FRONT, GL_EMISSION,col);
896 glTranslatef(t->tris[i].loc.x,t->tris[i].loc.y,t->tris[i].loc.z);
897 glBegin(wire ? GL_LINE_LOOP : GL_TRIANGLES);
898 glNormal3f(t->normals[i].x,t->normals[i].y,t->normals[i].z);
899 glVertex3f(spherev[pos+0].x,spherev[pos+0].y,spherev[pos+0].z);
900 glVertex3f(spherev[pos+1].x,spherev[pos+1].y,spherev[pos+1].z);
901 glVertex3f(spherev[pos+2].x,spherev[pos+2].y,spherev[pos+2].z);
906 glColor3f(t->color.x,t->color.y,t->color.z);
910 glMaterialfv(GL_FRONT, GL_DIFFUSE, col);
914 glMaterialfv(GL_FRONT, GL_EMISSION,col);
922 glTranslatef(t->tris[i].loc.x,t->tris[i].loc.y,t->tris[i].loc.z);
923 glBegin(wire ? GL_LINE_LOOP : GL_TRIANGLES);
924 glNormal3f(t->normals[i].x,t->normals[i].y,t->normals[i].z);
925 glVertex3f(spherev[pos+0].x,spherev[pos+0].y,spherev[pos+0].z);
926 glVertex3f(spherev[pos+1].x,spherev[pos+1].y,spherev[pos+1].z);
927 glVertex3f(spherev[pos+2].x,spherev[pos+2].y,spherev[pos+2].z);
939 static int drawpattern(boxedstruct *gp)
942 if (!gp->gllists[GLL_PATTERN]) {
943 glNewList(gp->listobjects + GLL_PATTERN, GL_COMPILE);
945 glBegin(GL_LINE_STRIP);
946 glVertex3f(-25.0f, 0.0f, 35.0f);
947 glVertex3f(-15.0f, 0.0f, 35.0f); gp->list_polys[GLL_PATTERN]++;
948 glVertex3f(-5.0f, 0.0f, 25.0f); gp->list_polys[GLL_PATTERN]++;
949 glVertex3f(5.0f, 0.0f, 25.0f); gp->list_polys[GLL_PATTERN]++;
950 glVertex3f(15.0f, 0.0f, 35.0f); gp->list_polys[GLL_PATTERN]++;
951 glVertex3f(25.0f, 0.0f, 35.0f); gp->list_polys[GLL_PATTERN]++;
952 glVertex3f(35.0f, 0.0f, 25.0f); gp->list_polys[GLL_PATTERN]++;
953 glVertex3f(35.0f, 0.0f, 15.0f); gp->list_polys[GLL_PATTERN]++;
954 glVertex3f(25.0f, 0.0f, 5.0f); gp->list_polys[GLL_PATTERN]++;
955 glVertex3f(25.0f, 0.0f, -5.0f); gp->list_polys[GLL_PATTERN]++;
956 glVertex3f(35.0f, 0.0f, -15.0f); gp->list_polys[GLL_PATTERN]++;
957 glVertex3f(35.0f, 0.0f, -25.0f); gp->list_polys[GLL_PATTERN]++;
958 glVertex3f(25.0f, 0.0f, -35.0f); gp->list_polys[GLL_PATTERN]++;
959 glVertex3f(15.0f, 0.0f,-35.0f); gp->list_polys[GLL_PATTERN]++;
960 glVertex3f(5.0f, 0.0f, -25.0f); gp->list_polys[GLL_PATTERN]++;
961 glVertex3f(-5.0f, 0.0f, -25.0f); gp->list_polys[GLL_PATTERN]++;
962 glVertex3f(-15.0f, 0.0f,-35.0f); gp->list_polys[GLL_PATTERN]++;
963 glVertex3f(-25.0f, 0.0f,-35.0f); gp->list_polys[GLL_PATTERN]++;
964 glVertex3f(-35.0f, 0.0f, -25.0f); gp->list_polys[GLL_PATTERN]++;
965 glVertex3f(-35.0f, 0.0f, -15.0f); gp->list_polys[GLL_PATTERN]++;
966 glVertex3f(-25.0f, 0.0f, -5.0f); gp->list_polys[GLL_PATTERN]++;
967 glVertex3f(-25.0f, 0.0f, 5.0f); gp->list_polys[GLL_PATTERN]++;
968 glVertex3f(-35.0f, 0.0f, 15.0f); gp->list_polys[GLL_PATTERN]++;
969 glVertex3f(-35.0f, 0.0f, 25.0f); gp->list_polys[GLL_PATTERN]++;
970 glVertex3f(-25.0f, 0.0f, 35.0f); gp->list_polys[GLL_PATTERN]++;
973 glBegin(GL_LINE_STRIP);
974 glVertex3f(-5.0f, 0.0f, 15.0f);
975 glVertex3f(5.0f, 0.0f, 15.0f); gp->list_polys[GLL_PATTERN]++;
976 glVertex3f(15.0f, 0.0f, 5.0f); gp->list_polys[GLL_PATTERN]++;
977 glVertex3f(15.0f, 0.0f, -5.0f); gp->list_polys[GLL_PATTERN]++;
978 glVertex3f(5.0f, 0.0f, -15.0f); gp->list_polys[GLL_PATTERN]++;
979 glVertex3f(-5.0f, 0.0f, -15.0f); gp->list_polys[GLL_PATTERN]++;
980 glVertex3f(-15.0f, 0.0f, -5.0f); gp->list_polys[GLL_PATTERN]++;
981 glVertex3f(-15.0f, 0.0f, 5.0f); gp->list_polys[GLL_PATTERN]++;
982 glVertex3f(-5.0f, 0.0f, 15.0f); gp->list_polys[GLL_PATTERN]++;
986 gp->gllists[GLL_PATTERN] = 1;
988 glCallList(gp->listobjects + GLL_PATTERN);
989 polys += gp->list_polys[GLL_PATTERN];
997 * main rendering loop
999 static void draw(ModeInfo * mi)
1001 boxedstruct *gp = &boxed[MI_SCREEN(mi)];
1002 int wire = MI_IS_WIREFRAME (mi);
1008 GLfloat dgray[4] = {0.3f, 0.3f, 0.3f, 1.0f};
1009 GLfloat black[4] = {0.0f, 0.0f, 0.0f, 1.0f};
1010 GLfloat lblue[4] = {0.4f,0.6f,1.0f };
1012 GLfloat l0_ambient[] = {0.0, 0.0, 0.0, 1.0};
1013 GLfloat l0_specular[] = {1.0, 1.0, 1.0, 1.0};
1014 GLfloat l0_diffuse[] = {1.0, 1.0, 1.0, 1.0};
1015 GLfloat l0_position[] = {0.0, 0.0, 0.0, 1.0}; /* w != 0 -> positional light */
1016 GLfloat l1_ambient[] = {0.0, 0.0, 0.0, 1.0};
1017 GLfloat l1_specular[] = {1.0, 1.0, 1.0, 1.0};
1018 GLfloat l1_diffuse[] = {0.5, 0.5, 0.5, 1.0};
1019 GLfloat l1_position[] = {0.0, 1.0, 0.0, 0.0}; /* w = 0 -> directional light */
1021 mi->polygon_count = 0;
1023 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1026 glRotatef(current_device_rotation(), 0, 0, 1);
1029 gp->camtic += 0.01f + 0.01f * sin(gp->tic * speed);
1031 /* rotate camera around (0,0,0), looking at (0,0,0), up is (0,1,0) */
1032 r = CAMDISTANCE_MIN + (CAMDISTANCE_MAX - CAMDISTANCE_MIN) + (CAMDISTANCE_MAX - CAMDISTANCE_MIN)*cos((gp->camtic/CAMDISTANCE_SPEED) * speed);
1033 v1.x = r * sin((gp->camtic/gp->cam_x_speed) * speed);
1034 v1.z = r * cos((gp->camtic/gp->cam_x_speed) * speed);
1035 v1.y = CAM_HEIGHT * sin((gp->camtic/gp->cam_y_speed) * speed) + 1.02 * CAM_HEIGHT;
1037 v2.x = LOOKAT_R * sin((gp->camtic/(gp->cam_x_speed * 5.0f)) * speed);
1038 v2.z = LOOKAT_R * cos((gp->camtic/(gp->cam_x_speed * 5.0f)) * speed);
1039 v2.y = (CAM_HEIGHT * sin((gp->camtic/gp->cam_y_speed) * speed) + 1.02 * CAM_HEIGHT)/10.0;
1041 gluLookAt(v1.x,v1.y,v1.z,v2.x,v2.y,v2.x,0.0,1.0,0.0);
1044 glLightfv(GL_LIGHT0, GL_AMBIENT, l0_ambient);
1045 glLightfv(GL_LIGHT0, GL_DIFFUSE, l0_diffuse);
1046 glLightfv(GL_LIGHT0, GL_SPECULAR, l0_specular);
1047 glLightfv(GL_LIGHT0, GL_POSITION, l0_position);
1048 glLightfv(GL_LIGHT1, GL_AMBIENT, l1_ambient);
1049 glLightfv(GL_LIGHT1, GL_DIFFUSE, l1_diffuse);
1050 glLightfv(GL_LIGHT1, GL_SPECULAR, l1_specular);
1051 glLightfv(GL_LIGHT1, GL_POSITION, l1_position);
1052 glEnable(GL_LIGHT0);
1053 glEnable(GL_LIGHT1);
1057 glMaterialfv(GL_FRONT, GL_SPECULAR, black);
1058 glMaterialfv(GL_FRONT, GL_EMISSION, lblue);
1059 glMaterialfv(GL_FRONT,GL_AMBIENT,black);
1060 glMaterialf(GL_FRONT, GL_SHININESS, 5.0);
1064 /* draw ground grid */
1065 /* glDisable(GL_DEPTH_TEST); */
1066 glDisable(GL_LIGHTING);
1068 glColor3f(0.1,0.1,0.6);
1069 for (dx= -2; dx<3; dx++) {
1070 for (dz= -2; dz<3; dz++) {
1072 glTranslatef(dx*30.0f, 0.0f, dz*30.0f);
1078 /* Set drawing mode for the boxes */
1079 glEnable(GL_DEPTH_TEST);
1080 if (!wire) glEnable(GL_TEXTURE_2D);
1082 glColor3f(1.0,1.0,1.0);
1083 glScalef(20.0,0.25,20.0);
1084 glTranslatef(0.0,2.0,0.0);
1085 mi->polygon_count += drawfilledbox(gp, wire);
1087 glDisable(GL_TEXTURE_2D);
1090 glColor3f(0.2,0.5,0.2);
1091 glScalef(20.0,20.0,0.25);
1092 glTranslatef(0.0,1.0,81.0);
1093 mi->polygon_count += drawbox(gp);
1097 glColor3f(0.2,0.5,0.2);
1098 glScalef(20.0,20.0,0.25);
1099 glTranslatef(0.0,1.0,-81.0);
1100 mi->polygon_count += drawbox(gp);
1104 glColor3f(0.2,0.5,0.2);
1105 glScalef(.25,20.0,20.0);
1106 glTranslatef(-81.0,1.0,0.0);
1107 mi->polygon_count += drawbox(gp);
1111 glColor3f(0.2,0.5,0.2);
1112 glScalef(.25,20.0,20.0);
1113 glTranslatef(81.0,1.0,0.0);
1114 mi->polygon_count += drawbox(gp);
1118 glEnable(GL_LIGHTING);
1120 glMaterialfv(GL_FRONT, GL_DIFFUSE, dgray);
1121 glMaterialfv(GL_FRONT, GL_EMISSION, black); /* turn it off before painting the balls */
1124 /* move the balls and shrapnel */
1125 updateballs(&gp->bman);
1127 glFrontFace(GL_CCW);
1128 for (i=0;i<gp->bman.num_balls;i++) {
1129 if (gp->bman.balls[i].justcreated) {
1130 gp->bman.balls[i].justcreated = FALSE;
1131 freetris(&gp->tman[i]);
1133 if (gp->bman.balls[i].bounced) {
1134 if (gp->tman[i].vertices == NULL) {
1135 createtrisfromball(&gp->tman[i],gp->spherev,gp->spherei,SPHERE_INDICES,&gp->bman.balls[i]);
1137 updatetris(&gp->tman[i]);
1139 glDisable(GL_CULL_FACE);
1140 mi->polygon_count += drawtriman(&gp->tman[i], wire);
1141 if (!wire) glEnable(GL_CULL_FACE);
1143 mi->polygon_count += drawball(gp, &gp->bman.balls[i], wire);
1153 * new window size or exposure
1155 ENTRYPOINT void reshape_boxed(ModeInfo *mi, int width, int height)
1157 GLfloat h = (GLfloat) height / (GLfloat) width;
1159 glViewport(0, 0, (GLint) width, (GLint) height);
1160 glMatrixMode(GL_PROJECTION);
1162 gluPerspective(50.0,1/h,2.0,1000.0);
1163 glMatrixMode (GL_MODELVIEW);
1171 pinit(ModeInfo * mi)
1173 boxedstruct *gp = &boxed[MI_SCREEN(mi)];
1174 int wire = MI_IS_WIREFRAME (mi);
1178 char *texpixeltarget;
1180 glShadeModel(GL_SMOOTH);
1182 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1184 /* Load configuration */
1185 setdefaultconfig(&gp->config);
1187 /* give the decay parameter a better curve */
1188 if (gp->config.decay <= 0.8182) { gp->config.decay = gp->config.decay / 3; }
1189 else { gp->config.decay = (gp->config.decay - 0.75) * 4; }
1193 bman->balls = (ball *)malloc(gp->config.numballs * sizeof(ball));
1194 bman->num_balls = gp->config.numballs;
1195 bman->ballsize = gp->config.ballsize;
1196 bman->explosion = gp->config.explosion;
1198 gp->tman = (triman *)malloc(bman->num_balls * sizeof(triman));
1199 memset(gp->tman,0,bman->num_balls * sizeof(triman));
1201 for(i=0;i<bman->num_balls;i++) {
1202 gp->tman[i].explosion = (float) (((int)gp->config.explosion) / 15.0f );
1203 gp->tman[i].decay = gp->config.decay;
1204 gp->tman[i].momentum = gp->config.momentum;
1205 gp->tman[i].vertices = NULL;
1206 gp->tman[i].normals = NULL;
1207 gp->tman[i].tris = NULL;
1208 createball(&bman->balls[i]);
1209 bman->balls[i].loc.y *= rnd();
1215 glEnable(GL_CULL_FACE);
1216 glEnable(GL_LIGHTING);
1219 /* define cam path */
1220 gp->cam_x_speed = 1.0f/((float)gp->config.camspeed/50.0 + rnd()*((float)gp->config.camspeed/50.0));
1221 gp->cam_z_speed = 1.0f/((float)gp->config.camspeed/50.0 + rnd()*((float)gp->config.camspeed/50.0));
1222 gp->cam_y_speed = 1.0f/((float)gp->config.camspeed/250.0 + rnd()*((float)gp->config.camspeed/250.0));
1223 if (rnd() < 0.5f) gp->cam_x_speed = -gp->cam_x_speed;
1224 if (rnd() < 0.5f) gp->cam_z_speed = -gp->cam_z_speed;
1226 /* define initial cam position */
1227 gp->tic = gp->camtic = rnd() * 100.0f;
1229 /* define tex1 (bottom plate) */
1230 gp->tex1 = (char *)malloc(3*width*height*sizeof(GLuint));
1231 texpixels = 256*256; /*width*height;*/
1232 texpixeldata = header_data;
1233 texpixeltarget = gp->tex1;
1234 for (i=0; i < texpixels; i++) {
1235 HEADER_PIXEL(texpixeldata,texpixeltarget);
1236 texpixeltarget += 3;
1240 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
1244 i = gluBuild2DMipmaps(GL_TEXTURE_2D, 3, 256, 256,
1245 GL_RGB, GL_UNSIGNED_BYTE, gp->tex1);
1248 const char *s = (char *) gluErrorString (i);
1249 fprintf (stderr, "%s: error mipmapping texture: %s\n",
1250 progname, (s ? s : "(unknown)"));
1253 check_gl_error("mipmapping");
1255 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, 256, 256, 0,
1256 GL_RGB, GL_UNSIGNED_BYTE,
1258 check_gl_error("texture");
1261 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1262 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
1263 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
1264 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1265 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1272 init_boxed(ModeInfo * mi)
1274 int screen = MI_SCREEN(mi);
1276 /* Colormap cmap; */
1277 /* Boolean rgba, doublebuffer, cmap_installed; */
1280 if (boxed == NULL) {
1281 if ((boxed = (boxedstruct *) calloc(MI_NUM_SCREENS(mi),sizeof (boxedstruct))) == NULL) return;
1283 gp = &boxed[screen];
1284 gp->window = MI_WINDOW(mi);
1286 if ((gp->glx_context = init_GL(mi)) != NULL) {
1287 reshape_boxed(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
1288 glDrawBuffer(GL_BACK);
1289 if (!glIsList(gp->listobjects)) {
1290 gp->listobjects = glGenLists(3);
1303 draw_boxed(ModeInfo * mi)
1305 boxedstruct *gp = &boxed[MI_SCREEN(mi)];
1306 Display *display = MI_DISPLAY(mi);
1307 Window window = MI_WINDOW(mi);
1309 if (!gp->glx_context)
1312 glDrawBuffer(GL_BACK);
1314 glXMakeCurrent(display, window, *(gp->glx_context));
1317 if (mi->fps_p) do_fps (mi);
1319 glXSwapBuffers(display, window);
1323 release_boxed(ModeInfo * mi)
1327 if (boxed != NULL) {
1330 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
1331 boxedstruct *gp = &boxed[screen];
1333 if (gp->glx_context) {
1334 /* Display lists MUST be freed while their glXContext is current. */
1335 glXMakeCurrent(MI_DISPLAY(mi), gp->window, *(gp->glx_context));
1337 if (glIsList(gp->listobjects))
1338 glDeleteLists(gp->listobjects, 3);
1340 for (i=0;i<gp->bman.num_balls;i++) {
1341 if (gp->bman.balls[i].bounced) freetris(&gp->tman[i]);
1343 free (gp->bman.balls);
1350 (void) free((void *) boxed);
1357 XSCREENSAVER_MODULE ("Boxed", boxed)
1359 /*********************************************************/