1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* pulsar --- pulsar module for xpulsar */
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.
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.
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
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.
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
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
44 #define DEFAULTS "*delay: 10000 \n" \
45 "*showFPS: False \n" \
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 */
54 #ifdef USE_GL /* whole file */
58 # include <X11/Xmu/Drawing.h>
60 # include <Xmu/Drawing.h>
64 #include "xpm-ximage.h"
66 /* Functions for loading and storing textures */
68 #define checkImageWidth 64
69 #define checkImageHeight 64
71 /* Functions for handling the frames per second timer */
74 #define countof(x) (sizeof((x))/sizeof((*x)))
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"
97 static int do_antialias;
98 static int do_texture;
99 static int do_texture_quality;
100 static int do_mipmap;
102 static char *which_image;
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 },
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},
144 static OptionStruct desc[] =
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"},
159 ENTRYPOINT ModeSpecOpt pulsar_opts = {countof(opts), opts, countof(vars), vars, desc};
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};
174 GLfloat dtx, dty, dtz;
175 GLfloat drx, dry, drz;
180 /* structure for holding the pulsar data */
182 int screen_width, screen_height;
183 GLXContext *glx_context;
188 float scale_x, scale_y, scale_z;
195 static pulsarstruct *Pulsar = NULL;
198 Generate_Image(int *width, int *height, int *format)
204 *width = checkImageWidth;
205 *height = checkImageHeight;
206 result = (GLubyte *)malloc(4 * (*width) * (*height));
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;
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)
232 if ( !strncmp(filename, "BUILTIN", 7))
233 image = Generate_Image(&width, &height, &format);
236 XImage *ximage = xpm_file_to_ximage (MI_DISPLAY (mi), MI_VISUAL (mi),
237 MI_COLORMAP (mi), filename);
238 image = (GLubyte *) ximage->data;
239 width = ximage->width;
240 height = ximage->height;
244 /* GL_MODULATE or GL_DECAL depending on what you want */
245 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
246 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
247 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
248 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
249 /* perhaps we can edge a bit more speed at the expense of quality */
250 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
252 if (do_texture_quality) {
253 /* with texture_quality, the min and mag filters look *much* nice but are *much* slower */
254 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
255 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
258 /* default is to do it quick and dirty */
259 /* if you have mipmaps turned on, but not texture quality, nothing will happen! */
260 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
261 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
264 /* mipmaps make the image look much nicer */
269 status = gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width, height, format,
270 GL_UNSIGNED_BYTE, image);
273 const char *s = (char *) gluErrorString (status);
274 fprintf (stderr, "%s: error mipmapping %dx%d texture: %s\n",
275 progname, width, height,
276 (s ? s : "(unknown)"));
279 check_gl_error("mipmapping");
284 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0,
285 format, GL_UNSIGNED_BYTE, image);
286 check_gl_error("texture");
290 static void resetProjection(void)
292 glMatrixMode(GL_PROJECTION);
294 glFrustum(-1, 1, -1, 1, 1, 100);
295 glMatrixMode(GL_MODELVIEW);
300 static void GenerateQuad(pulsarstruct *gp)
304 gp->quad_list = glGenLists(1);
305 glNewList(gp->quad_list,GL_COMPILE);
308 glColor4f(1,0,0,.4); glNormal3f(0,0,1); glTexCoord2f(0,0); glVertex2f(-1, -1);
309 glColor4f(0,1,0,.4); glNormal3f(0,0,1); glTexCoord2f(0,1); glVertex2f(-1, 1);
310 glColor4f(0,0,1,.4); glNormal3f(0,0,1); glTexCoord2f(1,1); glVertex2f( 1, 1);
311 glColor4f(1,1,1,1); glNormal3f(0,0,1); glTexCoord2f(1,0); glVertex2f( 1, -1);
313 glBegin(GL_TRIANGLE_STRIP);
314 glColor4f(0,1,0,.4); glNormal3f(0,0,1); glTexCoord2f(0,1); glVertex2f(-1, 1);
315 glColor4f(1,0,0,.4); glNormal3f(0,0,1); glTexCoord2f(0,0); glVertex2f(-1, -1);
316 glColor4f(0,0,1,.4); glNormal3f(0,0,1); glTexCoord2f(1,1); glVertex2f( 1, 1);
317 glColor4f(1,1,1,.4); glNormal3f(0,0,1); glTexCoord2f(1,0); glVertex2f( 1, -1);
322 gp->quads = (struct quad *) malloc(sizeof(struct quad) * num_quads);
323 for (i=0; i < num_quads; i++)
325 gp->quads[i].rx = 0.;
326 gp->quads[i].ry = 0.;
327 gp->quads[i].rz = 0.;
328 gp->quads[i].tx = 0.;
329 gp->quads[i].ty = 0.;
330 gp->quads[i].tz = -10;
332 gp->quads[i].drx = frand(5.0);
333 gp->quads[i].dry = frand(5.0);
334 gp->quads[i].drz = 0;
338 static void initializeGL(ModeInfo *mi, GLsizei width, GLsizei height)
340 pulsarstruct *gp = &Pulsar[MI_SCREEN(mi)];
341 GLfloat fogColor[4] = { 0.1, 0.1, 0.1, 0.1 };
343 glViewport( 0, 0, width, height );
347 glEnable(GL_DEPTH_TEST);
351 glEnable(GL_LINE_SMOOTH);
356 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
361 glShadeModel(GL_SMOOTH);
362 glEnable(GL_COLOR_MATERIAL);
363 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
366 # ifdef HAVE_JWZGLES /* #### glPolygonMode other than GL_FILL unimplemented */
371 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
373 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
377 glFogi(GL_FOG_MODE, GL_LINEAR);
378 glFogfv(GL_FOG_COLOR, fogColor);
379 glFogf(GL_FOG_DENSITY, 0.35);
380 /* glHint(GL_FOG_HINT, GL_FASTEST); */
381 glFogf(GL_FOG_START, 50.0);
382 glFogf(GL_FOG_END, 100.0);
387 Create_Texture(mi, which_image);
392 static void drawQuads(pulsarstruct *gp)
395 for (i=0; i < num_quads; i++)
398 glTranslatef(gp->quads[i].tx,0,0);
399 glTranslatef(0,gp->quads[i].ty,0);
400 glTranslatef(0,0,gp->quads[i].tz);
401 glRotatef(gp->quads[i].rx, 1,0,0);
402 glRotatef(gp->quads[i].ry, 0,1,0);
403 glRotatef(gp->quads[i].rz, 0,0,1);
404 glCallList(gp->quad_list);
407 gp->quads[i].rx += gp->quads[i].drx;
408 gp->quads[i].ry += gp->quads[i].dry;
409 gp->quads[i].rz += gp->quads[i].drz;
414 static GLvoid drawScene(ModeInfo * mi)
416 pulsarstruct *gp = &Pulsar[MI_SCREEN(mi)];
417 /* check_gl_error ("drawScene"); */
418 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
420 /* we have to do this here because the FPS meter turns these 3 features off!! */
423 glEnable(GL_LIGHTING);
428 glEnable(GL_TEXTURE_2D);
433 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
439 /* use XYZ scaling factors to change the size of the pulsar */
440 glScalef(gp->scale_x, gp->scale_y, gp->scale_z);
442 mi->polygon_count = num_quads;
444 /* update the scaling factors- cyclic */
445 gp->scale_x = cos(gp->frame/360.)*10.;
446 gp->scale_y = sin(gp->frame/360.)*10.;
449 /* increment frame-counter */
452 /* check_gl_error ("drawScene"); */
456 ENTRYPOINT void draw_pulsar(ModeInfo * mi)
458 pulsarstruct *gp = &Pulsar[MI_SCREEN(mi)];
459 Display *display = MI_DISPLAY(mi);
460 Window window = MI_WINDOW(mi);
462 if (!gp->glx_context)
465 glXMakeCurrent(display, window, *(gp->glx_context));
467 if (mi->fps_p) do_fps (mi);
468 glXSwapBuffers(display, window);
471 /* Standard reshape function */
473 reshape_pulsar(ModeInfo *mi, int width, int height)
475 glViewport( 0, 0, MI_WIDTH(mi), MI_HEIGHT(mi) );
480 init_pulsar(ModeInfo * mi)
482 int screen = MI_SCREEN(mi);
486 if (Pulsar == NULL) {
487 if ((Pulsar = (pulsarstruct *) calloc(MI_NUM_SCREENS(mi), sizeof (pulsarstruct))) == NULL)
490 gp = &Pulsar[screen];
492 gp->window = MI_WINDOW(mi);
494 gp->scale_x = gp->scale_y = gp->scale_z = 1;
496 if ((gp->glx_context = init_GL(mi)) != NULL) {
497 reshape_pulsar(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
498 initializeGL(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
505 /* all sorts of nice cleanup code should go here! */
506 ENTRYPOINT void release_pulsar(ModeInfo * mi)
509 if (Pulsar != NULL) {
510 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
511 pulsarstruct *gp = &Pulsar[screen];
514 (void) free((void *) Pulsar);
521 XSCREENSAVER_MODULE ("Pulsar", pulsar)