added window resize

master
Benjamin Kraft 3 years ago
parent 2fe9bcd9c1
commit 45a490f395
  1. 1
      headers/Mandelbrot.h
  2. 5
      headers/OutputWidget.h
  3. 2
      src/MainWindow.cpp
  4. 4
      src/Mandelbrot.cpp
  5. 34
      src/OutputWidget.cpp

@ -12,6 +12,7 @@ public:
void setIterations(int);
void translateRelative(QVector2D);
void zoomRelative(int, QVector2D);
void resizeRelative(QVector2D);
private:
int iterations = 0;
QVector2D origin {-2, -1};

@ -12,7 +12,7 @@ class OutputWidget : public QOpenGLWidget, protected QOpenGLExtraFunctions {
Q_OBJECT
public:
explicit OutputWidget(QWidget* parent): QOpenGLWidget(parent) {}
Mandelbrot* getMandelbrot() {return &mandelbrot;}
Mandelbrot& getMandelbrot() {return mandelbrot;}
private:
void initializeGL() override;
void paintGL() override;
@ -25,11 +25,12 @@ private:
void keyPressEvent(QKeyEvent*) override;
QPoint mousePos;
QSize oldSize {0, 0};
void initShader();
static std::vector<QVector2D> genVertices();
GLuint createVAO();
GLuint vao{};
GLuint vao {};
QOpenGLShaderProgram shader;
Mandelbrot mandelbrot;

@ -22,7 +22,7 @@ void MainWindow::buildUI() {
&QSlider::valueChanged,
this,
[iterationsLabel, this](int value) -> void {
this->outputWidget->getMandelbrot()->setIterations(value);
this->outputWidget->getMandelbrot().setIterations(value);
this->outputWidget->update();
iterationsLabel->setNum(value);
});

@ -37,6 +37,10 @@ void Mandelbrot::translateRelative(QVector2D relative) {
origin -= absolute;
}
void Mandelbrot::resizeRelative(QVector2D relative) {
size *= relative;
}

@ -7,7 +7,7 @@ using std::cout, std::endl;
void OutputWidget::initializeGL() {
cout << "Initialize OpenGL" << endl;
initializeOpenGLFunctions();
getMandelbrot()->init();
getMandelbrot().init();
initShader();
auto format = this->format();
@ -19,11 +19,7 @@ void OutputWidget::initializeGL() {
}
void OutputWidget::paintGL() {
getMandelbrot()->draw(vao, shader);
}
void OutputWidget::resizeGL(int w, int h) {
getMandelbrot().draw(vao, shader);
}
GLuint OutputWidget::createVAO() {
@ -64,19 +60,35 @@ void OutputWidget::initShader() {
if (!shader.bind()) close();
}
QVector2D relative(QPoint p, QSize s){
QVector2D divide(QPoint p, QSize s){
return {
float(p.x()) / float(s.width()),
float(p.y()) / float(s.height())
};
}
QVector2D divide(QSize s1, QSize s2){
return divide(QPoint(s1.width(), s1.height()), s2);
}
void OutputWidget::resizeGL(int w, int h) {
QSize newSize = QSize(w, h);
if (oldSize == QSize(0, 0)){
oldSize = newSize;
return;
}
QSize diff = oldSize - newSize;
QVector2D relative = QVector2D(1, 1) - divide(diff, oldSize);
getMandelbrot().resizeRelative(relative);
oldSize = newSize;
}
void OutputWidget::wheelEvent(QWheelEvent *e) {
QPoint pos = e->position().toPoint();
int direction = e->angleDelta().y() > 0 ? 1 : -1;
QVector2D relativePos = relative(pos, size());
QVector2D relativePos = divide(pos, size());
relativePos.setY(1 - relativePos.y());
getMandelbrot()->zoomRelative(direction, relativePos);
getMandelbrot().zoomRelative(direction, relativePos);
update();
}
@ -84,9 +96,9 @@ void OutputWidget::mouseMoveEvent(QMouseEvent *e) {
QPoint newMousePos = e->pos();
QPoint diff = newMousePos - mousePos;
mousePos = newMousePos;
QVector2D relativeDiff = relative(diff, size());
QVector2D relativeDiff = divide(diff, size());
relativeDiff.setY(-relativeDiff.y());
getMandelbrot()->translateRelative(relativeDiff);
getMandelbrot().translateRelative(relativeDiff);
update();
}

Loading…
Cancel
Save