-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDemo.cpp
268 lines (238 loc) · 7.33 KB
/
Demo.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
#include <QApplication>
#include <QMenu>
#include <QMenuBar>
#include <QStatusBar>
#include <QKeyEvent>
#include <QHBoxLayout>
#include <QVector3D>
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <sstream>
#include <limits>
#include <map>
#include <vector>
#include "Demo.h"
#include "Util.h"
#define WIN_WIDTH 1080
#define WIN_HEIGHT 720
CGMainWindow::CGMainWindow (QWidget* parent) : QMainWindow (parent) {
resize(WIN_WIDTH,WIN_HEIGHT);
QFrame* f = new QFrame (this);
f->setFrameStyle(QFrame::Sunken | QFrame::Panel);
f->setLineWidth(2);
ogl = new MyGLWidget(f, this);
QHBoxLayout* layout = new QHBoxLayout();
layout->addWidget(ogl);
layout->setMargin(0);
f->setLayout(layout);
setCentralWidget(f);
}
void MyGLWidget::updateAnimation() {
if (mandelbrotScaling >= 2.0) {
increasing = false;
} else if (mandelbrotScaling <= -2.0) {
increasing = true;
}
if (increasing) {
mandelbrotScaling += 0.01f;
} else {
mandelbrotScaling -= 0.01f;
}
update();
}
void MyGLWidget::initShader(QOpenGLShaderProgram& shader, std::string const& vertex, std::string const& fragment) {
setlocale(LC_NUMERIC,"C");
if (!shader.addShaderFromSourceFile(QOpenGLShader::Vertex, vertex.c_str())) close();
if (!shader.addShaderFromSourceFile(QOpenGLShader::Fragment, fragment.c_str())) close();
if (!shader.link()) close();
if (!shader.bind()) close();
setlocale(LC_ALL,"");
}
void MyGLWidget::initShaders() {
initShader(simpleShader, ":/SimpleVertex.glsl", ":/SimpleFragment.glsl");
initShader(juliaShader, ":/SimpleVertex.glsl", ":/JuliaFragment.glsl");
}
std::pair<GLuint,GLuint> MyGLWidget::makeVAOfromAttributes(std::vector<QVector3D> const& attributes) {
GLuint vboId;
glGenBuffers(1,&vboId);
glBindBuffer(GL_ARRAY_BUFFER,vboId);
glBufferData(GL_ARRAY_BUFFER,attributes.size()*sizeof(QVector3D),&attributes[0],GL_STATIC_DRAW);
GLuint vaoId;
glGenVertexArrays(1,&vaoId);
glBindVertexArray(vaoId);
{
//pixelColor changes from lecture
constexpr int numAttribs = 2;
constexpr int stride = numAttribs * sizeof(QVector3D);
glBindBuffer(GL_ARRAY_BUFFER,vboId);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, 0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, stride, (GLvoid const*)(sizeof(QVector3D)));
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
}
glBindVertexArray(0);
return {vaoId,vboId};
}
void MyGLWidget::drawTriangles(GLuint vao, size_t size) {
if(size <= 0) return;
{ // draw mandelbrot
QMatrix4x4 scalingMat{getScalingMatrix(false)};
QMatrix4x4 translationMat{getTranslationMatrix(false)};
simpleShader.bind();
simpleShader.setUniformValue("drawingJulia", false);
simpleShader.setUniformValue("rhombusColor", QVector3D(1.,1.,1.));
{
//rhombusPosition
simpleShader.setUniformValue("rhombusPosition", QVector3D(juliaC.x(), juliaC.y(), 0));
simpleShader.setUniformValue("scalar", mandelbrotScaling);
}
glBindVertexArray(vao);
{
simpleShader.setUniformValue("trafo", translationMat * scalingMat);
glDrawArrays(GL_TRIANGLES,0,(GLsizei)size);
}
glBindVertexArray(0);
}
{ // draw lulia
QMatrix4x4 scalingMat{getScalingMatrix(true)};
QMatrix4x4 translationMat{getTranslationMatrix(true)};
juliaShader.bind();
juliaShader.setUniformValue("drawingJulia", true);
juliaShader.setUniformValue("c", juliaC);
juliaShader.setUniformValue("trafo", translationMat * scalingMat);
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, (GLsizei)size);
glBindVertexArray(0);
}
}
void MyGLWidget::initializeGL() {
initializeOpenGLFunctions();
initShaders();
auto format = this->format();
std::cout << "OpenGL version: " << format.majorVersion() << "." << format.minorVersion() << std::endl;
{
auto vert = util::genTriangle();
std::tie(vaoTri,vboTri) = makeVAOfromAttributes(vert);
}
glClearColor(0.8f, 0.8f, .8f, 1.0f);
}
void MyGLWidget::paintGL() {
glClear(GL_COLOR_BUFFER_BIT);
drawTriangles(vaoTri, 6);
}
void MyGLWidget::resizeGL(int width, int height) {
glViewport(0,0,width,height);
update();
}
void MyGLWidget::wheelEvent(QWheelEvent*) {
update();
}
void MyGLWidget::mouseMoveEvent(QMouseEvent* event) {
mousePressEvent(event);
//update();
}
void MyGLWidget::mousePressEvent(QMouseEvent *event) {
button = event->button();
mouseX = event->x();
mouseY = event->y();
QMatrix4x4 scalingMat{getScalingMatrix(false)};
QMatrix4x4 translationMat{getTranslationMatrix(false)};
QMatrix4x4 combined = translationMat * scalingMat;
QVector3D compCoords{pxToCompCoords(mouseX, mouseY)};
juliaC = combined * compCoords;
updateTitle();
update();
}
void MyGLWidget::mouseReleaseEvent(QMouseEvent*) {}
void MyGLWidget::keyPressEvent(QKeyEvent* event) {
switch(event->key()) {
case Qt::Key_Space:
drawJulia = !drawJulia;
break;
case Qt::Key_Up:
updateTranslation({0.0f, 0.05f, 0.0f});
break;
case Qt::Key_Down:
updateTranslation({0.0f, -0.05f, 0.0f});
break;
case Qt::Key_Left:
updateTranslation({-0.05f, 0.0f, 0.0f});
break;
case Qt::Key_Right:
updateTranslation({0.05f, 0.0f, 0.0f});
break;
case Qt::Key_N:
updateScaling(1.1f);
break;
case Qt::Key_M:
updateScaling(0.9f);
break;
default: QWidget::keyPressEvent(event);
}
updateTitle();
update();
}
void MyGLWidget::updateTitle() {
QString title{ "Active side: " };
if (drawJulia) {
title += "Julia";
} else {
title += "Mandelbrot";
}
title += " ";
title += QString::number(juliaC.x()) + " + " + QString::number(juliaC.y()) + "i";
mainWin->setWindowTitle(title);
}
void MyGLWidget::updateScaling(float change) {
if (drawJulia) {
juliaScaling *= change;
} else {
mandelbrotScaling *= change;
}
}
void MyGLWidget::updateTranslation(QVector3D change) {
if (drawJulia) {
juliaTranslation += change * juliaScaling;
} else {
mandelbrotTranslation += change * mandelbrotScaling;
}
}
QMatrix4x4 MyGLWidget::getScalingMatrix(bool forJulia) {
float scaling;
if (forJulia) {
scaling = juliaScaling;
} else {
scaling = mandelbrotScaling;
}
QMatrix4x4 ret;
ret.scale(scaling);
return ret;
}
QMatrix4x4 MyGLWidget::getTranslationMatrix(bool forJulia) {
QVector3D translation;
if (forJulia) {
translation = juliaTranslation;
} else {
translation = mandelbrotTranslation;
}
QMatrix4x4 ret;
ret.translate(translation);
return ret;
}
QVector2D MyGLWidget::pxToCompCoords(int x, int y) {
float ret_x = -2.0 + 6.0 * static_cast<float>(x) / width();
float ret_y = 1.0 - 4.0 * (static_cast<float>(y) - height() / 4.0) / height();
return {ret_x, ret_y};
}
int main (int argc, char **argv) {
QSurfaceFormat glFormat;
glFormat.setVersion(3,3);
glFormat.setProfile(QSurfaceFormat::CoreProfile);
QSurfaceFormat::setDefaultFormat(glFormat);
QApplication app(argc, argv);
CGMainWindow w(nullptr);
w.show();
return app.exec();
}