From http://www.jwz.org/xscreensaver/xscreensaver-5.37.tar.gz
[xscreensaver] / hacks / glx / bouncingcow.c
1 /* bouncingcow, Copyright (c) 2003-2014 Jamie Zawinski <jwz@jwz.org>
2  *
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 
9  * implied warranty.
10  *
11  * Boing, boing, boing.  Cow, cow, cow.
12  */
13
14 #define DEFAULTS        "*delay:        30000       \n" \
15                         "*count:        1           \n" \
16                         "*showFPS:      False       \n" \
17                         "*wireframe:    False       \n" \
18
19 # define refresh_cow 0
20 # define release_cow 0
21 #define DEF_SPEED       "1.0"
22 #define DEF_TEXTURE     "(none)"
23
24 #undef countof
25 #define countof(x) (sizeof((x))/sizeof((*x)))
26
27 #undef BELLRAND
28 #define BELLRAND(n) ((frand((n)) + frand((n)) + frand((n))) / 3)
29 #undef RANDSIGN
30 #define RANDSIGN() ((random() & 1) ? 1 : -1)
31
32 #include "xlockmore.h"
33 #include "rotator.h"
34 #include "gltrackball.h"
35 #include "xpm-ximage.h"
36 #include <ctype.h>
37
38 #ifdef USE_GL /* whole file */
39
40 #include "gllist.h"
41
42 extern struct gllist
43  *cow_face, *cow_hide, *cow_hoofs, *cow_horns, *cow_tail, *cow_udder;
44
45 static struct gllist **all_objs[] = {
46  &cow_face, &cow_hide, &cow_hoofs, &cow_horns, &cow_tail, &cow_udder
47 };
48
49 #define FACE    0
50 #define HIDE    1
51 #define HOOFS   2
52 #define HORNS   3
53 #define TAIL    4
54 #define UDDER   5
55
56 typedef struct {
57   GLfloat x, y, z;
58   GLfloat ix, iy, iz;
59   GLfloat dx, dy, dz;
60   GLfloat ddx, ddy, ddz;
61   rotator *rot;
62   Bool spinner_p;
63 } floater;
64
65 typedef struct {
66   GLXContext *glx_context;
67   trackball_state *trackball;
68   Bool button_down_p;
69
70   GLuint *dlists;
71   GLuint texture;
72
73   int nfloaters;
74   floater *floaters;
75
76 } cow_configuration;
77
78 static cow_configuration *bps = NULL;
79
80 static GLfloat speed;
81 static const char *do_texture;
82
83 static XrmOptionDescRec opts[] = {
84   { "-speed",      ".speed",     XrmoptionSepArg, 0 },
85   {"-texture",     ".texture",   XrmoptionSepArg, 0 },
86   {"+texture",     ".texture",   XrmoptionNoArg, "(none)" },
87 };
88
89 static argtype vars[] = {
90   {&speed,      "speed",      "Speed",   DEF_SPEED,     t_Float},
91   {&do_texture, "texture",    "Texture", DEF_TEXTURE,   t_String},
92 };
93
94 ENTRYPOINT ModeSpecOpt cow_opts = {countof(opts), opts, countof(vars), vars, NULL};
95
96
97 #define BOTTOM 28.0
98
99 static void
100 reset_floater (ModeInfo *mi, floater *f)
101 {
102   cow_configuration *bp = &bps[MI_SCREEN(mi)];
103
104   f->y = -BOTTOM;
105   f->x = f->ix;
106   f->z = f->iz;
107
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. */
111
112   f->dy = 5.0;
113   f->dx = 0;
114   f->dz = 0;
115
116   /* -0.18 max  -0.3 top -0.4 middle  -0.6 bottom */
117   f->ddy = speed * (-0.6 + BELLRAND(0.45));
118   f->ddx = 0;
119   f->ddz = 0;
120
121   f->spinner_p = !(random() % (12 * bp->nfloaters));
122
123   if (! (random() % (30 * bp->nfloaters)))
124     {
125       f->dx = BELLRAND(1.8) * RANDSIGN();
126       f->dz = BELLRAND(1.8) * RANDSIGN();
127     }
128 }
129
130
131 static void
132 tick_floater (ModeInfo *mi, floater *f)
133 {
134   cow_configuration *bp = &bps[MI_SCREEN(mi)];
135
136   if (bp->button_down_p) return;
137
138   f->dx += f->ddx;
139   f->dy += f->ddy;
140   f->dz += f->ddz;
141
142   f->x += f->dx * speed;
143   f->y += f->dy * speed;
144   f->z += f->dz * speed;
145
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);
150 }
151
152
153 /* Window management, etc
154  */
155 ENTRYPOINT void
156 reshape_cow (ModeInfo *mi, int width, int height)
157 {
158   GLfloat h = (GLfloat) height / (GLfloat) width;
159
160   glViewport (0, 0, (GLint) width, (GLint) height);
161
162   glMatrixMode(GL_PROJECTION);
163   glLoadIdentity();
164   gluPerspective (30.0, 1/h, 1.0, 100);
165
166   glMatrixMode(GL_MODELVIEW);
167   glLoadIdentity();
168   gluLookAt( 0.0, 0.0, 30.0,
169              0.0, 0.0, 0.0,
170              0.0, 1.0, 0.0);
171
172   glClear(GL_COLOR_BUFFER_BIT);
173 }
174
175
176 ENTRYPOINT Bool
177 cow_handle_event (ModeInfo *mi, XEvent *event)
178 {
179   cow_configuration *bp = &bps[MI_SCREEN(mi)];
180
181   if (gltrackball_event_handler (event, bp->trackball,
182                                  MI_WIDTH (mi), MI_HEIGHT (mi),
183                                  &bp->button_down_p))
184     return True;
185
186   return False;
187 }
188
189
190 /* Textures
191  */
192
193 static Bool
194 load_texture (ModeInfo *mi, const char *filename)
195 {
196   Display *dpy = mi->dpy;
197   Visual *visual = mi->xgwa.visual;
198   Colormap cmap = mi->xgwa.colormap;
199   char buf[1024];
200   XImage *image;
201
202   if (MI_IS_WIREFRAME(mi))
203     return False;
204
205   if (!filename ||
206       !*filename ||
207       !strcasecmp (filename, "(none)"))
208     {
209       glDisable (GL_TEXTURE_2D);
210       return False;
211     }
212
213   image = xpm_file_to_ximage (dpy, visual, cmap, filename);
214   if (!image) return False;
215
216   clear_gl_error();
217   glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA,
218                 image->width, image->height, 0,
219                 GL_RGBA,
220                 /* GL_UNSIGNED_BYTE, */
221                 GL_UNSIGNED_INT_8_8_8_8_REV,
222                 image->data);
223   sprintf (buf, "texture: %.100s (%dx%d)",
224            filename, image->width, image->height);
225   check_gl_error(buf);
226
227   glPixelStorei (GL_UNPACK_ALIGNMENT, 4);
228   glPixelStorei (GL_UNPACK_ROW_LENGTH, image->width);
229
230   return True;
231 }
232
233
234 ENTRYPOINT void
235 init_cow (ModeInfo *mi)
236 {
237   cow_configuration *bp;
238   int wire = MI_IS_WIREFRAME(mi);
239   int i;
240   Bool tex_p = False;
241
242   MI_INIT (mi, bps, NULL);
243
244   bp = &bps[MI_SCREEN(mi)];
245
246   bp->glx_context = init_GL(mi);
247
248   reshape_cow (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
249
250   glShadeModel(GL_SMOOTH);
251
252   glEnable(GL_DEPTH_TEST);
253   glEnable(GL_NORMALIZE);
254   glEnable(GL_CULL_FACE);
255
256   if (!wire)
257     {
258       GLfloat pos[4] = {0.4, 0.2, 0.4, 0.0};
259 /*      GLfloat amb[4] = {0.0, 0.0, 0.0, 1.0};*/
260       GLfloat amb[4] = {0.2, 0.2, 0.2, 1.0};
261       GLfloat dif[4] = {1.0, 1.0, 1.0, 1.0};
262       GLfloat spc[4] = {1.0, 1.0, 1.0, 1.0};
263
264       glEnable(GL_LIGHTING);
265       glEnable(GL_LIGHT0);
266       glEnable(GL_DEPTH_TEST);
267       glEnable(GL_CULL_FACE);
268
269       glLightfv(GL_LIGHT0, GL_POSITION, pos);
270       glLightfv(GL_LIGHT0, GL_AMBIENT,  amb);
271       glLightfv(GL_LIGHT0, GL_DIFFUSE,  dif);
272       glLightfv(GL_LIGHT0, GL_SPECULAR, spc);
273     }
274
275   bp->trackball = gltrackball_init (False);
276
277   bp->dlists = (GLuint *) calloc (countof(all_objs)+1, sizeof(GLuint));
278   for (i = 0; i < countof(all_objs); i++)
279     bp->dlists[i] = glGenLists (1);
280
281   tex_p = load_texture (mi, do_texture);
282   if (tex_p)
283     glBindTexture (GL_TEXTURE_2D, bp->texture);
284
285   for (i = 0; i < countof(all_objs); i++)
286     {
287       GLfloat black[4] = {0, 0, 0, 1};
288       const struct gllist *gll = *all_objs[i];
289
290       glNewList (bp->dlists[i], GL_COMPILE);
291
292       glDisable (GL_TEXTURE_2D);
293
294       if (i == HIDE)
295         {
296           GLfloat color[4] = {0.63, 0.43, 0.36, 1.00};
297           if (tex_p)
298             {
299               /* if we have a texture, make the base color be white. */
300               color[0] = color[1] = color[2] = 1.0;
301
302               glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
303               glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
304               glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
305               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
306               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
307               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
308               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
309               glEnable(GL_TEXTURE_GEN_S);
310               glEnable(GL_TEXTURE_GEN_T);
311               glEnable(GL_TEXTURE_2D);
312
313               /* approximately line it up with ../images/earth.xpm */
314               glMatrixMode (GL_TEXTURE);
315               glLoadIdentity();
316               glTranslatef (0.45, 0.58, 0);
317               glScalef (0.08, 0.16, 1);
318               glRotatef (-5, 0, 0, 1);
319               glMatrixMode (GL_MODELVIEW);
320             }
321           glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
322           glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR,            black);
323           glMaterialf  (GL_FRONT_AND_BACK, GL_SHININESS,           128); 
324        }
325       else if (i == TAIL)
326         {
327           GLfloat color[4] = {0.63, 0.43, 0.36, 1.00};
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);
331         }
332       else if (i == UDDER)
333         {
334           GLfloat color[4] = {1.00, 0.53, 0.53, 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);
338         }
339       else if (i == HOOFS || i == HORNS)
340         {
341           GLfloat color[4] = {0.20, 0.20, 0.20, 1.00};
342           GLfloat spec[4]  = {0.30, 0.30, 0.30, 1.00};
343           GLfloat shiny    = 8.0;
344           glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
345           glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR,            spec);
346           glMaterialf  (GL_FRONT_AND_BACK, GL_SHININESS,           shiny);
347         }
348       else if (i == FACE)
349         {
350           GLfloat color[4] = {0.10, 0.10, 0.10, 1.00};
351           GLfloat spec[4]  = {0.10, 0.10, 0.10, 1.00};
352           GLfloat shiny    = 8.0;
353           glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
354           glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR,            spec);
355           glMaterialf  (GL_FRONT_AND_BACK, GL_SHININESS,           shiny);
356         }
357       else
358         {
359           GLfloat color[4] = {1.00, 1.00, 1.00, 1.00};
360           GLfloat spec[4]  = {1.00, 1.00, 1.00, 1.00};
361           GLfloat shiny    = 128.0;
362           glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
363           glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR,            spec);
364           glMaterialf  (GL_FRONT_AND_BACK, GL_SHININESS,           shiny);
365         }
366
367       renderList (gll, wire);
368
369       glEndList ();
370     }
371
372   bp->nfloaters = MI_COUNT (mi);
373   bp->floaters = (floater *) calloc (bp->nfloaters, sizeof (floater));
374
375   for (i = 0; i < bp->nfloaters; i++)
376     {
377       floater *f = &bp->floaters[i];
378       f->rot = make_rotator (10.0, 0, 0,
379                              4, 0.05 * speed,
380                              True);
381       if (bp->nfloaters == 2)
382         {
383           f->x = (i ? 6 : -6);
384         }
385       else if (i != 0)
386         {
387           double th = (i - 1) * M_PI*2 / (bp->nfloaters-1);
388           double r = 10;
389           f->x = r * cos(th);
390           f->z = r * sin(th);
391         }
392
393       f->ix = f->x;
394       f->iy = f->y;
395       f->iz = f->z;
396       reset_floater (mi, f);
397     }
398 }
399
400
401 static void
402 draw_floater (ModeInfo *mi, floater *f)
403 {
404   cow_configuration *bp = &bps[MI_SCREEN(mi)];
405   GLfloat n;
406   double x, y, z;
407
408   get_position (f->rot, &x, &y, &z, !bp->button_down_p);
409
410   glPushMatrix();
411   glTranslatef (f->x, f->y, f->z);
412
413   gltrackball_rotate (bp->trackball);
414
415   glRotatef (y * 360, 0.0, 1.0, 0.0);
416   if (f->spinner_p)
417     {
418       glRotatef (x * 360, 1.0, 0.0, 0.0);
419       glRotatef (z * 360, 0.0, 0.0, 1.0);
420     }
421
422   n = 1.5;
423   if      (bp->nfloaters > 99) n *= 0.05;
424   else if (bp->nfloaters > 25) n *= 0.18;
425   else if (bp->nfloaters > 9)  n *= 0.3;
426   else if (bp->nfloaters > 1)  n *= 0.7;
427   glScalef(n, n, n);
428
429   glCallList (bp->dlists[FACE]);
430   mi->polygon_count += (*all_objs[FACE])->points / 3;
431
432   glCallList (bp->dlists[HIDE]);
433   mi->polygon_count += (*all_objs[HIDE])->points / 3;
434
435   glCallList (bp->dlists[HOOFS]);
436   mi->polygon_count += (*all_objs[HOOFS])->points / 3;
437
438   glCallList (bp->dlists[HORNS]);
439   mi->polygon_count += (*all_objs[HORNS])->points / 3;
440
441   glCallList (bp->dlists[TAIL]);
442   mi->polygon_count += (*all_objs[TAIL])->points / 3;
443
444   glCallList (bp->dlists[UDDER]);
445   mi->polygon_count += (*all_objs[UDDER])->points / 3;
446
447   glPopMatrix();
448 }
449
450
451
452 ENTRYPOINT void
453 draw_cow (ModeInfo *mi)
454 {
455   cow_configuration *bp = &bps[MI_SCREEN(mi)];
456   Display *dpy = MI_DISPLAY(mi);
457   Window window = MI_WINDOW(mi);
458   int i;
459
460   if (!bp->glx_context)
461     return;
462
463   glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(bp->glx_context));
464
465   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
466
467   glPushMatrix ();
468
469 # ifdef HAVE_MOBILE     /* Keep it the same relative size when rotated. */
470   {
471     GLfloat h = MI_HEIGHT(mi) / (GLfloat) MI_WIDTH(mi);
472     int o = (int) current_device_rotation();
473     if (o != 0 && o != 180 && o != -180)
474       glScalef (1/h, 1/h, 1/h);
475     glRotatef(o, 0, 0, 1);
476   }
477 # endif
478
479   glScalef (0.5, 0.5, 0.5);
480
481   mi->polygon_count = 0;
482
483 # if 0
484   {
485     floater F;
486     F.x = F.y = F.z = 0;
487     F.dx = F.dy = F.dz = 0;
488     F.ddx = F.ddy = F.ddz = 0;
489     F.rot = make_rotator (0, 0, 0, 1, 0, False);
490     glScalef(2,2,2);
491     draw_floater (mi, &F);
492   }
493 # else
494   for (i = 0; i < bp->nfloaters; i++)
495     {
496       /* "Don't kid yourself, Jimmy.  If a cow ever got the chance,
497          he'd eat you and everyone you care about!"
498              -- Troy McClure in "Meat and You: Partners in Freedom"
499        */
500       floater *f = &bp->floaters[i];
501       draw_floater (mi, f);
502       tick_floater (mi, f);
503     }
504 # endif
505
506   glPopMatrix ();
507
508   if (mi->fps_p) do_fps (mi);
509   glFinish();
510
511   glXSwapBuffers(dpy, window);
512 }
513
514 XSCREENSAVER_MODULE_2 ("BouncingCow", bouncingcow, cow)
515
516 #endif /* USE_GL */