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