如何在Qt 5.0.2 OpenGL(QOpenGLWidget)中放大和缩小图形?

ki1q1bka  于 2022-09-26  发布在  其他
关注(0)|答案(0)|浏览(263)

我正在尝试放大和缩小,比方说,两条线成一个Angular (就像一个勾号(/))。我在网上浏览了很多选择,但没有找到任何有用的东西。如果你在Qt中有一个例子,展示了如何用滚轮、鼠标或你知道怎么做来添加一个形状的放大和缩小,如果你能和我分享它,我将非常感激。

我最近才开始学习Qt和OpenGL。如果你能告诉我如何通过x和y坐标移动,也就是一个人物通常的左、右、上、下移动,那就太酷了。

以下是我使用的代码,也许它会对解决我的问题有用。

这段代码摘自一个指南,如果你能指出其中的错误(如果有错误的话),那就太酷了。

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();
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题