Qt--openGL

实现第一个Qt的opengl窗口
效果图:
这里写图片描述

源码实现:
在工程文件中添加QT += opengl

**

  1. mainwindow.h

**

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QOpenGLWindow>
#include <QOpenGLFunctions>
 //继承QOpenGLWindow或QOpenGLWidget  and //QOpenGLFunctions
class MainWindow : public QOpenGLWindow,protected QOpenGLFunctions
{
    Q_OBJECT

public:
    ~MainWindow();
       //重写QOpenGLWindow的下面三个函数
  void initializeGL();  //初始化
void resizeGL(int width, int height);//绘制窗口大小
  void paintGL();//重新绘制窗口图像

private:
    void printContextInformation();//获取调试信息
};

#endif // MAINWINDOW_H

**

2.mainwindow.cpp

**

#include "mainwindow.h"
#include <QDebug>

MainWindow::~MainWindow()
{
}
void MainWindow::initializeGL(){
    initializeOpenGLFunctions();
    printContextInformation();
    glClearColor(0.0f,0.0f,1.0f,1.0f);//设置颜色
}

void MainWindow::resizeGL(int width, int height)
{
    //未作处理
}

void MainWindow::paintGL()
{
   glClear(GL_COLOR_BUFFER_BIT);
}

// 打印相关信息,调试用
void MainWindow::printContextInformation()\
{
    QString glType;
    QString glVersion;
    QString glProfile;

    glType = (context()->isOpenGLES())?"OpenGL ES":"OpenGL";
    glVersion = reinterpret_cast<const char*>(glGetString(GL_VERSION));

#define CASE(c) case QSurfaceFormat::c: glProfile = #c; break
    switch (format().profile()) {
      CASE(NoProfile);
      CASE(CoreProfile);
      CASE(CompatibilityProfile); //??
    }
#undef CASE
qDebug() << qPrintable(glType) << qPrintable(glVersion)<< "(" << qPrintable(glProfile) << ")";
}

**

  1. main.cpp

**

#include "mainwindow.h"
#include <QApplication>
#include <QGLFormat>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    // 设置 OpenGL 信息
     // 注意:必须在show()之前设置信息
//     QSurfaceFormat format;
//     format.setRenderableType(QSurfaceFormat::OpenGL);
//     format.setProfile(QSurfaceFormat::CoreProfile);
//     format.setVersion(3,3);

    MainWindow w;
    // w.setFormat(format);
     w.resize(QSize(800, 600));
     w.show();
    return a.exec();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值