I'm trying to make the zoom in and out of, say, two lines at an angle (like a tick(/)). I have looked through a lot of options on the Internet, but did not find anything useful. If you have an example in Qt which shows how to add zoom in and out of a shape, with a wheel, mouse, or you know how to do it, I will be very grateful if you can share it with me.
I just recently started to learn Qt and OpenGl. It would be cool if you could tell me how to move by x and y coordinates, i.e. the usual movement of a figure left, right, up and down.
Here is the code I use, maybe it will be useful for solving my question.
This code was taken from one guide, it would be cool if you could point out the mistakes in it (if there are any)
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_actionSalir_triggered();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
myqopenglwidget.h
#ifndef MYQOPENGLWIDGET_H
#define MYQOPENGLWIDGET_H
#include <QOpenGLFunctions>
#include <QColor>
#include <GL/gl.h>
#include <GL/glu.h>
#include <QTimer>
#include <QOpenGLWidget>
#include <QOpenGLFunctions>
#include <QQuaternion>
#include <QVector2D>
#include <QVector>
#include <functional>
#define RGB_MIN 1
#define RGB_MAX 255
class MyQOpenGLWidget : public QOpenGLWidget, public QOpenGLFunctions
{
public:
MyQOpenGLWidget(QWidget *parent = nullptr);
protected:
void initializeGL() override;
void paintGL() override;
void resizeGL(int w, int h) override;
private:
void qColorToRGB(const QColor &C, float &r, float &g, float &b) const;
float normaliza_0_1(float val, float min, float max) const;
protected:
};
#endif // MYQOPENGLWIDGET_H
myqopenglwidget.cpp
#include "myqopenglwidget.h"
#include <QOpenGLFunctions>
#include <QOpenGLFunctions_4_1_Compatibility>
#include <cmath>
#include <QOpenGLVertexArrayObject>
#include <QOpenGLFunctions>
#include <QOpenGLShaderProgram>
#include <QGLFramebufferObject>
#include <QOpenGLFramebufferObject>
#include <GL/gl.h>
#include <iostream>
#include <vector>
#include <cmath>
#include <iomanip>
#include <fstream>
#include <QGLWidget>
#include <QMouseEvent>
MyQOpenGLWidget::MyQOpenGLWidget(QWidget *parent) : QOpenGLWidget {parent}
{
}
void MyQOpenGLWidget::initializeGL()
{
float r,g,b,a = normaliza_0_1(255.0f, RGB_MIN, RGB_MAX);
initializeOpenGLFunctions();
qColorToRGB(Qt::black,r,g,b);
glClearColor(r,g,b,a);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
}
void MyQOpenGLWidget::paintGL()
{
float r,g,b;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//x
glBegin(GL_LINES);
qColorToRGB(Qt::yellow,r,g,b);
glColor3f(r,g,b);
glVertex3f(-1.0f, 0.0f,0.0f);
glVertex3f(1.0f, 0.0f,0.0f);
glEnd();
//y
glBegin(GL_LINES);
qColorToRGB(Qt::yellow,r,g,b);
glColor3f(r,g,b);
glVertex3f(0.0f, -1.0f,0.0f);
glVertex3f(0.0f, 1.0f,0.0f);
glEnd();
glBegin(GL_LINES);
qColorToRGB(Qt::green,r,g,b);
glColor3f(r,g,b);
glVertex3f(0.0f, 0.0f,0.0f);
glVertex3f(0.835f, 0.69f,0.0f);
glEnd();
glBegin(GL_LINES);
qColorToRGB(Qt::green,r,g,b);
glColor3f(r,g,b);
glVertex3f(-0.835f, 0.69f,0.0f);
glVertex3f(0.0f, 0.0f,0.0f);
glEnd();
}
void MyQOpenGLWidget::resizeGL(int w, int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
}
void MyQOpenGLWidget::qColorToRGB(const QColor &C, float &r, float &g, float &b) const
{
r = normaliza_0_1(C.red(),RGB_MIN, RGB_MAX);
g = normaliza_0_1(C.green(),RGB_MIN, RGB_MAX);
b = normaliza_0_1(C.blue(),RGB_MIN, RGB_MAX);
}
float MyQOpenGLWidget::normaliza_0_1(float val, float min, float max) const
{
return (val-min)/(max-min);
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
