-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElectropaintView.mm
382 lines (316 loc) · 9.08 KB
/
ElectropaintView.mm
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
/*
* ElectropaintOSXView.mm
* ElectropaintOSX
*
* Modifications for Universal Binary (Version 0.3)
* 07/10/06 Alexander v. Below <[email protected]>
*
* Modifications for antialiasing, VBL, parameter tweak.
* Vincent Fiano <[email protected]>
*
* http://www.lloydslounge.org/electropaintosx/
*
* Created by Douglas McInnes on 12/17/04.
* Copyright (c) 2004, Kent RosenKoetter, Douglas McInnes.
* All rights reserved.
*
* ported from Kent Rosenkoetter's electropaint.cpp:
* http://legolas.homelinux.org/~kent/electropaint/
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* 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.
*
*/
#import "ElectropaintView.h"
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
#include <OpenGL/OpenGL.h>
#include <cmath>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <ctime>
#include <list>
#include <algorithm>
#include <functional>
struct color_type {
CGFloat red;
CGFloat green;
CGFloat blue;
color_type(void) : red(1), green(1), blue(1)
{ };
color_type(CGFloat r, CGFloat g, CGFloat b) : red(r), green(g), blue(b)
{ };
color_type(const color_type & c) : red(c.red), green(c.green), blue(c.blue)
{ };
};
struct wing_type
{
CGFloat radius; // all angles in degrees, per OpenGL
CGFloat angle;
CGFloat delta_angle;
CGFloat z_delta;
CGFloat roll;
CGFloat pitch;
CGFloat yaw;
color_type color;
color_type edge_color;
CGFloat alpha;
wing_type(void) : radius(10), angle(0), delta_angle(15), z_delta(0.5),
roll(0), pitch(0), yaw(0),
color(), edge_color(), alpha(1)
{ }
wing_type(CGFloat _rad, CGFloat _ang, CGFloat _dang, CGFloat _dz,
CGFloat _roll, CGFloat _pitch, CGFloat _yaw,
const color_type & _c, const color_type & _ec = color_type(),
CGFloat _a = 1) : radius(_rad), angle(_ang), delta_angle(_dang), z_delta(_dz),
roll(_roll), pitch(_pitch), yaw(_yaw),
color(_c), edge_color(_ec),
alpha(_a)
{ }
};
struct random_generator_type
{
CGFloat min_value;
CGFloat max_value;
bool wrap;
CGFloat max_acceleration;
CGFloat max_speed;
NSUInteger stability;
random_generator_type(CGFloat min, CGFloat max = 1.0,
NSUInteger stab = 50,
bool wr = false,
CGFloat msp = 0.02,
CGFloat macc = 0.005) : min_value(min), max_value(max),
wrap(wr),
max_acceleration(macc), max_speed(msp), stability(stab),
value(0), delta(0), count(INT_MAX - 1),
rand_state(0)
{
}
CGFloat operator()(void)
{
if (++count > stability) {
accel = getNewAccel();
// std::cout << accel << std::endl;
count = 0;
}
delta += accel;
delta = std::min(delta, max_speed);
delta = std::max(delta, -max_speed);
value += delta;
if (wrap) {
// fmodf
value = remainderf(value - min_value, max_value - min_value) + min_value;
} else {
value = std::min(value, max_value);
value = std::max(value, min_value);
}
return value;
}
protected:
CGFloat value;
CGFloat delta;
CGFloat accel;
NSUInteger count;
NSUInteger rand_state;
CGFloat getNewAccel(void)
{
// unsigned int r(rand_r(&rand_state));
NSUInteger r(rand());
CGFloat f = r / (RAND_MAX + 1.0f);
f = (f - 0.5f) * 2.0f;
f *= max_acceleration;
return f;
}
};
GLuint wing_dl;
std::list<wing_type> wings;
random_generator_type red_movement(0.0, 1.0, 95);
random_generator_type green_movement(0.0, 1.0, 40);
random_generator_type blue_movement(0.0, 1.0, 70);
random_generator_type roll_change(0, 360, 80, true, 0.5, 0.125);
random_generator_type pitch_change(0, 360, 40, true, 1.0, 0.125);
random_generator_type yaw_change(0, 360, 50, true, 0.75, 0.125);
random_generator_type radius_change(-15, 15, 150, false, 0.05, 0.005);
random_generator_type angle_change(0, 360, 120, true, 1, 0.025);
random_generator_type delta_angle_change(0, 360, 80, true, 0.1, 0.01);
random_generator_type z_delta_change(0.4, 0.7, 200, false, 0.005, 0.0005);
@implementation ElectropaintView
/*****************************/
/* ScreenSaverView functions */
/*****************************/
- (id)initWithFrame:(NSRect)frame isPreview:(BOOL)isPreview {
self = [super initWithFrame:frame isPreview:isPreview];
if (self) {
[self setAnimationTimeInterval:1/60.0];
NSOpenGLPixelFormatAttribute attr[] = {
NSOpenGLPFANoRecovery,
NSOpenGLPFAAccelerated,
NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute) 24,
NSOpenGLPFAAlphaSize, (NSOpenGLPixelFormatAttribute) 8,
NSOpenGLPFAStencilSize, (NSOpenGLPixelFormatAttribute) 0,
NSOpenGLPFAWindow,
(NSOpenGLPixelFormatAttribute) 0,
};
NSOpenGLPixelFormat *format = [[[NSOpenGLPixelFormat alloc] initWithAttributes:attr] autorelease];
glview = [[[NSOpenGLView alloc] initWithFrame:NSZeroRect pixelFormat:format] autorelease];
[self initGL];
}
return self;
}
- (void)startAnimation {
if (![glview isDescendantOf:self]) {
[self addSubview:glview];
}
[super startAnimation];
}
- (void)stopAnimation {
[super stopAnimation];
}
- (void)drawRect:(NSRect)rect {
[super drawRect:rect];
}
- (void)animateOneFrame {
// animate
wings.pop_back();
wings.push_front(wing_type(radius_change(),
angle_change(),
delta_angle_change(),
z_delta_change(),
roll_change(),
pitch_change(),
yaw_change(),
color_type(red_movement(),
green_movement(),
blue_movement())));
//glutPostRedisplay();
[self display];
return;
}
- (BOOL)hasConfigureSheet {
return NO;
}
- (NSWindow*)configureSheet {
return nil;
}
- (void)setFrameSize:(NSSize)newSize {
[glview setFrameSize:newSize];
[self reshape:newSize];
[super setFrameSize:newSize];
}
- (void)dealloc {
[glview removeFromSuperview];
[super dealloc];
}
- (void)initGL {
srand(time(NULL));
[[glview openGLContext] makeCurrentContext];
GLint params[] = { 1 };
CGLSetParameter(CGLGetCurrentContext(), kCGLCPSwapInterval, params);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
//glutDisplayFunc(display);
//glutReshapeFunc(reshape);
glEnable (GL_LINE_SMOOTH);
glHint (GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
glBlendFunc (GL_SRC_ALPHA, GL_ONE);
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE);
#if defined(GL_VERSION_1_1)
glPolygonOffset(-0.5, -2);
#endif
wing_dl = glGenLists(1);
glNewList(wing_dl, GL_COMPILE);
glBegin(GL_QUADS);
// default z-value is 0
// default normal is (0, 0, 1);
glVertex2f(1, 1);
glVertex2f(-1, 1);
glVertex2f(-1, -1);
glVertex2f(1, -1);
glEnd();
glEndList();
wing_type newwing(radius_change(),
angle_change(),
delta_angle_change(),
z_delta_change(),
roll_change(),
pitch_change(),
yaw_change(),
color_type(red_movement(),
green_movement(),
blue_movement()));
wings.resize(40, newwing);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
- (void)display {
[[glview openGLContext] makeCurrentContext];
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0, 50, 50,
0, 0, 13,
0, 0, 1);
std::list<wing_type>::const_iterator i(wings.begin());
std::list<wing_type>::const_iterator end(wings.end());
unsigned int count(0);
//if (hasOpenGL(1, 1)) {
#if defined(GL_VERSION_1_1)
// if (hasOpenGL(1, 1))
glEnable(GL_POLYGON_OFFSET_LINE);
#endif
glPushMatrix();
while (i != end) {
wing_type wing(*i++);
glTranslatef(0, 0, wing.z_delta);
glPushMatrix();
glRotatef(wing.angle + count * wing.delta_angle,
0, 0, 1);
glTranslatef(wing.radius, 0, 0);
glRotatef(-wing.yaw, 0, 0, 1);
glRotatef(-wing.pitch, 0, 1, 0);
glRotatef(wing.roll, 1, 0, 0);
glDisable(GL_BLEND);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glColor3f(wing.color.red, wing.color.green, wing.color.blue);
glCallList(wing_dl);
glEnable (GL_BLEND);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glColor3f(wing.edge_color.red, wing.edge_color.green, wing.edge_color.blue);
glCallList(wing_dl);
glPopMatrix();
count++;
}
glPopMatrix();
glFlush();
//checkGLErrors(std::cerr);
glutSwapBuffers();
}
- (void)reshape:(NSSize)size {
[[glview openGLContext] makeCurrentContext];
glViewport(0, 0, (int)size.width, (int)size.height);
CGFloat xmult(1.0);
CGFloat ymult(1.0);
// float aspect(width / static_cast<float>(height));
if (size.width > size.height) {
xmult = size.width / size.height;
} else {
ymult = size.height / size.width;
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-20 * xmult, 20 * xmult,
-20 * ymult, 20 * ymult,
35, 105);
glMatrixMode(GL_MODELVIEW);
//checkGLErrors(std::cerr);
//[[glview openGLContext] update];
}
@end