http://packetstormsecurity.org/UNIX/admin/xscreensaver-4.01.tar.gz
[xscreensaver] / hacks / glx / boxed.c
1 /* thebox --- 3D bouncing balls that explode */
2
3 #if !defined( lint ) && !defined( SABER )
4 static const char sccsid[] = "@(#)boxed.c       0.9 01/09/26 xlockmore";
5
6 #endif
7
8 /*-
9  * Permission to use, copy, modify, and distribute this software and its
10  * documentation for any purpose and without fee is hereby granted,
11  * provided that the above copyright notice appear in all copies and that
12  * both that copyright notice and this permission notice appear in
13  * supporting documentation.
14  *
15  * This file is provided AS IS with no warranties of any kind.  The author
16  * shall have no liability with respect to the infringement of copyrights,
17  * trade secrets or any patents by this file or any part thereof.  In no
18  * event will the author be liable for any lost revenue or profits or
19  * other special, indirect and consequential damages.
20  *
21  * Revision History:
22  *
23  * 2001: Written by Sander van Grieken <mailsander@gmx.net> 
24  *       as an OpenGL screensaver for the xscreensaver package.
25  *       Lots of hardcoded values still in place. Also, there are some
26  *       copy/paste leftovers from the gears hack. opts don't work.
27  */
28
29 #include <X11/Intrinsic.h>
30 #include "boxed.h"
31
32 /*
33 **----------------------------------------------------------------------------
34 ** Defines
35 **----------------------------------------------------------------------------
36 */
37
38 #ifdef STANDALONE
39 # define PROGCLASS                                      "boxed"
40 # define HACK_INIT                                      init_boxed
41 # define HACK_DRAW                                      draw_boxed
42 # define HACK_RESHAPE                                   reshape_boxed
43 # define boxed_opts                                     xlockmore_opts
44 # define DEFAULTS       "*delay:                20000   \n"                     \
45      "*showFPS:         False   \n"                     \
46
47 # include "xlockmore.h"                         /* from the xscreensaver distribution */
48 #else  /* !STANDALONE */
49 # include "xlock.h"                                     /* from the xlockmore distribution */
50 #endif /* !STANDALONE */
51
52 #ifdef USE_GL
53 #include <GL/glu.h>
54
55 #undef countof 
56 #define countof(x) (int)(sizeof((x))/sizeof((*x)))
57 #undef rnd
58 #define rnd() ( ((float)random()) / ((float)RAND_MAX * 2.0) )
59
60 /* #define DEF_PLANETARY "False"
61
62 static int planetary;
63
64 static XrmOptionDescRec opts[] = {
65   {"-planetary", ".gears.planetary", XrmoptionNoArg, (caddr_t) "true" },
66   {"+planetary", ".gears.planetary", XrmoptionNoArg, (caddr_t) "false" },
67 };
68
69 static argtype vars[] = {
70   {(caddr_t *) &planetary, "planetary", "Planetary", DEF_PLANETARY, t_Bool},
71 };
72 */
73
74 /* ModeSpecOpts boxed_opts = {countof(opts), opts, countof(vars), vars, NULL}; */
75
76 ModeSpecOpt boxed_opts = {0, NULL, 0, NULL, NULL};
77
78 #ifdef USE_MODULES
79
80 ModStruct   boxed_description = { 
81      "boxed", "init_boxed", "draw_boxed", "release_boxed",
82      "draw_boxed", "init_boxed", NULL, &boxed_opts,
83      1000, 1, 2, 1, 4, 1.0, "",
84      "Shows GL's boxed balls", 0, NULL};
85
86 #endif
87
88 #define BOOL int
89 #define TRUE 1
90 #define FALSE 0
91
92 /* rendering defines */
93
94 /* box size */
95 #define BOX_SIZE        20.0f
96
97 /* camera */
98 #define CAM_HEIGHT      100.0f
99 #define CAMDISTANCE_MIN 20.0
100 #define CAMDISTANCE_MAX 150.0
101 #define CAMDISTANCE_SPEED 1.5
102
103 /* rendering the sphere */
104 #define MESH_SIZE       5
105 #define SPHERE_VERTICES (2+MESH_SIZE*MESH_SIZE*2)
106 #define SPHERE_INDICES  ((MESH_SIZE*4 + MESH_SIZE*4*(MESH_SIZE-1))*3)
107
108 #define EXPLOSION 10.0f
109 #define MAXBALLS  50;
110 #define NUMBALLS 12;
111 #define BALLSIZE 3.0f;
112
113 /*
114 **-----------------------------------------------------------------------------
115 **      Typedefs
116 **-----------------------------------------------------------------------------
117 */
118
119 typedef struct {
120    GLfloat x;
121    GLfloat y;
122    GLfloat z;
123 } vectorf;
124
125 typedef struct {
126    vectorf      loc;
127    vectorf      dir;
128    vectorf      color;
129    float        radius;
130    BOOL         bounced;
131    int          offside;
132    BOOL         justcreated;
133 } ball;
134
135 typedef struct {
136    int          num_balls;
137    float        ballsize;
138    float        explosion;
139    ball         *balls;
140 } ballman;
141
142 typedef struct {
143    vectorf      loc;
144    vectorf      dir;
145    BOOL         far;
146 } tri;
147
148 typedef struct {
149    int          num_tri;
150    int          lifetime;
151    float        scalefac;
152    float        explosion;
153    vectorf      color;
154    tri          *tris;
155    GLint        *indices;
156    vectorf      *normals;
157    vectorf      *vertices;
158 } triman;
159
160 typedef struct {
161    int numballs;
162    float ballsize;
163    float explosion;
164    BOOL textures;
165    BOOL transparent;
166    float camspeed;
167 } boxed_config;
168
169
170 typedef struct {
171    float          cam_x_speed, cam_z_speed, cam_y_speed;
172    boxed_config  config;
173    float          tic;
174    vectorf        spherev[SPHERE_VERTICES];
175    GLint          spherei[SPHERE_INDICES];
176    ballman        bman;
177    triman         *tman;
178    GLXContext     *glx_context;
179    GLint          listobjects;
180    GLint          gllists[3];
181    Window         window;
182    BOOL           stop;
183    char           *tex1;
184 } boxedstruct;
185
186 #define GLL_PATTERN 1
187 #define GLL_BALL    2
188 #define GLL_BOX     3
189
190 /*
191 **----------------------------------------------------------------------------
192 ** Local Variables
193 **----------------------------------------------------------------------------
194 */
195
196 static boxedstruct *boxed = NULL;
197
198
199 /*
200 **----------------------------------------------------------------------------
201 ** Functions
202 **----------------------------------------------------------------------------
203 */
204
205 /*
206  * Add 2 vectors
207  */ 
208 static inline void addvectors(vectorf *dest, vectorf *s1, vectorf *s2) 
209 {
210    dest->x = s1->x + s2->x;
211    dest->y = s1->y + s2->y;
212    dest->z = s1->z + s2->z;
213 }
214
215 /*
216  * Sub 2 vectors
217  */ 
218 static inline void subvectors(vectorf *dest, vectorf* s1, vectorf *s2) 
219 {
220    dest->x = s1->x - s2->x;
221    dest->y = s1->y - s2->y;
222    dest->z = s1->z - s2->z;
223 }
224
225 /*
226  * Multiply vector with scalar (scale vector)
227  */ 
228 static inline void scalevector(vectorf *dest, vectorf *source, GLfloat sc)
229 {
230    dest->x = source->x * sc;
231    dest->y = source->y * sc;
232    dest->z = source->z * sc;
233 }
234
235 /*
236  * Copy vector
237  */
238 static inline void copyvector(vectorf *dest, vectorf* source) 
239 {
240    dest->x = source->x;
241    dest->y = source->y;
242    dest->z = source->z;
243 }
244
245
246 static inline GLfloat
247 dotproduct(vectorf * v1, vectorf * v2)
248 {
249    return v1->x * v2->x + v1->y * v2->y + v1->z * v2->z;
250 }
251
252 static inline GLfloat
253 squaremagnitude(vectorf * v)
254 {
255    return v->x * v->x + v->y * v->y + v->z * v->z;
256 }
257
258
259
260 /*
261  * Generate the Sphere data
262  * 
263  * Input: 
264  */ 
265
266 static void generatesphere(void)
267 {
268    float   dj = M_PI/(MESH_SIZE+1.0f);
269    float   di = M_PI/MESH_SIZE;
270    int     v;   /* vertex offset */
271    int     ind; /* indices offset */
272    int     i,j,si;
273    GLfloat r_y_plane, h_y_plane;
274    vectorf *spherev;
275    GLint   *spherei;
276    
277    /*
278     * generate the sphere data
279     * vertices 0 and 1 are the north and south poles
280     */
281    
282    spherei = boxed->spherei;
283    spherev = boxed->spherev;
284    
285    spherev[0].x = 0.0f; spherev[0].y =1.0f; spherev[0].z = 0.0f;
286    spherev[1].x = 0.0f; spherev[1].y =-1.0f; spherev[1].z = 0.0f;
287    
288    for (j=0; j<MESH_SIZE; j++) {
289       r_y_plane = (float)sin((j+1) * dj);
290       h_y_plane = (float)cos((j+1) * dj);
291       for (i=0; i<MESH_SIZE*2; i++) {
292          si = 2+i+j*MESH_SIZE*2;
293          spherev[si].y = h_y_plane;
294          spherev[si].x = (float) sin(i * di) * r_y_plane;
295          spherev[si].z = (float) cos(i * di) * r_y_plane;
296       }
297    }
298    
299    /* generate indices */
300    for (i=0; i<MESH_SIZE*2; i++) {
301       spherei[3*i] = 0;
302       spherei[3*i+1] = i+2;
303       spherei[3*i+2] = i+3;
304       if (i==MESH_SIZE*2-1)
305         spherei[3*i+2] = 2;
306    }
307    
308    /* the middle strips */
309    for (j=0; j<MESH_SIZE-1; j++) {
310       v = 2+j*MESH_SIZE*2;
311       ind = 3*MESH_SIZE*2 + j*6*MESH_SIZE*2;
312       for (i=0; i<MESH_SIZE*2; i++) {
313          spherei[6*i+ind] = v+i;
314          spherei[6*i+2+ind] = v+i+1;
315          spherei[6*i+1+ind] = v+i+MESH_SIZE*2;
316          
317          spherei[6*i+ind+3] = v+i+MESH_SIZE*2;
318          spherei[6*i+2+ind+3] = v+i+1;
319          spherei[6*i+1+ind+3] = v+i+MESH_SIZE*2+1;
320          if (i==MESH_SIZE*2-1) {
321             spherei[6*i+2+ind] = v+i+1-2*MESH_SIZE;
322             spherei[6*i+2+ind+3] = v+i+1-2*MESH_SIZE;
323             spherei[6*i+1+ind+3] = v+i+MESH_SIZE*2+1-2*MESH_SIZE;
324          }
325       }
326    }
327    
328    v = SPHERE_VERTICES-MESH_SIZE*2;
329    ind = SPHERE_INDICES-3*MESH_SIZE*2;
330    for (i=0; i<MESH_SIZE*2; i++) {
331       spherei[3*i+ind] = 1;
332       spherei[3*i+1+ind] = v+i+1;
333       spherei[3*i+2+ind] = v+i;
334       if (i==MESH_SIZE*2-1)
335         spherei[3*i+1+ind] = v;
336    }
337 }
338
339       
340       
341     
342 /*
343  * create fresh ball 
344  */
345
346 void createball(ball *newball) {
347    float r=0.0f,g=0.0f,b=0.0f;
348    newball->loc.x = 5-10*rnd();
349    newball->loc.y = 35+20*rnd();
350    newball->loc.z = 5-10*rnd();
351    newball->dir.x = 0.5f-rnd();
352    newball->dir.y = 0.0;
353    newball->dir.z = 0.5-rnd();
354    newball->offside = 0;
355    newball->bounced = FALSE;
356    newball->radius = BALLSIZE;
357    while (r+g+b < 1.7f ) {
358       newball->color.x = r=rnd();
359       newball->color.y = g=rnd();
360       newball->color.z = b=rnd();
361    }
362    newball->justcreated = TRUE;
363 }
364
365 /* Update position of each ball */
366
367 void updateballs(ballman *bman) {
368    register int b,j;
369    vectorf dvect,richting,relspeed,influence;
370    GLfloat squaredist;
371
372    for (b=0;b<bman->num_balls;b++) {
373
374       /* apply gravity */
375       bman->balls[b].dir.y -= 0.15f;
376       /* apply movement */
377       addvectors(&bman->balls[b].loc,&bman->balls[b].loc,&bman->balls[b].dir);
378       /* boundary check */
379       if (bman->balls[b].loc.y < bman->balls[b].radius) { /* ball onder bodem? (bodem @ y=0) */
380          if ((bman->balls[b].loc.x < -100.0) || 
381              (bman->balls[b].loc.x > 100.0) ||
382              (bman->balls[b].loc.z < -100.0) ||
383              (bman->balls[b].loc.z > 100.0)) {
384             if (bman->balls[b].loc.y < -1000.0)
385               createball(&bman->balls[b]);
386          } else {
387             bman->balls[b].loc.y = bman->balls[b].radius  + (bman->balls[b].radius - bman->balls[b].loc.y);
388             bman->balls[b].dir.y = -bman->balls[b].dir.y;
389             if (bman->balls[b].offside) {
390                bman->balls[b].bounced = TRUE; /* temporary disable painting ball */
391                scalevector(&bman->balls[b].dir,&bman->balls[b].dir,0.80f);
392                if (squaremagnitude(&bman->balls[b].dir) < 0.08f) {
393                   createball(&bman->balls[b]);
394                }
395             }
396          }
397          
398       }
399       if (!bman->balls[b].offside) {
400          if (bman->balls[b].loc.x - bman->balls[b].radius < -20.0f) { /* x ondergrens */
401             if (bman->balls[b].loc.y > 41+bman->balls[b].radius)  bman->balls[b].offside=1;
402             else {
403                bman->balls[b].dir.x = -bman->balls[b].dir.x;
404                bman->balls[b].loc.x = -20.0f + bman->balls[b].radius;
405             }
406          }
407          if (bman->balls[b].loc.x + bman->balls[b].radius > 20.0f) { /* x bovengrens */
408             if (bman->balls[b].loc.y > 41+bman->balls[b].radius)  bman->balls[b].offside=1;
409             else {
410                bman->balls[b].dir.x = -bman->balls[b].dir.x;
411                bman->balls[b].loc.x = 20.0f - bman->balls[b].radius;
412             }
413          }
414          if (bman->balls[b].loc.z - bman->balls[b].radius < -20.0f) { /* z ondergrens */
415             if (bman->balls[b].loc.y > 41+bman->balls[b].radius)  bman->balls[b].offside=1;
416             else {
417                bman->balls[b].dir.z = -bman->balls[b].dir.z;
418                bman->balls[b].loc.z = -20.0f + bman->balls[b].radius;
419             }
420          }
421          if (bman->balls[b].loc.z + bman->balls[b].radius > 20.0f) { /* z bovengrens */
422             if (bman->balls[b].loc.y > 41+bman->balls[b].radius)  bman->balls[b].offside=1;
423             else {
424                bman->balls[b].dir.z = -bman->balls[b].dir.z;
425                bman->balls[b].loc.z = 20.0f - bman->balls[b].radius;
426             }
427          }
428       } /* end if !offside */
429    
430       /* check voor stuiteren */
431       for (j=b+1;j<bman->num_balls;j++) {
432          squaredist = (bman->balls[b].radius * bman->balls[b].radius) + (bman->balls[j].radius * bman->balls[j].radius);
433          subvectors(&dvect,&bman->balls[b].loc,&bman->balls[j].loc);
434          if ( squaremagnitude(&dvect) < squaredist ) { /* balls b and j touch */
435             subvectors(&richting,&bman->balls[j].loc,&bman->balls[b].loc);
436             subvectors(&relspeed,&bman->balls[b].dir,&bman->balls[j].dir);
437             /* calc mutual influence direction and magnitude */
438             scalevector(&influence,&richting,(dotproduct(&richting,&relspeed)/squaremagnitude(&richting)));
439             
440             subvectors(&bman->balls[b].dir,&bman->balls[b].dir,&influence);
441             addvectors(&bman->balls[j].dir,&bman->balls[j].dir,&influence);
442             addvectors(&bman->balls[b].loc,&bman->balls[b].loc,&bman->balls[b].dir);
443             addvectors(&bman->balls[j].loc,&bman->balls[j].loc,&bman->balls[j].dir);
444             
445             subvectors(&dvect,&bman->balls[b].loc,&bman->balls[j].loc);
446             while (squaremagnitude(&dvect) < squaredist) {
447                addvectors(&bman->balls[b].loc,&bman->balls[b].loc,&bman->balls[b].dir);
448                addvectors(&bman->balls[j].loc,&bman->balls[j].loc,&bman->balls[j].dir);
449                subvectors(&dvect,&bman->balls[b].loc,&bman->balls[j].loc);
450             }
451          }
452       } /* end for j */
453    } /* end for b */
454 }
455
456
457 /*
458 * explode ball into triangles
459 */
460
461 void createtrisfromball(triman* tman, vectorf *spherev, GLint *spherei, int ind_num, ball *b) {
462    int pos;
463    float explosion;
464    float scale;
465    register int i;
466    vectorf avgdir,dvect;
467
468    tman->scalefac = b->radius;
469    copyvector(&tman->color,&b->color);
470    explosion = 1.0f + tman->explosion * 2.0 * rnd();
471
472    tman->num_tri = ind_num/3;
473    
474    /* reserveer geheugen voor de poly's in een bal */
475    
476    tman->tris = (tri *)malloc(tman->num_tri * sizeof(tri));
477    tman->vertices = (vectorf *)malloc(ind_num * sizeof(vectorf));
478    tman->normals = (vectorf *)malloc(ind_num/3 * sizeof(vectorf));
479    
480    for (i=0; i<(tman->num_tri); i++) {
481       tman->tris[i].far = FALSE;
482       pos = i * 3;
483       /* kopieer elke poly apart naar een tri structure */
484       copyvector(&tman->vertices[pos+0],&spherev[spherei[pos+0]]);
485       copyvector(&tman->vertices[pos+1],&spherev[spherei[pos+1]]);
486       copyvector(&tman->vertices[pos+2],&spherev[spherei[pos+2]]);
487       /* Calculate average direction of shrapnel */
488       addvectors(&avgdir,&tman->vertices[pos+0],&tman->vertices[pos+1]);
489       addvectors(&avgdir,&avgdir,&tman->vertices[pos+2]);
490       scalevector(&avgdir,&avgdir,0.33333);
491       
492       /* should normalize first, NYI */
493       copyvector(&tman->normals[i],&avgdir);
494      
495       /* copy de lokatie */
496       addvectors(&tman->tris[i].loc,&b->loc,&avgdir);
497       /* en translate alle triangles terug naar hun eigen oorsprong */
498       tman->vertices[pos+0].x -= avgdir.x;
499       tman->vertices[pos+0].y -= avgdir.y;
500       tman->vertices[pos+0].z -= avgdir.z;
501       tman->vertices[pos+1].x -= avgdir.x;
502       tman->vertices[pos+1].y -= avgdir.y;
503       tman->vertices[pos+1].z -= avgdir.z;
504       tman->vertices[pos+2].x -= avgdir.x;
505       tman->vertices[pos+2].y -= avgdir.y;
506       tman->vertices[pos+2].z -= avgdir.z;
507       /* alwaar opschaling plaatsvindt */
508       scale = b->radius * 2;
509       scalevector(&tman->vertices[pos+0],&tman->vertices[pos+0],scale);
510       scalevector(&tman->vertices[pos+1],&tman->vertices[pos+1],scale);
511       scalevector(&tman->vertices[pos+2],&tman->vertices[pos+2],scale);
512             
513       /* bereken nieuwe richting */
514       scalevector(&tman->tris[i].dir,&avgdir,explosion);
515       dvect.x = 0.1f - 0.2f*rnd();
516       dvect.y = 0.15f - 0.3f*rnd();
517       dvect.z = 0.1f - 0.2f*rnd(); 
518       addvectors(&tman->tris[i].dir,&tman->tris[i].dir,&dvect);
519    }
520 }
521
522
523 /*
524 * update position of each tri
525 */
526
527 void updatetris(triman *t) {
528    int b;
529
530    for (b=0;b<t->num_tri;b++) {
531       /* apply gravity */
532       t->tris[b].dir.y -= 0.1f;
533       /* apply movement */
534       addvectors(&t->tris[b].loc,&t->tris[b].loc,&t->tris[b].dir);
535       /* boundary check */
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.75f); /* dampening */
545          }
546          else {
547             t->tris[b].far = TRUE;
548             continue;
549          }
550       }
551       
552       /* this should be replaced with code that determines 
553        * the correct wall the tri bounces in to. this code sucks.
554        */
555        if ((t->tris[b].loc.x > -21.0f) &
556           (t->tris[b].loc.x < 21.0f) &
557           (t->tris[b].loc.z > -21.0f) &
558           (t->tris[b].loc.z < 21.0f)) { /* in box? */
559          if ((t->tris[b].loc.x > -21.0f) &
560              (t->tris[b].loc.x < 0)) {
561             t->tris[b].loc.x = -21.0f;
562             t->tris[b].dir.x = -t->tris[b].dir.x;
563          }
564          if ((t->tris[b].loc.x < 21.0f) &
565              (t->tris[b].loc.x > 0)) {
566             t->tris[b].loc.x = 21.0f;
567             t->tris[b].dir.x = -t->tris[b].dir.x;
568          }
569          if ((t->tris[b].loc.z > -21.0f) &
570              (t->tris[b].loc.z < 0)) {
571             t->tris[b].loc.z = -21.0f;
572             t->tris[b].dir.z = -t->tris[b].dir.z;
573          }
574          if ((t->tris[b].loc.z < 21.0f) &
575              (t->tris[b].loc.z > 0)) {
576             t->tris[b].loc.z = 21.0f;
577             t->tris[b].dir.z = -t->tris[b].dir.z;
578          }
579       }
580    } /* end for b */
581
582
583              
584 /*
585  * free memory allocated by a tri manager
586  */
587 void freetris(triman *t) {
588    if (!t) return;
589    if (t->tris) free(t->tris);
590    if (t->vertices) free(t->vertices);
591    if (t->normals) free(t->normals);
592    t->tris = NULL;
593    t->vertices = NULL;
594    t->normals = NULL;
595    t->num_tri = 0;
596    t->lifetime = 0;
597 }
598
599
600 /*
601  *load defaults in config structure
602  */
603 void setdefaultconfig(boxed_config *config) {
604   config->numballs = NUMBALLS;
605   config->textures = TRUE;
606   config->transparent = FALSE;
607   config->explosion = 25.0f;
608   config->ballsize = BALLSIZE;
609   config->camspeed = 35.0f;
610 }
611
612
613 /*
614  * draw bottom
615  */ 
616 static void drawfilledbox(boxedstruct *boxed)
617 {   
618    /* draws texture filled box, 
619       top is drawn using the entire texture, 
620       the sides are drawn using the edge of the texture
621     */
622    
623    /* front */
624    glBegin(GL_QUADS);
625    glTexCoord2f(0,1);
626    glVertex3f(-1.0,1.0,1.0);
627    glTexCoord2f(1,1);
628    glVertex3f(1.0,1.0,1.0);
629    glTexCoord2f(1,1);
630    glVertex3f(1.0,-1.0,1.0);
631    glTexCoord2f(0,1);
632    glVertex3f(-1.0,-1.0,1.0);
633    /* rear */
634    glTexCoord2f(0,1);
635    glVertex3f(1.0,1.0,-1.0);
636    glTexCoord2f(1,1);
637    glVertex3f(-1.0,1.0,-1.0);
638    glTexCoord2f(1,1);
639    glVertex3f(-1.0,-1.0,-1.0);
640    glTexCoord2f(0,1);
641    glVertex3f(1.0,-1.0,-1.0);
642    /* left */
643    glTexCoord2f(1,1);
644    glVertex3f(-1.0,1.0,1.0);
645    glTexCoord2f(1,1);
646    glVertex3f(-1.0,-1.0,1.0);
647    glTexCoord2f(0,1);
648    glVertex3f(-1.0,-1.0,-1.0);
649    glTexCoord2f(0,1);
650    glVertex3f(-1.0,1.0,-1.0);
651    /* right */
652    glTexCoord2f(0,1);
653    glVertex3f(1.0,1.0,1.0);
654    glTexCoord2f(1,1);
655    glVertex3f(1.0,1.0,-1.0);
656    glTexCoord2f(1,1);
657    glVertex3f(1.0,-1.0,-1.0);
658    glTexCoord2f(0,1);
659    glVertex3f(1.0,-1.0,1.0);
660    /* top */
661    glTexCoord2f(0.0,0.0);
662    glVertex3f(-1.0,1.0,1.0);
663    glTexCoord2f(0.0,1.0);
664    glVertex3f(-1.0,1.0,-1.0);
665    glTexCoord2f(1.0,1.0);
666    glVertex3f(1.0,1.0,-1.0);
667    glTexCoord2f(1.0,0.0);
668    glVertex3f(1.0,1.0,1.0);
669    /* bottom */
670    glTexCoord2f(0,0);
671    glVertex3f(-1.0,-1.0,1.0);
672    glTexCoord2f(0,1);
673    glVertex3f(-1.0,-1.0,-1.0);
674    glTexCoord2f(1,1);
675    glVertex3f(1.0,-1.0,-1.0);
676    glTexCoord2f(1,0);
677    glVertex3f(1.0,-1.0,1.0);
678    glEnd();
679 }
680
681
682 /*
683  * Draw a box made of lines
684  */ 
685 static void drawbox(boxedstruct *boxed)
686 {
687    /* top */
688    glBegin(GL_LINE_STRIP);
689    glVertex3f(-1.0,1.0,1.0);
690    glVertex3f(-1.0,1.0,-1.0);
691    glVertex3f(1.0,1.0,-1.0);
692    glVertex3f(1.0,1.0,1.0);
693    glVertex3f(-1.0,1.0,1.0);
694    glEnd();
695    /* bottom */
696    glBegin(GL_LINE_STRIP);
697    glVertex3f(-1.0,-1.0,1.0);
698    glVertex3f(1.0,-1.0,1.0);
699    glVertex3f(1.0,-1.0,-1.0);
700    glVertex3f(-1.0,-1.0,-1.0);
701    glVertex3f(-1.0,-1.0,1.0);
702    glEnd();
703    /* connect top & bottom */
704    glBegin(GL_LINES);
705    glVertex3f(-1.0,1.0,1.0);
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);
711    glVertex3f(-1.0,1.0,-1.0);
712    glVertex3f(-1.0,-1.0,-1.0);
713    glEnd();
714 }
715
716
717   
718 /* 
719  * Draw ball
720  */
721 static void drawball(boxedstruct *gp, ball *b)
722 {
723    int i,pos,cnt;
724    GLint *spherei = gp->spherei;
725    vectorf *spherev = gp->spherev;
726    GLfloat col[3];
727    
728    glPushMatrix();
729    
730    glTranslatef(b->loc.x,b->loc.y,b->loc.z);
731    glScalef(b->radius,b->radius,b->radius);
732    glColor3f(b->color.x,b->color.y,b->color.z);
733    col[0] = b->color.x;
734    col[1] = b->color.y;
735    col[2] = b->color.z;
736    glMaterialfv(GL_FRONT, GL_DIFFUSE, col);
737    col[0] *= 0.5;
738    col[1] *= 0.5;
739    col[2] *= 0.5;
740    glMaterialfv(GL_FRONT, GL_EMISSION,col);
741
742    if (!gp->gllists[GLL_BALL]) {
743       glNewList(gp->listobjects + GLL_BALL,GL_COMPILE_AND_EXECUTE);
744       cnt = SPHERE_INDICES/3;
745       for (i=0; i<cnt; i++) {
746          pos = i * 3;
747          glBegin(GL_TRIANGLES);
748          glNormal3f(spherev[spherei[pos+0]].x,spherev[spherei[pos+0]].y,spherev[spherei[pos+0]].z);
749          glVertex3f(spherev[spherei[pos+0]].x,spherev[spherei[pos+0]].y,spherev[spherei[pos+0]].z);
750          glNormal3f(spherev[spherei[pos+1]].x,spherev[spherei[pos+1]].y,spherev[spherei[pos+1]].z);
751          glVertex3f(spherev[spherei[pos+1]].x,spherev[spherei[pos+1]].y,spherev[spherei[pos+1]].z);
752          glNormal3f(spherev[spherei[pos+2]].x,spherev[spherei[pos+2]].y,spherev[spherei[pos+2]].z);
753          glVertex3f(spherev[spherei[pos+2]].x,spherev[spherei[pos+2]].y,spherev[spherei[pos+2]].z);
754          glEnd();
755       }
756       glEndList();
757       gp->gllists[GLL_BALL] = 1;
758    } else {
759       glCallList(gp->listobjects + GLL_BALL);
760    }
761    
762    glPopMatrix();
763 }
764
765     
766 /* 
767  * Draw all triangles in triman
768  */
769 static void drawtriman(triman *t) 
770 {
771    int i,pos;
772    vectorf *spherev = t->vertices;
773    GLfloat col[3];
774    
775    glPushMatrix();
776    glColor3f(t->color.x,t->color.y,t->color.z);
777    col[0] = t->color.x;
778    col[1] = t->color.y;
779    col[2] = t->color.z;
780    glMaterialfv(GL_FRONT, GL_DIFFUSE, col);
781    col[0] *= 0.3;
782    col[1] *= 0.3;
783    col[2] *= 0.3;
784    glMaterialfv(GL_FRONT, GL_EMISSION,col);
785    
786    for (i=0; i<t->num_tri; i++) {
787       pos = i*3;
788       glPushMatrix();
789       glTranslatef(t->tris[i].loc.x,t->tris[i].loc.y,t->tris[i].loc.z);
790       glBegin(GL_TRIANGLES);
791       glNormal3f(t->normals[i].x,t->normals[i].y,t->normals[i].z);
792       glVertex3f(spherev[pos+0].x,spherev[pos+0].y,spherev[pos+0].z);
793       glVertex3f(spherev[pos+1].x,spherev[pos+1].y,spherev[pos+1].z);
794       glVertex3f(spherev[pos+2].x,spherev[pos+2].y,spherev[pos+2].z);
795       glEnd();
796       glPopMatrix();
797    }   
798    glPopMatrix();   
799 }
800       
801 /* 
802  * draw floor pattern
803  */   
804 static void drawpattern(boxedstruct *boxed)
805 {
806    if (!boxed->gllists[GLL_PATTERN]) {
807       glNewList(boxed->listobjects + GLL_PATTERN,GL_COMPILE_AND_EXECUTE);
808       
809       glBegin(GL_LINE_STRIP);
810       glVertex3f(-25.0f, 0.0f, 35.0f);
811       glVertex3f(-15.0f, 0.0f, 35.0f);
812       glVertex3f(-5.0f, 0.0f, 25.0f);
813       glVertex3f(5.0f, 0.0f, 25.0f);
814       glVertex3f(15.0f, 0.0f, 35.0f);
815       glVertex3f(25.0f, 0.0f, 35.0f);
816       glVertex3f(35.0f, 0.0f, 25.0f);
817       glVertex3f(35.0f, 0.0f, 15.0f);
818       glVertex3f(25.0f, 0.0f, 5.0f);
819       glVertex3f(25.0f, 0.0f, -5.0f);
820       glVertex3f(35.0f, 0.0f, -15.0f);
821       glVertex3f(35.0f, 0.0f, -25.0f);
822       glVertex3f(25.0f, 0.0f, -35.0f);
823       glVertex3f(15.0f, 0.0f,-35.0f);
824       glVertex3f(5.0f, 0.0f, -25.0f);
825       glVertex3f(-5.0f, 0.0f, -25.0f);
826       glVertex3f(-15.0f, 0.0f,-35.0f);
827       glVertex3f(-25.0f, 0.0f,-35.0f);
828       glVertex3f(-35.0f, 0.0f, -25.0f);
829       glVertex3f(-35.0f, 0.0f, -15.0f);
830       glVertex3f(-25.0f, 0.0f, -5.0f);
831       glVertex3f(-25.0f, 0.0f, 5.0f);
832       glVertex3f(-35.0f, 0.0f, 15.0f);
833       glVertex3f(-35.0f, 0.0f, 25.0f);
834       glVertex3f(-25.0f, 0.0f, 35.0f);
835       glEnd();
836       
837       glBegin(GL_LINE_STRIP);
838       glVertex3f(-5.0f, 0.0f, 15.0f);
839       glVertex3f(5.0f, 0.0f, 15.0f);
840       glVertex3f(15.0f, 0.0f, 5.0f);
841       glVertex3f(15.0f, 0.0f, -5.0f);
842       glVertex3f(5.0f, 0.0f, -15.0f);
843       glVertex3f(-5.0f, 0.0f, -15.0f);
844       glVertex3f(-15.0f, 0.0f, -5.0f);
845       glVertex3f(-15.0f, 0.0f, 5.0f);
846       glVertex3f(-5.0f, 0.0f, 15.0f);
847       glEnd();
848       glEndList();
849       boxed->gllists[GLL_PATTERN] = 1;
850    } else {
851       glCallList(boxed->listobjects + GLL_PATTERN);
852    }    
853 }
854       
855       
856 /*
857  * main rendering loop
858  */
859 static void draw(ModeInfo * mi)
860 {
861    boxedstruct *gp = &boxed[MI_SCREEN(mi)];
862    vectorf v1;
863    GLfloat dcam;
864    int dx, dz;
865    int i;   
866    
867    GLfloat dgray[4] = {0.3f, 0.3f, 0.3f, 1.0f};
868    GLfloat black[4] = {0.0f, 0.0f, 0.0f, 1.0f};
869    GLfloat lblue[4] = {0.4f,0.6f,1.0f };
870    
871    GLfloat l0_ambient[] =    {0.0, 0.0, 0.0, 1.0};
872    GLfloat l0_specular[] =    {1.0, 1.0, 1.0, 1.0};
873    GLfloat l0_diffuse[] =    {1.0, 1.0, 1.0, 1.0};
874    GLfloat l0_position[] =  {0.0, 0.0, 0.0, 1.0}; /* w != 0 -> positional light */
875    GLfloat l1_ambient[] =    {0.0, 0.0, 0.0, 1.0};
876    GLfloat l1_specular[] =    {1.0, 1.0, 1.0, 1.0};
877    GLfloat l1_diffuse[] =    {0.5, 0.5, 0.5, 1.0};
878    GLfloat l1_position[] =  {0.0, 1.0, 0.0, 0.0}; /* w = 0 -> directional light */
879    
880    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
881    glLoadIdentity();
882    
883    gp->tic += 0.01f;
884
885    /* rotate camera around (0,0,0), looking at (0,0,0), up is (0,1,0) */
886    dcam = CAMDISTANCE_MIN + (CAMDISTANCE_MAX - CAMDISTANCE_MIN) + (CAMDISTANCE_MAX - CAMDISTANCE_MIN)*cos(gp->tic/CAMDISTANCE_SPEED);
887    v1.x = dcam * sin(gp->tic/gp->cam_x_speed);
888    v1.z = dcam * cos(gp->tic/gp->cam_z_speed);
889    v1.y = CAM_HEIGHT * sin(gp->tic/gp->cam_y_speed) + 1.02 * CAM_HEIGHT;
890    gluLookAt(v1.x,v1.y,v1.z,0.0,0.0,0.0,0.0,1.0,0.0); 
891
892    glLightfv(GL_LIGHT0, GL_AMBIENT, l0_ambient); 
893    glLightfv(GL_LIGHT0, GL_DIFFUSE, l0_diffuse); 
894    glLightfv(GL_LIGHT0, GL_SPECULAR, l0_specular); 
895    glLightfv(GL_LIGHT0, GL_POSITION, l0_position);
896    glLightfv(GL_LIGHT1, GL_AMBIENT, l1_ambient); 
897    glLightfv(GL_LIGHT1, GL_DIFFUSE, l1_diffuse); 
898    glLightfv(GL_LIGHT1, GL_SPECULAR, l1_specular); 
899    glLightfv(GL_LIGHT1, GL_POSITION, l1_position);
900    glEnable(GL_LIGHT0);
901    glEnable(GL_LIGHT1);
902    
903    glFrontFace(GL_CW);
904    
905    glMaterialfv(GL_FRONT, GL_SPECULAR, black);
906    glMaterialfv(GL_FRONT, GL_EMISSION, lblue);
907    glMaterialfv(GL_FRONT,GL_AMBIENT,black);
908    glMaterialf(GL_FRONT, GL_SHININESS, 5.0);
909    
910    
911    /* draw ground grid */
912    /* glDisable(GL_DEPTH_TEST); */
913    glDisable(GL_LIGHTING);
914    
915    glColor3f(0.1,0.1,0.6);
916    for (dx= -2; dx<3; dx++) {
917       for (dz= -2; dz<3; dz++) {
918          glPushMatrix();
919          glTranslatef(dx*30.0f, 0.0f, dz*30.0f);
920          drawpattern(gp);
921          glPopMatrix();
922       }   
923    }
924    
925    /* Set drawing mode for the boxes */
926    glEnable(GL_DEPTH_TEST);
927    glEnable(GL_TEXTURE_2D);
928    glPushMatrix();
929    glColor3f(1.0,1.0,1.0);
930    glScalef(20.0,0.25,20.0);
931    glTranslatef(0.0,2.0,0.0);
932    drawfilledbox(gp);
933    glPopMatrix();
934    glDisable(GL_TEXTURE_2D);
935
936    glPushMatrix();
937    glColor3f(0.2,0.5,0.2);
938    glScalef(20.0,20.0,0.25);
939    glTranslatef(0.0,1.0,81.0);
940    drawbox(gp);
941    glPopMatrix();
942
943    glPushMatrix();
944    glColor3f(0.2,0.5,0.2);
945    glScalef(20.0,20.0,0.25);
946    glTranslatef(0.0,1.0,-81.0);
947    drawbox(gp);
948    glPopMatrix();
949
950    glPushMatrix();
951    glColor3f(0.2,0.5,0.2);
952    glScalef(.25,20.0,20.0);
953    glTranslatef(-81.0,1.0,0.0);
954    drawbox(gp);
955    glPopMatrix();
956
957    glPushMatrix();
958    glColor3f(0.2,0.5,0.2);
959    glScalef(.25,20.0,20.0);
960    glTranslatef(81.0,1.0,0.0);
961    drawbox(gp);
962    glPopMatrix();
963
964    glEnable(GL_LIGHTING);
965    
966    glMaterialfv(GL_FRONT, GL_DIFFUSE, dgray);
967    glMaterialfv(GL_FRONT, GL_EMISSION, black); /* turn it off before painting the balls */
968
969    /* move the balls and shrapnel */
970    updateballs(&gp->bman);
971
972    glFrontFace(GL_CCW);
973    for (i=0;i<gp->bman.num_balls;i++) {
974       if (gp->bman.balls[i].justcreated) {
975          gp->bman.balls[i].justcreated = FALSE;
976          freetris(&gp->tman[i]);
977       }
978       if ((gp->bman.balls[i].bounced) & (gp->tman[i].vertices == NULL)) {
979          createtrisfromball(&gp->tman[i],gp->spherev,gp->spherei,SPHERE_INDICES,&gp->bman.balls[i]);
980       }
981       if (gp->bman.balls[i].bounced) {
982          updatetris(&gp->tman[i]);
983          glDisable(GL_CULL_FACE);
984          drawtriman(&gp->tman[i]);
985          glEnable(GL_CULL_FACE);
986       } else {
987          drawball(gp, &gp->bman.balls[i]);
988       }
989    }
990       
991    glFlush();
992 }
993
994
995
996 /* 
997  * new window size or exposure 
998  */
999 void reshape_boxed(ModeInfo *mi, int width, int height)
1000 {
1001    GLfloat     h = (GLfloat) height / (GLfloat) width;
1002    
1003    glViewport(0, 0, (GLint) width, (GLint) height);
1004    glMatrixMode(GL_PROJECTION);
1005    glLoadIdentity();
1006    gluPerspective(50.0,1/h,2.0,1000.0);
1007    glMatrixMode (GL_MODELVIEW);
1008    
1009    glLineWidth(1);
1010    glPointSize(1);   
1011 }
1012
1013
1014 static void
1015 pinit(ModeInfo * mi)
1016 {
1017    boxedstruct *gp = &boxed[MI_SCREEN(mi)];
1018    ballman *bman;
1019    int i,texpixels;
1020    char *texpixeldata;
1021    char *texpixeltarget;
1022
1023    glShadeModel(GL_SMOOTH);
1024    glClearDepth(1.0);
1025    glClearColor(0.0,0.05,0.1,0.0);
1026    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1027    
1028    /* Load configuration */
1029    setdefaultconfig(&gp->config);
1030    
1031    bman = &gp->bman;
1032    
1033    bman->balls = (ball *)malloc(gp->config.numballs * sizeof(ball));
1034    bman->num_balls = gp->config.numballs;
1035    bman->ballsize = gp->config.ballsize;
1036    bman->explosion = gp->config.explosion;
1037    
1038    gp->tman = (triman *)malloc(bman->num_balls * sizeof(triman));
1039    memset(gp->tman,0,bman->num_balls * sizeof(triman));
1040    
1041    for(i=0;i<bman->num_balls;i++) {
1042       gp->tman[i].explosion = (float) (((int)gp->config.explosion) / 15.0f );
1043       gp->tman[i].vertices = NULL;
1044       gp->tman[i].normals = NULL;
1045       gp->tman[i].tris = NULL;
1046       createball(&bman->balls[i]);
1047       bman->balls[i].loc.y *= rnd();
1048    }
1049
1050    generatesphere();
1051    
1052    glEnable(GL_CULL_FACE);
1053    glEnable(GL_LIGHTING);
1054
1055    /* define cam path */
1056    gp->cam_x_speed = 1.0f/((float)gp->config.camspeed/50.0 + rnd()*((float)gp->config.camspeed/50.0));
1057    gp->cam_z_speed = 1.0f/((float)gp->config.camspeed/50.0 + rnd()*((float)gp->config.camspeed/50.0));
1058    gp->cam_y_speed = 1.0f/((float)gp->config.camspeed/250.0 + rnd()*((float)gp->config.camspeed/250.0));
1059    if (rnd() < 0.5f) gp->cam_x_speed = -gp->cam_x_speed;
1060    if (rnd() < 0.5f) gp->cam_z_speed = -gp->cam_z_speed;
1061    
1062    
1063    gp->tex1 = (char *)malloc(3*width*height*sizeof(GLuint));
1064    texpixels = 256*256; /*width*height;*/
1065    texpixeldata = header_data;
1066    texpixeltarget = gp->tex1;
1067    for (i=0; i < texpixels; i++) {
1068         HEADER_PIXEL(texpixeldata,texpixeltarget);
1069         texpixeltarget += 3;
1070    }
1071    
1072    
1073    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
1074    gluBuild2DMipmaps(GL_TEXTURE_2D, 3, 256, 256,
1075                      GL_RGB, GL_UNSIGNED_BYTE, gp->tex1);
1076    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1077    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
1078    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
1079    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1080    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1081    
1082 }
1083
1084  
1085
1086 void
1087 init_boxed(ModeInfo * mi)
1088 {
1089    int screen = MI_SCREEN(mi);
1090    
1091    /* Colormap    cmap; */
1092    /* Boolean     rgba, doublebuffer, cmap_installed; */
1093    boxedstruct *gp;
1094
1095    if (boxed == NULL) {
1096       if ((boxed = (boxedstruct *) calloc(MI_NUM_SCREENS(mi),sizeof (boxedstruct))) == NULL) return;
1097    }
1098    gp = &boxed[screen];
1099    gp->window = MI_WINDOW(mi);
1100    
1101    if ((gp->glx_context = init_GL(mi)) != NULL) {
1102       reshape_boxed(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
1103       glDrawBuffer(GL_BACK);
1104       if (!glIsList(gp->listobjects))   {
1105          gp->listobjects = glGenLists(3);
1106          gp->gllists[0] = 0;
1107          gp->gllists[1] = 0;
1108          gp->gllists[2] = 0;
1109       }
1110       pinit(mi);
1111    } else {
1112       MI_CLEARWINDOW(mi);
1113    }
1114 }
1115
1116
1117 void
1118 draw_boxed(ModeInfo * mi)
1119 {
1120    boxedstruct *gp = &boxed[MI_SCREEN(mi)];
1121    Display    *display = MI_DISPLAY(mi);
1122    Window      window = MI_WINDOW(mi);
1123    
1124    if (!gp->glx_context)
1125      return;
1126    
1127    glDrawBuffer(GL_BACK);
1128    
1129    glXMakeCurrent(display, window, *(gp->glx_context));
1130    draw(mi);
1131    
1132    if (mi->fps_p) do_fps (mi);
1133    glFinish();
1134    glXSwapBuffers(display, window);
1135 }
1136
1137 void
1138 release_boxed(ModeInfo * mi)
1139 {
1140    if (boxed != NULL) {
1141       int screen;
1142       
1143       for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
1144          boxedstruct *gp = &boxed[screen];
1145          
1146          if (gp->glx_context) {
1147             /* Display lists MUST be freed while their glXContext is current. */
1148             glXMakeCurrent(MI_DISPLAY(mi), gp->window, *(gp->glx_context));
1149             
1150             /*if (glIsList(gp->gear1))
1151               glDeleteLists(gp->gear1, 1);
1152             if (glIsList(gp->gear2))
1153               glDeleteLists(gp->gear2, 1);
1154             if (glIsList(gp->gear3))
1155               glDeleteLists(gp->gear3, 1);
1156             if (glIsList(gp->gear_inner))
1157               glDeleteLists(gp->gear_inner, 1);
1158             if (glIsList(gp->gear_outer))
1159               glDeleteLists(gp->gear_outer, 1);
1160              */
1161             
1162             /* TODO 
1163              * free all trimans
1164              * free all balls
1165              * free all sphere indices & vertices
1166              */
1167             
1168          }
1169       }
1170       (void) free((void *) boxed);
1171       boxed = NULL;
1172    }
1173    FreeAllGL(mi);
1174 }
1175
1176
1177 /*********************************************************/
1178
1179 #endif