Qt|OpenGL学习笔记-绕X轴、Y轴、以及XY轴旋转的四边体

程序运行截图如下:

 

源码如下:

myglwidget.h

#ifndef MYGLWIDGET_H
#define MYGLWIDGET_H

#include 
#include 
class QTimer;


class MyGLWidget : public QOpenGLWidget
{
    Q_OBJECT
public:
    explicit MyGLWidget(QWidget *parent=0);
    void setRelyX(const double x);
    void setRelyY(const double y);

protected:
    void initializeGL();
    void resizeGL(int w, int h);
    void paintGL();

protected slots:
    void beginRotate();

private:
    int m_angle;
    double m_relyX;
    double m_relyY;

    QTimer *m_timer;
};

#endif // MYGLWIDGET_H

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include 


namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

public slots:
    void rotateXClicked();
    void rotateYClicked();
    void rotateXAndYClicked();
    void rotateAngleChanged();

private:
    Ui::Widget *ui;
};

#endif // WIDGET_H

 

main.cpp

#include "widget.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

 

myglwidget.cpp

#include "myglwidget.h"
#include 
#include 

MyGLWidget::MyGLWidget(QWidget *parent):
    QOpenGLWidget(parent)
{
    m_angle=0;
    m_relyX=0.0;
    m_relyY=1.0;    //默认向Y轴转
    m_timer=new QTimer;
    connect(m_timer,SIGNAL(timeout()),this,SLOT(beginRotate()));
    m_timer->start(0);
}

void MyGLWidget::setRelyX(const double x)
{
    m_relyX=x;
}

void MyGLWidget::setRelyY(const double y)
{
    m_relyY=y;
}

void MyGLWidget::initializeGL()
{
    glClearColor(0.0,0.0,0.0,0.0);
    glShadeModel(GL_SMOOTH);
    glClearDepth(1.0);
    glEnable(GL_DEPTH_TEST);
}

void MyGLWidget::resizeGL(int w, int h)
{
    glViewport(0,0,(GLint)w,(GLint)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0,(GLfloat)w/(GLfloat)h,0.1,100);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void MyGLWidget::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glTranslatef(0.0,0.0,-6.0);
    glRotatef(m_angle,m_relyX,m_relyY,0.0);
    glBegin(GL_QUADS);
    // 上面
    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(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(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(0.2,0.2,0.5);
    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.5,0.4,0.5);
    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.8,0.1,0.7);
    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();
}

void MyGLWidget::beginRotate()
{
    if(m_angle==360){
        m_angle=0;
        update();
    }
    m_angle++;
    update();
}

 

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include 

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    connect(ui->rotateXPushButton,SIGNAL(clicked(bool)),this,SLOT(rotateXClicked()));
    connect(ui->rotateYPushButton,SIGNAL(clicked(bool)),this,SLOT(rotateYClicked()));
    connect(ui->rotateXandYpushButton,SIGNAL(clicked(bool)),this,SLOT(rotateXAndYClicked()));
}

Widget::~Widget()
{
    delete ui;
}

void Widget::rotateXClicked()
{
    ui->openGLWidget->setRelyX(1);
    ui->openGLWidget->setRelyY(0);
}

void Widget::rotateYClicked()
{
    ui->openGLWidget->setRelyX(0);
    ui->openGLWidget->setRelyY(1);
}

void Widget::rotateXAndYClicked()
{
    ui->openGLWidget->setRelyX(1);
    ui->openGLWidget->setRelyY(1);
}

 

你可能感兴趣的:(C/C++,Qt,OpenGL)