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