使用QT搭建点云显示框架系列一——QGLViewer

12 篇文章 4 订阅
6 篇文章 3 订阅

本文程序不会使用的同学请加我QQ:498771026

我感觉程序猿之间是不用瞎BB的,先讲明白然后直接上干货就行。

我们的主要目的就是利用QT 和 opengl搭建一个点云显示框架,笔者之前也搭建过一个,用的是qt自带的opengl es2,效果还算不错,只是有点大,大家可能吸收不了。文章链接如下,里面有源程序的各种免费下载,就不多说了。

http://blog.csdn.net/qq_30547073/article/details/78496279

后来笔者发现了一个好东西叫做QGLViewer,它的相机做的不错,选点也做得很好。它的官方的学习例子本人已经全部跑通,然后效果都还不错,做了一个框架把它们弄到了一起,QGLViewer这个库也编译好放到一起了,请叫我活雷锋,百度网盘下载链接如下:链接:https://pan.baidu.com/s/1i5MRSbF 密码:ku8f  

我们放几个效果上来:


配置的话,请看我这篇文章:QGLViewer排坑文档

http://blog.csdn.net/qq_30547073/article/details/78954592

我就先简单放一段代码上来,告诉大家怎么把QGLViewer加到主窗体里面去,当然只放核心代码。想要全部代码就加我QQ498771026 或者去我的网盘里面下就行了,链接在上面。

主窗体:

#include "ninqglstudy.h"
#include "ui_ninqglstudy.h"
#include <QApplication>
#include <QDesktopWidget>
#include <QGLViewer/manipulatedCameraFrame.h>
#include <QHBoxLayout>
#include <QSplitter>
#include <QGLFormat>
#include <QDesktopWidget>
#include <QLabel>
#include <QDebug>
using namespace qglviewer;

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

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

void NinQGLStudy::do_SetCenterWidget(QWidget* viewer)
{
    this->setCentralWidget(viewer);
    viewer->setAttribute(Qt::WA_DeleteOnClose);//关闭立刻释放
    viewer->show();
    statusBar()->showMessage("Case loaded!", 3000);
    do_ResetWindow();
}
//if we have two frame
void NinQGLStudy::do_SetCenterWidget(QWidget* viewer1 ,QWidget*viewer2)
{
    QWidget *CenterWidget = new QWidget();
    QHBoxLayout* myHBoxLayout = new QHBoxLayout();
    myHBoxLayout->addWidget(viewer1);
    myHBoxLayout->addWidget(viewer2);
    viewer1->show();
    viewer2->show();
    CenterWidget->setLayout(myHBoxLayout);
    this->setCentralWidget(CenterWidget);
    //statusBar()->showMessage("Case loaded!", 3000);
    do_ResetWindow(1200,600);
}
void NinQGLStudy::Start()
{
    Init_Window();
    Init_StatusBar();
    do_ResetWindow();
}
void NinQGLStudy::Init_StatusBar()
{
    QLabel *msgLabel = new QLabel;
    msgLabel->setMinimumSize(150, 20); // 设置标签最小大小
    msgLabel->setFrameShape(QFrame::WinPanel); // 设置标签形状
    msgLabel->setFrameShadow(QFrame::Sunken); // 设置标签阴影
    msgLabel->setAlignment(Qt::AlignHCenter);
    msgLabel->setText(tr("Ninja Scarlet"));
    statusBar()->addWidget(msgLabel);
    statusBar()->setSizeGripEnabled(true);
}
void NinQGLStudy::Init_Window()
{
    QDesktopWidget* desktopWidget = QApplication::desktop();
    //Availibal desktop 1920x1040
    QRect deskRect = desktopWidget->availableGeometry();
    //Get full screen size 1920 x 1080
    QRect screenRect = desktopWidget->screenGeometry();
    int ExpectW = deskRect.width()/4*3;
    int ExpectH = deskRect.height()/4*3;
    this->resize(ExpectW,ExpectH);
}
void NinQGLStudy::do_ResetWindow()
{
    QDesktopWidget* desktopWidget = QApplication::desktop();
    //Availibal desktop 1920x1040
    QRect deskRect = desktopWidget->availableGeometry();
    //Get full screen size 1920 x 1080
    QRect screenRect = desktopWidget->screenGeometry();
    int ExpectW = deskRect.width()/4*3;
    int ExpectH = deskRect.height()/4*3;
    this->resize(ExpectW,ExpectH);
//    this->move((QApplication::desktop()->width() - this->width()) / 2,
//               (QApplication::desktop()->height() - this->height()) / 2);
    //this->setWindowFlags(this->windowFlags() | Qt::WindowStaysOnTopHint);
    this->activateWindow();
}
void NinQGLStudy::do_ResetWindow(int Width ,int Height)
{
    this->resize(Width,Height);
//    this->move((QApplication::desktop()->width() - this->width()) / 2,
//               (QApplication::desktop()->height() - this->height()) / 2);
    this->activateWindow();
}

