~ubuntu-branches/ubuntu/dapper/xscreensaver/dapper-updates

« back to all changes in this revision

Viewing changes to hacks/glx/dangerball.c

  • Committer: Bazaar Package Importer
  • Author(s): Ralf Hildebrandt
  • Date: 2005-04-09 00:06:43 UTC
  • mfrom: (1.1.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20050409000643-z0abtifbt9s20pcc
Tags: 4.21-3
Patch by Joachim Breitner to check more frequently if DPMS kicked in (closes: #303374, #286664).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* dangerball, Copyright (c) 2001 Jamie Zawinski <jwz@jwz.org>
 
1
/* dangerball, Copyright (c) 2001-2004 Jamie Zawinski <jwz@jwz.org>
2
2
 *
3
3
 * Permission to use, copy, modify, distribute, and sell this software and its
4
4
 * documentation for any purpose is hereby granted without fee, provided that
17
17
#define HACK_INIT       init_ball
18
18
#define HACK_DRAW       draw_ball
19
19
#define HACK_RESHAPE    reshape_ball
 
20
#define HACK_HANDLE_EVENT ball_handle_event
 
21
#define EVENT_MASK      PointerMotionMask
20
22
#define sws_opts        xlockmore_opts
21
23
 
22
24
#define DEF_SPIN        "True"
27
29
                        "*count:        30          \n" \
28
30
                        "*showFPS:      False       \n" \
29
31
                        "*wireframe:    False       \n" \
30
 
                        "*speed:      " DEF_SPEED " \n" \
31
 
                        "*spin:       " DEF_SPIN   "\n" \
32
 
                        "*wander:     " DEF_WANDER "\n" \
33
 
 
34
32
 
35
33
#define SPIKE_FACES   12  /* how densely to render spikes */
36
34
#define SMOOTH_SPIKES True
45
43
#include "colors.h"
46
44
#include "sphere.h"
47
45
#include "tube.h"
 
46
#include "rotator.h"
 
47
#include "gltrackball.h"
48
48
#include <ctype.h>
49
49
 
50
50
#ifdef USE_GL /* whole file */
53
53
 
54
54
typedef struct {
55
55
  GLXContext *glx_context;
56
 
 
57
 
  GLfloat rotx, roty, rotz;        /* current object rotation */
58
 
  GLfloat dx, dy, dz;              /* current rotational velocity */
59
 
  GLfloat ddx, ddy, ddz;           /* current rotational acceleration */
60
 
  GLfloat d_max;                   /* max velocity */
 
56
  rotator *rot;
 
57
  trackball_state *trackball;
 
58
  Bool button_down_p;
61
59
 
62
60
  GLuint ball_list;
63
61
  GLuint spike_list;
74
72
 
75
73
static ball_configuration *bps = NULL;
76
74
 
77
 
static char *do_spin;
 
75
static Bool do_spin;
78
76
static GLfloat speed;
79
77
static Bool do_wander;
80
78
 
87
85
};
88
86
 
89
87
static argtype vars[] = {
90
 
  {(caddr_t *) &do_spin,   "spin",   "Spin",   DEF_SPIN,   t_Bool},
91
 
  {(caddr_t *) &do_wander, "wander", "Wander", DEF_WANDER, t_Bool},
92
 
  {(caddr_t *) &speed,     "speed",  "Speed",  DEF_SPEED,  t_Float},
 
88
  {&do_spin,   "spin",   "Spin",   DEF_SPIN,   t_Bool},
 
89
  {&do_wander, "wander", "Wander", DEF_WANDER, t_Bool},
 
90
  {&speed,     "speed",  "Speed",  DEF_SPEED,  t_Float},
93
91
};
94
92
 
95
93
ModeSpecOpt sws_opts = {countof(opts), opts, countof(vars), vars, NULL};
106
104
 
107
105
  glMatrixMode(GL_PROJECTION);
108
106
  glLoadIdentity();
 
107
  gluPerspective (30.0, 1/h, 1.0, 100.0);
109
108
 
110
 
  gluPerspective( 30.0, 1/h, 1.0, 100.0 );
111
 
  gluLookAt( 0.0, 0.0, 15.0,
 
109
  glMatrixMode(GL_MODELVIEW);
 
110
  glLoadIdentity();
 
111
  gluLookAt( 0.0, 0.0, 30.0,
112
112
             0.0, 0.0, 0.0,
113
113
             0.0, 1.0, 0.0);
114
 
  glMatrixMode(GL_MODELVIEW);
115
 
  glLoadIdentity();
116
 
  glTranslatef(0.0, 0.0, -15.0);
117
114
 
118
115
  glClear(GL_COLOR_BUFFER_BIT);
119
116
}
120
117
 
121
118
 
122
119
static void
123
 
gl_init (ModeInfo *mi)
124
 
{
125
 
/*  ball_configuration *bp = &bps[MI_SCREEN(mi)]; */
126
 
  int wire = MI_IS_WIREFRAME(mi);
127
 
 
128
 
  static GLfloat pos[4] = {5.0, 5.0, 10.0, 1.0};
129
 
 
130
 
  if (!wire)
131
 
    {
132
 
      glLightfv(GL_LIGHT0, GL_POSITION, pos);
133
 
      glEnable(GL_CULL_FACE);
134
 
      glEnable(GL_LIGHTING);
135
 
      glEnable(GL_LIGHT0);
136
 
      glEnable(GL_DEPTH_TEST);
137
 
    }
138
 
}
139
 
 
140
 
 
141
 
/* lifted from lament.c */
142
 
#define RAND(n) ((long) ((random() & 0x7fffffff) % ((long) (n))))
143
 
#define RANDSIGN() ((random() & 1) ? 1 : -1)
144
 
 
145
 
static void
146
 
rotate(GLfloat *pos, GLfloat *v, GLfloat *dv, GLfloat max_v)
147
 
{
148
 
  double ppos = *pos;
149
 
 
150
 
  /* tick position */
151
 
  if (ppos < 0)
152
 
    ppos = -(ppos + *v);
153
 
  else
154
 
    ppos += *v;
155
 
 
156
 
  if (ppos > 1.0)
157
 
    ppos -= 1.0;
158
 
  else if (ppos < 0)
159
 
    ppos += 1.0;
160
 
 
161
 
  if (ppos < 0) abort();
162
 
  if (ppos > 1.0) abort();
163
 
  *pos = (*pos > 0 ? ppos : -ppos);
164
 
 
165
 
  /* accelerate */
166
 
  *v += *dv;
167
 
 
168
 
  /* clamp velocity */
169
 
  if (*v > max_v || *v < -max_v)
170
 
    {
171
 
      *dv = -*dv;
172
 
    }
173
 
  /* If it stops, start it going in the other direction. */
174
 
  else if (*v < 0)
175
 
    {
176
 
      if (random() % 4)
177
 
        {
178
 
          *v = 0;
179
 
 
180
 
          /* keep going in the same direction */
181
 
          if (random() % 2)
182
 
            *dv = 0;
183
 
          else if (*dv < 0)
184
 
            *dv = -*dv;
185
 
        }
186
 
      else
187
 
        {
188
 
          /* reverse gears */
189
 
          *v = -*v;
190
 
          *dv = -*dv;
191
 
          *pos = -*pos;
192
 
        }
193
 
    }
194
 
 
195
 
  /* Alter direction of rotational acceleration randomly. */
196
 
  if (! (random() % 120))
197
 
    *dv = -*dv;
198
 
 
199
 
  /* Change acceleration very occasionally. */
200
 
  if (! (random() % 200))
201
 
    {
202
 
      if (*dv == 0)
203
 
        *dv = 0.00001;
204
 
      else if (random() & 1)
205
 
        *dv *= 1.2;
206
 
      else
207
 
        *dv *= 0.8;
208
 
    }
209
 
}
210
 
 
211
 
 
212
 
static void
213
120
randomize_spikes (ModeInfo *mi)
214
121
{
215
122
  ball_configuration *bp = &bps[MI_SCREEN(mi)];
253
160
      glScalef (diam, pos, diam);
254
161
      glCallList (bp->spike_list);
255
162
      glPopMatrix();
 
163
 
 
164
      mi->polygon_count += (SPIKE_FACES + 1);
256
165
    }
257
166
}
258
167
 
277
186
}
278
187
 
