OpenGL学习(二)绘制三维图形 固定管线

一.简单步骤

  • 考虑顶点数据
  • 上色
  • 旋转

二.完整代码

myopenglwidget.h

// An highlighted block
#ifndef MYOPENGLWIDGET_H
#define MYOPENGLWIDGET_H
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

class MyOpenGLWidget : public QOpenGLWidget,protected QOpenGLFunctions  // 可直接使用QOpenGLFunctions中的OpenGL函数
{
    Q_OBJECT
public:
    explicit MyOpenGLWidget (QWidget * parent = 0);



protected:
    void initializeGL();
    void paintGL();
    void resizeGL(int width,int height);
    void keyPressEvent(QKeyEvent* e) override;  //Qt键盘事件处理函数
    void mousePressEvent(QMouseEvent *event) override
    {
        if (event->button() == Qt::LeftButton)
        {
            m_lastPos = event->globalPos() - frameGeometry().topLeft();
//            qDebug() << "Clicked at position: " << m_lastPos;
        }
    }
    void mouseMoveEvent(QMouseEvent *event) override
        {
            if (event->buttons() & Qt::LeftButton)
            {
                QPoint newpos = event->globalPos() - m_lastPos;
                move(newpos);
            }
        }

private slots:
    void rotate() {
        update();
    }
private:
    GLfloat angle;
    double size_t;
    bool flag=true;
    bool fullscreen=false;
    QPoint m_lastPos;




};

#endif // MYOPENGLWIDGET_H

 

 myopenglwidget.cpp

#include "myopenglwidget.h"

MyOpenGLWidget::MyOpenGLWidget(QWidget *parent)
    : QOpenGLWidget(parent)
{

    setGeometry(1550,0,100,100);               //设置窗口大小、位置
    resize(50,50);

    // 去掉当前窗口的标题栏
    setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);//隐藏标题栏并且设置窗口悬浮在所有程序之上
    setAttribute(Qt::WA_TranslucentBackground);//设置背景透明
//    setWindowFlags(Qt::WindowStaysOnTopHint);
    angle = 0;
    size_t=0;

    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(rotate()));
    timer->start(20);


    foreach(const QHostAddress& hostAddress,QNetworkInterface::allAddresses())//对于所有检测到的地址
        if ( hostAddress != QHostAddress::LocalHost && hostAddress.toIPv4Address() )//如果格式为IPv4则输出
            qDebug()<=0.5)
    {
        flag=false;
    }
    else if(size_t==0)
    {
        flag=true;
    }

    angle += 2;

    if(flag)
    {
        size_t+=0.001;
    }
    else
    {
        size_t-=0.001;
    }
}

void MyOpenGLWidget::resizeGL(int w, int h)
{
    glViewport(0, 0, w, h);//使用当前屏幕的宽和高来设置视口
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

}
void MyOpenGLWidget::keyPressEvent(QKeyEvent* e)
{
    switch (e->key()) {
        case Qt::Key_Q: {
            fullscreen = !fullscreen;
            if(fullscreen) {
                showFullScreen();
            }else {
                showNormal();
                setGeometry(500,500,640,480);
            }
            update();
            break;
        }//case Qt::Key_Q

        case Qt::Key_Escape: {
            close();
        }//Qt::Key_Escape

    }//switch (e->key())
}

你可能感兴趣的:(OpenGL,qt)