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

« back to all changes in this revision

Viewing changes to hacks/glx/gears.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
1
/* -*- Mode: C; tab-width: 4 -*- */
2
2
/* gears --- 3D gear wheels */
3
3
 
4
 
#if !defined( lint ) && !defined( SABER )
 
4
#if 0
5
5
static const char sccsid[] = "@(#)gears.c       4.07 97/11/24 xlockmore";
6
 
 
7
6
#endif
8
7
 
9
8
/*-
35
34
 * been fixed in MesaGL 2.2 and later releases.
36
35
 */
37
36
 
38
 
/*-
39
 
 * due to a Bug/feature in VMS X11/Intrinsic.h has to be placed before xlock.
40
 
 * otherwise caddr_t is not defined correctly
41
 
 */
42
 
 
43
 
#include <X11/Intrinsic.h>
44
 
 
45
37
#ifdef STANDALONE
46
38
# define PROGCLASS                                      "Gears"
47
39
# define HACK_INIT                                      init_gears
48
40
# define HACK_DRAW                                      draw_gears
49
41
# define HACK_RESHAPE                           reshape_gears
 
42
# define HACK_HANDLE_EVENT                      gears_handle_event
 
43
# define EVENT_MASK                                     PointerMotionMask
50
44
# define gears_opts                                     xlockmore_opts
51
45
# define DEFAULTS       "*count:                1       \n"                     \
52
46
                                        "*cycles:               2       \n"                     \
53
47
                                        "*delay:                20000   \n"                     \
54
 
                                        "*planetary:    False   \n"                     \
55
48
                                        "*showFPS:      False   \n"                     \
56
49
                                        "*wireframe:    False   \n"
57
50
# include "xlockmore.h"                         /* from the xscreensaver distribution */
61
54
 
62
55
#ifdef USE_GL
63
56
 
 
57
#include "rotator.h"
 
58
#include "gltrackball.h"
 
59
 
64
60
#undef countof
65
61
#define countof(x) (sizeof((x))/sizeof((*x)))
66
62
 
69
65
static int planetary;
70
66
 
71
67
static XrmOptionDescRec opts[] = {
72
 
  {"-planetary", ".gears.planetary", XrmoptionNoArg, (caddr_t) "true" },
73
 
  {"+planetary", ".gears.planetary", XrmoptionNoArg, (caddr_t) "false" },
 
68
  {"-planetary", ".gears.planetary", XrmoptionNoArg, "true" },
 
69
  {"+planetary", ".gears.planetary", XrmoptionNoArg, "false" },
74
70
};
75
71
 
76
72
static argtype vars[] = {
77
 
  {(caddr_t *) &planetary, "planetary", "Planetary", DEF_PLANETARY, t_Bool},
 
73
  {&planetary, "planetary", "Planetary", DEF_PLANETARY, t_Bool},
78
74
};
79
75
 
80
76
ModeSpecOpt gears_opts = {countof(opts), opts, countof(vars), vars, NULL};
98
94
 
99
95
 
100
96
typedef struct {
101
 
 
102
 
  GLfloat rotx, roty, rotz;        /* current object rotation */
103
 
  GLfloat dx, dy, dz;              /* current rotational velocity */
104
 
  GLfloat ddx, ddy, ddz;           /* current rotational acceleration */
105
 
  GLfloat d_max;                           /* max velocity */
106
 
 
107
97
  GLuint      gear1, gear2, gear3;
108
98
  GLuint      gear_inner, gear_outer;
109
99
  GLuint      armature;
110
100
  GLfloat     angle;
111
101
  GLXContext *glx_context;
112
102
  Window      window;
 
103
  rotator    *rot;
 
104
  trackball_state *trackball;
 
105
  Bool            button_down_p;
113
106
} gearsstruct;
114
107
 
115
108
static gearsstruct *gears = NULL;
540
533
 
541
534
        glPushMatrix();
542
535
 
 
536
    gltrackball_rotate (gp->trackball);
 
537
 
543
538
    {
544
 
      GLfloat x = gp->rotx;
545
 
      GLfloat y = gp->roty;
546
 
      GLfloat z = gp->rotz;
547
 
      if (x < 0) x = 1 - (x + 1);
548
 
      if (y < 0) y = 1 - (y + 1);
549
 
      if (z < 0) z = 1 - (z + 1);
550
 
      glRotatef(x * 360, 1.0, 0.0, 0.0);
551
 
      glRotatef(y * 360, 0.0, 1.0, 0.0);
552
 
      glRotatef(z * 360, 0.0, 0.0, 1.0);
 
539
      double x, y, z;
 
540
      get_rotation (gp->rot, &x, &y, &z, !gp->button_down_p);
 
541
      glRotatef (x * 360, 1.0, 0.0, 0.0);
 
542
      glRotatef (y * 360, 0.0, 1.0, 0.0);
 
543
      glRotatef (z * 360, 0.0, 0.0, 1.0);
553
544
    }
554
545
 
555
546
    if (!planetary) {
921
912
}
922
913
 
923
914
 
924
 
/* lifted from lament.c */
925
 
#define RAND(n) ((long) ((random() & 0x7fffffff) % ((long) (n))))
926
 
#define RANDSIGN() ((random() & 1) ? 1 : -1)
927
 
 
928
 
static void
929
 
rotate(GLfloat *pos, GLfloat *v, GLfloat *dv, GLfloat max_v)
 
915
Bool
 
