使用Qt+OpenGL创建球体+简单交互

源码下载链接:http://download.csdn.net/download/a197p/9947482

效果图:

使用Qt+OpenGL创建球体+简单交互_第1张图片

这里面还是用到了glut的函数,需要配置opengl环境的话,要把

glut32.lib放到Ot的lib下,在include下新建Gl文件夹放入glut.h。

需要把简历的OpenGLwidget提升为QLWidget.

使用Qt+OpenGL创建球体+简单交互_第2张图片

也没什么说的,具体看代码吧。

glwidget.h:

#ifndef GLWIDGET_H
#define GLWIDGET_H

#include 
#include 
#include 
class GLWidget : public QGLWidget
{
    Q_OBJECT
public:
    explicit GLWidget(QWidget *parent = 0);

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

    int R;
    float x,y,z;
    bool wired;

    float ang;
private:
    QTimer timer;
};

#endif // GLWIDGET_H
glwidget.cpp

#include "glwidget.h"

GLWidget::GLWidget(QWidget *parent) : QGLWidget(parent)
{
    this->R=1;
    this->wired=false;
    this->ang=0.5;
    x=0,y=0,z=0;
       connect(&timer,SIGNAL(timeout()),this,SLOT(updateGL()));
       timer.start(16);
}

void GLWidget::initializeGL()
{
    glClearColor(0.2,0.2,0.2,1);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHTING);
    glEnable(GL_COLOR_MATERIAL);

}

void GLWidget::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);


    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0,0,5, 0,0,0  ,0,1,0);

    glTranslatef(x,y,z);
    ang+=0.5;
    glRotatef(ang,1,1,1);




    glColor3f(1,0,0);

    if(wired)
        glutWireSphere(R,30,30);
        else

    glutSolidSphere(R,30,30);

}

void GLWidget::resizeGL(int w, int h)
{
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0,(float)w/h,0.01,100.0);

    updateGL();
}
mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private slots:
    void on_X_valueChanged(double arg1);

    void on_Y_valueChanged(double arg1);

    void on_Z_valueChanged(double arg1);

    void on_R_valueChanged(double arg1);



    void on_checkBox_clicked(bool checked);

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H
mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

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

void MainWindow::on_X_valueChanged(double arg1)
{
    this->ui->openGLWidget->x=arg1;
}

void MainWindow::on_Y_valueChanged(double arg1)
{
this->ui->openGLWidget->y=arg1;
}


void MainWindow::on_Z_valueChanged(double arg1)
{
this->ui->openGLWidget->z=arg1;
}

void MainWindow::on_R_valueChanged(double arg1)
{
this->ui->openGLWidget->R=arg1;
}


void MainWindow::on_checkBox_clicked(bool checked)
{
this->ui->openGLWidget->wired=checked;

}

main.cpp

#include "mainwindow.h"
#include "glwidget.h"
#include 
#include 
#include 
#include 
#include 
#include 
int main(int argc, char *argv[])
{

glutInit(&argc,argv
           );
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}





你可能感兴趣的:(OpenGL)