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" \
48 # include "xlockmore.h" /* from the xscreensaver distribution */
49 #else /* !STANDALONE */
50 # include "xlock.h" /* from the xlockmore distribution */
51 #endif /* !STANDALONE */
57 #define countof(x) (int)(sizeof((x))/sizeof((*x)))
59 #define rnd() (frand(1.0))
61 GLfloat speed; /* jwz -- overall speed factor applied to all motion */
64 static XrmOptionDescRec opts[] = {
65 {"-speed", ".boxed.speed", XrmoptionSepArg, 0},
68 static argtype vars[] = {
69 {&speed, "speed", "Speed", DEF_SPEED, t_Float},
72 ModeSpecOpt boxed_opts = {countof(opts), opts, countof(vars), vars, NULL};
76 ModStruct boxed_description = {
77 "boxed", "init_boxed", "draw_boxed", "release_boxed",
78 "draw_boxed", "init_boxed", NULL, &boxed_opts,
79 1000, 1, 2, 1, 4, 1.0, "",
80 "Shows GL's boxed balls", 0, NULL};
88 /* rendering defines */
92 #define BOX_SIZE 20.0f
95 #define CAM_HEIGHT 100.0f
96 #define CAMDISTANCE_MIN 20.0
97 #define CAMDISTANCE_MAX 150.0
98 #define CAMDISTANCE_SPEED 1.5
100 /* rendering the sphere */
102 #define SPHERE_VERTICES (2+MESH_SIZE*MESH_SIZE*2)
103 #define SPHERE_INDICES ((MESH_SIZE*4 + MESH_SIZE*4*(MESH_SIZE-1))*3)
105 #define EXPLOSION 10.0f
108 #define BALLSIZE 3.0f;
111 **-----------------------------------------------------------------------------
113 **-----------------------------------------------------------------------------
168 float cam_x_speed, cam_z_speed, cam_y_speed;
171 vectorf spherev[SPHERE_VERTICES];
172 GLint spherei[SPHERE_INDICES];
175 GLXContext *glx_context;
183 #define GLL_PATTERN 0
188 **----------------------------------------------------------------------------
190 **----------------------------------------------------------------------------
193 static boxedstruct *boxed = NULL;
197 **----------------------------------------------------------------------------
199 **----------------------------------------------------------------------------
205 static inline void addvectors(vectorf *dest, vectorf *s1, vectorf *s2)
207 dest->x = s1->x + s2->x;
208 dest->y = s1->y + s2->y;
209 dest->z = s1->z + s2->z;
215 static inline void subvectors(vectorf *dest, vectorf* s1, vectorf *s2)
217 dest->x = s1->x - s2->x;
218 dest->y = s1->y - s2->y;
219 dest->z = s1->z - s2->z;
223 * Multiply vector with scalar (scale vector)
225 static inline void scalevector(vectorf *dest, vectorf *source, GLfloat sc)
227 dest->x = source->x * sc;
228 dest->y = source->y * sc;
229 dest->z = source->z * sc;
235 static inline void copyvector(vectorf *dest, vectorf* source)
243 static inline GLfloat
244 dotproduct(vectorf * v1, vectorf * v2)
246 return v1->x * v2->x + v1->y * v2->y + v1->z * v2->z;
249 static inline GLfloat
250 squaremagnitude(vectorf * v)
252 return v->x * v->x + v->y * v->y + v->z * v->z;
258 * Generate the Sphere data
263 static void generatesphere(void)
265 float dj = M_PI/(MESH_SIZE+1.0f);
266 float di = M_PI/MESH_SIZE;
267 int v; /* vertex offset */
268 int ind; /* indices offset */
270 GLfloat r_y_plane, h_y_plane;
275 * generate the sphere data
276 * vertices 0 and 1 are the north and south poles
279 spherei = boxed->spherei;
280 spherev = boxed->spherev;
282 spherev[0].x = 0.0f; spherev[0].y =1.0f; spherev[0].z = 0.0f;
283 spherev[1].x = 0.0f; spherev[1].y =-1.0f; spherev[1].z = 0.0f;
285 for (j=0; j<MESH_SIZE; j++) {
286 r_y_plane = (float)sin((j+1) * dj);
287 h_y_plane = (float)cos((j+1) * dj);
288 for (i=0; i<MESH_SIZE*2; i++) {
289 si = 2+i+j*MESH_SIZE*2;
290 spherev[si].y = h_y_plane;
291 spherev[si].x = (float) sin(i * di) * r_y_plane;
292 spherev[si].z = (float) cos(i * di) * r_y_plane;
296 /* generate indices */
297 for (i=0; i<MESH_SIZE*2; i++) {
299 spherei[3*i+1] = i+2;
300 spherei[3*i+2] = i+3;
301 if (i==MESH_SIZE*2-1)
305 /* the middle strips */
306 for (j=0; j<MESH_SIZE-1; j++) {
308 ind = 3*MESH_SIZE*2 + j*6*MESH_SIZE*2;
309 for (i=0; i<MESH_SIZE*2; i++) {
310 spherei[6*i+ind] = v+i;
311 spherei[6*i+2+ind] = v+i+1;
312 spherei[6*i+1+ind] = v+i+MESH_SIZE*2;
314 spherei[6*i+ind+3] = v+i+MESH_SIZE*2;
315 spherei[6*i+2+ind+3] = v+i+1;
316 spherei[6*i+1+ind+3] = v+i+MESH_SIZE*2+1;
317 if (i==MESH_SIZE*2-1) {
318 spherei[6*i+2+ind] = v+i+1-2*MESH_SIZE;
319 spherei[6*i+2+ind+3] = v+i+1-2*MESH_SIZE;
320 spherei[6*i+1+ind+3] = v+i+MESH_SIZE*2+1-2*MESH_SIZE;
325 v = SPHERE_VERTICES-MESH_SIZE*2;
326 ind = SPHERE_INDICES-3*MESH_SIZE*2;
327 for (i=0; i<MESH_SIZE*2; i++) {
328 spherei[3*i+ind] = 1;
329 spherei[3*i+1+ind] = v+i+1;
330 spherei[3*i+2+ind] = v+i;
331 if (i==MESH_SIZE*2-1)
332 spherei[3*i+1+ind] = v;
343 void createball(ball *newball)
345 float r=0.0f,g=0.0f,b=0.0f;
346 newball->loc.x = 5-10*rnd();
347 newball->loc.y = 35+20*rnd();
348 newball->loc.z = 5-10*rnd();
349 newball->dir.x = (0.5f-rnd()) * speed;
350 newball->dir.y = 0.0;
351 newball->dir.z = (0.5-rnd()) * speed;
352 newball->offside = 0;
353 newball->bounced = FALSE;
354 newball->radius = BALLSIZE;
355 while (r+g+b < 1.7f ) {
356 newball->color.x = r=rnd();
357 newball->color.y = g=rnd();
358 newball->color.z = b=rnd();
360 newball->justcreated = TRUE;
363 /* Update position of each ball */
365 void updateballs(ballman *bman)
368 vectorf dvect,richting,relspeed,influence;
371 for (b=0;b<bman->num_balls;b++) {
373 GLfloat gravity = 0.15f * speed;
376 bman->balls[b].dir.y -= gravity;
378 addvectors(&bman->balls[b].loc,&bman->balls[b].loc,&bman->balls[b].dir);
380 if (bman->balls[b].loc.y < bman->balls[b].radius) { /* ball onder bodem? (bodem @ y=0) */
381 if ((bman->balls[b].loc.x < -100.0) ||
382 (bman->balls[b].loc.x > 100.0) ||
383 (bman->balls[b].loc.z < -100.0) ||
384 (bman->balls[b].loc.z > 100.0)) {
385 if (bman->balls[b].loc.y < -1000.0)
386 createball(&bman->balls[b]);
388 bman->balls[b].loc.y = bman->balls[b].radius + (bman->balls[b].radius - bman->balls[b].loc.y);
389 bman->balls[b].dir.y = -bman->balls[b].dir.y;
390 if (bman->balls[b].offside) {
391 bman->balls[b].bounced = TRUE; /* temporary disable painting ball */
392 scalevector(&bman->balls[b].dir,&bman->balls[b].dir,0.80f);
393 if (squaremagnitude(&bman->balls[b].dir) < 0.08f) {
394 createball(&bman->balls[b]);
400 if (!bman->balls[b].offside) {
401 if (bman->balls[b].loc.x - bman->balls[b].radius < -20.0f) { /* x ondergrens */
402 if (bman->balls[b].loc.y > 41+bman->balls[b].radius) bman->balls[b].offside=1;
404 bman->balls[b].dir.x = -bman->balls[b].dir.x;
405 bman->balls[b].loc.x = -20.0f + bman->balls[b].radius;
408 if (bman->balls[b].loc.x + bman->balls[b].radius > 20.0f) { /* x bovengrens */
409 if (bman->balls[b].loc.y > 41+bman->balls[b].radius) bman->balls[b].offside=1;
411 bman->balls[b].dir.x = -bman->balls[b].dir.x;
412 bman->balls[b].loc.x = 20.0f - bman->balls[b].radius;
415 if (bman->balls[b].loc.z - bman->balls[b].radius < -20.0f) { /* z ondergrens */
416 if (bman->balls[b].loc.y > 41+bman->balls[b].radius) bman->balls[b].offside=1;
418 bman->balls[b].dir.z = -bman->balls[b].dir.z;
419 bman->balls[b].loc.z = -20.0f + bman->balls[b].radius;
422 if (bman->balls[b].loc.z + bman->balls[b].radius > 20.0f) { /* z bovengrens */
423 if (bman->balls[b].loc.y > 41+bman->balls[b].radius) bman->balls[b].offside=1;
425 bman->balls[b].dir.z = -bman->balls[b].dir.z;
426 bman->balls[b].loc.z = 20.0f - bman->balls[b].radius;
429 } /* end if !offside */
431 /* check voor stuiteren */
432 for (j=b+1;j<bman->num_balls;j++) {
433 squaredist = (bman->balls[b].radius * bman->balls[b].radius) + (bman->balls[j].radius * bman->balls[j].radius);
434 subvectors(&dvect,&bman->balls[b].loc,&bman->balls[j].loc);
435 if ( squaremagnitude(&dvect) < squaredist ) { /* balls b and j touch */
436 subvectors(&richting,&bman->balls[j].loc,&bman->balls[b].loc);
437 subvectors(&relspeed,&bman->balls[b].dir,&bman->balls[j].dir);
438 /* calc mutual influence direction and magnitude */
439 scalevector(&influence,&richting,(dotproduct(&richting,&relspeed)/squaremagnitude(&richting)));
441 subvectors(&bman->balls[b].dir,&bman->balls[b].dir,&influence);
442 addvectors(&bman->balls[j].dir,&bman->balls[j].dir,&influence);
443 addvectors(&bman->balls[b].loc,&bman->balls[b].loc,&bman->balls[b].dir);
444 addvectors(&bman->balls[j].loc,&bman->balls[j].loc,&bman->balls[j].dir);
446 subvectors(&dvect,&bman->balls[b].loc,&bman->balls[j].loc);
447 while (squaremagnitude(&dvect) < squaredist) {
448 addvectors(&bman->balls[b].loc,&bman->balls[b].loc,&bman->balls[b].dir);
449 addvectors(&bman->balls[j].loc,&bman->balls[j].loc,&bman->balls[j].dir);
450 subvectors(&dvect,&bman->balls[b].loc,&bman->balls[j].loc);
459 * explode ball into triangles
462 void createtrisfromball(triman* tman, vectorf *spherev, GLint *spherei, int ind_num, ball *b)
468 vectorf avgdir,dvect;
470 tman->scalefac = b->radius;
471 copyvector(&tman->color,&b->color);
472 explosion = 1.0f + tman->explosion * 2.0 * rnd();
474 tman->num_tri = ind_num/3;
476 /* reserveer geheugen voor de poly's in een bal */
478 tman->tris = (tri *)malloc(tman->num_tri * sizeof(tri));
479 tman->vertices = (vectorf *)malloc(ind_num * sizeof(vectorf));
480 tman->normals = (vectorf *)malloc(ind_num/3 * sizeof(vectorf));
482 for (i=0; i<(tman->num_tri); i++) {
483 tman->tris[i].far = FALSE;
485 /* kopieer elke poly apart naar een tri structure */
486 copyvector(&tman->vertices[pos+0],&spherev[spherei[pos+0]]);
487 copyvector(&tman->vertices[pos+1],&spherev[spherei[pos+1]]);
488 copyvector(&tman->vertices[pos+2],&spherev[spherei[pos+2]]);
489 /* Calculate average direction of shrapnel */
490 addvectors(&avgdir,&tman->vertices[pos+0],&tman->vertices[pos+1]);
491 addvectors(&avgdir,&avgdir,&tman->vertices[pos+2]);
492 scalevector(&avgdir,&avgdir,0.33333);
494 /* should normalize first, NYI */
495 copyvector(&tman->normals[i],&avgdir);
497 /* copy de lokatie */
498 addvectors(&tman->tris[i].loc,&b->loc,&avgdir);
499 /* en translate alle triangles terug naar hun eigen oorsprong */
500 tman->vertices[pos+0].x -= avgdir.x;
501 tman->vertices[pos+0].y -= avgdir.y;
502 tman->vertices[pos+0].z -= avgdir.z;
503 tman->vertices[pos+1].x -= avgdir.x;
504 tman->vertices[pos+1].y -= avgdir.y;
505 tman->vertices[pos+1].z -= avgdir.z;
506 tman->vertices[pos+2].x -= avgdir.x;
507 tman->vertices[pos+2].y -= avgdir.y;
508 tman->vertices[pos+2].z -= avgdir.z;
509 /* alwaar opschaling plaatsvindt */
510 scale = b->radius * 2;
511 scalevector(&tman->vertices[pos+0],&tman->vertices[pos+0],scale);
512 scalevector(&tman->vertices[pos+1],&tman->vertices[pos+1],scale);
513 scalevector(&tman->vertices[pos+2],&tman->vertices[pos+2],scale);
515 /* bereken nieuwe richting */
516 scalevector(&tman->tris[i].dir,&avgdir,explosion);
517 dvect.x = (0.1f - 0.2f*rnd());
518 dvect.y = (0.15f - 0.3f*rnd());
519 dvect.z = (0.1f - 0.2f*rnd());
520 addvectors(&tman->tris[i].dir,&tman->tris[i].dir,&dvect);
526 * update position of each tri
529 void updatetris(triman *t)
534 for (b=0;b<t->num_tri;b++) {
536 t->tris[b].dir.y -= (0.1f * speed);
538 addvectors(&t->tris[b].loc,&t->tris[b].loc,&t->tris[b].dir);
540 if (t->tris[b].far) continue;
541 if (t->tris[b].loc.y < 0) { /* onder bodem ? */
542 if ((t->tris[b].loc.x > -100.0f) &
543 (t->tris[b].loc.x < 100.0f) &
544 (t->tris[b].loc.z > -100.0f) &
545 (t->tris[b].loc.z < 100.0f)) { /* in veld */
546 t->tris[b].dir.y = -(t->tris[b].dir.y);
547 t->tris[b].loc.y = -t->tris[b].loc.y;
548 scalevector(&t->tris[b].dir,&t->tris[b].dir,0.80f); /* dampening */
551 t->tris[b].far = TRUE;
556 if ((t->tris[b].loc.x > -21.0f) &
557 (t->tris[b].loc.x < 21.0f) &
558 (t->tris[b].loc.z > -21.0f) &
559 (t->tris[b].loc.z < 21.0f)) { /* in box? */
561 xd = zd = 999.0f; /* big */
562 if ((t->tris[b].loc.x > -21.0f) &
563 (t->tris[b].loc.x < 0)) {
564 xd = t->tris[b].loc.x + 21.0f;
566 if ((t->tris[b].loc.x < 21.0f) &
567 (t->tris[b].loc.x > 0)) {
568 xd = 21.0f - t->tris[b].loc.x;
570 if ((t->tris[b].loc.z > -21.0f) &
571 (t->tris[b].loc.z < 0)) {
572 zd = t->tris[b].loc.z + 21.0f;
574 if ((t->tris[b].loc.z < 21.0f) &
575 (t->tris[b].loc.z > 0)) {
576 zd = 21.0f - t->tris[b].loc.z;
580 if (t->tris[b].dir.x < 0)
581 t->tris[b].loc.x += (21.0f - t->tris[b].loc.x);
583 t->tris[b].loc.x += (-21.0f - t->tris[b].loc.x);
584 t->tris[b].dir.x = -t->tris[b].dir.x;
587 if (t->tris[b].dir.z < 0)
588 t->tris[b].loc.z += (21.0f - t->tris[b].loc.z);
590 t->tris[b].loc.z += (-21.0f - t->tris[b].loc.z);
591 t->tris[b].dir.z = -t->tris[b].dir.z;
600 * free memory allocated by a tri manager
602 void freetris(triman *t)
605 if (t->tris) free(t->tris);
606 if (t->vertices) free(t->vertices);
607 if (t->normals) free(t->normals);
617 *load defaults in config structure
619 void setdefaultconfig(boxed_config *config)
621 config->numballs = NUMBALLS;
622 config->textures = TRUE;
623 config->transparent = FALSE;
624 config->explosion = 25.0f;
625 config->ballsize = BALLSIZE;
626 config->camspeed = 35.0f;
633 static void drawfilledbox(boxedstruct *boxed)
635 /* draws texture filled box,
636 top is drawn using the entire texture,
637 the sides are drawn using the edge of the texture
643 glVertex3f(-1.0,1.0,1.0);
645 glVertex3f(1.0,1.0,1.0);
647 glVertex3f(1.0,-1.0,1.0);
649 glVertex3f(-1.0,-1.0,1.0);
652 glVertex3f(1.0,1.0,-1.0);
654 glVertex3f(-1.0,1.0,-1.0);
656 glVertex3f(-1.0,-1.0,-1.0);
658 glVertex3f(1.0,-1.0,-1.0);
661 glVertex3f(-1.0,1.0,1.0);
663 glVertex3f(-1.0,-1.0,1.0);
665 glVertex3f(-1.0,-1.0,-1.0);
667 glVertex3f(-1.0,1.0,-1.0);
670 glVertex3f(1.0,1.0,1.0);
672 glVertex3f(1.0,1.0,-1.0);
674 glVertex3f(1.0,-1.0,-1.0);
676 glVertex3f(1.0,-1.0,1.0);
678 glTexCoord2f(0.0,0.0);
679 glVertex3f(-1.0,1.0,1.0);
680 glTexCoord2f(0.0,1.0);
681 glVertex3f(-1.0,1.0,-1.0);
682 glTexCoord2f(1.0,1.0);
683 glVertex3f(1.0,1.0,-1.0);
684 glTexCoord2f(1.0,0.0);
685 glVertex3f(1.0,1.0,1.0);
688 glVertex3f(-1.0,-1.0,1.0);
690 glVertex3f(-1.0,-1.0,-1.0);
692 glVertex3f(1.0,-1.0,-1.0);
694 glVertex3f(1.0,-1.0,1.0);
700 * Draw a box made of lines
702 static void drawbox(boxedstruct *boxed)
705 glBegin(GL_LINE_STRIP);
706 glVertex3f(-1.0,1.0,1.0);
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);
713 glBegin(GL_LINE_STRIP);
714 glVertex3f(-1.0,-1.0,1.0);
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);
720 /* connect top & bottom */
722 glVertex3f(-1.0,1.0,1.0);
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);
738 static void drawball(boxedstruct *gp, ball *b)
741 GLint *spherei = gp->spherei;
742 vectorf *spherev = gp->spherev;
747 glTranslatef(b->loc.x,b->loc.y,b->loc.z);
748 glScalef(b->radius,b->radius,b->radius);
749 glColor3f(b->color.x,b->color.y,b->color.z);
753 glMaterialfv(GL_FRONT, GL_DIFFUSE, col);
757 glMaterialfv(GL_FRONT, GL_EMISSION,col);
759 if (!gp->gllists[GLL_BALL]) {
760 glNewList(gp->listobjects + GLL_BALL,GL_COMPILE);
761 cnt = SPHERE_INDICES/3;
762 for (i=0; i<cnt; i++) {
764 glBegin(GL_TRIANGLES);
765 glNormal3f(spherev[spherei[pos+0]].x,spherev[spherei[pos+0]].y,spherev[spherei[pos+0]].z);
766 glVertex3f(spherev[spherei[pos+0]].x,spherev[spherei[pos+0]].y,spherev[spherei[pos+0]].z);
767 glNormal3f(spherev[spherei[pos+1]].x,spherev[spherei[pos+1]].y,spherev[spherei[pos+1]].z);
768 glVertex3f(spherev[spherei[pos+1]].x,spherev[spherei[pos+1]].y,spherev[spherei[pos+1]].z);
769 glNormal3f(spherev[spherei[pos+2]].x,spherev[spherei[pos+2]].y,spherev[spherei[pos+2]].z);
770 glVertex3f(spherev[spherei[pos+2]].x,spherev[spherei[pos+2]].y,spherev[spherei[pos+2]].z);
774 gp->gllists[GLL_BALL] = 1;
776 glCallList(gp->listobjects + GLL_BALL);
784 * Draw all triangles in triman
786 static void drawtriman(triman *t)
789 vectorf *spherev = t->vertices;
793 glColor3f(t->color.x,t->color.y,t->color.z);
797 glMaterialfv(GL_FRONT, GL_DIFFUSE, col);
801 glMaterialfv(GL_FRONT, GL_EMISSION,col);
803 for (i=0; i<t->num_tri; i++) {
806 glTranslatef(t->tris[i].loc.x,t->tris[i].loc.y,t->tris[i].loc.z);
807 glBegin(GL_TRIANGLES);
808 glNormal3f(t->normals[i].x,t->normals[i].y,t->normals[i].z);
809 glVertex3f(spherev[pos+0].x,spherev[pos+0].y,spherev[pos+0].z);
810 glVertex3f(spherev[pos+1].x,spherev[pos+1].y,spherev[pos+1].z);
811 glVertex3f(spherev[pos+2].x,spherev[pos+2].y,spherev[pos+2].z);
821 static void drawpattern(boxedstruct *boxed)
823 if (!boxed->gllists[GLL_PATTERN]) {
824 glNewList(boxed->listobjects + GLL_PATTERN, GL_COMPILE);
826 glBegin(GL_LINE_STRIP);
827 glVertex3f(-25.0f, 0.0f, 35.0f);
828 glVertex3f(-15.0f, 0.0f, 35.0f);
829 glVertex3f(-5.0f, 0.0f, 25.0f);
830 glVertex3f(5.0f, 0.0f, 25.0f);
831 glVertex3f(15.0f, 0.0f, 35.0f);
832 glVertex3f(25.0f, 0.0f, 35.0f);
833 glVertex3f(35.0f, 0.0f, 25.0f);
834 glVertex3f(35.0f, 0.0f, 15.0f);
835 glVertex3f(25.0f, 0.0f, 5.0f);
836 glVertex3f(25.0f, 0.0f, -5.0f);
837 glVertex3f(35.0f, 0.0f, -15.0f);
838 glVertex3f(35.0f, 0.0f, -25.0f);
839 glVertex3f(25.0f, 0.0f, -35.0f);
840 glVertex3f(15.0f, 0.0f,-35.0f);
841 glVertex3f(5.0f, 0.0f, -25.0f);
842 glVertex3f(-5.0f, 0.0f, -25.0f);
843 glVertex3f(-15.0f, 0.0f,-35.0f);
844 glVertex3f(-25.0f, 0.0f,-35.0f);
845 glVertex3f(-35.0f, 0.0f, -25.0f);
846 glVertex3f(-35.0f, 0.0f, -15.0f);
847 glVertex3f(-25.0f, 0.0f, -5.0f);
848 glVertex3f(-25.0f, 0.0f, 5.0f);
849 glVertex3f(-35.0f, 0.0f, 15.0f);
850 glVertex3f(-35.0f, 0.0f, 25.0f);
851 glVertex3f(-25.0f, 0.0f, 35.0f);
854 glBegin(GL_LINE_STRIP);
855 glVertex3f(-5.0f, 0.0f, 15.0f);
856 glVertex3f(5.0f, 0.0f, 15.0f);
857 glVertex3f(15.0f, 0.0f, 5.0f);
858 glVertex3f(15.0f, 0.0f, -5.0f);
859 glVertex3f(5.0f, 0.0f, -15.0f);
860 glVertex3f(-5.0f, 0.0f, -15.0f);
861 glVertex3f(-15.0f, 0.0f, -5.0f);
862 glVertex3f(-15.0f, 0.0f, 5.0f);
863 glVertex3f(-5.0f, 0.0f, 15.0f);
867 boxed->gllists[GLL_PATTERN] = 1;
869 glCallList(boxed->listobjects + GLL_PATTERN);
876 * main rendering loop
878 static void draw(ModeInfo * mi)
880 boxedstruct *gp = &boxed[MI_SCREEN(mi)];
886 GLfloat dgray[4] = {0.3f, 0.3f, 0.3f, 1.0f};
887 GLfloat black[4] = {0.0f, 0.0f, 0.0f, 1.0f};
888 GLfloat lblue[4] = {0.4f,0.6f,1.0f };
890 GLfloat l0_ambient[] = {0.0, 0.0, 0.0, 1.0};
891 GLfloat l0_specular[] = {1.0, 1.0, 1.0, 1.0};
892 GLfloat l0_diffuse[] = {1.0, 1.0, 1.0, 1.0};
893 GLfloat l0_position[] = {0.0, 0.0, 0.0, 1.0}; /* w != 0 -> positional light */
894 GLfloat l1_ambient[] = {0.0, 0.0, 0.0, 1.0};
895 GLfloat l1_specular[] = {1.0, 1.0, 1.0, 1.0};
896 GLfloat l1_diffuse[] = {0.5, 0.5, 0.5, 1.0};
897 GLfloat l1_position[] = {0.0, 1.0, 0.0, 0.0}; /* w = 0 -> directional light */
899 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
904 /* rotate camera around (0,0,0), looking at (0,0,0), up is (0,1,0) */
905 dcam = CAMDISTANCE_MIN + (CAMDISTANCE_MAX - CAMDISTANCE_MIN) + (CAMDISTANCE_MAX - CAMDISTANCE_MIN)*cos((gp->tic/CAMDISTANCE_SPEED) * speed);
906 v1.x = dcam * sin((gp->tic/gp->cam_x_speed) * speed);
907 v1.z = dcam * cos((gp->tic/gp->cam_z_speed) * speed);
908 v1.y = CAM_HEIGHT * sin((gp->tic/gp->cam_y_speed) * speed) + 1.02 * CAM_HEIGHT;
909 gluLookAt(v1.x,v1.y,v1.z,0.0,0.0,0.0,0.0,1.0,0.0);
911 glLightfv(GL_LIGHT0, GL_AMBIENT, l0_ambient);
912 glLightfv(GL_LIGHT0, GL_DIFFUSE, l0_diffuse);
913 glLightfv(GL_LIGHT0, GL_SPECULAR, l0_specular);
914 glLightfv(GL_LIGHT0, GL_POSITION, l0_position);
915 glLightfv(GL_LIGHT1, GL_AMBIENT, l1_ambient);
916 glLightfv(GL_LIGHT1, GL_DIFFUSE, l1_diffuse);
917 glLightfv(GL_LIGHT1, GL_SPECULAR, l1_specular);
918 glLightfv(GL_LIGHT1, GL_POSITION, l1_position);
924 glMaterialfv(GL_FRONT, GL_SPECULAR, black);
925 glMaterialfv(GL_FRONT, GL_EMISSION, lblue);
926 glMaterialfv(GL_FRONT,GL_AMBIENT,black);
927 glMaterialf(GL_FRONT, GL_SHININESS, 5.0);
930 /* draw ground grid */
931 /* glDisable(GL_DEPTH_TEST); */
932 glDisable(GL_LIGHTING);
934 glColor3f(0.1,0.1,0.6);
935 for (dx= -2; dx<3; dx++) {
936 for (dz= -2; dz<3; dz++) {
938 glTranslatef(dx*30.0f, 0.0f, dz*30.0f);
944 /* Set drawing mode for the boxes */
945 glEnable(GL_DEPTH_TEST);
946 glEnable(GL_TEXTURE_2D);
948 glColor3f(1.0,1.0,1.0);
949 glScalef(20.0,0.25,20.0);
950 glTranslatef(0.0,2.0,0.0);
953 glDisable(GL_TEXTURE_2D);
956 glColor3f(0.2,0.5,0.2);
957 glScalef(20.0,20.0,0.25);
958 glTranslatef(0.0,1.0,81.0);
963 glColor3f(0.2,0.5,0.2);
964 glScalef(20.0,20.0,0.25);
965 glTranslatef(0.0,1.0,-81.0);
970 glColor3f(0.2,0.5,0.2);
971 glScalef(.25,20.0,20.0);
972 glTranslatef(-81.0,1.0,0.0);
977 glColor3f(0.2,0.5,0.2);
978 glScalef(.25,20.0,20.0);
979 glTranslatef(81.0,1.0,0.0);
983 glEnable(GL_LIGHTING);
985 glMaterialfv(GL_FRONT, GL_DIFFUSE, dgray);
986 glMaterialfv(GL_FRONT, GL_EMISSION, black); /* turn it off before painting the balls */
988 /* move the balls and shrapnel */
989 updateballs(&gp->bman);
992 for (i=0;i<gp->bman.num_balls;i++) {
993 if (gp->bman.balls[i].justcreated) {
994 gp->bman.balls[i].justcreated = FALSE;
995 freetris(&gp->tman[i]);
997 if (gp->bman.balls[i].bounced) {
998 if (gp->tman[i].vertices == NULL) {
999 createtrisfromball(&gp->tman[i],gp->spherev,gp->spherei,SPHERE_INDICES,&gp->bman.balls[i]);
1001 updatetris(&gp->tman[i]);
1003 glDisable(GL_CULL_FACE);
1004 drawtriman(&gp->tman[i]);
1005 glEnable(GL_CULL_FACE);
1007 drawball(gp, &gp->bman.balls[i]);
1017 * new window size or exposure
1019 void reshape_boxed(ModeInfo *mi, int width, int height)
1021 GLfloat h = (GLfloat) height / (GLfloat) width;
1023 glViewport(0, 0, (GLint) width, (GLint) height);
1024 glMatrixMode(GL_PROJECTION);
1026 gluPerspective(50.0,1/h,2.0,1000.0);
1027 glMatrixMode (GL_MODELVIEW);
1035 pinit(ModeInfo * mi)
1037 boxedstruct *gp = &boxed[MI_SCREEN(mi)];
1041 char *texpixeltarget;
1043 glShadeModel(GL_SMOOTH);
1045 glClearColor(0.0,0.05,0.1,0.0);
1046 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1048 /* Load configuration */
1049 setdefaultconfig(&gp->config);
1053 bman->balls = (ball *)malloc(gp->config.numballs * sizeof(ball));
1054 bman->num_balls = gp->config.numballs;
1055 bman->ballsize = gp->config.ballsize;
1056 bman->explosion = gp->config.explosion;
1058 gp->tman = (triman *)malloc(bman->num_balls * sizeof(triman));
1059 memset(gp->tman,0,bman->num_balls * sizeof(triman));
1061 for(i=0;i<bman->num_balls;i++) {
1062 gp->tman[i].explosion = (float) (((int)gp->config.explosion) / 15.0f );
1063 gp->tman[i].vertices = NULL;
1064 gp->tman[i].normals = NULL;
1065 gp->tman[i].tris = NULL;
1066 createball(&bman->balls[i]);
1067 bman->balls[i].loc.y *= rnd();
1072 glEnable(GL_CULL_FACE);
1073 glEnable(GL_LIGHTING);
1075 /* define cam path */
1076 gp->cam_x_speed = 1.0f/((float)gp->config.camspeed/50.0 + rnd()*((float)gp->config.camspeed/50.0));
1077 gp->cam_z_speed = 1.0f/((float)gp->config.camspeed/50.0 + rnd()*((float)gp->config.camspeed/50.0));
1078 gp->cam_y_speed = 1.0f/((float)gp->config.camspeed/250.0 + rnd()*((float)gp->config.camspeed/250.0));
1079 if (rnd() < 0.5f) gp->cam_x_speed = -gp->cam_x_speed;
1080 if (rnd() < 0.5f) gp->cam_z_speed = -gp->cam_z_speed;
1083 gp->tex1 = (char *)malloc(3*width*height*sizeof(GLuint));
1084 texpixels = 256*256; /*width*height;*/
1085 texpixeldata = header_data;
1086 texpixeltarget = gp->tex1;
1087 for (i=0; i < texpixels; i++) {
1088 HEADER_PIXEL(texpixeldata,texpixeltarget);
1089 texpixeltarget += 3;
1093 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
1096 i = gluBuild2DMipmaps(GL_TEXTURE_2D, 3, 256, 256,
1097 GL_RGB, GL_UNSIGNED_BYTE, gp->tex1);
1100 const char *s = (char *) gluErrorString (i);
1101 fprintf (stderr, "%s: error mipmapping texture: %s\n",
1102 progname, (s ? s : "(unknown)"));
1105 check_gl_error("mipmapping");
1107 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1108 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
1109 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
1110 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1111 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1118 init_boxed(ModeInfo * mi)
1120 int screen = MI_SCREEN(mi);
1122 /* Colormap cmap; */
1123 /* Boolean rgba, doublebuffer, cmap_installed; */
1126 if (boxed == NULL) {
1127 if ((boxed = (boxedstruct *) calloc(MI_NUM_SCREENS(mi),sizeof (boxedstruct))) == NULL) return;
1129 gp = &boxed[screen];
1130 gp->window = MI_WINDOW(mi);
1132 if ((gp->glx_context = init_GL(mi)) != NULL) {
1133 reshape_boxed(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
1134 glDrawBuffer(GL_BACK);
1135 if (!glIsList(gp->listobjects)) {
1136 gp->listobjects = glGenLists(3);
1149 draw_boxed(ModeInfo * mi)
1151 boxedstruct *gp = &boxed[MI_SCREEN(mi)];
1152 Display *display = MI_DISPLAY(mi);
1153 Window window = MI_WINDOW(mi);
1155 if (!gp->glx_context)
1158 glDrawBuffer(GL_BACK);
1160 glXMakeCurrent(display, window, *(gp->glx_context));
1163 if (mi->fps_p) do_fps (mi);
1165 glXSwapBuffers(display, window);
1169 release_boxed(ModeInfo * mi)
1173 if (boxed != NULL) {
1176 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
1177 boxedstruct *gp = &boxed[screen];
1179 if (gp->glx_context) {
1180 /* Display lists MUST be freed while their glXContext is current. */
1181 glXMakeCurrent(MI_DISPLAY(mi), gp->window, *(gp->glx_context));
1183 if (glIsList(gp->listobjects))
1184 glDeleteLists(gp->listobjects, 3);
1186 for (i=0;i<gp->bman.num_balls;i++) {
1187 if (gp->bman.balls[i].bounced) freetris(&gp->tman[i]);
1189 free (gp->bman.balls);
1196 (void) free((void *) boxed);
1203 /*********************************************************/