-
Notifications
You must be signed in to change notification settings - Fork 2
/
mandelbrot.cpp
233 lines (175 loc) · 4.84 KB
/
mandelbrot.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
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <QApplication>
#include <QString>
/*#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>*/
#include "mandelbrot.hpp"
const double PanSpeed = 0.005;
const double RotateSpeed = 0.4;
RenderWidget::RenderWidget(const QGLFormat &format)
: QGLWidget(format, (QWidget*) 0)
, tex(0)
, curSize(-1, -1)
, texSize(-1, -1)
, scale(0.)
, panX(0.), panY(0.)
{
grabKeyboard();
}
#include "shaders/quad.glsl.hpp"
#include "shaders/palette.glsl.hpp"
#include "shaders/mandelbrot.glsl.hpp"
void RenderWidget::compileShaders()
{
const int version = 430;
progQuad.link(
VertexShader(version, {shaders::quad}),
FragmentShader(version, {shaders::quad}));
progCompute.link(
ComputeShader(version, {shaders::palette, shaders::mandelbrot}));
}
void RenderWidget::initializeGL()
{
std::cout << "OpenGL version: " << glGetString(GL_VERSION)
<< ", renderer: " << glGetString(GL_RENDERER) << std::endl;
GLint major, minor;
glGetIntegerv(GL_MAJOR_VERSION, &major);
glGetIntegerv(GL_MINOR_VERSION, &minor);
if (major*10 + minor < 43) {
throw std::runtime_error(
"OpenGL version 4.3 or higher is required to run this program.");
}
compileShaders();
glGenVertexArrays(1, &vao); // create an empty VAO
}
void RenderWidget::resizeGL(int width, int height)
{
if (curSize == QSize(-1, -1))
{
panX = width * 0.75;
panY = height * 0.5;
scale = 2.0 / height;
}
glViewport(0, 0, width, height);
curSize = QSize(width, height);
aspect = (double) width / height;
}
void RenderWidget::createTexture(QSize size)
{
if (tex) {
glDeleteTextures(1, &tex);
}
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, size.width(), size.height(),
0, GL_RGBA, GL_FLOAT, NULL);
}
void RenderWidget::paintGL()
{
if (curSize != texSize) {
createTexture(curSize);
texSize = curSize;
}
progCompute.use();
glUniform2f(progCompute.uniform("center"), panX, panY);
glUniform1f(progCompute.uniform("scale"), scale);
glBindImageTexture(0, tex, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA32F);
int lsize[3];
progCompute.localSize(lsize);
int ngroups[3];
ngroups[0] = (texSize.width() + lsize[0]-1) / lsize[0];
ngroups[1] = (texSize.height() + lsize[1]-1) / lsize[1];
ngroups[2] = 1;
glDispatchCompute(ngroups[0], ngroups[1], ngroups[2]);
// prevent sampling before all writes to texture are done
glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
// display the output of the compute shader
progQuad.use();
glUniform1i(progQuad.uniform("sampler"), 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
}
void RenderWidget::mousePressEvent(QMouseEvent *event)
{
lastPos = event->pos();
centerPos = QPoint(lastPos.x(), curSize.height() - lastPos.y());
}
void RenderWidget::zoom(double delta)
{
double cx = scale*(centerPos.x() - panX);
double cy = scale*(centerPos.y() - panY);
scale *= pow(1.01, delta);
panX = (scale*centerPos.x() - cx) / scale;
panY = (scale*centerPos.y() - cy) / scale;
}
void RenderWidget::mouseMoveEvent(QMouseEvent *event)
{
double deltaX(event->x() - lastPos.x());
double deltaY(event->y() - lastPos.y());
bool leftButton();
bool rightButton(event->buttons() & Qt::RightButton);
if (event->buttons() & Qt::RightButton)
{
zoom(deltaY/2);
}
else if (event->buttons() & Qt::LeftButton)
{
panX += deltaX;
panY -= deltaY;
}
lastPos = event->pos();
updateGL();
}
void RenderWidget::wheelEvent(QWheelEvent *event)
{
centerPos = QPoint(event->x(), curSize.height() - event->y());
zoom(-double(event->delta()) / 10);
updateGL();
}
void RenderWidget::keyPressEvent(QKeyEvent * event)
{
switch (event->key())
{
case Qt::Key_Q:
parentWidget()->close();
break;
case Qt::Key_Left:
break;
case Qt::Key_Right:
break;
case Qt::Key_Minus:
break;
case Qt::Key_Plus:
break;
}
updateGL();
}
MainWindow::MainWindow(QWidget* gl)
{
setCentralWidget(gl);
setWindowTitle("mandelbrot");
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QGLFormat glf = QGLFormat::defaultFormat();
/*glf.setSampleBuffers(true);
glf.setSamples(8);*/
RenderWidget* gl =
new RenderWidget(glf);
MainWindow wnd(gl);
gl->setParent(&wnd);
QSize size(1200, 900);
wnd.resize(size);
wnd.show();
return app.exec();
}