Qt5中使用QOpenGLWindow渲染三角形

    Qt5中的例程:openglwindow,是采用继承QWindow类和QOpenGLFunctions类,来实现OpenGL窗口的显示和渲染的,在新版本的Qt中,可采用继承QOpenGLWindow类和QOpenGLFunctions类来实现。

QOpenGLWindow类继承于QPaintDeviceWindow类,具有更强大的OpenGL支持功能,因此在新的程序中推荐使用QOpenGLWindow类,不推荐使用QOpenGLWidget类。

第一步:可自定义新的类 class Window ,继承于QOpenGLWindow类和QOpenGLFunctions类,重载QOpenGLWindow类中的3个虚函数:

void initializedGL(),void resizeGL(),void paintGL(),window.h如下所示:

#ifndef WINDOW_H
#define WINDOW_H

#include <QOpenGLWindow>
#include <QOpenGLFunctions>
#include <QTimer>


class Window : public QOpenGLWindow, protected QOpenGLFunctions
{
    Q_OBJECT
public:
    Window();
    ~Window();

protected:
    void initializeGL();          //初始化设置
    void resizeGL(int w, int h);  //窗口尺寸变化响应函数
    void paintGL();               //重绘响应函数
private:
    GLfloat angle;                //定义旋转角度
    QTimer *timer;                //定义新的定时器
};

#endif // WINDOW_H

第二步:分别实现上述3个虚函数,并初始化timer定时器和旋转角度angle。设置初始角度为0,并通过connect()函数将定时器的timeoout()信号与QOpenGLWindow函数中的update()槽进行连接,并启用定时器timer。

注:在void initializeGL()函数中必须调用initializeOpenGLFunctions()函数,对QOpenGLFunctions进行使用环境的初始化。

在void initializeGL()函数中设置清除背景颜色调用glClearColor(),清除颜色的位深glClearDepth()。

在void paintGL()函数中,绘制想要绘制的三角形,具体可参考OpenGL文档:

https://www.khronos.org/registry/OpenGL-Refpages/es2.0/

window.cpp如下所示:

#include "window.h"

Window::Window()
{
    timer = new QTimer();
    angle = 0.0;
    connect(timer,SIGNAL(timeout()),this,SLOT(update()));   //连接定时器的信号与槽
    timer->start(100);
}

Window::~Window()
{

}

void Window::initializeGL()
{
    initializeOpenGLFunctions();
    glClearColor(0.0,0.0,0.0,0.0);
    glClearDepth(1.0);
}

void Window::resizeGL(int w, int h)
{
    Q_UNUSED(w);
    Q_UNUSED(h);
}

void Window::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glRotated(angle,0.0,1.0,0.0);
    glBegin(GL_TRIANGLES);
        glColor3f(1.0,0.0,0.0);
        glVertex3f(0.0,0.8,0.0);
        glColor3f(0.0,0.0,1.0);
        glVertex3f(0.5,0.0,0.0);
        glColor3f(0.0,1.0,0.0);
        glVertex3f(-0.5,0.0,0.0);
    glEnd();
    angle+=10.0;
}

完成以上两个步骤,基本的绘图程序就实现了,但是会出现以下问题:

因此还需要第三部进行处理。

第三步:在.pro文件中加入两行指令:

LIBS += -lopengl32\

             -lglu32

如果加入上述指令后,仍出现上述问题,那么请使用MinGW编译器进行调试,在MSVC****编译器中会存在该问题。

opengl.pro文件如下:

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = OpenGL
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

CONFIG += c++11

SOURCES += \
        main.cpp \
    window.cpp

HEADERS += \
    window.h

FORMS +=
LIBS += -lopengl32\
        -lglu32

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

main.cpp文件如下:

#include "window.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Window w;
    w.show();
    return a.exec();
}

效果如下:

 

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值