78c065ae2bf33f2f4c44f5d9af075387131295f5
[xscreensaver] / hacks / glx / gflux.c
1 /* -*- Mode: C; tab-width: 4 -*- emacs friendly */
2 /* gflux - creates a fluctuating 3D grid 
3  * requires OpenGL or MesaGL
4  * 
5  * Copyright (c) Josiah Pease, 2000, 2003
6  * Permission to use, copy, modify, distribute, and sell this software and its
7  * documentation for any purpose is hereby granted without fee, provided that
8  * the above copyright notice appear in all copies and that both that
9  * copyright notice and this permission notice appear in supporting
10  * documentation.  No representations are made about the suitability of this
11  * software for any purpose.  It is provided "as is" without express or 
12  * implied warranty.
13  *
14  * Thanks go to all those who worked on...
15  * MesaGL, OpenGL, UtahGLX, XFree86, gcc, vim, rxvt, the PNM (anymap) format
16  * xscreensaver and the thousands of other tools, apps and daemons that make
17  * linux usable  
18  * Personal thanks to Kevin Moss, Paul Sheahee and Jamie Zawinski
19  * 
20  * some xscreensaver code lifted from superquadrics.  Most other glx hacks 
21  * used as reference at some time.
22  *
23  * This hack and others can cause UtahGLX to crash my X server
24  * wireframe looks good with software only rendering anyway
25  * If anyone can work out why and supply a fix I'd love to hear from them
26  * 
27  * Josiah Pease <gfluxcode@jpease.force9.co.uk> 21 July 2000
28  * 
29  * History 
30  * 10 June 2000 : wireframe rippling grid standalone written
31  * 18 June 2000 : xscreensaver code added
32  * 25 June 2000 : solid and light added
33  * 04 July 2000 : majour bug hunt, xscreensaver code rewritten
34  * 08 July 2000 : texture mapping, rotation and zoom added
35  * 21 July 2000 : cleaned up code from bug hunts, manpage written
36  * 24 November 2000 : fixed x co-ord calculation in solid - textured
37  * 05 March 2001 : put back non pnmlib code with #ifdefs
38  * 11 May 2002 : fixed image problems with large images
39  */
40
41
42 #ifdef STANDALONE
43 #define DEFAULTS                        "*delay:                20000\n" \
44                                                                                 "*showFPS:      False\n" \
45                                         "*mode:         light\n" \
46                                         "*useSHM:       True \n" 
47
48
49 # define refresh_gflux 0
50 # include "xlockmore.h"                         /* from the xscreensaver distribution */
51 #else /* !STANDALONE */
52 # include "xlock.h"                                     /* from the xlockmore distribution */
53 #endif /* !STANDALONE */
54
55 #ifdef USE_GL /* whole file */
56
57 #ifdef HAVE_XMU
58 # ifndef VMS
59 #  include <X11/Xmu/Drawing.h>
60 #else  /* VMS */
61 #  include <Xmu/Drawing.h>
62 # endif /* VMS */
63 #endif
64
65 #undef countof
66 #define countof(x) (sizeof((x))/sizeof((*x)))
67
68 #include <stdlib.h>
69 #include <stdio.h>
70
71 #include <math.h>
72
73 #include "grab-ximage.h"
74 #include "gltrackball.h"
75
76
77 static enum {wire=0,solid,light,checker,grab} _draw;
78
79 static int _squares;                        /* grid size */
80 static int _resolution;                    /* wireframe resolution */
81 static int _flat;
82
83 static float _speed;
84 static float _rotationx;
85 static float _rotationy;
86 static float _rotationz;
87 static float _zoom;
88
89 static int _waves;
90 static int _waveChange;
91 static float _waveHeight;
92 static float _waveFreq;
93
94
95 #define WIDTH 320
96 #define HEIGHT 240
97
98 static XrmOptionDescRec opts[] = {
99     {"-squares", ".gflux.squares", XrmoptionSepArg, 0},
100     {"-resolution", ".gflux.resolution", XrmoptionSepArg, 0},
101 /*    {"-draw", ".gflux.draw", XrmoptionSepArg, 0},*/
102     {"-mode", ".gflux.mode", XrmoptionSepArg, 0},
103     {"-wireframe", ".gflux.mode", XrmoptionNoArg, "wire"},
104     {"-flat", ".gflux.flat", XrmoptionSepArg, 0},
105     {"-speed", ".gflux.speed", XrmoptionSepArg, 0},
106     {"-rotationx", ".gflux.rotationx", XrmoptionSepArg, 0},
107     {"-rotationy", ".gflux.rotationy", XrmoptionSepArg, 0},
108     {"-rotationz", ".gflux.rotationz", XrmoptionSepArg, 0},
109     {"-waves", ".gflux.waves", XrmoptionSepArg, 0},
110     {"-waveChange", ".gflux.waveChange", XrmoptionSepArg, 0},
111     {"-waveHeight", ".gflux.waveHeight", XrmoptionSepArg, 0},
112     {"-waveFreq", ".gflux.waveFreq", XrmoptionSepArg, 0},
113     {"-zoom", ".gflux.zoom", XrmoptionSepArg, 0},
114 };
115
116
117 static argtype vars[] = {
118     {&_squares, "squares", "Squares", "19", t_Int},
119     {&_resolution, "resolution", "Resolution", "4", t_Int},
120 /*    {&_draw, "draw", "Draw", "2", t_Int},*/
121     {&_flat, "flat", "Flat", "0", t_Int},
122     {&_speed, "speed", "Speed", "0.05", t_Float},
123     {&_rotationx, "rotationx", "Rotationx", "0.01", t_Float},
124     {&_rotationy, "rotationy", "Rotationy", "0.0", t_Float},
125     {&_rotationz, "rotationz", "Rotationz", "0.1", t_Float},
126     {&_waves, "waves", "Waves", "3", t_Int},
127     {&_waveChange, "waveChange", "WaveChange", "50", t_Int},
128     {&_waveHeight, "waveHeight", "WaveHeight", "1.0", t_Float},
129     {&_waveFreq, "waveFreq", "WaveFreq", "3.0", t_Float},
130     {&_zoom, "zoom", "Zoom", "1.0", t_Float},
131 };
132
133
134 static OptionStruct desc[] =
135 {
136     {"-squares num", "size of grid in squares (19)"},
137     {"-resolution num", "detail of lines making grid, wireframe only (4)"},
138 /*    {"-draw num", "draw method to use: 0=wireframe 1=solid 2=lit (0)"},*/
139     {"-flat num", "shading method, not wireframe: 0=smooth 1=flat (0)"},
140     {"-speed num", "speed of waves (0.05)"},
141     {"-rotationx num", "speed of xrotation (0.01)"},
142     {"-rotationy num", "speed of yrotation (0.00)"},
143     {"-rotationz num", "speed of zrotation (0.10)"},
144     {"-waves num", "number of simultanious waves (3)"},
145     {"-waveChange num", "number of cyles for a wave to change (50)"},
146     {"-waveHeight num", "height of waves (1.0)"},
147     {"-waveFreq num", "max frequency of a wave (3.0)"},
148     {"-zoom num", "camera control (1.0)"},
149 };
150
151 ENTRYPOINT ModeSpecOpt gflux_opts = {countof(opts), opts, countof(vars), vars, desc};
152
153 #ifdef USE_MODULES
154 ModStruct   gflux_description =
155 {"gflux", "init_gflux", "draw_gflux", "release_gflux",
156  "draw_gflux", "init_gflux", NULL, &gflux_opts,
157  1000, 1, 2, 1, 4, 1.0, "",
158  "Gflux: an OpenGL gflux", 0, NULL};
159 #endif
160
161 /* structure for holding the gflux data */
162 typedef struct gfluxstruct {
163     ModeInfo *modeinfo;
164     int screen_width, screen_height;
165     GLXContext *glx_context;
166     Window window;
167     XColor fg, bg;
168 #define MAXWAVES 10   /* should be dynamic    */
169     double wa[MAXWAVES];
170     double freq[MAXWAVES];
171     double dispy[MAXWAVES];
172     double dispx[MAXWAVES];
173     GLfloat colour[3];
174     GLuint texName;
175     GLfloat tex_xscale;
176     GLfloat tex_yscale;
177     XRectangle img_geom;
178     int img_width, img_height;
179     void (*drawFunc)(struct gfluxstruct *);
180
181     trackball_state *trackball;
182     Bool button_down_p;
183
184     double time;
185     double anglex;
186     double angley;
187     double anglez;
188
189     int counter;
190     int newWave;
191
192     Bool mipmap_p;
193     Bool waiting_for_image_p;
194
195 } gfluxstruct;
196 static gfluxstruct *gfluxes = NULL;
197
198 /* prototypes */
199 static void initLighting(void);
200 static void grabTexture(gfluxstruct *);
201 static void createTexture(gfluxstruct *);
202 static void displaySolid(gfluxstruct *);     /* drawFunc implementations */
203 static void displayLight(gfluxstruct *);
204 static void displayTexture(gfluxstruct *);
205 static void displayWire(gfluxstruct *);
206 static void calcGrid(gfluxstruct *);
207 static double getGrid(gfluxstruct *,double,double,double);
208
209 /* as macro for speed */
210 /* could do with colour cycling here */
211 /* void genColour(double);*/
212 #define genColour(X) \
213 {\
214     gp->colour[0] = 0.0;\
215     gp->colour[1] = 0.5+0.5*(X);\
216     gp->colour[2] = 0.5-0.5*(X);\
217 }
218
219 /* BEGINNING OF FUNCTIONS */
220
221
222 ENTRYPOINT Bool
223 gflux_handle_event (ModeInfo *mi, XEvent *event)
224 {
225   gfluxstruct *gp = &gfluxes[MI_SCREEN(mi)];
226
227   if (event->xany.type == ButtonPress &&
228       event->xbutton.button == Button1)
229     {
230       gp->button_down_p = True;
231       gltrackball_start (gp->trackball,
232                          event->xbutton.x, event->xbutton.y,
233                          MI_WIDTH (mi), MI_HEIGHT (mi));
234       return True;
235     }
236   else if (event->xany.type == ButtonRelease &&
237            event->xbutton.button == Button1)
238     {
239       gp->button_down_p = False;
240       return True;
241     }
242   else if (event->xany.type == ButtonPress &&
243            (event->xbutton.button == Button4 ||
244             event->xbutton.button == Button5))
245     {
246       gltrackball_mousewheel (gp->trackball, event->xbutton.button, 10,
247                               !!event->xbutton.state);
248       return True;
249     }
250   else if (event->xany.type == MotionNotify &&
251            gp->button_down_p)
252     {
253       gltrackball_track (gp->trackball,
254                          event->xmotion.x, event->xmotion.y,
255                          MI_WIDTH (mi), MI_HEIGHT (mi));
256       return True;
257     }
258
259   return False;
260 }
261
262
263 static void
264 userRot(gfluxstruct *gp)
265 {
266   gltrackball_rotate (gp->trackball);
267 }
268
269 /* draw the gflux once */
270 ENTRYPOINT void draw_gflux(ModeInfo * mi)
271 {
272     gfluxstruct *gp = &gfluxes[MI_SCREEN(mi)];
273     Display    *display = MI_DISPLAY(mi);
274     Window      window = MI_WINDOW(mi);
275
276     if (!gp->glx_context) return;
277
278     /* Just keep running before the texture has come in. */
279     /* if (gp->waiting_for_image_p) return; */
280
281     glXMakeCurrent(display, window, *(gp->glx_context));
282
283     calcGrid(gp);
284     gp->drawFunc(gp);
285     if (mi->fps_p) do_fps (mi);
286     glXSwapBuffers(display, window);
287 }
288
289
290 /* reset the projection matrix */
291 static void resetProjection(void) 
292 {
293     glMatrixMode(GL_PROJECTION);
294     glLoadIdentity();
295     glFrustum(-_zoom,_zoom,-0.8*_zoom,0.8*_zoom,2,6);
296     glTranslatef(0.0,0.0,-4.0);
297     glMatrixMode(GL_MODELVIEW);
298     glLoadIdentity();
299 }
300
301 /* Standard reshape function */
302 ENTRYPOINT void
303 reshape_gflux(ModeInfo *mi, int width, int height)
304 {
305     glViewport( 0, 0, width, height );
306     resetProjection();
307 }
308
309
310 /* main OpenGL initialization routine */
311 static void initializeGL(ModeInfo *mi, GLsizei width, GLsizei height) 
312 {
313   gfluxstruct *gp = &gfluxes[MI_SCREEN(mi)];
314
315   reshape_gflux(mi, width, height);
316   glViewport( 0, 0, width, height ); 
317
318   gp->tex_xscale = 1.0;  /* maybe changed later */
319   gp->tex_yscale = 1.0;
320
321   switch(_draw) {
322     case solid :
323       gp->drawFunc = (displaySolid);
324       glEnable(GL_DEPTH_TEST);
325     break;
326     case light :
327       gp->drawFunc = (displayLight);
328       glEnable(GL_DEPTH_TEST);
329       initLighting();
330         break;
331         case checker :
332       gp->drawFunc = (displayTexture);
333       glEnable(GL_DEPTH_TEST);
334       createTexture(gp);
335       initLighting();
336     break;
337         case grab :
338       gp->drawFunc = (displayTexture);
339       glEnable(GL_DEPTH_TEST);
340       grabTexture(gp);
341       initLighting();
342     break;
343     case wire :
344         default :
345       gp->drawFunc = (displayWire);
346       glDisable(GL_DEPTH_TEST);
347     break;
348   }
349
350   if(_flat) glShadeModel(GL_FLAT);
351   else glShadeModel(GL_SMOOTH);
352
353 }
354
355
356 /* xgflux initialization routine */
357 ENTRYPOINT void init_gflux(ModeInfo * mi)
358 {
359     int screen = MI_SCREEN(mi);
360     gfluxstruct *gp;
361
362     if (gfluxes == NULL) {
363         if ((gfluxes = (gfluxstruct *) 
364                  calloc(MI_NUM_SCREENS(mi), sizeof (gfluxstruct))) == NULL)
365             return;
366     }
367     gp = &gfluxes[screen];
368
369     gp->trackball = gltrackball_init ();
370
371     gp->time = frand(1000.0);  /* don't run two screens in lockstep */
372
373     {
374       char *s = get_string_resource (mi->dpy, "mode", "Mode");
375       if (!s || !*s)                       _draw = wire;
376       else if (!strcasecmp (s, "wire"))    _draw = wire;
377       else if (!strcasecmp (s, "solid"))   _draw = solid;
378       else if (!strcasecmp (s, "light"))   _draw = light;
379       else if (!strcasecmp (s, "checker")) _draw = checker;
380       else if (!strcasecmp (s, "grab"))    _draw = grab;
381       else
382         {
383           fprintf (stderr,
384                    "%s: mode must be one of: wire, solid, "
385                    "light, checker, or grab; not \"%s\"\n",
386                    progname, s);
387           exit (1);
388         }
389     }
390
391     gp->modeinfo = mi;
392     gp->window = MI_WINDOW(mi);
393     if ((gp->glx_context = init_GL(mi)) != NULL) {
394         reshape_gflux(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
395         initializeGL(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
396     } else {
397         MI_CLEARWINDOW(mi);
398     }
399 }
400
401 /* cleanup code */
402 ENTRYPOINT void release_gflux(ModeInfo * mi)
403 {
404     if (gfluxes != NULL) {
405       free((void *) gfluxes);
406       gfluxes = NULL;
407     }
408     FreeAllGL(mi);
409 }
410
411
412 static void createTexture(gfluxstruct *gp)
413 {
414   int size = 4;
415   unsigned int data[] = { 0xFFFFFFFF, 0xAAAAAAAA, 0xFFFFFFFF, 0xAAAAAAAA,
416                           0xAAAAAAAA, 0xFFFFFFFF, 0xAAAAAAAA, 0xFFFFFFFF,
417                           0xFFFFFFFF, 0xAAAAAAAA, 0xFFFFFFFF, 0xAAAAAAAA,
418                           0xAAAAAAAA, 0xFFFFFFFF, 0xAAAAAAAA, 0xFFFFFFFF };
419
420   gp->tex_xscale = size;
421   gp->tex_yscale = size;
422
423   glGenTextures (1, &gp->texName);
424   glBindTexture (GL_TEXTURE_2D, gp->texName);
425
426   glTexImage2D (GL_TEXTURE_2D, 0, 3, size, size, 0,
427                 GL_RGBA, GL_UNSIGNED_BYTE, data);
428
429   glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
430   glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
431
432   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
433   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
434 }
435
436
437 static void
438 image_loaded_cb (const char *filename, XRectangle *geometry,
439                  int image_width, int image_height, 
440                  int texture_width, int texture_height,
441                  void *closure)
442 {
443   gfluxstruct *gp = (gfluxstruct *) closure;
444   gp->img_geom = *geometry;
445
446   gp->tex_xscale =  (GLfloat) image_width  / texture_width;
447   gp->tex_yscale = -(GLfloat) image_height / texture_height;
448   gp->img_width  = image_width;
449   gp->img_height = image_height;
450    
451   glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
452   glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
453                    (gp->mipmap_p ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR));
454
455   gp->waiting_for_image_p = False;
456 }
457
458 static void
459 grabTexture(gfluxstruct *gp)
460 {
461   if (MI_IS_WIREFRAME(gp->modeinfo))
462     return;
463
464   gp->waiting_for_image_p = True;
465   gp->mipmap_p = True;
466   load_texture_async (gp->modeinfo->xgwa.screen, gp->modeinfo->window,
467                       *gp->glx_context, 0, 0, gp->mipmap_p, gp->texName,
468                       image_loaded_cb, gp);
469 }
470
471
472 static void initLighting(void)
473 {
474     static const float ambientA[]  = {0.0, 0.0, 0.0, 1.0};
475     static const float diffuseA[]  = {1.0, 1.0, 1.0, 1.0};
476     static const float positionA[] = {5.0, 5.0, 15.0, 1.0};
477
478     static const float front_mat_shininess[] = {30.0};
479     static const float front_mat_specular[]  = {0.5, 0.5, 0.5, 1.0};
480
481     static const float mat_diffuse[] = {0.5, 0.5, 0.5, 1.0};
482
483     glMaterialfv(GL_FRONT, GL_SHININESS, front_mat_shininess);
484     glMaterialfv(GL_FRONT, GL_SPECULAR, front_mat_specular);
485
486     glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
487
488     glLightfv(GL_LIGHT0, GL_AMBIENT, ambientA);
489     glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseA);
490     glLightfv(GL_LIGHT0, GL_POSITION, positionA);
491     glEnable(GL_LIGHTING);
492     glEnable(GL_LIGHT0);
493     glLightModelf(GL_LIGHT_MODEL_TWO_SIDE,1);
494
495     glEnable(GL_NORMALIZE);         /* would it be faster ...   */
496     glEnable(GL_COLOR_MATERIAL);
497     glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
498 }
499
500 /************************************/
501 /* draw implementations             */
502 /* somewhat inefficient since they  */
503 /* all calculate previously         */
504 /* calculated values again          */
505 /* storing the values in an array   */
506 /* is a posibility                  */
507 /************************************/
508 static void displayTexture(gfluxstruct *gp)
509 {
510     double x,y,u,v;
511     double z;
512     double dx = 2.0/((double)_squares);
513     double dy = 2.0/((double)_squares);
514
515     double du = 2.0/((double)_squares);
516     double dv = 2.0/((double)_squares);
517
518     double xs = gp->tex_xscale;
519     double ys = gp->tex_yscale;
520
521     double minx, miny, maxx, maxy;
522     double minu, minv;
523
524 #if 0
525     minx = (GLfloat) gp->img_geom.x / gp->img_width;
526     miny = (GLfloat) gp->img_geom.y / gp->img_height;
527     maxx = ((GLfloat) (gp->img_geom.x + gp->img_geom.width) /
528             gp->img_width);
529     maxy = ((GLfloat) (gp->img_geom.y + gp->img_geom.height) /
530             gp->img_height);
531     minu = minx;
532     minv = miny;
533     minx = (minx * 2) - 1;
534     miny = (miny * 2) - 1;
535     maxx = (maxx * 2) - 1;
536     maxy = (maxy * 2) - 1;
537 #else
538     minx = -1;
539     miny = -1;
540     maxx = 1;
541     maxy = 1;
542     minv = 0;
543     minu = 0;
544 #endif
545
546         glMatrixMode (GL_TEXTURE);
547         glLoadIdentity ();
548         glTranslatef(-1,-1,0);
549         glScalef(0.5,0.5,1);
550         glMatrixMode (GL_MODELVIEW);
551
552     glLoadIdentity();
553     userRot(gp);
554     glRotatef(gp->anglex,1,0,0);
555     glRotatef(gp->angley,0,1,0);
556     glRotatef(gp->anglez,0,0,1);
557     glScalef(1,1,(GLfloat)_waveHeight);
558     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
559         glEnable(GL_TEXTURE_2D);
560
561     clear_gl_error();
562         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
563         glBindTexture(GL_TEXTURE_2D, gp->texName);
564     check_gl_error("texture binding");
565
566         glColor3f(0.5,0.5,0.5);
567  
568     for(x = minx, u = minu; x < maxx - 0.01; x += dx, u += du) {
569         glBegin(GL_QUAD_STRIP);
570         for (y = miny, v = minv; y <= maxy + 0.01; y += dy, v += dv) {
571             z = getGrid(gp,x,y,gp->time);
572             glTexCoord2f(u*xs,v*ys);
573             glNormal3f(
574                 getGrid(gp,x+dx,y,gp->time)-getGrid(gp, x-dx,y,gp->time),
575                 getGrid(gp,x,y+dy,gp->time)-getGrid(gp, x,y-dy,gp->time),
576                 1
577             );
578             glVertex3f(x,y,z);
579
580             z = getGrid(gp,x+dx,y,gp->time);
581             glTexCoord2f((u+du)*xs,v*ys);
582             glNormal3f(
583                 getGrid(gp,x+dx+dx,y,gp->time)-getGrid(gp, x,y,gp->time),
584                 getGrid(gp,x+dx,y+dy,gp->time)-getGrid(gp, x+dx,y-dy,gp->time),
585                 1
586             );
587             glVertex3f(x+dx,y,z);
588         }
589         glEnd();
590     }
591
592     /* Draw a border around the grid.
593      */
594     glColor3f(0.4, 0.4, 0.4);
595     glDisable(GL_TEXTURE_2D);
596     glEnable (GL_LINE_SMOOTH);
597
598     glBegin(GL_LINE_LOOP);
599     y = miny;
600     for (x = minx; x <= maxx; x += dx)
601       glVertex3f (x, y, getGrid (gp, x, y, gp->time));
602     x = maxx;
603     for (y = miny; y <= maxy; y += dy)
604       glVertex3f (x, y, getGrid (gp, x, y, gp->time));
605     y = maxy;
606     for (x = maxx; x >= minx; x -= dx)
607       glVertex3f (x, y, getGrid (gp, x, y, gp->time));
608     x = minx;
609     for (y = maxy; y >= miny; y -= dy)
610       glVertex3f (x, y, getGrid (gp, x, y, gp->time));
611
612     glEnd();
613     glEnable(GL_TEXTURE_2D);
614
615     if (! gp->button_down_p) {
616       gp->time -= _speed;
617       gp->anglex -= _rotationx;
618       gp->angley -= _rotationy;
619       gp->anglez -= _rotationz;
620     }
621 }
622 static void displaySolid(gfluxstruct *gp)
623 {
624     double x,y;
625     double z;
626     double dx = 2.0/((double)_squares);
627     double dy = 2.0/((double)_squares);
628
629     glLoadIdentity();
630     glRotatef(gp->anglex,1,0,0);
631     glRotatef(gp->angley,0,1,0);
632     glRotatef(gp->anglez,0,0,1);
633     userRot(gp);
634     glScalef(1,1,(GLfloat)_waveHeight);
635     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
636
637     for(x=-1;x<0.9999;x+=dx) {
638         glBegin(GL_QUAD_STRIP);
639         for(y=-1;y<=1;y+=dy) {
640             z = getGrid(gp, x,y,gp->time);
641             genColour(z);
642             glColor3fv(gp->colour);
643             glVertex3f(x,y,z);
644
645             z = getGrid(gp, x+dx,y,gp->time);
646             genColour(z);
647             glColor3fv(gp->colour);
648             glVertex3f(x+dx,y,z);
649         }
650         glEnd();
651     }
652
653     if (! gp->button_down_p) {
654       gp->time -= _speed;
655       gp->anglex -= _rotationx;
656       gp->angley -= _rotationy;
657       gp->anglez -= _rotationz;
658     }
659
660 }
661
662 static void displayLight(gfluxstruct *gp)
663 {
664     double x,y;
665     double z;
666     double dx = 2.0/((double)_squares);
667     double dy = 2.0/((double)_squares);
668
669     glLoadIdentity();
670     glRotatef(gp->anglex,1,0,0);
671     glRotatef(gp->angley,0,1,0);
672     glRotatef(gp->anglez,0,0,1);
673     userRot(gp);
674     glScalef(1,1,(GLfloat)_waveHeight);
675     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
676
677     for(x=-1;x<0.9999;x+=dx) {
678         glBegin(GL_QUAD_STRIP);
679         for(y=-1;y<=1;y+=dy) {
680             z = getGrid(gp, x,y,gp->time);
681             genColour(z);
682             glColor3fv(gp->colour);
683             glNormal3f(
684                 getGrid(gp, x+dx,y,gp->time)-getGrid(gp, x-dx,y,gp->time),
685                 getGrid(gp, x,y+dy,gp->time)-getGrid(gp, x,y-dy,gp->time),
686                 1
687             );
688             glVertex3f(x,y,z);
689
690             z = getGrid(gp, x+dx,y,gp->time);
691             genColour(z);
692             glColor3fv(gp->colour);
693             glNormal3f(
694                 getGrid(gp, x+dx+dx,y,gp->time)-getGrid(gp, x,y,gp->time),
695                 getGrid(gp, x+dx,y+dy,gp->time)-getGrid(gp, x+dx,y-dy,gp->time),
696                 1
697             );
698             glVertex3f(x+dx,y,z);
699         }
700         glEnd();
701     }
702
703     if (! gp->button_down_p) {
704       gp->time -= _speed;
705       gp->anglex -= _rotationx;
706       gp->angley -= _rotationy;
707       gp->anglez -= _rotationz;
708     }
709 }
710
711 static void displayWire(gfluxstruct *gp)
712 {
713     double x,y;
714     double z;
715     double dx1 = 2.0/((double)(_squares*_resolution)) - 0.00001;
716     double dy1 = 2.0/((double)(_squares*_resolution)) - 0.00001;
717     double dx2 = 2.0/((double)_squares) - 0.00001;
718     double dy2 = 2.0/((double)_squares) - 0.00001;
719
720     glLoadIdentity();
721     glRotatef(gp->anglex,1,0,0);
722     glRotatef(gp->angley,0,1,0);
723     glRotatef(gp->anglez,0,0,1);
724     userRot(gp);
725     glScalef(1,1,(GLfloat)_waveHeight);
726     glClear(GL_COLOR_BUFFER_BIT);
727
728     for(x=-1;x<=1;x+=dx2) {
729         glBegin(GL_LINE_STRIP);
730         for(y=-1;y<=1;y+=dy1) {
731             z = getGrid(gp, x,y,gp->time);
732             genColour(z);
733             glColor3fv(gp->colour);
734             glVertex3f(x,y,z);
735         }
736         glEnd();
737     }
738     for(y=-1;y<=1;y+=dy2) {
739         glBegin(GL_LINE_STRIP);
740         for(x=-1;x<=1;x+=dx1) {
741             z = getGrid(gp, x,y,gp->time);
742             genColour(z);
743             glColor3fv(gp->colour);
744             glVertex3f(x,y,z);
745         }
746         glEnd();
747     }
748
749     if (! gp->button_down_p) {
750       gp->time -= _speed;
751       gp->anglex -= _rotationx;
752       gp->angley -= _rotationy;
753       gp->anglez -= _rotationz;
754     }
755 }
756
757 /* generates new ripples */
758 static void calcGrid(gfluxstruct *gp)
759 {
760     double tmp;
761
762     if (gp->button_down_p) return;
763
764     tmp = 1.0/((double)_waveChange);
765     if(!(gp->counter%_waveChange)) {
766         gp->newWave = ((int)(gp->counter*tmp))%_waves;
767         gp->dispx[gp->newWave] = -frand(1.0);
768         gp->dispy[gp->newWave] = -frand(1.0);
769         gp->freq[gp->newWave] = _waveFreq * frand(1.0);
770         gp->wa[gp->newWave] = 0.0;
771     }
772     gp->counter++;
773     gp->wa[gp->newWave] += tmp;
774     gp->wa[(gp->newWave+1)%_waves] -= tmp;
775 }
776
777 /* returns a height for the grid given time and x,y space co-ords */
778 static double getGrid(gfluxstruct *gp, double x, double y, double a)
779 {
780     register int i;
781     double retval=0.0;
782     double tmp;
783
784     tmp = 1.0/((float)_waves);
785     for(i=0;i<_waves;i++) {
786       retval += gp->wa[i] * tmp * sin( gp->freq[i]
787               * ( (x+gp->dispx[i]) * (x+gp->dispx[i]) 
788                 + (y+gp->dispy[i]) * (y+gp->dispy[i]) +a ) );
789     }
790     return(retval);
791 }
792
793
794 XSCREENSAVER_MODULE ("Gflux", gflux)
795
796 #endif