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
43 # define DEFAULTS "*delay: 20000 \n" \
44 "*showFPS: False \n" \
46 # include "xlockmore.h" /* from the xscreensaver distribution */
47 #else /* !STANDALONE */
48 # include "xlock.h" /* from the xlockmore distribution */
49 #endif /* !STANDALONE */
55 #define countof(x) (int)(sizeof((x))/sizeof((*x)))
57 #define rnd() (frand(1.0))
59 /* #define DEF_PLANETARY "False"
63 static XrmOptionDescRec opts[] = {
64 {"-planetary", ".gears.planetary", XrmoptionNoArg, (caddr_t) "true" },
65 {"+planetary", ".gears.planetary", XrmoptionNoArg, (caddr_t) "false" },
68 static argtype vars[] = {
69 {(caddr_t *) &planetary, "planetary", "Planetary", DEF_PLANETARY, t_Bool},
73 /* ModeSpecOpts boxed_opts = {countof(opts), opts, countof(vars), vars, NULL}; */
75 ModeSpecOpt boxed_opts = {0, NULL, 0, NULL, NULL};
79 ModStruct boxed_description = {
80 "boxed", "init_boxed", "draw_boxed", "release_boxed",
81 "draw_boxed", "init_boxed", NULL, &boxed_opts,
82 1000, 1, 2, 1, 4, 1.0, "",
83 "Shows GL's boxed balls", 0, NULL};
91 /* rendering defines */
94 #define BOX_SIZE 20.0f
97 #define CAM_HEIGHT 100.0f
98 #define CAMDISTANCE_MIN 20.0
99 #define CAMDISTANCE_MAX 150.0
100 #define CAMDISTANCE_SPEED 1.5
102 /* rendering the sphere */
104 #define SPHERE_VERTICES (2+MESH_SIZE*MESH_SIZE*2)
105 #define SPHERE_INDICES ((MESH_SIZE*4 + MESH_SIZE*4*(MESH_SIZE-1))*3)
107 #define EXPLOSION 10.0f
110 #define BALLSIZE 3.0f;
113 **-----------------------------------------------------------------------------
115 **-----------------------------------------------------------------------------
170 float cam_x_speed, cam_z_speed, cam_y_speed;
173 vectorf spherev[SPHERE_VERTICES];
174 GLint spherei[SPHERE_INDICES];
177 GLXContext *glx_context;
185 #define GLL_PATTERN 0
190 **----------------------------------------------------------------------------
192 **----------------------------------------------------------------------------
195 static boxedstruct *boxed = NULL;
199 **----------------------------------------------------------------------------
201 **----------------------------------------------------------------------------
207 static inline void addvectors(vectorf *dest, vectorf *s1, vectorf *s2)
209 dest->x = s1->x + s2->x;
210 dest->y = s1->y + s2->y;
211 dest->z = s1->z + s2->z;
217 static inline void subvectors(vectorf *dest, vectorf* s1, vectorf *s2)
219 dest->x = s1->x - s2->x;
220 dest->y = s1->y - s2->y;
221 dest->z = s1->z - s2->z;
225 * Multiply vector with scalar (scale vector)
227 static inline void scalevector(vectorf *dest, vectorf *source, GLfloat sc)
229 dest->x = source->x * sc;
230 dest->y = source->y * sc;
231 dest->z = source->z * sc;
237 static inline void copyvector(vectorf *dest, vectorf* source)
245 static inline GLfloat
246 dotproduct(vectorf * v1, vectorf * v2)
248 return v1->x * v2->x + v1->y * v2->y + v1->z * v2->z;
251 static inline GLfloat
252 squaremagnitude(vectorf * v)
254 return v->x * v->x + v->y * v->y + v->z * v->z;
260 * Generate the Sphere data
265 static void generatesphere(void)
267 float dj = M_PI/(MESH_SIZE+1.0f);
268 float di = M_PI/MESH_SIZE;
269 int v; /* vertex offset */
270 int ind; /* indices offset */
272 GLfloat r_y_plane, h_y_plane;
277 * generate the sphere data
278 * vertices 0 and 1 are the north and south poles
281 spherei = boxed->spherei;
282 spherev = boxed->spherev;
284 spherev[0].x = 0.0f; spherev[0].y =1.0f; spherev[0].z = 0.0f;
285 spherev[1].x = 0.0f; spherev[1].y =-1.0f; spherev[1].z = 0.0f;
287 for (j=0; j<MESH_SIZE; j++) {
288 r_y_plane = (float)sin((j+1) * dj);
289 h_y_plane = (float)cos((j+1) * dj);
290 for (i=0; i<MESH_SIZE*2; i++) {
291 si = 2+i+j*MESH_SIZE*2;
292 spherev[si].y = h_y_plane;
293 spherev[si].x = (float) sin(i * di) * r_y_plane;
294 spherev[si].z = (float) cos(i * di) * r_y_plane;
298 /* generate indices */
299 for (i=0; i<MESH_SIZE*2; i++) {
301 spherei[3*i+1] = i+2;
302 spherei[3*i+2] = i+3;
303 if (i==MESH_SIZE*2-1)
307 /* the middle strips */
308 for (j=0; j<MESH_SIZE-1; j++) {
310 ind = 3*MESH_SIZE*2 + j*6*MESH_SIZE*2;
311 for (i=0; i<MESH_SIZE*2; i++) {
312 spherei[6*i+ind] = v+i;
313 spherei[6*i+2+ind] = v+i+1;
314 spherei[6*i+1+ind] = v+i+MESH_SIZE*2;
316 spherei[6*i+ind+3] = v+i+MESH_SIZE*2;
317 spherei[6*i+2+ind+3] = v+i+1;
318 spherei[6*i+1+ind+3] = v+i+MESH_SIZE*2+1;
319 if (i==MESH_SIZE*2-1) {
320 spherei[6*i+2+ind] = v+i+1-2*MESH_SIZE;
321 spherei[6*i+2+ind+3] = v+i+1-2*MESH_SIZE;
322 spherei[6*i+1+ind+3] = v+i+MESH_SIZE*2+1-2*MESH_SIZE;
327 v = SPHERE_VERTICES-MESH_SIZE*2;
328 ind = SPHERE_INDICES-3*MESH_SIZE*2;
329 for (i=0; i<MESH_SIZE*2; i++) {
330 spherei[3*i+ind] = 1;
331 spherei[3*i+1+ind] = v+i+1;
332 spherei[3*i+2+ind] = v+i;
333 if (i==MESH_SIZE*2-1)
334 spherei[3*i+1+ind] = v;
345 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();
351 newball->dir.y = 0.0;
352 newball->dir.z = 0.5-rnd();
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) {
368 vectorf dvect,richting,relspeed,influence;
371 for (b=0;b<bman->num_balls;b++) {
374 bman->balls[b].dir.y -= 0.15f;
376 addvectors(&bman->balls[b].loc,&bman->balls[b].loc,&bman->balls[b].dir);
378 if (bman->balls[b].loc.y < bman->balls[b].radius) { /* ball onder bodem? (bodem @ y=0) */
379 if ((bman->balls[b].loc.x < -100.0) ||
380 (bman->balls[b].loc.x > 100.0) ||
381 (bman->balls[b].loc.z < -100.0) ||
382 (bman->balls[b].loc.z > 100.0)) {
383 if (bman->balls[b].loc.y < -1000.0)
384 createball(&bman->balls[b]);
386 bman->balls[b].loc.y = bman->balls[b].radius + (bman->balls[b].radius - bman->balls[b].loc.y);
387 bman->balls[b].dir.y = -bman->balls[b].dir.y;
388 if (bman->balls[b].offside) {
389 bman->balls[b].bounced = TRUE; /* temporary disable painting ball */
390 scalevector(&bman->balls[b].dir,&bman->balls[b].dir,0.80f);
391 if (squaremagnitude(&bman->balls[b].dir) < 0.08f) {
392 createball(&bman->balls[b]);
398 if (!bman->balls[b].offside) {
399 if (bman->balls[b].loc.x - bman->balls[b].radius < -20.0f) { /* x ondergrens */
400 if (bman->balls[b].loc.y > 41+bman->balls[b].radius) bman->balls[b].offside=1;
402 bman->balls[b].dir.x = -bman->balls[b].dir.x;
403 bman->balls[b].loc.x = -20.0f + bman->balls[b].radius;
406 if (bman->balls[b].loc.x + bman->balls[b].radius > 20.0f) { /* x bovengrens */
407 if (bman->balls[b].loc.y > 41+bman->balls[b].radius) bman->balls[b].offside=1;
409 bman->balls[b].dir.x = -bman->balls[b].dir.x;
410 bman->balls[b].loc.x = 20.0f - bman->balls[b].radius;
413 if (bman->balls[b].loc.z - bman->balls[b].radius < -20.0f) { /* z ondergrens */
414 if (bman->balls[b].loc.y > 41+bman->balls[b].radius) bman->balls[b].offside=1;
416 bman->balls[b].dir.z = -bman->balls[b].dir.z;
417 bman->balls[b].loc.z = -20.0f + bman->balls[b].radius;
420 if (bman->balls[b].loc.z + bman->balls[b].radius > 20.0f) { /* z bovengrens */
421 if (bman->balls[b].loc.y > 41+bman->balls[b].radius) bman->balls[b].offside=1;
423 bman->balls[b].dir.z = -bman->balls[b].dir.z;
424 bman->balls[b].loc.z = 20.0f - bman->balls[b].radius;
427 } /* end if !offside */
429 /* check voor stuiteren */
430 for (j=b+1;j<bman->num_balls;j++) {
431 squaredist = (bman->balls[b].radius * bman->balls[b].radius) + (bman->balls[j].radius * bman->balls[j].radius);
432 subvectors(&dvect,&bman->balls[b].loc,&bman->balls[j].loc);
433 if ( squaremagnitude(&dvect) < squaredist ) { /* balls b and j touch */
434 subvectors(&richting,&bman->balls[j].loc,&bman->balls[b].loc);
435 subvectors(&relspeed,&bman->balls[b].dir,&bman->balls[j].dir);
436 /* calc mutual influence direction and magnitude */
437 scalevector(&influence,&richting,(dotproduct(&richting,&relspeed)/squaremagnitude(&richting)));
439 subvectors(&bman->balls[b].dir,&bman->balls[b].dir,&influence);
440 addvectors(&bman->balls[j].dir,&bman->balls[j].dir,&influence);
441 addvectors(&bman->balls[b].loc,&bman->balls[b].loc,&bman->balls[b].dir);
442 addvectors(&bman->balls[j].loc,&bman->balls[j].loc,&bman->balls[j].dir);
444 subvectors(&dvect,&bman->balls[b].loc,&bman->balls[j].loc);
445 while (squaremagnitude(&dvect) < squaredist) {
446 addvectors(&bman->balls[b].loc,&bman->balls[b].loc,&bman->balls[b].dir);
447 addvectors(&bman->balls[j].loc,&bman->balls[j].loc,&bman->balls[j].dir);
448 subvectors(&dvect,&bman->balls[b].loc,&bman->balls[j].loc);
457 * explode ball into triangles
460 void createtrisfromball(triman* tman, vectorf *spherev, GLint *spherei, int ind_num, ball *b) {
465 vectorf avgdir,dvect;
467 tman->scalefac = b->radius;
468 copyvector(&tman->color,&b->color);
469 explosion = 1.0f + tman->explosion * 2.0 * rnd();
471 tman->num_tri = ind_num/3;
473 /* reserveer geheugen voor de poly's in een bal */
475 tman->tris = (tri *)malloc(tman->num_tri * sizeof(tri));
476 tman->vertices = (vectorf *)malloc(ind_num * sizeof(vectorf));
477 tman->normals = (vectorf *)malloc(ind_num/3 * sizeof(vectorf));
479 for (i=0; i<(tman->num_tri); i++) {
480 tman->tris[i].far = FALSE;
482 /* kopieer elke poly apart naar een tri structure */
483 copyvector(&tman->vertices[pos+0],&spherev[spherei[pos+0]]);
484 copyvector(&tman->vertices[pos+1],&spherev[spherei[pos+1]]);
485 copyvector(&tman->vertices[pos+2],&spherev[spherei[pos+2]]);
486 /* Calculate average direction of shrapnel */
487 addvectors(&avgdir,&tman->vertices[pos+0],&tman->vertices[pos+1]);
488 addvectors(&avgdir,&avgdir,&tman->vertices[pos+2]);
489 scalevector(&avgdir,&avgdir,0.33333);
491 /* should normalize first, NYI */
492 copyvector(&tman->normals[i],&avgdir);
494 /* copy de lokatie */
495 addvectors(&tman->tris[i].loc,&b->loc,&avgdir);
496 /* en translate alle triangles terug naar hun eigen oorsprong */
497 tman->vertices[pos+0].x -= avgdir.x;
498 tman->vertices[pos+0].y -= avgdir.y;
499 tman->vertices[pos+0].z -= avgdir.z;
500 tman->vertices[pos+1].x -= avgdir.x;
501 tman->vertices[pos+1].y -= avgdir.y;
502 tman->vertices[pos+1].z -= avgdir.z;
503 tman->vertices[pos+2].x -= avgdir.x;
504 tman->vertices[pos+2].y -= avgdir.y;
505 tman->vertices[pos+2].z -= avgdir.z;
506 /* alwaar opschaling plaatsvindt */
507 scale = b->radius * 2;
508 scalevector(&tman->vertices[pos+0],&tman->vertices[pos+0],scale);
509 scalevector(&tman->vertices[pos+1],&tman->vertices[pos+1],scale);
510 scalevector(&tman->vertices[pos+2],&tman->vertices[pos+2],scale);
512 /* bereken nieuwe richting */
513 scalevector(&tman->tris[i].dir,&avgdir,explosion);
514 dvect.x = 0.1f - 0.2f*rnd();
515 dvect.y = 0.15f - 0.3f*rnd();
516 dvect.z = 0.1f - 0.2f*rnd();
517 addvectors(&tman->tris[i].dir,&tman->tris[i].dir,&dvect);
523 * update position of each tri
526 void updatetris(triman *t) {
530 for (b=0;b<t->num_tri;b++) {
532 t->tris[b].dir.y -= 0.1f;
534 addvectors(&t->tris[b].loc,&t->tris[b].loc,&t->tris[b].dir);
536 if (t->tris[b].far) continue;
537 if (t->tris[b].loc.y < 0) { /* onder bodem ? */
538 if ((t->tris[b].loc.x > -100.0f) &
539 (t->tris[b].loc.x < 100.0f) &
540 (t->tris[b].loc.z > -100.0f) &
541 (t->tris[b].loc.z < 100.0f)) { /* in veld */
542 t->tris[b].dir.y = -(t->tris[b].dir.y);
543 t->tris[b].loc.y = -t->tris[b].loc.y;
544 scalevector(&t->tris[b].dir,&t->tris[b].dir,0.80f); /* dampening */
547 t->tris[b].far = TRUE;
552 if ((t->tris[b].loc.x > -21.0f) &
553 (t->tris[b].loc.x < 21.0f) &
554 (t->tris[b].loc.z > -21.0f) &
555 (t->tris[b].loc.z < 21.0f)) { /* in box? */
557 xd = zd = 999.0f; /* big */
558 if ((t->tris[b].loc.x > -21.0f) &
559 (t->tris[b].loc.x < 0)) {
560 xd = t->tris[b].loc.x + 21.0f;
562 if ((t->tris[b].loc.x < 21.0f) &
563 (t->tris[b].loc.x > 0)) {
564 xd = 21.0f - t->tris[b].loc.x;
566 if ((t->tris[b].loc.z > -21.0f) &
567 (t->tris[b].loc.z < 0)) {
568 zd = t->tris[b].loc.z + 21.0f;
570 if ((t->tris[b].loc.z < 21.0f) &
571 (t->tris[b].loc.z > 0)) {
572 zd = 21.0f - t->tris[b].loc.z;
576 if (t->tris[b].dir.x < 0)
577 t->tris[b].loc.x += (21.0f - t->tris[b].loc.x);
579 t->tris[b].loc.x += (-21.0f - t->tris[b].loc.x);
580 t->tris[b].dir.x = -t->tris[b].dir.x;
583 if (t->tris[b].dir.z < 0)
584 t->tris[b].loc.z += (21.0f - t->tris[b].loc.z);
586 t->tris[b].loc.z += (-21.0f - t->tris[b].loc.z);
587 t->tris[b].dir.z = -t->tris[b].dir.z;
596 * free memory allocated by a tri manager
598 void freetris(triman *t) {
600 if (t->tris) free(t->tris);
601 if (t->vertices) free(t->vertices);
602 if (t->normals) free(t->normals);
612 *load defaults in config structure
614 void setdefaultconfig(boxed_config *config) {
615 config->numballs = NUMBALLS;
616 config->textures = TRUE;
617 config->transparent = FALSE;
618 config->explosion = 25.0f;
619 config->ballsize = BALLSIZE;
620 config->camspeed = 35.0f;
627 static void drawfilledbox(boxedstruct *boxed)
629 /* draws texture filled box,
630 top is drawn using the entire texture,
631 the sides are drawn using the edge of the texture
637 glVertex3f(-1.0,1.0,1.0);
639 glVertex3f(1.0,1.0,1.0);
641 glVertex3f(1.0,-1.0,1.0);
643 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);
652 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);
661 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);
670 glVertex3f(1.0,-1.0,1.0);
672 glTexCoord2f(0.0,0.0);
673 glVertex3f(-1.0,1.0,1.0);
674 glTexCoord2f(0.0,1.0);
675 glVertex3f(-1.0,1.0,-1.0);
676 glTexCoord2f(1.0,1.0);
677 glVertex3f(1.0,1.0,-1.0);
678 glTexCoord2f(1.0,0.0);
679 glVertex3f(1.0,1.0,1.0);
682 glVertex3f(-1.0,-1.0,1.0);
684 glVertex3f(-1.0,-1.0,-1.0);
686 glVertex3f(1.0,-1.0,-1.0);
688 glVertex3f(1.0,-1.0,1.0);
694 * Draw a box made of lines
696 static void drawbox(boxedstruct *boxed)
699 glBegin(GL_LINE_STRIP);
700 glVertex3f(-1.0,1.0,1.0);
701 glVertex3f(-1.0,1.0,-1.0);
702 glVertex3f(1.0,1.0,-1.0);
703 glVertex3f(1.0,1.0,1.0);
704 glVertex3f(-1.0,1.0,1.0);
707 glBegin(GL_LINE_STRIP);
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);
712 glVertex3f(-1.0,-1.0,1.0);
714 /* connect top & bottom */
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);
720 glVertex3f(1.0,1.0,-1.0);
721 glVertex3f(1.0,-1.0,-1.0);
722 glVertex3f(-1.0,1.0,-1.0);
723 glVertex3f(-1.0,-1.0,-1.0);
732 static void drawball(boxedstruct *gp, ball *b)
735 GLint *spherei = gp->spherei;
736 vectorf *spherev = gp->spherev;
741 glTranslatef(b->loc.x,b->loc.y,b->loc.z);
742 glScalef(b->radius,b->radius,b->radius);
743 glColor3f(b->color.x,b->color.y,b->color.z);
747 glMaterialfv(GL_FRONT, GL_DIFFUSE, col);
751 glMaterialfv(GL_FRONT, GL_EMISSION,col);
753 if (!gp->gllists[GLL_BALL]) {
754 glNewList(gp->listobjects + GLL_BALL,GL_COMPILE);
755 cnt = SPHERE_INDICES/3;
756 for (i=0; i<cnt; i++) {
758 glBegin(GL_TRIANGLES);
759 glNormal3f(spherev[spherei[pos+0]].x,spherev[spherei[pos+0]].y,spherev[spherei[pos+0]].z);
760 glVertex3f(spherev[spherei[pos+0]].x,spherev[spherei[pos+0]].y,spherev[spherei[pos+0]].z);
761 glNormal3f(spherev[spherei[pos+1]].x,spherev[spherei[pos+1]].y,spherev[spherei[pos+1]].z);
762 glVertex3f(spherev[spherei[pos+1]].x,spherev[spherei[pos+1]].y,spherev[spherei[pos+1]].z);
763 glNormal3f(spherev[spherei[pos+2]].x,spherev[spherei[pos+2]].y,spherev[spherei[pos+2]].z);
764 glVertex3f(spherev[spherei[pos+2]].x,spherev[spherei[pos+2]].y,spherev[spherei[pos+2]].z);
768 gp->gllists[GLL_BALL] = 1;
770 glCallList(gp->listobjects + GLL_BALL);
778 * Draw all triangles in triman
780 static void drawtriman(triman *t)
783 vectorf *spherev = t->vertices;
787 glColor3f(t->color.x,t->color.y,t->color.z);
791 glMaterialfv(GL_FRONT, GL_DIFFUSE, col);
795 glMaterialfv(GL_FRONT, GL_EMISSION,col);
797 for (i=0; i<t->num_tri; i++) {
800 glTranslatef(t->tris[i].loc.x,t->tris[i].loc.y,t->tris[i].loc.z);
801 glBegin(GL_TRIANGLES);
802 glNormal3f(t->normals[i].x,t->normals[i].y,t->normals[i].z);
803 glVertex3f(spherev[pos+0].x,spherev[pos+0].y,spherev[pos+0].z);
804 glVertex3f(spherev[pos+1].x,spherev[pos+1].y,spherev[pos+1].z);
805 glVertex3f(spherev[pos+2].x,spherev[pos+2].y,spherev[pos+2].z);
815 static void drawpattern(boxedstruct *boxed)
817 if (!boxed->gllists[GLL_PATTERN]) {
818 glNewList(boxed->listobjects + GLL_PATTERN, GL_COMPILE);
820 glBegin(GL_LINE_STRIP);
821 glVertex3f(-25.0f, 0.0f, 35.0f);
822 glVertex3f(-15.0f, 0.0f, 35.0f);
823 glVertex3f(-5.0f, 0.0f, 25.0f);
824 glVertex3f(5.0f, 0.0f, 25.0f);
825 glVertex3f(15.0f, 0.0f, 35.0f);
826 glVertex3f(25.0f, 0.0f, 35.0f);
827 glVertex3f(35.0f, 0.0f, 25.0f);
828 glVertex3f(35.0f, 0.0f, 15.0f);
829 glVertex3f(25.0f, 0.0f, 5.0f);
830 glVertex3f(25.0f, 0.0f, -5.0f);
831 glVertex3f(35.0f, 0.0f, -15.0f);
832 glVertex3f(35.0f, 0.0f, -25.0f);
833 glVertex3f(25.0f, 0.0f, -35.0f);
834 glVertex3f(15.0f, 0.0f,-35.0f);
835 glVertex3f(5.0f, 0.0f, -25.0f);
836 glVertex3f(-5.0f, 0.0f, -25.0f);
837 glVertex3f(-15.0f, 0.0f,-35.0f);
838 glVertex3f(-25.0f, 0.0f,-35.0f);
839 glVertex3f(-35.0f, 0.0f, -25.0f);
840 glVertex3f(-35.0f, 0.0f, -15.0f);
841 glVertex3f(-25.0f, 0.0f, -5.0f);
842 glVertex3f(-25.0f, 0.0f, 5.0f);
843 glVertex3f(-35.0f, 0.0f, 15.0f);
844 glVertex3f(-35.0f, 0.0f, 25.0f);
845 glVertex3f(-25.0f, 0.0f, 35.0f);
848 glBegin(GL_LINE_STRIP);
849 glVertex3f(-5.0f, 0.0f, 15.0f);
850 glVertex3f(5.0f, 0.0f, 15.0f);
851 glVertex3f(15.0f, 0.0f, 5.0f);
852 glVertex3f(15.0f, 0.0f, -5.0f);
853 glVertex3f(5.0f, 0.0f, -15.0f);
854 glVertex3f(-5.0f, 0.0f, -15.0f);
855 glVertex3f(-15.0f, 0.0f, -5.0f);
856 glVertex3f(-15.0f, 0.0f, 5.0f);
857 glVertex3f(-5.0f, 0.0f, 15.0f);
861 boxed->gllists[GLL_PATTERN] = 1;
863 glCallList(boxed->listobjects + GLL_PATTERN);
870 * main rendering loop
872 static void draw(ModeInfo * mi)
874 boxedstruct *gp = &boxed[MI_SCREEN(mi)];
880 GLfloat dgray[4] = {0.3f, 0.3f, 0.3f, 1.0f};
881 GLfloat black[4] = {0.0f, 0.0f, 0.0f, 1.0f};
882 GLfloat lblue[4] = {0.4f,0.6f,1.0f };
884 GLfloat l0_ambient[] = {0.0, 0.0, 0.0, 1.0};
885 GLfloat l0_specular[] = {1.0, 1.0, 1.0, 1.0};
886 GLfloat l0_diffuse[] = {1.0, 1.0, 1.0, 1.0};
887 GLfloat l0_position[] = {0.0, 0.0, 0.0, 1.0}; /* w != 0 -> positional light */
888 GLfloat l1_ambient[] = {0.0, 0.0, 0.0, 1.0};
889 GLfloat l1_specular[] = {1.0, 1.0, 1.0, 1.0};
890 GLfloat l1_diffuse[] = {0.5, 0.5, 0.5, 1.0};
891 GLfloat l1_position[] = {0.0, 1.0, 0.0, 0.0}; /* w = 0 -> directional light */
893 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
898 /* rotate camera around (0,0,0), looking at (0,0,0), up is (0,1,0) */
899 dcam = CAMDISTANCE_MIN + (CAMDISTANCE_MAX - CAMDISTANCE_MIN) + (CAMDISTANCE_MAX - CAMDISTANCE_MIN)*cos(gp->tic/CAMDISTANCE_SPEED);
900 v1.x = dcam * sin(gp->tic/gp->cam_x_speed);
901 v1.z = dcam * cos(gp->tic/gp->cam_z_speed);
902 v1.y = CAM_HEIGHT * sin(gp->tic/gp->cam_y_speed) + 1.02 * CAM_HEIGHT;
903 gluLookAt(v1.x,v1.y,v1.z,0.0,0.0,0.0,0.0,1.0,0.0);
905 glLightfv(GL_LIGHT0, GL_AMBIENT, l0_ambient);
906 glLightfv(GL_LIGHT0, GL_DIFFUSE, l0_diffuse);
907 glLightfv(GL_LIGHT0, GL_SPECULAR, l0_specular);
908 glLightfv(GL_LIGHT0, GL_POSITION, l0_position);
909 glLightfv(GL_LIGHT1, GL_AMBIENT, l1_ambient);
910 glLightfv(GL_LIGHT1, GL_DIFFUSE, l1_diffuse);
911 glLightfv(GL_LIGHT1, GL_SPECULAR, l1_specular);
912 glLightfv(GL_LIGHT1, GL_POSITION, l1_position);
918 glMaterialfv(GL_FRONT, GL_SPECULAR, black);
919 glMaterialfv(GL_FRONT, GL_EMISSION, lblue);
920 glMaterialfv(GL_FRONT,GL_AMBIENT,black);
921 glMaterialf(GL_FRONT, GL_SHININESS, 5.0);
924 /* draw ground grid */
925 /* glDisable(GL_DEPTH_TEST); */
926 glDisable(GL_LIGHTING);
928 glColor3f(0.1,0.1,0.6);
929 for (dx= -2; dx<3; dx++) {
930 for (dz= -2; dz<3; dz++) {
932 glTranslatef(dx*30.0f, 0.0f, dz*30.0f);
938 /* Set drawing mode for the boxes */
939 glEnable(GL_DEPTH_TEST);
940 glEnable(GL_TEXTURE_2D);
942 glColor3f(1.0,1.0,1.0);
943 glScalef(20.0,0.25,20.0);
944 glTranslatef(0.0,2.0,0.0);
947 glDisable(GL_TEXTURE_2D);
950 glColor3f(0.2,0.5,0.2);
951 glScalef(20.0,20.0,0.25);
952 glTranslatef(0.0,1.0,81.0);
957 glColor3f(0.2,0.5,0.2);
958 glScalef(20.0,20.0,0.25);
959 glTranslatef(0.0,1.0,-81.0);
964 glColor3f(0.2,0.5,0.2);
965 glScalef(.25,20.0,20.0);
966 glTranslatef(-81.0,1.0,0.0);
971 glColor3f(0.2,0.5,0.2);
972 glScalef(.25,20.0,20.0);
973 glTranslatef(81.0,1.0,0.0);
977 glEnable(GL_LIGHTING);
979 glMaterialfv(GL_FRONT, GL_DIFFUSE, dgray);
980 glMaterialfv(GL_FRONT, GL_EMISSION, black); /* turn it off before painting the balls */
982 /* move the balls and shrapnel */
983 updateballs(&gp->bman);
986 for (i=0;i<gp->bman.num_balls;i++) {
987 if (gp->bman.balls[i].justcreated) {
988 gp->bman.balls[i].justcreated = FALSE;
989 freetris(&gp->tman[i]);
991 if (gp->bman.balls[i].bounced) {
992 if (gp->tman[i].vertices == NULL) {
993 createtrisfromball(&gp->tman[i],gp->spherev,gp->spherei,SPHERE_INDICES,&gp->bman.balls[i]);
995 updatetris(&gp->tman[i]);
997 glDisable(GL_CULL_FACE);
998 drawtriman(&gp->tman[i]);
999 glEnable(GL_CULL_FACE);
1001 drawball(gp, &gp->bman.balls[i]);
1011 * new window size or exposure
1013 void reshape_boxed(ModeInfo *mi, int width, int height)
1015 GLfloat h = (GLfloat) height / (GLfloat) width;
1017 glViewport(0, 0, (GLint) width, (GLint) height);
1018 glMatrixMode(GL_PROJECTION);
1020 gluPerspective(50.0,1/h,2.0,1000.0);
1021 glMatrixMode (GL_MODELVIEW);
1029 pinit(ModeInfo * mi)
1031 boxedstruct *gp = &boxed[MI_SCREEN(mi)];
1035 char *texpixeltarget;
1037 glShadeModel(GL_SMOOTH);
1039 glClearColor(0.0,0.05,0.1,0.0);
1040 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1042 /* Load configuration */
1043 setdefaultconfig(&gp->config);
1047 bman->balls = (ball *)malloc(gp->config.numballs * sizeof(ball));
1048 bman->num_balls = gp->config.numballs;
1049 bman->ballsize = gp->config.ballsize;
1050 bman->explosion = gp->config.explosion;
1052 gp->tman = (triman *)malloc(bman->num_balls * sizeof(triman));
1053 memset(gp->tman,0,bman->num_balls * sizeof(triman));
1055 for(i=0;i<bman->num_balls;i++) {
1056 gp->tman[i].explosion = (float) (((int)gp->config.explosion) / 15.0f );
1057 gp->tman[i].vertices = NULL;
1058 gp->tman[i].normals = NULL;
1059 gp->tman[i].tris = NULL;
1060 createball(&bman->balls[i]);
1061 bman->balls[i].loc.y *= rnd();
1066 glEnable(GL_CULL_FACE);
1067 glEnable(GL_LIGHTING);
1069 /* define cam path */
1070 gp->cam_x_speed = 1.0f/((float)gp->config.camspeed/50.0 + rnd()*((float)gp->config.camspeed/50.0));
1071 gp->cam_z_speed = 1.0f/((float)gp->config.camspeed/50.0 + rnd()*((float)gp->config.camspeed/50.0));
1072 gp->cam_y_speed = 1.0f/((float)gp->config.camspeed/250.0 + rnd()*((float)gp->config.camspeed/250.0));
1073 if (rnd() < 0.5f) gp->cam_x_speed = -gp->cam_x_speed;
1074 if (rnd() < 0.5f) gp->cam_z_speed = -gp->cam_z_speed;
1077 gp->tex1 = (char *)malloc(3*width*height*sizeof(GLuint));
1078 texpixels = 256*256; /*width*height;*/
1079 texpixeldata = header_data;
1080 texpixeltarget = gp->tex1;
1081 for (i=0; i < texpixels; i++) {
1082 HEADER_PIXEL(texpixeldata,texpixeltarget);
1083 texpixeltarget += 3;
1087 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
1090 i = gluBuild2DMipmaps(GL_TEXTURE_2D, 3, 256, 256,
1091 GL_RGB, GL_UNSIGNED_BYTE, gp->tex1);
1094 const char *s = (char *) gluErrorString (i);
1095 fprintf (stderr, "%s: error mipmapping texture: %s\n",
1096 progname, (s ? s : "(unknown)"));
1099 check_gl_error("mipmapping");
1101 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1102 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
1103 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
1104 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1105 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1112 init_boxed(ModeInfo * mi)
1114 int screen = MI_SCREEN(mi);
1116 /* Colormap cmap; */
1117 /* Boolean rgba, doublebuffer, cmap_installed; */
1120 if (boxed == NULL) {
1121 if ((boxed = (boxedstruct *) calloc(MI_NUM_SCREENS(mi),sizeof (boxedstruct))) == NULL) return;
1123 gp = &boxed[screen];
1124 gp->window = MI_WINDOW(mi);
1126 if ((gp->glx_context = init_GL(mi)) != NULL) {
1127 reshape_boxed(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
1128 glDrawBuffer(GL_BACK);
1129 if (!glIsList(gp->listobjects)) {
1130 gp->listobjects = glGenLists(3);
1143 draw_boxed(ModeInfo * mi)
1145 boxedstruct *gp = &boxed[MI_SCREEN(mi)];
1146 Display *display = MI_DISPLAY(mi);
1147 Window window = MI_WINDOW(mi);
1149 if (!gp->glx_context)
1152 glDrawBuffer(GL_BACK);
1154 glXMakeCurrent(display, window, *(gp->glx_context));
1157 if (mi->fps_p) do_fps (mi);
1159 glXSwapBuffers(display, window);
1163 release_boxed(ModeInfo * mi)
1167 if (boxed != NULL) {
1170 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
1171 boxedstruct *gp = &boxed[screen];
1173 if (gp->glx_context) {
1174 /* Display lists MUST be freed while their glXContext is current. */
1175 glXMakeCurrent(MI_DISPLAY(mi), gp->window, *(gp->glx_context));
1177 if (glIsList(gp->listobjects))
1178 glDeleteLists(gp->listobjects, 3);
1180 for (i=0;i<gp->bman.num_balls;i++) {
1181 if (gp->bman.balls[i].bounced) freetris(&gp->tman[i]);
1183 free (gp->bman.balls);
1190 (void) free((void *) boxed);
1197 /*********************************************************/