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 release_sballs 0
52 #include "xlockmore.h" /* from the xscreensaver distribution */
53 #include "gltrackball.h"
54 #else /* !STANDALONE */
55 #include "xlock.h" /* from the xlockmore distribution */
57 #endif /* !STANDALONE */
59 #define MINSIZE 32 /* minimal viewport size */
60 #define FRAME 50 /* frame count interval */
61 #define MAX_OBJ 8 /* number of 3D objects */
63 #if defined( USE_XPM ) || defined( USE_XPMINC ) || defined( STANDALONE )
64 /* USE_XPM & USE_XPMINC in xlock mode ; HAVE_XPM in xscreensaver mode */
65 # include "ximage-loader.h"
68 # include "images/gen/sball_png.h"
69 # include "images/gen/sball-bg_png.h"
71 /* Manage option vars */
72 #define DEF_TEXTURE "True"
73 #define DEF_OBJECT "0"
74 static Bool do_texture;
75 static int object, object_arg;
78 static XrmOptionDescRec opts[] = {
79 {"-texture", ".sballs.texture", XrmoptionNoArg, "on"},
80 {"+texture", ".sballs.texture", XrmoptionNoArg, "off"},
81 {"-object", ".sballs.object", XrmoptionSepArg, 0},
85 static argtype vars[] = {
86 {&do_texture, "texture", "Texture", DEF_TEXTURE, t_Bool},
87 {&object_arg, "object", "Object", DEF_OBJECT, t_Int},
91 static OptionStruct desc[] = {
92 /*{"-count spheres", "set number of spheres"},*/
93 /*{"-cycles speed", "set ball speed value"},*/
94 {"-/+texture", "turn on/off texturing"},
95 {"-object num", "number of the 3D object (0 means random)"},
98 ENTRYPOINT ModeSpecOpt sballs_opts =
99 { sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc };
102 ModStruct sballs_description =
103 { "sballs", "init_sballs", "draw_sballs", NULL,
104 "draw_sballs", "change_sballs", "free_sballs", &sballs_opts,
106 delay,count,cycles,size,ncolors,sat
108 10000, 0, 10, 400, 64, 1.0, "",
109 "balls spinning like crazy in GL", 0, NULL
111 #endif /* USE_MODULES */
113 /* misc types and defines */
114 #define vinit(a,i,j,k) {\
120 typedef vec_t vec3_t[3];
124 /* the mode struct, contains all per screen variables */
126 GLint WIDTH, HEIGHT; /* display dimensions */
127 GLXContext *glx_context;
130 XImage *btexture; /* back texture image bits */
131 XImage *ftexture; /* face texture image bits */
132 GLuint backid; /* back texture id: GL world */
133 GLuint faceid; /* face texture id: GL world */
138 float radius[MAX_BALLS];
140 trackball_state *trackball;
145 /* array of sballsstruct indexed by screen number */
146 static sballsstruct *sballs = (sballsstruct *) NULL;
149 static const float LightAmbient[]= { 1.0f, 1.0f, 1.0f, 1.0f };
150 static const float LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f };
151 static const float LightPosition[]= { 0.0f, 0.0f, 4.0f, 1.0f };
153 /* structure of the polyhedras */
155 const char *longname; /* long name of object */
156 const char *shortname; /* short name of object */
157 int numverts; /* number of vertices */
158 float radius; /* radius */
159 vec3_t v[MAX_BALLS];/* the vertices */
162 static const Polyinfo polygons[] =
165 /* 0: objtetra - structure values for tetrahedron */
167 "tetrahedron", "tetra", /* long and short names */
168 4, /* number of vertices */
170 { /* vertices (x,y,z) */
171 /* all points must be within radius 2 of the origin */
181 /* 1: objcube - structure values for cube */
184 "hexahedron", "cube", /* long and short names */
185 8, /* number of vertices, edges, and faces */
187 { /* vertices (x,y,z) */
188 /* all points must be within radius 2 of the origin */
202 /* 2: objocta - structure values for octahedron */
205 "octahedron", "octa", /* long and short names */
206 6, /* number of vertices */
208 { /* vertices (x,y,z) */
209 /* all points must be within radius 2 of the origin */
220 /* 3: objdodec - structure values for dodecahedron */
223 "dodecahedron", "dodeca", /* long and short names */
224 20, /* number of vertices */
226 { /* vertices (x,y,z) */
227 /* all points must be within radius 2 of the origin */
228 {0.000000, 0.500000, 1.000000},
229 {0.000000, -0.500000, 1.000000},
230 {0.000000, -0.500000, -1.000000},
231 {0.000000, 0.500000, -1.000000},
232 {1.000000, 0.000000, 0.500000},
233 {-1.000000, 0.000000, 0.500000},
234 {-1.000000, 0.000000, -0.500000},
235 {1.000000, 0.000000, -0.500000},
236 {0.500000, 1.000000, 0.000000},
237 {-0.500000, 1.000000, 0.000000},
238 {-0.500000, -1.000000, 0.000000},
239 {0.500000, -1.000000, 0.000000},
240 {0.750000, 0.750000, 0.750000},
241 {-0.750000, 0.750000, 0.750000},
242 {-0.750000, -0.750000, 0.750000},
243 {0.750000, -0.750000, 0.750000},
244 {0.750000, -0.750000, -0.750000},
245 {0.750000, 0.750000, -0.750000},
246 {-0.750000, 0.750000, -0.750000},
247 {-0.750000, -0.750000, -0.750000},
251 /* 4: objicosa - structure values for icosahedron */
254 "icosahedron", "icosa", /* long and short names */
255 12, /* number of vertices */
257 { /* vertices (x,y,z) */
258 /* all points must be within radius 2 of the origin */
259 {0.00000000, 0.00000000, -0.95105650},
260 {0.00000000, 0.85065080, -0.42532537},
261 {0.80901698, 0.26286556, -0.42532537},
262 {0.50000000, -0.68819095, -0.42532537},
263 {-0.50000000, -0.68819095, -0.42532537},
264 {-0.80901698, 0.26286556, -0.42532537},
265 {0.50000000, 0.68819095, 0.42532537},
266 {0.80901698, -0.26286556, 0.42532537},
267 {0.00000000, -0.85065080, 0.42532537},
268 {-0.80901698, -0.26286556, 0.42532537},
269 {-0.50000000, 0.68819095, 0.42532537},
270 {0.00000000, 0.00000000, 0.95105650}
274 /* 5: objplane - structure values for plane */
277 "plane", "plane", /* long and short names */
278 4, /* number of vertices */
280 { /* vertices (x,y,z) */
281 /* all points must be within radius 2 of the origin */
291 /* 6: objpyr - structure values for pyramid */
294 "pyramid", "pyramid", /* long and short names */
295 5, /* number of vertices */
297 { /* vertices (x,y,z) */
298 /* all points must be within radius 1 of the origin */
309 /* 7: objstar - structure values for octahedron star (stellated octahedron?) */
311 "star", "star", /* long and short names */
312 8, /* number of vertices */
314 { /* vertices (x,y,z) */
315 /* all points must be within radius 1 of the origin */
335 *-----------------------------------------------------------------------------
336 *-----------------------------------------------------------------------------
338 *-----------------------------------------------------------------------------
339 *-----------------------------------------------------------------------------
343 /* initialise textures */
344 static void inittextures(ModeInfo * mi)
346 sballsstruct *sb = &sballs[MI_SCREEN(mi)];
348 #if defined( I_HAVE_XPM )
351 glGenTextures(1, &sb->backid);
352 #ifdef HAVE_GLBINDTEXTURE
353 glBindTexture(GL_TEXTURE_2D, sb->backid);
354 #endif /* HAVE_GLBINDTEXTURE */
356 sb->btexture = image_data_to_ximage(MI_DISPLAY(mi), MI_VISUAL(mi),
358 sizeof(sball_bg_png));
359 if (!(sb->btexture)) {
360 (void) fprintf(stderr, "Error reading the background texture.\n");
361 glDeleteTextures(1, &sb->backid);
363 sb->faceid = 0; /* default textures */
368 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
370 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
371 sb->btexture->width, sb->btexture->height, 0,
372 GL_RGBA, GL_UNSIGNED_BYTE, sb->btexture->data);
373 check_gl_error("texture");
375 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
376 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
379 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
380 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
381 Let's pixellate it instead:
383 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
384 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
386 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
388 glGenTextures(1, &sb->faceid);
389 #ifdef HAVE_GLBINDTEXTURE
390 glBindTexture(GL_TEXTURE_2D, sb->faceid);
391 #endif /* HAVE_GLBINDTEXTURE */
393 sb->ftexture = image_data_to_ximage(MI_DISPLAY(mi), MI_VISUAL(mi),
394 sball_png, sizeof(sball_png));
395 if (!(sb->ftexture)) {
396 (void) fprintf(stderr, "Error reading the face texture.\n");
397 glDeleteTextures(1, &sb->faceid);
402 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
404 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
405 sb->ftexture->width, sb->ftexture->height, 0,
406 GL_RGBA, GL_UNSIGNED_BYTE, sb->ftexture->data);
407 check_gl_error("texture");
409 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
410 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
413 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
414 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
415 Let's pixellate it instead:
417 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
418 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
420 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
424 sb->faceid = 0; /* default textures */
427 #else /* !I_HAVE_XPM */
429 sb->faceid = 0; /* default textures */
431 #endif /* !I_HAVE_XPM */
434 static void drawSphere(ModeInfo * mi,int sphere_num)
436 sballsstruct *sb = &sballs[MI_SCREEN(mi)];
437 float x = polygons[object].v[sphere_num][0];
438 float y = polygons[object].v[sphere_num][1];
439 float z = polygons[object].v[sphere_num][2];
442 float radius = sb->radius[sphere_num];
443 double majorStep = (M_PI / numMajor);
444 double minorStep = (2.0 * M_PI / numMinor);
448 glTranslatef(x, y, z);
450 glColor4f(1, 1, 1, 1);
451 for (i = 0; i < numMajor; ++i)
453 double a = i * majorStep;
454 double b = a + majorStep;
455 double r0 = radius * sin(a);
456 double r1 = radius * sin(b);
457 GLfloat z0 = radius * cos(a);
458 GLfloat z1 = radius * cos(b);
460 glBegin(MI_IS_WIREFRAME(mi) ? GL_LINE_STRIP: GL_TRIANGLE_STRIP);
461 for (j = 0; j <= numMinor; ++j)
463 double c = j * minorStep;
467 glNormal3f((x * r0) / radius, (y * r0) / radius, z0 / radius);
468 glTexCoord2f(j / (GLfloat) numMinor, i / (GLfloat) numMajor);
469 glVertex3f(x * r0, y * r0, z0);
471 glNormal3f((x * r1) / radius, (y * r1) / radius, z1 / radius);
472 glTexCoord2f(j / (GLfloat) numMinor, (i + 1) / (GLfloat) numMajor);
473 glVertex3f(x * r1, y * r1, z1);
484 *-----------------------------------------------------------------------------
485 *-----------------------------------------------------------------------------
487 *-----------------------------------------------------------------------------
488 *-----------------------------------------------------------------------------
492 static void Reshape(ModeInfo * mi)
494 ENTRYPOINT void reshape_sballs(ModeInfo * mi, int width, int height)
498 sballsstruct *sb = &sballs[MI_SCREEN(mi)];
499 int size = MI_SIZE(mi);
501 /* Viewport is specified size if size >= MINSIZE && size < screensize */
503 sb->WIDTH = MI_WIDTH(mi);
504 sb->HEIGHT = MI_HEIGHT(mi);
505 } else if (size < MINSIZE) {
507 sb->HEIGHT = MINSIZE;
509 sb->WIDTH = (size > MI_WIDTH(mi)) ? MI_WIDTH(mi) : size;
510 sb->HEIGHT = (size > MI_HEIGHT(mi)) ? MI_HEIGHT(mi) : size;
513 if (width > height * 5) { /* tiny window: show middle */
515 sb->HEIGHT = sb->WIDTH*0.75;
518 glViewport((MI_WIDTH(mi) - sb->WIDTH) / 2, (MI_HEIGHT(mi) - sb->HEIGHT) / 2, sb->WIDTH, sb->HEIGHT);
519 glMatrixMode(GL_PROJECTION);
521 gluPerspective(55.0, (float)sb->WIDTH / (float) sb->HEIGHT, 1.0, 300.0);
523 glMatrixMode(GL_MODELVIEW);
527 static void Draw(ModeInfo * mi)
529 sballsstruct *sb = &sballs[MI_SCREEN(mi)];
532 mi->polygon_count = 0;
534 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
537 glEnable(GL_DEPTH_TEST);
540 glTranslatef (-sb->eye[0], -sb->eye[1], -sb->eye[2]);
542 /* draw background */
545 glEnable(GL_LIGHTING);
546 glEnable(GL_TEXTURE_2D);
548 #ifdef HAVE_GLBINDTEXTURE
549 glBindTexture(GL_TEXTURE_2D, sb->backid);
550 #endif /* HAVE_GLBINDTEXTURE */
556 glBegin(GL_QUAD_STRIP);
558 /* Letterbox the background image */
559 glNormal3f(0, 0, 1); glTexCoord2f(0,0); glVertex3f(8, 4.1, -4);
560 glNormal3f(0, 0, 1); glTexCoord2f(0,1); glVertex3f(8, -4.1, -4);
561 glNormal3f(0, 0, 1); glTexCoord2f(1,0); glVertex3f(-8, 4.1, -4);
562 glNormal3f(0, 0, 1); glTexCoord2f(1,1); glVertex3f(-8, -4.1, -4);
564 /* Fill the iPhone screen. Letterboxing looks dumb there. */
565 glNormal3f(0, 0, 1); glTexCoord2f(0,0); glVertex3f(4, 5.2, -4);
566 glNormal3f(0, 0, 1); glTexCoord2f(0,1); glVertex3f(4, -5.2, -4);
567 glNormal3f(0, 0, 1); glTexCoord2f(1,0); glVertex3f(-4, 5.2, -4);
568 glNormal3f(0, 0, 1); glTexCoord2f(1,1); glVertex3f(-4, -5.2, -4);
573 gltrackball_rotate (sb->trackball);
575 /* rotate the balls */
576 glRotatef(sb->rotm[0], 1.0f, 0.0f, 0.0f);
577 glRotatef(sb->rotm[1], 0.0f, 1.0f, 0.0f);
578 glRotatef(sb->rotm[2], 0.0f, 0.0f, 1.0f);
580 if (! sb->button_down_p) {
581 sb->rotm[0] += sb->speed;
582 sb->rotm[1] += -(sb->speed);
588 #ifdef HAVE_GLBINDTEXTURE
589 glBindTexture(GL_TEXTURE_2D, sb->faceid);
591 #endif /* HAVE_GLBINDTEXTURE */
592 glEnable(GL_LIGHTING);
593 for (sphere=0;sphere<spheres;sphere++)
595 drawSphere(mi,sphere);
598 glDisable(GL_TEXTURE_2D);
599 glDisable(GL_DEPTH_TEST);
600 glDisable(GL_LIGHTING);
602 /* manage framerate display */
603 if (MI_IS_FPS(mi)) do_fps (mi);
608 static void Init(ModeInfo * mi)
610 sballsstruct *sb = &sballs[MI_SCREEN(mi)];
613 /* Default settings */
614 if (MI_IS_WIREFRAME(mi))
620 sb->btexture = (XImage*) NULL;
621 sb->ftexture = (XImage*) NULL;
624 vinit(sb->eye ,0.0f, 0.0f, 6.0f);
625 vinit(sb->rotm ,0.0f, 0.0f, 0.0f);
626 sb->speed = MI_CYCLES(mi);
628 /* initialise object number */
629 object = object_arg-1;
630 if (object < 0 || object >= MAX_OBJ)
631 object = NRAND(MAX_OBJ);
633 /* initialise sphere number */
634 spheres = MI_COUNT(mi);
635 if (MI_COUNT(mi) > polygons[object].numverts)
636 spheres = polygons[object].numverts;
637 if (MI_COUNT(mi) < 1)
638 spheres = polygons[object].numverts;
639 /* initialise sphere radius */
640 for(i=0; i < spheres;i++)
643 sb->radius[i] = ((float) LRAND() / (float) MAXRAND);
644 if (sb->radius[i] < 0.3)
646 if (sb->radius[i] > 0.7)
649 sb->radius[i] = polygons[object].radius;
653 if (MI_IS_DEBUG(mi)) {
654 (void) fprintf(stderr,
655 "%s:\n\tobject=%s\n\tspheres=%d\n\tspeed=%d\n\ttexture=%s\n",
657 polygons[object].shortname,
660 do_texture ? "on" : "off"
664 glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);
665 glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
666 glLightfv(GL_LIGHT1, GL_POSITION,LightPosition);
671 *-----------------------------------------------------------------------------
672 *-----------------------------------------------------------------------------
674 *-----------------------------------------------------------------------------
675 *-----------------------------------------------------------------------------
679 *-----------------------------------------------------------------------------
680 * Initialize sballs. Called each time the window changes.
681 *-----------------------------------------------------------------------------
684 ENTRYPOINT void init_sballs(ModeInfo * mi)
689 sb = &sballs[MI_SCREEN(mi)];
691 sb->trackball = gltrackball_init (True);
693 if ((sb->glx_context = init_GL(mi)) != NULL) {
696 Reshape(mi); /* xlock mode */
698 reshape_sballs(mi,MI_WIDTH(mi),MI_HEIGHT(mi)); /* xscreensaver mode */
700 glDrawBuffer(GL_BACK);
709 *-----------------------------------------------------------------------------
710 * Called by the mainline code periodically to update the display.
711 *-----------------------------------------------------------------------------
713 ENTRYPOINT void draw_sballs(ModeInfo * mi)
715 Display *display = MI_DISPLAY(mi);
716 Window window = MI_WINDOW(mi);
721 sb = &sballs[MI_SCREEN(mi)];
723 MI_IS_DRAWN(mi) = True;
724 if (!sb->glx_context)
727 glXMakeCurrent(display, window, *(sb->glx_context));
730 Reshape(mi); /* xlock mode */
732 reshape_sballs(mi,MI_WIDTH(mi),MI_HEIGHT(mi)); /* xscreensaver mode */
736 glXSwapBuffers(display, window);
741 *-----------------------------------------------------------------------------
742 * The display is being taken away from us. Free up malloc'ed
743 * memory and X resources that we've alloc'ed.
744 *-----------------------------------------------------------------------------
747 ENTRYPOINT void free_sballs(ModeInfo * mi)
749 sballsstruct *sb = &sballs[MI_SCREEN(mi)];
752 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(sb->glx_context));
755 glDeleteTextures(1,&sb->backid);
756 XDestroyImage(sb->btexture);
761 glDeleteTextures(1,&sb->faceid);
762 XDestroyImage(sb->ftexture);
769 sballs_handle_event (ModeInfo *mi, XEvent *event)
771 sballsstruct *sb = &sballs[MI_SCREEN(mi)];
773 if (gltrackball_event_handler (event, sb->trackball,
774 MI_WIDTH (mi), MI_HEIGHT (mi),
783 ENTRYPOINT void change_sballs(ModeInfo * mi)
789 sb = &sballs[MI_SCREEN(mi)];
791 if (!sb->glx_context)
794 /* initialise object number */
795 if ((object == 0) || (object > MAX_OBJ))
796 object = NRAND(MAX_OBJ-1)+1;
799 /* correct sphere number */
800 spheres = MI_COUNT(mi);
801 if (MI_COUNT(mi) > polygons[object].numverts)
802 spheres = polygons[object].numverts;
803 if (MI_COUNT(mi) < 1)
804 spheres = polygons[object].numverts;
806 if (MI_IS_DEBUG(mi)) {
807 (void) fprintf(stderr,
808 "%s:\n\tobject=%s\n\tspheres=%d\n\tspeed=%d\n\ttexture=%s\n",
810 polygons[object].shortname,
813 do_texture ? "on" : "off"
816 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(sb->glx_context));
819 #endif /* !STANDALONE */
821 XSCREENSAVER_MODULE ("SBalls", sballs)
823 #endif /* MODE_sballs */