1. 程式人生 > >使用Qt+OpenGL建立球體+簡單互動

使用Qt+OpenGL建立球體+簡單互動

原始碼下載連結:http://download.csdn.net/download/a197p/9947482

效果圖:


這裡面還是用到了glut的函式,需要配置opengl環境的話,要把

glut32.lib放到Ot的lib下,在include下新建Gl資料夾放入glut.h。

需要把簡歷的OpenGLwidget提升為QLWidget.


也沒什麼說的,具體看程式碼吧。

glwidget.h:

#ifndef GLWIDGET_H
#define GLWIDGET_H

#include <QtOpenGL/QGLWidget>
#include <GL/glut.h>
#include <QTimer>
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 <QMainWindow>

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 <QApplication>
#include <GL/glut.h>
#include <GL/GLU.h>
#include <GL/GL.h>
#include <QtOpenGL>
#include <QtOpenGL/qgl.h>
int main(int argc, char *argv[])
{

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

    return a.exec();
}