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 DEFAULTS "*delay: 20000 \n" \
31 "*showFPS: False \n" \
32 "*wireframe: False \n"
34 # define refresh_extrusion 0
35 # define release_extrusion 0
36 # include "xlockmore.h" /* from the xscreensaver distribution */
37 #else /* !STANDALONE */
38 # include "xlock.h" /* from the xlockmore distribution */
39 #endif /* !STANDALONE */
41 #ifdef USE_GL /* whole file */
45 # include <X11/Xmu/Drawing.h>
47 # include <Xmu/Drawing.h>
52 #define countof(x) (sizeof((x))/sizeof((*x)))
54 #include "xpm-ximage.h"
56 #include "gltrackball.h"
57 #include "extrusion.h"
59 #define checkImageWidth 64
60 #define checkImageHeight 64
66 #define DEF_LIGHT "True"
67 #define DEF_TEXTURE "False"
68 #define DEF_TEX_QUAL "False"
69 #define DEF_MIPMAP "False"
70 #define DEF_NAME "RANDOM"
71 #define DEF_IMAGE "BUILTIN"
74 static int do_texture;
75 static int do_tex_qual;
77 static char *which_name;
78 static char *which_image;
80 static XrmOptionDescRec opts[] = {
81 {"-light", ".extrusion.light", XrmoptionNoArg, "true" },
82 {"+light", ".extrusion.light", XrmoptionNoArg, "false" },
83 {"-texture", ".extrusion.texture", XrmoptionNoArg, "true" },
84 {"+texture", ".extrusion.texture", XrmoptionNoArg, "false" },
85 {"-texture", ".extrusion.texture", XrmoptionNoArg, "true" },
86 {"+texture_quality", ".extrusion.texture", XrmoptionNoArg, "false" },
87 {"-texture_quality", ".extrusion.texture", XrmoptionNoArg, "true" },
88 {"+mipmap", ".extrusion.mipmap", XrmoptionNoArg, "false" },
89 {"-mipmap", ".extrusion.mipmap", XrmoptionNoArg, "true" },
90 {"-name", ".extrusion.name", XrmoptionSepArg, 0 },
91 {"-image", ".extrusion.image", XrmoptionSepArg, 0 },
95 static argtype vars[] = {
96 {&do_light, "light", "Light", DEF_LIGHT, t_Bool},
97 {&do_texture, "texture", "Texture", DEF_TEXTURE, t_Bool},
98 {&do_tex_qual, "texture_quality", "Texture_Quality", DEF_TEX_QUAL, t_Bool},
99 {&do_mipmap, "mipmap", "Mipmap", DEF_MIPMAP, t_Bool},
100 {&which_name, "name", "Name", DEF_NAME, t_String},
101 {&which_image, "image", "Image", DEF_IMAGE, t_String},
105 static OptionStruct desc[] =
107 {"-name num", "example 'name' to draw (helix2, helix3, helix4, joinoffset, screw, taper, twistoid)"},
108 {"-/+ light", "whether to do enable lighting (slower)"},
109 {"-/+ texture", "whether to apply a texture (slower)"},
110 {"-image <filename>", "texture image to load"},
111 {"-/+ texture_quality", "whether to use texture smoothing (slower)"},
112 {"-/+ mipmap", "whether to use texture mipmap (slower)"},
115 ENTRYPOINT ModeSpecOpt extrusion_opts = {countof(opts), opts, countof(vars), vars, desc};
118 ModStruct extrusion_description =
119 {"extrusion", "init_extrusion", "draw_extrusion", "release_extrusion",
120 "draw_extrusion", "init_extrusion", NULL, &extrusion_opts,
121 1000, 1, 2, 1, 4, 1.0, "",
122 "OpenGL extrusion", 0, NULL};
126 /* structure for holding the extrusion data */
128 int screen_width, screen_height;
129 GLXContext *glx_context;
131 trackball_state *trackball;
134 int mouse_start_x, mouse_start_y;
135 int mouse_x, mouse_y;
136 int mouse_dx, mouse_dy;
139 int extrusion_number;
142 static extrusionstruct *Extrusion = NULL;
147 static const GLfloat lightOnePosition[] = {40.0, 40, 100.0, 0.0};
148 static const GLfloat lightOneColor[] = {0.99, 0.99, 0.00, 1.0};
150 static const GLfloat lightTwoPosition[] = {-40.0, 40, 100.0, 0.0};
151 static const GLfloat lightTwoColor[] = {0.00, 0.99, 0.99, 1.0};
153 float rot_x=0, rot_y=0, rot_z=0;
154 float lastx=0, lasty=0;
156 static float max_lastx=400, max_lasty=400;
157 static float min_lastx=-400, min_lasty=-400;
160 void (*InitStuff)(void);
161 void (*DrawStuff)(void);
165 /* currently joinoffset and twistoid look funny-
166 like we're looking at them from the back or something
169 static const struct functions funcs_ptr[] = {
170 {InitStuff_helix2, DrawStuff_helix2, "helix2"},
171 {InitStuff_helix3, DrawStuff_helix3, "helix3"},
172 {InitStuff_helix4, DrawStuff_helix4, "helix4"},
173 {InitStuff_joinoffset, DrawStuff_joinoffset, "joinoffset"},
174 {InitStuff_screw, DrawStuff_screw, "screw"},
175 {InitStuff_taper, DrawStuff_taper, "taper"},
176 {InitStuff_twistoid, DrawStuff_twistoid, "twistoid"},
179 static int num_extrusions = countof(funcs_ptr);
182 /* BEGINNING OF FUNCTIONS */
186 Generate_Image(int *width, int *height, int *format)
192 *width = checkImageWidth;
193 *height = checkImageHeight;
194 result = (GLubyte *)malloc(4 * (*width) * (*height));
197 for (i = 0; i < checkImageWidth; i++) {
198 for (j = 0; j < checkImageHeight; j++) {
199 c = (((((i&0x8)==0))^(((j&0x8))==0)))*255;
200 result[counter++] = (GLubyte) c;
201 result[counter++] = (GLubyte) c;
202 result[counter++] = (GLubyte) c;
203 result[counter++] = (GLubyte) 255;
212 /* Create a texture in OpenGL. First an image is loaded
213 and stored in a raster buffer, then it's */
214 static void Create_Texture(ModeInfo *mi, const char *filename)
220 if ( !strncmp(filename, "BUILTIN", 7))
221 image = Generate_Image(&width, &height, &format);
224 XImage *ximage = xpm_file_to_ximage (MI_DISPLAY (mi), MI_VISUAL (mi),
225 MI_COLORMAP (mi), filename);
226 image = (GLubyte *) ximage->data;
227 width = ximage->width;
228 height = ximage->height;
232 /* GL_MODULATE or GL_DECAL depending on what you want */
233 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
234 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
235 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
236 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
237 /* perhaps we can edge a bit more speed at the expense of quality */
238 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
241 /* with texture_quality, the min and mag filters look *much* nice but are *much* slower */
242 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
243 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
246 /* default is to do it quick and dirty */
247 /* if you have mipmaps turned on, but not texture quality, nothing will happen! */
248 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
249 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
252 /* mipmaps make the image look much nicer */
257 status = gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width, height, format,
258 GL_UNSIGNED_BYTE, image);
261 const char *s = (char *) gluErrorString (status);
262 fprintf (stderr, "%s: error mipmapping %dx%d texture: %s\n",
263 progname, width, height,
264 (s ? s : "(unknown)"));
267 check_gl_error("mipmapping");
272 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0,
273 format, GL_UNSIGNED_BYTE, image);
274 check_gl_error("texture");
280 init_rotation (ModeInfo *mi)
282 extrusionstruct *gp = &Extrusion[MI_SCREEN(mi)];
283 double spin_speed = 0.5;
284 gp->rot = make_rotator (spin_speed, spin_speed, spin_speed,
288 gp->trackball = gltrackball_init ();
290 lastx = (random() % (int) (max_lastx - min_lastx)) + min_lastx;
291 lasty = (random() % (int) (max_lasty - min_lasty)) + min_lasty;
295 /* draw the extrusion once */
297 draw_extrusion(ModeInfo * mi)
299 extrusionstruct *gp = &Extrusion[MI_SCREEN(mi)];
300 Display *display = MI_DISPLAY(mi);
301 Window window = MI_WINDOW(mi);
303 static const GLfloat color[4] = {0.6, 0.6, 0.4, 1.0};
304 /* static const GLfloat spec[4] = {0.6, 0.6, 0.6, 1.0}; */
305 /* static const GLfloat shiny = 40.0; */
309 if (!gp->glx_context)
312 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(gp->glx_context));
316 gltrackball_rotate (gp->trackball);
318 get_rotation (gp->rot, &x, &y, &z,
319 !(gp->button_down_p || gp->button2_down_p));
320 glRotatef (x * 360, 1.0, 0.0, 0.0);
321 glRotatef (y * 360, 0.0, 1.0, 0.0);
322 glRotatef (z * 360, 0.0, 0.0, 1.0);
324 /* track the mouse only if a button is down. */
325 if (gp->button2_down_p)
327 gp->mouse_dx += gp->mouse_x - gp->mouse_start_x;
328 gp->mouse_dy += gp->mouse_y - gp->mouse_start_y;
329 gp->mouse_start_x = gp->mouse_x;
330 gp->mouse_start_y = gp->mouse_y;
334 float scale = (max_lastx - min_lastx);
335 get_position (gp->rot, &x, &y, &z,
336 !(gp->button_down_p || gp->button2_down_p));
337 lastx = x * scale + min_lastx + gp->mouse_dx;
338 lasty = y * scale + min_lasty + gp->mouse_dy;
341 glScalef(0.5, 0.5, 0.5);
343 /* glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, spec); */
344 /* glMateriali (GL_FRONT_AND_BACK, GL_SHININESS, shiny); */
346 glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
349 funcs_ptr[gp->extrusion_number].DrawStuff();
353 if (mi->fps_p) do_fps (mi);
354 glXSwapBuffers(display, window);
358 /* set up lighting conditions */
362 glLightfv (GL_LIGHT0, GL_POSITION, lightOnePosition);
363 glLightfv (GL_LIGHT0, GL_DIFFUSE, lightOneColor);
364 glLightfv (GL_LIGHT1, GL_POSITION, lightTwoPosition);
365 glLightfv (GL_LIGHT1, GL_DIFFUSE, lightTwoColor);
367 glEnable (GL_LIGHT0);
368 glEnable (GL_LIGHT1);
369 glEnable (GL_LIGHTING);
371 glColorMaterial (GL_FRONT, GL_DIFFUSE);
372 glColorMaterial (GL_BACK, GL_DIFFUSE);
373 glEnable (GL_COLOR_MATERIAL);
376 /* Standard reshape function */
378 reshape_extrusion (ModeInfo *mi, int width, int height)
380 GLfloat h = (GLfloat) height / (GLfloat) width;
382 glViewport (0, 0, (GLint) width, (GLint) height);
384 glMatrixMode(GL_PROJECTION);
386 gluPerspective (30.0, 1/h, 1.0, 100.0);
388 glMatrixMode(GL_MODELVIEW);
390 gluLookAt( 0.0, 0.0, 30.0,
394 glClear(GL_COLOR_BUFFER_BIT);
398 /* decide which extrusion example to run */
400 chooseExtrusionExample (ModeInfo *mi)
402 extrusionstruct *gp = &Extrusion[MI_SCREEN(mi)];
404 /* call the extrusion init routine */
406 if (!strncmp(which_name, "RANDOM", strlen(which_name))) {
407 gp->extrusion_number = random() % num_extrusions;
410 gp->extrusion_number=-1;
411 for (i=0; i < num_extrusions; i++) {
412 if (!strncmp(which_name, funcs_ptr[i].name, strlen(which_name))) {
413 gp->extrusion_number = i;
418 if (gp->extrusion_number < 0 || gp->extrusion_number >= num_extrusions) {
419 fprintf(stderr, "%s: invalid extrusion example number!\n", progname);
420 fprintf(stderr, "%s: known extrusions:\n", progname);
421 for (i=0; i < num_extrusions; i++)
422 fprintf(stderr,"\t%s\n", funcs_ptr[i].name);
426 funcs_ptr[gp->extrusion_number].InitStuff();
430 /* main OpenGL initialization routine */
432 initializeGL(ModeInfo *mi, GLsizei width, GLsizei height)
437 reshape_extrusion(mi, width, height);
438 glViewport( 0, 0, width, height );
440 glEnable(GL_DEPTH_TEST);
441 glDisable (GL_CULL_FACE);
442 glLightModeli (GL_LIGHT_MODEL_TWO_SIDE, True);
443 glShadeModel(GL_SMOOTH);
447 if (MI_IS_WIREFRAME(mi)) {
448 glPolygonMode(GL_FRONT,GL_LINE);
449 glPolygonMode(GL_BACK,GL_LINE);
452 Create_Texture(mi, which_image);
453 glEnable(GL_TEXTURE_2D);
455 /* configure the pipeline */
457 style |= TUBE_CONTOUR_CLOSED;
458 style |= TUBE_NORM_FACET;
459 style |= TUBE_JN_ANGLE;
460 gleSetJoinStyle (style);
463 mode = GLE_TEXTURE_ENABLE | GLE_TEXTURE_VERTEX_MODEL_FLAT;
464 glMatrixMode (GL_TEXTURE); glLoadIdentity ();
465 glScalef (0.25, 0.1, 1); glMatrixMode (GL_MODELVIEW);
466 gleTextureMode (mode);
473 extrusion_handle_event (ModeInfo *mi, XEvent *event)
475 extrusionstruct *gp = &Extrusion[MI_SCREEN(mi)];
477 if (event->xany.type == ButtonPress &&
478 (event->xbutton.button == Button4 ||
479 event->xbutton.button == Button5 ||
480 event->xbutton.button == Button6 ||
481 event->xbutton.button == Button7))
483 gltrackball_mousewheel (gp->trackball, event->xbutton.button, 10,
484 !!event->xbutton.state);
487 else if (event->xany.type == ButtonPress && /* rotate with left button */
488 !event->xbutton.state) /* if no modifier keys */
490 gp->button_down_p = True;
491 gltrackball_start (gp->trackball,
492 event->xbutton.x, event->xbutton.y,
493 MI_WIDTH (mi), MI_HEIGHT (mi));
496 else if (event->xany.type == ButtonPress) /* deform with other buttons */
497 { /* or with modifier keys */
498 gp->button2_down_p = True;
499 gp->mouse_start_x = gp->mouse_x = event->xbutton.x;
500 gp->mouse_start_y = gp->mouse_y = event->xbutton.y;
503 else if (event->xany.type == ButtonRelease)
505 gp->button_down_p = False;
506 gp->button2_down_p = False;
509 else if (event->xany.type == MotionNotify)
511 if (gp->button_down_p)
512 gltrackball_track (gp->trackball,
513 event->xmotion.x, event->xmotion.y,
514 MI_WIDTH (mi), MI_HEIGHT (mi));
515 if (gp->button2_down_p)
517 gp->mouse_x = event->xmotion.x;
518 gp->mouse_y = event->xmotion.y;
527 /* xextrusion initialization routine */
529 init_extrusion (ModeInfo * mi)
531 int screen = MI_SCREEN(mi);
534 if (MI_IS_WIREFRAME(mi)) do_light = 0;
536 if (Extrusion == NULL) {
537 if ((Extrusion = (extrusionstruct *)
538 calloc(MI_NUM_SCREENS(mi), sizeof (extrusionstruct))) == NULL)
541 gp = &Extrusion[screen];
543 gp->window = MI_WINDOW(mi);
544 if ((gp->glx_context = init_GL(mi)) != NULL) {
545 reshape_extrusion(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
546 initializeGL(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
547 chooseExtrusionExample(mi);
554 XSCREENSAVER_MODULE ("Extrusion", extrusion)