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
31 #include <X11/Intrinsic.h>
35 **----------------------------------------------------------------------------
37 **----------------------------------------------------------------------------
41 # define PROGCLASS "boxed"
42 # define HACK_INIT init_boxed
43 # define HACK_DRAW draw_boxed
44 # define HACK_RESHAPE reshape_boxed
45 # define boxed_opts xlockmore_opts
47 # define DEF_SPEED "0.5"
48 # define DEF_BALLS "25"
49 # define DEF_BALLSIZE "2.0"
50 # define DEF_EXPLOSION "25.0f"
51 # define DEFAULTS "*delay: 20000 \n" \
52 "*showFPS: False \n" \
53 "*wireframe: False \n"
55 # include "xlockmore.h" /* from the xscreensaver distribution */
56 #else /* !STANDALONE */
57 # include "xlock.h" /* from the xlockmore distribution */
58 #endif /* !STANDALONE */
64 #define countof(x) (int)(sizeof((x))/sizeof((*x)))
66 #define rnd() (frand(1.0))
68 GLfloat speed; /* jwz -- overall speed factor applied to all motion */
71 GLfloat cfg_explosion;
74 static XrmOptionDescRec opts[] = {
75 {"-speed", ".boxed.speed", XrmoptionSepArg, 0},
76 {"-balls", ".boxed.balls", XrmoptionSepArg, 0},
77 {"-ballsize", ".boxed.ballsize", XrmoptionSepArg, 0},
78 {"-explosion", ".boxed.explosion", XrmoptionSepArg, 0},
81 static argtype vars[] = {
82 {&speed, "speed", "Speed", DEF_SPEED, t_Float},
83 {&cfg_balls, "balls", "Balls", DEF_BALLS, t_Int},
84 {&cfg_ballsize, "ballsize", "Ball Size", DEF_BALLSIZE, t_Float},
85 {&cfg_explosion, "explosion", "Exlosion", DEF_BALLSIZE, t_Float},
88 ModeSpecOpt boxed_opts = {countof(opts), opts, countof(vars), vars, NULL};
92 ModStruct boxed_description = {
93 "boxed", "init_boxed", "draw_boxed", "release_boxed",
94 "draw_boxed", "init_boxed", NULL, &boxed_opts,
95 1000, 1, 2, 1, 4, 1.0, "",
96 "Shows GL's boxed balls", 0, NULL};
105 #define CAM_HEIGHT 100.0f
106 #define CAMDISTANCE_MIN 20.0
107 #define CAMDISTANCE_MAX 150.0
108 #define CAMDISTANCE_SPEED 1.5
110 /* rendering the sphere */
112 #define SPHERE_VERTICES (2+MESH_SIZE*MESH_SIZE*2)
113 #define SPHERE_INDICES ((MESH_SIZE*4 + MESH_SIZE*4*(MESH_SIZE-1))*3)
116 **-----------------------------------------------------------------------------
118 **-----------------------------------------------------------------------------
173 float cam_x_speed, cam_z_speed, cam_y_speed;
177 vectorf spherev[SPHERE_VERTICES];
178 GLint spherei[SPHERE_INDICES];
181 GLXContext *glx_context;
189 #define GLL_PATTERN 0
194 **----------------------------------------------------------------------------
196 **----------------------------------------------------------------------------
199 static boxedstruct *boxed = NULL;
203 **----------------------------------------------------------------------------
205 **----------------------------------------------------------------------------
211 static inline void addvectors(vectorf *dest, vectorf *s1, vectorf *s2)
213 dest->x = s1->x + s2->x;
214 dest->y = s1->y + s2->y;
215 dest->z = s1->z + s2->z;
221 static inline void subvectors(vectorf *dest, vectorf* s1, vectorf *s2)
223 dest->x = s1->x - s2->x;
224 dest->y = s1->y - s2->y;
225 dest->z = s1->z - s2->z;
229 * Multiply vector with scalar (scale vector)
231 static inline void scalevector(vectorf *dest, vectorf *source, GLfloat sc)
233 dest->x = source->x * sc;
234 dest->y = source->y * sc;
235 dest->z = source->z * sc;
241 static inline void copyvector(vectorf *dest, vectorf* source)
249 static inline GLfloat
250 dotproduct(vectorf * v1, vectorf * v2)
252 return v1->x * v2->x + v1->y * v2->y + v1->z * v2->z;
255 static inline GLfloat
256 squaremagnitude(vectorf * v)
258 return v->x * v->x + v->y * v->y + v->z * v->z;
264 * Generate the Sphere data
269 static void generatesphere(void)
271 float dj = M_PI/(MESH_SIZE+1.0f);
272 float di = M_PI/MESH_SIZE;
273 int v; /* vertex offset */
274 int ind; /* indices offset */
276 GLfloat r_y_plane, h_y_plane;
281 * generate the sphere data
282 * vertices 0 and 1 are the north and south poles
285 spherei = boxed->spherei;
286 spherev = boxed->spherev;
288 spherev[0].x = 0.0f; spherev[0].y =1.0f; spherev[0].z = 0.0f;
289 spherev[1].x = 0.0f; spherev[1].y =-1.0f; spherev[1].z = 0.0f;
291 for (j=0; j<MESH_SIZE; j++) {
292 r_y_plane = (float)sin((j+1) * dj);
293 h_y_plane = (float)cos((j+1) * dj);
294 for (i=0; i<MESH_SIZE*2; i++) {
295 si = 2+i+j*MESH_SIZE*2;
296 spherev[si].y = h_y_plane;
297 spherev[si].x = (float) sin(i * di) * r_y_plane;
298 spherev[si].z = (float) cos(i * di) * r_y_plane;
302 /* generate indices */
303 for (i=0; i<MESH_SIZE*2; i++) {
305 spherei[3*i+1] = i+2;
306 spherei[3*i+2] = i+3;
307 if (i==MESH_SIZE*2-1)
311 /* the middle strips */
312 for (j=0; j<MESH_SIZE-1; j++) {
314 ind = 3*MESH_SIZE*2 + j*6*MESH_SIZE*2;
315 for (i=0; i<MESH_SIZE*2; i++) {
316 spherei[6*i+ind] = v+i;
317 spherei[6*i+2+ind] = v+i+1;
318 spherei[6*i+1+ind] = v+i+MESH_SIZE*2;
320 spherei[6*i+ind+3] = v+i+MESH_SIZE*2;
321 spherei[6*i+2+ind+3] = v+i+1;
322 spherei[6*i+1+ind+3] = v+i+MESH_SIZE*2+1;
323 if (i==MESH_SIZE*2-1) {
324 spherei[6*i+2+ind] = v+i+1-2*MESH_SIZE;
325 spherei[6*i+2+ind+3] = v+i+1-2*MESH_SIZE;
326 spherei[6*i+1+ind+3] = v+i+MESH_SIZE*2+1-2*MESH_SIZE;
331 v = SPHERE_VERTICES-MESH_SIZE*2;
332 ind = SPHERE_INDICES-3*MESH_SIZE*2;
333 for (i=0; i<MESH_SIZE*2; i++) {
334 spherei[3*i+ind] = 1;
335 spherei[3*i+1+ind] = v+i+1;
336 spherei[3*i+2+ind] = v+i;
337 if (i==MESH_SIZE*2-1)
338 spherei[3*i+1+ind] = v;
349 void createball(ball *newball)
351 float r=0.0f,g=0.0f,b=0.0f;
352 newball->loc.x = 5-10*rnd();
353 newball->loc.y = 35+20*rnd();
354 newball->loc.z = 5-10*rnd();
355 newball->dir.x = (0.5f-rnd()) * speed;
356 newball->dir.y = 0.0;
357 newball->dir.z = (0.5-rnd()) * speed;
358 newball->offside = 0;
359 newball->bounced = FALSE;
360 newball->radius = cfg_ballsize;
361 while (r+g+b < 1.8f ) {
362 newball->color.x = r=rnd();
363 newball->color.y = g=rnd();
364 newball->color.z = b=rnd();
366 newball->justcreated = TRUE;
369 /* Update position of each ball */
371 void updateballs(ballman *bman)
374 vectorf dvect,richting,relspeed,influence;
377 for (b=0;b<bman->num_balls;b++) {
379 GLfloat gravity = 0.30f * speed;
382 bman->balls[b].dir.y -= gravity;
384 addvectors(&bman->balls[b].loc,&bman->balls[b].loc,&bman->balls[b].dir);
386 if (bman->balls[b].loc.y < bman->balls[b].radius) { /* ball onder bodem? (bodem @ y=0) */
387 if ((bman->balls[b].loc.x < -95.0) ||
388 (bman->balls[b].loc.x > 95.0) ||
389 (bman->balls[b].loc.z < -95.0) ||
390 (bman->balls[b].loc.z > 95.0)) {
391 if (bman->balls[b].loc.y < -1000.0)
392 createball(&bman->balls[b]);
394 bman->balls[b].loc.y = bman->balls[b].radius + (bman->balls[b].radius - bman->balls[b].loc.y);
395 bman->balls[b].dir.y = -bman->balls[b].dir.y;
396 if (bman->balls[b].offside) {
397 bman->balls[b].bounced = TRUE; /* temporary disable painting ball */
398 scalevector(&bman->balls[b].dir,&bman->balls[b].dir,0.80f);
399 if (squaremagnitude(&bman->balls[b].dir) < 0.08f) {
400 createball(&bman->balls[b]);
406 if (!bman->balls[b].offside) {
407 if (bman->balls[b].loc.x - bman->balls[b].radius < -20.0f) { /* x ondergrens */
408 if (bman->balls[b].loc.y > 41+bman->balls[b].radius) bman->balls[b].offside=1;
410 bman->balls[b].dir.x = -bman->balls[b].dir.x;
411 bman->balls[b].loc.x = -20.0f + bman->balls[b].radius;
414 if (bman->balls[b].loc.x + bman->balls[b].radius > 20.0f) { /* x bovengrens */
415 if (bman->balls[b].loc.y > 41+bman->balls[b].radius) bman->balls[b].offside=1;
417 bman->balls[b].dir.x = -bman->balls[b].dir.x;
418 bman->balls[b].loc.x = 20.0f - bman->balls[b].radius;
421 if (bman->balls[b].loc.z - bman->balls[b].radius < -20.0f) { /* z ondergrens */
422 if (bman->balls[b].loc.y > 41+bman->balls[b].radius) bman->balls[b].offside=1;
424 bman->balls[b].dir.z = -bman->balls[b].dir.z;
425 bman->balls[b].loc.z = -20.0f + bman->balls[b].radius;
428 if (bman->balls[b].loc.z + bman->balls[b].radius > 20.0f) { /* z bovengrens */
429 if (bman->balls[b].loc.y > 41+bman->balls[b].radius) bman->balls[b].offside=1;
431 bman->balls[b].dir.z = -bman->balls[b].dir.z;
432 bman->balls[b].loc.z = 20.0f - bman->balls[b].radius;
435 } /* end if !offside */
437 /* check voor stuiteren */
438 for (j=b+1;j<bman->num_balls;j++) {
439 squaredist = (bman->balls[b].radius * bman->balls[b].radius) + (bman->balls[j].radius * bman->balls[j].radius);
440 subvectors(&dvect,&bman->balls[b].loc,&bman->balls[j].loc);
441 if ( squaremagnitude(&dvect) < squaredist ) { /* balls b and j touch */
442 subvectors(&richting,&bman->balls[j].loc,&bman->balls[b].loc);
443 subvectors(&relspeed,&bman->balls[b].dir,&bman->balls[j].dir);
444 /* calc mutual influence direction and magnitude */
445 scalevector(&influence,&richting,(dotproduct(&richting,&relspeed)/squaremagnitude(&richting)));
447 subvectors(&bman->balls[b].dir,&bman->balls[b].dir,&influence);
448 addvectors(&bman->balls[j].dir,&bman->balls[j].dir,&influence);
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);
452 subvectors(&dvect,&bman->balls[b].loc,&bman->balls[j].loc);
453 while (squaremagnitude(&dvect) < squaredist) {
454 addvectors(&bman->balls[b].loc,&bman->balls[b].loc,&bman->balls[b].dir);
455 addvectors(&bman->balls[j].loc,&bman->balls[j].loc,&bman->balls[j].dir);
456 subvectors(&dvect,&bman->balls[b].loc,&bman->balls[j].loc);
465 * explode ball into triangles
468 void createtrisfromball(triman* tman, vectorf *spherev, GLint *spherei, int ind_num, ball *b)
474 vectorf avgdir,dvect;
476 tman->scalefac = b->radius;
477 copyvector(&tman->color,&b->color);
478 explosion = 1.0f + tman->explosion * 2.0 * rnd();
480 tman->num_tri = ind_num/3;
482 /* reserveer geheugen voor de poly's in een bal */
484 tman->tris = (tri *)malloc(tman->num_tri * sizeof(tri));
485 tman->vertices = (vectorf *)malloc(ind_num * sizeof(vectorf));
486 tman->normals = (vectorf *)malloc(ind_num/3 * sizeof(vectorf));
488 for (i=0; i<(tman->num_tri); i++) {
489 tman->tris[i].far = FALSE;
491 /* kopieer elke poly apart naar een tri structure */
492 copyvector(&tman->vertices[pos+0],&spherev[spherei[pos+0]]);
493 copyvector(&tman->vertices[pos+1],&spherev[spherei[pos+1]]);
494 copyvector(&tman->vertices[pos+2],&spherev[spherei[pos+2]]);
495 /* Calculate average direction of shrapnel */
496 addvectors(&avgdir,&tman->vertices[pos+0],&tman->vertices[pos+1]);
497 addvectors(&avgdir,&avgdir,&tman->vertices[pos+2]);
498 scalevector(&avgdir,&avgdir,0.33333);
500 /* should normalize first, NYI */
501 copyvector(&tman->normals[i],&avgdir);
503 /* copy de lokatie */
504 addvectors(&tman->tris[i].loc,&b->loc,&avgdir);
505 /* en translate alle triangles terug naar hun eigen oorsprong */
506 tman->vertices[pos+0].x -= avgdir.x;
507 tman->vertices[pos+0].y -= avgdir.y;
508 tman->vertices[pos+0].z -= avgdir.z;
509 tman->vertices[pos+1].x -= avgdir.x;
510 tman->vertices[pos+1].y -= avgdir.y;
511 tman->vertices[pos+1].z -= avgdir.z;
512 tman->vertices[pos+2].x -= avgdir.x;
513 tman->vertices[pos+2].y -= avgdir.y;
514 tman->vertices[pos+2].z -= avgdir.z;
515 /* alwaar opschaling plaatsvindt */
516 scale = b->radius * 2;
517 scalevector(&tman->vertices[pos+0],&tman->vertices[pos+0],scale);
518 scalevector(&tman->vertices[pos+1],&tman->vertices[pos+1],scale);
519 scalevector(&tman->vertices[pos+2],&tman->vertices[pos+2],scale);
521 tman->vertices[pos+0].x += avgdir.x;
522 tman->vertices[pos+0].y += avgdir.y;
523 tman->vertices[pos+0].z += avgdir.z;
524 tman->vertices[pos+1].x += avgdir.x;
525 tman->vertices[pos+1].y += avgdir.y;
526 tman->vertices[pos+1].z += avgdir.z;
527 tman->vertices[pos+2].x += avgdir.x;
528 tman->vertices[pos+2].y += avgdir.y;
529 tman->vertices[pos+2].z += avgdir.z;
531 /* bereken nieuwe richting */
532 scalevector(&tman->tris[i].dir,&avgdir,explosion);
533 dvect.x = (0.1f - 0.2f*rnd());
534 dvect.y = (0.15f - 0.3f*rnd());
535 dvect.z = (0.1f - 0.2f*rnd());
536 addvectors(&tman->tris[i].dir,&tman->tris[i].dir,&dvect);
542 * update position of each tri
545 void updatetris(triman *t)
550 for (b=0;b<t->num_tri;b++) {
552 t->tris[b].dir.y -= (0.1f * speed);
554 addvectors(&t->tris[b].loc,&t->tris[b].loc,&t->tris[b].dir);
556 if (t->tris[b].far) continue;
557 if (t->tris[b].loc.y < 0) { /* onder bodem ? */
558 if ((t->tris[b].loc.x > -95.0f) &
559 (t->tris[b].loc.x < 95.0f) &
560 (t->tris[b].loc.z > -95.0f) &
561 (t->tris[b].loc.z < 95.0f)) { /* in veld */
562 t->tris[b].dir.y = -(t->tris[b].dir.y);
563 t->tris[b].loc.y = -t->tris[b].loc.y;
564 scalevector(&t->tris[b].dir,&t->tris[b].dir,0.80f); /* dampening */
567 t->tris[b].far = TRUE;
572 if ((t->tris[b].loc.x > -21.0f) &
573 (t->tris[b].loc.x < 21.0f) &
574 (t->tris[b].loc.z > -21.0f) &
575 (t->tris[b].loc.z < 21.0f)) { /* in box? */
577 xd = zd = 999.0f; /* big */
578 if ((t->tris[b].loc.x > -21.0f) &
579 (t->tris[b].loc.x < 0)) {
580 xd = t->tris[b].loc.x + 21.0f;
582 if ((t->tris[b].loc.x < 21.0f) &
583 (t->tris[b].loc.x > 0)) {
584 xd = 21.0f - t->tris[b].loc.x;
586 if ((t->tris[b].loc.z > -21.0f) &
587 (t->tris[b].loc.z < 0)) {
588 zd = t->tris[b].loc.z + 21.0f;
590 if ((t->tris[b].loc.z < 21.0f) &
591 (t->tris[b].loc.z > 0)) {
592 zd = 21.0f - t->tris[b].loc.z;
596 if (t->tris[b].dir.x < 0)
597 t->tris[b].loc.x += (21.0f - t->tris[b].loc.x);
599 t->tris[b].loc.x += (-21.0f - t->tris[b].loc.x);
600 t->tris[b].dir.x = -t->tris[b].dir.x;
603 if (t->tris[b].dir.z < 0)
604 t->tris[b].loc.z += (21.0f - t->tris[b].loc.z);
606 t->tris[b].loc.z += (-21.0f - t->tris[b].loc.z);
607 t->tris[b].dir.z = -t->tris[b].dir.z;
616 * free memory allocated by a tri manager
618 void freetris(triman *t)
621 if (t->tris) free(t->tris);
622 if (t->vertices) free(t->vertices);
623 if (t->normals) free(t->normals);
633 *load defaults in config structure
635 void setdefaultconfig(boxed_config *config)
637 cfg_balls = MAX(3,MIN(40,cfg_balls));
638 cfg_ballsize = MAX(1.0f,MIN(5.0f,cfg_ballsize));
639 cfg_explosion = MAX(0.0f,MIN(50.0f,cfg_explosion));
640 config->numballs = cfg_balls;
641 config->textures = TRUE;
642 config->transparent = FALSE;
643 config->explosion = cfg_explosion;
644 config->ballsize = cfg_ballsize;
645 config->camspeed = 35.0f;
652 static void drawfilledbox(boxedstruct *boxed, int wire)
654 /* draws texture filled box,
655 top is drawn using the entire texture,
656 the sides are drawn using the edge of the texture
660 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
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);
680 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);
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);
697 glTexCoord2f(0.0,0.0);
698 glVertex3f(-1.0,1.0,1.0);
699 glTexCoord2f(0.0,1.0);
700 glVertex3f(-1.0,1.0,-1.0);
701 glTexCoord2f(1.0,1.0);
702 glVertex3f(1.0,1.0,-1.0);
703 glTexCoord2f(1.0,0.0);
704 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);
713 glVertex3f(1.0,-1.0,1.0);
719 * Draw a box made of lines
721 static void drawbox(boxedstruct *boxed)
724 glBegin(GL_LINE_STRIP);
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);
732 glBegin(GL_LINE_STRIP);
733 glVertex3f(-1.0,-1.0,1.0);
734 glVertex3f(1.0,-1.0,1.0);
735 glVertex3f(1.0,-1.0,-1.0);
736 glVertex3f(-1.0,-1.0,-1.0);
737 glVertex3f(-1.0,-1.0,1.0);
739 /* connect top & bottom */
741 glVertex3f(-1.0,1.0,1.0);
742 glVertex3f(-1.0,-1.0,1.0);
743 glVertex3f(1.0,1.0,1.0);
744 glVertex3f(1.0,-1.0,1.0);
745 glVertex3f(1.0,1.0,-1.0);
746 glVertex3f(1.0,-1.0,-1.0);
747 glVertex3f(-1.0,1.0,-1.0);
748 glVertex3f(-1.0,-1.0,-1.0);
757 static void drawball(boxedstruct *gp, ball *b, int wire)
760 GLint *spherei = gp->spherei;
761 vectorf *spherev = gp->spherev;
766 glTranslatef(b->loc.x,b->loc.y,b->loc.z);
767 glScalef(b->radius,b->radius,b->radius);
768 glColor3f(b->color.x,b->color.y,b->color.z);
772 glMaterialfv(GL_FRONT, GL_DIFFUSE, col);
776 glMaterialfv(GL_FRONT, GL_EMISSION,col);
778 if (!gp->gllists[GLL_BALL]) {
779 glNewList(gp->listobjects + GLL_BALL,GL_COMPILE);
780 cnt = SPHERE_INDICES/3;
781 for (i=0; i<cnt; i++) {
783 glBegin(wire ? GL_LINE_LOOP : GL_TRIANGLES);
784 glNormal3f(spherev[spherei[pos+0]].x,spherev[spherei[pos+0]].y,spherev[spherei[pos+0]].z);
785 glVertex3f(spherev[spherei[pos+0]].x,spherev[spherei[pos+0]].y,spherev[spherei[pos+0]].z);
786 glNormal3f(spherev[spherei[pos+1]].x,spherev[spherei[pos+1]].y,spherev[spherei[pos+1]].z);
787 glVertex3f(spherev[spherei[pos+1]].x,spherev[spherei[pos+1]].y,spherev[spherei[pos+1]].z);
788 glNormal3f(spherev[spherei[pos+2]].x,spherev[spherei[pos+2]].y,spherev[spherei[pos+2]].z);
789 glVertex3f(spherev[spherei[pos+2]].x,spherev[spherei[pos+2]].y,spherev[spherei[pos+2]].z);
793 gp->gllists[GLL_BALL] = 1;
795 glCallList(gp->listobjects + GLL_BALL);
803 * Draw all triangles in triman
805 static void drawtriman(triman *t, int wire)
808 vectorf *spherev = t->vertices;
812 glColor3f(t->color.x,t->color.y,t->color.z);
816 glMaterialfv(GL_FRONT, GL_DIFFUSE, col);
820 glMaterialfv(GL_FRONT, GL_EMISSION,col);
822 for (i=0; i<t->num_tri; i++) {
825 glTranslatef(t->tris[i].loc.x,t->tris[i].loc.y,t->tris[i].loc.z);
826 glBegin(wire ? GL_LINE_LOOP : GL_TRIANGLES);
827 glNormal3f(t->normals[i].x,t->normals[i].y,t->normals[i].z);
828 glVertex3f(spherev[pos+0].x,spherev[pos+0].y,spherev[pos+0].z);
829 glVertex3f(spherev[pos+1].x,spherev[pos+1].y,spherev[pos+1].z);
830 glVertex3f(spherev[pos+2].x,spherev[pos+2].y,spherev[pos+2].z);
840 static void drawpattern(boxedstruct *boxed)
842 if (!boxed->gllists[GLL_PATTERN]) {
843 glNewList(boxed->listobjects + GLL_PATTERN, GL_COMPILE);
845 glBegin(GL_LINE_STRIP);
846 glVertex3f(-25.0f, 0.0f, 35.0f);
847 glVertex3f(-15.0f, 0.0f, 35.0f);
848 glVertex3f(-5.0f, 0.0f, 25.0f);
849 glVertex3f(5.0f, 0.0f, 25.0f);
850 glVertex3f(15.0f, 0.0f, 35.0f);
851 glVertex3f(25.0f, 0.0f, 35.0f);
852 glVertex3f(35.0f, 0.0f, 25.0f);
853 glVertex3f(35.0f, 0.0f, 15.0f);
854 glVertex3f(25.0f, 0.0f, 5.0f);
855 glVertex3f(25.0f, 0.0f, -5.0f);
856 glVertex3f(35.0f, 0.0f, -15.0f);
857 glVertex3f(35.0f, 0.0f, -25.0f);
858 glVertex3f(25.0f, 0.0f, -35.0f);
859 glVertex3f(15.0f, 0.0f,-35.0f);
860 glVertex3f(5.0f, 0.0f, -25.0f);
861 glVertex3f(-5.0f, 0.0f, -25.0f);
862 glVertex3f(-15.0f, 0.0f,-35.0f);
863 glVertex3f(-25.0f, 0.0f,-35.0f);
864 glVertex3f(-35.0f, 0.0f, -25.0f);
865 glVertex3f(-35.0f, 0.0f, -15.0f);
866 glVertex3f(-25.0f, 0.0f, -5.0f);
867 glVertex3f(-25.0f, 0.0f, 5.0f);
868 glVertex3f(-35.0f, 0.0f, 15.0f);
869 glVertex3f(-35.0f, 0.0f, 25.0f);
870 glVertex3f(-25.0f, 0.0f, 35.0f);
873 glBegin(GL_LINE_STRIP);
874 glVertex3f(-5.0f, 0.0f, 15.0f);
875 glVertex3f(5.0f, 0.0f, 15.0f);
876 glVertex3f(15.0f, 0.0f, 5.0f);
877 glVertex3f(15.0f, 0.0f, -5.0f);
878 glVertex3f(5.0f, 0.0f, -15.0f);
879 glVertex3f(-5.0f, 0.0f, -15.0f);
880 glVertex3f(-15.0f, 0.0f, -5.0f);
881 glVertex3f(-15.0f, 0.0f, 5.0f);
882 glVertex3f(-5.0f, 0.0f, 15.0f);
886 boxed->gllists[GLL_PATTERN] = 1;
888 glCallList(boxed->listobjects + GLL_PATTERN);
895 * main rendering loop
897 static void draw(ModeInfo * mi)
899 boxedstruct *gp = &boxed[MI_SCREEN(mi)];
900 int wire = MI_IS_WIREFRAME (mi);
906 GLfloat dgray[4] = {0.3f, 0.3f, 0.3f, 1.0f};
907 GLfloat black[4] = {0.0f, 0.0f, 0.0f, 1.0f};
908 GLfloat lblue[4] = {0.4f,0.6f,1.0f };
910 GLfloat l0_ambient[] = {0.0, 0.0, 0.0, 1.0};
911 GLfloat l0_specular[] = {1.0, 1.0, 1.0, 1.0};
912 GLfloat l0_diffuse[] = {1.0, 1.0, 1.0, 1.0};
913 GLfloat l0_position[] = {0.0, 0.0, 0.0, 1.0}; /* w != 0 -> positional light */
914 GLfloat l1_ambient[] = {0.0, 0.0, 0.0, 1.0};
915 GLfloat l1_specular[] = {1.0, 1.0, 1.0, 1.0};
916 GLfloat l1_diffuse[] = {0.5, 0.5, 0.5, 1.0};
917 GLfloat l1_position[] = {0.0, 1.0, 0.0, 0.0}; /* w = 0 -> directional light */
919 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
923 gp->camtic += 0.01f + 0.01f * sin(gp->tic * speed);
925 /* rotate camera around (0,0,0), looking at (0,0,0), up is (0,1,0) */
926 dcam = CAMDISTANCE_MIN + (CAMDISTANCE_MAX - CAMDISTANCE_MIN) + (CAMDISTANCE_MAX - CAMDISTANCE_MIN)*cos((gp->camtic/CAMDISTANCE_SPEED) * speed);
927 v1.x = dcam * sin((gp->camtic/gp->cam_x_speed) * speed);
928 v1.z = dcam * cos((gp->camtic/gp->cam_z_speed) * speed);
929 v1.y = CAM_HEIGHT * sin((gp->camtic/gp->cam_y_speed) * speed) + 1.02 * CAM_HEIGHT;
930 gluLookAt(v1.x,v1.y,v1.z,0.0,0.0,0.0,0.0,1.0,0.0);
933 glLightfv(GL_LIGHT0, GL_AMBIENT, l0_ambient);
934 glLightfv(GL_LIGHT0, GL_DIFFUSE, l0_diffuse);
935 glLightfv(GL_LIGHT0, GL_SPECULAR, l0_specular);
936 glLightfv(GL_LIGHT0, GL_POSITION, l0_position);
937 glLightfv(GL_LIGHT1, GL_AMBIENT, l1_ambient);
938 glLightfv(GL_LIGHT1, GL_DIFFUSE, l1_diffuse);
939 glLightfv(GL_LIGHT1, GL_SPECULAR, l1_specular);
940 glLightfv(GL_LIGHT1, GL_POSITION, l1_position);
946 glMaterialfv(GL_FRONT, GL_SPECULAR, black);
947 glMaterialfv(GL_FRONT, GL_EMISSION, lblue);
948 glMaterialfv(GL_FRONT,GL_AMBIENT,black);
949 glMaterialf(GL_FRONT, GL_SHININESS, 5.0);
953 /* draw ground grid */
954 /* glDisable(GL_DEPTH_TEST); */
955 glDisable(GL_LIGHTING);
957 glColor3f(0.1,0.1,0.6);
958 for (dx= -2; dx<3; dx++) {
959 for (dz= -2; dz<3; dz++) {
961 glTranslatef(dx*30.0f, 0.0f, dz*30.0f);
967 /* Set drawing mode for the boxes */
968 glEnable(GL_DEPTH_TEST);
969 if (!wire) glEnable(GL_TEXTURE_2D);
971 glColor3f(1.0,1.0,1.0);
972 glScalef(20.0,0.25,20.0);
973 glTranslatef(0.0,2.0,0.0);
974 drawfilledbox(gp, wire);
976 glDisable(GL_TEXTURE_2D);
979 glColor3f(0.2,0.5,0.2);
980 glScalef(20.0,20.0,0.25);
981 glTranslatef(0.0,1.0,81.0);
986 glColor3f(0.2,0.5,0.2);
987 glScalef(20.0,20.0,0.25);
988 glTranslatef(0.0,1.0,-81.0);
993 glColor3f(0.2,0.5,0.2);
994 glScalef(.25,20.0,20.0);
995 glTranslatef(-81.0,1.0,0.0);
1000 glColor3f(0.2,0.5,0.2);
1001 glScalef(.25,20.0,20.0);
1002 glTranslatef(81.0,1.0,0.0);
1007 glEnable(GL_LIGHTING);
1009 glMaterialfv(GL_FRONT, GL_DIFFUSE, dgray);
1010 glMaterialfv(GL_FRONT, GL_EMISSION, black); /* turn it off before painting the balls */
1013 /* move the balls and shrapnel */
1014 updateballs(&gp->bman);
1016 glFrontFace(GL_CCW);
1017 for (i=0;i<gp->bman.num_balls;i++) {
1018 if (gp->bman.balls[i].justcreated) {
1019 gp->bman.balls[i].justcreated = FALSE;
1020 freetris(&gp->tman[i]);
1022 if (gp->bman.balls[i].bounced) {
1023 if (gp->tman[i].vertices == NULL) {
1024 createtrisfromball(&gp->tman[i],gp->spherev,gp->spherei,SPHERE_INDICES,&gp->bman.balls[i]);
1026 updatetris(&gp->tman[i]);
1028 glDisable(GL_CULL_FACE);
1029 drawtriman(&gp->tman[i], wire);
1030 if (!wire) glEnable(GL_CULL_FACE);
1032 drawball(gp, &gp->bman.balls[i], wire);
1042 * new window size or exposure
1044 void reshape_boxed(ModeInfo *mi, int width, int height)
1046 GLfloat h = (GLfloat) height / (GLfloat) width;
1048 glViewport(0, 0, (GLint) width, (GLint) height);
1049 glMatrixMode(GL_PROJECTION);
1051 gluPerspective(50.0,1/h,2.0,1000.0);
1052 glMatrixMode (GL_MODELVIEW);
1060 pinit(ModeInfo * mi)
1062 boxedstruct *gp = &boxed[MI_SCREEN(mi)];
1063 int wire = MI_IS_WIREFRAME (mi);
1067 char *texpixeltarget;
1069 glShadeModel(GL_SMOOTH);
1071 glClearColor(0.0,0.05,0.1,0.0);
1072 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1074 /* Load configuration */
1075 setdefaultconfig(&gp->config);
1079 bman->balls = (ball *)malloc(gp->config.numballs * sizeof(ball));
1080 bman->num_balls = gp->config.numballs;
1081 bman->ballsize = gp->config.ballsize;
1082 bman->explosion = gp->config.explosion;
1084 gp->tman = (triman *)malloc(bman->num_balls * sizeof(triman));
1085 memset(gp->tman,0,bman->num_balls * sizeof(triman));
1087 for(i=0;i<bman->num_balls;i++) {
1088 gp->tman[i].explosion = (float) (((int)gp->config.explosion) / 15.0f );
1089 gp->tman[i].vertices = NULL;
1090 gp->tman[i].normals = NULL;
1091 gp->tman[i].tris = NULL;
1092 createball(&bman->balls[i]);
1093 bman->balls[i].loc.y *= rnd();
1099 glEnable(GL_CULL_FACE);
1100 glEnable(GL_LIGHTING);
1103 /* define cam path */
1104 gp->cam_x_speed = 1.0f/((float)gp->config.camspeed/50.0 + rnd()*((float)gp->config.camspeed/50.0));
1105 gp->cam_z_speed = 1.0f/((float)gp->config.camspeed/50.0 + rnd()*((float)gp->config.camspeed/50.0));
1106 gp->cam_y_speed = 1.0f/((float)gp->config.camspeed/250.0 + rnd()*((float)gp->config.camspeed/250.0));
1107 if (rnd() < 0.5f) gp->cam_x_speed = -gp->cam_x_speed;
1108 if (rnd() < 0.5f) gp->cam_z_speed = -gp->cam_z_speed;
1110 /* define initial cam position */
1111 gp->tic = gp->camtic = rnd() * 100.0f;
1113 /* define tex1 (bottom plate) */
1114 gp->tex1 = (char *)malloc(3*width*height*sizeof(GLuint));
1115 texpixels = 256*256; /*width*height;*/
1116 texpixeldata = header_data;
1117 texpixeltarget = gp->tex1;
1118 for (i=0; i < texpixels; i++) {
1119 HEADER_PIXEL(texpixeldata,texpixeltarget);
1120 texpixeltarget += 3;
1124 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
1127 i = gluBuild2DMipmaps(GL_TEXTURE_2D, 3, 256, 256,
1128 GL_RGB, GL_UNSIGNED_BYTE, gp->tex1);
1131 const char *s = (char *) gluErrorString (i);
1132 fprintf (stderr, "%s: error mipmapping texture: %s\n",
1133 progname, (s ? s : "(unknown)"));
1136 check_gl_error("mipmapping");
1138 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1139 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
1140 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
1141 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1142 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1149 init_boxed(ModeInfo * mi)
1151 int screen = MI_SCREEN(mi);
1153 /* Colormap cmap; */
1154 /* Boolean rgba, doublebuffer, cmap_installed; */
1157 if (boxed == NULL) {
1158 if ((boxed = (boxedstruct *) calloc(MI_NUM_SCREENS(mi),sizeof (boxedstruct))) == NULL) return;
1160 gp = &boxed[screen];
1161 gp->window = MI_WINDOW(mi);
1163 if ((gp->glx_context = init_GL(mi)) != NULL) {
1164 reshape_boxed(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
1165 glDrawBuffer(GL_BACK);
1166 if (!glIsList(gp->listobjects)) {
1167 gp->listobjects = glGenLists(3);
1180 draw_boxed(ModeInfo * mi)
1182 boxedstruct *gp = &boxed[MI_SCREEN(mi)];
1183 Display *display = MI_DISPLAY(mi);
1184 Window window = MI_WINDOW(mi);
1186 if (!gp->glx_context)
1189 glDrawBuffer(GL_BACK);
1191 glXMakeCurrent(display, window, *(gp->glx_context));
1194 if (mi->fps_p) do_fps (mi);
1196 glXSwapBuffers(display, window);
1200 release_boxed(ModeInfo * mi)
1204 if (boxed != NULL) {
1207 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
1208 boxedstruct *gp = &boxed[screen];
1210 if (gp->glx_context) {
1211 /* Display lists MUST be freed while their glXContext is current. */
1212 glXMakeCurrent(MI_DISPLAY(mi), gp->window, *(gp->glx_context));
1214 if (glIsList(gp->listobjects))
1215 glDeleteLists(gp->listobjects, 3);
1217 for (i=0;i<gp->bman.num_balls;i++) {
1218 if (gp->bman.balls[i].bounced) freetris(&gp->tman[i]);
1220 free (gp->bman.balls);
1227 (void) free((void *) boxed);
1234 /*********************************************************/