1 /* bouncingcow, Copyright (c) 2003-2014 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" \
19 # define refresh_cow 0
20 # define release_cow 0
21 #define DEF_SPEED "1.0"
22 #define DEF_TEXTURE "(none)"
25 #define countof(x) (sizeof((x))/sizeof((*x)))
28 #define BELLRAND(n) ((frand((n)) + frand((n)) + frand((n))) / 3)
30 #define RANDSIGN() ((random() & 1) ? 1 : -1)
32 #include "xlockmore.h"
34 #include "gltrackball.h"
35 #include "xpm-ximage.h"
38 #ifdef USE_GL /* whole file */
43 *cow_face, *cow_hide, *cow_hoofs, *cow_horns, *cow_tail, *cow_udder;
45 static struct gllist **all_objs[] = {
46 &cow_face, &cow_hide, &cow_hoofs, &cow_horns, &cow_tail, &cow_udder
60 GLfloat ddx, ddy, ddz;
66 GLXContext *glx_context;
67 trackball_state *trackball;
78 static cow_configuration *bps = NULL;
81 static const char *do_texture;
83 static XrmOptionDescRec opts[] = {
84 { "-speed", ".speed", XrmoptionSepArg, 0 },
85 {"-texture", ".texture", XrmoptionSepArg, 0 },
86 {"+texture", ".texture", XrmoptionNoArg, "(none)" },
89 static argtype vars[] = {
90 {&speed, "speed", "Speed", DEF_SPEED, t_Float},
91 {&do_texture, "texture", "Texture", DEF_TEXTURE, t_String},
94 ENTRYPOINT ModeSpecOpt cow_opts = {countof(opts), opts, countof(vars), vars, NULL};
100 reset_floater (ModeInfo *mi, floater *f)
102 cow_configuration *bp = &bps[MI_SCREEN(mi)];
108 /* Yes, I know I'm varying the force of gravity instead of varying the
109 launch velocity. That's intentional: empirical studies indicate
110 that it's way, way funnier that way. */
116 /* -0.18 max -0.3 top -0.4 middle -0.6 bottom */
117 f->ddy = speed * (-0.6 + BELLRAND(0.45));
121 f->spinner_p = !(random() % (12 * bp->nfloaters));
123 if (! (random() % (30 * bp->nfloaters)))
125 f->dx = BELLRAND(1.8) * RANDSIGN();
126 f->dz = BELLRAND(1.8) * RANDSIGN();
132 tick_floater (ModeInfo *mi, floater *f)
134 cow_configuration *bp = &bps[MI_SCREEN(mi)];
136 if (bp->button_down_p) return;
142 f->x += f->dx * speed;
143 f->y += f->dy * speed;
144 f->z += f->dz * speed;
146 if (f->y < -BOTTOM ||
147 f->x < -BOTTOM*8 || f->x > BOTTOM*8 ||
148 f->z < -BOTTOM*8 || f->z > BOTTOM*8)
149 reset_floater (mi, f);
153 /* Window management, etc
156 reshape_cow (ModeInfo *mi, int width, int height)
158 GLfloat h = (GLfloat) height / (GLfloat) width;
160 glViewport (0, 0, (GLint) width, (GLint) height);
162 glMatrixMode(GL_PROJECTION);
164 gluPerspective (30.0, 1/h, 1.0, 100);
166 glMatrixMode(GL_MODELVIEW);
168 gluLookAt( 0.0, 0.0, 30.0,
172 glClear(GL_COLOR_BUFFER_BIT);
177 cow_handle_event (ModeInfo *mi, XEvent *event)
179 cow_configuration *bp = &bps[MI_SCREEN(mi)];
181 if (gltrackball_event_handler (event, bp->trackball,
182 MI_WIDTH (mi), MI_HEIGHT (mi),
194 load_texture (ModeInfo *mi, const char *filename)
196 Display *dpy = mi->dpy;
197 Visual *visual = mi->xgwa.visual;
198 Colormap cmap = mi->xgwa.colormap;
202 if (MI_IS_WIREFRAME(mi))
207 !strcasecmp (filename, "(none)"))
209 glDisable (GL_TEXTURE_2D);
213 image = xpm_file_to_ximage (dpy, visual, cmap, filename);
214 if (!image) return False;
217 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA,
218 image->width, image->height, 0,
220 /* GL_UNSIGNED_BYTE, */
221 GL_UNSIGNED_INT_8_8_8_8_REV,
223 sprintf (buf, "texture: %.100s (%dx%d)",
224 filename, image->width, image->height);
227 glPixelStorei (GL_UNPACK_ALIGNMENT, 4);
228 glPixelStorei (GL_UNPACK_ROW_LENGTH, image->width);
235 init_cow (ModeInfo *mi)
237 cow_configuration *bp;
238 int wire = MI_IS_WIREFRAME(mi);
243 bps = (cow_configuration *)
244 calloc (MI_NUM_SCREENS(mi), sizeof (cow_configuration));
246 fprintf(stderr, "%s: out of memory\n", progname);
251 bp = &bps[MI_SCREEN(mi)];
253 bp->glx_context = init_GL(mi);
255 reshape_cow (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
257 glShadeModel(GL_SMOOTH);
259 glEnable(GL_DEPTH_TEST);
260 glEnable(GL_NORMALIZE);
261 glEnable(GL_CULL_FACE);
265 GLfloat pos[4] = {0.4, 0.2, 0.4, 0.0};
266 /* GLfloat amb[4] = {0.0, 0.0, 0.0, 1.0};*/
267 GLfloat amb[4] = {0.2, 0.2, 0.2, 1.0};
268 GLfloat dif[4] = {1.0, 1.0, 1.0, 1.0};
269 GLfloat spc[4] = {1.0, 1.0, 1.0, 1.0};
271 glEnable(GL_LIGHTING);
273 glEnable(GL_DEPTH_TEST);
274 glEnable(GL_CULL_FACE);
276 glLightfv(GL_LIGHT0, GL_POSITION, pos);
277 glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
278 glLightfv(GL_LIGHT0, GL_DIFFUSE, dif);
279 glLightfv(GL_LIGHT0, GL_SPECULAR, spc);
282 bp->trackball = gltrackball_init (False);
284 bp->dlists = (GLuint *) calloc (countof(all_objs)+1, sizeof(GLuint));
285 for (i = 0; i < countof(all_objs); i++)
286 bp->dlists[i] = glGenLists (1);
288 tex_p = load_texture (mi, do_texture);
290 glBindTexture (GL_TEXTURE_2D, bp->texture);
292 for (i = 0; i < countof(all_objs); i++)
294 GLfloat black[4] = {0, 0, 0, 1};
295 const struct gllist *gll = *all_objs[i];
297 glNewList (bp->dlists[i], GL_COMPILE);
299 glDisable (GL_TEXTURE_2D);
303 GLfloat color[4] = {0.63, 0.43, 0.36, 1.00};
306 /* if we have a texture, make the base color be white. */
307 color[0] = color[1] = color[2] = 1.0;
309 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
310 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
311 glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
312 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
313 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
314 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
315 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
316 glEnable(GL_TEXTURE_GEN_S);
317 glEnable(GL_TEXTURE_GEN_T);
318 glEnable(GL_TEXTURE_2D);
320 /* approximately line it up with ../images/earth.xpm */
321 glMatrixMode (GL_TEXTURE);
323 glTranslatef (0.45, 0.58, 0);
324 glScalef (0.08, 0.16, 1);
325 glRotatef (-5, 0, 0, 1);
326 glMatrixMode (GL_MODELVIEW);
328 glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
329 glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, black);
330 glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, 128);
334 GLfloat color[4] = {0.63, 0.43, 0.36, 1.00};
335 glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
336 glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, black);
337 glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, 128);
341 GLfloat color[4] = {1.00, 0.53, 0.53, 1.00};
342 glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
343 glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, black);
344 glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, 128);
346 else if (i == HOOFS || i == HORNS)
348 GLfloat color[4] = {0.20, 0.20, 0.20, 1.00};
349 GLfloat spec[4] = {0.30, 0.30, 0.30, 1.00};
351 glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
352 glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, spec);
353 glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, shiny);
357 GLfloat color[4] = {0.10, 0.10, 0.10, 1.00};
358 GLfloat spec[4] = {0.10, 0.10, 0.10, 1.00};
360 glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
361 glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, spec);
362 glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, shiny);
366 GLfloat color[4] = {1.00, 1.00, 1.00, 1.00};
367 GLfloat spec[4] = {1.00, 1.00, 1.00, 1.00};
368 GLfloat shiny = 128.0;
369 glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
370 glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, spec);
371 glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, shiny);
374 renderList (gll, wire);
379 bp->nfloaters = MI_COUNT (mi);
380 bp->floaters = (floater *) calloc (bp->nfloaters, sizeof (floater));
382 for (i = 0; i < bp->nfloaters; i++)
384 floater *f = &bp->floaters[i];
385 f->rot = make_rotator (10.0, 0, 0,
388 if (bp->nfloaters == 2)
394 double th = (i - 1) * M_PI*2 / (bp->nfloaters-1);
403 reset_floater (mi, f);
409 draw_floater (ModeInfo *mi, floater *f)
411 cow_configuration *bp = &bps[MI_SCREEN(mi)];
415 get_position (f->rot, &x, &y, &z, !bp->button_down_p);
418 glTranslatef (f->x, f->y, f->z);
420 gltrackball_rotate (bp->trackball);
422 glRotatef (y * 360, 0.0, 1.0, 0.0);
425 glRotatef (x * 360, 1.0, 0.0, 0.0);
426 glRotatef (z * 360, 0.0, 0.0, 1.0);
430 if (bp->nfloaters > 99) n *= 0.05;
431 else if (bp->nfloaters > 25) n *= 0.18;
432 else if (bp->nfloaters > 9) n *= 0.3;
433 else if (bp->nfloaters > 1) n *= 0.7;
436 glCallList (bp->dlists[FACE]);
437 mi->polygon_count += (*all_objs[FACE])->points / 3;
439 glCallList (bp->dlists[HIDE]);
440 mi->polygon_count += (*all_objs[HIDE])->points / 3;
442 glCallList (bp->dlists[HOOFS]);
443 mi->polygon_count += (*all_objs[HOOFS])->points / 3;
445 glCallList (bp->dlists[HORNS]);
446 mi->polygon_count += (*all_objs[HORNS])->points / 3;
448 glCallList (bp->dlists[TAIL]);
449 mi->polygon_count += (*all_objs[TAIL])->points / 3;
451 glCallList (bp->dlists[UDDER]);
452 mi->polygon_count += (*all_objs[UDDER])->points / 3;
460 draw_cow (ModeInfo *mi)
462 cow_configuration *bp = &bps[MI_SCREEN(mi)];
463 Display *dpy = MI_DISPLAY(mi);
464 Window window = MI_WINDOW(mi);
467 if (!bp->glx_context)
470 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(bp->glx_context));
472 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
476 # ifdef HAVE_MOBILE /* Keep it the same relative size when rotated. */
478 GLfloat h = MI_HEIGHT(mi) / (GLfloat) MI_WIDTH(mi);
479 int o = (int) current_device_rotation();
480 if (o != 0 && o != 180 && o != -180)
481 glScalef (1/h, 1/h, 1/h);
482 glRotatef(o, 0, 0, 1);
486 glScalef (0.5, 0.5, 0.5);
488 mi->polygon_count = 0;
494 F.dx = F.dy = F.dz = 0;
495 F.ddx = F.ddy = F.ddz = 0;
496 F.rot = make_rotator (0, 0, 0, 1, 0, False);
498 draw_floater (mi, &F);
501 for (i = 0; i < bp->nfloaters; i++)
503 /* "Don't kid yourself, Jimmy. If a cow ever got the chance,
504 he'd eat you and everyone you care about!"
505 -- Troy McClure in "Meat and You: Partners in Freedom"
507 floater *f = &bp->floaters[i];
508 draw_floater (mi, f);
509 tick_floater (mi, f);
515 if (mi->fps_p) do_fps (mi);
518 glXSwapBuffers(dpy, window);
521 XSCREENSAVER_MODULE_2 ("BouncingCow", bouncingcow, cow)