279
188
 
 
189
Bool
 
190
ball_handle_event (ModeInfo *mi, XEvent *event)
 
191
{
 
192
  ball_configuration *bp = &bps[MI_SCREEN(mi)];
 
193
 
 
194
  if (event->xany.type == ButtonPress &&
 
195
      event->xbutton.button == Button1)
 
196
    {
 
197
      bp->button_down_p = True;
 
198
      gltrackball_start (bp->trackball,
 
199
                         event->xbutton.x, event->xbutton.y,
 
200
                         MI_WIDTH (mi), MI_HEIGHT (mi));
 
201
      return True;
 
202
    }
 
203
  else if (event->xany.type == ButtonRelease &&
 
204
           event->xbutton.button == Button1)
 
205
    {
 
206
      bp->button_down_p = False;
 
207
      return True;
 
208
    }
 
209
  else if (event->xany.type == ButtonPress &&
 
210
           (event->xbutton.button == Button4 ||
 
211
            event->xbutton.button == Button5))
 
212
    {
 
213
      gltrackball_mousewheel (bp->trackball, event->xbutton.button, 10,
 
214
                              !!event->xbutton.state);
 
215
      return True;
 
216
    }
 
217
  else if (event->xany.type == MotionNotify &&
 
218
           bp->button_down_p)
 
219
    {
 
220
      gltrackball_track (bp->trackball,
 
221
                         event->xmotion.x, event->xmotion.y,
 
222
                         MI_WIDTH (mi), MI_HEIGHT (mi));
 
223
      return True;
 
224
    }
 
225
 
 
226
  return False;
 
227
}
 