//Basic example=======================
void NinQGLStudy::do_ShowExample_SimpleViewer()
{
    Example_simpleViewer* viewer = new Example_simpleViewer();
    do_SetCenterWidget(viewer);
}
example_cpp:

下面这段代码就是QGLViewer里面的具体绘制,本人擅自加了一点绘制立方体的代码,我们可以选择绘制立方体或者QGLViewer官方的那一坨彩色的翔:

#include "example_simpleviewer.h"
#include <QString>
Example_simpleViewer::Example_simpleViewer()
{

}

void DrawCube()
{
    glBegin(GL_QUADS);
    //0----------------------------
    glNormal3f( 0.0F, 0.0F, 1.0F);
    glVertex3f( 0.5f, 0.5f, 0.5f);
    glVertex3f(-0.5f, 0.5f, 0.5f);
    glVertex3f(-0.5f,-0.5f, 0.5f);
    glVertex3f( 0.5f,-0.5f, 0.5f);
    //1----------------------------
    //this is to show the face is remnant
    glNormal3f( 0.0F, 0.0F,-1.0F);
    glVertex3f(-0.2f,-0.5f,-0.5f);
    glVertex3f(-0.5f, 0.5f,-0.5f);
    glVertex3f( 0.5f, 0.5f,-0.5f);
    glVertex3f( 0.5f,-0.5f,-0.5f);
    //2----------------------------
    glNormal3f( 0.0F, 1.0F, 0.0F);
    glVertex3f( 0.5f, 0.5f, 0.5f);
    glVertex3f( 0.5f, 0.5f,-0.5f);
    glVertex3f(-0.5f, 0.5f,-0.5f);
    glVertex3f(-0.5f, 0.5f, 0.5f);
    //3----------------------------
    glNormal3f( 0.0F,-1.0F, 0.0F);
    glVertex3f(-0.5f,-0.5f,-0.5f);
    glVertex3f( 0.5f,-0.5f,-0.5f);
    glVertex3f( 0.5f,-0.5f, 0.5f);
    glVertex3f(-0.5f,-0.5f, 0.5f);
    //4----------------------------
    glNormal3f( 1.0F, 0.0F, 0.0F);
    glVertex3f( 0.5f, 0.5f, 0.5f);
    glVertex3f( 0.5f,-0.5f, 0.5f);
    glVertex3f( 0.5f,-0.5f,-0.5f);
    glVertex3f( 0.5f, 0.5f,-0.5f);
    //5----------------------------
    glNormal3f(-1.0F, 0.0F, 0.0F);
    glVertex3f(-0.5f,-0.5f,-0.5f);
    glVertex3f(-0.5f,-0.5f, 0.5f);
    glVertex3f(-0.5f, 0.5f, 0.5f);
    glVertex3f(-0.5f, 0.5f,-0.5f);
    glEnd();
}

void DrawShit()
{
    const float nbSteps = 200.0;
    glBegin(GL_QUAD_STRIP);
    for (int i = 0; i < nbSteps; ++i)
    {
      const float ratio = i / nbSteps;
      const float angle = 21.0 * ratio;
      const float c = cos(angle);
      const float s = sin(angle);
      const float r1 = 1.0 - 0.8f * ratio;
      const float r2 = 0.8f - 0.8f * ratio;
      const float alt = ratio - 0.5f;
      const float nor = 0.5f;
      const float up = sqrt(1.0 - nor * nor);
      glColor3f(1.0 - ratio, 0.2f, ratio);
      glNormal3f(nor * c, up, nor * s);
      glVertex3f(r1 * c, alt, r1 * s);
      glVertex3f(r2 * c, alt + 0.05f, r2 * s);
    }
    glEnd();
}

