-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlasmaFractal.cpp
278 lines (230 loc) · 5.22 KB
/
PlasmaFractal.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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <Windows.h>
#include <gl/GL.h>
#include <GL/glut.h>
const int size = 513; // This is required to be 2^n + 1
int range = 196; // Degree of randomness / roughness. Larger values generate rougher images.
// The 2D array of integer values into which our fractal will be written
int map[size][size];
// Random helper
int rnd(int min = 0, int max = 255)
{
return min + (rand() % static_cast<int>(max - min + 1));
}
// Init corner values
void init()
{
map[0][0] = rnd();
map[0][size - 1] = rnd();
map[size - 1][0] = rnd();
map[size - 1][size - 1] = rnd();
}
// Diamond step
void diamond(int sideLength)
{
int halfSide = sideLength / 2;
for (int y = 0; y < size / (sideLength-1); y++)
{
for (int x = 0; x < size / (sideLength-1); x++)
{
int center_x = x*(sideLength-1) + halfSide;
int center_y = y*(sideLength-1) + halfSide;
int avg = (map[x*(sideLength - 1)][y*(sideLength - 1)] +
map[x*(sideLength - 1)][(y+1) * (sideLength - 1)] +
map[(x + 1) * (sideLength - 1)][y*(sideLength - 1)] +
map[(x + 1) * (sideLength - 1)][(y + 1) * (sideLength - 1)])
/ 4.0f;
map[center_x][center_y] = avg + rnd(-range, range);
}
}
}
// Averaging helper function for square step to ignore out of bounds points
void average(int x, int y, int sideLength)
{
float counter = 0;
float accumulator = 0;
int halfSide = sideLength / 2;
if (x != 0)
{
counter += 1.0f;
accumulator += map[y][x - halfSide];
}
if (y != 0)
{
counter += 1.0f;
accumulator += map[y - halfSide][x];
}
if (x != size - 1)
{
counter += 1.0f;
accumulator += map[y][x + halfSide];
}
if (y != size - 1)
{
counter += 1.0f;
accumulator += map[y + halfSide][x];
}
map[y][x] = (accumulator / counter) + rnd(-range, range);
}
// Square step
void square(int sideLength)
{
int halfLength = sideLength / 2;
for (int y = 0; y < size / (sideLength - 1); y++)
{
for (int x = 0; x < size / (sideLength - 1); x++)
{
// Top
average(x*(sideLength - 1) + halfLength, y*(sideLength - 1), sideLength);
// Right
average((x + 1)*(sideLength - 1), y*(sideLength - 1) + halfLength, sideLength);
// Bottom
average(x*(sideLength - 1) + halfLength, (y+1)*(sideLength - 1), sideLength);
// Left
average(x*(sideLength - 1), y*(sideLength - 1) + halfLength, sideLength);
}
}
}
// Main fractal generating loop
void fractal()
{
int sideLength = size/2;
diamond(size);
square(size);
range /= 2;
while (sideLength >= 2)
{
diamond(sideLength + 1);
square(sideLength + 1);
sideLength /= 2;
range /= 2;
}
}
// Integer clamping helper
void clamp(int* val, int min, int max)
{
if (*val < min) *val = min;
if (*val > max) *val = max;
}
// Function to clamp all map values
void clamp_map()
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
clamp(&map[i][j], 0, 255);
}
}
}
// Debug print of the 2D array
void print_map()
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
std::cout << std::setw(10) << map[i][j];
}
std::cout << std::endl;
}
}
// File saving helper, saves to a PGM file (See https://en.wikipedia.org/wiki/Netpbm_format)
void save_to_file()
{
std::ofstream file_out("test.pgm", std::ios_base::out
| std::ios_base::binary
| std::ios_base::trunc
);
file_out << "P2" << "\n" << size << "\n" << size << "\n" << 255 << "\n";
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
file_out << map[i][j] << " ";
}
file_out << std::endl;
}
file_out.close();
}
// OpenGL rendering function
void render()
{
int color;
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(3);
glBegin(GL_POINTS);
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
color = map[j][i];
glColor3ub(color, color, color);
glVertex2f(i * 100.0f/size, j * 100.0f/size);
}
}
glEnd();
glFlush();
}
// Window resize handler with Aspect Ratio correction
void ChangeSize(GLsizei horizontal, GLsizei vertical)
{
GLfloat AspectRatio;
// Division by zero guard
if (vertical == 0)
vertical = 1;
glViewport(0, 0, horizontal, vertical);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
AspectRatio = (GLfloat)horizontal / (GLfloat)vertical;
if (horizontal <= vertical)
glOrtho(0, 100.0, 100.0 / AspectRatio, 0, 1.0, -1.0);
else
glOrtho(0, 100.0*AspectRatio, 100.0, 0, 1.0, -1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
// Handle key presses
void handle_input(unsigned char key, int x, int y)
{
switch (key)
{
// Quit the program on escape key press
case 27:
glutDestroyWindow(glutGetWindow());
exit(0);
break;
// Generate a new fractal on space key press
case 32:
range = 196;
init();
fractal();
clamp_map();
glutPostRedisplay();
save_to_file();
break;
}
}
int main(int argc, char * argv[])
{
glutInit(&argc, argv);
srand(time(NULL));
init();
fractal();
clamp_map();
//print_map();
save_to_file();
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutCreateWindow("Plasma fractal");
glutFullScreen();
// Pass function to call on keyboard input
glutKeyboardFunc(handle_input);
// Pass function to call on rerender
glutDisplayFunc(render);
// Pass function to call on window size change
glutReshapeFunc(ChangeSize);
init();
glutMainLoop();
}