使用Qt编写opengl学习路线

xiaoxiao2021-02-28  97

之前想在qt上编译opengl,无从下手,最近看的到网上很多的例子。

那不如就从这些例子下手,慢慢做实例。

1.实现窗口

效果:

第一步新建,Qt Widgets Application工程,选择一个Widget控件拖入窗口,选择在窗口中进行栅格布局。

简单说就是在ui界面,拖入一个Widget。

2.新建类,右键添加新项,添加openglwindow类。base class为QOpenGLWidget

openglwindow.h写为:

#ifndef OPENGLWINDOW_H #define OPENGLWINDOW_H #include <QOpenGLWidget> #include <QOpenGLFunctions_3_3_Core> class openglwindow : public QOpenGLWidget, protected QOpenGLFunctions_3_3_Core { Q_OBJECT public: openglwindow(QWidget *parent = 0); ~openglwindow(); void initializeGL(); void resizeGL(int w, int h); void paintGL(); }; #endif // OPENGLWINDOW_H openglwindow.cpp为:

#include "openglwindow.h" openglwindow::openglwindow(QWidget *parent) :QOpenGLWidget(parent) { QSurfaceFormat format; format.setRenderableType(QSurfaceFormat::OpenGL); format.setProfile(QSurfaceFormat::CoreProfile); format.setVersion(3,3); setFormat(format); } openglwindow::~openglwindow() {} void openglwindow::initializeGL() { initializeOpenGLFunctions(); glClearColor(0.0f,0.0f,0.0f,1.0f); } void openglwindow::paintGL() { glClear(GL_COLOR_BUFFER_BIT); } void openglwindow::resizeGL(int w,int h) { Q_UNUSED(w); Q_UNUSED(h); } 3.把widget那个空间提升为openglwindow类:

转载请注明原文地址: https://www.6miu.com/read-29403.html

最新回复(0)