1 /* thebox --- 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.
28 #include <X11/Intrinsic.h>
32 **----------------------------------------------------------------------------
34 **----------------------------------------------------------------------------
38 # define PROGCLASS "boxed"
39 # define HACK_INIT init_boxed
40 # define HACK_DRAW draw_boxed
41 # define HACK_RESHAPE reshape_boxed
42 # define boxed_opts xlockmore_opts
44 # define DEF_SPEED "0.5"
45 # define DEFAULTS "*delay: 20000 \n" \
46 "*showFPS: False \n" \
47 "*wireframe: False \n"
49 # include "xlockmore.h" /* from the xscreensaver distribution */
50 #else /* !STANDALONE */
51 # include "xlock.h" /* from the xlockmore distribution */
52 #endif /* !STANDALONE */
58 #define countof(x) (int)(sizeof((x))/sizeof((*x)))
60 #define rnd() (frand(1.0))
62 GLfloat speed; /* jwz -- overall speed factor applied to all motion */
65 static XrmOptionDescRec opts[] = {
66 {"-speed", ".boxed.speed", XrmoptionSepArg, 0},
69 static argtype vars[] = {
70 {&speed, "speed", "Speed", DEF_SPEED, t_Float},
73 ModeSpecOpt boxed_opts = {countof(opts), opts, countof(vars), vars, NULL};
77 ModStruct boxed_description = {
78 "boxed", "init_boxed", "draw_boxed", "release_boxed",
79 "draw_boxed", "init_boxed", NULL, &boxed_opts,
80 1000, 1, 2, 1, 4, 1.0, "",
81 "Shows GL's boxed balls", 0, NULL};
89 /* rendering defines */
93 #define BOX_SIZE 20.0f
96 #define CAM_HEIGHT 100.0f
97 #define CAMDISTANCE_MIN 20.0
98 #define CAMDISTANCE_MAX 150.0
99 #define CAMDISTANCE_SPEED 1.5
101 /* rendering the sphere */
103 #define SPHERE_VERTICES (2+MESH_SIZE*MESH_SIZE*2)
104 #define SPHERE_INDICES ((MESH_SIZE*4 + MESH_SIZE*4*(MESH_SIZE-1))*3)
106 #define EXPLOSION 10.0f
109 #define BALLSIZE 3.0f;
112 **-----------------------------------------------------------------------------
114 **-----------------------------------------------------------------------------
169 float cam_x_speed, cam_z_speed, cam_y_speed;
172 vectorf spherev[SPHERE_VERTICES];
173 GLint spherei[SPHERE_INDICES];
176 GLXContext *glx_context;
184 #define GLL_PATTERN 0
189 **----------------------------------------------------------------------------
191 **----------------------------------------------------------------------------
194 static boxedstruct *boxed = NULL;
198 **----------------------------------------------------------------------------
200 **----------------------------------------------------------------------------
206 static inline void addvectors(vectorf *dest, vectorf *s1, vectorf *s2)
208 dest->x = s1->x + s2->x;
209 dest->y = s1->y + s2->y;
210 dest->z = s1->z + s2->z;
216 static inline void subvectors(vectorf *dest, vectorf* s1, vectorf *s2)
218 dest->x = s1->x - s2->x;
219 dest->y = s1->y - s2->y;
220 dest->z = s1->z - s2->z;
224 * Multiply vector with scalar (scale vector)
226 static inline void scalevector(vectorf *dest, vectorf *source, GLfloat sc)
228 dest->x = source->x * sc;
229 dest->y = source->y * sc;
230 dest->z = source->z * sc;
236 static inline void copyvector(vectorf *dest, vectorf* source)
244 static inline GLfloat
245 dotproduct(vectorf * v1, vectorf * v2)
247 return v1->x * v2->x + v1->y * v2->y + v1->z * v2->z;
250 static inline GLfloat
251 squaremagnitude(vectorf * v)
253 return v->x * v->x + v->y * v->y + v->z * v->z;
259 * Generate the Sphere data
264 static void generatesphere(void)
266 float dj = M_PI/(MESH_SIZE+1.0f);
267 float di = M_PI/MESH_SIZE;
268 int v; /* vertex offset */
269 int ind; /* indices offset */
271 GLfloat r_y_plane, h_y_plane;
276 * generate the sphere data
277 * vertices 0 and 1 are the north and south poles
280 spherei = boxed->spherei;
281 spherev = boxed->spherev;
283 spherev[0].x = 0.0f; spherev[0].y =1.0f; spherev[0].z = 0.0f;
284 spherev[1].x = 0.0f; spherev[1].y =-1.0f; spherev[1].z = 0.0f;
286 for (j=0; j<MESH_SIZE; j++) {
287 r_y_plane = (float)sin((j+1) * dj);
288 h_y_plane = (float)cos((j+1) * dj);
289 for (i=0; i<MESH_SIZE*2; i++) {
290 si = 2+i+j*MESH_SIZE*2;
291 spherev[si].y = h_y_plane;
292 spherev[si].x = (float) sin(i * di) * r_y_plane;
293 spherev[si].z = (float) cos(i * di) * r_y_plane;
297 /* generate indices */
298 for (i=0; i<MESH_SIZE*2; i++) {
300 spherei[3*i+1] = i+2;
301 spherei[3*i+2] = i+3;
302 if (i==MESH_SIZE*2-1)
306 /* the middle strips */
307 for (j=0; j<MESH_SIZE-1; j++) {
309 ind = 3*MESH_SIZE*2 + j*6*MESH_SIZE*2;
310 for (i=0; i<MESH_SIZE*2; i++) {
311 spherei[6*i+ind] = v+i;
312 spherei[6*i+2+ind] = v+i+1;
313 spherei[6*i+1+ind] = v+i+MESH_SIZE*2;
315 spherei[6*i+ind+3] = v+i+MESH_SIZE*2;
316 spherei[6*i+2+ind+3] = v+i+1;
317 spherei[6*i+1+ind+3] = v+i+MESH_SIZE*2+1;
318 if (i==MESH_SIZE*2-1) {
319 spherei[6*i+2+ind] = v+i+1-2*MESH_SIZE;
320 spherei[6*i+2+ind+3] = v+i+1-2*MESH_SIZE;
321 spherei[6*i+1+ind+3] = v+i+MESH_SIZE*2+1-2*MESH_SIZE;
326 v = SPHERE_VERTICES-MESH_SIZE*2;
327 ind = SPHERE_INDICES-3*MESH_SIZE*2;
328 for (i=0; i<MESH_SIZE*2; i++) {
329 spherei[3*i+ind] = 1;
330 spherei[3*i+1+ind] = v+i+1;
331 spherei[3*i+2+ind] = v+i;
332 if (i==MESH_SIZE*2-1)
333 spherei[3*i+1+ind] = v;
344 void createball(ball *newball)
346 float r=0.0f,g=0.0f,b=0.0f;
347 newball->loc.x = 5-10*rnd();
348 newball->loc.y = 35+20*rnd();
349 newball->loc.z = 5-10*rnd();
350 newball->dir.x = (0.5f-rnd()) * speed;
351 newball->dir.y = 0.0;
352 newball->dir.z = (0.5-rnd()) * speed;
353 newball->offside = 0;
354 newball->bounced = FALSE;
355 newball->radius = BALLSIZE;
356 while (r+g+b < 1.7f ) {
357 newball->color.x = r=rnd();
358 newball->color.y = g=rnd();
359 newball->color.z = b=rnd();
361 newball->justcreated = TRUE;
364 /* Update position of each ball */
366 void updateballs(ballman *bman)
369 vectorf dvect,richting,relspeed,influence;
372 for (b=0;b<bman->num_balls;b++) {
374 GLfloat gravity = 0.15f * speed;
377 bman->balls[b].dir.y -= gravity;
379 addvectors(&bman->balls[b].loc,&bman->balls[b].loc,&bman->balls[b].dir);
381 if (bman->balls[b].loc.y < bman->balls[b].radius) { /* ball onder bodem? (bodem @ y=0) */
382 if ((bman->balls[b].loc.x < -100.0) ||
383 (bman->balls[b].loc.x > 100.0) ||
384 (bman->balls[b].loc.z < -100.0) ||
385 (bman->balls[b].loc.z > 100.0)) {
386 if (bman->balls[b].loc.y < -1000.0)
387 createball(&bman->balls[b]);
389 bman->balls[b].loc.y = bman->balls[b].radius + (bman->balls[b].radius - bman->balls[b].loc.y);
390 bman->balls[b].dir.y = -bman->balls[b].dir.y;
391 if (bman->balls[b].offside) {
392 bman->balls[b].bounced = TRUE; /* temporary disable painting ball */
393 scalevector(&bman->balls[b].dir,&bman->balls[b].dir,0.80f);
394 if (squaremagnitude(&bman->balls[b].dir) < 0.08f) {
395 createball(&bman->balls[b]);
401 if (!bman->balls[b].offside) {
402 if (bman->balls[b].loc.x - bman->balls[b].radius < -20.0f) { /* x ondergrens */
403 if (bman->balls[b].loc.y > 41+bman->balls[b].radius) bman->balls[b].offside=1;
405 bman->balls[b].dir.x = -bman->balls[b].dir.x;
406 bman->balls[b].loc.x = -20.0f + bman->balls[b].radius;
409 if (bman->balls[b].loc.x + bman->balls[b].radius > 20.0f) { /* x bovengrens */
410 if (bman->balls[b].loc.y > 41+bman->balls[b].radius) bman->balls[b].offside=1;
412 bman->balls[b].dir.x = -bman->balls[b].dir.x;
413 bman->balls[b].loc.x = 20.0f - bman->balls[b].radius;
416 if (bman->balls[b].loc.z - bman->balls[b].radius < -20.0f) { /* z ondergrens */
417 if (bman->balls[b].loc.y > 41+bman->balls[b].radius) bman->balls[b].offside=1;
419 bman->balls[b].dir.z = -bman->balls[b].dir.z;
420 bman->balls[b].loc.z = -20.0f + bman->balls[b].radius;
423 if (bman->balls[b].loc.z + bman->balls[b].radius > 20.0f) { /* z bovengrens */
424 if (bman->balls[b].loc.y > 41+bman->balls[b].radius) bman->balls[b].offside=1;
426 bman->balls[b].dir.z = -bman->balls[b].dir.z;
427 bman->balls[b].loc.z = 20.0f - bman->balls[b].radius;
430 } /* end if !offside */
432 /* check voor stuiteren */
433 for (j=b+1;j<bman->num_balls;j++) {
434 squaredist = (bman->balls[b].radius * bman->balls[b].radius) + (bman->balls[j].radius * bman->balls[j].radius);
435 subvectors(&dvect,&bman->balls[b].loc,&bman->balls[j].loc);
436 if ( squaremagnitude(&dvect) < squaredist ) { /* balls b and j touch */
437 subvectors(&richting,&bman->balls[j].loc,&bman->balls[b].loc);
438 subvectors(&relspeed,&bman->balls[b].dir,&bman->balls[j].dir);
439 /* calc mutual influence direction and magnitude */
440 scalevector(&influence,&richting,(dotproduct(&richting,&relspeed)/squaremagnitude(&richting)));
442 subvectors(&bman->balls[b].dir,&bman->balls[b].dir,&influence);
443 addvectors(&bman->balls[j].dir,&bman->balls[j].dir,&influence);
444 addvectors(&bman->balls[b].loc,&bman->balls[b].loc,&bman->balls[b].dir);
445 addvectors(&bman->balls[j].loc,&bman->balls[j].loc,&bman->balls[j].dir);
447 subvectors(&dvect,&bman->balls[b].loc,&bman->balls[j].loc);
448 while (squaremagnitude(&dvect) < squaredist) {
449 addvectors(&bman->balls[b].loc,&bman->balls[b].loc,&bman->balls[b].dir);
450 addvectors(&bman->balls[j].loc,&bman->balls[j].loc,&bman->balls[j].dir);
451 subvectors(&dvect,&bman->balls[b].loc,&bman->balls[j].loc);
460 * explode ball into triangles
463 void createtrisfromball(triman* tman, vectorf *spherev, GLint *spherei, int ind_num, ball *b)
469 vectorf avgdir,dvect;
471 tman->scalefac = b->radius;
472 copyvector(&tman->color,&b->color);
473 explosion = 1.0f + tman->explosion * 2.0 * rnd();
475 tman->num_tri = ind_num/3;
477 /* reserveer geheugen voor de poly's in een bal */
479 tman->tris = (tri *)malloc(tman->num_tri * sizeof(tri));
480 tman->vertices = (vectorf *)malloc(ind_num * sizeof(vectorf));
481 tman->normals = (vectorf *)malloc(ind_num/3 * sizeof(vectorf));
483 for (i=0; i<(tman->num_tri); i++) {
484 tman->tris[i].far = FALSE;
486 /* kopieer elke poly apart naar een tri structure */
487 copyvector(&tman->vertices[pos+0],&spherev[spherei[pos+0]]);
488 copyvector(&tman->vertices[pos+1],&spherev[spherei[pos+1]]);
489 copyvector(&tman->vertices[pos+2],&spherev[spherei[pos+2]]);
490 /* Calculate average direction of shrapnel */
491 addvectors(&avgdir,&tman->vertices[pos+0],&tman->vertices[pos+1]);
492 addvectors(&avgdir,&avgdir,&tman->vertices[pos+2]);
493 scalevector(&avgdir,&avgdir,0.33333);
495 /* should normalize first, NYI */
496 copyvector(&tman->normals[i],&avgdir);
498 /* copy de lokatie */
499 addvectors(&tman->tris[i].loc,&b->loc,&avgdir);
500 /* en translate alle triangles terug naar hun eigen oorsprong */
501 tman->vertices[pos+0].x -= avgdir.x;
502 tman->vertices[pos+0].y -= avgdir.y;
503 tman->vertices[pos+0].z -= avgdir.z;
504 tman->vertices[pos+1].x -= avgdir.x;
505 tman->vertices[pos+1].y -= avgdir.y;
506 tman->vertices[pos+1].z -= avgdir.z;
507 tman->vertices[pos+2].x -= avgdir.x;
508 tman->vertices[pos+2].y -= avgdir.y;
509 tman->vertices[pos+2].z -= avgdir.z;
510 /* alwaar opschaling plaatsvindt */
511 scale = b->radius * 2;
512 scalevector(&tman->vertices[pos+0],&tman->vertices[pos+0],scale);
513 scalevector(&tman->vertices[pos+1],&tman->vertices[pos+1],scale);
514 scalevector(&tman->vertices[pos+2],&tman->vertices[pos+2],scale);
516 /* bereken nieuwe richting */
517 scalevector(&tman->tris[i].dir,&avgdir,explosion);
518 dvect.x = (0.1f - 0.2f*rnd());
519 dvect.y = (0.15f - 0.3f*rnd());
520 dvect.z = (0.1f - 0.2f*rnd());
521 addvectors(&tman->tris[i].dir,&tman->tris[i].dir,&dvect);
527 * update position of each tri
530 void updatetris(triman *t)
535 for (b=0;b<t->num_tri;b++) {
537 t->tris[b].dir.y -= (0.1f * speed);
539 addvectors(&t->tris[b].loc,&t->tris[b].loc,&t->tris[b].dir);
541 if (t->tris[b].far) continue;
542 if (t->tris[b].loc.y < 0) { /* onder bodem ? */
543 if ((t->tris[b].loc.x > -100.0f) &
544 (t->tris[b].loc.x < 100.0f) &
545 (t->tris[b].loc.z > -100.0f) &
546 (t->tris[b].loc.z < 100.0f)) { /* in veld */
547 t->tris[b].dir.y = -(t->tris[b].dir.y);
548 t->tris[b].loc.y = -t->tris[b].loc.y;
549 scalevector(&t->tris[b].dir,&t->tris[b].dir,0.80f); /* dampening */
552 t->tris[b].far = TRUE;
557 if ((t->tris[b].loc.x > -21.0f) &
558 (t->tris[b].loc.x < 21.0f) &
559 (t->tris[b].loc.z > -21.0f) &
560 (t->tris[b].loc.z < 21.0f)) { /* in box? */
562 xd = zd = 999.0f; /* big */
563 if ((t->tris[b].loc.x > -21.0f) &
564 (t->tris[b].loc.x < 0)) {
565 xd = t->tris[b].loc.x + 21.0f;
567 if ((t->tris[b].loc.x < 21.0f) &
568 (t->tris[b].loc.x > 0)) {
569 xd = 21.0f - t->tris[b].loc.x;
571 if ((t->tris[b].loc.z > -21.0f) &
572 (t->tris[b].loc.z < 0)) {
573 zd = t->tris[b].loc.z + 21.0f;
575 if ((t->tris[b].loc.z < 21.0f) &
576 (t->tris[b].loc.z > 0)) {
577 zd = 21.0f - t->tris[b].loc.z;
581 if (t->tris[b].dir.x < 0)
582 t->tris[b].loc.x += (21.0f - t->tris[b].loc.x);
584 t->tris[b].loc.x += (-21.0f - t->tris[b].loc.x);
585 t->tris[b].dir.x = -t->tris[b].dir.x;
588 if (t->tris[b].dir.z < 0)
589 t->tris[b].loc.z += (21.0f - t->tris[b].loc.z);
591 t->tris[b].loc.z += (-21.0f - t->tris[b].loc.z);
592 t->tris[b].dir.z = -t->tris[b].dir.z;
601 * free memory allocated by a tri manager
603 void freetris(triman *t)
606 if (t->tris) free(t->tris);
607 if (t->vertices) free(t->vertices);
608 if (t->normals) free(t->normals);
618 *load defaults in config structure
620 void setdefaultconfig(boxed_config *config)
622 config->numballs = NUMBALLS;
623 config->textures = TRUE;
624 config->transparent = FALSE;
625 config->explosion = 25.0f;
626 config->ballsize = BALLSIZE;
627 config->camspeed = 35.0f;
634 static void drawfilledbox(boxedstruct *boxed, int wire)
636 /* draws texture filled box,
637 top is drawn using the entire texture,
638 the sides are drawn using the edge of the texture
642 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
644 glVertex3f(-1.0,1.0,1.0);
646 glVertex3f(1.0,1.0,1.0);
648 glVertex3f(1.0,-1.0,1.0);
650 glVertex3f(-1.0,-1.0,1.0);
653 glVertex3f(1.0,1.0,-1.0);
655 glVertex3f(-1.0,1.0,-1.0);
657 glVertex3f(-1.0,-1.0,-1.0);
659 glVertex3f(1.0,-1.0,-1.0);
662 glVertex3f(-1.0,1.0,1.0);
664 glVertex3f(-1.0,-1.0,1.0);
666 glVertex3f(-1.0,-1.0,-1.0);
668 glVertex3f(-1.0,1.0,-1.0);
671 glVertex3f(1.0,1.0,1.0);
673 glVertex3f(1.0,1.0,-1.0);
675 glVertex3f(1.0,-1.0,-1.0);
677 glVertex3f(1.0,-1.0,1.0);
679 glTexCoord2f(0.0,0.0);
680 glVertex3f(-1.0,1.0,1.0);
681 glTexCoord2f(0.0,1.0);
682 glVertex3f(-1.0,1.0,-1.0);
683 glTexCoord2f(1.0,1.0);
684 glVertex3f(1.0,1.0,-1.0);
685 glTexCoord2f(1.0,0.0);
686 glVertex3f(1.0,1.0,1.0);
689 glVertex3f(-1.0,-1.0,1.0);
691 glVertex3f(-1.0,-1.0,-1.0);
693 glVertex3f(1.0,-1.0,-1.0);
695 glVertex3f(1.0,-1.0,1.0);
701 * Draw a box made of lines
703 static void drawbox(boxedstruct *boxed)
706 glBegin(GL_LINE_STRIP);
707 glVertex3f(-1.0,1.0,1.0);
708 glVertex3f(-1.0,1.0,-1.0);
709 glVertex3f(1.0,1.0,-1.0);
710 glVertex3f(1.0,1.0,1.0);
711 glVertex3f(-1.0,1.0,1.0);
714 glBegin(GL_LINE_STRIP);
715 glVertex3f(-1.0,-1.0,1.0);
716 glVertex3f(1.0,-1.0,1.0);
717 glVertex3f(1.0,-1.0,-1.0);
718 glVertex3f(-1.0,-1.0,-1.0);
719 glVertex3f(-1.0,-1.0,1.0);
721 /* connect top & bottom */
723 glVertex3f(-1.0,1.0,1.0);
724 glVertex3f(-1.0,-1.0,1.0);
725 glVertex3f(1.0,1.0,1.0);
726 glVertex3f(1.0,-1.0,1.0);
727 glVertex3f(1.0,1.0,-1.0);
728 glVertex3f(1.0,-1.0,-1.0);
729 glVertex3f(-1.0,1.0,-1.0);
730 glVertex3f(-1.0,-1.0,-1.0);
739 static void drawball(boxedstruct *gp, ball *b, int wire)
742 GLint *spherei = gp->spherei;
743 vectorf *spherev = gp->spherev;
748 glTranslatef(b->loc.x,b->loc.y,b->loc.z);
749 glScalef(b->radius,b->radius,b->radius);
750 glColor3f(b->color.x,b->color.y,b->color.z);
754 glMaterialfv(GL_FRONT, GL_DIFFUSE, col);
758 glMaterialfv(GL_FRONT, GL_EMISSION,col);
760 if (!gp->gllists[GLL_BALL]) {
761 glNewList(gp->listobjects + GLL_BALL,GL_COMPILE);
762 cnt = SPHERE_INDICES/3;
763 for (i=0; i<cnt; i++) {
765 glBegin(wire ? GL_LINE_LOOP : GL_TRIANGLES);
766 glNormal3f(spherev[spherei[pos+0]].x,spherev[spherei[pos+0]].y,spherev[spherei[pos+0]].z);
767 glVertex3f(spherev[spherei[pos+0]].x,spherev[spherei[pos+0]].y,spherev[spherei[pos+0]].z);
768 glNormal3f(spherev[spherei[pos+1]].x,spherev[spherei[pos+1]].y,spherev[spherei[pos+1]].z);
769 glVertex3f(spherev[spherei[pos+1]].x,spherev[spherei[pos+1]].y,spherev[spherei[pos+1]].z);
770 glNormal3f(spherev[spherei[pos+2]].x,spherev[spherei[pos+2]].y,spherev[spherei[pos+2]].z);
771 glVertex3f(spherev[spherei[pos+2]].x,spherev[spherei[pos+2]].y,spherev[spherei[pos+2]].z);
775 gp->gllists[GLL_BALL] = 1;
777 glCallList(gp->listobjects + GLL_BALL);
785 * Draw all triangles in triman
787 static void drawtriman(triman *t, int wire)
790 vectorf *spherev = t->vertices;
794 glColor3f(t->color.x,t->color.y,t->color.z);
798 glMaterialfv(GL_FRONT, GL_DIFFUSE, col);
802 glMaterialfv(GL_FRONT, GL_EMISSION,col);
804 for (i=0; i<t->num_tri; i++) {
807 glTranslatef(t->tris[i].loc.x,t->tris[i].loc.y,t->tris[i].loc.z);
808 glBegin(wire ? GL_LINE_LOOP : GL_TRIANGLES);
809 glNormal3f(t->normals[i].x,t->normals[i].y,t->normals[i].z);
810 glVertex3f(spherev[pos+0].x,spherev[pos+0].y,spherev[pos+0].z);
811 glVertex3f(spherev[pos+1].x,spherev[pos+1].y,spherev[pos+1].z);
812 glVertex3f(spherev[pos+2].x,spherev[pos+2].y,spherev[pos+2].z);
822 static void drawpattern(boxedstruct *boxed)
824 if (!boxed->gllists[GLL_PATTERN]) {
825 glNewList(boxed->listobjects + GLL_PATTERN, GL_COMPILE);
827 glBegin(GL_LINE_STRIP);
828 glVertex3f(-25.0f, 0.0f, 35.0f);
829 glVertex3f(-15.0f, 0.0f, 35.0f);
830 glVertex3f(-5.0f, 0.0f, 25.0f);
831 glVertex3f(5.0f, 0.0f, 25.0f);
832 glVertex3f(15.0f, 0.0f, 35.0f);
833 glVertex3f(25.0f, 0.0f, 35.0f);
834 glVertex3f(35.0f, 0.0f, 25.0f);
835 glVertex3f(35.0f, 0.0f, 15.0f);
836 glVertex3f(25.0f, 0.0f, 5.0f);
837 glVertex3f(25.0f, 0.0f, -5.0f);
838 glVertex3f(35.0f, 0.0f, -15.0f);
839 glVertex3f(35.0f, 0.0f, -25.0f);
840 glVertex3f(25.0f, 0.0f, -35.0f);
841 glVertex3f(15.0f, 0.0f,-35.0f);
842 glVertex3f(5.0f, 0.0f, -25.0f);
843 glVertex3f(-5.0f, 0.0f, -25.0f);
844 glVertex3f(-15.0f, 0.0f,-35.0f);
845 glVertex3f(-25.0f, 0.0f,-35.0f);
846 glVertex3f(-35.0f, 0.0f, -25.0f);
847 glVertex3f(-35.0f, 0.0f, -15.0f);
848 glVertex3f(-25.0f, 0.0f, -5.0f);
849 glVertex3f(-25.0f, 0.0f, 5.0f);
850 glVertex3f(-35.0f, 0.0f, 15.0f);
851 glVertex3f(-35.0f, 0.0f, 25.0f);
852 glVertex3f(-25.0f, 0.0f, 35.0f);
855 glBegin(GL_LINE_STRIP);
856 glVertex3f(-5.0f, 0.0f, 15.0f);
857 glVertex3f(5.0f, 0.0f, 15.0f);
858 glVertex3f(15.0f, 0.0f, 5.0f);
859 glVertex3f(15.0f, 0.0f, -5.0f);
860 glVertex3f(5.0f, 0.0f, -15.0f);
861 glVertex3f(-5.0f, 0.0f, -15.0f);
862 glVertex3f(-15.0f, 0.0f, -5.0f);
863 glVertex3f(-15.0f, 0.0f, 5.0f);
864 glVertex3f(-5.0f, 0.0f, 15.0f);
868 boxed->gllists[GLL_PATTERN] = 1;
870 glCallList(boxed->listobjects + GLL_PATTERN);
877 * main rendering loop
879 static void draw(ModeInfo * mi)
881 boxedstruct *gp = &boxed[MI_SCREEN(mi)];
882 int wire = MI_IS_WIREFRAME (mi);
888 GLfloat dgray[4] = {0.3f, 0.3f, 0.3f, 1.0f};
889 GLfloat black[4] = {0.0f, 0.0f, 0.0f, 1.0f};
890 GLfloat lblue[4] = {0.4f,0.6f,1.0f };
892 GLfloat l0_ambient[] = {0.0, 0.0, 0.0, 1.0};
893 GLfloat l0_specular[] = {1.0, 1.0, 1.0, 1.0};
894 GLfloat l0_diffuse[] = {1.0, 1.0, 1.0, 1.0};
895 GLfloat l0_position[] = {0.0, 0.0, 0.0, 1.0}; /* w != 0 -> positional light */
896 GLfloat l1_ambient[] = {0.0, 0.0, 0.0, 1.0};
897 GLfloat l1_specular[] = {1.0, 1.0, 1.0, 1.0};
898 GLfloat l1_diffuse[] = {0.5, 0.5, 0.5, 1.0};
899 GLfloat l1_position[] = {0.0, 1.0, 0.0, 0.0}; /* w = 0 -> directional light */
901 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
906 /* rotate camera around (0,0,0), looking at (0,0,0), up is (0,1,0) */
907 dcam = CAMDISTANCE_MIN + (CAMDISTANCE_MAX - CAMDISTANCE_MIN) + (CAMDISTANCE_MAX - CAMDISTANCE_MIN)*cos((gp->tic/CAMDISTANCE_SPEED) * speed);
908 v1.x = dcam * sin((gp->tic/gp->cam_x_speed) * speed);
909 v1.z = dcam * cos((gp->tic/gp->cam_z_speed) * speed);
910 v1.y = CAM_HEIGHT * sin((gp->tic/gp->cam_y_speed) * speed) + 1.02 * CAM_HEIGHT;
911 gluLookAt(v1.x,v1.y,v1.z,0.0,0.0,0.0,0.0,1.0,0.0);
914 glLightfv(GL_LIGHT0, GL_AMBIENT, l0_ambient);
915 glLightfv(GL_LIGHT0, GL_DIFFUSE, l0_diffuse);
916 glLightfv(GL_LIGHT0, GL_SPECULAR, l0_specular);
917 glLightfv(GL_LIGHT0, GL_POSITION, l0_position);
918 glLightfv(GL_LIGHT1, GL_AMBIENT, l1_ambient);
919 glLightfv(GL_LIGHT1, GL_DIFFUSE, l1_diffuse);
920 glLightfv(GL_LIGHT1, GL_SPECULAR, l1_specular);
921 glLightfv(GL_LIGHT1, GL_POSITION, l1_position);
927 glMaterialfv(GL_FRONT, GL_SPECULAR, black);
928 glMaterialfv(GL_FRONT, GL_EMISSION, lblue);
929 glMaterialfv(GL_FRONT,GL_AMBIENT,black);
930 glMaterialf(GL_FRONT, GL_SHININESS, 5.0);
934 /* draw ground grid */
935 /* glDisable(GL_DEPTH_TEST); */
936 glDisable(GL_LIGHTING);
938 glColor3f(0.1,0.1,0.6);
939 for (dx= -2; dx<3; dx++) {
940 for (dz= -2; dz<3; dz++) {
942 glTranslatef(dx*30.0f, 0.0f, dz*30.0f);
948 /* Set drawing mode for the boxes */
949 glEnable(GL_DEPTH_TEST);
950 if (!wire) glEnable(GL_TEXTURE_2D);
952 glColor3f(1.0,1.0,1.0);
953 glScalef(20.0,0.25,20.0);
954 glTranslatef(0.0,2.0,0.0);
955 drawfilledbox(gp, wire);
957 glDisable(GL_TEXTURE_2D);
960 glColor3f(0.2,0.5,0.2);
961 glScalef(20.0,20.0,0.25);
962 glTranslatef(0.0,1.0,81.0);
967 glColor3f(0.2,0.5,0.2);
968 glScalef(20.0,20.0,0.25);
969 glTranslatef(0.0,1.0,-81.0);
974 glColor3f(0.2,0.5,0.2);
975 glScalef(.25,20.0,20.0);
976 glTranslatef(-81.0,1.0,0.0);
981 glColor3f(0.2,0.5,0.2);
982 glScalef(.25,20.0,20.0);
983 glTranslatef(81.0,1.0,0.0);
988 glEnable(GL_LIGHTING);
990 glMaterialfv(GL_FRONT, GL_DIFFUSE, dgray);
991 glMaterialfv(GL_FRONT, GL_EMISSION, black); /* turn it off before painting the balls */
994 /* move the balls and shrapnel */
995 updateballs(&gp->bman);
998 for (i=0;i<gp->bman.num_balls;i++) {
999 if (gp->bman.balls[i].justcreated) {
1000 gp->bman.balls[i].justcreated = FALSE;
1001 freetris(&gp->tman[i]);
1003 if (gp->bman.balls[i].bounced) {
1004 if (gp->tman[i].vertices == NULL) {
1005 createtrisfromball(&gp->tman[i],gp->spherev,gp->spherei,SPHERE_INDICES,&gp->bman.balls[i]);
1007 updatetris(&gp->tman[i]);
1009 glDisable(GL_CULL_FACE);
1010 drawtriman(&gp->tman[i], wire);
1011 if (!wire) glEnable(GL_CULL_FACE);
1013 drawball(gp, &gp->bman.balls[i], wire);
1023 * new window size or exposure
1025 void reshape_boxed(ModeInfo *mi, int width, int height)
1027 GLfloat h = (GLfloat) height / (GLfloat) width;
1029 glViewport(0, 0, (GLint) width, (GLint) height);
1030 glMatrixMode(GL_PROJECTION);
1032 gluPerspective(50.0,1/h,2.0,1000.0);
1033 glMatrixMode (GL_MODELVIEW);
1041 pinit(ModeInfo * mi)
1043 boxedstruct *gp = &boxed[MI_SCREEN(mi)];
1044 int wire = MI_IS_WIREFRAME (mi);
1048 char *texpixeltarget;
1050 glShadeModel(GL_SMOOTH);
1052 glClearColor(0.0,0.05,0.1,0.0);
1053 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1055 /* Load configuration */
1056 setdefaultconfig(&gp->config);
1060 bman->balls = (ball *)malloc(gp->config.numballs * sizeof(ball));
1061 bman->num_balls = gp->config.numballs;
1062 bman->ballsize = gp->config.ballsize;
1063 bman->explosion = gp->config.explosion;
1065 gp->tman = (triman *)malloc(bman->num_balls * sizeof(triman));
1066 memset(gp->tman,0,bman->num_balls * sizeof(triman));
1068 for(i=0;i<bman->num_balls;i++) {
1069 gp->tman[i].explosion = (float) (((int)gp->config.explosion) / 15.0f );
1070 gp->tman[i].vertices = NULL;
1071 gp->tman[i].normals = NULL;
1072 gp->tman[i].tris = NULL;
1073 createball(&bman->balls[i]);
1074 bman->balls[i].loc.y *= rnd();
1080 glEnable(GL_CULL_FACE);
1081 glEnable(GL_LIGHTING);
1084 /* define cam path */
1085 gp->cam_x_speed = 1.0f/((float)gp->config.camspeed/50.0 + rnd()*((float)gp->config.camspeed/50.0));
1086 gp->cam_z_speed = 1.0f/((float)gp->config.camspeed/50.0 + rnd()*((float)gp->config.camspeed/50.0));
1087 gp->cam_y_speed = 1.0f/((float)gp->config.camspeed/250.0 + rnd()*((float)gp->config.camspeed/250.0));
1088 if (rnd() < 0.5f) gp->cam_x_speed = -gp->cam_x_speed;
1089 if (rnd() < 0.5f) gp->cam_z_speed = -gp->cam_z_speed;
1092 gp->tex1 = (char *)malloc(3*width*height*sizeof(GLuint));
1093 texpixels = 256*256; /*width*height;*/
1094 texpixeldata = header_data;
1095 texpixeltarget = gp->tex1;
1096 for (i=0; i < texpixels; i++) {
1097 HEADER_PIXEL(texpixeldata,texpixeltarget);
1098 texpixeltarget += 3;
1102 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
1105 i = gluBuild2DMipmaps(GL_TEXTURE_2D, 3, 256, 256,
1106 GL_RGB, GL_UNSIGNED_BYTE, gp->tex1);
1109 const char *s = (char *) gluErrorString (i);
1110 fprintf (stderr, "%s: error mipmapping texture: %s\n",
1111 progname, (s ? s : "(unknown)"));
1114 check_gl_error("mipmapping");
1116 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1117 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
1118 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
1119 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1120 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1127 init_boxed(ModeInfo * mi)
1129 int screen = MI_SCREEN(mi);
1131 /* Colormap cmap; */
1132 /* Boolean rgba, doublebuffer, cmap_installed; */
1135 if (boxed == NULL) {
1136 if ((boxed = (boxedstruct *) calloc(MI_NUM_SCREENS(mi),sizeof (boxedstruct))) == NULL) return;
1138 gp = &boxed[screen];
1139 gp->window = MI_WINDOW(mi);
1141 if ((gp->glx_context = init_GL(mi)) != NULL) {
1142 reshape_boxed(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
1143 glDrawBuffer(GL_BACK);
1144 if (!glIsList(gp->listobjects)) {
1145 gp->listobjects = glGenLists(3);
1158 draw_boxed(ModeInfo * mi)
1160 boxedstruct *gp = &boxed[MI_SCREEN(mi)];
1161 Display *display = MI_DISPLAY(mi);
1162 Window window = MI_WINDOW(mi);
1164 if (!gp->glx_context)
1167 glDrawBuffer(GL_BACK);
1169 glXMakeCurrent(display, window, *(gp->glx_context));
1172 if (mi->fps_p) do_fps (mi);
1174 glXSwapBuffers(display, window);
1178 release_boxed(ModeInfo * mi)
1182 if (boxed != NULL) {
1185 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
1186 boxedstruct *gp = &boxed[screen];
1188 if (gp->glx_context) {
1189 /* Display lists MUST be freed while their glXContext is current. */
1190 glXMakeCurrent(MI_DISPLAY(mi), gp->window, *(gp->glx_context));
1192 if (glIsList(gp->listobjects))
1193 glDeleteLists(gp->listobjects, 3);
1195 for (i=0;i<gp->bman.num_balls;i++) {
1196 if (gp->bman.balls[i].bounced) freetris(&gp->tman[i]);
1198 free (gp->bman.balls);
1205 (void) free((void *) boxed);
1212 /*********************************************************/