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

网友投稿 307 2022-11-15

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

效果图:

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

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

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

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

glwidget.h:

#ifndef GLWIDGET_H#define GLWIDGET_H#include #include #include class GLWidget : public QGLWidget{ Q_OBJECTpublic: 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_OBJECTpublic: 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();}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java 分位点(分位值)计算方式
下一篇:22-Spring Authorization Server初体验
相关文章

 发表评论

暂时没有评论,来抢沙发吧~