~ubuntu-branches/ubuntu/precise/rss-glx/precise

« back to all changes in this revision

Viewing changes to other_src/busyspheres.c

  • Committer: Bazaar Package Importer
  • Author(s): Steve Langasek
  • Date: 2009-06-03 18:41:32 UTC
  • mfrom: (1.1.5 upstream) (2.2.1 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090603184132-znjy66pq9xom7hac
Tags: 0.9.0-2ubuntu1
* Merge from debian unstable, remaining changes:
  - Drop dependency on openal. It is not enabled by default anyway, and
    allows openal to migrate to universe.
  - src/skyrocket.{cpp,xml}: Disable sound by default.
  - Add --disable-sound configure flag to debian/rules, as we don't keep 
    the dependencies on openal nor freeglut (both universe packages).
* Dropped changes, merged upstream:
  - other_src/Makefile.am: fix the upstream build rules to actually use
    the ImageMagick CFLAGS returned by pkg-config.
  - Move the unconditional ImageMagick check up in configure.in so that
    our first PKG_CHECK_MODULES() call isn't hidden behind a shell
    conditional.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2002  <hk@dgmr.nl>
3
 
 * Ported to Linux by Tugrul Galatali <tugrul@galatali.com>
4
 
 *
5
 
 * BusySpheres is free software; you can redistribute it and/or modify
6
 
 * it under the terms of the GNU General Public License version 2 as 
7
 
 * published by the Free Software Foundation.
8
 
 *
9
 
 * BusySpheres is distributed in the hope that it will be useful,
10
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
 * GNU General Public License for more details.
13
 
 *
14
 
 * You should have received a copy of the GNU General Public License
15
 
 * along with this program; if not, write to the Free Software
16
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 
 */
18
 
 
19
 
// BusySpheres screen saver
20
 
 
21
 
#include <math.h>
22
 
#include <stdio.h>
23
 
#include <stdlib.h>
24
 
#include <GL/gl.h>
25
 
#include <GL/glu.h>
26
 
 
27
 
#include "driver.h"
28
 
#include "rsDefines.h"
29
 
#include "rsRand.h"
30
 
 
31
 
char *hack_name = (char *)"BusySpheres";
32
 
 
33
 
#define NRPOINTS 100
34
 
 
35
 
#define EFFECT_RANDOM           0
36
 
#define EFFECT_CRESCENT         1
37
 
#define EFFECT_DOT              2
38
 
#define EFFECT_RING             3
39
 
#define EFFECT_LONGITUDE        4
40
 
 
41
 
int OldMode = 0, NewMode;
42
 
float ConvertTime = 0;
43
 
float Points[NRPOINTS][4];
44
 
 
45
 
void hack_reshape (xstuff_t * XStuff)
46
 
{
47
 
        glViewport (0, 0, (GLsizei) XStuff->windowWidth, (GLsizei) XStuff->windowHeight);
48
 
 
49
 
        glMatrixMode(GL_PROJECTION);
50
 
        glLoadIdentity();
51
 
 
52
 
        gluPerspective (30, (float)XStuff->windowWidth / (float)XStuff->windowHeight, 7, 13);
53
 
 
54
 
        glMatrixMode (GL_MODELVIEW);
55
 
        glLoadIdentity ();
56
 
 
57
 
        glClearColor (0, 0, 0, 0);
58
 
}
59
 
 
60
 
void hack_init (xstuff_t * XStuff)      // Called right after the window is created, and OpenGL is initialized.
61
 
{
62
 
        int i, j;
63
 
        float x, y, r, d;
64
 
        unsigned char texbuf[64][64];
65
 
 
66
 
        for (i = 0; i < NRPOINTS; i++) {
67
 
                Points[i][0] = rsRandf (PIx2);
68
 
                Points[i][1] = rsRandf (PI) - 0.5 * PI;
69
 
                Points[i][2] = Points[i][0];
70
 
                Points[i][3] = Points[i][1];
71
 
        }
72
 
 
73
 
        glColorMaterial (GL_FRONT, GL_DIFFUSE);
74
 
 
75
 
        glDisable (GL_DEPTH_TEST);
76
 
        glDisable (GL_LIGHTING);
77
 
 
78
 
        memset ((void *)&texbuf, 0, 4096);
79
 
 
80
 
        r = 32;
81
 
        for (i = 0; i < 64; i++) {
82
 
                for (j = 0; j < 64; j++) {
83
 
                        x = abs (i - 32);
84
 
                        y = abs (j - 32);
85
 
                        d = sqrt (x * x + y * y);
86
 
 
87
 
                        if (d < r) {
88
 
                                d = 1 - (d / r);
89
 
                                texbuf[i][j] = (char)(255.0f * d * d);
90
 
                        }
91
 
                }
92
 
        }
93
 
 
94
 
        gluBuild2DMipmaps (GL_TEXTURE_2D, 1, 64, 64, GL_LUMINANCE, GL_UNSIGNED_BYTE, texbuf);
95
 
 
96
 
        glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
97
 
        glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
98
 
 
99
 
        hack_reshape(XStuff);
100
 
}
101
 
 
102
 
void hack_cleanup (xstuff_t * XStuff)
103
 
{
104
 
}
105
 
 
106
 
void CalcPoints (float currentTime)
107
 
{
108
 
        float x1, x2, y1, y2;
109
 
        int i;
110
 
        float dt;
111
 
 
112
 
        dt = currentTime - ConvertTime;
113
 
 
114
 
        if (dt < 5) {
115
 
                for (i = 0; i < NRPOINTS; i++) {
116
 
                        x1 = 0;
117
 
                        y1 = 0;
118
 
                        x2 = 0;
119
 
                        y2 = 0;
120
 
 
121
 
                        switch (NewMode) {
122
 
                        case EFFECT_RANDOM:
123
 
                                x1 = Points[i][2];
124
 
                                y1 = Points[i][3];
125
 
 
126
 
                                break;
127
 
 
128
 
                        case EFFECT_CRESCENT:
129
 
                                x1 = (i * 0.2 * (1 + sin (0.3 * currentTime))) / (PIx2);
130
 
                                x1 = PIx2 * (x1 - (int)x1);
131
 
                                y1 = PI * ((float)i / NRPOINTS - 0.5);
132
 
 
133
 
                                break;
134
 
 
135
 
                        case EFFECT_DOT:
136
 
                                x1 = Points[i][2];
137
 
                                y1 = ((Points[i][3] > 0) ? 0.5 : -0.5) * PI - Points[i][3] * 0.5 * (1 + sin (0.3 * currentTime));
138
 
 
139
 
                                break;
140
 
 
141
 
                        case EFFECT_RING:
142
 
                                x1 = Points[i][2];
143
 
                                y1 = Points[i][3] * 0.5 * (1 + sin (0.3 * currentTime));
144
 
 
145
 
                                break;
146
 
 
147
 
                        case EFFECT_LONGITUDE:
148
 
                                x1 = i / 10.0f;
149
 
                                x1 = PIx2 * (x1 - (int)x1);
150
 
                                y1 = 0.9 * PI * ((float)i / NRPOINTS - 0.5);
151
 
                        }
152
 
 
153
 
                        switch (OldMode) {
154
 
                        case EFFECT_RANDOM:
155
 
                                x2 = Points[i][2];
156
 
                                y2 = Points[i][3];
157
 
 
158
 
                                break;
159
 
 
160
 
                        case EFFECT_CRESCENT:
161
 
                                x2 = (i * 0.2 * (1 + sin (0.3 * currentTime))) / (PIx2);
162
 
                                x2 = PIx2 * (x2 - (int)x2);
163
 
                                y2 = PI * ((float)i / NRPOINTS - 0.5);
164
 
 
165
 
                                break;
166
 
 
167
 
                        case EFFECT_DOT:
168
 
                                x2 = Points[i][2];
169
 
                                if (Points[i][3] > 0) {
170
 
                                        y2 = 0.5 * PI - Points[i][3] * 0.5 * (1 + sin (0.3 * currentTime));
171
 
                                } else {
172
 
                                        y2 = -0.5 * PI - Points[i][3] * 0.5 * (1 + sin (0.3 * currentTime));
173
 
                                }
174
 
 
175
 
                                break;
176
 
 
177
 
                        case EFFECT_RING:
178
 
                                x2 = Points[i][2];
179
 
                                y2 = Points[i][3] * 0.5 * (1 + sin (0.3 * currentTime));
180
 
 
181
 
                                break;
182
 
 
183
 
                        case EFFECT_LONGITUDE:
184
 
                                x2 = i / 10.0f;
185
 
                                x2 = PIx2 * (x1 - (int)x2);
186
 
                                y2 = 0.9 * PI * ((float)i / NRPOINTS - 0.5);
187
 
                        }
188
 
 
189
 
                        Points[i][0] = 0.2 * (x1 * dt + x2 * (5 - dt));
190
 
                        Points[i][1] = 0.2 * (y1 * dt + y2 * (5 - dt));
191
 
                }
192
 
        } else {
193
 
                switch (NewMode) {
194
 
                case EFFECT_RANDOM:
195
 
                        for (i = 0; i < NRPOINTS; i++) {
196
 
                                Points[i][0] = Points[i][2];
197
 
                                Points[i][1] = Points[i][3];
198
 
                        }
199
 
 
200
 
                        break;
201
 
 
202
 
                case EFFECT_CRESCENT:
203
 
                        for (i = 0; i < NRPOINTS; i++) {
204
 
                                Points[i][0] = (i * 0.2 * (1 + sin (0.3 * currentTime))) / (PIx2);
205
 
                                Points[i][0] = PIx2 * (Points[i][0] - (int)Points[i][0]);
206
 
                                Points[i][1] = PI * ((float)i / NRPOINTS - 0.5);
207
 
                        }
208
 
 
209
 
                        break;
210
 
 
211
 
                case EFFECT_DOT:
212
 
                        for (i = 0; i < NRPOINTS; i++) {
213
 
                                Points[i][0] = Points[i][2];
214
 
                                Points[i][1] = ((Points[i][3] > 0) ? 0.5 : -0.5) * PI - Points[i][3] * 0.5 * (1 + sin (0.3 * currentTime));
215
 
                        }
216
 
 
217
 
                        break;
218
 
 
219
 
                case EFFECT_RING:
220
 
                        for (i = 0; i < NRPOINTS; i++) {
221
 
                                Points[i][0] = Points[i][2];
222
 
                                Points[i][1] = Points[i][3] * 0.5 * (1 + sin (0.3 * currentTime));
223
 
                        }
224
 
 
225
 
                        break;
226
 
 
227
 
                case EFFECT_LONGITUDE:
228
 
                        for (i = 0; i < NRPOINTS; i++) {
229
 
                                Points[i][0] = i / 10.0f;
230
 
                                Points[i][0] = PIx2 * (Points[i][0] - (int)Points[i][0]);
231
 
                                Points[i][1] = 0.9 * PI * ((float)i / NRPOINTS - 0.5);
232
 
                        }
233
 
                }
234
 
        }
235
 
}
236
 
 
237
 
void hack_draw (xstuff_t * XStuff, double currentTime, float frameTime)
238
 
{
239
 
        float x, y, z, w, ws;
240
 
        int i, k;
241
 
        float st2, ct2, t, t2;
242
 
        float fb_buffer[NRPOINTS * 37 * 4];
243
 
 
244
 
        currentTime = currentTime - (int)currentTime + (int)currentTime % 86400;
245
 
 
246
 
        t = 5 * currentTime + 0.35 * (cos (currentTime * 0.41 + 0.123) + cos (currentTime * 0.51 + 0.234) + cos (currentTime * 0.61 + 0.623) + cos (currentTime * 0.21 + 0.723));
247
 
        t2 = 0.3 * currentTime;
248
 
 
249
 
        if ((currentTime - ConvertTime) > 10) {
250
 
                OldMode = NewMode;
251
 
                NewMode = rsRandi (5);
252
 
                ConvertTime = currentTime;
253
 
        }
254
 
 
255
 
        CalcPoints(currentTime);
256
 
 
257
 
        glClear (GL_COLOR_BUFFER_BIT);
258
 
 
259
 
        // Setup Feedback buffer for retrieval
260
 
        glFeedbackBuffer (NRPOINTS * 37 * 4, GL_3D, fb_buffer);
261
 
        glRenderMode (GL_FEEDBACK);
262
 
 
263
 
        // set camera position
264
 
        glMatrixMode (GL_MODELVIEW);
265
 
        glPushMatrix ();
266
 
        glLoadIdentity ();
267
 
 
268
 
        gluLookAt (10, 0, 0, 0, 0, 0, 0, 0, 1);
269
 
 
270
 
        glRotatef (2.4 * t, 1, 0, 0);
271
 
        glRotatef (2.5 * t, 0, 1, 0);
272
 
        glRotatef (2.6 * t, 0, 0, 1);
273
 
 
274
 
        st2 = sin (t2);
275
 
        ct2 = cos (t2);
276
 
 
277
 
        // Generate the points
278
 
        glBegin (GL_POINTS);
279
 
        for (i = 0; i < NRPOINTS; i++) {
280
 
                x = sin (Points[i][0]) * cos (Points[i][1]);
281
 
                y = cos (Points[i][0]) * cos (Points[i][1]);
282
 
                z = sin (Points[i][1]);
283
 
 
284
 
                glVertex3f (x, y, z);
285
 
 
286
 
                x = 0.5 * x;
287
 
                y = 0.5 * y;
288
 
                z = 0.5 * z;
289
 
 
290
 
                glVertex3f (x + 1.5, y, z);
291
 
                glVertex3f (x - 1.5, y, z);
292
 
                glVertex3f (x, y + 1.5, z);
293
 
                glVertex3f (x, y - 1.5, z);
294
 
                glVertex3f (x, y, z + 1.5);
295
 
                glVertex3f (x, y, z - 1.5);
296
 
 
297
 
                x = 0.5 * x;
298
 
                y = 0.5 * y;
299
 
                z = 0.5 * z;
300
 
 
301
 
                glVertex3f (x + 2.25, y, z);
302
 
                glVertex3f (x + 1.5, y + 0.75 * st2, z - 0.75 * ct2);
303
 
                glVertex3f (x + 1.5, y - 0.75 * ct2, z - 0.75 * st2);
304
 
                glVertex3f (x + 1.5, y - 0.75 * st2, z + 0.75 * ct2);
305
 
                glVertex3f (x + 1.5, y + 0.75 * ct2, z + 0.75 * st2);
306
 
 
307
 
                glVertex3f (x - 2.25, y, z);
308
 
                glVertex3f (x - 1.5, y - 0.75 * st2, z - 0.75 * ct2);
309
 
                glVertex3f (x - 1.5, y + 0.75 * ct2, z - 0.75 * st2);
310
 
                glVertex3f (x - 1.5, y + 0.75 * st2, z + 0.75 * ct2);
311
 
                glVertex3f (x - 1.5, y - 0.75 * ct2, z + 0.75 * st2);
312
 
 
313
 
                glVertex3f (x, y + 2.25, z);
314
 
                glVertex3f (x + 0.75 * st2, y + 1.5, z - 0.75 * ct2);
315
 
                glVertex3f (x - 0.75 * ct2, y + 1.5, z - 0.75 * st2);
316
 
                glVertex3f (x - 0.75 * st2, y + 1.5, z + 0.75 * ct2);
317
 
                glVertex3f (x + 0.75 * ct2, y + 1.5, z + 0.75 * st2);
318
 
 
319
 
                glVertex3f (x, y - 2.25, z);
320
 
                glVertex3f (x - 0.75 * st2, y - 1.5, z - 0.75 * ct2);
321
 
                glVertex3f (x + 0.75 * ct2, y - 1.5, z - 0.75 * st2);
322
 
                glVertex3f (x + 0.75 * st2, y - 1.5, z + 0.75 * ct2);
323
 
                glVertex3f (x - 0.75 * ct2, y - 1.5, z + 0.75 * st2);
324
 
 
325
 
                glVertex3f (x, y, z + 2.25);
326
 
                glVertex3f (x + 0.75 * st2, y - 0.75 * ct2, z + 1.5);
327
 
                glVertex3f (x - 0.75 * ct2, y - 0.75 * st2, z + 1.5);
328
 
                glVertex3f (x - 0.75 * st2, y + 0.75 * ct2, z + 1.5);
329
 
                glVertex3f (x + 0.75 * ct2, y + 0.75 * st2, z + 1.5);
330
 
 
331
 
                glVertex3f (x, y, z - 2.25);
332
 
                glVertex3f (x - 0.75 * st2, y - 0.75 * ct2, z - 1.5);
333
 
                glVertex3f (x + 0.75 * ct2, y - 0.75 * st2, z - 1.5);
334
 
                glVertex3f (x + 0.75 * st2, y + 0.75 * ct2, z - 1.5);
335
 
                glVertex3f (x - 0.75 * ct2, y + 0.75 * st2, z - 1.5);
336
 
        }
337
 
        glEnd ();
338
 
 
339
 
        glPopMatrix();
340
 
 
341
 
#define Wi 0
342
 
#define Xi 1
343
 
#define Yi 2
344
 
#define Zi 3
345
 
#define Ri 4
346
 
#define Gi 5
347
 
#define Bi 6
348
 
 
349
 
        glRenderMode (GL_RENDER);
350
 
 
351
 
        glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
352
 
 
353
 
        glMatrixMode(GL_PROJECTION);
354
 
        glPushMatrix();
355
 
        glLoadIdentity();
356
 
 
357
 
        glMatrixMode(GL_MODELVIEW);
358
 
        glPushMatrix();
359
 
        glLoadIdentity ();
360
 
 
361
 
        glScalef (2.0 / XStuff->windowWidth, 2.0 / XStuff->windowHeight, 1.0);
362
 
        glTranslatef (-0.5 * XStuff->windowWidth, -0.5 * XStuff->windowHeight, 0);
363
 
 
364
 
        glEnable (GL_TEXTURE_2D);
365
 
        glBlendFunc (GL_ONE, GL_ONE);
366
 
        glEnable (GL_BLEND);
367
 
 
368
 
        // Draw objects from back to front
369
 
        glBegin (GL_QUADS);
370
 
        for (i = 0, k = 0; i < NRPOINTS * 37; i++, k++) {
371
 
                if (k == 37)
372
 
                        k = 0;
373
 
 
374
 
                x = fb_buffer[4 * i + 1];
375
 
                y = fb_buffer[4 * i + 2];
376
 
                z = fb_buffer[4 * i + 3];
377
 
 
378
 
                w = 1.3 - z;    // diminishing fading
379
 
                ws = 0.002 * XStuff->windowHeight * (1 - z);    // Keep Bitmaps same size realive to screensize
380
 
 
381
 
                if (k == 0) {   // big sphere
382
 
                        glColor3f (0.6 * w, 0.5 * w, 0.3 * w);
383
 
                        w = 70 * ws;
384
 
                } else if (k < 7) {
385
 
                        glColor3f (0.3 * w, 0.6 * w, 0.4 * w);
386
 
                        w = 30 * ws;
387
 
                } else {
388
 
                        glColor3f (0.2 * w, 0.3 * w, 0.4 * w);
389
 
                        w = 12 * ws;
390
 
                }
391
 
 
392
 
                glTexCoord2f (0.0, 0.0);
393
 
                glVertex2f (x - w, y - w);
394
 
 
395
 
                glTexCoord2f (1.0, 0.0);
396
 
                glVertex2f (x + w, y - w);
397
 
 
398
 
                glTexCoord2f (1.0, 1.0);
399
 
                glVertex2f (x + w, y + w);
400
 
 
401
 
                glTexCoord2f (0.0, 1.0);
402
 
                glVertex2f (x - w, y + w);
403
 
        }
404
 
        glEnd ();
405
 
 
406
 
        glMatrixMode(GL_MODELVIEW);
407
 
        glPopMatrix();
408
 
 
409
 
        glMatrixMode(GL_PROJECTION);
410
 
        glPopMatrix();
411
 
}
412
 
 
413
 
void hack_handle_opts (int argc, char **argv)
414
 
{
415
 
        while (1) {
416
 
                int c;
417
 
 
418
 
#ifdef HAVE_GETOPT_H
419
 
                static struct option long_options[] = {
420
 
                        {"help", 0, 0, 'h'},
421
 
                        DRIVER_OPTIONS_LONG {0, 0, 0, 0}
422
 
                };
423
 
 
424
 
                c = getopt_long (argc, argv, DRIVER_OPTIONS_SHORT "hq:s:t:f:b:eE", long_options, NULL);
425
 
#else
426
 
                c = getopt (argc, argv, DRIVER_OPTIONS_SHORT "hq:s:t:f:b:eE");
427
 
#endif
428
 
                if (c == -1)
429
 
                        break;
430
 
 
431
 
                switch (c) {
432
 
                        DRIVER_OPTIONS_CASES case 'h':printf ("%s:"
433
 
#ifndef HAVE_GETOPT_H
434
 
                                                              " Not built with GNU getopt.h, long options *NOT* enabled."
435
 
#endif
436
 
                                                              "\n" DRIVER_OPTIONS_HELP, argv[0]);
437
 
                        exit (1);
438
 
                }
439
 
        }
440
 
}