当前位置: 首页 > news >正文

Qt与OpenGL绘制大全(加载obj模型文件、点、线、面、立方体、圆等)

Qt与OpenGL绘制大全(加载obj模型文件、点、线、面、立方体、圆等)

  • 1、功能介绍
  • 2、成果展示
  • 3、关键技术
  • 4、关键代码
    • 4.1、画布使用
    • 4.2、可用接口
  • 5、扩展

1、功能介绍

最近项目要求用Qt+OpenGL绘制图形,要求可以加载Obj模型文件,并且可以画点、线、多边形(三角形、四边形、五边形、…、n边形、空心圆、实心圆、圆柱、圆锥、圆台等,在绘制前后还可以分别指定图形的透明度、颜色、大小等参数。

2、成果展示

画布版:
在这里插入图片描述
添加天空盒:
在这里插入图片描述

3、关键技术

Qt、 OpenGl、 着色器

4、关键代码

4.1、画布使用

成果展示左侧部分是我为验证接口写的调试界面,主要代码都是在右侧画布上,只是将画布潜在了调试的类里。
mainwindow.ui
在这里插入图片描述

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include "OpenGLMainWidget.h"
#include <QtMath>
#include <vector>
#include <random>
#include <cmath>
#include <QProgressDialog>
#include <QListWidgetItem>QT_BEGIN_NAMESPACE
namespace Ui {class MainWindow;}
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECT
public:MainWindow(QWidget *parent = nullptr);~MainWindow();
private:Ui::MainWindow *ui;float height, float depth);
public:OpenGLMainWidget* m_openGLWidget;
private slots:void on_pushButton_clearCanvas_clicked();...
};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QCoreApplication>
#include <QFileDialog>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);this->setWindowState(Qt::WindowMaximized);m_openGLWidget = new OpenGLMainWidget(parent);ui->gridLayout_5->addWidget(m_openGLWidget);
}MainWindow::~MainWindow()
{delete ui;
}

4.2、可用接口

