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 "Extrusion"
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: 20000 \n" \
38 "*showFPS: False \n" \
39 "*wireframe: False \n"
41 # include "xlockmore.h" /* from the xscreensaver distribution */
42 #else /* !STANDALONE */
43 # include "xlock.h" /* from the xlockmore distribution */
44 #endif /* !STANDALONE */
46 #ifdef USE_GL /* whole file */
50 # include <X11/Xmu/Drawing.h>
52 # include <Xmu/Drawing.h>
69 #define countof(x) (sizeof((x))/sizeof((*x)))
71 #include "xpm-ximage.h"
73 #include "gltrackball.h"
75 #define checkImageWidth 64
76 #define checkImageHeight 64
79 extern void InitStuff_helix2(void);
80 extern void DrawStuff_helix2(void);
81 extern void InitStuff_helix3(void);
82 extern void DrawStuff_helix3(void);
83 extern void InitStuff_helix4(void);
84 extern void DrawStuff_helix4(void);
85 extern void InitStuff_joinoffset(void);
86 extern void DrawStuff_joinoffset(void);
87 extern void InitStuff_screw(void);
88 extern void DrawStuff_screw(void);
89 extern void InitStuff_taper(void);
90 extern void DrawStuff_taper(void);
91 extern void InitStuff_twistoid(void);
92 extern void DrawStuff_twistoid(void);
99 #define DEF_LIGHT "True"
100 #define DEF_TEXTURE "False"
101 #define DEF_TEX_QUAL "False"
102 #define DEF_MIPMAP "False"
103 #define DEF_NAME "RANDOM"
104 #define DEF_IMAGE "BUILTIN"
107 static int do_texture;
108 static int do_tex_qual;
109 static int do_mipmap;
110 static char *which_name;
111 static char *which_image;
113 static XrmOptionDescRec opts[] = {
114 {"-light", ".extrusion.light", XrmoptionNoArg, "true" },
115 {"+light", ".extrusion.light", XrmoptionNoArg, "false" },
116 {"-texture", ".extrusion.texture", XrmoptionNoArg, "true" },
117 {"+texture", ".extrusion.texture", XrmoptionNoArg, "false" },
118 {"-texture", ".extrusion.texture", XrmoptionNoArg, "true" },
119 {"+texture_quality", ".extrusion.texture", XrmoptionNoArg, "false" },
120 {"-texture_quality", ".extrusion.texture", XrmoptionNoArg, "true" },
121 {"+mipmap", ".extrusion.mipmap", XrmoptionNoArg, "false" },
122 {"-mipmap", ".extrusion.mipmap", XrmoptionNoArg, "true" },
123 {"-name", ".extrusion.name", XrmoptionSepArg, 0 },
124 {"-image", ".extrusion.image", XrmoptionSepArg, 0 },
128 static argtype vars[] = {
129 {&do_light, "light", "Light", DEF_LIGHT, t_Bool},
130 {&do_texture, "texture", "Texture", DEF_TEXTURE, t_Bool},
131 {&do_tex_qual, "texture_quality", "Texture_Quality", DEF_TEX_QUAL, t_Bool},
132 {&do_mipmap, "mipmap", "Mipmap", DEF_MIPMAP, t_Bool},
133 {&which_name, "name", "Name", DEF_NAME, t_String},
134 {&which_image, "image", "Image", DEF_IMAGE, t_String},
138 static OptionStruct desc[] =
140 {"-name num", "example 'name' to draw (helix2, helix3, helix4, joinoffset, screw, taper, twistoid)"},
141 {"-/+ light", "whether to do enable lighting (slower)"},
142 {"-/+ texture", "whether to apply a texture (slower)"},
143 {"-image <filename>", "texture image to load"},
144 {"-/+ texture_quality", "whether to use texture smoothing (slower)"},
145 {"-/+ mipmap", "whether to use texture mipmap (slower)"},
148 ModeSpecOpt screensaver_opts = {countof(opts), opts, countof(vars), vars, desc};
151 ModStruct screensaver_description =
152 {"screensaver", "init_screensaver", "draw_screensaver", "release_screensaver",
153 "draw_screensaver", "init_screensaver", NULL, &screensaver_opts,
154 1000, 1, 2, 1, 4, 1.0, "",
155 "OpenGL screensaver", 0, NULL};
159 /* structure for holding the screensaver data */
161 int screen_width, screen_height;
162 GLXContext *glx_context;
164 trackball_state *trackball;
167 int mouse_start_x, mouse_start_y;
168 int mouse_x, mouse_y;
169 int mouse_dx, mouse_dy;
174 static screensaverstruct *Screensaver = NULL;
179 static GLfloat lightOnePosition[] = {40.0, 40, 100.0, 0.0};
180 static GLfloat lightOneColor[] = {0.99, 0.99, 0.00, 1.0};
182 static GLfloat lightTwoPosition[] = {-40.0, 40, 100.0, 0.0};
183 static GLfloat lightTwoColor[] = {0.00, 0.99, 0.99, 1.0};
185 float rot_x=0, rot_y=0, rot_z=0;
186 float lastx=0, lasty=0;
188 static float max_lastx=400, max_lasty=400;
189 static float min_lastx=-400, min_lasty=-400;
191 static int screensaver_number;
194 void (*InitStuff)(void);
195 void (*DrawStuff)(void);
199 /* currently joinoffset and twistoid look funny-
200 like we're looking at them from the back or something
203 static struct functions funcs_ptr[] = {
204 {InitStuff_helix2, DrawStuff_helix2, "helix2"},
205 {InitStuff_helix3, DrawStuff_helix3, "helix3"},
206 {InitStuff_helix4, DrawStuff_helix4, "helix4"},
207 {InitStuff_joinoffset, DrawStuff_joinoffset, "joinoffset"},
208 {InitStuff_screw, DrawStuff_screw, "screw"},
209 {InitStuff_taper, DrawStuff_taper, "taper"},
210 {InitStuff_twistoid, DrawStuff_twistoid, "twistoid"},
213 static int num_screensavers = countof(funcs_ptr);
216 /* BEGINNING OF FUNCTIONS */
220 Generate_Image(int *width, int *height, int *format)
226 *width = checkImageWidth;
227 *height = checkImageHeight;
228 result = (GLubyte *)malloc(4 * (*width) * (*height));
231 for (i = 0; i < checkImageWidth; i++) {
232 for (j = 0; j < checkImageHeight; j++) {
233 c = (((((i&0x8)==0))^(((j&0x8))==0)))*255;
234 result[counter++] = (GLubyte) c;
235 result[counter++] = (GLubyte) c;
236 result[counter++] = (GLubyte) c;
237 result[counter++] = (GLubyte) 255;
246 /* Create a texture in OpenGL. First an image is loaded
247 and stored in a raster buffer, then it's */
248 void Create_Texture(ModeInfo *mi, const char *filename)
254 if ( !strncmp(filename, "BUILTIN", 7))
255 image = Generate_Image(&width, &height, &format);
258 XImage *ximage = xpm_file_to_ximage (MI_DISPLAY (mi), MI_VISUAL (mi),
259 MI_COLORMAP (mi), filename);
260 image = (GLubyte *) ximage->data;
261 width = ximage->width;
262 height = ximage->height;
266 /* GL_MODULATE or GL_DECAL depending on what you want */
267 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
268 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
269 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
270 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
271 /* perhaps we can edge a bit more speed at the expense of quality */
272 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
275 /* with texture_quality, the min and mag filters look *much* nice but are *much* slower */
276 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
277 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
280 /* default is to do it quick and dirty */
281 /* if you have mipmaps turned on, but not texture quality, nothing will happen! */
282 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
283 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
286 /* mipmaps make the image look much nicer */
291 status = gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width, height, format,
292 GL_UNSIGNED_BYTE, image);
295 const char *s = (char *) gluErrorString (status);
296 fprintf (stderr, "%s: error mipmapping %dx%d texture: %s\n",
297 progname, width, height,
298 (s ? s : "(unknown)"));
301 check_gl_error("mipmapping");
306 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0,
307 format, GL_UNSIGNED_BYTE, image);
308 check_gl_error("texture");
314 init_rotation (ModeInfo *mi)
316 screensaverstruct *gp = &Screensaver[MI_SCREEN(mi)];
317 double spin_speed = 0.5;
318 gp->rot = make_rotator (spin_speed, spin_speed, spin_speed,
322 gp->trackball = gltrackball_init ();
324 lastx = (random() % (int) (max_lastx - min_lastx)) + min_lastx;
325 lasty = (random() % (int) (max_lasty - min_lasty)) + min_lasty;
329 /* draw the screensaver once */
331 draw_screensaver(ModeInfo * mi)
333 screensaverstruct *gp = &Screensaver[MI_SCREEN(mi)];
334 Display *display = MI_DISPLAY(mi);
335 Window window = MI_WINDOW(mi);
337 static GLfloat color[4] = {0.6, 0.6, 0.4, 1.0};
338 /* static GLfloat spec[4] = {0.6, 0.6, 0.6, 1.0}; */
339 /* static GLfloat shiny = 40.0; */
343 if (!gp->glx_context)
348 gltrackball_rotate (gp->trackball);
350 get_rotation (gp->rot, &x, &y, &z,
351 !(gp->button_down_p || gp->button2_down_p));
352 glRotatef (x * 360, 1.0, 0.0, 0.0);
353 glRotatef (y * 360, 0.0, 1.0, 0.0);
354 glRotatef (z * 360, 0.0, 0.0, 1.0);
356 /* track the mouse only if a button is down. */
357 if (gp->button2_down_p)
359 gp->mouse_dx += gp->mouse_x - gp->mouse_start_x;
360 gp->mouse_dy += gp->mouse_y - gp->mouse_start_y;
361 gp->mouse_start_x = gp->mouse_x;
362 gp->mouse_start_y = gp->mouse_y;
366 float scale = (max_lastx - min_lastx);
367 get_position (gp->rot, &x, &y, &z,
368 !(gp->button_down_p || gp->button2_down_p));
369 lastx = x * scale + min_lastx + gp->mouse_dx;
370 lasty = y * scale + min_lasty + gp->mouse_dy;
373 glScalef(0.5, 0.5, 0.5);
375 /* glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, spec); */
376 /* glMateriali (GL_FRONT_AND_BACK, GL_SHININESS, shiny); */
378 glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
381 funcs_ptr[screensaver_number].DrawStuff();
385 if (mi->fps_p) do_fps (mi);
386 glXSwapBuffers(display, window);
390 /* set up lighting conditions */
394 glLightfv (GL_LIGHT0, GL_POSITION, lightOnePosition);
395 glLightfv (GL_LIGHT0, GL_DIFFUSE, lightOneColor);
396 glLightfv (GL_LIGHT1, GL_POSITION, lightTwoPosition);
397 glLightfv (GL_LIGHT1, GL_DIFFUSE, lightTwoColor);
399 glEnable (GL_LIGHT0);
400 glEnable (GL_LIGHT1);
401 glEnable (GL_LIGHTING);
403 glColorMaterial (GL_FRONT, GL_DIFFUSE);
404 glColorMaterial (GL_BACK, GL_DIFFUSE);
405 glEnable (GL_COLOR_MATERIAL);
408 /* Standard reshape function */
410 reshape_screensaver (ModeInfo *mi, int width, int height)
412 GLfloat h = (GLfloat) height / (GLfloat) width;
414 glViewport (0, 0, (GLint) width, (GLint) height);
416 glMatrixMode(GL_PROJECTION);
418 gluPerspective (30.0, 1/h, 1.0, 100.0);
420 glMatrixMode(GL_MODELVIEW);
422 gluLookAt( 0.0, 0.0, 30.0,
426 glClear(GL_COLOR_BUFFER_BIT);
430 /* decide which screensaver example to run */
432 chooseScreensaverExample (ModeInfo *mi)
435 /* call the extrusion init routine */
437 if (!strncmp(which_name, "RANDOM", strlen(which_name))) {
438 screensaver_number = random() % num_screensavers;
441 screensaver_number=-1;
442 for (i=0; i < num_screensavers; i++) {
443 if (!strncmp(which_name, funcs_ptr[i].name, strlen(which_name))) {
444 screensaver_number = i;
449 if (screensaver_number < 0 || screensaver_number >= num_screensavers) {
450 fprintf(stderr, "%s: invalid screensaver example number!\n", progname);
451 fprintf(stderr, "%s: known screensavers:\n", progname);
452 for (i=0; i < num_screensavers; i++)
453 fprintf(stderr,"\t%s\n", funcs_ptr[i].name);
457 funcs_ptr[screensaver_number].InitStuff();
461 /* main OpenGL initialization routine */
463 initializeGL(ModeInfo *mi, GLsizei width, GLsizei height)
468 reshape_screensaver(mi, width, height);
469 glViewport( 0, 0, width, height );
471 glEnable(GL_DEPTH_TEST);
472 glClearColor(0,0,0,0);
473 glDisable (GL_CULL_FACE);
474 glLightModeli (GL_LIGHT_MODEL_TWO_SIDE, True);
475 glShadeModel(GL_SMOOTH);
479 if (MI_IS_WIREFRAME(mi)) {
480 glPolygonMode(GL_FRONT,GL_LINE);
481 glPolygonMode(GL_BACK,GL_LINE);
484 Create_Texture(mi, which_image);
485 glEnable(GL_TEXTURE_2D);
487 /* configure the pipeline */
489 style |= TUBE_CONTOUR_CLOSED;
490 style |= TUBE_NORM_FACET;
491 style |= TUBE_JN_ANGLE;
492 gleSetJoinStyle (style);
495 mode = GLE_TEXTURE_ENABLE | GLE_TEXTURE_VERTEX_MODEL_FLAT;
496 glMatrixMode (GL_TEXTURE); glLoadIdentity ();
497 glScalef (0.25, 0.1, 1); glMatrixMode (GL_MODELVIEW);
498 gleTextureMode (mode);
505 screensaver_handle_event (ModeInfo *mi, XEvent *event)
507 screensaverstruct *gp = &Screensaver[MI_SCREEN(mi)];
509 if (event->xany.type == ButtonPress &&
510 event->xbutton.button == Button1)
512 gp->button_down_p = True;
513 gltrackball_start (gp->trackball,
514 event->xbutton.x, event->xbutton.y,
515 MI_WIDTH (mi), MI_HEIGHT (mi));
518 else if (event->xany.type == ButtonRelease &&
519 event->xbutton.button == Button1)
521 gp->button_down_p = False;
524 else if (event->xany.type == ButtonPress &&
525 (event->xbutton.button == Button4 ||
526 event->xbutton.button == Button5))
528 gltrackball_mousewheel (gp->trackball, event->xbutton.button, 10,
529 !!event->xbutton.state);
532 else if (event->xany.type == ButtonPress &&
533 event->xbutton.button != Button1)
535 gp->button2_down_p = True;
536 gp->mouse_start_x = gp->mouse_x = event->xbutton.x;
537 gp->mouse_start_y = gp->mouse_y = event->xbutton.y;
540 else if (event->xany.type == ButtonRelease &&
541 event->xbutton.button != Button1)
543 gp->button2_down_p = False;
546 else if (event->xany.type == MotionNotify)
548 if (gp->button_down_p)
549 gltrackball_track (gp->trackball,
550 event->xmotion.x, event->xmotion.y,
551 MI_WIDTH (mi), MI_HEIGHT (mi));
552 if (gp->button2_down_p)
554 gp->mouse_x = event->xmotion.x;
555 gp->mouse_y = event->xmotion.y;
564 /* xscreensaver initialization routine */
566 init_screensaver (ModeInfo * mi)
568 int screen = MI_SCREEN(mi);
569 screensaverstruct *gp;
571 if (MI_IS_WIREFRAME(mi)) do_light = 0;
573 if (Screensaver == NULL) {
574 if ((Screensaver = (screensaverstruct *)
575 calloc(MI_NUM_SCREENS(mi), sizeof (screensaverstruct))) == NULL)
578 gp = &Screensaver[screen];
580 gp->window = MI_WINDOW(mi);
581 if ((gp->glx_context = init_GL(mi)) != NULL) {
582 reshape_screensaver(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
583 initializeGL(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
584 chooseScreensaverExample(mi);