http://packetstormsecurity.org/UNIX/admin/xscreensaver-4.01.tar.gz
[xscreensaver] / hacks / glx / sballs.c
1 /* sballs --- balls spinning like crazy in GL */
2
3 #if !defined( lint ) && !defined( SABER )
4 static const char sccsid[] = "@(#)sballs.c      5.02 2001/03/10 xlockmore";
5 #endif
6
7 /* Copyright (c) E. Lassauge, 2001. */
8
9 /*
10  * Permission to use, copy, modify, and distribute this software and its
11  * documentation for any purpose and without fee is hereby granted,
12  * provided that the above copyright notice appear in all copies and that
13  * both that copyright notice and this permission notice appear in
14  * supporting documentation.
15  *
16  * This file is provided AS IS with no warranties of any kind.  The author
17  * shall have no liability with respect to the infringement of copyrights,
18  * trade secrets or any patents by this file or any part thereof.  In no
19  * event will the author be liable for any lost revenue or profits or
20  * other special, indirect and consequential damages.
21  *
22  * The original code for this mode was written by 
23  * Mustata Bogdan (LoneRunner) <lonerunner@planetquake.com>
24  * and can be found at http://www.cfxweb.net/lonerunner/
25  *
26  * Eric Lassauge  (November-07-2000) <lassauge@mail.dotcom.fr>
27  *                                  http://lassauge.free.fr/linux.html
28  *
29  * REVISION HISTORY:
30  *
31  * E.Lassauge - 03-Oct-2001:
32  *      - minor bugfixes - get ready for xscreensaver
33  * E.Lassauge - 09-Mar-2001:
34  *      - get rid of my framerate options to use showfps
35  * E.Lassauge - 28-Nov-2000:
36  *      - add handling of polyhedrons (like in ico)
37  *      - modified release part to add freeing of GL objects
38  * E.Lassauge - 14-Nov-2000:
39  *      - use new common xpm_to_ximage function
40  *
41  */
42
43 #ifdef STANDALONE       /* xscreensaver mode */
44 #define PROGCLASS       "Sballs"
45 #define HACK_INIT       init_sballs
46 #define HACK_DRAW       draw_sballs
47 #define HACK_RESHAPE    reshape_sballs
48 #define sballs_opts     xlockmore_opts
49 #define DEFAULTS        "*delay:        10000 \n" \
50                         "*size:             0 \n" \
51                         "*cycles:          10 \n" \
52                         "*object:           0 \n" \
53                         "*trackmouse:   False \n" \
54                         "*showFPS:      False \n" \
55                         "*wireframe:    False \n" \
56                         "*texture:      True  \n"
57
58 #define MODE_sballs
59 #include "xlockmore.h"          /* from the xscreensaver distribution */
60 #else                           /* !STANDALONE */
61 #include "xlock.h"              /* from the xlockmore distribution */
62 #include "visgl.h"
63 #endif                          /* !STANDALONE */
64
65 #ifdef MODE_sballs
66
67 #define MINSIZE         32      /* minimal viewport size */
68 #define FRAME           50      /* frame count interval */
69 #define MAX_OBJ         8       /* number of 3D objects */
70
71 #include <GL/gl.h>
72 #include <GL/glx.h>
73 #include <GL/glu.h>
74
75 #if defined( USE_XPM ) || defined( USE_XPMINC ) || defined( HAVE_XPM )
76 /* USE_XPM & USE_XPMINC in xlock mode ; HAVE_XPM in xscreensaver mode */
77 #include "xpm-ximage.h"
78 #define I_HAVE_XPM
79
80 #ifdef STANDALONE
81 #include "../images/sball.xpm"
82 #include "../images/sball-bg.xpm"
83 #else /* !STANDALONE */
84 #include "pixmaps/sball.xpm"
85 #include "pixmaps/sball-bg.xpm"
86 #endif /* !STANDALONE */
87 #endif /* HAVE_XPM */
88
89 /* Manage option vars */
90 #define DEF_TEXTURE     "True"
91 #define DEF_TRACKMOUSE  "False"
92 #define DEF_OBJECT      "2"
93 #define DEF_OBJECT_INDX 2
94 static Bool do_texture;
95 static Bool do_trackmouse;
96 static int  object;
97 static int  spheres;
98
99 static XrmOptionDescRec opts[] = {
100     {(char *) "-texture", (char *) ".sballs.texture", XrmoptionNoArg, (caddr_t) "on"},
101     {(char *) "+texture", (char *) ".sballs.texture", XrmoptionNoArg, (caddr_t) "off"},
102     {(char *) "-trackmouse", (char *) ".sballs.trackmouse", XrmoptionNoArg, (caddr_t) "on"},
103     {(char *) "+trackmouse", (char *) ".sballs.trackmouse", XrmoptionNoArg, (caddr_t) "off"},
104     {(char *) "-object", (char *) ".sballs.object", XrmoptionSepArg, (caddr_t) NULL},
105
106 };
107
108 static argtype vars[] = {
109     {(caddr_t *) & do_texture, (char *) "texture", (char *) "Texture", (char *) DEF_TEXTURE, t_Bool},
110     {(caddr_t *) & do_trackmouse, (char *) "trackmouse", (char *) "TrackMouse", (char *) DEF_TRACKMOUSE, t_Bool},
111     {(caddr_t *) & object, (char *) "object", (char *) "Object", (char *) DEF_OBJECT, t_Int},
112
113 };
114
115 static OptionStruct desc[] = {
116     /*{(char *) "-count spheres", (char *) "set number of spheres"},*/
117     /*{(char *) "-cycles speed", (char *) "set ball speed value"},*/
118     {(char *) "-/+texture", (char *) "turn on/off texturing"},
119     {(char *) "-/+trackmouse", (char *) "turn on/off the tracking of the mouse"},
120     {(char *) "-object num", (char *) "number of the 3D object (0 means random)"},
121 };
122
123 ModeSpecOpt sballs_opts =
124  { sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc };
125
126 #ifdef USE_MODULES
127 ModStruct sballs_description =
128     { "sballs", "init_sballs", "draw_sballs", "release_sballs",
129     "draw_sballs", "change_sballs", (char *) NULL, &sballs_opts,
130     /*
131     delay,count,cycles,size,ncolors,sat
132      */
133     10000, 0, 10, 400, 64, 1.0, "",
134     "balls spinning like crazy in GL", 0, NULL
135 };
136 #endif /* USE_MODULES */
137
138 /* misc types and defines */
139 #define vinit(a,i,j,k) {\
140   (a)[0]=i;\
141   (a)[1]=j;\
142   (a)[2]=k;\
143 }
144 typedef float vec_t;
145 typedef vec_t vec3_t[3];
146
147 #define MAX_BALLS       20
148
149 /* the mode struct, contains all per screen variables */
150 typedef struct {
151     GLint WIDTH, HEIGHT;        /* display dimensions */
152     GLXContext *glx_context;
153
154
155     XImage *btexture;           /* back texture image bits */
156     XImage *ftexture;           /* face texture image bits */
157     GLuint backid;              /* back texture id: GL world */
158     GLuint faceid;              /* face texture id: GL world */
159
160     vec3_t eye;
161     vec3_t rot;
162     vec3_t rotm;
163     int speed;
164     float radius[MAX_BALLS];
165
166 } sballsstruct;
167
168 /* array of sballsstruct indexed by screen number */
169 static sballsstruct *sballs = (sballsstruct *) NULL;
170
171 /* lights */
172 static float LightAmbient[]=   { 1.0f, 1.0f, 1.0f, 1.0f };
173 static float LightDiffuse[]=   { 1.0f, 1.0f, 1.0f, 1.0f };
174 static float LightPosition[]=  { 0.0f, 0.0f, 4.0f, 1.0f };
175
176 /* structure of the polyhedras */
177 typedef struct {
178         const char *longname;   /* long name of object */
179         const char *shortname;  /* short name of object */
180         int         numverts;   /* number of vertices */
181         float       radius;     /* radius */
182         vec3_t      v[MAX_BALLS];/* the vertices */
183 } Polyinfo;
184
185 static Polyinfo polygons[] =
186 {
187
188 /* 0: objtetra - structure values for tetrahedron */
189         {
190                 "tetrahedron", "tetra",         /* long and short names */
191                 4,                              /* number of vertices */
192                 0.8,
193                 {                               /* vertices (x,y,z) */
194                         /* all points must be within radius 2 of the origin */
195 #define T 1.0
196                         {T, T, T},
197                         {T, -T, -T},
198                         {-T, T, -T},
199                         {-T, -T, T},
200 #undef T
201                 }
202         },
203
204 /* 1: objcube - structure values for cube */
205
206         {
207                 "hexahedron", "cube",           /* long and short names */
208                 8,                              /* number of vertices, edges, and faces */
209                 0.6,
210                 {                               /* vertices (x,y,z) */
211                         /* all points must be within radius 2 of the origin */
212 #define T 1.0
213                         {T, T, T},
214                         {T, T, -T},
215                         {T, -T, -T},
216                         {T, -T, T},
217                         {-T, T, T},
218                         {-T, T, -T},
219                         {-T, -T, -T},
220                         {-T, -T, T},
221 #undef T
222                 }
223         },
224
225 /* 2: objocta - structure values for octahedron */
226
227         {
228                 "octahedron", "octa",   /* long and short names */
229                 6,                      /* number of vertices */
230                 0.6,
231                 {                       /* vertices (x,y,z) */
232                         /* all points must be within radius 2 of the origin */
233 #define T 1.5
234                         {T, 0, 0},
235                         {-T, 0, 0},
236                         {0, T, 0},
237                         {0, -T, 0},
238                         {0, 0, T},
239                         {0, 0, -T},
240 #undef T
241                 }
242         },
243 /* 3: objdodec - structure values for dodecahedron */
244
245         {
246                 "dodecahedron", "dodeca",       /* long and short names */
247                 20,                             /* number of vertices */
248                 0.35,
249                 {                               /* vertices (x,y,z) */
250                         /* all points must be within radius 2 of the origin */
251                         {0.000000, 0.500000, 1.000000},
252                         {0.000000, -0.500000, 1.000000},
253                         {0.000000, -0.500000, -1.000000},
254                         {0.000000, 0.500000, -1.000000},
255                         {1.000000, 0.000000, 0.500000},
256                         {-1.000000, 0.000000, 0.500000},
257                         {-1.000000, 0.000000, -0.500000},
258                         {1.000000, 0.000000, -0.500000},
259                         {0.500000, 1.000000, 0.000000},
260                         {-0.500000, 1.000000, 0.000000},
261                         {-0.500000, -1.000000, 0.000000},
262                         {0.500000, -1.000000, 0.000000},
263                         {0.750000, 0.750000, 0.750000},
264                         {-0.750000, 0.750000, 0.750000},
265                         {-0.750000, -0.750000, 0.750000},
266                         {0.750000, -0.750000, 0.750000},
267                         {0.750000, -0.750000, -0.750000},
268                         {0.750000, 0.750000, -0.750000},
269                         {-0.750000, 0.750000, -0.750000},
270                         {-0.750000, -0.750000, -0.750000},
271                 }
272         },
273
274 /* 4: objicosa - structure values for icosahedron */
275
276         {
277                 "icosahedron", "icosa",         /* long and short names */
278                 12,                             /* number of vertices */
279                 0.4,
280                 {                               /* vertices (x,y,z) */
281                         /* all points must be within radius 2 of the origin */
282                         {0.00000000, 0.00000000, -0.95105650},
283                         {0.00000000, 0.85065080, -0.42532537},
284                         {0.80901698, 0.26286556, -0.42532537},
285                         {0.50000000, -0.68819095, -0.42532537},
286                         {-0.50000000, -0.68819095, -0.42532537},
287                         {-0.80901698, 0.26286556, -0.42532537},
288                         {0.50000000, 0.68819095, 0.42532537},
289                         {0.80901698, -0.26286556, 0.42532537},
290                         {0.00000000, -0.85065080, 0.42532537},
291                         {-0.80901698, -0.26286556, 0.42532537},
292                         {-0.50000000, 0.68819095, 0.42532537},
293                         {0.00000000, 0.00000000, 0.95105650}
294                 }
295         },
296
297 /* 5: objplane - structure values for plane */
298
299         {
300                 "plane", "plane",       /* long and short names */
301                 4,                      /* number of vertices */
302                 0.7,
303                 {                       /* vertices (x,y,z) */
304                         /* all points must be within radius 2 of the origin */
305 #define T 1.1
306                         {T, 0, 0},
307                         {-T, 0, 0},
308                         {0, T, 0},
309                         {0, -T, 0},
310 #undef T
311                 }
312         },
313
314 /* 6: objpyr - structure values for pyramid */
315
316         {
317                 "pyramid", "pyramid",   /* long and short names */
318                 5,                      /* number of vertices */ 
319                 0.5,
320                 {                       /* vertices (x,y,z) */
321                         /* all points must be within radius 1 of the origin */
322 #define T 1.0
323                         {T, 0, 0},
324                         {-T, 0, 0},
325                         {0, T, 0},
326                         {0, -T, 0},
327                         {0, 0, T},
328 #undef T
329                 }
330         },
331
332 /* 7: objstar - structure values for octahedron star (stellated octahedron?) */
333         {
334                 "star", "star", /* long and short names */
335                 8,              /* number of vertices */
336                 0.7,
337                 {               /* vertices (x,y,z) */
338                         /* all points must be within radius 1 of the origin */
339 #define T 0.9
340                         {T, T, T},
341                         {T, -T, -T},
342                         {-T, T, -T},
343                         {-T, -T, T},
344                         {-T, -T, -T},
345                         {-T, T, T},
346                         {T, -T, T},
347                         {T, T, -T},
348 #undef T
349                 }
350         },
351
352 };
353
354
355
356
357 /*
358  *-----------------------------------------------------------------------------
359  *-----------------------------------------------------------------------------
360  *    Misc funcs.
361  *-----------------------------------------------------------------------------
362  *-----------------------------------------------------------------------------
363  */
364
365 static void clamp(vec3_t v)
366 {
367     int i;
368
369     for (i = 0; i < 3; i ++)
370         if (v[i] > 360 || v[i] < -360)
371             v[i] = 0;
372 }
373
374 /* track the mouse in a joystick manner : not perfect but it works */
375 static void trackmouse(ModeInfo * mi)
376 {
377     sballsstruct *sb = &sballs[MI_SCREEN(mi)];
378     /* we keep static values (not per screen) for the mouse stuff: in general you have only one mouse :-> */
379     static int max[2] = { 0, 0 };
380     static int min[2] = { 0x7fffffff, 0x7fffffff }, center[2];
381     Window r, c;
382     int rx, ry, cx, cy;
383     unsigned int m;
384
385     (void) XQueryPointer(MI_DISPLAY(mi), MI_WINDOW(mi),
386                          &r, &c, &rx, &ry, &cx, &cy, &m);
387
388     if (max[0] < cx)
389         max[0] = cx;
390     if (min[0] > cx)
391         min[0] = cx;
392     center[0] = (max[0] + min[0]) / 2;
393
394     if (max[1] < cy)
395         max[1] = cy;
396     if (min[1] > cy)
397         min[1] = cy;
398     center[1] = (max[1] + min[1]) / 2;
399
400     if (fabs(center[0] - (float) cx) > 0.1 * (max[0] - min[0]))
401         sb->rot[0] -= ((center[0] - (float) cx) / (max[0] - min[0]) * 180.0f) / 200.0f;
402     if (fabs(center[1] - (float) cy) > 0.1 * (max[1] - min[1]))
403         sb->rot[1] -= ((center[1] - (float) cy) / (max[1] - min[1]) * 180.0f) / 200.0f;;
404     clamp(sb->rot);
405
406     /* oops: can't get those buttons */
407     if (m & Button4Mask)
408         sb->speed++;
409     if (m & Button5Mask)
410         sb->speed--;
411
412 }
413
414 /* initialise textures */
415 static void inittextures(ModeInfo * mi)
416 {
417     sballsstruct *sb = &sballs[MI_SCREEN(mi)];
418
419 #if defined( I_HAVE_XPM )
420     if (do_texture) {
421
422         glGenTextures(1, &sb->backid);
423 #ifdef HAVE_GLBINDTEXTURE
424         glBindTexture(GL_TEXTURE_2D, sb->backid);
425 #endif /* HAVE_GLBINDTEXTURE */
426
427         sb->btexture = xpm_to_ximage(MI_DISPLAY(mi),
428                                      MI_VISUAL(mi),
429                                      MI_COLORMAP(mi),
430                                      sball_bg);
431         if (!(sb->btexture)) {
432             (void) fprintf(stderr, "Error reading the background texture.\n");
433             glDeleteTextures(1, &sb->backid);
434             do_texture = False;
435             sb->faceid = 0;       /* default textures */
436             sb->backid = 0;
437             return;
438         }
439
440         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
441         clear_gl_error();
442         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
443                      sb->btexture->width, sb->btexture->height, 0,
444                      GL_RGBA, GL_UNSIGNED_BYTE, sb->btexture->data);
445         check_gl_error("texture");
446
447         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
448         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
449
450         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
451         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
452
453         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
454
455         glGenTextures(1, &sb->faceid);
456 #ifdef HAVE_GLBINDTEXTURE
457         glBindTexture(GL_TEXTURE_2D, sb->faceid);
458 #endif /* HAVE_GLBINDTEXTURE */
459
460         sb->ftexture = xpm_to_ximage(MI_DISPLAY(mi),
461                                      MI_VISUAL(mi),
462                                      MI_COLORMAP(mi),
463                                      sball);
464         if (!(sb->ftexture)) {
465             (void) fprintf(stderr, "Error reading the face texture.\n");
466             glDeleteTextures(1, &sb->faceid);
467             sb->faceid = 0;
468             return;
469         }
470
471         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
472         clear_gl_error();
473         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
474                      sb->ftexture->width, sb->ftexture->height, 0,
475                      GL_RGBA, GL_UNSIGNED_BYTE, sb->ftexture->data);
476         check_gl_error("texture");
477
478         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
479         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
480
481         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
482         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
483
484         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
485     }
486     else
487     {
488         sb->faceid = 0;       /* default textures */
489         sb->backid = 0;
490     }
491 #else /* !I_HAVE_XPM */
492   do_texture = False;
493   sb->faceid = 0;       /* default textures */
494   sb->backid = 0;
495 #endif /* !I_HAVE_XPM */
496 }
497
498 static void drawSphere(ModeInfo * mi,int sphere_num)
499 {
500   sballsstruct *sb = &sballs[MI_SCREEN(mi)];
501   float x = polygons[object].v[sphere_num][0];
502   float y = polygons[object].v[sphere_num][1];
503   float z = polygons[object].v[sphere_num][2];
504   int numMajor = 10;
505   int numMinor = 10;
506   float radius = sb->radius[sphere_num];
507   double majorStep = (M_PI / numMajor);
508   double minorStep = (2.0 * M_PI / numMinor);
509   int i, j;
510
511   glPushMatrix();
512   glTranslatef(x, y, z);
513
514   glColor4f(1, 1, 1, 1);
515   for (i = 0; i < numMajor; ++i)
516   {
517     double a = i * majorStep;
518     double b = a + majorStep;
519     double r0 = radius * sin(a);
520     double r1 = radius * sin(b);
521     GLfloat z0 = radius * cos(a);
522     GLfloat z1 = radius * cos(b);
523
524     glBegin(MI_IS_WIREFRAME(mi) ? GL_LINE_STRIP: GL_TRIANGLE_STRIP);
525     for (j = 0; j <= numMinor; ++j)
526         {
527       double c = j * minorStep;
528       GLfloat x = cos(c);
529       GLfloat y = sin(c);
530
531       glNormal3f((x * r0) / radius, (y * r0) / radius, z0 / radius);
532       glTexCoord2f(j / (GLfloat) numMinor, i / (GLfloat) numMajor);
533       glVertex3f(x * r0, y * r0, z0);
534
535       glNormal3f((x * r1) / radius, (y * r1) / radius, z1 / radius);
536       glTexCoord2f(j / (GLfloat) numMinor, (i + 1) / (GLfloat) numMajor);
537       glVertex3f(x * r1, y * r1, z1);
538     }
539     glEnd();
540   }
541
542   glPopMatrix();
543 }
544
545 /*
546  *-----------------------------------------------------------------------------
547  *-----------------------------------------------------------------------------
548  *    GL funcs.
549  *-----------------------------------------------------------------------------
550  *-----------------------------------------------------------------------------
551  */
552
553 #ifndef STANDALONE
554 static void Reshape(ModeInfo * mi)
555 #else
556 void reshape_sballs(ModeInfo * mi, int width, int height)
557 #endif
558 {
559
560     sballsstruct *sb = &sballs[MI_SCREEN(mi)];
561     int size = MI_SIZE(mi);
562
563     /* Viewport is specified size if size >= MINSIZE && size < screensize */
564     if (size <= 1) {
565         sb->WIDTH = MI_WIDTH(mi);
566         sb->HEIGHT = MI_HEIGHT(mi);
567     } else if (size < MINSIZE) {
568         sb->WIDTH = MINSIZE;
569         sb->HEIGHT = MINSIZE;
570     } else {
571         sb->WIDTH = (size > MI_WIDTH(mi)) ? MI_WIDTH(mi) : size;
572         sb->HEIGHT = (size > MI_HEIGHT(mi)) ? MI_HEIGHT(mi) : size;
573     }
574     glViewport((MI_WIDTH(mi) - sb->WIDTH) / 2, (MI_HEIGHT(mi) - sb->HEIGHT) / 2, sb->WIDTH, sb->HEIGHT);
575     glMatrixMode(GL_PROJECTION);
576     glLoadIdentity();
577     gluPerspective(55.0, (float)sb->WIDTH / (float) sb->HEIGHT, 1.0, 300.0);
578
579     glMatrixMode(GL_MODELVIEW);
580
581 }
582
583 static void Draw(ModeInfo * mi)
584 {
585     sballsstruct *sb = &sballs[MI_SCREEN(mi)];
586     int sphere;
587
588     if (do_trackmouse && !MI_IS_ICONIC(mi))
589         trackmouse(mi);
590
591     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
592
593     glPushMatrix();
594     glEnable(GL_DEPTH_TEST);
595
596     /* move eyes */
597     glTranslatef (-sb->eye[0], -sb->eye[1], -sb->eye[2]);
598
599     /* draw background */
600     if (do_texture)
601     {
602        glEnable(GL_LIGHTING);
603        glEnable(GL_TEXTURE_2D);
604        glColor3f(1, 1, 1);
605 #ifdef HAVE_GLBINDTEXTURE
606        glBindTexture(GL_TEXTURE_2D, sb->backid);
607 #endif /* HAVE_GLBINDTEXTURE */
608     }
609     else
610     {
611        glColor3f(0, 0, 0);
612     }
613     glBegin(GL_QUAD_STRIP);
614     glNormal3f(0, 0, 1); glTexCoord2f(0,0); glVertex3f(8, 4.1, -4);
615     glNormal3f(0, 0, 1); glTexCoord2f(0,1); glVertex3f(8, -4.1, -4);
616     glNormal3f(0, 0, 1); glTexCoord2f(1,0); glVertex3f(-8, 4.1, -4);
617     glNormal3f(0, 0, 1); glTexCoord2f(1,1); glVertex3f(-8, -4.1, -4);
618     glEnd();
619
620     /* rotate the mouse */
621     glRotatef(sb->rot[0], 1.0f, 0.0f, 0.0f);
622     glRotatef(sb->rot[1], 0.0f, 1.0f, 0.0f);
623     glRotatef(sb->rot[2], 0.0f, 0.0f, 1.0f);
624
625     /* rotate the balls */
626     glRotatef(sb->rotm[0], 1.0f, 0.0f, 0.0f);
627     glRotatef(sb->rotm[1], 0.0f, 1.0f, 0.0f);
628     glRotatef(sb->rotm[2], 0.0f, 0.0f, 1.0f);
629
630     sb->rotm[0] += sb->speed;
631     sb->rotm[1] += -(sb->speed);
632     sb->rotm[2] += 0;
633
634     /* draw the balls */
635     if (do_texture)
636 #ifdef HAVE_GLBINDTEXTURE
637        glBindTexture(GL_TEXTURE_2D, sb->faceid);
638 #endif /* HAVE_GLBINDTEXTURE */
639     else
640        glEnable(GL_LIGHTING);
641     for (sphere=0;sphere<spheres;sphere++)
642     {
643         drawSphere(mi,sphere);
644     }
645
646     glDisable(GL_TEXTURE_2D);
647     glDisable(GL_DEPTH_TEST);
648     glDisable(GL_LIGHTING);
649
650     /* manage framerate display */
651     if (MI_IS_FPS(mi)) do_fps (mi);
652     glPopMatrix();
653 }
654
655
656 static void Init(ModeInfo * mi)
657 {
658     sballsstruct *sb = &sballs[MI_SCREEN(mi)];
659     int i;
660
661     /* Default settings */
662     if (MI_IS_WIREFRAME(mi))
663         do_texture = False;
664     if (do_texture)
665         inittextures(mi);
666     else
667     {
668         sb->btexture = (XImage*) NULL;
669         sb->ftexture = (XImage*) NULL;
670     }
671
672     vinit(sb->eye   ,0.0f, 0.0f, 6.0f);
673     vinit(sb->rot   ,0.0f, 0.0f, 0.0f);
674     vinit(sb->rotm  ,0.0f, 0.0f, 0.0f);
675     sb->speed = MI_CYCLES(mi);
676
677     /* initialise object number */
678     if (object == 0)
679         object = NRAND(MAX_OBJ);
680     if ((object == 0) || (object > MAX_OBJ))
681         object = DEF_OBJECT_INDX;
682     object--;
683
684     /* initialise sphere number */
685     spheres = MI_COUNT(mi);
686     if (MI_COUNT(mi) > polygons[object].numverts)
687         spheres = polygons[object].numverts;
688     if (MI_COUNT(mi) < 1)
689         spheres = polygons[object].numverts;
690     /* initialise sphere radius */
691     for(i=0; i < spheres;i++)
692     {
693 #if RANDOM_RADIUS
694         sb->radius[i] = ((float) LRAND() / (float) MAXRAND);
695         if (sb->radius[i] < 0.3)
696             sb->radius[i] = 0.3;
697         if (sb->radius[i] > 0.7)
698             sb->radius[i] = 0.7;
699 #else
700         sb->radius[i] = polygons[object].radius;
701 #endif
702     }
703
704     if (MI_IS_DEBUG(mi)) {
705         (void) fprintf(stderr,
706                        "%s:\n\tobject=%s\n\tspheres=%d\n\tspeed=%d\n\ttexture=%s\n",
707                        MI_NAME(mi),
708                        polygons[object].shortname,
709                        spheres,
710                        MI_CYCLES(mi),
711                        do_texture ? "on" : "off"
712                         );
713     }
714
715         glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);
716         glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
717         glLightfv(GL_LIGHT1, GL_POSITION,LightPosition);
718         glEnable(GL_LIGHT1);
719
720         glClearColor(0, 0, 0, 0);
721 }
722
723 /*
724  *-----------------------------------------------------------------------------
725  *-----------------------------------------------------------------------------
726  *    Xlock hooks.
727  *-----------------------------------------------------------------------------
728  *-----------------------------------------------------------------------------
729  */
730
731 /*
732  *-----------------------------------------------------------------------------
733  *    Initialize sballs.  Called each time the window changes.
734  *-----------------------------------------------------------------------------
735  */
736
737 void init_sballs(ModeInfo * mi)
738 {
739     sballsstruct *sb;
740
741     if (sballs == NULL) {
742         if ((sballs = (sballsstruct *) calloc(MI_NUM_SCREENS(mi),
743                                           sizeof(sballsstruct))) == NULL)
744             return;
745     }
746     sb = &sballs[MI_SCREEN(mi)];
747
748     if ((sb->glx_context = init_GL(mi)) != NULL) {
749
750 #ifndef STANDALONE
751         Reshape(mi); /* xlock mode */
752 #else
753         reshape_sballs(mi,MI_WIDTH(mi),MI_HEIGHT(mi)); /* xscreensaver mode */
754 #endif
755         glDrawBuffer(GL_BACK);
756         Init(mi);
757
758     } else {
759         MI_CLEARWINDOW(mi);
760     }
761 }
762
763 /*
764  *-----------------------------------------------------------------------------
765  *    Called by the mainline code periodically to update the display.
766  *-----------------------------------------------------------------------------
767  */
768 void draw_sballs(ModeInfo * mi)
769 {
770     Display *display = MI_DISPLAY(mi);
771     Window window = MI_WINDOW(mi);
772     sballsstruct *sb;
773
774     if (sballs == NULL)
775             return;
776     sb = &sballs[MI_SCREEN(mi)];
777
778     MI_IS_DRAWN(mi) = True;
779     if (!sb->glx_context)
780         return;
781
782     glXMakeCurrent(display, window, *(sb->glx_context));
783     Draw(mi);
784 #ifndef STANDALONE
785     Reshape(mi); /* xlock mode */
786 #else
787     reshape_sballs(mi,MI_WIDTH(mi),MI_HEIGHT(mi)); /* xscreensaver mode */
788 #endif
789
790     glFinish();
791     glXSwapBuffers(display, window);
792 }
793
794
795 /*
796  *-----------------------------------------------------------------------------
797  *    The display is being taken away from us.  Free up malloc'ed
798  *      memory and X resources that we've alloc'ed.  Only called
799  *      once, we must zap everything for every screen.
800  *-----------------------------------------------------------------------------
801  */
802
803 void release_sballs(ModeInfo * mi)
804 {
805     int screen;
806
807     if (sballs != NULL) {
808         for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
809             sballsstruct *sb = &sballs[screen];
810             if (sb->btexture)
811             {
812                 glDeleteTextures(1,&sb->backid);
813                 XDestroyImage(sb->btexture);
814             }
815             if (sb->ftexture)
816             {
817                 glDeleteTextures(1,&sb->faceid);
818                 XDestroyImage(sb->ftexture);
819             }
820         }
821         (void) free((void *) sballs);
822         sballs = (sballsstruct *) NULL;
823     }
824     FreeAllGL(mi);
825 }
826
827 void change_sballs(ModeInfo * mi)
828 {
829     sballsstruct *sb;
830
831     if (sballs == NULL)
832             return;
833     sb = &sballs[MI_SCREEN(mi)];
834
835     if (!sb->glx_context)
836         return;
837
838     /* initialise object number */
839     if (object == 0)
840         object = NRAND(MAX_OBJ);
841     if ((object == 0) || (object > MAX_OBJ))
842         object = DEF_OBJECT_INDX;
843     object--;
844
845     /* correct sphere number */
846     spheres = MI_COUNT(mi);
847     if (MI_COUNT(mi) > polygons[object].numverts)
848         spheres = polygons[object].numverts;
849     if (MI_COUNT(mi) < 1)
850         spheres = polygons[object].numverts;
851
852     if (MI_IS_DEBUG(mi)) {
853         (void) fprintf(stderr,
854                        "%s:\n\tobject=%s\n\tspheres=%d\n\tspeed=%d\n\ttexture=%s\n",
855                        MI_NAME(mi),
856                        polygons[object].shortname,
857                        spheres,
858                        MI_CYCLES(mi),
859                        do_texture ? "on" : "off"
860                         );
861     }
862     glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(sb->glx_context));
863
864 }
865 #endif /* MODE_sballs */