6637512f6b20dc55b66b4f13993f38c3132ccd13
[xscreensaver] / hacks / glx / extrusion.c
1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* extrusion --- extrusion module for xscreensaver */
3 /*-
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.
9  *
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.
15
16  * Revision History:
17  * Tue Oct 19 22:24:47 PDT 1999    Initial creation by David Konerding
18  *                                 <dek@cgl.ucsf.edu>
19  *                                                                 
20  * Notes:
21  * This screensaver requires the GLE ("OpenGL Tubing and Extrusion Library")
22  * which can be obtained from http://www.linas.org/gle/index.html
23   */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #ifdef STANDALONE
30 #define DEFAULTS        "*delay:         20000  \n" \
31                                         "*showFPS:       False  \n" \
32                                         "*wireframe: False   \n"
33
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 */
40
41 #ifdef USE_GL /* whole file */
42
43 #ifdef HAVE_XMU
44 # ifndef VMS
45 #  include <X11/Xmu/Drawing.h>
46 #else  /* VMS */
47 #  include <Xmu/Drawing.h>
48 # endif /* VMS */
49 #endif
50
51 #undef countof
52 #define countof(x) (sizeof((x))/sizeof((*x)))
53
54 #include "xpm-ximage.h"
55 #include "rotator.h"
56 #include "gltrackball.h"
57 #include "extrusion.h"
58
59 #define checkImageWidth 64
60 #define checkImageHeight 64
61
62
63 #define WIDTH 640
64 #define HEIGHT 480
65
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"
72
73 static int do_light;
74 static int do_texture;
75 static int do_tex_qual;
76 static int do_mipmap;
77 static char *which_name;
78 static char *which_image;
79
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 },
92 };
93
94
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},
102 };
103
104
105 static OptionStruct desc[] =
106 {
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)"},
113 };
114
115 ENTRYPOINT ModeSpecOpt extrusion_opts = {countof(opts), opts, countof(vars), vars, desc};
116
117 #ifdef USE_MODULES
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};
123 #endif
124
125
126 /* structure for holding the extrusion data */
127 typedef struct {
128   int screen_width, screen_height;
129   GLXContext *glx_context;
130   rotator *rot;
131   trackball_state *trackball;
132   Bool button_down_p;
133   Bool button2_down_p;
134   int mouse_start_x, mouse_start_y;
135   int mouse_x, mouse_y;
136   int mouse_dx, mouse_dy;
137   Window window;
138   XColor fg, bg;
139   int extrusion_number;
140 } extrusionstruct;
141
142 static extrusionstruct *Extrusion = NULL;
143
144
145
146 /* set up a light */
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}; 
149
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}; 
152
153 float rot_x=0, rot_y=0, rot_z=0;
154 float lastx=0, lasty=0;
155
156 static float max_lastx=400,  max_lasty=400;
157 static float min_lastx=-400, min_lasty=-400;
158
159 struct functions {
160   void (*InitStuff)(void);
161   void (*DrawStuff)(void);
162   char *name;
163 };
164
165 /* currently joinoffset and twistoid look funny-
166    like we're looking at them from the back or something
167 */
168
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"},
177 };
178
179 static int num_extrusions = countof(funcs_ptr);
180
181
182 /* BEGINNING OF FUNCTIONS */
183
184
185 static GLubyte *
186 Generate_Image(int *width, int *height, int *format)
187 {
188   GLubyte *result;
189   int i, j, c;
190   int counter=0;
191
192   *width = checkImageWidth;
193   *height = checkImageHeight;
194   result = (GLubyte *)malloc(4 * (*width) * (*height));
195
196   counter = 0;
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;
204     }
205   }
206
207   *format = GL_RGBA;
208   return result;
209 }
210
211
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)
215 {
216   int height, width;
217   GLubyte *image;
218   int format;
219
220   if ( !strncmp(filename, "BUILTIN", 7))
221     image = Generate_Image(&width, &height, &format);
222   else
223     {
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;
229       format = GL_RGBA;
230     }
231
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);
239
240   if (do_tex_qual) {
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);
244   }
245   else {
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);
250   }
251
252   /* mipmaps make the image look much nicer */
253   if (do_mipmap)
254     {
255       int status;
256       clear_gl_error();
257       status = gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width, height, format,
258                                  GL_UNSIGNED_BYTE, image);
259       if (status)
260         {
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)"));
265           exit (1);
266         }
267       check_gl_error("mipmapping");
268     }
269   else
270     {
271       clear_gl_error();
272       glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0,
273                    format, GL_UNSIGNED_BYTE, image);
274       check_gl_error("texture");
275     }
276 }
277
278
279 static void
280 init_rotation (ModeInfo *mi)
281 {
282   extrusionstruct *gp = &Extrusion[MI_SCREEN(mi)];
283   double spin_speed = 0.5;
284   gp->rot = make_rotator (spin_speed, spin_speed, spin_speed,
285                           0.2,
286                           0.005,
287                           True);
288   gp->trackball = gltrackball_init ();
289
290   lastx = (random() % (int) (max_lastx - min_lastx)) + min_lastx;
291   lasty = (random() % (int) (max_lasty - min_lasty)) + min_lasty;
292 }
293
294
295 /* draw the extrusion once */
296 ENTRYPOINT void
297 draw_extrusion(ModeInfo * mi)
298 {
299   extrusionstruct *gp = &Extrusion[MI_SCREEN(mi)];
300   Display    *display = MI_DISPLAY(mi);
301   Window      window = MI_WINDOW(mi);
302
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; */
306
307   double x, y, z;
308
309   if (!gp->glx_context)
310         return;
311
312   glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(gp->glx_context));
313
314   glPushMatrix();
315
316   gltrackball_rotate (gp->trackball);
317
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);
323
324   /* track the mouse only if a button is down. */
325   if (gp->button2_down_p)
326     {
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;
331     }
332
333   {
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;
339   }
340
341   glScalef(0.5, 0.5, 0.5);
342
343   /* glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR,            spec); */
344   /* glMateriali  (GL_FRONT_AND_BACK, GL_SHININESS,           shiny); */
345
346   glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
347   glFrontFace(GL_CCW);
348
349   funcs_ptr[gp->extrusion_number].DrawStuff();
350           
351   glPopMatrix();
352
353   if (mi->fps_p) do_fps (mi);
354   glXSwapBuffers(display, window);
355 }
356
357
358 /* set up lighting conditions */
359 static void
360 SetupLight(void)
361 {
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);
366
367   glEnable (GL_LIGHT0);
368   glEnable (GL_LIGHT1);
369   glEnable (GL_LIGHTING);
370
371   glColorMaterial (GL_FRONT, GL_DIFFUSE);
372   glColorMaterial (GL_BACK, GL_DIFFUSE);
373   glEnable (GL_COLOR_MATERIAL);
374 }
375
376 /* Standard reshape function */
377 ENTRYPOINT void
378 reshape_extrusion (ModeInfo *mi, int width, int height)
379 {
380   GLfloat h = (GLfloat) height / (GLfloat) width;
381
382   glViewport (0, 0, (GLint) width, (GLint) height);
383
384   glMatrixMode(GL_PROJECTION);
385   glLoadIdentity();
386   gluPerspective (30.0, 1/h, 1.0, 100.0);
387
388   glMatrixMode(GL_MODELVIEW);
389   glLoadIdentity();
390   gluLookAt( 0.0, 0.0, 30.0,
391              0.0, 0.0, 0.0,
392              0.0, 1.0, 0.0);
393
394   glClear(GL_COLOR_BUFFER_BIT);
395 }
396
397
398 /* decide which extrusion example to run */
399 static void
400 chooseExtrusionExample (ModeInfo *mi)
401 {
402   extrusionstruct *gp = &Extrusion[MI_SCREEN(mi)];
403   int i;
404   /* call the extrusion init routine */
405
406   if (!strncmp(which_name, "RANDOM", strlen(which_name))) {
407     gp->extrusion_number = random() % num_extrusions;
408   }
409   else {
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;
414           }
415         }         
416   }
417         
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);
423         exit(1);
424   }
425   init_rotation(mi);
426   funcs_ptr[gp->extrusion_number].InitStuff();
427 }
428
429
430 /* main OpenGL initialization routine */
431 static void
432 initializeGL(ModeInfo *mi, GLsizei width, GLsizei height) 
433 {
434   int style;
435   int mode;
436
437   reshape_extrusion(mi, width, height);
438   glViewport( 0, 0, width, height ); 
439
440   glEnable(GL_DEPTH_TEST);
441   glClearColor(0,0,0,0);
442   glDisable (GL_CULL_FACE);
443   glLightModeli (GL_LIGHT_MODEL_TWO_SIDE, True);
444   glShadeModel(GL_SMOOTH);
445
446   if (do_light)
447         SetupLight();
448   if (MI_IS_WIREFRAME(mi)) {
449         glPolygonMode(GL_FRONT,GL_LINE);
450         glPolygonMode(GL_BACK,GL_LINE);
451   }
452   if (do_texture) {
453         Create_Texture(mi, which_image);
454         glEnable(GL_TEXTURE_2D);
455
456         /* configure the pipeline */
457         style = TUBE_JN_CAP;
458         style |= TUBE_CONTOUR_CLOSED;
459         style |= TUBE_NORM_FACET;
460         style |= TUBE_JN_ANGLE;
461         gleSetJoinStyle (style);
462
463         if (do_texture) {
464           mode = GLE_TEXTURE_ENABLE | GLE_TEXTURE_VERTEX_MODEL_FLAT;
465           glMatrixMode (GL_TEXTURE); glLoadIdentity ();
466           glScalef (0.25, 0.1, 1); glMatrixMode (GL_MODELVIEW);
467           gleTextureMode (mode);
468         }
469   }
470
471 }
472
473 ENTRYPOINT Bool
474 extrusion_handle_event (ModeInfo *mi, XEvent *event)
475 {
476   extrusionstruct *gp = &Extrusion[MI_SCREEN(mi)];
477
478   if (event->xany.type == ButtonPress &&
479       (event->xbutton.button == Button4 ||
480        event->xbutton.button == Button5))
481     {
482       gltrackball_mousewheel (gp->trackball, event->xbutton.button, 10,
483                               !!event->xbutton.state);
484       return True;
485     }
486   else if (event->xany.type == ButtonPress &&   /* rotate with left button */
487            !event->xbutton.state)               /* if no modifier keys */
488     {
489       gp->button_down_p = True;
490       gltrackball_start (gp->trackball,
491                          event->xbutton.x, event->xbutton.y,
492                          MI_WIDTH (mi), MI_HEIGHT (mi));
493       return True;
494     }
495   else if (event->xany.type == ButtonPress)  /* deform with other buttons */
496     {                                        /* or with modifier keys */
497       gp->button2_down_p = True;
498       gp->mouse_start_x = gp->mouse_x = event->xbutton.x;
499       gp->mouse_start_y = gp->mouse_y = event->xbutton.y;
500       return True;
501     }
502   else if (event->xany.type == ButtonRelease)
503     {
504       gp->button_down_p = False;
505       gp->button2_down_p = False;
506       return True;
507     }
508   else if (event->xany.type == MotionNotify)
509     {
510       if (gp->button_down_p)
511         gltrackball_track (gp->trackball,
512                            event->xmotion.x, event->xmotion.y,
513                            MI_WIDTH (mi), MI_HEIGHT (mi));
514       if (gp->button2_down_p)
515         {
516           gp->mouse_x = event->xmotion.x;
517           gp->mouse_y = event->xmotion.y;
518         }
519       return True;
520     }
521
522   return False;
523 }
524
525
526 /* xextrusion initialization routine */
527 ENTRYPOINT void
528 init_extrusion (ModeInfo * mi)
529 {
530   int screen = MI_SCREEN(mi);
531   extrusionstruct *gp;
532
533   if (MI_IS_WIREFRAME(mi)) do_light = 0;
534
535   if (Extrusion == NULL) {
536         if ((Extrusion = (extrusionstruct *)
537          calloc(MI_NUM_SCREENS(mi), sizeof (extrusionstruct))) == NULL)
538           return;
539   }
540   gp = &Extrusion[screen];
541
542   gp->window = MI_WINDOW(mi);
543   if ((gp->glx_context = init_GL(mi)) != NULL) {
544         reshape_extrusion(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
545         initializeGL(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
546         chooseExtrusionExample(mi);
547   } else {
548         MI_CLEARWINDOW(mi);
549   }
550
551 }
552
553 XSCREENSAVER_MODULE ("Extrusion", extrusion)
554
555 #endif  /* USE_GL */