-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
226 lines (178 loc) · 7.66 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
#define __CL_ENABLE_EXCEPTIONS
#include <complex>
#include <iostream>
#include <memory>
#include <regex>
#include <tuple>
#include "Bounds2D.h"
#include "Screen_Stream.h"
#include "Image_Stream.h"
#include "Pixel_Stream_Base.h"
#include "Pixel_Calculator.h"
RGB retrieve_color(const std::regex&);
std::tuple<float,
float,
float,
float> retrieve_draw_pos(const std::regex&);
// Set stream to image mode
void initiate_png_mode(std::shared_ptr<Pixel_Stream_Base>&, std::shared_ptr<Bounds2D<int>>&);
// Set stream to opengl mode
void initiate_opengl_mode(std::shared_ptr<Pixel_Stream_Base>&, std::shared_ptr<Bounds2D<int>>&);
int main() {
std::cout << "Running mandelbrot-fractal-drawer by Balajanovski..." << std::endl;
// Use regex to get the user's desired fractal colour
std::regex retrieve_RGB_pattern("\\{ *([0-9]{1,3}) *, *([0-9]{1,3}) *, *([0-9]{1,3}) *\\}");
RGB chosen_color;
while (true) {
std::cout << "\nPlease enter the color you want the fractal to be drawn in RGB format: {R,G,B}.\nNumbers may be from 0 to 255" << std::endl;
try {
chosen_color = retrieve_color(retrieve_RGB_pattern);
}
// If the input is invalid ask the user to input again
catch (std::runtime_error e) {
std::cout << e.what() << std::endl;
continue;
}
break;
}
// Use regex to retrieve the user's desired complex plane
// If input is invalid ask the user to
std::regex retrieve_complex_pos_pattern("\\([ ]*([0-9.-]+) *,[ ]*([0-9.-]+) *,[ ]*([0-9.-]+) *,[ ]*([0-9.-]+) *\\)");
std::tuple<float, float, float, float> chosen_range;
while (true) {
std::cout << "\nPlease enter the range of complex numbers within which you want the fractal to " <<
"be drawn (real_min, real_max, imag_min, imag_max).\nSuggested input: (-2, 3.25, -1.25, 2.5)" << std::endl;
try {
chosen_range = retrieve_draw_pos(retrieve_complex_pos_pattern);
}
// If the input is invalid ask the user to input again
catch (std::runtime_error e) {
std::cout << e.what() << std::endl;
continue;
}
break;
}
// Declare window object to represent the complex plane with user's input
Bounds2D<float> complex_plane(std::get<0>(chosen_range), std::get<1>(chosen_range),
std::get<2>(chosen_range), std::get<3>(chosen_range));
// Declare window object to represent the OpenGL window
auto window = std::make_shared<Bounds2D<int>>(0, 1000, 0, 1000);
// Create a pointer to a generic pixel buffer
// To exploit polymorphic behavior
std::shared_ptr<Pixel_Stream_Base> pixel_buffer;
// Use preprocessor macros to ensure that the program still runs if the user has one of the
// libraries but not another
#if defined(GLEW_FOUND) && defined(PNG_FOUND) && defined(GLFW_FOUND) && defined(OPENGL_FOUND)
std::cout << "\nWould you like to draw fractal to a window or an image?\n"
<< "Type W for window or I for image" << std::endl;
char response;
while (!(std::cin >> response))
;
switch (response) {
case 'W' : case 'w' :
{
initiate_opengl_mode(pixel_buffer, window);
}
break;
case 'I' : case 'i' :
{
initiate_png_mode(pixel_buffer, window);
}
break;
default:
{
std::cout << "\nInvalid response: Shutting down" << std::endl;
exit(EXIT_FAILURE);
}
break;
}
#elif defined(GLEW_FOUND) && defined(GLFW_FOUND) && defined(OPENGL_FOUND)
std::cout << "\nwarning: libPNG not detected\nInitiating GLEW (Windowed) mode" << std::endl;
initiate_opengl_mode(pixel_buffer, window);
#elif defined(PNG_FOUND)
std::cout << "\nwarning: GLEW not detected\nInitiating libPNG (Image) mode" << std::endl;
initiate_png_mode(pixel_buffer, window);
#else
std::cout << "\nfatal error: neither GLEW or libPNG were detected\nClosing down..." << std::endl;
exit(EXIT_FAILURE);
#endif
// Create a pixel calculator to use the GPU to accelerate the calculation
Pixel_Calculator pixel_calculator("accelerated_calculation.cl", pixel_buffer, chosen_color, complex_plane);
pixel_calculator.calculate();
pixel_buffer->flush();
std::cout << "\nClosing down..." << std::endl;
}
RGB retrieve_color(const std::regex& pattern) {
std::string input;
std::getline(std::cin, input);
std::smatch regex_results;
RGB processed_results;
if (std::regex_match(input, regex_results, pattern)) {
// Make sure input values are not greater than 255
if (std::stoi(regex_results[1].str()) > 255 ||
std::stoi(regex_results[2].str()) > 255 ||
std::stoi(regex_results[3].str()) > 255)
{
throw std::runtime_error("error: RGB values must be between 0 and 255");
}
processed_results.r = static_cast<uint8_t>(std::stoi(regex_results[1].str()));
processed_results.g = static_cast<uint8_t>(std::stoi(regex_results[2].str()));
processed_results.b = static_cast<uint8_t>(std::stoi(regex_results[3].str()));
}
else {
throw std::runtime_error("error: invalid RGB sequence input");
}
return processed_results;
}
std::tuple<float,
float,
float,
float> retrieve_draw_pos(const std::regex& pattern) {
std::string input;
std::getline(std::cin, input);
// Use regex to retrieve the draw pos
std::smatch regex_results;
std::tuple<float, float, float, float> processed_results;
if (std::regex_match(input, regex_results, pattern)) {
processed_results =
std::make_tuple(
std::stof(regex_results[1].str()),
std::stof(regex_results[2].str()),
std::stof(regex_results[3].str()),
std::stof(regex_results[4].str())
);
}
else {
throw std::runtime_error("error: invalid complex plane input");
}
// If the x_min is greater than the x_max and y_min is greater than the y_max
if (std::get<0>(processed_results) > std::get<1>(processed_results)
&& std::get<2>(processed_results) > std::get<3>(processed_results)) {
throw std::runtime_error("error: x_min cannot be larger than x_max and y_min cannot be larger than y_max");
}
// If the x_min is greater than the x_max
else if (std::get<0>(processed_results) > std::get<1>(processed_results)) {
throw std::runtime_error("error: x_min cannot be larger than x_max");
}
// If the y_min is greater than the y_max
else if (std::get<2>(processed_results) > std::get<3>(processed_results)) {
throw std::runtime_error("error: y_min cannot be larger than y_max");
}
return processed_results;
}
// Set stream to image mode
void initiate_png_mode(std::shared_ptr<Pixel_Stream_Base>& pixel_buffer,
std::shared_ptr<Bounds2D<int>>& window) {
std::cout << "\nPlease enter the location to where you want the fractal to be drawn" << std::endl;
std::string src;
while (!(std::cin >> src))
;
// Initialise pointer to an image buffer
pixel_buffer.reset(new Image_Stream(window, src));
}
// Set stream to opengl mode
void initiate_opengl_mode(std::shared_ptr<Pixel_Stream_Base>& pixel_buffer,
std::shared_ptr<Bounds2D<int>>& window) {
// Initialise pointer to a draw buffer
pixel_buffer.reset(new Screen_Stream(window, "vertex_shader.glsl", "fragment_shader.glsl"));
}