From http://www.jwz.org/xscreensaver/xscreensaver-5.36.tar.gz
[xscreensaver] / hacks / glx / pulsar.c
1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* pulsar --- pulsar module for xpulsar */
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  * 4-Apr-1999:  dek@cgl.ucsf.edu  Created module "pulsar"
18  * 27-Apr-1999:  dek@cgl.ucsf.edu  Submitted module "pulsar"
19  * 4-May-1999:  jwz@jwz.org  Added module "pulsar"
20  * 4-May-1999:  dek@cgl.ucsf.edu  Submitted module "pulsar" updates
21  *
22  * Notes:
23  * The pulsar pulsar draws a number of rotating, pulsing rectangles
24  * on your screen.  Depending on the options you choose, you can set a number
25  * of interesting OpenGL parameters, including alpha blending, depth testing, fog,
26  * lighting, texturing, mipmapping, bilinear filtering, and line antialiasing.  
27  * Additionally, there is a "frames per second" meter which gives an estimate of
28  * the speed of your graphics card.  
29  *
30  * Example command-line switches:
31  * Only draw a single quad, and don't fill it (boring but fast)
32  * pulsar -wire -quads 1 -fps
33  *
34  * Only try this with hardware graphics acceleration (PPro 200 w/ a Voodoo 2 runs great)
35  * pulsar -quads 10 -texture -mipmap -texture_quality -light -fog -fps
36  *                                                                 
37  */
38
39 #include <math.h> 
40 #include <stdio.h>
41 #include <stdlib.h>
42
43 #ifdef STANDALONE
44 #define DEFAULTS                        "*delay:                        10000   \n" \
45                                                                                 "*showFPS:          False   \n" \
46
47 # define refresh_pulsar 0
48 # define pulsar_handle_event 0
49 # include "xlockmore.h"                         /* from the xpulsar distribution */
50 #else /* !STANDALONE */
51 # include "xlock.h"                                     /* from the xlockmore distribution */
52 #endif /* !STANDALONE */
53
54 #ifdef USE_GL /* whole file */
55
56 #ifdef HAVE_XMU
57 # ifndef VMS
58 #  include <X11/Xmu/Drawing.h>
59 #else  /* VMS */
60 #  include <Xmu/Drawing.h>
61 # endif /* VMS */
62 #endif
63
64 #include "xpm-ximage.h"
65
66 /* Functions for loading and storing textures */
67
68 #define checkImageWidth 64
69 #define checkImageHeight 64
70
71 /* Functions for handling the frames per second timer */
72
73 #undef countof
74 #define countof(x) (sizeof((x))/sizeof((*x)))
75
76 #define WIDTH 800
77 #define HEIGHT 600
78
79 #define NUM_QUADS 5
80 #define DEF_QUADS       "5"
81 #define DEF_LIGHT               "False"
82 #define DEF_WIRE                "False"
83 #define DEF_BLEND       "True"
84 #define DEF_FOG                 "False"
85 #define DEF_ANTIALIAS   "False"
86 #define DEF_TEXTURE     "False"
87 #define DEF_TEXTURE_QUALITY   "False"
88 #define DEF_MIPMAP      "False"
89 #define DEF_DO_DEPTH    "False"
90 #define DEF_IMAGE       "BUILTIN"
91
92 static int num_quads;
93 static int do_light;
94 static int do_wire;
95 static int do_blend;
96 static int do_fog;
97 static int do_antialias;
98 static int do_texture;
99 static int do_texture_quality;
100 static int do_mipmap;
101 static int do_depth;
102 static char *which_image;
103
104
105 static XrmOptionDescRec opts[] = {
106   {"-quads",   ".pulsar.quads",   XrmoptionSepArg, 0 },
107   {"-light",   ".pulsar.light",   XrmoptionNoArg, "true" },
108   {"+light",   ".pulsar.light",   XrmoptionNoArg, "false" },
109   {"-wire",   ".pulsar.wire",   XrmoptionNoArg, "true" },
110   {"+wire",   ".pulsar.wire",   XrmoptionNoArg, "false" },
111   {"-blend",   ".pulsar.blend",   XrmoptionNoArg, "true" },
112   {"+blend",   ".pulsar.blend",   XrmoptionNoArg, "false" },
113   {"-fog",   ".pulsar.fog",   XrmoptionNoArg, "true" },
114   {"+fog",   ".pulsar.fog",   XrmoptionNoArg, "false" },
115   {"-antialias",   ".pulsar.antialias",   XrmoptionNoArg, "true" },
116   {"+antialias",   ".pulsar.antialias",   XrmoptionNoArg, "false" },
117   {"-texture",   ".pulsar.texture",   XrmoptionNoArg, "true" },
118   {"+texture",   ".pulsar.texture",   XrmoptionNoArg, "false" },
119   {"-texture_quality",   ".pulsar.textureQuality",   XrmoptionNoArg, "true" },
120   {"+texture_quality",   ".pulsar.textureQuality",   XrmoptionNoArg, "false" },
121   {"-mipmap",   ".pulsar.mipmap",   XrmoptionNoArg, "true" },
122   {"+mipmap",   ".pulsar.mipmap",   XrmoptionNoArg, "false" },
123   {"-do_depth",   ".pulsar.doDepth",   XrmoptionNoArg, "true" },
124   {"+do_depth",   ".pulsar.doDepth",   XrmoptionNoArg, "false" },
125   {"-image",   ".pulsar.image",  XrmoptionSepArg, 0 },
126 };
127
128
129 static argtype vars[] = {
130   {&num_quads,    "quads",     "Quads",     DEF_QUADS,     t_Int},
131   {&do_light,     "light",     "Light",     DEF_LIGHT,     t_Bool},
132   {&do_wire,      "wire",      "Wire",      DEF_WIRE,      t_Bool},
133   {&do_blend,     "blend",     "Blend",     DEF_BLEND,     t_Bool},
134   {&do_fog,       "fog",       "Fog",       DEF_FOG,       t_Bool},
135   {&do_antialias, "antialias", "Antialias", DEF_ANTIALIAS, t_Bool},
136   {&do_texture,   "texture",   "Texture",   DEF_TEXTURE,   t_Bool},
137   {&do_texture_quality, "textureQuality", "TextureQuality", DEF_TEXTURE_QUALITY,   t_Bool},
138   {&do_mipmap,    "mipmap",    "Mipmap",    DEF_MIPMAP,    t_Bool},
139   {&do_depth,    "doDepth",    "DoDepth",   DEF_DO_DEPTH,  t_Bool},
140   {&which_image, "image",      "Image",     DEF_IMAGE,     t_String},
141 };
142
143
144 static OptionStruct desc[] =
145 {
146         {"-quads num", "how many quads to draw"},
147         {"-/+ light", "whether to do enable lighting (slower)"},
148         {"-/+ wire", "whether to do use wireframe instead of filled (faster)"},
149         {"-/+ blend", "whether to do enable blending (slower)"},
150         {"-/+ fog", "whether to do enable fog (?)"},
151         {"-/+ antialias", "whether to do enable antialiased lines (slower)"},
152         {"-/+ texture", "whether to do enable texturing (much slower)"},
153         {"-/+ texture_quality", "whether to do enable linear/mipmap filtering (much much slower)"},
154         {"-/+ mipmap", "whether to do enable mipmaps (much slower)"},
155         {"-/+ depth", "whether to do enable depth buffer checking (slower)"},
156         {"-image <filename>", "texture image to load"},
157 };
158
159 ENTRYPOINT ModeSpecOpt pulsar_opts = {countof(opts), opts, countof(vars), vars, desc};
160
161 #ifdef USE_MODULES
162 ModStruct   pulsar_description =
163 {"pulsar", "init_pulsar", "draw_pulsar", "release_pulsar",
164  "draw_pulsar", "init_pulsar", NULL, &pulsar_opts,
165  1000, 1, 2, 1, 4, 1.0, "",
166  "OpenGL pulsar", 0, NULL};
167 #endif
168
169 struct quad
170 {
171   GLfloat tx, ty, tz;
172   GLfloat rx, ry, rz;
173
174   GLfloat dtx, dty, dtz;
175   GLfloat drx, dry, drz;
176
177 };
178
179
180 /* structure for holding the pulsar data */
181 typedef struct {
182   int screen_width, screen_height;
183   GLXContext *glx_context;
184   Window window;
185   XColor fg, bg;
186
187   GLint quad_list;
188   float scale_x, scale_y, scale_z;
189   int frame;
190
191   struct quad *quads;
192
193 } pulsarstruct;
194
195 static pulsarstruct *Pulsar = NULL;
196
197 static GLubyte *
198 Generate_Image(int *width, int *height, int *format)
199 {
200   GLubyte *result;
201   int i, j, c;
202   int counter=0;
203
204   *width = checkImageWidth;
205   *height = checkImageHeight;
206   result = (GLubyte *)malloc(4 * (*width) * (*height));
207
208   counter = 0;
209   for (i = 0; i < checkImageWidth; i++) {
210     for (j = 0; j < checkImageHeight; j++) {
211       c = (((((i&0x8)==0))^(((j&0x8))==0)))*255;
212       result[counter++] = (GLubyte) c;
213       result[counter++] = (GLubyte) c;
214       result[counter++] = (GLubyte) c;
215       result[counter++] = (GLubyte) 255;
216     }
217   }
218
219   *format = GL_RGBA;
220   return result;
221 }
222
223
224 /* Create a texture in OpenGL.  First an image is loaded 
225    and stored in a raster buffer, then it's  */
226 static void Create_Texture(ModeInfo *mi, const char *filename)
227 {
228   int height, width;
229   GLubyte *image;
230   int format;
231
232   if ( !strncmp(filename, "BUILTIN", 7))
233     {
234     BUILTIN:
235       image = Generate_Image(&width, &height, &format);
236     }
237   else
238     {
239       XImage *ximage = xpm_file_to_ximage (MI_DISPLAY (mi), MI_VISUAL (mi),
240                                            MI_COLORMAP (mi), filename);
241       if (! ximage)
242         goto BUILTIN;
243       image  = (GLubyte *) ximage->data;
244       width  = ximage->width;
245       height = ximage->height;
246       format = GL_RGBA;
247     }
248
249   /* GL_MODULATE or GL_DECAL depending on what you want */
250   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
251   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
252   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
253   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
254   /* perhaps we can edge a bit more speed at the expense of quality */
255   glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
256
257   if (do_texture_quality) {
258         /* with texture_quality, the min and mag filters look *much* nice but are *much* slower */
259         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
260         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
261   }
262   else {
263         /* default is to do it quick and dirty */
264         /* if you have mipmaps turned on, but not texture quality, nothing will happen! */
265         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
266         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
267   }
268
269   /* mipmaps make the image look much nicer */
270   if (do_mipmap)
271     {
272       int status;
273       clear_gl_error();
274       status = gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width, height, format,
275                                  GL_UNSIGNED_BYTE, image);
276       if (status)
277         {
278           const char *s = (char *) gluErrorString (status);
279           fprintf (stderr, "%s: error mipmapping %dx%d texture: %s\n",
280                    progname, width, height,
281                    (s ? s : "(unknown)"));
282           exit (1);
283         }
284       check_gl_error("mipmapping");
285     }
286   else
287     {
288       clear_gl_error();
289       glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0,
290                    format, GL_UNSIGNED_BYTE, image);
291       check_gl_error("texture");
292     }
293 }
294
295 static void resetProjection(void) 
296 {
297   glMatrixMode(GL_PROJECTION);
298   glLoadIdentity();
299   glFrustum(-1, 1, -1, 1, 1, 100); 
300   glMatrixMode(GL_MODELVIEW);
301   glLoadIdentity();
302 }
303
304
305 static void GenerateQuad(pulsarstruct *gp)
306 {
307   int i;
308
309   gp->quad_list = glGenLists(1);
310   glNewList(gp->quad_list,GL_COMPILE);
311 #if 1
312   glBegin(GL_QUADS);
313   glColor4f(1,0,0,.4); glNormal3f(0,0,1);  glTexCoord2f(0,0); glVertex2f(-1, -1);
314   glColor4f(0,1,0,.4); glNormal3f(0,0,1);  glTexCoord2f(0,1); glVertex2f(-1,  1);
315   glColor4f(0,0,1,.4); glNormal3f(0,0,1);  glTexCoord2f(1,1); glVertex2f( 1,  1);
316   glColor4f(1,1,1,1); glNormal3f(0,0,1);  glTexCoord2f(1,0); glVertex2f( 1,  -1);
317 #else
318   glBegin(GL_TRIANGLE_STRIP);
319   glColor4f(0,1,0,.4); glNormal3f(0,0,1);  glTexCoord2f(0,1); glVertex2f(-1,  1);
320   glColor4f(1,0,0,.4); glNormal3f(0,0,1);  glTexCoord2f(0,0); glVertex2f(-1, -1);
321   glColor4f(0,0,1,.4); glNormal3f(0,0,1);  glTexCoord2f(1,1); glVertex2f( 1,  1);
322   glColor4f(1,1,1,.4); glNormal3f(0,0,1);  glTexCoord2f(1,0); glVertex2f( 1,  -1);
323 #endif
324   glEnd();
325   glEndList();
326
327   gp->quads = (struct quad *) malloc(sizeof(struct quad) * num_quads);
328   for (i=0; i < num_quads; i++)
329     {
330       gp->quads[i].rx = 0.;
331       gp->quads[i].ry = 0.;
332       gp->quads[i].rz = 0.;
333       gp->quads[i].tx = 0.;
334       gp->quads[i].ty = 0.;
335       gp->quads[i].tz = -10;
336
337       gp->quads[i].drx = frand(5.0);
338       gp->quads[i].dry = frand(5.0);
339       gp->quads[i].drz = 0;
340     }
341 }
342
343 static void initializeGL(ModeInfo *mi, GLsizei width, GLsizei height) 
344 {
345   pulsarstruct *gp = &Pulsar[MI_SCREEN(mi)];
346   GLfloat fogColor[4] = { 0.1, 0.1, 0.1, 0.1 };
347
348   glViewport( 0, 0, width, height ); 
349   resetProjection();
350
351   if (do_depth)
352         glEnable(GL_DEPTH_TEST);
353
354   if (do_antialias) {
355         do_blend = 1;
356         glEnable(GL_LINE_SMOOTH);
357   }
358
359   if (do_blend) {
360         glEnable(GL_BLEND);
361         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
362   }
363
364
365   if (do_light) {
366         glShadeModel(GL_SMOOTH);
367         glEnable(GL_COLOR_MATERIAL);
368         glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
369   }
370
371 # ifdef HAVE_JWZGLES /* #### glPolygonMode other than GL_FILL unimplemented */
372   do_wire = 0;
373 # endif
374
375   if (do_wire)
376         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
377   else 
378         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
379
380   if (do_fog) {
381         glEnable(GL_FOG);
382         glFogi(GL_FOG_MODE, GL_LINEAR);
383         glFogfv(GL_FOG_COLOR, fogColor);
384         glFogf(GL_FOG_DENSITY, 0.35);
385 /*      glHint(GL_FOG_HINT, GL_FASTEST); */
386         glFogf(GL_FOG_START, 50.0);
387         glFogf(GL_FOG_END, 100.0);
388   }
389         
390
391   if (do_texture)
392           Create_Texture(mi, which_image); 
393
394   GenerateQuad(gp);
395 }
396
397 static void drawQuads(pulsarstruct *gp) 
398 {
399   int i;
400   for (i=0; i < num_quads; i++)
401     {
402       glPushMatrix();
403       glTranslatef(gp->quads[i].tx,0,0);
404       glTranslatef(0,gp->quads[i].ty,0);
405       glTranslatef(0,0,gp->quads[i].tz);
406       glRotatef(gp->quads[i].rx, 1,0,0);
407       glRotatef(gp->quads[i].ry, 0,1,0);
408       glRotatef(gp->quads[i].rz, 0,0,1);
409       glCallList(gp->quad_list);
410       glPopMatrix();
411
412       gp->quads[i].rx += gp->quads[i].drx;
413       gp->quads[i].ry += gp->quads[i].dry;
414       gp->quads[i].rz += gp->quads[i].drz;
415
416     }
417 }
418
419 static GLvoid drawScene(ModeInfo * mi) 
420 {
421   pulsarstruct *gp = &Pulsar[MI_SCREEN(mi)];
422 /*  check_gl_error ("drawScene"); */
423   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
424
425   /* we have to do this here because the FPS meter turns these 3 features off!! */
426   {
427         if (do_light) {
428           glEnable(GL_LIGHTING);
429           glEnable(GL_LIGHT0);
430         }
431         
432         if (do_texture) {
433           glEnable(GL_TEXTURE_2D);
434         }
435         
436         if (do_blend) {
437           glEnable(GL_BLEND);
438           glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
439         }
440   }
441
442   resetProjection();
443
444   /* use XYZ scaling factors to change the size of the pulsar */
445   glScalef(gp->scale_x, gp->scale_y, gp->scale_z);
446   drawQuads(gp);
447   mi->polygon_count = num_quads;
448
449   /* update the scaling factors- cyclic */
450   gp->scale_x = cos(gp->frame/360.)*10.;
451   gp->scale_y = sin(gp->frame/360.)*10.;
452   gp->scale_z = 1;
453
454   /* increment frame-counter */
455   gp->frame++;
456
457 /*  check_gl_error ("drawScene"); */
458 }
459
460
461 ENTRYPOINT void draw_pulsar(ModeInfo * mi)
462 {
463   pulsarstruct *gp = &Pulsar[MI_SCREEN(mi)];
464   Display    *display = MI_DISPLAY(mi);
465   Window      window = MI_WINDOW(mi);
466
467   if (!gp->glx_context)
468         return;
469
470   glXMakeCurrent(display, window, *(gp->glx_context));
471   drawScene(mi);
472   if (mi->fps_p) do_fps (mi);
473   glXSwapBuffers(display, window);
474 }
475
476 /* Standard reshape function */
477 ENTRYPOINT void
478 reshape_pulsar(ModeInfo *mi, int width, int height)
479 {
480   glViewport( 0, 0, MI_WIDTH(mi), MI_HEIGHT(mi) );
481   resetProjection();
482 }
483
484 ENTRYPOINT void
485 init_pulsar(ModeInfo * mi)
486 {
487   int screen = MI_SCREEN(mi);
488
489   pulsarstruct *gp;
490
491   if (Pulsar == NULL) {
492         if ((Pulsar = (pulsarstruct *) calloc(MI_NUM_SCREENS(mi), sizeof (pulsarstruct))) == NULL)
493           return;
494   }
495   gp = &Pulsar[screen];
496
497   gp->window = MI_WINDOW(mi);
498
499   gp->scale_x = gp->scale_y = gp->scale_z = 1;
500
501   if ((gp->glx_context = init_GL(mi)) != NULL) {
502         reshape_pulsar(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
503         initializeGL(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
504   } else {
505         MI_CLEARWINDOW(mi);
506   }
507 }
508
509
510 /* all sorts of nice cleanup code should go here! */
511 ENTRYPOINT void release_pulsar(ModeInfo * mi)
512 {
513   int screen;
514   if (Pulsar != NULL) {
515         for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
516           pulsarstruct *gp = &Pulsar[screen];
517       free(gp->quads);
518         }
519         (void) free((void *) Pulsar);
520         Pulsar = NULL;
521   }
522   FreeAllGL(mi);
523 }
524 #endif
525
526 XSCREENSAVER_MODULE ("Pulsar", pulsar)