added translating

master
Benjamin Kraft 3 years ago
parent 660e8a1df2
commit 6c10723997
  1. 8
      headers/Mandelbrot.h
  2. 13
      src/Mandelbrot.cpp
  3. 21
      src/OutputWidget.cpp

@ -9,13 +9,13 @@ Q_OBJECT
public: public:
void init(); void init();
void draw(GLuint, QOpenGLShaderProgram&); void draw(GLuint, QOpenGLShaderProgram&);
void zoom(double);
public slots:
void setIterations(int); void setIterations(int);
void translateRelative(QVector2D);
void zoomRelative(double, QVector2D);
private: private:
int iterations = 0; int iterations = 0;
double scale = 1; QVector2D origin {-2, 1};
QVector2D translation; QVector2D size {2, 2};
void setShaderValues(QOpenGLShaderProgram&) const; void setShaderValues(QOpenGLShaderProgram&) const;
}; };

@ -14,19 +14,24 @@ void Mandelbrot::draw(GLuint vao, QOpenGLShaderProgram& shader) {
} }
void Mandelbrot::setShaderValues(QOpenGLShaderProgram &shader) const { void Mandelbrot::setShaderValues(QOpenGLShaderProgram &shader) const {
shader.setUniformValue("origin", QVector2D(-2, 1)); shader.setUniformValue("origin", origin);
shader.setUniformValue("size", QVector2D(2, 2)); shader.setUniformValue("size", size);
shader.setUniformValue("iterationCount", iterations); shader.setUniformValue("iterationCount", iterations);
} }
void Mandelbrot::zoom(double delta) { void Mandelbrot::zoomRelative(double delta, QVector2D posRelative) {
scale *= delta;
} }
void Mandelbrot::setIterations(int value) { void Mandelbrot::setIterations(int value) {
iterations = value; iterations = value;
} }
void Mandelbrot::translateRelative(QVector2D relative) {
QVector2D absolute = relative * size;
origin -= absolute;
}

@ -64,18 +64,33 @@ void OutputWidget::initShader() {
if (!shader.bind()) close(); if (!shader.bind()) close();
} }
void OutputWidget::wheelEvent(QWheelEvent *e) { QVector2D relative(QPoint p, QSize s){
return {
float(p.x()) / float(s.width()),
float(p.y()) / float(s.height())
};
}
void OutputWidget::wheelEvent(QWheelEvent *e) {
double modifier = 1;
if (e->angleDelta().y() > 0)
modifier *= 1.1;
else
modifier /= 1.1;
getMandelbrot()->zoomRelative(modifier, relative(mousePos, size()));
update();
} }
void OutputWidget::mouseMoveEvent(QMouseEvent *e) { void OutputWidget::mouseMoveEvent(QMouseEvent *e) {
QPoint newMousePos = e->pos(); QPoint newMousePos = e->pos();
QPoint diff = newMousePos - mousePos; QPoint diff = newMousePos - mousePos;
auto m = getMandelbrot(); mousePos = newMousePos;
getMandelbrot()->translateRelative(relative(diff, size()));
update();
} }
void OutputWidget::mousePressEvent(QMouseEvent *e) { void OutputWidget::mousePressEvent(QMouseEvent *e) {
mousePos = e->pos();
} }
void OutputWidget::mouseReleaseEvent(QMouseEvent *e) { void OutputWidget::mouseReleaseEvent(QMouseEvent *e) {

Loading…
Cancel
Save