916
gears_handle_event (ModeInfo *mi, XEvent *event)
930
917
{
931
 
  double ppos = *pos;
932
 
 
933
 
  /* tick position */
934
 
  if (ppos < 0)
935
 
    ppos = -(ppos + *v);
936
 
  else
937
 
    ppos += *v;
938
 
 
939
 
  if (ppos > 1.0)
940
 
    ppos -= 1.0;
941
 
  else if (ppos < 0)
942
 
    ppos += 1.0;
943
 
 
944
 
  if (ppos < 0) abort();
945
 
  if (ppos > 1.0) abort();
946
 
  *pos = (*pos > 0 ? ppos : -ppos);
947
 
 
948
 
  /* accelerate */
949
 
  *v += *dv;
950
 
 
951
 
  /* clamp velocity */
952
 
  if (*v > max_v || *v < -max_v)
953
 
    {
954
 
      *dv = -*dv;
955
 
    }
956
 
  /* If it stops, start it going in the other direction. */
957
 
  else if (*v < 0)
958
 
    {
959
 
      if (random() % 4)
960
 
        {
961
 
          *v = 0;
962
 
 
963
 
          /* keep going in the same direction */
964
 
          if (random() % 2)
965
 
            *dv = 0;
966
 
          else if (*dv < 0)
967
 
            *dv = -*dv;
968
 
        }
969
 
      else
970
 
        {
971
 
          /* reverse gears */
972
 
          *v = -*v;
973
 
          *dv = -*dv;
974
 
          *pos = -*pos;
975
 
        }
976
 
    }
977
 
 
978
 
  /* Alter direction of rotational acceleration randomly. */
979
 
  if (! (random() % 120))
980
 
    *dv = -*dv;
981
 
 
982
 
  /* Change acceleration very occasionally. */
983
 
  if (! (random() % 200))
984
 
    {
985
 
      if (*dv == 0)
986
 
        *dv = 0.00001;
987
 
      else if (random() & 1)
988
 
        *dv *= 1.2;
989
 
      else
990
 
        *dv *= 0.8;
991
 
    }
 
918
  gearsstruct *gp = &gears[MI_SCREEN(mi)];
 
919
 
 
920
  if (event->xany.type == ButtonPress &&
 
921
      event->xbutton.button == Button1)
 
922
    {
 
923
      gp->button_down_p = True;
 
924
      gltrackball_start (gp->trackball,
 
925
                         event->xbutton.x, event->xbutton.y,
 
926
                         MI_WIDTH (mi), MI_HEIGHT (mi));
 
927
      return True;
 
928
    }
 
929
  else if (event->xany.type == ButtonRelease &&
 
930
           event->xbutton.button == Button1)
 
931
    {
 
932
      gp->button_down_p = False;
 
933
      return True;
 
934
    }
 
935
  else if (event->xany.type == ButtonPress &&
 
936
           (event->xbutton.button == Button4 ||
 
937
            event->xbutton.button == Button5))
 
938
    {
 
939
      gltrackball_mousewheel (gp->trackball, event->xbutton.button, 10,
 
940
                              !!event->xbutton.state);
 
941
      return True;
 
942
    }
 
943
  else if (event->xany.type == MotionNotify &&
 
944
           gp->button_down_p)
 
945
    {
 
946
      gltrackball_track (gp->trackball,
 
947
                         event->xmotion.x, event->xmotion.y,
 
948
                         MI_WIDTH (mi), MI_HEIGHT (mi));
 
949
      return True;
 
950
    }
 
951
 
 
952
  return False;
992
953
}
993
954
 
994
955
 
1010
971
 
1011
972
        gp->window = MI_WINDOW(mi);
1012
973
 
1013
 
    gp->rotx = frand(1.0) * RANDSIGN();
1014
 
    gp->roty = frand(1.0) * RANDSIGN();
1015
 
    gp->rotz = frand(1.0) * RANDSIGN();
1016
 
 
1017
 
    /* bell curve from 0-1.5 degrees, avg 0.75 */
1018
 
    gp->dx = (frand(1) + frand(1) + frand(1)) / (360*2);
1019
 
    gp->dy = (frand(1) + frand(1) + frand(1)) / (360*2);
1020
 
    gp->dz = (frand(1) + frand(1) + frand(1)) / (360*2);
1021
 
 
1022
 
    gp->d_max = gp->dx * 2;
1023
 
 
1024
 
    gp->ddx = 0.00006 + frand(0.00003);
1025
 
    gp->ddy = 0.00006 + frand(0.00003);
1026
 
    gp->ddz = 0.00006 + frand(0.00003);
 
974
    gp->rot = make_rotator (1, 1, 1, 1, 0, True);
 
975
    gp->trackball = gltrackball_init ();
1027
976
 
1028
977
        if ((gp->glx_context = init_GL(mi)) != NULL) {
1029
978
                reshape_gears(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
1055
1004
        /* let's do something so we don't get bored */
1056
1005
        gp->angle = (int) (gp->angle + angle_incr) % 360;
1057
1006
 
1058
 
    rotate(&gp->rotx, &gp->dx, &gp->ddx, gp->d_max);
1059
 
    rotate(&gp->roty, &gp->dy, &gp->ddy, gp->d_max);
1060
 
    rotate(&gp->rotz, &gp->dz, &gp->ddz, gp->d_max);
1061
 
 
1062
1007
    if (mi->fps_p) do_fps (mi);
1063
1008
        glFinish();
1064
1009
        glXSwapBuffers(display, window);