228
 
 
229
 
280
230
void 
281
231
init_ball (ModeInfo *mi)
282
232
{
296
246
 
297
247
  bp = &bps[MI_SCREEN(mi)];
298
248
 
299
 
  if ((bp->glx_context = init_GL(mi)) != NULL) {
300
 
    gl_init(mi);
301
 
    reshape_ball (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
 
249
  bp->glx_context = init_GL(mi);
 
250
 
 
251
  reshape_ball (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
 
252
 
 
253
  if (!wire)
 
254
    {
 
255
      GLfloat pos[4] = {1.0, 1.0, 1.0, 0.0};
 
256
      GLfloat amb[4] = {0.0, 0.0, 0.0, 1.0};
 
257
      GLfloat dif[4] = {1.0, 1.0, 1.0, 1.0};
 
258
      GLfloat spc[4] = {0.0, 1.0, 1.0, 1.0};
 
259
 
 
260
      glEnable(GL_LIGHTING);
 
261
      glEnable(GL_LIGHT0);
 
262
      glEnable(GL_DEPTH_TEST);
 
263
      glEnable(GL_CULL_FACE);
 
264
 
 
265
      glLightfv(GL_LIGHT0, GL_POSITION, pos);
 
266
      glLightfv(GL_LIGHT0, GL_AMBIENT,  amb);
 
267
      glLightfv(GL_LIGHT0, GL_DIFFUSE,  dif);
 
268
      glLightfv(GL_LIGHT0, GL_SPECULAR, spc);
 
269
    }
 
270
 
 
271
  {
 
272
    double spin_speed   = 10.0;
 
273
    double wander_speed = 0.12;
 
274
    double spin_accel   = 2.0;
 
275
 
 
276
    bp->rot = make_rotator (do_spin ? spin_speed : 0,
 
277
                            do_spin ? spin_speed : 0,
 
278
                            do_spin ? spin_speed : 0,
 
279
                            spin_accel,
 
280
                            do_wander ? wander_speed : 0,
 
281
                            True);
 
282
    bp->trackball = gltrackball_init ();
302
283
  }
303
284
 
304
 
  bp->rotx = frand(1.0) * RANDSIGN();
305
 
  bp->roty = frand(1.0) * RANDSIGN();
306
 
  bp->rotz = frand(1.0) * RANDSIGN();
307
 
 
308
 
  /* bell curve from 0-6 degrees, avg 3 */
309
 
  bp->dx = (frand(2) + frand(2) + frand(2)) / (360/2);
310
 
  bp->dy = (frand(2) + frand(2) + frand(2)) / (360/2);
311
 
  bp->dz = (frand(2) + frand(2) + frand(2)) / (360/2);
312
 
 
313
 
  bp->d_max = bp->dx * 2;
314
 
 
315
 
  bp->ddx = 0.00006 + frand(0.00003);
316
 
  bp->ddy = 0.00006 + frand(0.00003);
317
 
  bp->ddz = 0.00006 + frand(0.00003);
318
 
 
319
285
  bp->ncolors = 128;
320
286
  bp->colors = (XColor *) calloc(bp->ncolors, sizeof(XColor));
321
287
  make_smooth_colormap (0, 0, 0,
334
300
  glNewList (bp->spike_list, GL_COMPILE);
335
301
  cone (0, 0, 0,
336
302
        0, 1, 0,
337
 
        1, 0, SPIKE_FACES, SMOOTH_SPIKES, wire);
 
303
        1, 0, SPIKE_FACES, SMOOTH_SPIKES, False, wire);
338
304
  glEndList ();
339
305
 
340
306
  randomize_spikes (mi);
349
315
  Window window = MI_WINDOW(mi);
350
316
  int c2;
351
317
 
352
 
  static GLfloat color1[4] = {0.0, 0.0, 0.0, 1.0};
353
 
  static GLfloat color2[4] = {0.0, 0.0, 0.0, 1.0};
 
318
  static GLfloat bcolor[4] = {0.0, 0.0, 0.0, 1.0};
 
319
  static GLfloat scolor[4] = {0.0, 0.0, 0.0, 1.0};
 
320
  static GLfloat bspec[4]  = {1.0, 1.0, 1.0, 1.0};
 
321
  static GLfloat sspec[4]  = {0.0, 0.0, 0.0, 1.0};
 
322
  static GLfloat bshiny    = 128.0;
 
323
  static GLfloat sshiny    = 0.0;
354
324
 
355
325
  if (!bp->glx_context)
356
326
    return;
368
338
  glScalef(1.1, 1.1, 1.1);
369
339
 
370
340
  {
371
 
    GLfloat x, y, z;
372
 
 
373
 
    if (do_wander)
374
 
      {
375
 
        static int frame = 0;
376
 
 
377
 
#       define SINOID(SCALE,SIZE) \
378
 
        ((((1 + sin((frame * (SCALE)) / 2 * M_PI)) / 2.0) * (SIZE)) - (SIZE)/2)
379
 
 
380
 
        x = SINOID(0.051, 8.0);
381
 
        y = SINOID(0.037, 8.0);
382
 
        z = SINOID(0.131, 13.0);
383
 
        frame++;
384
 
        glTranslatef(x, y, z);
385
 
      }
386
 
 
387
 
    if (do_spin)
388
 
      {
389
 
        x = bp->rotx;
390
 
        y = bp->roty;
391
 
        z = bp->rotz;
392
 
        if (x < 0) x = 1 - (x + 1);
393
 
        if (y < 0) y = 1 - (y + 1);
394
 
        if (z < 0) z = 1 - (z + 1);
395
 
 
396
 
        glRotatef(x * 360, 1.0, 0.0, 0.0);
397
 
        glRotatef(y * 360, 0.0, 1.0, 0.0);
398
 
        glRotatef(z * 360, 0.0, 0.0, 1.0);
399
 
 
400
 
        rotate(&bp->rotx, &bp->dx, &bp->ddx, bp->d_max);
401
 
        rotate(&bp->roty, &bp->dy, &bp->ddy, bp->d_max);
402
 
        rotate(&bp->rotz, &bp->dz, &bp->ddz, bp->d_max);
403
 
      }
 
341
    double x, y, z;
 
342
    get_position (bp->rot, &x, &y, &z, !bp->button_down_p);
 
343
    glTranslatef((x - 0.5) * 8,
 
344
                 (y - 0.5) * 8,
 
345
                 (z - 0.5) * 15);
 
346
 
 
347
    gltrackball_rotate (bp->trackball);
 
348
 
 
349
    get_rotation (bp->rot, &x, &y, &z, !bp->button_down_p);
 
350
    glRotatef (x * 360, 1.0, 0.0, 0.0);
 
351
    glRotatef (y * 360, 0.0, 1.0, 0.0);
 
352
    glRotatef (z * 360, 0.0, 0.0, 1.0);
404
353
  }
405
354
 
406
 
  color1[0] = bp->colors[bp->ccolor].red   / 65536.0;
407
 
  color1[1] = bp->colors[bp->ccolor].green / 65536.0;
408
 
  color1[2] = bp->colors[bp->ccolor].blue  / 65536.0;
 
355
  bcolor[0] = bp->colors[bp->ccolor].red   / 65536.0;
 
356
  bcolor[1] = bp->colors[bp->ccolor].green / 65536.0;
 
357
  bcolor[2] = bp->colors[bp->ccolor].blue  / 65536.0;
409
358
 
410
359
  c2 = (bp->ccolor + bp->color_shift) % bp->ncolors;
411
 
  color2[0] = bp->colors[c2].red   / 65536.0;
412
 
  color2[1] = bp->colors[c2].green / 65536.0;
413
 
  color2[2] = bp->colors[c2].blue  / 65536.0;
 
360
  scolor[0] = bp->colors[c2].red   / 65536.0;
 
361
  scolor[1] = bp->colors[c2].green / 65536.0;
 
362
  scolor[2] = bp->colors[c2].blue  / 65536.0;
414
363
 
415
364
  bp->ccolor++;
416
365
  if (bp->ccolor >= bp->ncolors) bp->ccolor = 0;
417
366
 
 
367
  mi->polygon_count = 0;
 
368
 
418
369
  glScalef (2.0, 2.0, 2.0);
419
370
 
420
371
  move_spikes (mi);
421
372
 
422
 
  glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color1);
 
373
  glMaterialfv (GL_FRONT, GL_SPECULAR,            bspec);
 
374
  glMateriali  (GL_FRONT, GL_SHININESS,           bshiny);
 
375
  glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, bcolor);
423
376
  glCallList (bp->ball_list);
 
377
  mi->polygon_count += (SPHERE_SLICES * SPHERE_STACKS);
424
378
 
425
 
  glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color2);
 
379
  glMaterialfv (GL_FRONT, GL_SPECULAR,            sspec);
 
380
  glMaterialf  (GL_FRONT, GL_SHININESS,           sshiny);
 
381
  glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, scolor);
426
382
  draw_spikes (mi);
427
383
  glPopMatrix ();
428
384