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
|
/*
* Really Slick XScreenSavers
* Copyright (C) 2002-2006 Michael Chapman
*
* This program 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.
*
* This program 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
*
*****************************************************************************
*
* This is a Linux port of the Really Slick Screensavers,
* Copyright (C) 2002 Terence M. Welsh, available from www.reallyslick.com
*/
#include <common.hh>
#include <color.hh>
#include <fieldlines.hh>
#include <hack.hh>
#include <ion.hh>
#include <vector.hh>
namespace Hack {
unsigned int numIons = 4;
float stepSize = 15.0f;
unsigned int maxSteps = 100;
float width = 30.0f;
float speed = 10.0f;
bool constWidth = false;
bool electric = false;
};
namespace Hack {
enum Arguments {
ARG_IONS = 1,
ARG_SEGSIZE,
ARG_MAXSEGS,
ARG_SPEED,
ARG_WIDTH,
ARG_CONSTANT = 0x100, ARG_NO_CONSTANT,
ARG_ELECTRIC = 0x200, ARG_NO_ELECTRIC
};
std::vector<Ion> _ions;
static void drawFieldLine(const Ion&, float, float, float);
error_t parse(int, char*, struct argp_state*);
};
static void Hack::drawFieldLine(const Ion& ion, float x, float y, float z) {
static float brightness = 10000.0f;
int charge = ion.getCharge();
Vector lastV(ion.getV());
Vector dir(x, y, z);
// Do the first segment
RGBColor RGB(
std::abs(dir.z()) * brightness,
std::abs(dir.x()) * brightness,
std::abs(dir.y()) * brightness
);
RGB.clamp();
RGBColor lastRGB(RGB);
glColor3fv(RGB.get());
Vector V(lastV + dir);
if (electric)
V += Vector(
Common::randomFloat(stepSize * 0.3f) - (stepSize * 0.15f),
Common::randomFloat(stepSize * 0.3f) - (stepSize * 0.15f),
Common::randomFloat(stepSize * 0.3f) - (stepSize * 0.15f)
);
if (!constWidth)
glLineWidth((V.z() + 300.0f) * 0.000333f * width);
glBegin(GL_LINE_STRIP);
glColor3fv(lastRGB.get());
glVertex3fv(lastV.get());
glColor3fv(RGB.get());
glVertex3fv(V.get());
Vector end;
unsigned int i;
for (i = 0; i < maxSteps; ++i) {
dir.set(0.0f, 0.0f, 0.0f);
for (std::vector<Ion>::const_iterator j = _ions.begin(); j != _ions.end(); ++j) {
int repulsion = charge * j->getCharge();
Vector temp(V - j->getV());
float distSquared = temp.lengthSquared();
float dist = std::sqrt(distSquared);
if (dist < stepSize && i > 2) {
end = j->getV();
i = 10000;
}
temp /= dist;
if (distSquared < 1.0f)
distSquared = 1.0f;
dir += temp * (repulsion / distSquared);
}
lastRGB = RGB;
RGB.set(
std::abs(dir.z()) * brightness,
std::abs(dir.x()) * brightness,
std::abs(dir.y()) * brightness
);
if (electric) {
RGB *= 10.0f;
if (RGB.r() > RGB.g() * 0.5f)
RGB.r() = RGB.g() * 0.5f;
if (RGB.g() > RGB.b() * 0.3f)
RGB.g() = RGB.b() * 0.3f;
}
RGB.clamp();
float distSquared = dir.lengthSquared();
float distRec = stepSize / std::sqrt(distSquared);
dir *= distRec;
if (electric)
dir += Vector(
Common::randomFloat(stepSize) - (stepSize * 0.5f),
Common::randomFloat(stepSize) - (stepSize * 0.5f),
Common::randomFloat(stepSize) - (stepSize * 0.5f)
);
lastV = V;
V += dir;
if (!constWidth) {
glEnd();
float lineWidth = (V.z() + 300.0f) * 0.000333f * width;
glLineWidth(lineWidth < 0.01f ? 0.01f : lineWidth);
glBegin(GL_LINE_STRIP);
}
glColor3fv(lastRGB.get());
glVertex3fv(lastV.get());
if (i != 10000) {
if (i == (maxSteps - 1))
glColor3f(0.0f, 0.0f, 0.0f);
else
glColor3fv(RGB.get());
glVertex3fv(V.get());
}
}
if (i == 10001) {
glColor3fv(RGB.get());
glVertex3fv(end.get());
}
glEnd();
}
error_t Hack::parse(int key, char* arg, struct argp_state* state) {
switch (key) {
case ARG_IONS:
if (Common::parseArg(arg, numIons, 1u, 10u))
argp_failure(state, EXIT_FAILURE, 0,
"number of ions must be between 1 and 10");
return 0;
case ARG_SEGSIZE:
if (Common::parseArg(arg, stepSize, 1.0f, 100.0f))
argp_failure(state, EXIT_FAILURE, 0,
"fieldline segment length must be between 1 and 100");
return 0;
case ARG_MAXSEGS:
if (Common::parseArg(arg, maxSteps, 1u, 1000u))
argp_failure(state, EXIT_FAILURE, 0,
"maximum number of fieldline segments must be between 1 and 1000");
return 0;
case ARG_SPEED:
if (Common::parseArg(arg, speed, 1.0f, 100.0f))
argp_failure(state, EXIT_FAILURE, 0,
"motion speed must be between 1 and 100");
return 0;
case ARG_WIDTH:
if (Common::parseArg(arg, width, 1.0f, 100.0f))
argp_failure(state, EXIT_FAILURE, 0,
"fieldline width factor must be between 1 and 100");
return 0;
case ARG_CONSTANT:
constWidth = true;
return 0;
case ARG_NO_CONSTANT:
constWidth = false;
return 0;
case ARG_ELECTRIC:
electric = true;
return 0;
case ARG_NO_ELECTRIC:
electric = false;
return 0;
default:
return ARGP_ERR_UNKNOWN;
}
}
const struct argp* Hack::getParser() {
static struct argp_option options[] = {
{ NULL, 0, NULL, 0, "Ion options:" },
{ "ions", ARG_IONS, "NUM", 0, "Number of ions (1-10, default = 4)" },
{ "speed", ARG_SPEED, "NUM", 0, "Motion speed (1-100, default = 10)" },
{ NULL, 0, NULL, 0, "Fieldline options:" },
{ "segsize", ARG_SEGSIZE, "NUM", 0,
"Length of each fieldline segment (1-100, default = 15)" },
{ "maxsegs", ARG_MAXSEGS, "NUM", 0,
"Maximum number of segments per fieldline (1-1000, default = 100)" },
{ "width", ARG_WIDTH, "NUM", 0,
"Fieldline width factor (1-100, default = 30)" },
{ "constant", ARG_CONSTANT, NULL, 0, "Enable constant-width fieldlines" },
{ "no-constant", ARG_NO_CONSTANT, NULL, OPTION_ALIAS | OPTION_HIDDEN },
{ "electric", ARG_ELECTRIC, NULL, 0, "Enable electric mode" },
{ "no-electric", ARG_NO_ELECTRIC, NULL, OPTION_ALIAS | OPTION_HIDDEN },
{}
};
static struct argp parser = {
options, parse, NULL,
"Simulates field lines between charged particles."
};
return &parser;
}
std::string Hack::getShortName() { return "fieldlines"; }
std::string Hack::getName() { return "Field Lines"; }
void Hack::start() {
glViewport(0, 0, Common::width, Common::height);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LINE_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(80.0, Common::aspectRatio, 50, 3000);
glTranslatef(0.0, 0.0, -(WIDE * 2));
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
if (constWidth)
glLineWidth(width * 0.1f);
stdx::construct_n(_ions, numIons);
}
void Hack::tick() {
static float s = std::sqrt(stepSize * stepSize * 0.333f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
stdx::call_all(_ions, &Ion::update);
for (std::vector<Ion>::const_iterator i = _ions.begin(); i != _ions.end(); ++i) {
drawFieldLine(*i, s, s, s);
drawFieldLine(*i, s, s, -s);
drawFieldLine(*i, s, -s, s);
drawFieldLine(*i, s, -s, -s);
drawFieldLine(*i, -s, s, s);
drawFieldLine(*i, -s, s, -s);
drawFieldLine(*i, -s, -s, s);
drawFieldLine(*i, -s, -s, -s);
}
// Common::flush();
}
void Hack::reshape() {
glViewport(0, 0, Common::width, Common::height);
}
void Hack::stop() {}
void Hack::keyPress(char c, const KeySym&) {
switch (c) {
case 3: case 27:
case 'q': case 'Q':
Common::running = false;
break;
}
}
void Hack::keyRelease(char, const KeySym&) {}
void Hack::pointerMotion(int, int) {}
void Hack::buttonPress(unsigned int) {}
void Hack::buttonRelease(unsigned int) {}
void Hack::pointerEnter() {}
void Hack::pointerLeave() {}
#define _LINUX
#include "../../../addons/include/xbmc_scr_dll.h"
extern "C" {
ADDON_STATUS Create(void* hdl, void* props)
{
if (!props)
return STATUS_UNKNOWN;
SCR_PROPS* scrprops = (SCR_PROPS*)props;
Common::width = scrprops->width;
Common::height = scrprops->height;
Common::aspectRatio = float(Common::width) / float(Common::height);
return STATUS_OK;
}
void Start()
{
Hack::start();
}
void Render()
{
Hack::tick();
}
void Stop()
{
Hack::stop();
}
void Destroy()
{
}
ADDON_STATUS GetStatus()
{
return STATUS_OK;
}
bool HasSettings()
{
return false;
}
unsigned int GetSettings(StructSetting ***sSet)
{
return 0;
}
ADDON_STATUS SetSetting(const char *settingName, const void *settingValue)
{
return STATUS_OK;
}
void FreeSettings()
{
}
void GetInfo(SCR_INFO *info)
{
}
void Remove()
{
}
}
|