Qt6.3学习笔记 QOpenGLWidget 简单使用

QOpenGLWidget绘图的简单使用

#ifndef ORIENTATIONGLWIDGET_H
#define ORIENTATIONGLWIDGET_H
#include <QtOpenGL>
#include <QOpenGLWidget>
#include <QOpenGLExtraFunctions>
class OrientationGLWidget : public QOpenGLWidget, public QOpenGLExtraFunctions
{
    Q_OBJECT
public:
    explicit OrientationGLWidget(QWidget *parent = nullptr);
    ~OrientationGLWidget();
signals:
    
private:

public:
    void drawView();
    // QOpenGLWidget interface
protected:
    //设置渲染环境
    void initializeGL();
    //绘制窗口
    void resizeGL(int w, int h);
    void paintGL();
    
};

#endif // ORIENTATIONGLWIDGET_H

#include "orientationglwidget.h"
#include <gl/glu.h>

OrientationGLWidget::OrientationGLWidget(QWidget *parent)
    : QOpenGLWidget{parent}
{
}
OrientationGLWidget::~OrientationGLWidget()
{

}

void OrientationGLWidget::initializeGL()
{
    initializeOpenGLFunctions();

    //这个代表截取屏幕,这里我这里截取整个屏幕
    //注意此关键函数
    //glOrtho(0.0,1000,1000,0.0,-1.0,1.0);
    glOrtho(0.0,1000,0,1000,-1.0,1.0);

}

void OrientationGLWidget::resizeGL(int width, int height)
{

    // 防止窗口大小变为0
    if ( height == 0 )
        height = 1;
    if(width == 0)
        width = 1;

    //这里是显示窗体,上面有截取,这里才有显示。不然窗口不会显示任何内容
    glViewport(0, 0, width, height);

}

void OrientationGLWidget::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);

    glLineWidth(2);

    //黑色
    glColor3f(0.0, 0.0,0.0);
    glBegin(GL_LINES);
    glVertex2d(10,10);
    glVertex2d(10,800);
    glEnd();

    //红色
    glColor3f(1.0, 0.0, 0.0);
    glBegin(GL_LINES);
    glVertex2d(10,800);
    glVertex2d(800,800);
    glEnd();


    //绿色
    glColor3f(0.0, 1.0, 0.0);
    glBegin(GL_LINES);
    glVertex2d(800,800);
    glVertex2d(800,10);
    glEnd();

    //黄色
    glColor3f(1.0, 1.0, 0.0);
    glBegin(GL_LINES);
    glVertex2d(800,10);
    glVertex2d(10,10);
    glEnd();

    //注意平移和缩放的先后顺序,先缩放的话会影响平移的距离
  //  glTranslatef(500,0,0);
  //  glScaled(0.5,0.5,1);

//    glScaled(0.5,0.5,1);
//    glTranslatef(1000,0,0);

}

显示结果
在这里插入图片描述

多视口

#include "orientationglwidget.h"
#include <gl/glu.h>

OrientationGLWidget::OrientationGLWidget(QWidget *parent)
    : QOpenGLWidget{parent}
{
}
OrientationGLWidget::~OrientationGLWidget()
{

}

void OrientationGLWidget::initializeGL()
{
    initializeOpenGLFunctions();

    // 背景
    //glClearColor( 0.0, 0.0, 0.0, 0.0 );

    //这个代表截取屏幕,这里我这里截取整个屏幕
    //glOrtho(0.0,1000,1000,0.0,-1.0,1.0);
    glOrtho(0.0,1000,0,1000,-1.0,1.0);

}

void OrientationGLWidget::resizeGL(int width, int height)
{

    // 防止窗口大小变为0
    if ( height == 0 )
        height = 1;
    if(width == 0)
        width = 1;

    //这里是显示窗体,上面有截取,这里才有显示。不然窗口不会显示任何内容
    //glViewport(0, 0, width, height);

}

void OrientationGLWidget::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);

    glLineWidth(2);

    glViewport(0, 0, this->width()/2, this->height()/2);
    glColor3f(1.0, 1.0,1.0);
    this->drawView();

    glViewport(this->width()/2, this->height()/2, this->width()/2, this->height()/2);
    glColor3f(1.0, 1.0,1.0);
    this->drawView();

    glViewport(0, this->height()/2, this->width()/2, this->height()/2);
    glColor3f(1.0, 1.0,1.0);
    this->drawView();

    glViewport(this->width()/2, 0, this->width()/2, this->height()/2);
    glColor3f(1.0, 1.0,1.0);
    this->drawView();

    //glScaled(1,1,1);
}

void  OrientationGLWidget::drawView()
{
    glLineWidth(2);

    glBegin(GL_POLYGON);
    glVertex2d(0,0);
    glVertex2d(0,1000);
    glVertex2d(1000,1000);
    glVertex2d(1000,0);
    glVertex2d(0,0);
    glEnd();

    //黑色
    glColor3f(0.0, 0.0,0.0);
    glBegin(GL_LINES);
    glVertex2d(10,10);
    glVertex2d(10,800);
    glEnd();

    //红色
    glColor3f(1.0, 0.0, 0.0);
    glBegin(GL_LINES);
    glVertex2d(10,800);
    glVertex2d(800,800);
    glEnd();


    //绿色
    glColor3f(0.0, 1.0, 0.0);
    glBegin(GL_LINES);
    glVertex2d(800,800);
    glVertex2d(800,10);
    glEnd();

    //黄色
    glColor3f(1.0, 1.0, 0.0);
    glBegin(GL_LINES);
    glVertex2d(800,10);
    glVertex2d(10,10);
    glEnd();

}

显示结果
在这里插入图片描述