summaryrefslogtreecommitdiffstats
path: root/hacks/glx/deepstars.c
blob: b477e001e0aa84805037537912f2de0183404e00 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/* xscreensaver, Copyright (c) 2019 Jamie Zawinski <jwz@jwz.org>
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation.  No representations are made about the suitability of this
 * software for any purpose.  It is provided "as is" without express or 
 * implied warranty.
 */

#ifdef STANDALONE
#define DEFAULTS	"*delay:			30000   \n"	\
					"*showFPS:			False   \n" \
					"*suppressRotationAnimation: True\n" \

# define release_deepstars 0
# include "xlockmore.h"				/* from the xscreensaver distribution */
#else  /* !STANDALONE */
# include "xlock.h"					/* from the xlockmore distribution */
#endif /* !STANDALONE */

#ifdef USE_GL /* whole file */

#include "sphere.h"
#include "gltrackball.h"

#define DEF_SPEED "1.0"
#define DEF_SMEAR "1.0"
#define SMEAR_BASE 400
#define SPEED_BASE 0.02

#undef countof
#define countof(x) (sizeof((x))/sizeof((*x)))

#undef BELLRAND
#define BELLRAND(n) ((frand((n)) + frand((n)) + frand((n))) / 3)

static GLfloat speed_arg, smear_arg;

static XrmOptionDescRec opts[] = {
  {"-speed",  ".speed",  XrmoptionSepArg, 0 },
  {"-smear",  ".smear",  XrmoptionSepArg, 0 },
};

static argtype vars[] = {
  {&speed_arg, "speed" ,"Speed", DEF_SPEED, t_Float},
  {&smear_arg, "smear" ,"Smear", DEF_SMEAR, t_Float},
};

ENTRYPOINT ModeSpecOpt deepstars_opts = {countof(opts), opts, countof(vars), vars, NULL};

#ifdef USE_MODULES
ModStruct   deepstars_description =
{"deepstars", "init_deepstars", "draw_deepstars", NULL,
 "draw_deepstars", "init_deepstars", "free_deepstars", &deepstars_opts,
 1000, 1, 2, 1, 4, 1.0, "",
 "Animates texture mapped sphere (deepstars)", 0, NULL};
#endif

typedef struct {
  GLfloat *colors;
  GLuint starlist, groundlist;
  int ncolors, starcount, groundcount;
  GLXContext *glx_context;
  GLfloat z, latitude, facing;
  int smear, dsmear;
  trackball_state *trackball;
  Bool button_down_p;
} starstruct;

static starstruct *deepstarss = NULL;


ENTRYPOINT void
reshape_deepstars (ModeInfo *mi, int width, int height)
{
  starstruct *gp = &deepstarss[MI_SCREEN(mi)];
  GLfloat h = (GLfloat) height / (GLfloat) width;

  glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *gp->glx_context);

  glViewport(0, 0, (GLint) width, (GLint) height);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glFrustum(-1.0, 1.0, -h, h, 5.0, 200.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glTranslatef(0.0, 0.0, -40);

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}


ENTRYPOINT Bool
deepstars_handle_event (ModeInfo *mi, XEvent *event)
{
  starstruct *gp = &deepstarss[MI_SCREEN(mi)];

  /* Neutralize any horizontal motion, and flip Y */
  GLfloat rot = current_device_rotation();
  Bool rotp = ((rot >  45 && rot <  135) ||
               (rot < -45 && rot > -135));

  if (event->xany.type == ButtonPress ||
      event->xany.type == ButtonRelease)
    {
      if (rotp)
        {
          event->xbutton.y = MI_HEIGHT(mi) / 2;
          event->xbutton.x = MI_WIDTH(mi) - event->xbutton.x;
        }
      else
        {
          event->xbutton.x = MI_WIDTH(mi) / 2;
          event->xbutton.y = MI_HEIGHT(mi) - event->xbutton.y;
        }
    }
  else if (event->xany.type == MotionNotify)
    {
      if (rotp)
        {
          event->xmotion.y = MI_HEIGHT(mi) / 2;
          event->xmotion.x = MI_WIDTH(mi) - event->xmotion.x;
        }
      else
        {
          event->xmotion.x = MI_WIDTH(mi) / 2;
          event->xmotion.y = MI_HEIGHT(mi) - event->xmotion.y;
        }
    }

  if (gltrackball_event_handler (event, gp->trackball,
                                 MI_WIDTH (mi), MI_HEIGHT (mi),
                                 &gp->button_down_p))
    return True;
  else if (screenhack_event_helper (MI_DISPLAY(mi), MI_WINDOW(mi), event))
    {
      if (gp->smear <= 1)
        gp->dsmear = 1;
      else
        gp->dsmear = gp->smear = 0;
      return True;
    }

  return False;
}


