1 /* -*- Mode: C; tab-width: 4 -*- emacs friendly */
3 /* gflux - creates a fluctuating 3D grid
4 * requires OpenGL or MesaGL
6 * Copyright (c) Josiah Pease, 2000
7 * Permission to use, copy, modify, distribute, and sell this software and its
8 * documentation for any purpose is hereby granted without fee, provided that
9 * the above copyright notice appear in all copies and that both that
10 * copyright notice and this permission notice appear in supporting
11 * documentation. No representations are made about the suitability of this
12 * software for any purpose. It is provided "as is" without express or
15 * Thanks go to all those who worked on...
16 * MesaGL, OpenGL, UtahGLX, XFree86, gcc, vim, rxvt, the PNM (anymap) format
17 * xscreensaver and the thousands of other tools, apps and daemons that make
19 * Personal thanks to Kevin Moss, Paul Sheahee and Jamie Zawinski
21 * some xscreensaver code lifted from superquadrics. Most other glx hacks
22 * used as reference at some time.
24 * This hack and others can cause UtahGLX to crash my X server
25 * wireframe looks good with software only rendering anyway
26 * If anyone can work out why and supply a fix I'd love to hear from them
28 * Josiah Pease <gfluxcode@jpease.force9.co.uk> 21 July 2000
31 * 10 June 2000 : wireframe rippling grid standalone written
32 * 18 June 2000 : xscreensaver code added
33 * 25 June 2000 : solid and light added
34 * 04 July 2000 : majour bug hunt, xscreensaver code rewritten
35 * 08 July 2000 : texture mapping, rotation and zoom added
36 * 21 July 2000 : cleaned up code from bug hunts, manpage written
41 * due to a Bug/feature in VMS X11/Intrinsic.h has to be placed before xlock.
42 * otherwise caddr_t is not defined correctly
45 #include <X11/Intrinsic.h>
49 # define PROGCLASS "gflux"
50 # define HACK_INIT init_gflux
51 # define HACK_DRAW draw_gflux
52 # define gflux_opts xlockmore_opts
53 #define DEFAULTS "*squares: 19 \n" \
58 "*rotationx: 0.01 \n" \
59 "*rotationy: 0.0 \n" \
60 "*rotationz: 0.1 \n" \
62 "*waveChange: 50 \n" \
63 "*waveHeight: 1.0 \n" \
68 # include "xlockmore.h" /* from the xscreensaver distribution */
69 #else /* !STANDALONE */
70 # include "xlock.h" /* from the xlockmore distribution */
71 #endif /* !STANDALONE */
73 #ifdef USE_GL /* whole file */
77 # ifndef PIXEL_ALREADY_TYPEDEFED
78 # define PIXEL_ALREADY_TYPEDEFED /* Sigh, Xmu/Drawing.h needs this... */
84 # include <X11/Xmu/Drawing.h>
86 # include <Xmu/Drawing.h>
91 #define countof(x) (sizeof((x))/sizeof((*x)))
102 static enum {wire=0,solid,light,checker,textured} _draw; /* draw style */
103 static int _squares = 19; /* grid size */
104 static int _resolution = 4; /* wireframe resolution */
105 static int _flat = 0;
107 static float _speed = 0.05;
108 static float _rotationx = 0.01;
109 static float _rotationy = 0.0;
110 static float _rotationz = 0.1;
111 static float _zoom = 1.0;
113 static int _waves = 3;
114 static int _waveChange = 50;
115 static float _waveHeight = 1.0;
116 static float _waveFreq = 3.0;
121 static XrmOptionDescRec opts[] = {
122 {"-squares", ".gflux.squares", XrmoptionSepArg, (caddr_t) NULL},
123 {"-resolution", ".gflux.resolution", XrmoptionSepArg, (caddr_t) NULL},
124 {"-draw", ".gflux.draw", XrmoptionSepArg, (caddr_t) NULL},
125 {"-flat", ".gflux.flat", XrmoptionSepArg, (caddr_t) NULL},
126 {"-speed", ".gflux.speed", XrmoptionSepArg, (caddr_t) NULL},
127 {"-rotationx", ".gflux.rotationx", XrmoptionSepArg, (caddr_t) NULL},
128 {"-rotationy", ".gflux.rotationy", XrmoptionSepArg, (caddr_t) NULL},
129 {"-rotationz", ".gflux.rotationz", XrmoptionSepArg, (caddr_t) NULL},
130 {"-waves", ".gflux.waves", XrmoptionSepArg, (caddr_t) NULL},
131 {"-waveChange", ".gflux.waveChange", XrmoptionSepArg, (caddr_t) NULL},
132 {"-waveHeight", ".gflux.waveHeight", XrmoptionSepArg, (caddr_t) NULL},
133 {"-waveFreq", ".gflux.waveFreq", XrmoptionSepArg, (caddr_t) NULL},
134 {"-zoom", ".gflux.zoom", XrmoptionSepArg, (caddr_t) NULL},
138 static argtype vars[] = {
139 {(caddr_t *) & _squares, "squares", "Squares", "19", t_Int},
140 {(caddr_t *) & _resolution, "resolution", "Resolution", "4", t_Int},
141 {(caddr_t *) & _draw, "draw", "Draw", "0", t_Int},
142 {(caddr_t *) & _flat, "flat", "Flat", "0", t_Int},
143 {(caddr_t *) & _speed, "speed", "Speed", "0.05", t_Float},
144 {(caddr_t *) & _rotationx, "rotationx", "Rotationx", "0.01", t_Float},
145 {(caddr_t *) & _rotationy, "rotationy", "Rotationy", "0.0", t_Float},
146 {(caddr_t *) & _rotationz, "rotationz", "Rotationz", "0.1", t_Float},
147 {(caddr_t *) & _waves, "waves", "Waves", "3", t_Int},
148 {(caddr_t *) & _waveChange, "waveChange", "WaveChange", "50", t_Int},
149 {(caddr_t *) & _waveHeight, "waveHeight", "WaveHeight", "1.0", t_Float},
150 {(caddr_t *) & _waveFreq, "waveFreq", "WaveFreq", "3.0", t_Float},
151 {(caddr_t *) & _zoom, "zoom", "Zoom", "1.0", t_Float},
155 static OptionStruct desc[] =
157 {"-squares num", "size of grid in squares (19)"},
158 {"-resolution num", "detail of lines making grid, wireframe only (4)"},
159 {"-draw num", "draw method to use: 0=wireframe 1=solid 2=lit (0)"},
160 {"-flat num", "shading method, not wireframe: 0=smooth 1=flat (0)"},
161 {"-speed num", "speed of waves (0.05)"},
162 {"-rotationx num", "speed of xrotation (0.01)"},
163 {"-rotationy num", "speed of yrotation (0.00)"},
164 {"-rotationz num", "speed of zrotation (0.10)"},
165 {"-waves num", "number of simultanious waves (3)"},
166 {"-waveChange num", "number of cyles for a wave to change (50)"},
167 {"-waveHeight num", "height of waves (1.0)"},
168 {"-waveFreq num", "max frequency of a wave (3.0)"},
169 {"-zoom num", "camera control (1.0)"},
172 ModeSpecOpt gflux_opts = {countof(opts), opts, countof(vars), vars, desc};
175 ModStruct gflux_description =
176 {"gflux", "init_gflux", "draw_gflux", "release_gflux",
177 "draw_gflux", "init_gflux", NULL, &gflux_opts,
178 1000, 1, 2, 1, 4, 1.0, "",
179 "Gflux: an OpenGL gflux", 0, NULL};
182 /* structure for holding the gflux data */
184 int screen_width, screen_height;
185 GLXContext *glx_context;
188 #define MAXWAVES 10 /* should be dynamic */
190 double freq[MAXWAVES];
191 double dispy[MAXWAVES];
192 double dispx[MAXWAVES];
198 void (*drawFunc)(void);
200 static gfluxstruct *gflux = NULL;
203 void initLighting(void);
204 void initTexture(void);
205 void loadTexture(void);
206 void createTexture(void);
207 void displaySolid(void); /* drawFunc implementations */
208 void displayLight(void);
209 void displayTexture(void);
210 void displayWire(void);
212 double getGrid(double,double,double);
214 /* as macro for speed */
215 /* could do with colour cycling here */
216 /* void genColour(double);*/
217 #define genColour(X) \
219 gflux->colour[0] = 0.0;\
220 gflux->colour[1] = 0.5+0.5*(X);\
221 gflux->colour[2] = 0.5-0.5*(X);\
224 /* BEGINNING OF FUNCTIONS */
227 /* draw the gflux once */
228 void draw_gflux(ModeInfo * mi)
230 gfluxstruct *gp = &gflux[MI_SCREEN(mi)];
231 Display *display = MI_DISPLAY(mi);
232 Window window = MI_WINDOW(mi);
234 if (!gp->glx_context) return;
236 glXMakeCurrent(display, window, *(gp->glx_context));
240 glXSwapBuffers(display, window);
244 /* reset the projection matrix */
245 void resetProjection(void) {
246 glMatrixMode(GL_PROJECTION);
248 glFrustum(-_zoom,_zoom,-0.8*_zoom,0.8*_zoom,2,6);
249 glTranslatef(0.0,0.0,-4.0);
250 glMatrixMode(GL_MODELVIEW);
254 /* Standard reshape function */
256 reshape(int width, int height)
258 glViewport( 0, 0, width, height );
263 /* main OpenGL initialization routine */
264 void initializeGL(GLsizei width, GLsizei height)
266 reshape(width, height);
267 glViewport( 0, 0, width, height );
270 gflux->drawFunc = (displaySolid);
271 glEnable(GL_DEPTH_TEST);
274 gflux->drawFunc = (displayLight);
275 glEnable(GL_DEPTH_TEST);
279 gflux->drawFunc = (displayTexture);
280 glEnable(GL_DEPTH_TEST);
286 gflux->drawFunc = (displayTexture);
287 glEnable(GL_DEPTH_TEST);
294 gflux->drawFunc = (displayWire);
295 glDisable(GL_DEPTH_TEST);
299 if(_flat) glShadeModel(GL_FLAT);
300 else glShadeModel(GL_SMOOTH);
305 /* xgflux initialization routine */
306 void init_gflux(ModeInfo * mi)
308 int screen = MI_SCREEN(mi);
312 if ((gflux = (gfluxstruct *)
313 calloc(MI_NUM_SCREENS(mi), sizeof (gfluxstruct))) == NULL)
318 gp->window = MI_WINDOW(mi);
319 if ((gp->glx_context = init_GL(mi)) != NULL) {
320 reshape(MI_WIDTH(mi), MI_HEIGHT(mi));
321 initializeGL(MI_WIDTH(mi), MI_HEIGHT(mi));
328 void release_gflux(ModeInfo * mi)
330 if(gflux->image!=NULL) free(gflux->image);
331 if(gflux->glx_context!=NULL) free(gflux->glx_context);
333 (void) free((void *) gflux);
339 /* loads several different types of PNM image from stdin */
340 #define presult(A,B,C) (*(result+(A)*(gflux->imageWidth)*4+(B)*4+(C)))
341 void loadTexture(void)
343 int i, j, levels, width, height;
352 if(!strncmp(s,"P6",2)) ppmType=6;
353 if(!strncmp(s,"P5",2)) ppmType=5;
354 if(!strncmp(s,"P3",2)) ppmType=3;
355 if(!strncmp(s,"P2",2)) ppmType=2;
358 while((i=getc(file))=='#')
360 while(getc(file)!='\n');
364 fscanf(file,"%d %d %d",&width,&height,&levels);
366 result = malloc(sizeof(GLubyte)*4*width*height);
367 gflux->imageWidth = width;
368 gflux->imageHeight = height;
371 case 2 : /* ASCII grey */
372 for(i=0;i<height;i++) {
373 for(j=0;j<width;j++) {
374 fscanf(file,"%d",&red);
375 presult(j,i,0) = red;
376 presult(j,i,1) = red;
377 presult(j,i,2) = red;
381 case 3 : /* ASCII rgb */
382 for(i=0;i<height;i++) {
383 for(j=0;j<width;j++) {
384 fscanf(file,"%d %d %d",&red,&green,&blue);
385 presult(j,i,0) = red;
386 presult(j,i,1) = green;
387 presult(j,i,2) = blue;
391 case 5 : /* Binary grey */
392 getc(file); /* seems nessessary */
393 for(i=0;i<height;i++) {
394 for(j=0;j<width;j++) {
396 presult(j,i,0) = red;
397 presult(j,i,1) = red;
398 presult(j,i,2) = red;
402 case 6 : /* Binary rgb */
403 getc(file); /* seems nessessary */
404 for(i=0;i<height;i++) {
405 for(j=0;j<width;j++) {
409 presult(j,i,0) = red;
410 presult(j,i,1) = green;
411 presult(j,i,2) = blue;
416 gflux->image = result;
419 /* creates an image for texture mapping */
420 void createTexture(void)
425 gflux->imageHeight = gflux->imageWidth = 8;
427 result = malloc(sizeof(GLubyte)*4*gflux->imageHeight*gflux->imageWidth);
428 for(i=0;i<gflux->imageHeight;i++) {
429 for(j=0;j<gflux->imageWidth;j++) {
430 c = (((i)%2 ^ (j)%2) ? 100 : 200 );
431 presult(i,j,0) = (GLubyte) c;
432 presult(i,j,1) = (GLubyte) c;
433 presult(i,j,2) = (GLubyte) c;
434 presult(i,j,3) = (GLubyte) 255;
437 gflux->image = result;
441 /* specifies image as texture */
442 void initTexture(void)
444 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
445 glGenTextures(1, &gflux->texName);
446 glBindTexture(GL_TEXTURE_2D, gflux->texName);
447 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
448 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
449 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
450 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
451 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, gflux->imageWidth,
452 gflux->imageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, gflux->image);
456 void initLighting(void)
458 static float ambientA[] = {0.0, 0.0, 0.0, 1.0};
459 static float diffuseA[] = {1.0, 1.0, 1.0, 1.0};
460 static float positionA[] = {5.0, 5.0, 15.0, 1.0};
462 static float front_mat_shininess[] = {30.0};
463 static float front_mat_specular[] = {0.5, 0.5, 0.5, 1.0};
465 static float mat_diffuse[] = {0.5, 0.5, 0.5, 1.0};
467 glMaterialfv(GL_FRONT, GL_SHININESS, front_mat_shininess);
468 glMaterialfv(GL_FRONT, GL_SPECULAR, front_mat_specular);
470 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
472 glLightfv(GL_LIGHT0, GL_AMBIENT, ambientA);
473 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseA);
474 glLightfv(GL_LIGHT0, GL_POSITION, positionA);
475 glEnable(GL_LIGHTING);
477 glLightModelf(GL_LIGHT_MODEL_TWO_SIDE,1);
479 glEnable(GL_NORMALIZE); /* would it be faster ... */
480 glEnable(GL_COLOR_MATERIAL);
481 glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
484 /************************************/
485 /* draw implementations */
486 /* somewhat inefficient since they */
487 /* all calculate previously */
488 /* calculated values again */
489 /* storing the values in an array */
490 /* is a posibility */
491 /************************************/
492 void displayTexture(void)
494 static double time = 0.0;
495 static double anglex = 0.0;
496 static double angley = 0.0;
497 static double anglez = 0.0;
501 double dx = 2.0/((double)_squares);
502 double dy = 2.0/((double)_squares);
504 glMatrixMode (GL_TEXTURE);
506 glTranslatef(-1,-1,0);
508 glMatrixMode (GL_MODELVIEW);
511 glRotatef(anglex,1,0,0);
512 glRotatef(angley,0,1,0);
513 glRotatef(anglez,0,0,1);
514 glScalef(1,1,(GLfloat)_waveHeight);
515 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
516 glEnable(GL_TEXTURE_2D);
517 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
518 glBindTexture(GL_TEXTURE_2D, gflux->texName);
519 glColor3f(0.5,0.5,0.5);
521 for(x=-1,u=0;x<=1;x+=dx,u+=dx) {
522 glBegin(GL_QUAD_STRIP);
523 for(y=-1,v=0;y<=1;y+=dy,v+=dx) {
524 z = getGrid(x,y,time);
526 glColor3fv(gflux->colour);
527 */ glTexCoord2f(u,v);
529 getGrid(x+dx,y,time)-getGrid(x-dx,y,time),
530 getGrid(x,y+dy,time)-getGrid(x,y-dy,time),
535 z = getGrid(x+dx,y,time);
537 glColor3fv(gflux->colour);
538 */ glTexCoord2f(u+dx,v);
540 getGrid(x+dx+dx,y,time)-getGrid(x,y,time),
541 getGrid(x+dx,y+dy,time)-getGrid(x+dx,y-dy,time),
544 glVertex3f(x+dx,y,z);
550 anglex -= _rotationx;
551 angley -= _rotationy;
552 anglez -= _rotationz;
554 void displaySolid(void)
556 static double time = 0.0;
557 static double anglex = 0.0;
558 static double angley = 0.0;
559 static double anglez = 0.0;
563 double dx = 2.0/((double)_squares);
564 double dy = 2.0/((double)_squares);
567 glRotatef(anglex,1,0,0);
568 glRotatef(angley,0,1,0);
569 glRotatef(anglez,0,0,1);
570 glScalef(1,1,(GLfloat)_waveHeight);
571 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
573 for(x=-1;x<=1;x+=dx) {
574 glBegin(GL_QUAD_STRIP);
575 for(y=-1;y<=1;y+=dy) {
576 z = getGrid(x,y,time);
578 glColor3fv(gflux->colour);
581 z = getGrid(x+dx,y,time);
583 glColor3fv(gflux->colour);
584 glVertex3f(x+dx,y,z);
590 anglex -= _rotationx;
591 angley -= _rotationy;
592 anglez -= _rotationz;
596 void displayLight(void)
598 static double time = 0.0;
599 static double anglex = 0.0;
600 static double angley = 0.0;
601 static double anglez = 0.0;
605 double dx = 2.0/((double)_squares);
606 double dy = 2.0/((double)_squares);
609 glRotatef(anglex,1,0,0);
610 glRotatef(angley,0,1,0);
611 glRotatef(anglez,0,0,1);
612 glScalef(1,1,(GLfloat)_waveHeight);
613 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
615 for(x=-1;x<=1;x+=dx) {
616 glBegin(GL_QUAD_STRIP);
617 for(y=-1;y<=1;y+=dy) {
618 z = getGrid(x,y,time);
620 glColor3fv(gflux->colour);
622 getGrid(x+dx,y,time)-getGrid(x-dx,y,time),
623 getGrid(x,y+dy,time)-getGrid(x,y-dy,time),
628 z = getGrid(x+dx,y,time);
630 glColor3fv(gflux->colour);
632 getGrid(x+dx+dx,y,time)-getGrid(x,y,time),
633 getGrid(x+dx,y+dy,time)-getGrid(x+dx,y-dy,time),
636 glVertex3f(x+dx,y,z);
642 anglex -= _rotationx;
643 angley -= _rotationy;
644 anglez -= _rotationz;
647 void displayWire(void)
649 static double time = 0.0;
650 static double anglex = 0.0;
651 static double angley = 0.0;
652 static double anglez = 0.0;
656 double dx1 = 2.0/((double)(_squares*_resolution)) - 0.00001;
657 double dy1 = 2.0/((double)(_squares*_resolution)) - 0.00001;
658 double dx2 = 2.0/((double)_squares) - 0.00001;
659 double dy2 = 2.0/((double)_squares) - 0.00001;
662 glRotatef(anglex,1,0,0);
663 glRotatef(angley,0,1,0);
664 glRotatef(anglez,0,0,1);
665 glScalef(1,1,(GLfloat)_waveHeight);
666 glClear(GL_COLOR_BUFFER_BIT);
668 for(x=-1;x<=1;x+=dx2) {
669 glBegin(GL_LINE_STRIP);
670 for(y=-1;y<=1;y+=dy1) {
671 z = getGrid(x,y,time);
673 glColor3fv(gflux->colour);
678 for(y=-1;y<=1;y+=dy2) {
679 glBegin(GL_LINE_STRIP);
680 for(x=-1;x<=1;x+=dx1) {
681 z = getGrid(x,y,time);
683 glColor3fv(gflux->colour);
690 anglex -= _rotationx;
691 angley -= _rotationy;
692 anglez -= _rotationz;
695 /* generates new ripples */
698 static int counter=0;
702 tmp = 1.0/((double)_waveChange);
703 if(!(counter%_waveChange)) {
704 newWave = ((int)(counter*tmp))%_waves;
705 gflux->dispx[newWave] = 1.0 - ((double)random())/RAND_MAX;
706 gflux->dispy[newWave] = 1.0 - ((double)random())/RAND_MAX;
707 gflux->freq[newWave] = _waveFreq * ((float)random())/RAND_MAX;
708 gflux->wa[newWave] = 0.0;
711 gflux->wa[newWave] += tmp;
712 gflux->wa[(newWave+1)%_waves] -= tmp;
715 /* returns a height for the grid given time and x,y space co-ords */
716 double getGrid(double x, double y, double a)
722 tmp = 1.0/((float)_waves);
723 for(i=0;i<_waves;i++) {
724 retval += gflux->wa[i] * tmp * sin( gflux->freq[i]
725 * ( (x+gflux->dispx[i]) * (x+gflux->dispx[i])
726 + (y+gflux->dispy[i]) * (y+gflux->dispy[i]) +a ) );