通过这些接口,可以完成对点、线、多边形(三角形、四边形、五边形、…、n边形、空心圆、实心圆、圆柱、圆锥、圆台等图形的绘制与修改。注意看每个接口后的注释!
OpenGLMainWidget.h

public:explicit OpenGLMainWidget(QWidget* parent = nullptr);~OpenGLMainWidget();//画布可画://1、obj模型//2、图形//2.1、点//2.2、线//2.3、多边形(三角形、四边形、五边形、...、n边形)//2.4、其他(实心圆、空心圆、圆锥、圆柱、圆台)//------------------------------------------------------------------------------------对画布的操作-------------------------------------------------------------void UpdateTheCanvas();                                                                         //刷新画布void ClearCanvas();                                                                             //清空画布void RemoveAllModels();                                                                         //清除所有模型void RemoveAllGraphics();                                                                       //清除所有图形(除模型以外的所有物体)void RemoveAllPoints();                                                                         //清除所有点void RemoveAllLineSegments();                                                                   //清除所有线void RemoveAllPolygons();                                                                       //清除所有多边形void SetAllModelTransparencyByCode(const float &transparency);                                  //修改所有模型透明度void SetAllPolygonsFaceTransparencyByCode(const float &transparency);                            //修改所有多边形面透明度void SetAllPolygonsEdgeTransparencyByCode(const float &transparency);                            //修改所有多边形面透明度void SetAllPolygonsFaceColorByCode(const QVector3D &color);                                      //修改所有多边形面颜色void SetAllPolygonsEdgeColorByCode(const QVector3D &color);                                      //修改所有多边形边颜色void SetCamaraPosition(const QVector3D &cameraPosition);                                        //修改相机位置void RestoreOriginalPosition();                                                                 //将相机、模型、多边形恢复到最初位置void CheckSkyBox(const bool &isload);                                                           //设置是否使用天空盒void SetSkyBoxImagePath(const SkyBoxStruct &skyBoxImagePath);                                   //设置天空盒素材void SetCanvasColor(const QVector4D &canvasColor);                                              //设置画布背景颜色//------------------------------------------------------------------------------------对模型的操作-------------------------------------------------------------void loadModel(const LoadModelInfo &loadModelInfo);                                             //加载模型void RemoveModelsByCode(const QStringList &codeList);                                           //根据模型code删除画布上的指定模型void SetModelWorldPosByCode(const QString &code,const QVector3D &worldPos);                     //修改模型位置void SetModelRotationAngleByCode(const QStringList &codeList,const QVector3D &rotationAngle);   //旋转模型角度void SetModelIsDisplayOutlineByCode(const QStringList &codeList,const bool &display);           //设置模型是否显示外轮廓void SetModelOutlineColorByCode(const QStringList &codeList,const QVector3D &outlineColor);     //修改外轮廓颜色void SetModelScaleByCode(const QStringList &codeList, const QVector3D &scale);                  //修改缩放比例void SetModelTransparencyByCode(const QStringList &codeList,const float &transparency);         //修改透明度//------------------------------------------------------------------------------------对点的操作-------------------------------------------------------------void DrawPoint(const LoadPointInfo &loadPointInfo);                                             //绘制点void RemovePointByCode(const QStringList &codeList);                                            //根据code删除画布上的指定点void SetPointColorByCode(const QStringList &codeList,const QVector3D &color);                   //修改颜色void SetPointTransparencyByCode(const QStringList &codeList, float transparency);               //修改透明度void SetPointSizeByCode(const QStringList &codeList, float size);                               //修改点的尺寸//------------------------------------------------------------------------------------对线段的操作-------------------------------------------------------------void DrawLineSegment(const LoadLineSegmentInfo &loadLineSegmentInfo);                           //画线段void RemoveLineSegmentByCode(const QStringList &codeList);                                      //根据code删除画布上的指定线段void SetLineSegmentTransparencyByCode(const QStringList &codeList,const float &transparency);   //修改透明度void SetLineSegmentColorByCode(const QStringList &codeList,const QVector3D &color);             //修改颜色void SetLineSegmentWidthByCode(const QStringList &codeList, float width);                       //修改线宽//------------------------------------------------------------------------------------对多边形的操作-------------------------------------------------------------void DrawPolygons(const QVector<LoadPolygonInfo> &loadPolygonInfoVec);                          // 画多边形void RemovePolygonsByCode(const QStringList &codeList);                                         //根据code删除画布上的指定多边形void SetPolygonFaceTransparencyByCode(const QStringList &codeList,const float &transparency);   //修改多边形面版透明度void SetPolygonEdgeTransparencyByCode(const QStringList &codeList,const float &transparency);   //修改多边形边框透明度void SetPolygonFaceColorByCode(const QStringList &codeList,const QVector3D &faceColor);         //修改多边形面颜色void SetPolygonEdgeColorByCode(const QStringList &codeList,const QVector3D &edgeColor);         //修改多边形边框颜色void SetPolygonIsTextureByCode(const bool &isTexture);                                          //设置多边形是否贴图void SetPolygonTexturePathByCode(const QString &code,const QString &texturePath);               //修改多边形贴图文件void SetPolygonEdgeWidthByCode(const QStringList &codeList, float width);                       //修改边框线宽//------------------------------------------------------------------------------------对圆台的操作-------------------------------------------------------------void DrawFrustumCone(const LoadFrustumConeInfo &loadFrustumConeInfo);                           //画圆台void RemoveFrustumConesByCode(const QStringList &codeList);                                     //根据code删除画布上的指定圆台void SetFrustumConeTransparencyByCode(const QStringList &codeList, float transparency);         //修改透明度void SetFrustumConeColorByCode(const QStringList &codeList, const QVector3D &color);            //修改颜色//------------------------------------------------------------------------------------对圆柱的操作-------------------------------------------------------------void DrawCylinder(const LoadCylinderInfo &loadCylinderInfo);                                    //画圆柱void RemoveCylindersByCode(const QStringList &codeList);                                        //根据code删除画布上的指定圆柱void SetCylinderTransparencyByCode(const QStringList &codeList, float transparency);            //修改透明度void SetCylinderColorByCode(const QStringList &codeList, const QVector3D &color);               //修改颜色//------------------------------------------------------------------------------------对圆锥的操作-------------------------------------------------------------void DrawCone(const LoadConeInfo &loadConeInfo);                                                //画圆锥void RemoveConesByCode(const QStringList &codeList);                                            //根据code删除画布上的指定圆锥void SetConeTransparencyByCode(const QStringList &codeList, float transparency);                //修改透明度void SetConeColorByCode(const QStringList &codeList, const QVector3D &color);                   //修改颜色//------------------------------------------------------------------------------------对圆的操作-------------------------------------------------------------void DrawCircular(const LoadCircleInfo &loadCircleInfo);                                        //画圆void RemoveCircularsByCode(const QStringList &codeList);                                        //根据code删除画布上的指定圆void SetCircularTransparencyByCode(const QStringList &codeList, float transparency);            //修改透明度void SetCircularColorByCode(const QStringList &codeList, const QVector3D &color);               //修改颜色//------------------------------------------------------------------------------------其他算法-------------------------------------------------------------std::vector<std::vector<float> > CalculateCubeFaces(const QVector3D &center, float width, float height, float depth);//根据长方体的中心坐标、宽度、高度、深度,计算长方体六个面的顶点坐标,返回包含6个面的顶点坐标集合,每个面由4个顶点共12个浮点数组成signals:void MousePickingPosSignal(QVector3D worldPos);//鼠标拾取位置信号void MouseClickedModelSignal(QString modelCode);//鼠标点击模型返回的信号
protected:void keyPressEvent(QKeyEvent *event);void mouseMoveEvent(QMouseEvent *event);void wheelEvent(QWheelEvent *event);void mousePressEvent(QMouseEvent *event);void mouseDoubleClickEvent(QMouseEvent *event);

5、扩展

目前还在不停增加功能,后续希望能够添加文字、坐标指示器等。

http://www.xdnf.cn/news/525889.html

相关文章:

  • 枪机定焦系统的自动控制装置
  • 健康生活指南:从日常细节开启养生之旅
  • RK3568下QT实现按钮切换tabWidget
  • CentOS7修改ip
  • npm 安装时 SSL 证书过期问题笔记
  • 计算机视觉与深度学习 | EMD-KPCA-LSTM、EMD-LSTM、LSTM回归预测对比,多输入单输出(Matlab完整程序和数据)
  • 【Python 算法零基础 4.排序 ② 冒泡排序】
  • c/c++的opencv均值函数
  • chrome源码中WeakPtr 跨线程使用详解:原理、风险与最佳实践
  • fnOS手机APP+NAS架构:破解跨地域数据实时访问的内网穿透难题
  • Linux笔记---内核态与用户态
  • Manus AI 突破多语言手写识别技术壁垒:创新架构、算法与应用解析
  • 智象科技:自动化模块驱动IT运维效能升级
  • pyspark测试样例
  • OpenCv(7.0)——银行卡号识别
  • 芯驰科技与安波福联合举办技术研讨会,深化智能汽车领域合作交流
  • Java知识点-Stream流
  • Maven配置安装
  • Unity入门学习(三)3D数学(3)之Vector3类的介绍
  • 15、Python布尔逻辑全解析:运算符优先级、短路特性与实战避坑指南
  • 使用 NGINX 的 `ngx_http_secure_link_module` 模块保护资源链接
  • 编译Qt5.15.16并启用pdf模块
  • 紫光同创FPGA实现AD9238数据采集转UDP网络传输,分享PDS工程源码和技术支持和QT上位机
  • PDF 合并测试:性能与内容完整性
  • 2025-5-19Vue3快速上手
  • 双指针算法:原理与应用详解
  • 大数据实时分析:ClickHouse、Doris、TiDB 对比分析
  • [低代码] 明道云调用本地部署 Dify 的进阶方法
  • 存储系统03——数据缓冲evBuffer
  • 不同类型桥梁的无人机检测内容及技术难度