fd9f0f4891b5bcf084c3da5308ff07b6398a0d31
[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 /*-
26  * due to a Bug/feature in VMS X11/Intrinsic.h has to be placed before xlock.
27  * otherwise caddr_t is not defined correctly
28  */
29
30 #include <X11/Intrinsic.h>
31
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35
36 #ifdef STANDALONE
37 # define PROGCLASS                                              "Screensaver"
38 # define HACK_INIT                                              init_screensaver
39 # define HACK_DRAW                                              draw_screensaver
40 # define HACK_RESHAPE                                   reshape_screensaver
41 # define HACK_HANDLE_EVENT                              screensaver_handle_event
42 # define EVENT_MASK                                             PointerMotionMask
43 # define screensaver_opts                               xlockmore_opts
44 #define DEFAULTS                        "*delay:                        10000   \n" \
45                                                                                 "*showFPS:              False   \n" \
46                                                                                 "*light:                        True    \n" \
47                                         "*wire:                         False   \n" \
48                                         "*texture:                      False   \n" \
49                                                                                 "*image:                        BUILTIN \n" \
50                                         "*name:             RANDOM  \n" \
51                                         "*example:          0       \n"
52
53 # include "xlockmore.h"                         /* from the xscreensaver distribution */
54 #else /* !STANDALONE */
55 # include "xlock.h"                                     /* from the xlockmore distribution */
56 #endif /* !STANDALONE */
57
58 #ifdef USE_GL /* whole file */
59
60 #ifdef HAVE_XMU
61 # ifndef VMS
62 #  include <X11/Xmu/Drawing.h>
63 #else  /* VMS */
64 #  include <Xmu/Drawing.h>
65 # endif /* VMS */
66 #endif
67
68 #include <stdlib.h>
69 #include <string.h>
70 #include <stdio.h>
71 #include <stdlib.h>
72 #include <malloc.h>
73 #include <GL/gl.h>
74 #include <GL/glu.h>
75 #ifdef HAVE_GLE3
76 #include <GL/gle.h>
77 #else
78 #include <GL/tube.h>
79 #endif
80
81 #undef countof
82 #define countof(x) (sizeof((x))/sizeof((*x)))
83
84 #include "xpm-ximage.h"
85 #include "rotator.h"
86
87 #define checkImageWidth 64
88 #define checkImageHeight 64
89
90
91 extern void InitStuff_helix2(void);
92 extern void DrawStuff_helix2(void);
93 extern void InitStuff_helix3(void);
94 extern void DrawStuff_helix3(void);
95 extern void InitStuff_helix4(void);
96 extern void DrawStuff_helix4(void);
97 extern void InitStuff_joinoffset(void);
98 extern void DrawStuff_joinoffset(void);
99 extern void InitStuff_screw(void);
100 extern void DrawStuff_screw(void);
101 extern void InitStuff_taper(void);
102 extern void DrawStuff_taper(void);
103 extern void InitStuff_twistoid(void);
104 extern void DrawStuff_twistoid(void);
105
106
107
108 #define WIDTH 640
109 #define HEIGHT 480
110
111 #define DEF_LIGHT               "True"
112 #define DEF_WIRE                "False"
113 #define DEF_TEXTURE             "False"
114 #define DEF_TEXTURE_QUALITY   "False"
115 #define DEF_MIPMAP      "False"
116 #define DEF_NAME        "RANDOM"
117 #define DEF_IMAGE       "BUILTIN"
118
119 static int do_light;
120 static int do_wire;
121 static int do_texture;
122 static int do_texture_quality;
123 static int do_mipmap;
124 static char *which_name;
125 static char *which_image;
126
127 static XrmOptionDescRec opts[] = {
128   {"-light",   ".extrusion.light",     XrmoptionNoArg, (caddr_t) "true" },
129   {"+light",   ".extrusion.light",     XrmoptionNoArg, (caddr_t) "false" },
130   {"-wire",    ".extrusion.wire",      XrmoptionNoArg, (caddr_t) "true" },
131   {"+wire",    ".extrusion.wire",      XrmoptionNoArg, (caddr_t) "false" },
132   {"-texture", ".extrusion.texture",   XrmoptionNoArg, (caddr_t) "true" },
133   {"+texture", ".extrusion.texture",   XrmoptionNoArg, (caddr_t) "false" },
134   {"-texture", ".extrusion.texture",   XrmoptionNoArg, (caddr_t) "true" },
135   {"+texture_quality", ".extrusion.texture",   XrmoptionNoArg, (caddr_t) "false" },
136   {"-texture_quality", ".extrusion.texture",   XrmoptionNoArg, (caddr_t) "true" },
137   {"+mipmap", ".extrusion.mipmap",   XrmoptionNoArg, (caddr_t) "false" },
138   {"-mipmap", ".extrusion.mipmap",   XrmoptionNoArg, (caddr_t) "true" },
139   {"-name",   ".extrusion.name",  XrmoptionSepArg, (caddr_t) NULL },
140   {"-image",   ".extrusion.image",  XrmoptionSepArg, (caddr_t) NULL },
141 };
142
143
144 static argtype vars[] = {
145   {(caddr_t *) &do_light,    "light",   "Light",   DEF_LIGHT,   t_Bool},
146   {(caddr_t *) &do_wire,    "wire",   "Wire",   DEF_WIRE,   t_Bool},
147   {(caddr_t *) &do_texture,    "texture",   "Texture",   DEF_TEXTURE,   t_Bool},
148   {(caddr_t *) &do_texture_quality,    "texture_quality",   "Texture_Quality",   DEF_TEXTURE_QUALITY,   t_Bool},
149   {(caddr_t *) &do_mipmap,    "mipmap",   "Mipmap",   DEF_MIPMAP,   t_Bool},
150   {(caddr_t *) &which_name, "name",   "Name",   DEF_NAME,   t_String},
151   {(caddr_t *) &which_image, "image",   "Image",   DEF_IMAGE,   t_String},
152 };
153
154
155 static OptionStruct desc[] =
156 {
157   {"-name num", "example 'name' to draw (helix2, helix3, helix4, joinoffset, screw, taper, twistoid)"},
158   {"-/+ light", "whether to do enable lighting (slower)"},
159   {"-/+ wire", "whether to do use wireframe instead of filled (faster)"},
160   {"-/+ texture", "whether to apply a texture (slower)"},
161   {"-image <filename>", "texture image to load"},
162   {"-/+ texture_quality", "whether to use texture smoothing (slower)"},
163   {"-/+ mipmap", "whether to use texture mipmap (slower)"},
164 };
165
166 ModeSpecOpt screensaver_opts = {countof(opts), opts, countof(vars), vars, desc};
167
168 #ifdef USE_MODULES
169 ModStruct   screensaver_description =
170 {"screensaver", "init_screensaver", "draw_screensaver", "release_screensaver",
171  "draw_screensaver", "init_screensaver", NULL, &screensaver_opts,
172  1000, 1, 2, 1, 4, 1.0, "",
173  "OpenGL screensaver", 0, NULL};
174 #endif
175
176
177 /* structure for holding the screensaver data */
178 typedef struct {
179   int screen_width, screen_height;
180   GLXContext *glx_context;
181   rotator *rot;
182   Bool button_down_p;
183   int mouse_x, mouse_y;
184   Window window;
185   XColor fg, bg;
186 } screensaverstruct;
187 static screensaverstruct *Screensaver = NULL;
188
189
190
191
192 /* convenient access to the screen width */
193 static int global_width=640, global_height=480;
194
195 /* set up a light */
196 static GLfloat lightOnePosition[] = {40.0, 40, 100.0, 0.0};
197 static GLfloat lightOneColor[] = {0.99, 0.99, 0.99, 1.0}; 
198
199 static GLfloat lightTwoPosition[] = {-40.0, 40, 100.0, 0.0};
200 static GLfloat lightTwoColor[] = {0.99, 0.99, 0.99, 1.0}; 
201
202 float rot_x=0, rot_y=0, rot_z=0;
203 float lastx=0, lasty=0;
204
205 static float max_lastx=300, max_lasty=400;
206 static float min_lastx=-400, min_lasty=-400;
207
208 static int screensaver_number;
209
210 struct functions {
211   void (*InitStuff)(void);
212   void (*DrawStuff)(void);
213   char *name;
214 };
215
216 /* currently joinoffset and twistoid look funny-
217    like we're looking at them from the back or something
218 */
219
220 static struct functions funcs_ptr[] = {
221   {InitStuff_helix2, DrawStuff_helix2, "helix2"},
222   {InitStuff_helix3, DrawStuff_helix3, "helix3"},
223   {InitStuff_helix4, DrawStuff_helix4, "helix4"},
224   {InitStuff_joinoffset, DrawStuff_joinoffset, "joinoffset"},
225   {InitStuff_screw, DrawStuff_screw, "screw"},
226   {InitStuff_taper, DrawStuff_taper, "taper"},
227   {InitStuff_twistoid, DrawStuff_twistoid, "twistoid"},
228 };
229
230 static int num_screensavers = countof(funcs_ptr);
231
232
233 /* BEGINNING OF FUNCTIONS */
234
235
236 GLubyte *
237 Generate_Image(int *width, int *height, int *format)
238 {
239   GLubyte *result;
240   int i, j, c;
241   int counter=0;
242
243   *width = checkImageWidth;
244   *height = checkImageHeight;
245   result = (GLubyte *)malloc(4 * (*width) * (*height));
246
247   counter = 0;
248   for (i = 0; i < checkImageWidth; i++) {
249     for (j = 0; j < checkImageHeight; j++) {
250       c = (((((i&0x8)==0))^(((j&0x8))==0)))*255;
251       result[counter++] = (GLubyte) c;
252       result[counter++] = (GLubyte) c;
253       result[counter++] = (GLubyte) c;
254       result[counter++] = (GLubyte) 255;
255     }
256   }
257
258   *format = GL_RGBA;
259   return result;
260 }
261
262
263 /* Create a texture in OpenGL.  First an image is loaded 
264    and stored in a raster buffer, then it's  */
265 void Create_Texture(ModeInfo *mi, const char *filename)
266 {
267   int height, width;
268   GLubyte *image;
269   int format;
270
271   if ( !strncmp(filename, "BUILTIN", 7))
272     image = Generate_Image(&width, &height, &format);
273   else
274     {
275       XImage *ximage = xpm_file_to_ximage (MI_DISPLAY (mi), MI_VISUAL (mi),
276                                            MI_COLORMAP (mi), filename);
277       image  = ximage->data;
278       width  = ximage->width;
279       height = ximage->height;
280       format = GL_RGBA;
281     }
282
283   /* GL_MODULATE or GL_DECAL depending on what you want */
284   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
285   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
286   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
287   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
288   /* perhaps we can edge a bit more speed at the expense of quality */
289   glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
290
291   if (do_texture_quality) {
292         /* with texture_quality, the min and mag filters look *much* nice but are *much* slower */
293         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
294         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
295   }
296   else {
297         /* default is to do it quick and dirty */
298         /* if you have mipmaps turned on, but not texture quality, nothing will happen! */
299         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
300         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
301   }
302
303   /* mipmaps make the image look much nicer */
304   if (do_mipmap)
305     {
306       int status;
307       clear_gl_error();
308       status = gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width, height, format,
309                                  GL_UNSIGNED_BYTE, image);
310       if (status)
311         {
312           const char *s = gluErrorString (status);
313           fprintf (stderr, "%s: error mipmapping %dx%d texture: %s\n",
314                    progname, width, height,
315                    (s ? s : "(unknown)"));
316           exit (1);
317         }
318       check_gl_error("mipmapping");
319     }
320   else
321     {
322       clear_gl_error();
323       glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0,
324                    format, GL_UNSIGNED_BYTE, image);
325       check_gl_error("texture");
326     }
327 }
328
329
330 static void
331 init_rotation (ModeInfo *mi)
332 {
333   screensaverstruct *gp = &Screensaver[MI_SCREEN(mi)];
334   double spin_speed = 1.0;
335   gp->rot = make_rotator (spin_speed, spin_speed, spin_speed, 1.0, 0.0, True);
336
337   lastx = (random() % (int) (max_lastx - min_lastx)) + min_lastx;
338   lasty = (random() % (int) (max_lasty - min_lasty)) + min_lasty;
339 }
340
341
342 /* draw the screensaver once */
343 void draw_screensaver(ModeInfo * mi)
344 {
345   screensaverstruct *gp = &Screensaver[MI_SCREEN(mi)];
346   Display    *display = MI_DISPLAY(mi);
347   Window      window = MI_WINDOW(mi);
348
349   if (!gp->glx_context)
350         return;
351
352   glXMakeCurrent(display, window, *(gp->glx_context));
353
354   funcs_ptr[screensaver_number].DrawStuff();
355           
356   /* track the mouse only if a button is down. */
357   if (gp->button_down_p)
358     {
359       lastx = gp->mouse_x;
360       lasty = gp->mouse_y;
361     }
362   else
363     {
364       float scale = (max_lastx - min_lastx);
365       double x, y, z;
366       get_rotation (gp->rot, &x, &y, &z, True);
367       rot_x = x * 360;
368       rot_y = y * 360;
369       rot_z = z * 360;
370       lastx = x * scale + min_lastx;
371       lasty = y * scale + min_lasty;
372     }
373
374   if (mi->fps_p) do_fps (mi);
375   glXSwapBuffers(display, window);
376 }
377
378
379 /* set up lighting conditions */
380 static void SetupLight(void)
381 {
382   glLightfv (GL_LIGHT0, GL_POSITION, lightOnePosition);
383   glLightfv (GL_LIGHT0, GL_DIFFUSE, lightOneColor);
384   glLightfv (GL_LIGHT1, GL_POSITION, lightTwoPosition);
385   glLightfv (GL_LIGHT1, GL_DIFFUSE, lightTwoColor);
386
387   glEnable (GL_LIGHT0);
388   glEnable (GL_LIGHT1);
389   glEnable (GL_LIGHTING);
390
391   glColorMaterial (GL_FRONT, GL_DIFFUSE);
392   glColorMaterial (GL_BACK, GL_DIFFUSE);
393   glEnable (GL_COLOR_MATERIAL);
394 }
395
396 /* reset the projection matrix */
397 static void resetProjection(void) {
398   glMatrixMode(GL_PROJECTION);
399   glLoadIdentity();
400   glFrustum (-9, 9, -9, 9, 50, 150.0);
401   glMatrixMode(GL_MODELVIEW);
402   glLoadIdentity();
403 }
404
405 /* Standard reshape function */
406 void
407 reshape_screensaver(ModeInfo *mi, int width, int height)
408 {
409   global_width=width;
410   global_height=height;
411   glViewport( 0, 0, global_width, global_height );
412   resetProjection();
413 }
414
415
416 /* decide which screensaver example to run */
417 static void chooseScreensaverExample(ModeInfo *mi)
418 {
419   int i;
420   /* call the extrusion init routine */
421
422   if (!strncmp(which_name, "RANDOM", strlen(which_name))) {
423     screensaver_number = random() % num_screensavers;
424   }
425   else {
426         screensaver_number=-1;
427         for (i=0; i < num_screensavers; i++) {
428           if (!strncmp(which_name, funcs_ptr[i].name, strlen(which_name))) {
429                 screensaver_number = i;
430           }
431         }         
432   }
433         
434   if (screensaver_number < 0 || screensaver_number >= num_screensavers) {
435         fprintf(stderr, "%s: invalid screensaver example number!\n", progname);
436         fprintf(stderr, "%s: known screensavers:\n", progname);
437         for (i=0; i < num_screensavers; i++)
438           fprintf(stderr,"\t%s\n", funcs_ptr[i].name);
439         exit(1);
440   }
441   init_rotation(mi);
442   funcs_ptr[screensaver_number].InitStuff();
443 }
444
445 /* main OpenGL initialization routine */
446 static void
447 initializeGL(ModeInfo *mi, GLsizei width, GLsizei height) 
448 {
449   int style;
450   int mode;
451
452   reshape_screensaver(mi, width, height);
453   glViewport( 0, 0, width, height ); 
454
455   glEnable(GL_DEPTH_TEST);
456   glClearColor(0,0,0,0);
457 /*    glCullFace(GL_BACK); */
458 /*    glEnable(GL_CULL_FACE); */
459   glLightModeli (GL_LIGHT_MODEL_TWO_SIDE, TRUE);
460   glShadeModel(GL_SMOOTH);
461
462   if (do_light)
463         SetupLight();
464   if (do_wire) {
465         glPolygonMode(GL_FRONT,GL_LINE);
466         glPolygonMode(GL_BACK,GL_LINE);
467   }
468   if (do_texture) {
469         Create_Texture(mi, which_image);
470         glEnable(GL_TEXTURE_2D);
471
472         /* configure the pipeline */
473         style = TUBE_JN_CAP;
474         style |= TUBE_CONTOUR_CLOSED;
475         style |= TUBE_NORM_FACET;
476         style |= TUBE_JN_ANGLE;
477         gleSetJoinStyle (style);
478
479         if (do_texture) {
480           mode = GLE_TEXTURE_ENABLE | GLE_TEXTURE_VERTEX_MODEL_FLAT;
481           glMatrixMode (GL_TEXTURE); glLoadIdentity ();
482           glScalef (0.25, 0.1, 1); glMatrixMode (GL_MODELVIEW);
483           gleTextureMode (mode);
484         }
485   }
486
487 }
488
489 Bool
490 screensaver_handle_event (ModeInfo *mi, XEvent *event)
491 {
492   screensaverstruct *gp = &Screensaver[MI_SCREEN(mi)];
493
494   if (event->xany.type == ButtonPress &&
495       event->xbutton.button & Button1)
496     {
497       gp->button_down_p = True;
498       gp->mouse_x = event->xbutton.x;
499       gp->mouse_y = event->xbutton.y;
500       return True;
501     }
502   else if (event->xany.type == ButtonRelease &&
503            event->xbutton.button & Button1)
504     {
505       gp->button_down_p = False;
506       return True;
507     }
508   else if (event->xany.type == MotionNotify)
509     {
510       gp->mouse_x = event->xmotion.x;
511       gp->mouse_y = event->xmotion.y;
512       return True;
513     }
514
515   return False;
516 }
517
518
519
520 /* xscreensaver initialization routine */
521 void init_screensaver(ModeInfo * mi)
522 {
523   int screen = MI_SCREEN(mi);
524   screensaverstruct *gp;
525
526   if (Screensaver == NULL) {
527         if ((Screensaver = (screensaverstruct *) calloc(MI_NUM_SCREENS(mi), sizeof (screensaverstruct))) == NULL)
528           return;
529   }
530   gp = &Screensaver[screen];
531
532   gp->window = MI_WINDOW(mi);
533   if ((gp->glx_context = init_GL(mi)) != NULL) {
534         reshape_screensaver(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
535         initializeGL(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
536         chooseScreensaverExample(mi);
537   } else {
538         MI_CLEARWINDOW(mi);
539   }
540
541 }
542
543 /* all sorts of nice cleanup code should go here! */
544 void release_screensaver(ModeInfo * mi)
545 {
546   int screen;
547   if (Screensaver != NULL) {
548         for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
549           /*      screensaverstruct *gp = &Screensaver[screen];*/
550         }
551         (void) free((void *) Screensaver);
552         Screensaver = NULL;
553   }
554   FreeAllGL(mi);
555 }
556 #endif
557