QOpenGLWidget

myopenglwidget.h

#ifndef MYOPENGLWIDGET_H
#define MYOPENGLWIDGET_H

#include <QOpenGLWidget>
#include <QOpenGLFunctions>
#include <QOpenGLFunctions_4_0_Core>
#include <QOpenGLFunctions_4_1_Core>
#include <QOpenGLFunctions_4_2_Core>
#include <QOpenGLFunctions_4_3_Core>
#include <QOpenGLFunctions_4_4_Core>
#include <QOpenGLFunctions_4_5_Core>
#include <QPen>
class myOpenGLWidget : public QOpenGLWidget, public QOpenGLFunctions_4_5_Core
{
    Q_OBJECT
public:
    explicit myOpenGLWidget(QWidget *parent = nullptr);
protected:
    void initializeGL() Q_DECL_OVERRIDE;
    void paintEvent(QPaintEvent *e) Q_DECL_OVERRIDE;
    void paintGL() Q_DECL_OVERRIDE;
private:
    QPen mPen;
};

#endif // MYOPENGLWIDGET_H

myopenglwidget.cpp

#include "myopenglwidget.h"
#include <QPainter>
#include <QMouseEvent>
#include <QDebug>

myOpenGLWidget::myOpenGLWidget(QWidget *parent) : QOpenGLWidget(parent)
{
    mPen.setWidth(5);
    mPen.setColor(Qt::green);
}

void myOpenGLWidget::initializeGL()
{
#if 0
    QOpenGLFunctions_4_5_Core* funcs = nullptr;
    funcs = context()->versionFunctions<QOpenGLFunctions_4_5_Core>();
    if (!funcs) {
        qWarning() << "Could not obtain required OpenGL context version";
        exit(1);
    }
    funcs->glClearColor(1.0f, 1.0f, 0.0f, 0.0f);
#else
    QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
    f->glClearColor(0.0f, 0.5f, 0.5f, 0.0f);
#endif
}

void myOpenGLWidget::paintEvent(QPaintEvent *e)
{
    QOpenGLWidget::paintEvent(e);
    QPainter painter(this);
    painter.setPen(mPen);
    painter.drawText(100, 100, "hello");
}

void myOpenGLWidget::paintGL()
{
    QOpenGLWidget::paintGL();
#if 0
    QOpenGLFunctions_4_5_Core* funcs = nullptr;
    funcs = context()->versionFunctions<QOpenGLFunctions_4_5_Core>();
    if (!funcs) {
        qWarning() << "Could not obtain required OpenGL context version";
        exit(1);
    }
    funcs->glClear(GL_COLOR_BUFFER_BIT);
#else
    QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
    f->glClear(GL_COLOR_BUFFER_BIT);
#endif
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值