1 /* sballs --- balls spinning like crazy in GL */
4 static const char sccsid[] = "@(#)sballs.c 5.02 2001/03/10 xlockmore";
7 /* Copyright (c) E. Lassauge, 2001. */
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.
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.
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/
26 * Eric Lassauge (November-07-2000) <lassauge@users.sourceforge.net>
27 * http://lassauge.free.fr/linux.html
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
43 #ifdef STANDALONE /* xscreensaver mode */
44 #define DEFAULTS "*delay: 30000 \n" \
47 "*showFPS: False \n" \
48 "*wireframe: False \n" \
50 # define refresh_sballs 0
51 # define release_sballs 0
53 #include "xlockmore.h" /* from the xscreensaver distribution */
54 #include "gltrackball.h"
55 #else /* !STANDALONE */
56 #include "xlock.h" /* from the xlockmore distribution */
58 #endif /* !STANDALONE */
62 #define MINSIZE 32 /* minimal viewport size */
63 #define FRAME 50 /* frame count interval */
64 #define MAX_OBJ 8 /* number of 3D objects */
66 #if defined( USE_XPM ) || defined( USE_XPMINC ) || defined( STANDALONE )
67 /* USE_XPM & USE_XPMINC in xlock mode ; HAVE_XPM in xscreensaver mode */
68 # include "xpm-ximage.h"
74 __extension__ /* don't warn about "string length is greater than the length
75 ISO C89 compilers are required to support" when including
76 the following XPM file... */
78 # 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 */
89 /* Manage option vars */
90 #define DEF_TEXTURE "True"
91 #define DEF_OBJECT "0"
92 static Bool do_texture;
96 static XrmOptionDescRec opts[] = {
97 {"-texture", ".sballs.texture", XrmoptionNoArg, "on"},
98 {"+texture", ".sballs.texture", XrmoptionNoArg, "off"},
99 {"-object", ".sballs.object", XrmoptionSepArg, 0},
103 static argtype vars[] = {
104 {&do_texture, "texture", "Texture", DEF_TEXTURE, t_Bool},
105 {&object, "object", "Object", DEF_OBJECT, t_Int},
109 static OptionStruct desc[] = {
110 /*{"-count spheres", "set number of spheres"},*/
111 /*{"-cycles speed", "set ball speed value"},*/
112 {"-/+texture", "turn on/off texturing"},
113 {"-object num", "number of the 3D object (0 means random)"},
116 ENTRYPOINT ModeSpecOpt sballs_opts =
117 { sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc };
120 ModStruct sballs_description =
121 { "sballs", "init_sballs", "draw_sballs", NULL,
122 "draw_sballs", "change_sballs", (char *) NULL, &sballs_opts,
124 delay,count,cycles,size,ncolors,sat
126 10000, 0, 10, 400, 64, 1.0, "",
127 "balls spinning like crazy in GL", 0, NULL
129 #endif /* USE_MODULES */
131 /* misc types and defines */
132 #define vinit(a,i,j,k) {\
138 typedef vec_t vec3_t[3];
142 /* the mode struct, contains all per screen variables */
144 GLint WIDTH, HEIGHT; /* display dimensions */
145 GLXContext *glx_context;
148 XImage *btexture; /* back texture image bits */
149 XImage *ftexture; /* face texture image bits */
150 GLuint backid; /* back texture id: GL world */
151 GLuint faceid; /* face texture id: GL world */
156 float radius[MAX_BALLS];
158 trackball_state *trackball;
163 /* array of sballsstruct indexed by screen number */
164 static sballsstruct *sballs = (sballsstruct *) NULL;
167 static const float LightAmbient[]= { 1.0f, 1.0f, 1.0f, 1.0f };
168 static const float LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f };
169 static const float LightPosition[]= { 0.0f, 0.0f, 4.0f, 1.0f };
171 /* structure of the polyhedras */
173 const char *longname; /* long name of object */
174 const char *shortname; /* short name of object */
175 int numverts; /* number of vertices */
176 float radius; /* radius */
177 vec3_t v[MAX_BALLS];/* the vertices */
180 static const Polyinfo polygons[] =
183 /* 0: objtetra - structure values for tetrahedron */
185 "tetrahedron", "tetra", /* long and short names */
186 4, /* number of vertices */
188 { /* vertices (x,y,z) */
189 /* all points must be within radius 2 of the origin */
199 /* 1: objcube - structure values for cube */
202 "hexahedron", "cube", /* long and short names */
203 8, /* number of vertices, edges, and faces */
205 { /* vertices (x,y,z) */
206 /* all points must be within radius 2 of the origin */
220 /* 2: objocta - structure values for octahedron */
223 "octahedron", "octa", /* long and short names */
224 6, /* number of vertices */
226 { /* vertices (x,y,z) */
227 /* all points must be within radius 2 of the origin */
238 /* 3: objdodec - structure values for dodecahedron */
241 "dodecahedron", "dodeca", /* long and short names */
242 20, /* number of vertices */
244 { /* vertices (x,y,z) */
245 /* all points must be within radius 2 of the origin */
246 {0.000000, 0.500000, 1.000000},
247 {0.000000, -0.500000, 1.000000},
248 {0.000000, -0.500000, -1.000000},
249 {0.000000, 0.500000, -1.000000},
250 {1.000000, 0.000000, 0.500000},
251 {-1.000000, 0.000000, 0.500000},
252 {-1.000000, 0.000000, -0.500000},
253 {1.000000, 0.000000, -0.500000},
254 {0.500000, 1.000000, 0.000000},
255 {-0.500000, 1.000000, 0.000000},
256 {-0.500000, -1.000000, 0.000000},
257 {0.500000, -1.000000, 0.000000},
258 {0.750000, 0.750000, 0.750000},
259 {-0.750000, 0.750000, 0.750000},
260 {-0.750000, -0.750000, 0.750000},
261 {0.750000, -0.750000, 0.750000},
262 {0.750000, -0.750000, -0.750000},
263 {0.750000, 0.750000, -0.750000},
264 {-0.750000, 0.750000, -0.750000},
265 {-0.750000, -0.750000, -0.750000},
269 /* 4: objicosa - structure values for icosahedron */
272 "icosahedron", "icosa", /* long and short names */
273 12, /* number of vertices */
275 { /* vertices (x,y,z) */
276 /* all points must be within radius 2 of the origin */
277 {0.00000000, 0.00000000, -0.95105650},
278 {0.00000000, 0.85065080, -0.42532537},
279 {0.80901698, 0.26286556, -0.42532537},
280 {0.50000000, -0.68819095, -0.42532537},
281 {-0.50000000, -0.68819095, -0.42532537},
282 {-0.80901698, 0.26286556, -0.42532537},
283 {0.50000000, 0.68819095, 0.42532537},
284 {0.80901698, -0.26286556, 0.42532537},
285 {0.00000000, -0.85065080, 0.42532537},
286 {-0.80901698, -0.26286556, 0.42532537},
287 {-0.50000000, 0.68819095, 0.42532537},
288 {0.00000000, 0.00000000, 0.95105650}
292 /* 5: objplane - structure values for plane */
295 "plane", "plane", /* long and short names */
296 4, /* number of vertices */
298 { /* vertices (x,y,z) */
299 /* all points must be within radius 2 of the origin */
309 /* 6: objpyr - structure values for pyramid */
312 "pyramid", "pyramid", /* long and short names */
313 5, /* number of vertices */
315 { /* vertices (x,y,z) */
316 /* all points must be within radius 1 of the origin */
327 /* 7: objstar - structure values for octahedron star (stellated octahedron?) */
329 "star", "star", /* long and short names */
330 8, /* number of vertices */
332 { /* vertices (x,y,z) */
333 /* all points must be within radius 1 of the origin */
353 *-----------------------------------------------------------------------------
354 *-----------------------------------------------------------------------------
356 *-----------------------------------------------------------------------------
357 *-----------------------------------------------------------------------------
361 /* initialise textures */
362 static void inittextures(ModeInfo * mi)
364 sballsstruct *sb = &sballs[MI_SCREEN(mi)];
366 #if defined( I_HAVE_XPM )
369 glGenTextures(1, &sb->backid);
370 #ifdef HAVE_GLBINDTEXTURE
371 glBindTexture(GL_TEXTURE_2D, sb->backid);
372 #endif /* HAVE_GLBINDTEXTURE */
374 sb->btexture = xpm_to_ximage(MI_DISPLAY(mi),
378 if (!(sb->btexture)) {
379 (void) fprintf(stderr, "Error reading the background texture.\n");
380 glDeleteTextures(1, &sb->backid);
382 sb->faceid = 0; /* default textures */
387 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
389 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
390 sb->btexture->width, sb->btexture->height, 0,
392 /* GL_UNSIGNED_BYTE, */
393 GL_UNSIGNED_INT_8_8_8_8_REV,
395 check_gl_error("texture");
397 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
398 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
400 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
401 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
403 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
405 glGenTextures(1, &sb->faceid);
406 #ifdef HAVE_GLBINDTEXTURE
407 glBindTexture(GL_TEXTURE_2D, sb->faceid);
408 #endif /* HAVE_GLBINDTEXTURE */
410 sb->ftexture = xpm_to_ximage(MI_DISPLAY(mi),
414 if (!(sb->ftexture)) {
415 (void) fprintf(stderr, "Error reading the face texture.\n");
416 glDeleteTextures(1, &sb->faceid);
421 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
423 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
424 sb->ftexture->width, sb->ftexture->height, 0,
426 /* GL_UNSIGNED_BYTE, */
427 GL_UNSIGNED_INT_8_8_8_8_REV,
429 check_gl_error("texture");
431 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
432 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
434 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
435 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
437 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
441 sb->faceid = 0; /* default textures */
444 #else /* !I_HAVE_XPM */
446 sb->faceid = 0; /* default textures */
448 #endif /* !I_HAVE_XPM */
451 static void drawSphere(ModeInfo * mi,int sphere_num)
453 sballsstruct *sb = &sballs[MI_SCREEN(mi)];
454 float x = polygons[object].v[sphere_num][0];
455 float y = polygons[object].v[sphere_num][1];
456 float z = polygons[object].v[sphere_num][2];
459 float radius = sb->radius[sphere_num];
460 double majorStep = (M_PI / numMajor);
461 double minorStep = (2.0 * M_PI / numMinor);
465 glTranslatef(x, y, z);
467 glColor4f(1, 1, 1, 1);
468 for (i = 0; i < numMajor; ++i)
470 double a = i * majorStep;
471 double b = a + majorStep;
472 double r0 = radius * sin(a);
473 double r1 = radius * sin(b);
474 GLfloat z0 = radius * cos(a);
475 GLfloat z1 = radius * cos(b);
477 glBegin(MI_IS_WIREFRAME(mi) ? GL_LINE_STRIP: GL_TRIANGLE_STRIP);
478 for (j = 0; j <= numMinor; ++j)
480 double c = j * minorStep;
484 glNormal3f((x * r0) / radius, (y * r0) / radius, z0 / radius);
485 glTexCoord2f(j / (GLfloat) numMinor, i / (GLfloat) numMajor);
486 glVertex3f(x * r0, y * r0, z0);
488 glNormal3f((x * r1) / radius, (y * r1) / radius, z1 / radius);
489 glTexCoord2f(j / (GLfloat) numMinor, (i + 1) / (GLfloat) numMajor);
490 glVertex3f(x * r1, y * r1, z1);
501 *-----------------------------------------------------------------------------
502 *-----------------------------------------------------------------------------
504 *-----------------------------------------------------------------------------
505 *-----------------------------------------------------------------------------
509 static void Reshape(ModeInfo * mi)
511 ENTRYPOINT void reshape_sballs(ModeInfo * mi, int width, int height)
515 sballsstruct *sb = &sballs[MI_SCREEN(mi)];
516 int size = MI_SIZE(mi);
518 /* Viewport is specified size if size >= MINSIZE && size < screensize */
520 sb->WIDTH = MI_WIDTH(mi);
521 sb->HEIGHT = MI_HEIGHT(mi);
522 } else if (size < MINSIZE) {
524 sb->HEIGHT = MINSIZE;
526 sb->WIDTH = (size > MI_WIDTH(mi)) ? MI_WIDTH(mi) : size;
527 sb->HEIGHT = (size > MI_HEIGHT(mi)) ? MI_HEIGHT(mi) : size;
529 glViewport((MI_WIDTH(mi) - sb->WIDTH) / 2, (MI_HEIGHT(mi) - sb->HEIGHT) / 2, sb->WIDTH, sb->HEIGHT);
530 glMatrixMode(GL_PROJECTION);
532 gluPerspective(55.0, (float)sb->WIDTH / (float) sb->HEIGHT, 1.0, 300.0);
534 glMatrixMode(GL_MODELVIEW);
538 static void Draw(ModeInfo * mi)
540 sballsstruct *sb = &sballs[MI_SCREEN(mi)];
543 mi->polygon_count = 0;
545 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
548 glEnable(GL_DEPTH_TEST);
551 glTranslatef (-sb->eye[0], -sb->eye[1], -sb->eye[2]);
553 /* draw background */
556 glEnable(GL_LIGHTING);
557 glEnable(GL_TEXTURE_2D);
559 #ifdef HAVE_GLBINDTEXTURE
560 glBindTexture(GL_TEXTURE_2D, sb->backid);
561 #endif /* HAVE_GLBINDTEXTURE */
567 glBegin(GL_QUAD_STRIP);
569 /* Letterbox the background image */
570 glNormal3f(0, 0, 1); glTexCoord2f(0,0); glVertex3f(8, 4.1, -4);
571 glNormal3f(0, 0, 1); glTexCoord2f(0,1); glVertex3f(8, -4.1, -4);
572 glNormal3f(0, 0, 1); glTexCoord2f(1,0); glVertex3f(-8, 4.1, -4);
573 glNormal3f(0, 0, 1); glTexCoord2f(1,1); glVertex3f(-8, -4.1, -4);
575 /* Fill the iPhone screen. Letterboxing looks dumb there. */
576 glNormal3f(0, 0, 1); glTexCoord2f(0,0); glVertex3f(4, 5.2, -4);
577 glNormal3f(0, 0, 1); glTexCoord2f(0,1); glVertex3f(4, -5.2, -4);
578 glNormal3f(0, 0, 1); glTexCoord2f(1,0); glVertex3f(-4, 5.2, -4);
579 glNormal3f(0, 0, 1); glTexCoord2f(1,1); glVertex3f(-4, -5.2, -4);
584 gltrackball_rotate (sb->trackball);
586 /* rotate the balls */
587 glRotatef(sb->rotm[0], 1.0f, 0.0f, 0.0f);
588 glRotatef(sb->rotm[1], 0.0f, 1.0f, 0.0f);
589 glRotatef(sb->rotm[2], 0.0f, 0.0f, 1.0f);
591 if (! sb->button_down_p) {
592 sb->rotm[0] += sb->speed;
593 sb->rotm[1] += -(sb->speed);
599 #ifdef HAVE_GLBINDTEXTURE
600 glBindTexture(GL_TEXTURE_2D, sb->faceid);
602 #endif /* HAVE_GLBINDTEXTURE */
603 glEnable(GL_LIGHTING);
604 for (sphere=0;sphere<spheres;sphere++)
606 drawSphere(mi,sphere);
609 glDisable(GL_TEXTURE_2D);
610 glDisable(GL_DEPTH_TEST);
611 glDisable(GL_LIGHTING);
613 /* manage framerate display */
614 if (MI_IS_FPS(mi)) do_fps (mi);
619 static void Init(ModeInfo * mi)
621 sballsstruct *sb = &sballs[MI_SCREEN(mi)];
624 /* Default settings */
625 if (MI_IS_WIREFRAME(mi))
631 sb->btexture = (XImage*) NULL;
632 sb->ftexture = (XImage*) NULL;
635 vinit(sb->eye ,0.0f, 0.0f, 6.0f);
636 vinit(sb->rotm ,0.0f, 0.0f, 0.0f);
637 sb->speed = MI_CYCLES(mi);
639 /* initialise object number */
640 if ((object == 0) || (object > MAX_OBJ))
641 object = NRAND(MAX_OBJ-1)+1;
644 /* initialise sphere number */
645 spheres = MI_COUNT(mi);
646 if (MI_COUNT(mi) > polygons[object].numverts)
647 spheres = polygons[object].numverts;
648 if (MI_COUNT(mi) < 1)
649 spheres = polygons[object].numverts;
650 /* initialise sphere radius */
651 for(i=0; i < spheres;i++)
654 sb->radius[i] = ((float) LRAND() / (float) MAXRAND);
655 if (sb->radius[i] < 0.3)
657 if (sb->radius[i] > 0.7)
660 sb->radius[i] = polygons[object].radius;
664 if (MI_IS_DEBUG(mi)) {
665 (void) fprintf(stderr,
666 "%s:\n\tobject=%s\n\tspheres=%d\n\tspeed=%d\n\ttexture=%s\n",
668 polygons[object].shortname,
671 do_texture ? "on" : "off"
675 glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);
676 glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
677 glLightfv(GL_LIGHT1, GL_POSITION,LightPosition);
682 *-----------------------------------------------------------------------------
683 *-----------------------------------------------------------------------------
685 *-----------------------------------------------------------------------------
686 *-----------------------------------------------------------------------------
689 static void free_sballs(ModeInfo * mi);
692 *-----------------------------------------------------------------------------
693 * Initialize sballs. Called each time the window changes.
694 *-----------------------------------------------------------------------------
697 ENTRYPOINT void init_sballs(ModeInfo * mi)
701 MI_INIT(mi, sballs, free_sballs);
702 sb = &sballs[MI_SCREEN(mi)];
704 sb->trackball = gltrackball_init (True);
706 if ((sb->glx_context = init_GL(mi)) != NULL) {
709 Reshape(mi); /* xlock mode */
711 reshape_sballs(mi,MI_WIDTH(mi),MI_HEIGHT(mi)); /* xscreensaver mode */
713 glDrawBuffer(GL_BACK);
722 *-----------------------------------------------------------------------------
723 * Called by the mainline code periodically to update the display.
724 *-----------------------------------------------------------------------------
726 ENTRYPOINT void draw_sballs(ModeInfo * mi)
728 Display *display = MI_DISPLAY(mi);
729 Window window = MI_WINDOW(mi);
734 sb = &sballs[MI_SCREEN(mi)];
736 MI_IS_DRAWN(mi) = True;
737 if (!sb->glx_context)
740 glXMakeCurrent(display, window, *(sb->glx_context));
743 Reshape(mi); /* xlock mode */
745 reshape_sballs(mi,MI_WIDTH(mi),MI_HEIGHT(mi)); /* xscreensaver mode */
749 glXSwapBuffers(display, window);
754 *-----------------------------------------------------------------------------
755 * The display is being taken away from us. Free up malloc'ed
756 * memory and X resources that we've alloc'ed.
757 *-----------------------------------------------------------------------------
760 static void free_sballs(ModeInfo * mi)
762 sballsstruct *sb = &sballs[MI_SCREEN(mi)];
765 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(sb->glx_context));
768 glDeleteTextures(1,&sb->backid);
769 XDestroyImage(sb->btexture);
773 glDeleteTextures(1,&sb->faceid);
774 XDestroyImage(sb->ftexture);
780 sballs_handle_event (ModeInfo *mi, XEvent *event)
782 sballsstruct *sb = &sballs[MI_SCREEN(mi)];
784 if (gltrackball_event_handler (event, sb->trackball,
785 MI_WIDTH (mi), MI_HEIGHT (mi),
794 ENTRYPOINT void change_sballs(ModeInfo * mi)
800 sb = &sballs[MI_SCREEN(mi)];
802 if (!sb->glx_context)
805 /* initialise object number */
806 if ((object == 0) || (object > MAX_OBJ))
807 object = NRAND(MAX_OBJ-1)+1;
810 /* correct sphere number */
811 spheres = MI_COUNT(mi);
812 if (MI_COUNT(mi) > polygons[object].numverts)
813 spheres = polygons[object].numverts;
814 if (MI_COUNT(mi) < 1)
815 spheres = polygons[object].numverts;
817 if (MI_IS_DEBUG(mi)) {
818 (void) fprintf(stderr,
819 "%s:\n\tobject=%s\n\tspheres=%d\n\tspeed=%d\n\ttexture=%s\n",
821 polygons[object].shortname,
824 do_texture ? "on" : "off"
827 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(sb->glx_context));
830 #endif /* !STANDALONE */
832 XSCREENSAVER_MODULE ("SBalls", sballs)
834 #endif /* MODE_sballs */