~ubuntu-branches/ubuntu/karmic/rss-glx/karmic

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
/*
 * Copyright (C) 2002  Dirk Songuer <delax@sundancerinc.de>
 * Ported to Linux by Tugrul Galatali <tugrul@galatali.com>
 *
 * Sundancer[2] is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as 
 * published by the Free Software Foundation.
 *
 * Sundancer[2] is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

// Sundancer[2] screen saver

#include <math.h>
#include <stdio.h>
#include <GL/gl.h>

typedef struct quadsystem_struct {
	float PositionX, PositionY, PositionZ;
	float ColorR, ColorG, ColorB;
	float Rotation;
	int Laenge, Hoehe;
	int Status, Intensity;
} quadsystem;

#include "driver.h"
#include "rsRand.h"

const char *hack_name = "Sundancer[2]";

quadsystem *quads;

int quad_count = 150;

#define QUAD_SPEED_MAX_DEFAULT 20.0 / 400.0
float quad_speed_max = QUAD_SPEED_MAX_DEFAULT;
float quad_speed = QUAD_SPEED_MAX_DEFAULT;

int quads_timer = -1;
float direction_add = 0.00001f;
int reverse = 0;

int sr = 255, sg = 0, sb = 0;
int er = 255, eg = 255, eb = 0;

float transparency_value = 0.25f;

float LightPosition[4] = { 0.0, 0.0, 0.0, 1.0 };

float vlight, hlight, zlight;
float vlightmul, hlightmul, zlightmul;

float vertexw1, vertexw2, vertexw3, vertexw4;
float vertexwmul1, vertexwmul2, vertexwmul3, vertexwmul4;

void hack_reshape (xstuff_t * XStuff)
{
	// Window initialization
	glViewport (0, 0, (GLsizei) XStuff->windowWidth, (GLsizei) XStuff->windowHeight);

	glMatrixMode (GL_PROJECTION);
	glLoadIdentity ();

	glOrtho (-400, 400, -300, 300, -400, 400);
	glMatrixMode (GL_MODELVIEW);
	glLoadIdentity ();
}

void hack_init (xstuff_t * XStuff)	// Called right after the window is created, and OpenGL is initialized.
{
	float Ambient[4] = { 0.1, 0.1, 0.1, 1.0 };
	float Diffuse[4] = { 1.0, 1.0, 1.0, 1.0 };
	float temp_r, temp_g, temp_b;
	float step_r, step_g, step_b;
	int i;

	glClearColor (0.0, 0.0, 0.0, 0.0);
	glShadeModel (GL_SMOOTH);

	if (transparency_value == 1.0f) {
		glEnable (GL_DEPTH_TEST);
		glClearDepth (1.0);
		glDepthFunc (GL_LESS);
	} else {
		glEnable (GL_BLEND);	// Turn Blending On
		glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	}

	glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

	glEnable (GL_LIGHTING);

	glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, Diffuse);
	glLightfv (GL_LIGHT1, GL_AMBIENT, Ambient);
	glLightfv (GL_LIGHT1, GL_DIFFUSE, Diffuse);
	glLightfv (GL_LIGHT1, GL_POSITION, LightPosition);
	glLightfv (GL_LIGHT1, GL_SPOT_DIRECTION, LightPosition);

	glEnable (GL_LIGHT1);
	glEnable (GL_COLOR_MATERIAL);

	hack_reshape (XStuff);

// Init Quads
	quads = (quadsystem *) malloc (quad_count * sizeof (quadsystem));

	step_r = (er - sr) / (quad_count * 255.0f);
	step_g = (eg - sg) / (quad_count * 255.0f);
	step_b = (eb - sb) / (quad_count * 255.0f);

	temp_r = sr / 255.0f;
	temp_g = sg / 255.0f;
	temp_b = sb / 255.0f;

	for (i = 0; i < quad_count; i++) {
		quads[i].PositionX = -100.0f;
		quads[i].PositionY = -100.0f;
		quads[i].PositionZ = i * 1.4f;

		quads[i].Laenge = 300 - i;
		quads[i].Hoehe = 300 - i;
		quads[i].Rotation = i / 100.0f;

		quads[i].ColorR = temp_r;
		quads[i].ColorG = temp_g;
		quads[i].ColorB = temp_b;

		temp_r += step_r;
		temp_g += step_g;
		temp_b += step_b;
	}

// Init Light
	vlight = 0.0;
	hlight = 0.0;
	zlight = 0.0;
	vlightmul = -1;
	hlightmul = -1;
	zlightmul = -1;

// Init Vertices
	vertexw1 = 1.0;
	vertexw2 = 1.0;
	vertexw3 = 1.0;
	vertexw4 = 1.0;

	vertexwmul1 = rsRandf (10) / 4096;
	vertexwmul2 = rsRandf (10) / 4096;
	vertexwmul3 = rsRandf (10) / 4096;
	vertexwmul4 = rsRandf (10) / 4096;
}

void hack_cleanup (xstuff_t * XStuff)
{
}

void hack_draw (xstuff_t * XStuff, double currentTime, float frameTime)
{
	static int change_direction = 0;
	int i;

	if (quads_timer == -1)
		quads_timer = (int)currentTime + (rsRandi (30) + 3);

	LightPosition[0] = hlight;	// set new light position - x
	LightPosition[1] = vlight;	// set new light position - y
	LightPosition[2] = zlight;	// set new light position - z

	glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity ();

	glLightfv (GL_LIGHT1, GL_POSITION, LightPosition);
	glTranslatef (0.0, -10.0, 0.0);

	for (i = 0; i < quad_count; i++) {
		glRotatef (quads[i].Rotation, 0.0, 0.0, 1.0);
		glColor4f (quads[i].ColorR, quads[i].ColorG, quads[i].ColorB, transparency_value);

		glColorMaterial (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);

		glBegin (GL_QUADS);
		glVertex4f (quads[i].PositionX, quads[i].PositionY, quads[i].PositionZ, vertexw1);
		glVertex4f (quads[i].PositionX + quads[i].Laenge, quads[i].PositionY, quads[i].PositionZ, vertexw2);
		glVertex4f (quads[i].PositionX + quads[i].Laenge, quads[i].PositionY + quads[i].Hoehe, quads[i].PositionZ, vertexw3);
		glVertex4f (quads[i].PositionX, quads[i].PositionY + quads[i].Hoehe, quads[i].PositionZ, vertexw4);
		glEnd ();
	}

// Update Quads
	if (reverse) {		//rotation
		if (quads_timer < (int)currentTime)
			change_direction = 1;

		if (change_direction)
			quad_speed -= direction_add * quad_count;

		if ((quad_speed > quad_speed_max) || (quad_speed < -quad_speed_max)) {
			if (quad_speed > quad_speed_max) {
				quad_speed = quad_speed_max - 0.0001;
			} else {
				quad_speed = -quad_speed_max + 0.0001;
			}

			change_direction = 0;
			direction_add *= -1;
			quads_timer = (int)currentTime + (rsRandi (30) + 3);
		}
	}

	for (i = 0; i < quad_count; i++) {
		quads[i].Rotation += quad_speed;
	}

// Update Light
	if ((vlight > 300) || (vlight < -300))
		vlightmul *= -1;
	if ((hlight > 300) || (hlight < -300))
		hlightmul *= -1;
	if ((zlight > 400) || (zlight < 0))
		zlightmul *= -1;

	vlight += vlightmul;
	hlight += hlightmul;
	zlight += zlightmul;

// Update Vertices
	if ((vertexw1 > 2.5) || (vertexw1 < 0.7))
		vertexwmul1 *= -1;
	if ((vertexw2 > 2.5) || (vertexw2 < 0.7))
		vertexwmul2 *= -1;
	if ((vertexw3 > 2.5) || (vertexw3 < 0.7))
		vertexwmul3 *= -1;
	if ((vertexw4 > 2.5) || (vertexw4 < 0.7))
		vertexwmul4 *= -1;

	vertexw1 += vertexwmul1;
	vertexw2 += vertexwmul2;
	vertexw3 += vertexwmul3;
	vertexw4 += vertexwmul4;
}

void hack_handle_opts (int argc, char **argv)
{
	int change_flag = 0;

	while (1) {
		int c;

#ifdef HAVE_GETOPT_H
		static struct option long_options[] = {
			{"help", 0, 0, 'h'},
			DRIVER_OPTIONS_LONG {"quad_count", 1, 0, 'q'},
			{"speed", 1, 0, 's'},
			{"transparency", 1, 0, 't'},
			{"foreground", 1, 0, 'f'},
			{"background", 1, 0, 'b'},
			{"reverse", 0, 0, 'e'},
			{"no-reverse", 0, 0, 'E'},
			{0, 0, 0, 0}
		};

		c = getopt_long (argc, argv, DRIVER_OPTIONS_SHORT "hq:s:t:f:b:eE", long_options, NULL);
#else
		c = getopt (argc, argv, DRIVER_OPTIONS_SHORT "hq:s:t:f:b:eE");
#endif
		if (c == -1)
			break;

		switch (c) {
		DRIVER_OPTIONS_CASES 
		case 'h':
			printf ("%s:"
#ifndef HAVE_GETOPT_H
				" Not built with GNU getopt.h, long options *NOT* enabled."
#endif
				"\n" DRIVER_OPTIONS_HELP "\t--quad_count/-q <arg>\n" "\t--speed/-s <arg>\n" "\t--transparency/-t <arg>\n" "\t--foreground/-f <arg>\n"
				"\t--background/-b <arg>\n" "\t--reverse/-e\n" "\t--no-reverse/-E\n", argv[0]);
			exit (1);
		case 'q':
			change_flag = 1;
			quad_count = strtol_minmaxdef (optarg, 10, 1, 300, 1, 150, "--quad_count: ");
			break;
		case 's':
			change_flag = 1;

			quad_speed_max = strtol_minmaxdef (optarg, 10, 1, 40, 1, 20, "--speed: ") / 400.0;
			quad_speed = quad_speed_max;

			break;
		case 't':
			change_flag = 1;
			transparency_value = strtol_minmaxdef (optarg, 10, 1, 100, 1, 75, "--transparency: ") / 100.0f;
			break;
		case 'f':{
				int color = strtol_minmaxdef (optarg, 16, 0, 16777216, 0, 0xFF0000, "--foreground: ");

				change_flag = 1;

				er = (color & 0xFF0000) >> 16;
				eg = (color & 0xFF00) >> 8;
				eb = (color & 0xFF);

			}
			break;
		case 'b':{
				int color = strtol_minmaxdef (optarg, 16, 0, 16777216, 0, 0xFFFF00, "--background: ");

				change_flag = 1;

				sr = (color & 0xFF0000) >> 16;
				sg = (color & 0xFF00) >> 8;
				sb = (color & 0xFF);

			}
			break;
		case 'e':
			change_flag = 1;
			reverse = 1;
			break;
		case 'E':
			change_flag = 1;
			reverse = 0;
			break;
		}
	}

	if (!change_flag) {
		quad_count = rsRandi (296) + 5;

		quad_speed_max = rsRandf (50) / 400.0;
		quad_speed = quad_speed_max;

		transparency_value = (rsRandf (100) + 1) / 100.0f;

		sr = rsRandi (256);
		sg = rsRandi (256);
		sb = rsRandi (256);

		er = rsRandi (256);
		eg = rsRandi (256);
		eb = rsRandi (256);

		reverse = rsRandi (2);
	}
}