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