开发环境 : Qt5.12.7 + Qt creator +vs2017
1、裁剪MVE功能,只留下三维模型文件读取和三维场景渲染
2、利用libQGLViewer 实现三维场景显示、鼠标抓取、动画
二、使用步骤 1.裁剪MVE
主要涉及到以下几个文件:
math:MVE中涉及的自定义vector 、matrix等数据结构及其相应计算
mve:MVE中涉及的网格处理、文件读取、纹理图片读取
ogl:MVE中涉及的顶点着色器、网格着色器、纹理贴图、相机类
util:MVE中涉及的string等自定义工具类
2.libQGLViewer修改
1、libQGLViewer中QGLViewer用于显示三维场景,MVE中用GLWidget显示三维场景,两者均是QOpenGLWidget的子类,参照GLWidget修改QGLViewer如下:
QGLViewer.h
#include "scene_addins/addin_state.h"#include "ogl/context.h"#include <QGLViewer/qglviewer.h>#include <QTimer>class Viewer : public QGLViewer {public:Viewer(); virtual void draw(); virtual void init(); virtual QString helpString() const; void set_context(ogl::Context* context); void resizeGL(int width, int height); void paintGL(void); void mouseMoveEvent(QMouseEvent *event); void wheelEvent(QWheelEvent* event); void mousePressEvent(QMouseEvent *event); void mouseReleaseEvent(QMouseEvent *event);public slots:void repaint_async(void);signals :void sig_replain();private: bool cx_init; int gl_width; int gl_height; ogl::Context* context; qreal device_pixel_ratio =1; std::set<ogl::Context*> init_set; QTimer* repaint_timer;
QGLViewer draw()函数修改:
void Viewer::draw() {if (this->context == 0){std::cout << "context NULL" << std::endl;return;}elsestd::cout << "context Not Null" << std::endl;/* Current context may need initialization. */if (this->cx_init){if (this->init_set.find(this->context) == this->init_set.end()){std::cout << "Using OpenGL " << this->format().majorVersion()<< '.' << this->format().minorVersion() << " ..."<< std::endl;this->context->init();this->context->resize(this->gl_width, this->gl_height);this->init_set.insert(this->context); // Mark initialized}this->cx_init = false;}/* Paint it! */std::cout << "next paint" << std::endl;this->context->paint();}
paintGL函数:
void Viewer::paintGL(void){QGLViewer::paintGL();}
resizeGL函数:
voidViewer::resizeGL(int width, int height){width *= this->device_pixel_ratio;height *= this->device_pixel_ratio;this->gl_width = width;this->gl_height = height;//initLable();if (this->context != nullptr)this->context->resize(width, height);QGLViewer::resizeGL(width, height);}
鼠标响应事件修改,以点击事件为例:
voidViewer::mousePressEvent(QMouseEvent *event){QGLViewer::mousePressEvent(event);this->makeCurrent();ogl::MouseEvent e;e.type = ogl::MOUSE_EVENT_PRESS;e.button = (ogl::MouseButton)event->button();e.button_mask = event->buttons();e.x = event->x() * this->device_pixel_ratio;e.y = event->y() * this->device_pixel_ratio;this->context->mouse_event(e);this->repaint_async();}
添加set_context函数:
void Viewer::set_context(ogl::Context* context){this->context = context;this->cx_init = true;}
2、将AddinManager中GLWidget 替换为Viewer
#include "scene_inspect/addin_manager.h"AddinManager::AddinManager (/*GLWidget*/Viewer* gl_widget, QTabWidget* tab_widget) : tab_widget(tab_widget) , clear_color(6, 60, 107,200) , clear_color_cb(new QCheckBox(/*"Background color"*/QStringLiteral("背景颜色"))){ /* Initialize state and widgets. */ this->state.gl_widget = gl_widget; this->state.ui_needs_redraw = true; this->mesh_renderer = new AddinMeshesRenderer(); /* Register addins. */ this->addins.push_back(this->mesh_renderer);
3、各类将关联初始化:
view = new Viewer();glWidget = new GLWidget;this->addin_manager = new AddinManager(this->view, NULL);this->view->set_context(this->addin_manager);view->setAxisIsDrawn();
4、显示效果:
总结
场景能够加载,渲染正常,但界面无显示,子类化QGLViewer时未重新实现鼠标事件
85676087