1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* extrusion --- extrusion module for xscreensaver */
4 * Permission to use, copy, modify, and distribute this software and its
5 * documentation for any purpose and without fee is hereby granted,
6 * provided that the above copyright notice appear in all copies and that
7 * both that copyright notice and this permission notice appear in
8 * supporting documentation.
10 * This file is provided AS IS with no warranties of any kind. The author
11 * shall have no liability with respect to the infringement of copyrights,
12 * trade secrets or any patents by this file or any part thereof. In no
13 * event will the author be liable for any lost revenue or profits or
14 * other special, indirect and consequential damages.
17 * Tue Oct 19 22:24:47 PDT 1999 Initial creation by David Konerding
21 * This screensaver requires the GLE ("OpenGL Tubing and Extrusion Library")
22 * which can be obtained from http://www.linas.org/gle/index.html
30 # define PROGCLASS "Screensaver"
31 # define HACK_INIT init_screensaver
32 # define HACK_DRAW draw_screensaver
33 # define HACK_RESHAPE reshape_screensaver
34 # define HACK_HANDLE_EVENT screensaver_handle_event
35 # define EVENT_MASK PointerMotionMask
36 # define screensaver_opts xlockmore_opts
37 #define DEFAULTS "*delay: 10000 \n" \
38 "*showFPS: False \n" \
41 "*texture: False \n" \
42 "*image: BUILTIN \n" \
46 # include "xlockmore.h" /* from the xscreensaver distribution */
47 #else /* !STANDALONE */
48 # include "xlock.h" /* from the xlockmore distribution */
49 #endif /* !STANDALONE */
51 #ifdef USE_GL /* whole file */
55 # include <X11/Xmu/Drawing.h>
57 # include <Xmu/Drawing.h>
74 #define countof(x) (sizeof((x))/sizeof((*x)))
76 #include "xpm-ximage.h"
79 #define checkImageWidth 64
80 #define checkImageHeight 64
83 extern void InitStuff_helix2(void);
84 extern void DrawStuff_helix2(void);
85 extern void InitStuff_helix3(void);
86 extern void DrawStuff_helix3(void);
87 extern void InitStuff_helix4(void);
88 extern void DrawStuff_helix4(void);
89 extern void InitStuff_joinoffset(void);
90 extern void DrawStuff_joinoffset(void);
91 extern void InitStuff_screw(void);
92 extern void DrawStuff_screw(void);
93 extern void InitStuff_taper(void);
94 extern void DrawStuff_taper(void);
95 extern void InitStuff_twistoid(void);
96 extern void DrawStuff_twistoid(void);
103 #define DEF_LIGHT "True"
104 #define DEF_WIRE "False"
105 #define DEF_TEXTURE "False"
106 #define DEF_TEXTURE_QUALITY "False"
107 #define DEF_MIPMAP "False"
108 #define DEF_NAME "RANDOM"
109 #define DEF_IMAGE "BUILTIN"
113 static int do_texture;
114 static int do_texture_quality;
115 static int do_mipmap;
116 static char *which_name;
117 static char *which_image;
119 static XrmOptionDescRec opts[] = {
120 {"-light", ".extrusion.light", XrmoptionNoArg, "true" },
121 {"+light", ".extrusion.light", XrmoptionNoArg, "false" },
122 {"-wire", ".extrusion.wire", XrmoptionNoArg, "true" },
123 {"+wire", ".extrusion.wire", XrmoptionNoArg, "false" },
124 {"-texture", ".extrusion.texture", XrmoptionNoArg, "true" },
125 {"+texture", ".extrusion.texture", XrmoptionNoArg, "false" },
126 {"-texture", ".extrusion.texture", XrmoptionNoArg, "true" },
127 {"+texture_quality", ".extrusion.texture", XrmoptionNoArg, "false" },
128 {"-texture_quality", ".extrusion.texture", XrmoptionNoArg, "true" },
129 {"+mipmap", ".extrusion.mipmap", XrmoptionNoArg, "false" },
130 {"-mipmap", ".extrusion.mipmap", XrmoptionNoArg, "true" },
131 {"-name", ".extrusion.name", XrmoptionSepArg, 0 },
132 {"-image", ".extrusion.image", XrmoptionSepArg, 0 },
136 static argtype vars[] = {
137 {&do_light, "light", "Light", DEF_LIGHT, t_Bool},
138 {&do_wire, "wire", "Wire", DEF_WIRE, t_Bool},
139 {&do_texture, "texture", "Texture", DEF_TEXTURE, t_Bool},
140 {&do_texture_quality, "texture_quality", "Texture_Quality", DEF_TEXTURE_QUALITY, t_Bool},
141 {&do_mipmap, "mipmap", "Mipmap", DEF_MIPMAP, t_Bool},
142 {&which_name, "name", "Name", DEF_NAME, t_String},
143 {&which_image, "image", "Image", DEF_IMAGE, t_String},
147 static OptionStruct desc[] =
149 {"-name num", "example 'name' to draw (helix2, helix3, helix4, joinoffset, screw, taper, twistoid)"},
150 {"-/+ light", "whether to do enable lighting (slower)"},
151 {"-/+ wire", "whether to do use wireframe instead of filled (faster)"},
152 {"-/+ texture", "whether to apply a texture (slower)"},
153 {"-image <filename>", "texture image to load"},
154 {"-/+ texture_quality", "whether to use texture smoothing (slower)"},
155 {"-/+ mipmap", "whether to use texture mipmap (slower)"},
158 ModeSpecOpt screensaver_opts = {countof(opts), opts, countof(vars), vars, desc};
161 ModStruct screensaver_description =
162 {"screensaver", "init_screensaver", "draw_screensaver", "release_screensaver",
163 "draw_screensaver", "init_screensaver", NULL, &screensaver_opts,
164 1000, 1, 2, 1, 4, 1.0, "",
165 "OpenGL screensaver", 0, NULL};
169 /* structure for holding the screensaver data */
171 int screen_width, screen_height;
172 GLXContext *glx_context;
175 int mouse_x, mouse_y;
179 static screensaverstruct *Screensaver = NULL;
184 /* convenient access to the screen width */
185 static int global_width=640, global_height=480;
188 static GLfloat lightOnePosition[] = {40.0, 40, 100.0, 0.0};
189 static GLfloat lightOneColor[] = {0.99, 0.99, 0.99, 1.0};
191 static GLfloat lightTwoPosition[] = {-40.0, 40, 100.0, 0.0};
192 static GLfloat lightTwoColor[] = {0.99, 0.99, 0.99, 1.0};
194 float rot_x=0, rot_y=0, rot_z=0;
195 float lastx=0, lasty=0;
197 static float max_lastx=300, max_lasty=400;
198 static float min_lastx=-400, min_lasty=-400;
200 static int screensaver_number;
203 void (*InitStuff)(void);
204 void (*DrawStuff)(void);
208 /* currently joinoffset and twistoid look funny-
209 like we're looking at them from the back or something
212 static struct functions funcs_ptr[] = {
213 {InitStuff_helix2, DrawStuff_helix2, "helix2"},
214 {InitStuff_helix3, DrawStuff_helix3, "helix3"},
215 {InitStuff_helix4, DrawStuff_helix4, "helix4"},
216 {InitStuff_joinoffset, DrawStuff_joinoffset, "joinoffset"},
217 {InitStuff_screw, DrawStuff_screw, "screw"},
218 {InitStuff_taper, DrawStuff_taper, "taper"},
219 {InitStuff_twistoid, DrawStuff_twistoid, "twistoid"},
222 static int num_screensavers = countof(funcs_ptr);
225 /* BEGINNING OF FUNCTIONS */
229 Generate_Image(int *width, int *height, int *format)
235 *width = checkImageWidth;
236 *height = checkImageHeight;
237 result = (GLubyte *)malloc(4 * (*width) * (*height));
240 for (i = 0; i < checkImageWidth; i++) {
241 for (j = 0; j < checkImageHeight; j++) {
242 c = (((((i&0x8)==0))^(((j&0x8))==0)))*255;
243 result[counter++] = (GLubyte) c;
244 result[counter++] = (GLubyte) c;
245 result[counter++] = (GLubyte) c;
246 result[counter++] = (GLubyte) 255;
255 /* Create a texture in OpenGL. First an image is loaded
256 and stored in a raster buffer, then it's */
257 void Create_Texture(ModeInfo *mi, const char *filename)
263 if ( !strncmp(filename, "BUILTIN", 7))
264 image = Generate_Image(&width, &height, &format);
267 XImage *ximage = xpm_file_to_ximage (MI_DISPLAY (mi), MI_VISUAL (mi),
268 MI_COLORMAP (mi), filename);
269 image = (GLubyte *) ximage->data;
270 width = ximage->width;
271 height = ximage->height;
275 /* GL_MODULATE or GL_DECAL depending on what you want */
276 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
277 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
278 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
279 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
280 /* perhaps we can edge a bit more speed at the expense of quality */
281 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
283 if (do_texture_quality) {
284 /* with texture_quality, the min and mag filters look *much* nice but are *much* slower */
285 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
286 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
289 /* default is to do it quick and dirty */
290 /* if you have mipmaps turned on, but not texture quality, nothing will happen! */
291 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
292 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
295 /* mipmaps make the image look much nicer */
300 status = gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width, height, format,
301 GL_UNSIGNED_BYTE, image);
304 const char *s = (char *) gluErrorString (status);
305 fprintf (stderr, "%s: error mipmapping %dx%d texture: %s\n",
306 progname, width, height,
307 (s ? s : "(unknown)"));
310 check_gl_error("mipmapping");
315 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0,
316 format, GL_UNSIGNED_BYTE, image);
317 check_gl_error("texture");
323 init_rotation (ModeInfo *mi)
325 screensaverstruct *gp = &Screensaver[MI_SCREEN(mi)];
326 double spin_speed = 1.0;
327 gp->rot = make_rotator (spin_speed, spin_speed, spin_speed, 1.0, 0.0, True);
329 lastx = (random() % (int) (max_lastx - min_lastx)) + min_lastx;
330 lasty = (random() % (int) (max_lasty - min_lasty)) + min_lasty;
334 /* draw the screensaver once */
335 void draw_screensaver(ModeInfo * mi)
337 screensaverstruct *gp = &Screensaver[MI_SCREEN(mi)];
338 Display *display = MI_DISPLAY(mi);
339 Window window = MI_WINDOW(mi);
341 if (!gp->glx_context)
344 glXMakeCurrent(display, window, *(gp->glx_context));
346 funcs_ptr[screensaver_number].DrawStuff();
348 /* track the mouse only if a button is down. */
349 if (gp->button_down_p)
356 float scale = (max_lastx - min_lastx);
358 get_rotation (gp->rot, &x, &y, &z, True);
362 lastx = x * scale + min_lastx;
363 lasty = y * scale + min_lasty;
366 if (mi->fps_p) do_fps (mi);
367 glXSwapBuffers(display, window);
371 /* set up lighting conditions */
372 static void SetupLight(void)
374 glLightfv (GL_LIGHT0, GL_POSITION, lightOnePosition);
375 glLightfv (GL_LIGHT0, GL_DIFFUSE, lightOneColor);
376 glLightfv (GL_LIGHT1, GL_POSITION, lightTwoPosition);
377 glLightfv (GL_LIGHT1, GL_DIFFUSE, lightTwoColor);
379 glEnable (GL_LIGHT0);
380 glEnable (GL_LIGHT1);
381 glEnable (GL_LIGHTING);
383 glColorMaterial (GL_FRONT, GL_DIFFUSE);
384 glColorMaterial (GL_BACK, GL_DIFFUSE);
385 glEnable (GL_COLOR_MATERIAL);
388 /* reset the projection matrix */
389 static void resetProjection(void) {
390 glMatrixMode(GL_PROJECTION);
392 glFrustum (-9, 9, -9, 9, 50, 150.0);
393 glMatrixMode(GL_MODELVIEW);
397 /* Standard reshape function */
399 reshape_screensaver(ModeInfo *mi, int width, int height)
402 global_height=height;
403 glViewport( 0, 0, global_width, global_height );
408 /* decide which screensaver example to run */
409 static void chooseScreensaverExample(ModeInfo *mi)
412 /* call the extrusion init routine */
414 if (!strncmp(which_name, "RANDOM", strlen(which_name))) {
415 screensaver_number = random() % num_screensavers;
418 screensaver_number=-1;
419 for (i=0; i < num_screensavers; i++) {
420 if (!strncmp(which_name, funcs_ptr[i].name, strlen(which_name))) {
421 screensaver_number = i;
426 if (screensaver_number < 0 || screensaver_number >= num_screensavers) {
427 fprintf(stderr, "%s: invalid screensaver example number!\n", progname);
428 fprintf(stderr, "%s: known screensavers:\n", progname);
429 for (i=0; i < num_screensavers; i++)
430 fprintf(stderr,"\t%s\n", funcs_ptr[i].name);
434 funcs_ptr[screensaver_number].InitStuff();
437 /* main OpenGL initialization routine */
439 initializeGL(ModeInfo *mi, GLsizei width, GLsizei height)
444 reshape_screensaver(mi, width, height);
445 glViewport( 0, 0, width, height );
447 glEnable(GL_DEPTH_TEST);
448 glClearColor(0,0,0,0);
449 /* glCullFace(GL_BACK); */
450 /* glEnable(GL_CULL_FACE); */
451 glLightModeli (GL_LIGHT_MODEL_TWO_SIDE, True);
452 glShadeModel(GL_SMOOTH);
457 glPolygonMode(GL_FRONT,GL_LINE);
458 glPolygonMode(GL_BACK,GL_LINE);
461 Create_Texture(mi, which_image);
462 glEnable(GL_TEXTURE_2D);
464 /* configure the pipeline */
466 style |= TUBE_CONTOUR_CLOSED;
467 style |= TUBE_NORM_FACET;
468 style |= TUBE_JN_ANGLE;
469 gleSetJoinStyle (style);
472 mode = GLE_TEXTURE_ENABLE | GLE_TEXTURE_VERTEX_MODEL_FLAT;
473 glMatrixMode (GL_TEXTURE); glLoadIdentity ();
474 glScalef (0.25, 0.1, 1); glMatrixMode (GL_MODELVIEW);
475 gleTextureMode (mode);
482 screensaver_handle_event (ModeInfo *mi, XEvent *event)
484 screensaverstruct *gp = &Screensaver[MI_SCREEN(mi)];
486 if (event->xany.type == ButtonPress &&
487 event->xbutton.button & Button1)
489 gp->button_down_p = True;
490 gp->mouse_x = event->xbutton.x;
491 gp->mouse_y = event->xbutton.y;
494 else if (event->xany.type == ButtonRelease &&
495 event->xbutton.button & Button1)
497 gp->button_down_p = False;
500 else if (event->xany.type == MotionNotify)
502 gp->mouse_x = event->xmotion.x;
503 gp->mouse_y = event->xmotion.y;
512 /* xscreensaver initialization routine */
513 void init_screensaver(ModeInfo * mi)
515 int screen = MI_SCREEN(mi);
516 screensaverstruct *gp;
518 if (Screensaver == NULL) {
519 if ((Screensaver = (screensaverstruct *) calloc(MI_NUM_SCREENS(mi), sizeof (screensaverstruct))) == NULL)
522 gp = &Screensaver[screen];
524 gp->window = MI_WINDOW(mi);
525 if ((gp->glx_context = init_GL(mi)) != NULL) {
526 reshape_screensaver(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
527 initializeGL(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
528 chooseScreensaverExample(mi);
535 /* all sorts of nice cleanup code should go here! */
536 void release_screensaver(ModeInfo * mi)
539 if (Screensaver != NULL) {
540 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
541 /* screensaverstruct *gp = &Screensaver[screen];*/
543 (void) free((void *) Screensaver);