/
main.cpp
323 lines (291 loc) · 7.45 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h> // áèáëèîòåêà OpenGL
#include <omp.h> //áèáëèîòåêà äëÿ ïàðàëëåëüíûõ âû÷èñëåíèé
typedef struct {
GLbyte r, g, b;
} Pixel;
typedef struct {
GLdouble real, imag;
} Complex;
//ðàçìåð îêíà
#define width 1280
#define height 720
Pixel image[height][width];
GLuint texture;
Pixel dark_blue;
int window_id;
//Îáëàñòü äëÿ ðèñîâàíèÿ
double real_min = -2.9; //ëåâàÿ ãðàíèöà
double real_max = 1.4; //ïðàâàÿ ãðàíèöà
double img_min = -1.2; //âåðõíÿÿ ãðàíèöà
double img_max = img_min + (real_max - real_min)*height / width; //íèæíÿÿ ãðàíèöà
double real_factor = (real_max - real_min) / (width - 1); //ðåàëüíûé ìíîæåòåëü
double imag_factor = (img_max - img_min) / (height - 1); //ìíèìûé ìíîæèòåëü
int max = omp_get_max_threads(); //îïðåäåëÿåì ìàêñèìàëüíîå êîë-âî ïîòîêîâ
int iterations = 0, thread_count = max, step = 1, color_profile = 1;
//Îòîáðàæåíèå öâåòà
Pixel mapping[16];
void RenderFrame()
{
double real_factor = (real_max - real_min) / (width - 1);
double imag_factor = (img_max - img_min) / (height - 1);
double color_step = (double)iterations / 256;
double start_time = omp_get_wtime();
//ïàðàëëåëüíûé öèêë äëÿ âû÷èñëåíèé
#pragma omp parallel for num_threads(thread_count)
for (int y = 0; y < height; ++y)
{
double c_im = img_max - y*imag_factor;
for (unsigned x = 0; x < width; ++x)
{
double c_re = real_min + x*real_factor;
double Z_re = c_re, Z_im = c_im;
bool isInside = true;
unsigned n;
for (n = 0; n < iterations; ++n)
{
double Z_re2 = Z_re*Z_re, Z_im2 = Z_im*Z_im;
if (Z_re2 + Z_im2 > 4)
{
isInside = false;
break;
}
Z_im = 2 * Z_re*Z_im + c_im;
Z_re = Z_re2 - Z_im2 + c_re;
}
if (isInside)
{
Pixel p;
p.r = 0;
p.g = 0;
p.b = 0;
image[y][x] = p;
}
else
{
unsigned dn = n;
double color = (double)dn / color_step;
int nc = (int)color;
Pixel p;
switch (color_profile)
{
case 1:
if (n < iterations && n > 0)
{
int id = n % 16;
p = mapping[id];
}
break;
case 2:
p.r = 0;
p.g = nc;
p.b = 0;
break;
case 3:
p.r = 0;
p.g = 0;
p.b = nc;
break;
case 4:
p.r = nc;
p.g = 0;
p.b = nc;
break;
case 5:
p.r = nc;
p.g = 0;
p.b = 0;
break;
default:
break;
}
image[y][x] = p;
}
}
}
double end_time = omp_get_wtime();
printf("Render time: %f\n", end_time - start_time);
}
//Îïðåäåëåíèå öâåòà äëÿ ïîêàçà ðàçëè÷íûõ óðîâíåé
void init_color_mapping()
{
mapping[0].r = 66; mapping[0].g = 30; mapping[0].b = 15;
mapping[1].r = 25; mapping[1].g = 7; mapping[1].b = 26;
mapping[2].r = 9; mapping[2].g = 1; mapping[2].b = 47;
mapping[3].r = 4; mapping[3].g = 4; mapping[3].b = 73;
mapping[4].r = 0; mapping[4].g = 7; mapping[4].b = 100;
mapping[5].r = 12; mapping[5].g = 44; mapping[5].b = 138;
mapping[6].r = 24; mapping[6].g = 82; mapping[6].b = 177;
mapping[7].r = 57; mapping[7].g = 125; mapping[7].b = 209;
mapping[8].r = 134; mapping[8].g = 181; mapping[8].b = 229;
mapping[9].r = 211; mapping[9].g = 236; mapping[9].b = 248;
mapping[10].r = 241; mapping[10].g = 233; mapping[10].b = 191;
mapping[11].r = 248; mapping[11].g = 201; mapping[11].b = 95;
mapping[12].r = 255; mapping[12].g = 170; mapping[12].b = 0;
mapping[13].r = 204; mapping[13].g = 128; mapping[13].b = 0;
mapping[13].r = 153; mapping[13].g = 87; mapping[13].b = 0;
mapping[13].r = 106; mapping[13].g = 52; mapping[13].b = 3;
}
// Èíèöèèëèçàöèÿ ñîñòîÿíèé OpenGL
void init()
{
// Íàñòðîéêà òåêñòóð
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
// Äðóãîå
glClearColor(0, 0, 0, 0);
gluOrtho2D(-1, 1, -1, 1);
glLoadIdentity();
glColor3f(1, 1, 1);
init_color_mapping();
}
// Ãåíåðàöèÿ è îòîáðàæåíèå èçîáðàæåíèÿ
void display()
{
// Âûçîâ ïîëüçîâàòåëåì ãåíåðàöèè èçîáðàæåíèÿ
RenderFrame();
// Êîïèðîâàíèÿ òåêñòóð èçîáðàæåíèÿ â ïàìÿòü
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
// Î÷èñòêà áóôåðà
glClear(GL_COLOR_BUFFER_BIT);
// Ðåíäåð
glBegin(GL_QUADS);
glTexCoord2f(1, 0); glVertex2f(1, -1);
glTexCoord2f(1, 1); glVertex2f(1, 1);
glTexCoord2f(0, 1); glVertex2f(-1, 1);
glTexCoord2f(0, 0); glVertex2f(-1, -1);
glEnd();
// Îòîáðàæåíèå ðåçóëüòàòàò
glFlush();
glutSwapBuffers();
}
//Óïðàâëåíèå ñ êëàâèàòóðû
void keypress(unsigned char key, int x, int y)
{
double real_diff = fabs(real_min - real_max) * 0.05;
double img_diff = fabs(img_min - img_max) * 0.05;
switch (key)
{
//Ïðèáëèæåíèå
case 'S':
case 's':
real_min -= real_diff;
real_max += real_diff;
img_min -= img_diff;
img_max += img_diff;
break;
//Îòäàëåíèå
case 'W':
case 'w':
real_min += real_diff;
real_max -= real_diff;
img_min += img_diff;
img_max -= img_diff;
break;
//Óìåíüøèòü êîë-âî èòåðàöèé
case 'A':
case 'a':
if (iterations > step) iterations -= step;
printf("Iterations:\t%d\n", iterations);
break;
//Óâåëè÷èòü êîë-âî èòåðàöèé
case 'D':
case 'd':
iterations += step;
printf("Iterations:\t%d\n", iterations);
break;
//Óâåëè÷ü êîë-âî øàãîâ
case 'E':
case 'e':
step++;
printf("Step:\t%d\n", step);
break;
//Óìåíüøèòü êîë-âî øàãîâ
case 'Q':
case 'q':
if (step > 1) step--;
printf("Step:\t%d\n", step);
break;
//Óâåëè÷èòü êîë-âî èñïîëüçîâàííûõ ïîòîêîâ
case 'L':
case 'l':
thread_count += 1;
printf("Threads:\t%d\n", thread_count);
break;
//Óìåíüøèòü êîë-âî èñïîëüçîâàííûõ ïîòîêîâ
case 'K':
case 'k':
if (thread_count > 1) thread_count -= 1;
printf("Threads:\t%d\n", thread_count);
break;
//Ïîìåíÿòü öâåò
case 'C':
case 'c':
color_profile++;
if (color_profile > 5) color_profile = 1;
printf("Color profile changed\n");
break;
//Âûõîä
case 27:
glutDestroyWindow(window_id);
exit(EXIT_SUCCESS);
break;
default:
break;
}
// Îáíîâëåíèÿ îêíà
glutPostRedisplay();
}
void SpecialKeys(int key, int x, int y)
{
double realDif = fabs(real_min - real_max) * 0.05;
double imagDif = fabs(img_min - img_max) * 0.05;
switch (key)
{
case (char)GLUT_KEY_UP:
img_min -= imagDif;
img_max -= imagDif;
break;
case GLUT_KEY_DOWN:
img_min += imagDif;
img_max += imagDif;
break;
case GLUT_KEY_RIGHT:
real_min += realDif;
real_max += realDif;
break;
case GLUT_KEY_LEFT:
real_min -= realDif;
real_max -= realDif;
break;
default:
break;
}
// Îáíîâëåíèÿ îêíà
glutPostRedisplay();
}
// Main
int main(int argc, char ** argv)
{
// Èíèöèëèçàöèÿ GLUT
glutInit(&argc, argv);
glutInitWindowSize(width, height);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
window_id = glutCreateWindow("Mandelbrot Set");
// Çàïóñê ôóíêöèé äëÿ óïðàâëåíèÿ
glutSpecialFunc(SpecialKeys);
glutKeyboardFunc(keypress);
glutDisplayFunc(display);
init();
// Çàïóñê ìåõàíèçìà îáðàáîòêè ñîáûòèé
glutMainLoop();
return EXIT_SUCCESS;
}