void DrawColorCube()
{
    //glPushMatrix();
    {
        glBegin(GL_POLYGON); //前表面
        glColor3ub((GLubyte)255,(GLubyte)255,(GLubyte)255);//颜色设置为白色
        glVertex3f(50.0f,50.0f,50.0f);

        glColor3ub((GLubyte)255,(GLubyte)255,(GLubyte)0);//颜色设置为黄色
        glVertex3f(50.0f,-50.0f,50.0f);

        glColor3ub((GLubyte)255,(GLubyte)0,(GLubyte)0);//颜色设置为红色
        glVertex3f(-50.0f,-50.0f,50.0f);

        glColor3ub((GLubyte)255,(GLubyte)0,(GLubyte)255);//颜色设置为白色
        glVertex3f(-50.0f,50.0f,50.0f);
        glEnd();

        glBegin(GL_POLYGON); //后表面
        glColor3f(0.0f,1.0f,1.0f);//颜色设置为青色
        glVertex3f(50.0f,50.0f,-50.0f);

        glColor3f(0.0f,1.0f,0.0f);//颜色设置为绿色
        glVertex3f(50.0f,-50.0f,-50.0f);

        glColor3f(0.0f,0.0f,0.0f);//颜色设置为黑色
        glVertex3f(-50.0f,-50.0f,-50.0f);

        glColor3f(0.0f,0.0f,1.0f);//颜色设置为蓝色
        glVertex3f(-50.0f,50.0f,-50.0f);
        glEnd();

        glBegin(GL_POLYGON); //上表面
        glColor3d(0.0,1.0,1.0);//颜色设置为青色
        glVertex3f(50.0f,50.0f,-50.0f);

        glColor3d(1.0,1.0,1.0);//颜色设置为白色
        glVertex3f(50.0f,50.0f,50.0f);

        glColor3d(1.0,0.0,1.0);//颜色设置为品红色
        glVertex3f(-50.0f,50.0f,50.0f);

        glColor3d(0.0,0.0,1.0);//颜色设置为蓝色
        glVertex3f(-50.0f,50.0f,-50.0f);
        glEnd();

        glBegin(GL_POLYGON); //下表面
        glColor3ub(0u,255u,0u);//颜色设置为绿色
        glVertex3f(50.0f,-50.0f,-50.0f);

        glColor3ub(255u,255u,0u);//颜色设置为黄色
        glVertex3f(50.0f,-50.0f,50.0f);

        glColor3ub(255u,0u,0u);//颜色设置为红色
        glVertex3f(-50.0f,-50.0f,50.0f);

        glColor3ub(0u,0u,0u);//颜色设置为黑色
        glVertex3f(-50.0f,-50.0f,-50.0f);
        glEnd();

        glBegin(GL_POLYGON); //左表面
        glColor3ub((GLubyte)255,(GLubyte)255,(GLubyte)255);//颜色设置为白色
        glVertex3f(50.0f,50.0f,50.0f);

        glColor3ub((GLubyte)0,(GLubyte)255,(GLubyte)255);//颜色设置为青色
        glVertex3f(50.0f,50.0f,-50.0f);

        glColor3ub((GLubyte)0,(GLubyte)255,(GLubyte)0);//颜色设置为绿色
        glVertex3f(50.0f,-50.0f,-50.0f);

        glColor3ub((GLubyte)255,(GLubyte)255,(GLubyte)0);//颜色设置为黄色
        glVertex3f(50.0f,-50.0f,50.0f);
        glEnd();

        glBegin(GL_POLYGON); //右表面
        glColor3f(1.0f,0.0f,1.0f);//颜色设置为品红色
        glVertex3f(-50.0f,50.0f,50.0f);

        glColor3f(0.0f,0.0f,1.0f);//颜色设置为蓝色
        glVertex3f(-50.0f,50.0f,-50.0f);

        glColor3f(0.0f,0.0f,0.0f);//颜色设置为黑色
        glVertex3f(-50.0f,-50.0f,-50.0f);

        glColor3f(1.0f,0.0f,0.0f);//颜色设置为红色
        glVertex3f(-50.0f,-50.0f,50.0f);
        glEnd();
    }
    //glPopMatrix();
}

void Example_simpleViewer::draw()
{
    //DrawColorCube();
    DrawCube();
    //DrawShit();
}

void Example_simpleViewer::init()
{
  // Restore previous viewer state.
  //the program can done without this function
  restoreStateFromFile();

  // Opens help window
  help();
}

QString Example_simpleViewer::helpString() const
{
    QString text("<h2>S i m p l e V i e w e r</h2>");
    text += "Use the mouse to move the camera around the object. ";
    text += "You can respectively revolve around, zoom and translate with the "
          "three mouse buttons. ";
    text += "Left and middle buttons pressed together rotate around the camera "
          "view direction axis<br><br>";
    text += "Pressing <b>Alt</b> and one of the function keys "
          "(<b>F1</b>..<b>F12</b>) defines a camera keyFrame. ";
    text += "Simply press the function key again to restore it. Several "
          "keyFrames define a ";
    text += "camera path. Paths are saved when you quit the application and "
          "restored at next start.<br><br>";
    text +=
      "Press <b>F</b> to display the frame rate, <b>A</b> for the world axis, ";
    text += "<b>Alt+Return</b> for full screen mode and <b>Control+S</b> to save "
          "a snapshot. ";
    text += "See the <b>Keyboard</b> tab in this window for a complete shortcut "
          "list.<br><br>";
    text += "Double clicks automates single click actions: A left button double "
          "click aligns the closer axis with the camera (if close enough). ";
    text += "A middle button double click fits the zoom of the camera and the "
          "right button re-centers the scene.<br><br>";
    text += "A left button double click while holding right button pressed "
          "defines the camera <i>Revolve Around Point</i>. ";
    text += "See the <b>Mouse</b> tab and the documentation web pages for "
          "details.<br><br>";
    text += "Press <b>Escape</b> to exit the viewer.";
    return text;
}
头文件是很简单的:

#ifndef EXAMPLE_SIMPLEVIEWER_H
#define EXAMPLE_SIMPLEVIEWER_H

#include <QGLViewer/qglviewer.h>

class Example_simpleViewer: public QGLViewer
{
public:
    Example_simpleViewer();
protected:
  virtual void draw();
  virtual void init();
  virtual QString helpString() const;
};

#endif // EXAMPLE_SIMPLEVIEWER_H



  • 9
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值