1 /* bouncingcow, Copyright (c) 2003, 2004 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 #include <X11/Intrinsic.h>
16 extern XtAppContext app;
18 #define PROGCLASS "BouncingCow"
19 #define HACK_INIT init_cow
20 #define HACK_DRAW draw_cows
21 #define HACK_RESHAPE reshape_cow
22 #define HACK_HANDLE_EVENT cow_handle_event
23 #define EVENT_MASK PointerMotionMask
24 #define sws_opts xlockmore_opts
26 #define DEF_SPEED "0.7"
27 #define DEF_TEXTURE "(none)"
29 #define DEFAULTS "*delay: 30000 \n" \
31 "*showFPS: False \n" \
32 "*wireframe: False \n" \
37 #define countof(x) (sizeof((x))/sizeof((*x)))
40 #define BELLRAND(n) ((frand((n)) + frand((n)) + frand((n))) / 3)
42 #define RANDSIGN() ((random() & 1) ? 1 : -1)
44 #include "xlockmore.h"
46 #include "gltrackball.h"
47 #include "xpm-ximage.h"
50 #ifdef USE_GL /* whole file */
58 *cow_face, *cow_hide, *cow_hoofs, *cow_horns, *cow_tail, *cow_udder;
60 struct gllist **all_objs[] = {
61 &cow_face, &cow_hide, &cow_hoofs, &cow_horns, &cow_tail, &cow_udder
75 GLfloat ddx, ddy, ddz;
81 GLXContext *glx_context;
82 trackball_state *trackball;
93 static cow_configuration *bps = NULL;
96 static const char *do_texture;
98 static XrmOptionDescRec opts[] = {
99 { "-speed", ".speed", XrmoptionSepArg, 0 },
100 {"-texture", ".texture", XrmoptionSepArg, 0 },
101 {"+texture", ".texture", XrmoptionNoArg, "(none)" },
104 static argtype vars[] = {
105 {&speed, "speed", "Speed", DEF_SPEED, t_Float},
106 {&do_texture, "texture", "Texture", DEF_TEXTURE, t_String},
109 ModeSpecOpt sws_opts = {countof(opts), opts, countof(vars), vars, NULL};
115 reset_floater (ModeInfo *mi, floater *f)
117 cow_configuration *bp = &bps[MI_SCREEN(mi)];
123 /* Yes, I know I'm varying the force of gravity instead of varying the
124 launch velocity. That's intentional: empirical studies indicate
125 that it's way, way funnier that way. */
131 /* -0.18 max -0.3 top -0.4 middle -0.6 bottom */
132 f->ddy = speed * (-0.6 + BELLRAND(0.45));
136 f->spinner_p = !(random() % (12 * bp->nfloaters));
138 if (! (random() % (30 * bp->nfloaters)))
140 f->dx = BELLRAND(1.8) * RANDSIGN();
141 f->dz = BELLRAND(1.8) * RANDSIGN();
147 tick_floater (ModeInfo *mi, floater *f)
149 cow_configuration *bp = &bps[MI_SCREEN(mi)];
151 if (bp->button_down_p) return;
157 f->x += f->dx * speed;
158 f->y += f->dy * speed;
159 f->z += f->dz * speed;
161 if (f->y < -BOTTOM ||
162 f->x < -BOTTOM*8 || f->x > BOTTOM*8 ||
163 f->z < -BOTTOM*8 || f->z > BOTTOM*8)
164 reset_floater (mi, f);
168 /* Window management, etc
171 reshape_cow (ModeInfo *mi, int width, int height)
173 GLfloat h = (GLfloat) height / (GLfloat) width;
175 glViewport (0, 0, (GLint) width, (GLint) height);
177 glMatrixMode(GL_PROJECTION);
179 gluPerspective (30.0, 1/h, 1.0, 100);
181 glMatrixMode(GL_MODELVIEW);
183 gluLookAt( 0.0, 0.0, 30.0,
187 glClear(GL_COLOR_BUFFER_BIT);
192 cow_handle_event (ModeInfo *mi, XEvent *event)
194 cow_configuration *bp = &bps[MI_SCREEN(mi)];
196 if (event->xany.type == ButtonPress &&
197 event->xbutton.button == Button1)
199 bp->button_down_p = True;
200 gltrackball_start (bp->trackball,
201 event->xbutton.x, event->xbutton.y,
202 MI_WIDTH (mi), MI_HEIGHT (mi));
205 else if (event->xany.type == ButtonRelease &&
206 event->xbutton.button == Button1)
208 bp->button_down_p = False;
211 else if (event->xany.type == ButtonPress &&
212 (event->xbutton.button == Button4 ||
213 event->xbutton.button == Button5))
215 gltrackball_mousewheel (bp->trackball, event->xbutton.button, 10,
216 !event->xbutton.state);
219 else if (event->xany.type == MotionNotify &&
222 gltrackball_track (bp->trackball,
223 event->xmotion.x, event->xmotion.y,
224 MI_WIDTH (mi), MI_HEIGHT (mi));
236 load_texture (ModeInfo *mi, const char *filename)
238 Display *dpy = mi->dpy;
239 Visual *visual = mi->xgwa.visual;
240 Colormap cmap = mi->xgwa.colormap;
244 if (MI_IS_WIREFRAME(mi))
249 !strcasecmp (filename, "(none)"))
251 glDisable (GL_TEXTURE_2D);
255 image = xpm_file_to_ximage (dpy, visual, cmap, filename);
258 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA,
259 image->width, image->height, 0,
261 /* GL_UNSIGNED_BYTE, */
262 GL_UNSIGNED_INT_8_8_8_8_REV,
264 sprintf (buf, "texture: %.100s (%dx%d)",
265 filename, image->width, image->height);
268 glPixelStorei (GL_UNPACK_ALIGNMENT, 4);
269 glPixelStorei (GL_UNPACK_ROW_LENGTH, image->width);
270 glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
271 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
272 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
273 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
274 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
276 glEnable(GL_TEXTURE_GEN_S);
277 glEnable(GL_TEXTURE_GEN_T);
278 glEnable(GL_TEXTURE_2D);
284 init_cow (ModeInfo *mi)
286 cow_configuration *bp;
287 int wire = MI_IS_WIREFRAME(mi);
291 bps = (cow_configuration *)
292 calloc (MI_NUM_SCREENS(mi), sizeof (cow_configuration));
294 fprintf(stderr, "%s: out of memory\n", progname);
298 bp = &bps[MI_SCREEN(mi)];
301 bp = &bps[MI_SCREEN(mi)];
303 bp->glx_context = init_GL(mi);
305 reshape_cow (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
307 glShadeModel(GL_SMOOTH);
309 glEnable(GL_DEPTH_TEST);
310 glEnable(GL_NORMALIZE);
311 glEnable(GL_CULL_FACE);
315 GLfloat pos[4] = {0.4, 0.2, 0.4, 0.0};
316 /* GLfloat amb[4] = {0.0, 0.0, 0.0, 1.0};*/
317 GLfloat amb[4] = {0.2, 0.2, 0.2, 1.0};
318 GLfloat dif[4] = {1.0, 1.0, 1.0, 1.0};
319 GLfloat spc[4] = {1.0, 1.0, 1.0, 1.0};
321 glEnable(GL_LIGHTING);
323 glEnable(GL_DEPTH_TEST);
324 glEnable(GL_CULL_FACE);
326 glLightfv(GL_LIGHT0, GL_POSITION, pos);
327 glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
328 glLightfv(GL_LIGHT0, GL_DIFFUSE, dif);
329 glLightfv(GL_LIGHT0, GL_SPECULAR, spc);
332 bp->trackball = gltrackball_init ();
334 bp->dlists = (GLuint *) calloc (countof(all_objs)+1, sizeof(GLuint));
335 for (i = 0; i < countof(all_objs); i++)
336 bp->dlists[i] = glGenLists (1);
338 for (i = 0; i < countof(all_objs); i++)
340 GLfloat black[4] = {0, 0, 0, 1};
341 struct gllist *gll = *all_objs[i];
343 gll->primitive = GL_LINE_LOOP;
345 glNewList (bp->dlists[i], GL_COMPILE);
347 glMatrixMode(GL_MODELVIEW);
349 glMatrixMode(GL_TEXTURE);
351 glMatrixMode(GL_MODELVIEW);
353 glBindTexture (GL_TEXTURE_2D, 0);
357 GLfloat color[4] = {0.63, 0.43, 0.36, 1.00};
359 if (load_texture (mi, do_texture))
361 glBindTexture (GL_TEXTURE_2D, bp->texture);
362 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
363 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
364 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
366 /* approximately line it up with ../images/earth.xpm */
367 glMatrixMode(GL_TEXTURE);
368 glTranslatef(0.45, 0.58, 0);
369 glScalef(0.08, 0.16, 1);
370 glRotatef(-5, 0, 0, 1);
371 glMatrixMode(GL_MODELVIEW);
373 /* if we have a texture, make the base color be white. */
374 color[0] = color[1] = color[2] = 1.0;
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);
383 GLfloat color[4] = {0.63, 0.43, 0.36, 1.00};
384 glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
385 glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, black);
386 glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, 128);
390 GLfloat color[4] = {1.00, 0.53, 0.53, 1.00};
391 glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
392 glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, black);
393 glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, 128);
395 else if (i == HOOFS || i == HORNS)
397 GLfloat color[4] = {0.20, 0.20, 0.20, 1.00};
398 GLfloat spec[4] = {0.30, 0.30, 0.30, 1.00};
400 glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
401 glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, spec);
402 glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, shiny);
406 GLfloat color[4] = {0.10, 0.10, 0.10, 1.00};
407 GLfloat spec[4] = {0.10, 0.10, 0.10, 1.00};
409 glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
410 glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, spec);
411 glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, shiny);
415 GLfloat color[4] = {1.00, 1.00, 1.00, 1.00};
416 GLfloat spec[4] = {1.00, 1.00, 1.00, 1.00};
417 GLfloat shiny = 128.0;
418 glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
419 glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, spec);
420 glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, shiny);
425 glMatrixMode(GL_TEXTURE);
427 glMatrixMode(GL_MODELVIEW);
433 bp->nfloaters = MI_COUNT (mi);
434 bp->floaters = (floater *) calloc (bp->nfloaters, sizeof (floater));
436 for (i = 0; i < bp->nfloaters; i++)
438 floater *f = &bp->floaters[i];
439 f->rot = make_rotator (10.0, 0, 0,
442 if (bp->nfloaters == 2)
448 double th = (i - 1) * M_PI*2 / (bp->nfloaters-1);
457 reset_floater (mi, f);
463 draw_floater (ModeInfo *mi, floater *f)
465 cow_configuration *bp = &bps[MI_SCREEN(mi)];
469 get_position (f->rot, &x, &y, &z, !bp->button_down_p);
472 glTranslatef (f->x, f->y, f->z);
474 glRotatef (y * 360, 0.0, 1.0, 0.0);
477 glRotatef (x * 360, 1.0, 0.0, 0.0);
478 glRotatef (z * 360, 0.0, 0.0, 1.0);
482 if (bp->nfloaters > 99) n *= 0.05;
483 else if (bp->nfloaters > 25) n *= 0.18;
484 else if (bp->nfloaters > 9) n *= 0.3;
485 else if (bp->nfloaters > 1) n *= 0.7;
488 glCallList (bp->dlists[FACE]);
489 mi->polygon_count += (*all_objs[FACE])->points / 3;
491 glCallList (bp->dlists[HIDE]);
492 mi->polygon_count += (*all_objs[HIDE])->points / 3;
494 glCallList (bp->dlists[HOOFS]);
495 mi->polygon_count += (*all_objs[HOOFS])->points / 3;
497 glCallList (bp->dlists[HORNS]);
498 mi->polygon_count += (*all_objs[HORNS])->points / 3;
500 glCallList (bp->dlists[TAIL]);
501 mi->polygon_count += (*all_objs[TAIL])->points / 3;
503 glCallList (bp->dlists[UDDER]);
504 mi->polygon_count += (*all_objs[UDDER])->points / 3;
512 draw_cows (ModeInfo *mi)
514 cow_configuration *bp = &bps[MI_SCREEN(mi)];
515 Display *dpy = MI_DISPLAY(mi);
516 Window window = MI_WINDOW(mi);
519 if (!bp->glx_context)
522 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
525 gltrackball_rotate (bp->trackball);
527 glScalef (0.5, 0.5, 0.5);
529 mi->polygon_count = 0;
535 F.dx = F.dy = F.dz = 0;
536 F.ddx = F.ddy = F.ddz = 0;
537 F.rot = make_rotator (0, 0, 0, 1, 0, False);
539 draw_floater (mi, &F);
542 for (i = 0; i < bp->nfloaters; i++)
544 /* "Don't kid yourself, Jimmy. If a cow ever got the chance,
545 he'd eat you and everyone you care about!"
546 -- Troy McClure in "Meat and You: Partners in Freedom"
548 floater *f = &bp->floaters[i];
549 draw_floater (mi, f);
550 tick_floater (mi, f);
556 if (mi->fps_p) do_fps (mi);
559 glXSwapBuffers(dpy, window);