This repository was archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnow.cpp
649 lines (596 loc) · 22.5 KB
/
snow.cpp
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
/**** BEGIN LICENSE BLOCK ****
BSD 3-Clause License
Copyright (c) 2023, the wind.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**** END LICENCE BLOCK ****/
#ifdef _WIN32
#include <windows.h>
#endif
#include <math.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <GL/gl.h>
#include <GL/glut.h>
#include <GL/freeglut.h>
#include <GL/freeglut_ext.h>
#ifdef _WIN32
#include <GL/glext.h>
#endif
#define private private:
#define public public:
template <typename T> class DMem final
{
private T * _p;
public DMem(size_t n)
{
_p = static_cast<T *>(calloc (n, sizeof(T)));
if (! _p) printf (
"Failed to allocate %zu bytes of memory. Stop.", n * sizeof(T)),
exit (1);
}
public ~DMem() { if (_p) free (_p), _p = nullptr; }
operator T*() { return _p; }
public DMem(const DMem<T> &) = delete;
public DMem operator=(const DMem<T> &) = delete;
public DMem(DMem<T> && s) { operator= (s); }
public DMem<T> && operator=(DMem<T> && s)
{
return _p = s._p, s._p = nullptr, static_cast<DMem<T> &&>(*this);
}
};
// glFrustum parameters; _WIN32 doesn't like NEAR and FAR
static float const NEARR = 1.f, FARR = 3.f;//, DEPTH = FAR - NEAR;
static float const LEFT = -1.f, RIGHT = 1.f, BOTTOM = -1.f, TOP = 1.f;
static bool paused = false;
static GLsizei current_viewport_width {0}, current_viewport_height {0};
// float should be enough
#define float float
#define MY_PI 3.14159265358979323846
//TODO os.h
#ifdef _WIN32
float rndf(float n) { return rand () / static_cast<float>(RAND_MAX) * n; }
#else
float rndf(float n) { return random () / static_cast<float>(RAND_MAX) * n; }
#endif
float rndi(int n) { return static_cast<int>(rndf (n)+0.5f); }
float rndfr(float a, float b) { return a + rndf (b-a); }
static float const TMAX {1.0/32}; // translate max value
static float const TMIN {TMAX/10}; // translate min value
static float const RMAX {5.0}; // rotate max value
static float const RMIN {-5.0}; // rotate min value
class Snowflake final
{
static int const TNUM {4};
GLfloat _color; // R=G=B
float _wx, _wy, _wz; // weight (0;1] by which _d are multiplied
float _p[3*(TNUM+1)] {}; // base pos x,y,z + trail (TNUM * x,y,z)
float _r[3*(TNUM+1)] {}; // base rotation x,y,z + trail (TNUM * x,y,z)
int _mblur_cnt {}; // how many of the "trail" are initialzied
float _dx, _dy, _dz; // delta x, y, z - translate
float _rdx, _rdy, _rdz; // delta x, y, z - rotate
// when -1 or +1 is reached, invert the _rd
// float _ra {}; // rotation angle
float _scale;
float _color_delta {}; // _color delta for fading out; 2^n
GLfloat _target_color; // R=G=B for fade_in
bool _fade_out {};
bool _fade_in {}; // do appear, not out of thin air
const GLfloat _v[3*4]; // vertices: 0-3 = tl, bl, br, tr
const GLfloat _t[2*4]; // uv
const GLubyte _i[4] {0, 1, 3, 2}; // indices; TRISTRIP
#ifdef _WIN32
struct rnd_init { rnd_init() { srand (time (nullptr)); } };
#else
struct rnd_init { rnd_init() { srandom (time (nullptr)); } };
#endif
// bounding box
private float
min_x{-0.25}, max_x{1.25}, min_y{-0.5}, max_y{1.5}, min_z{}, max_z{1};
private void Init()
{
// cleanup the prev. mblur
memset (_p+3, 0, 3*TNUM*sizeof(float));
memset (_r, 0, 3*(TNUM+1)*sizeof(float));
_mblur_cnt = 0;
_fade_out = false;
_wx = _wy = _wz = 1;
_dx = rndfr (TMIN, TMAX) - rndfr (TMIN, TMAX);
_dy = -rndfr (TMIN, TMAX);
_dz = (rndfr (TMIN, TMAX) - rndfr (TMIN, TMAX))/10.0;
_rdx = rndfr (RMIN, RMAX) - rndfr (RMIN, RMAX);
_rdy = rndfr (RMIN, RMAX) - rndfr (RMIN, RMAX);
_rdz = rndfr (RMIN, RMAX) - rndfr (RMIN, RMAX);
_scale = rndfr (TMIN, TMAX);
_target_color = rndfr (.5f, 1.f);
_color = 0.001f;
_color_delta = {_target_color / 256};
_fade_in = true;
_p[0] = rndfr (min_x, max_x); _p[1] = rndf (max_y); _p[2] = rndf (max_z);
// printf ("Snowflake: pos(%5.2f,%5.2f,%5.2f)\n", _p[0], _p[1], _p[2]);
// printf ("Snowflake: ro(%5.2f,%5.2f,%5.2f)\n", _rdx, _rdy, _rdz);
}
public Snowflake()
: _v {0,1,0, 0,0,0, 1,0,0, 1,1,0}, _t {0,1, 0,0, 1,0, 1,1}
{
static rnd_init __r__i__ {};
Init ();
}
public void Render()//TODO shader (as an option)
{
if (_fade_out) {
_color -= _color_delta;
if (_color < 0) return;
_color_delta += _color_delta;
}
else if (_fade_in) { // can't happen simultaneously
_color += _color_delta;
if (_color > _target_color) {
_color = _target_color;
_fade_in = false;
}
else _color_delta += _color_delta;
}
for (int t = _mblur_cnt; t >= 0; t--) {
glLoadIdentity ();
glTranslatef ((_p+3*t)[0], (_p+3*t)[1], (_p+3*t)[2]);
glTranslatef (0.5, 0.5, 0);
// glRotatef (_ra, _rx, _ry, _rz);
glRotatef ((_r+3*t)[0], 1, 0, 0);
glRotatef ((_r+3*t)[1], 0, 1, 0);
glRotatef ((_r+3*t)[2], 0, 0, 1);
glScalef (_scale, _scale, 1);
glTranslatef (-0.5, -0.5, 0);
glColor4f (_color, _color, _color, 1.0f / (t*t*t+1));
glBegin (GL_TRIANGLE_STRIP);//TODO come on; (leave this optional)
for (int i = 0; i < 4; i++)
glTexCoord2fv (&(_t[_i[i]*2])),
glVertex3fv (&(_v[_i[i]*3]));
glEnd ();
}
}
private inline Snowflake & Rotate(float & a, float & d)
{
a += d; if (a > 360) a = a-360; else if (a < -360) a = -a-360;
return * this;
}
public void Step()
{
static bool w_odd {}; // aply to half the snowflakes
static int w_frames {}, w_frames_next_change {200};
static float x_w {}, y_w {},
dy_w {0.0001}, // random cos parameter to simulate wind blows
dx_w {0.0001}; // random sin parameter to simulate wind blows
if (_color < 0 && _fade_out) Init ();
memmove (_p+3, _p, 3*TNUM*sizeof(float));
memmove (_r+3, _r, 3*TNUM*sizeof(float));
if (_mblur_cnt < TNUM) _mblur_cnt++;
Rotate (_r[0], _rdx).Rotate (_r[1], _rdy).Rotate (_r[2], _rdz);
if (w_odd) {
_wx = sin (x_w);
_wy = cos (y_w);
if (! (w_frames = (w_frames + 1) % w_frames_next_change))
dy_w = rndfr (0.00001, 0.0002),
dx_w = rndfr (0.00001, 0.0001),
w_frames_next_change = 200 + rndi (1000);
x_w += dx_w; if (x_w > 6.28) x_w = 0; if (_wx < 0) _wx = -_wx;
y_w += dy_w; if (y_w > 6.28) y_w = 0; if (_wy < 0) _wy = -_wy;
}
w_odd = ! w_odd;
_p[0] += _wx * _dx;
_p[1] += _wy * _dy;
_p[2] += _wz * _dz;
// stop disappearing out of thin air
if (_p[2] > max_z || _p[2] < min_z) _dz = -_dz, _p[2] += _dz;
if (_fade_out) return;
_fade_out = _p[1] > max_y || _p[1] < min_y
|| _p[0] < min_x || _p[0] > max_x;
if (_fade_out) _color_delta = _color / 256; // ~8 frames
}// Step
};// Snowflake
static int const WT_SIZE {100};
static int const WT_NUM {WT_SIZE*WT_SIZE};
static float WTable[WT_NUM] {};// Shall be used by the wx, wy, wz above
static float Noise(int i, int s, float & m, float & n)
{
// this is the shortest (LOC) ring buffer I've ever created
int const R = 1*WT_SIZE; // MA(R)
static float ring[R] {}, sum {};
static int ring_p {R-1}, ring_g {0}; // put and get ptr
// static float t {}, t2 {0.01}, t2_p {1};
#ifdef _WIN32
float r = rand () / static_cast<float>(RAND_MAX) * s;
#else
float r = random () / static_cast<float>(RAND_MAX) * s;
#endif
sum -= ring[ring_g];
sum += ring[ring_p] = r;
ring_p = (ring_p + 1) % R;
ring_g = (ring_g + 1) % R;
r = sum / R;
int x = i % WT_SIZE, y = i / WT_SIZE;
float tx = (x+1) / static_cast<float>(WT_SIZE),
ty = (y+1) / static_cast<float>(WT_SIZE);
//TODO variate these coefficients to create animation
tx *= 2.2/r,
ty *= 23.2/r; // shift randomly
// looks great without the "if (tx > 0.5)" above
float y_f = WT_SIZE / 2.0, x_f = y_f / 2.0;
float rr = tx*cos(x/x_f)*r - ty*cos(y/y_f)*r; // looks great
if (rr > m) m = rr;
if (rr < n) n = rr;
return rr;
}
static class Snow final
{
static int const NUM {200};
Snowflake _s[NUM];
public void Render() { for (auto & s : _s) s.Render (); }
public void Step() { for (auto & s : _s) s.Step (); }
} Snow;
static struct timespec frame_a, frame_b;
#define timespec_diff(a, b) \
((b.tv_sec - a.tv_sec)*1000000000 + (b.tv_nsec - a.tv_nsec))
//
// 1.7000 2.5000 = 8000
static void glutDisplay()
{
clock_gettime (CLOCK_REALTIME, &frame_a);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Snow.Render ();
//TODO this axis isn't scaled by the projection matrix - I'm missing
// something obvious it seems?
/*glLoadIdentity ();
// glRotatef (10,1,0,0); glRotatef (-10,0,1,0);
GLfloat a[6] {0,0,0,1,0,0};
glBegin (GL_LINES);
for (int i = 1; i < 4; i++)
glColor3fv (a+i), glVertex3fv (a+0), glVertex3fv (a+i);
glColor3fv (a+2); glVertex3fv (a+1), glVertex3f (0,0.1,1);
glColor3fv (a+3); glVertex3fv (a+1), glVertex3f (0.1,0,1);
glEnd ();*/
glutSwapBuffers ();
clock_gettime (CLOCK_REALTIME, &frame_b);
}
// auto-hide the mouse cursor on inactivity when full-screen
static time_t mouse_moved, tmouse;
static bool fs = false;
static void glutIdle()
{
if (fs) {
auto df = difftime (tmouse = time (0), mouse_moved);
if (df > 3.0)
glutSetCursor (GLUT_CURSOR_NONE);
}
if (! paused) Snow.Step ();
long frame_time = timespec_diff (frame_a, frame_b); // [nsec]
if (frame_time > 0) {
auto ftus = frame_time / 1000.0; // [usec]
// GLdouble fps = 1000000.0 / ftus;
auto const TARGET_FPS {60};
GLdouble adj = 1000000.0 / TARGET_FPS - ftus; // [usec]
/*printf ("lastframe: %0000000.5f usec, fps: %0000000.5f, adj:"
" %0000000.5f\n",
frame_time / 1000.0, fps, adj);*/
if (adj > 0)
usleep ((useconds_t)(adj));
glutPostRedisplay ();
}
else
usleep (40000), glutPostRedisplay ();
}
static void glutReshape(GLsizei width, GLsizei height)
{
if (height < 1) height = 1;
if (width < 1) width = 1;
current_viewport_height = height;
current_viewport_width = width;
glViewport (0, 0, width, height);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glFrustum (LEFT, RIGHT, BOTTOM, TOP, NEARR, FARR);
glScalef (2.0, 2.0, 1.0);
glTranslatef (-1, -1,-(NEARR + (FARR-NEARR)/2)); // glue NEAR to the screen
// glScalef (1.0, 1.0, 2.0);
glMatrixMode (GL_MODELVIEW);
// glLoadIdentity ();
}
static GLvoid glutKeyPressed(unsigned char key, int, int)
{
if ('p' == key) paused = ! paused;
else if ('q' == key) exit (0);
//D else if ('a' == key) ry += 1.f;
//D else if ('d' == key) ry -= 1.f;
else if (key == 'f') {
//TODO this doesn't seem to be taking care of NC and decorations
if (!fs) {
glutFullScreenToggle (), fs = true;
mouse_moved = time (0);
} else
glutSetCursor (GLUT_CURSOR_INHERIT), glutFullScreenToggle (),
fs = false;
}
}
static GLvoid glutPassiveMotion(int a, int b)
{
static int _pa {}, _pb {};
if (_pa != a || _pb != b) {
mouse_moved = time (0);
glutSetCursor (GLUT_CURSOR_INHERIT);
}
_pa = a; _pb = b;
}
// Bresenham's line algorithm. Nothing scary - its just fast.
static inline void Swap(int * a, int * b) { int t = *a; *a = *b; *b = t; }
static inline void Line(int x0, int y0, int x1, int y1, unsigned char * bitmap,
int w, int h, unsigned int v)
{
(void)h;
int step = abs (y1 - y0) > abs (x1 - x0);
if (step) {
Swap (&x0, &y0);
Swap (&x1, &y1);
}
if (x0 > x1) {
Swap (&x0, &x1);
Swap (&y0, &y1);
}
int deltax = x1 - x0;
int deltay = abs(y1 - y0);
int error = deltax / 2;
int ystep;
int y = y0;
int x;
if (y0 < y1) ystep = 1;
else ystep = -1;
for (x = x0; x <= x1; x++) {
if (step && x*w+y >= 0 && x*w+y < w*h) {
((unsigned int *)bitmap)[x*w + y] = v;
/*for (int i = 1 ; i < 3; i++) {
if (x * w + y+i < w*h) ((unsigned int *)bitmap)[x*w + y+i] = v;
if (x * w + y-i >= 0) ((unsigned int *)bitmap)[x*w + y-i] = v;
}*/
}
else if (y*w+x >= 0 && y*w+x < w*h) {
((unsigned int *)bitmap)[y*w + x] = v;
/*for (int i = 1 ; i < 3; i++) {
if (y * w + x+i < w*h) ((unsigned int *)bitmap)[y*w + x+i] = v;
if (y * w + x-i >= 0) ((unsigned int *)bitmap)[y*w + x-i] = v;
}*/
}
error = error - deltay;
if (error < 0) {
y = y + ystep;
error = error + deltax;
}
}
}// Line()
// Something-like-a-snowflake generator; generates to an RGBA buffer of size "a"
// with alpha = 0xaa.
class SnoflakeGen final
{
private int _a; // size "a"
private static int const _I {2}; // max iterations
struct P final // 2D point/vector - whatever role is required
{
int _x, _y;
P& operator+=(const P & p) { _x += p._x; _y += p._y; return *this; }
P operator+(const P & p) const { return P {*this} += p; }
P Translate(int x, int y) const
{
P result {*this};
result._x += x;
result._y += y;
return result;
}
// Rotate _one above, "a" [degrees], around "p". "p" is expected
// to be translated to 0,0 prior this call.
P Rotate(const P & p, float a) const
{
// y = a*x + b
float a_radians = a*MY_PI/180.0;// degrees = radians*180.0/3.14
float p_tita = p._x ? p._y
//TODO to os.h
#ifdef _WIN32
? atan (fabs (static_cast<float>(p._y)/p._x)) : 0.0 : MY_PI/2.0;
#else
? atan (abs (static_cast<float>(p._y)/p._x)) : 0.0 : MY_PI/2.0;
#endif
P result {*this};
// printf ("p.x: %d, p.y: %d, arctan: %5.2f\n", p._x, p._y, p_tita);
result._x = static_cast<int>(round (result._x *
(p._x < 0 ? -1.0 : 1.0) * cos (p_tita-a_radians)));
result._y = static_cast<int>(round (result._y *
(p._y < 0 ? -1.0 : 1.0) * sin (p_tita-a_radians)));
return result;
}
P(size_t a) : _x{static_cast<int>(a)}, _y{_x} {}
// x1 < x2 && y1 < y2
bool Outside(int x1, int y1, int x2, int y2) const
{
return _x < x1 || _x > x2 || _y < y1 || _y > y2;
}
P & operator=(const P & p) { _x = p._x; _y = p._y; return *this; }
P(const P & p) { this->operator= (p); }
P(int x, int y) : _x{x}, _y{y} {}
const int & X() { return _x; }
const int & Y() { return _y; }
};// class P final
private P _one, _c; // _one: line segment length; _c: center
//TODO there is no reason "_one" to be a "P"
private DMem<unsigned char> _bmp; // RGBA back buffer
private unsigned int _color {0xaaaaaaffu}; // guess
private void Line(const P & p1, const P & p2)
{
::Line (p1._x, p1._y, p2._x, p2._y, _bmp, _a, _a, _color);
}
private void Gen()
{
//D border
//D ::Line (0, 0, 0, _a-1, _bmp, _a, _a, 0xff808080u);
//D ::Line (0, _a-1, _a-1, _a-1, _bmp, _a, _a, 0xff808080u);
//D ::Line (_a-1, _a-1, _a-1, 0, _bmp, _a, _a, 0xff808080u);
//D ::Line (_a-1, 0, 0, 0, _bmp, _a, _a, 0xff808080u);
P up_vector {0, _c._y}; // 12 o'clock
// dance
for (int i = 0; i < 360; i += 60) {
P n {_c + _one.Rotate (up_vector, i)};
Line (_c, n);
Gen (n, _c, 0);
}
}
// p: newest point; pp: prev point; l: recursion depth
private void Gen(P & p, P & pp, int l)
{
if (p.Outside (0, 0, _a-1, _a-1)) return;
if (_I == l) return;
int d = 0;// random () % 2; perhaps someday, but not now
switch (d) {
// case 0: return;
case 0: { // extend by _one
int tx = p._x, ty = p._y;
int one_x = p._x == pp._x ? 0
: p._x < pp._x ? -_one._x : _one._x,
one_y = p._y == pp._y ? 0
: p._y < pp._y ? -_one._y : _one._y;
int nx = abs(p._x - pp._x), ny = abs(p._y - pp._y);
if (nx > ny) { // y has partial pixels
tx += one_x;
ty += nx ? static_cast<int>(round (
static_cast<float>(ny)/nx * one_y)) : 0;
}
else {
tx += ny ? static_cast<int>(round (
static_cast<float>(nx)/ny * one_x)) : 0;
ty += one_y;
}
P p2 {tx, ty};
Line (p, p2);
Gen (p2, p, l+1);
//D if (l > 0) return;
};
case 1: { // branch to 2 lines at +60 and -60 degrees
auto p2 = p + _one.Rotate (p.Translate (-_c._x, -_c._y), 60);
auto p3 = p + _one.Rotate (p.Translate (-_c._x, -_c._y), -60);
Line (p, p2); Line (p, p3);
Gen (p2, p, l+1);
Gen (p3, p, l+1);
return;
} break;
default: return;
}
}// Gen(p, pp, l)
public SnoflakeGen(size_t a)
: _a{static_cast<int>(a)}, _one {a/6}, _c {a/2}, _bmp {a*a*4}
{
Gen ();
}
public const unsigned char * Bmp() { return _bmp; }
};// SnowflakeGen
namespace __pointless_verbosity
{
struct try_finally_gl_res__ final
{
GLsizei _n;
GLuint * _t;
try_finally_gl_res__(GLsizei n, GLuint * t) : _n{n}, _t{t} {}
~try_finally_gl_res__() { if (_t && _n) glDeleteTextures (_n, _t); }
};
}
int main()
{
/*float m {}, n {}; //TODO apply the noise to wx, wy, wz above
for (int i = 0; i < WT_NUM; i++)
WTable[i] = Noise(i, WT_NUM, m, n);
for (int i = 0; i < WT_NUM; i++) {
WTable[i] = (WTable[i] - n) / (m - n); // this is part of the noise
printf ("%5.2f ", WTable[i]);
if (not ((i+1) % WT_SIZE)) printf ("\n");
}
{
DMem<unsigned char> pixel_buf_store {WT_NUM<<2};
unsigned char * pixel_buf = pixel_buf_store;
for (int i = 0; i < WT_NUM; i++) {
pixel_buf[i*4+0] = pixel_buf[i*4+1] = pixel_buf[i*4+2] =
static_cast<int>(roundf (WTable[i]*255));
pixel_buf[i*4+3] = 255;
}
BMP (WT_SIZE, WT_SIZE, pixel_buf, "noise.bmp");
}*/
const int TEX0_SZ {128};
SnoflakeGen tex0 {TEX0_SZ};
// BMP(TEX0_SZ, TEX0_SZ, tex0.Bmp (), "tex0.bmp");
int GLUT_ARGC = 0;
glutInit (&GLUT_ARGC, 0);
glutSetOption (GLUT_ACTION_ON_WINDOW_CLOSE,
GLUT_ACTION_GLUTMAINLOOP_RETURNS);
current_viewport_width = glutGet (GLUT_SCREEN_WIDTH);
current_viewport_height = glutGet (GLUT_SCREEN_HEIGHT);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_ALPHA);
glutInitWindowSize (current_viewport_width, current_viewport_height);
glutInitWindowPosition (0, 0);
glutCreateWindow ("snow gl 1.0");
// glEnable (GL_COLOR_MATERIAL);
glEnable (GL_TEXTURE_2D);
GLuint t;
glGenTextures (1, &t);
__pointless_verbosity::try_finally_gl_res__ ____ {1, &t}; // not Morse code
glBindTexture (GL_TEXTURE_2D, t);
float tex_filtering = 0.0f;
glGetFloatv (GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &tex_filtering);
// printf ("Anisotropy filter max: %5.2f\n", tex_filtering);
glTexParameterf (
GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, tex_filtering);
//TODO bell-blur the tex_t.Bmp (); say 0.1 0.1 0.2 0.3 0.5 0.8
glTexParameteri (
GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
for (int i = TEX0_SZ, j = 0; i > 0; i >>= 1) {
SnoflakeGen tex_t {static_cast<size_t>(i)};
glTexImage2D (GL_TEXTURE_2D, j++, GL_RGBA, i, i, 0, GL_BGRA,
GL_UNSIGNED_BYTE, tex_t.Bmp ());
}
glDisable (GL_LIGHTING);
glShadeModel (GL_FLAT);
glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth (1.0f);
glDisable (GL_DEPTH_TEST);
glEnable (GL_POINT_SMOOTH);
glEnable (GL_ALPHA_TEST);
glAlphaFunc (GL_GEQUAL, 0.01f);
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glutKeyboardFunc (glutKeyPressed);
glutReshapeFunc (glutReshape);
glutDisplayFunc (glutDisplay);
glutIdleFunc (glutIdle);
glutPassiveMotionFunc (glutPassiveMotion);
mouse_moved = time (nullptr);
glutMainLoop ();
return 0;
}