继承于QOpenGLWindow,描画出来。新建类myopengl,头文件如下:
#ifndef MYOPENGL_H #define MYOPENGL_H #include <QWidget> #include <QOpenGLWidget> #include <QOpenGLWindow> #include <QGLWidget> #include <QGLFunctions> #include <QOpenGLFunctions> #include <QtOpenGL> #include <QOpenGLBuffer> #include <QOpenGLShaderProgram> #include <QMatrix4x4> #include <GL/glu.h> class myopenGL:public QOpenGLWindow, protected QOpenGLFunctions { Q_OBJECT public: explicit myopenGL(QWindow *parent = 0); ~myopenGL(); protected: void initializeGL() override; void paintGL() override; void resizeGL(int width, int height) override; void loadGLTextures(); protected: GLfloat rTri; GLfloat rQuad; GLfloat xRot, yRot, zRot; GLuint texture[1]; private: }; #endif // MYOPENGL_H
myopengl.cpp 代码如下:
#include "myopengl.h" #include <QImage> #include <QDebug> myopenGL::myopenGL(QWindow *parent) { rTri = 0.0; rQuad = 0.0; xRot = yRot = zRot = 0.0; } myopenGL::~myopenGL() www.devze.com{ } void myopenGL::loadGLTextures() { QImage tex, buf; if ( !buf.load( "./msbg_blue.png" ) ) { qWarning( "Could not read image file, using single-color inste编程客栈ad." ); QImage dummy( 128, 128, QImage::Format_RGB32 ); dummy.fill( Qt::green ); buf = dummy; } tex = QGLWidget::convertToGLFormat( buf ); glGenTextures( 1, &texture[0] ); glBindTexture( GL_TEXTURE_2D, texture[0] ); glTexImage2D( GL_TEXTURE_2D, 0, 3, tex.width(), tex.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, tex.bits() ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); } //初始化opengl窗口部件 void myopenGL::initializeGL() { initializeOpenGLFunctions(); glClearColor(0.0,0.0,0.0,1.0); glShadeModel(GL_SMOOTH); glClearDepth(1.0); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST); } //绘制opengl窗口 void myopenGL::paintGL() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslatef(-1.5f,0.0f,-6.0f); glRotatef( rTri, 0.0, 1.0, 0.0 ); glBegin(GL_TRIANGLES); glColor3f( 1.0, 0.0, 0.0 ); glVertex3f( 0.0, 1.0, 0.0 ); glColor3f( 0.0, 1.0, 0.0 ); glVertex3f( -1.0, -1.0, 1.0 ); glColor3f( 0.0, 0.0, 1.0 ); glVertex3f( 1.0, -1.0, 1.0 ); glColor3f( 1.0, 0.0, 0.0 ); glVertex3f( 0.0, 1.0, 0.0 ); glColor3f( 0.0, 0.0, 1.0 ); glVertex3f( 1.0, -1.0, 1.0 ); glColor3f( 0.0, 1.0, 0.0 ); glVertex3f( 1.0, -1.0, -1.0 ); glColor3f( 1.0, 0.0, 0.0 ); glVertex3f( 0.0, 1.0, 0.0 ); glColor3f( 0.0, 1.0, 0.0 ); glVertex3f( 1.0, -1.0, -1.0 ); glColor3f( 0.0, 0.0, 1.0 ); glVertex3f( -1.0, -1.0, -1.0 ); php glColor3f( 1.0, 0.0, 0.0 ); glVertex3f( 0.0, 1.0, 0.0 ); glColor3f( 0.0, 0.0, 1.0 ); glVertex3f( -1.0, -1.0, -1.0 ); glColor3f( 0.0, 1.0, 0.0 ); glVertex3f( -1.0, -1.0, 1.0 ); glEnd(); glTranslatef(3.0f,0.0f,-7.0f); glRotatef( rQuad, 0.0, 0.0, 1.0 ); glRotatef( rTri, 0.0, 1.0, 0.0 ); glRotatef( rQuad, 0.0, 0.0, 1.0 ); glBegin(GL_QUADS); glColor3f( 0.0, 1.0, 0.0 ); glVertex3f( 1.0, 1.0, -1.0 ); glVertex3f( -1.0, 1.0, -1.0 ); glVertex3f( -1.0, 1.0, 1.0 ); glVertex3f( 1.0, 1.0, 1.0 ); glColor3f( 1.0, 0.5, 0.0 ); glVertex3f( 1.0, -1.0, 1.0 ); glVertex3f( -1.0, -1.0, 1.0 ); glVertex3f( -1.0, -1.0, -1.0 ); glVertex3f( 1.0, -1.0, -1.0 ); glColor3f( 1.0, 0.0, 0.0 ); glVertex3f( 1.0, 1.0, 1.0 ); glVertex3f( -1.0, 1.0, 1.0 ); glVertex3f( -1.0, -1.0, 1.0 ); glVertex3f( 1.0, -1.0, 1.0 ); glColor3f( 1.0, 1.0, 0.0 ); glVertex3f( 1.0, -1.0, -1.0 ); glVertex3f( -1.0, -1.0, -1.0 ); glVertex3f( -1.0, 1.0, -1.0 ); glVertex3f( 1.0, 1.0, -1.0 ); glColor3f( 0.0, 0.0, 1.0 ); glVertex3f( -1.0, 1.0, 1.0 ); glVertex3f( -1.0, 1.0, -1.0 ); glVertex3f( -1.0, -开发者_Go开发1.0, -1.0 ); glVertex3f( -1.0, -1.0, 1.0 ); glColor3f( 1.0, 0.0, 1.0 ); glVertex3f( 1.0, 1.0, -1.0 ); glVertex3f( 1.0, 1.0, 1.0 ); glVertex3f( 1.0, -1.0, 1.0 ); glVertex3f( 1.0, -1.0, -1.0 ); glEnd(); rTri += 0.2; rQuad -= 0.15; } //处理窗口大小 void myopenGL::resizeGL(int width, int height) { glViewport(0,0,(GLint)width,(GLint)height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0,(GLfloat)width/(GLfloat)height,0.1,100.0); phpglMatrixMode(GL_MODELVIEW); //选择模型观察矩阵 glLoadIdentity(); //重置模型观察矩阵 }
main.cpp修改代码如下:
#include "myopengl.h" #include <QGuiApplication&ghttp://www.devze.comt; #include <QPushButton> int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_UseDesktopOpenGL); QGuiApplication a(argc, argv); myopenGL w; w.resize(QSize(1280,720)); //QPushButton ptn(&w); w.show(); return a.exec(); }
效果图:
这么简单的效果用了就走了好多弯路。跟QT的版本有关系。有的人用的是QOpenGLWindow,有的人用的是QOpenGLWidget。听说推荐用的是QOpenGLWidget
到此这篇关于QT+OpenGL实现简单图形的绘制的文章就介绍到这了,更多相关QT OpenGL绘制图形内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
精彩评论