1 /* bouncingcow, Copyright (c) 2003-2006 Jamie Zawinski <jwz@jwz.org>
3 * Permission to use, copy, modify, distribute, and sell this software and its
4 * documentation for any purpose is hereby granted without fee, provided that
5 * the above copyright notice appear in all copies and that both that
6 * copyright notice and this permission notice appear in supporting
7 * documentation. No representations are made about the suitability of this
8 * software for any purpose. It is provided "as is" without express or
11 * Boing, boing, boing. Cow, cow, cow.
14 #define DEFAULTS "*delay: 30000 \n" \
16 "*showFPS: False \n" \
17 "*wireframe: False \n" \
22 # define refresh_cow 0
23 # define release_cow 0
24 #define DEF_SPEED "1.0"
25 #define DEF_TEXTURE "(none)"
28 #define countof(x) (sizeof((x))/sizeof((*x)))
31 #define BELLRAND(n) ((frand((n)) + frand((n)) + frand((n))) / 3)
33 #define RANDSIGN() ((random() & 1) ? 1 : -1)
35 #include "xlockmore.h"
37 #include "gltrackball.h"
38 #include "xpm-ximage.h"
41 #ifdef USE_GL /* whole file */
46 *cow_face, *cow_hide, *cow_hoofs, *cow_horns, *cow_tail, *cow_udder;
48 static struct gllist **all_objs[] = {
49 &cow_face, &cow_hide, &cow_hoofs, &cow_horns, &cow_tail, &cow_udder
63 GLfloat ddx, ddy, ddz;
69 GLXContext *glx_context;
70 trackball_state *trackball;
81 static cow_configuration *bps = NULL;
84 static const char *do_texture;
86 static XrmOptionDescRec opts[] = {
87 { "-speed", ".speed", XrmoptionSepArg, 0 },
88 {"-texture", ".texture", XrmoptionSepArg, 0 },
89 {"+texture", ".texture", XrmoptionNoArg, "(none)" },
92 static argtype vars[] = {
93 {&speed, "speed", "Speed", DEF_SPEED, t_Float},
94 {&do_texture, "texture", "Texture", DEF_TEXTURE, t_String},
97 ENTRYPOINT ModeSpecOpt cow_opts = {countof(opts), opts, countof(vars), vars, NULL};
103 reset_floater (ModeInfo *mi, floater *f)
105 cow_configuration *bp = &bps[MI_SCREEN(mi)];
111 /* Yes, I know I'm varying the force of gravity instead of varying the
112 launch velocity. That's intentional: empirical studies indicate
113 that it's way, way funnier that way. */
119 /* -0.18 max -0.3 top -0.4 middle -0.6 bottom */
120 f->ddy = speed * (-0.6 + BELLRAND(0.45));
124 f->spinner_p = !(random() % (12 * bp->nfloaters));
126 if (! (random() % (30 * bp->nfloaters)))
128 f->dx = BELLRAND(1.8) * RANDSIGN();
129 f->dz = BELLRAND(1.8) * RANDSIGN();
135 tick_floater (ModeInfo *mi, floater *f)
137 cow_configuration *bp = &bps[MI_SCREEN(mi)];
139 if (bp->button_down_p) return;
145 f->x += f->dx * speed;
146 f->y += f->dy * speed;
147 f->z += f->dz * speed;
149 if (f->y < -BOTTOM ||
150 f->x < -BOTTOM*8 || f->x > BOTTOM*8 ||
151 f->z < -BOTTOM*8 || f->z > BOTTOM*8)
152 reset_floater (mi, f);
156 /* Window management, etc
159 reshape_cow (ModeInfo *mi, int width, int height)
161 GLfloat h = (GLfloat) height / (GLfloat) width;
163 glViewport (0, 0, (GLint) width, (GLint) height);
165 glMatrixMode(GL_PROJECTION);
167 gluPerspective (30.0, 1/h, 1.0, 100);
169 glMatrixMode(GL_MODELVIEW);
171 gluLookAt( 0.0, 0.0, 30.0,
175 glClear(GL_COLOR_BUFFER_BIT);
180 cow_handle_event (ModeInfo *mi, XEvent *event)
182 cow_configuration *bp = &bps[MI_SCREEN(mi)];
184 if (event->xany.type == ButtonPress &&
185 event->xbutton.button == Button1)
187 bp->button_down_p = True;
188 gltrackball_start (bp->trackball,
189 event->xbutton.x, event->xbutton.y,
190 MI_WIDTH (mi), MI_HEIGHT (mi));
193 else if (event->xany.type == ButtonRelease &&
194 event->xbutton.button == Button1)
196 bp->button_down_p = False;
199 else if (event->xany.type == ButtonPress &&
200 (event->xbutton.button == Button4 ||
201 event->xbutton.button == Button5 ||
202 event->xbutton.button == Button6 ||
203 event->xbutton.button == Button7))
205 gltrackball_mousewheel (bp->trackball, event->xbutton.button, 10,
206 !event->xbutton.state);
209 else if (event->xany.type == MotionNotify &&
212 gltrackball_track (bp->trackball,
213 event->xmotion.x, event->xmotion.y,
214 MI_WIDTH (mi), MI_HEIGHT (mi));
226 load_texture (ModeInfo *mi, const char *filename)
228 Display *dpy = mi->dpy;
229 Visual *visual = mi->xgwa.visual;
230 Colormap cmap = mi->xgwa.colormap;
234 if (MI_IS_WIREFRAME(mi))
239 !strcasecmp (filename, "(none)"))
241 glDisable (GL_TEXTURE_2D);
245 image = xpm_file_to_ximage (dpy, visual, cmap, filename);
248 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA,
249 image->width, image->height, 0,
251 /* GL_UNSIGNED_BYTE, */
252 GL_UNSIGNED_INT_8_8_8_8_REV,
254 sprintf (buf, "texture: %.100s (%dx%d)",
255 filename, image->width, image->height);
258 glPixelStorei (GL_UNPACK_ALIGNMENT, 4);
259 glPixelStorei (GL_UNPACK_ROW_LENGTH, image->width);
260 glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
261 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
262 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
263 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
264 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
266 glEnable(GL_TEXTURE_GEN_S);
267 glEnable(GL_TEXTURE_GEN_T);
268 glEnable(GL_TEXTURE_2D);
274 init_cow (ModeInfo *mi)
276 cow_configuration *bp;
277 int wire = MI_IS_WIREFRAME(mi);
281 bps = (cow_configuration *)
282 calloc (MI_NUM_SCREENS(mi), sizeof (cow_configuration));
284 fprintf(stderr, "%s: out of memory\n", progname);
289 bp = &bps[MI_SCREEN(mi)];
291 bp->glx_context = init_GL(mi);
293 reshape_cow (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
295 glShadeModel(GL_SMOOTH);
297 glEnable(GL_DEPTH_TEST);
298 glEnable(GL_NORMALIZE);
299 glEnable(GL_CULL_FACE);
303 GLfloat pos[4] = {0.4, 0.2, 0.4, 0.0};
304 /* GLfloat amb[4] = {0.0, 0.0, 0.0, 1.0};*/
305 GLfloat amb[4] = {0.2, 0.2, 0.2, 1.0};
306 GLfloat dif[4] = {1.0, 1.0, 1.0, 1.0};
307 GLfloat spc[4] = {1.0, 1.0, 1.0, 1.0};
309 glEnable(GL_LIGHTING);
311 glEnable(GL_DEPTH_TEST);
312 glEnable(GL_CULL_FACE);
314 glLightfv(GL_LIGHT0, GL_POSITION, pos);
315 glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
316 glLightfv(GL_LIGHT0, GL_DIFFUSE, dif);
317 glLightfv(GL_LIGHT0, GL_SPECULAR, spc);
320 bp->trackball = gltrackball_init ();
322 bp->dlists = (GLuint *) calloc (countof(all_objs)+1, sizeof(GLuint));
323 for (i = 0; i < countof(all_objs); i++)
324 bp->dlists[i] = glGenLists (1);
326 for (i = 0; i < countof(all_objs); i++)
328 GLfloat black[4] = {0, 0, 0, 1};
329 const struct gllist *gll = *all_objs[i];
331 glNewList (bp->dlists[i], GL_COMPILE);
333 glMatrixMode(GL_MODELVIEW);
335 glMatrixMode(GL_TEXTURE);
337 glMatrixMode(GL_MODELVIEW);
339 glBindTexture (GL_TEXTURE_2D, 0);
343 GLfloat color[4] = {0.63, 0.43, 0.36, 1.00};
345 if (load_texture (mi, do_texture))
347 glBindTexture (GL_TEXTURE_2D, bp->texture);
348 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
349 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
350 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
352 /* approximately line it up with ../images/earth.xpm */
353 glMatrixMode(GL_TEXTURE);
354 glTranslatef(0.45, 0.58, 0);
355 glScalef(0.08, 0.16, 1);
356 glRotatef(-5, 0, 0, 1);
357 glMatrixMode(GL_MODELVIEW);
359 /* if we have a texture, make the base color be white. */
360 color[0] = color[1] = color[2] = 1.0;
363 glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
364 glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, black);
365 glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, 128);
369 GLfloat color[4] = {0.63, 0.43, 0.36, 1.00};
370 glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
371 glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, black);
372 glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, 128);
376 GLfloat color[4] = {1.00, 0.53, 0.53, 1.00};
377 glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
378 glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, black);
379 glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, 128);
381 else if (i == HOOFS || i == HORNS)
383 GLfloat color[4] = {0.20, 0.20, 0.20, 1.00};
384 GLfloat spec[4] = {0.30, 0.30, 0.30, 1.00};
386 glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
387 glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, spec);
388 glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, shiny);
392 GLfloat color[4] = {0.10, 0.10, 0.10, 1.00};
393 GLfloat spec[4] = {0.10, 0.10, 0.10, 1.00};
395 glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
396 glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, spec);
397 glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, shiny);
401 GLfloat color[4] = {1.00, 1.00, 1.00, 1.00};
402 GLfloat spec[4] = {1.00, 1.00, 1.00, 1.00};
403 GLfloat shiny = 128.0;
404 glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
405 glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, spec);
406 glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, shiny);
409 renderList (gll, wire);
411 glMatrixMode(GL_TEXTURE);
413 glMatrixMode(GL_MODELVIEW);
419 bp->nfloaters = MI_COUNT (mi);
420 bp->floaters = (floater *) calloc (bp->nfloaters, sizeof (floater));
422 for (i = 0; i < bp->nfloaters; i++)
424 floater *f = &bp->floaters[i];
425 f->rot = make_rotator (10.0, 0, 0,
428 if (bp->nfloaters == 2)
434 double th = (i - 1) * M_PI*2 / (bp->nfloaters-1);
443 reset_floater (mi, f);
449 draw_floater (ModeInfo *mi, floater *f)
451 cow_configuration *bp = &bps[MI_SCREEN(mi)];
455 get_position (f->rot, &x, &y, &z, !bp->button_down_p);
458 glTranslatef (f->x, f->y, f->z);
460 glRotatef (y * 360, 0.0, 1.0, 0.0);
463 glRotatef (x * 360, 1.0, 0.0, 0.0);
464 glRotatef (z * 360, 0.0, 0.0, 1.0);
468 if (bp->nfloaters > 99) n *= 0.05;
469 else if (bp->nfloaters > 25) n *= 0.18;
470 else if (bp->nfloaters > 9) n *= 0.3;
471 else if (bp->nfloaters > 1) n *= 0.7;
474 glCallList (bp->dlists[FACE]);
475 mi->polygon_count += (*all_objs[FACE])->points / 3;
477 glCallList (bp->dlists[HIDE]);
478 mi->polygon_count += (*all_objs[HIDE])->points / 3;
480 glCallList (bp->dlists[HOOFS]);
481 mi->polygon_count += (*all_objs[HOOFS])->points / 3;
483 glCallList (bp->dlists[HORNS]);
484 mi->polygon_count += (*all_objs[HORNS])->points / 3;
486 glCallList (bp->dlists[TAIL]);
487 mi->polygon_count += (*all_objs[TAIL])->points / 3;
489 glCallList (bp->dlists[UDDER]);
490 mi->polygon_count += (*all_objs[UDDER])->points / 3;
498 draw_cow (ModeInfo *mi)
500 cow_configuration *bp = &bps[MI_SCREEN(mi)];
501 Display *dpy = MI_DISPLAY(mi);
502 Window window = MI_WINDOW(mi);
505 if (!bp->glx_context)
508 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(bp->glx_context));
510 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
513 gltrackball_rotate (bp->trackball);
515 glScalef (0.5, 0.5, 0.5);
517 mi->polygon_count = 0;
523 F.dx = F.dy = F.dz = 0;
524 F.ddx = F.ddy = F.ddz = 0;
525 F.rot = make_rotator (0, 0, 0, 1, 0, False);
527 draw_floater (mi, &F);
530 for (i = 0; i < bp->nfloaters; i++)
532 /* "Don't kid yourself, Jimmy. If a cow ever got the chance,
533 he'd eat you and everyone you care about!"
534 -- Troy McClure in "Meat and You: Partners in Freedom"
536 floater *f = &bp->floaters[i];
537 draw_floater (mi, f);
538 tick_floater (mi, f);
544 if (mi->fps_p) do_fps (mi);
547 glXSwapBuffers(dpy, window);
550 XSCREENSAVER_MODULE_2 ("BouncingCow", bouncingcow, cow)