QT+OpenGL 描绘简单图形

初学opengl,绕了很多弯路,继承于QOpenGLWindow,描画出来。新建类myopengl,头文件如下:

#ifndef MYOPENGL_H
#define MYOPENGL_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 


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 
#include 

myopenGL::myopenGL(QWindow *parent)
{
    rTri = 0.0;
    rQuad = 0.0;
    xRot = yRot = zRot = 0.0;
}

myopenGL::~myopenGL()
{

}
void myopenGL::loadGLTextures()
{
    QImage tex, buf;
    if ( !buf.load( "./msbg_blue.png" ) )
    {
      qWarning( "Could not read image file, using single-color instead." );
      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 );

    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, -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);
    glMatrixMode(GL_MODELVIEW);         //选择模型观察矩阵
    glLoadIdentity();                   //重置模型观察矩阵
}

main.cpp修改代码如下:

#include "myopengl.h"
#include 
#include 

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+OpenGL 描绘简单图形_第1张图片

这么简单的效果用了就走了好多弯路。跟QT的版本有关系。有的人用的是QOpenGLWindow,有的人用的是QOpenGLWidget。听说推荐用的是QOpenGLWidget。

转载于:https://www.cnblogs.com/wxmwanggood/p/11143000.html

你可能感兴趣的:(QT+OpenGL 描绘简单图形)