ENTRYPOINT void
init_deepstars (ModeInfo * mi)
{
  starstruct *gp;
  int screen = MI_SCREEN(mi);

  int i, j, k;
  int width  = MI_WIDTH(mi);
  int height = MI_HEIGHT(mi);
  int size = (width > height ? width : height);
  int nstars = size * size / 80;
  int max_size = 3;
  GLfloat inc = 0.5;
  int sizes = max_size / inc;
  GLfloat scale = 1;

  MI_INIT (mi, deepstarss);
  gp = &deepstarss[screen];

  if ((gp->glx_context = init_GL(mi)) != NULL) {
	reshape_deepstars(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
  }

# ifdef HAVE_MOBILE
  scale *= 3;
  nstars /= 3;
# else /* !HAVE_MOBILE */
  if (MI_WIDTH(mi) > 2560) {  /* Retina displays */
    scale *= 2;
    nstars /= 2;
  }
# endif /* !HAVE_MOBILE */


  gp->trackball = gltrackball_init (True);

  gp->latitude = 10 + frand(70);
  gp->facing = 10 * (frand(1.0)-0.5);

  /* Only need a small number of distinct star colors, and we have one
     display list for each color, so we can modify the alpha. 
   */
  gp->ncolors = 16;
  gp->colors = (GLfloat *) malloc (4 * gp->ncolors * sizeof(*gp->colors));

  for (i = 0; i < gp->ncolors; i++)
    {
      GLfloat d = 0.1;
      GLfloat r = 0.15 + frand(0.3);
      GLfloat g = r + frand(d) - d;
      GLfloat b = r + frand(d) - d;
      gp->colors[i*4+0] = r;
      gp->colors[i*4+1] = g;
      gp->colors[i*4+2] = b;
      gp->colors[i*4+3] = 1;
    }

  gp->starcount = nstars / gp->ncolors;
  gp->starlist = glGenLists(gp->ncolors);
  for (i = 0; i < gp->ncolors; i++)
    {
      glNewList (gp->starlist + i, GL_COMPILE);
      for (j = 1; j <= sizes; j++)
        {
          glPointSize (inc * j * scale);
          glBegin (GL_POINTS);
          for (k = 0; k < gp->starcount / sizes; k++)
            {
              GLfloat x = frand(1)-0.5;
              GLfloat y = frand(1)-0.5;
              GLfloat z = ((random() & 1)
                           ? frand(1)-0.5
                           : (BELLRAND(1)-0.5)/20);   /* milky way */
              GLfloat d = sqrt (x*x + y*y + z*z);
              x /= d;
              y /= d;
              z /= d;
              glVertex3f (x, y, z);
            }
          glEnd();
        }
      glEndList();
    }

  glDisable (GL_BLEND);
  gp->groundlist = glGenLists(1);
  glNewList(gp->groundlist, GL_COMPILE);
  {
    GLfloat inc = 0.5;
    glColor3f (0.02, 0.02, 0.05);
    glBegin (GL_QUAD_STRIP);
    gp->groundcount = 50;
    for (i = 0; i <= gp->groundcount; i++)
      {
        glVertex3f (i / (GLfloat) gp->groundcount, 0, 0);
        glVertex3f (i / (GLfloat) gp->groundcount, inc, 0);
        inc += 0.1 * (frand(1.0) - 0.5);
      }
    glEnd();
  }
  glEndList();
}


ENTRYPOINT void
draw_deepstars (ModeInfo * mi)
{
  starstruct *gp = &deepstarss[MI_SCREEN(mi)];
  Display *dpy = MI_DISPLAY(mi);
  Window window = MI_WINDOW(mi);
  int smear_change = 800;
  int sky_scale = 60;
  int i, j;

  if (!gp->glx_context)
	return;

  glDrawBuffer(GL_BACK);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  glXMakeCurrent (dpy, window, *gp->glx_context);

  mi->polygon_count = 0;

  glEnable (GL_LINE_SMOOTH);
  glEnable (GL_POINT_SMOOTH);
  glDisable (GL_DEPTH_TEST);
  glDisable (GL_CULL_FACE);

  glPushMatrix();

  gltrackball_rotate (gp->trackball);

  /* At the equator, Polaris is on the horizon. In the Arctic, overhead. */
  glRotatef (180 - gp->latitude, 1, 0, 0);
  glRotatef (gp->facing, 0, 1, 0);

  if (gp->dsmear == 0 && !(random() % smear_change))
    gp->dsmear = 1;
  else if (gp->smear == SMEAR_BASE * smear_arg && !(random() % smear_change))
    gp->dsmear = -1;

  if (! gp->button_down_p)
    gp->smear += gp->dsmear;
  if (gp->smear < 1) gp->smear = 1;
  else if (gp->smear > SMEAR_BASE * smear_arg)
    gp->smear = SMEAR_BASE * smear_arg;

  if (!gp->button_down_p)
    gp->z -= SPEED_BASE * speed_arg;

  glEnable (GL_BLEND);
  glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

  for (i = 0; i < gp->smear; i++)
    {
      GLfloat alpha = 1 - (i / (GLfloat) gp->smear);

      glPushMatrix();

      glRotatef (gp->z - (-i * SPEED_BASE * speed_arg), 0, 0, 1);

# if 0
      if (i == 0)
        {
          glBegin(GL_LINES);
          glVertex3f(0,0,0); glVertex3f(0,0,-3);
          glVertex3f(0,-1,0); glVertex3f(0,1,0);
          glVertex3f(-1,0,0); glVertex3f(1,0,0);
          glEnd();

          glPushMatrix();
          glRotatef (90, 1, 0, 0);
          glScalef (sky_scale, sky_scale, sky_scale);
          mi->polygon_count += unit_sphere (12, 24, 1);
          glPopMatrix();
        }
# endif

      glRotatef (50, 1, 0, 0);  /* Tilt milky way */
      glScalef (sky_scale, sky_scale, sky_scale);

      for (j = 0; j < gp->ncolors; j++)
        {
          gp->colors[j*4+3] = alpha;
          glColor4fv (&gp->colors[j*4]);
          glCallList (gp->starlist + j);
          mi->polygon_count += gp->starcount;
        }
      glPopMatrix();
    }

  glPopMatrix();

  glMatrixMode(GL_PROJECTION);
  glPushMatrix();
  {
    glLoadIdentity();

    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    {
      glLoadIdentity();
      glTranslatef (-1, -1, 0);
      glScalef (2, 0.7, 1);
      glCallList (gp->groundlist);
      mi->polygon_count += gp->groundcount;
    }
    glPopMatrix();
  }
  glMatrixMode(GL_PROJECTION);
  glPopMatrix();

  glMatrixMode(GL_MODELVIEW);

  if (mi->fps_p) do_fps (mi);
  glFinish();
  glXSwapBuffers(dpy, window);
}


ENTRYPOINT void
free_deepstars (ModeInfo * mi)
{
  starstruct *gp = &deepstarss[MI_SCREEN(mi)];

  if (!gp->glx_context) return;
  glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *gp->glx_context);
  if (gp->colors) free (gp->colors);
  if (glIsList(gp->groundlist))  glDeleteLists(gp->groundlist, 1);
  if (glIsList(gp->starlist))    glDeleteLists(gp->starlist, gp->ncolors);
  if (gp->trackball) gltrackball_free (gp->trackball);
}


XSCREENSAVER_MODULE ("DeepStars", deepstars)

#endif