qt结合vs,opengl基础示例

一、一些准备工作:

          

1.安装Qt for VS 的插件;

        安装Qt for VS 的插件

        安装Qt 4.8.06

 2.进行一些设置:

        找到菜单项:

        Qt——> Qt Option——> 选取版本   


——>  ——>

在工程选项中添加必须的包含文件和lib文件

然后可以使用Qt


二、工程和代码:

Vs控制台工程可以直接使用Qt的显示框架,使用类似于Qt-IDE的主函数代码:

[cpp]  view plain  copy
  1. int _tmain(int argc, char* argv[])  
  2. {  
  3.     QApplication a(argc, argv);  
  4.     CPlot *objViewer = new CPlot();  
  5.     objViewer->show();  
  6.       
  7.     return a.exec();  
  8. }  


头文件代码:

[cpp]  view plain  copy
  1. #pragma once  
  2. /* 
  3. */  
  4.   
  5. #include <QApplication>  
  6. #include <QMainWindow>  
  7. #include <QWidget>  
  8. #include <QAction>  
  9. #include <QMenu>  
  10. #include <QToolBar>  
  11. #include <QGLWidget>  
  12.   
  13. #include <gl/glut.h>  
  14.   
  15. #include <QMainWindow>  
  16. #include <QWidget>  
  17. #include <QAction>  
  18. #include <QMenu>  
  19. #include <QToolBar>  
  20. #include "OpenGLViewer.h"  
  21.   
  22. class QApplication;  
  23. class QMainWindow;  
  24. class QWidget;  
  25. class QAction;  
  26. class QMenu;  
  27. class QToolBar;  
  28.   
  29. class CPlot: public QMainWindow{  
  30.   
  31.     Q_OBJECT  
  32.   
  33. public:  
  34.     CPlot(QWidget *parent = 0);  
  35.     //CPlot();  
  36.     ~CPlot(void);  
  37.   
  38.     //测试OpenGL画图  
  39. public:  
  40.     static void RenderScene();  
  41.     void SetupRC();  
  42.     static void ChangeSize( GLsizei w, GLsizei h );  
  43.     void drawCircle(int argc, char *argv[]);  
  44.   
  45.     GLfloat boundingRadius;  
  46.     GLfloat LightDistanceRatio;  
  47.     GLfloat rotationX;  
  48.     GLfloat rotationY;  
  49.     GLfloat rotationZ;  
  50.     GLfloat xscale;  
  51.     GLfloat yscale;  
  52.     GLfloat zscale;  
  53.     GLfloat transX;  
  54.     GLfloat transY;  
  55.     GLfloat transZ;  
  56.   
  57.     void draw3dAxis();  
  58.     void draw3dAxis(int argc, char *argv[]);  
  59.     void updatePos(const Mat  &rMat);  
  60.   
  61. private:  
  62.     CEkfSlam m_Slamer;  
  63.   
  64.     private slots:  
  65.         void openFile();  
  66.         //void closeFile();  
  67.         void segmentObj();  
  68.         void capture();  
  69.   
  70. private:  
  71.     OpenGLViewer *openglViewer;  
  72.   
  73. private:  
  74.     void initializeGL();  
  75.     void setMaterial();  
  76.     void GLMaterial(const OpenGL::Material& material);  
  77.     void setLight();  
  78.     void setAntiAliasing();  
  79.     float getBoundingRadius();  
  80.     void setTexture(IplImage* img);  
  81.     void loadTexture();  
  82.     void resetGLLightPosition();  
  83.   
  84.     void loadMeshFile(char* filename);  
  85.     void createActions();  
  86.     void createMenus();  
  87.     void createToolBars();  
  88.   
  89. private:  
  90.   
  91.     QAction *loadFileAction;  
  92.     QAction *closeFileAction;  
  93.     QAction *segmentObjAction;  
  94.     QAction *captureAction;  
  95.   
  96.     QMenu *fileMenu;  
  97.     QMenu *toolMenu;  
  98.   
  99.     QToolBar *fileToolBar;  
  100.     QToolBar *toolsBar;  
  101.   
  102.     vector<int> faceColors;  
  103.     vector<QColor> FaceColorList;  
  104.     vector<double>ssdf;  
  105.     QPoint lastPos;  
  106.     Core::Geometry::MyMesh *mesh;  
  107.     int key_type;  
  108.     GLuint texName;  
  109.     Mat textImage;  
  110.   
  111. public:  
  112.     //1.使用 OpenCV Mat画图!用于显示图像和特征点匹配!  
  113.     cv::Mat  m_Canvas;  
  114.     cv::Mat  m_CanvasSrc;  
  115. private:  
  116.     //2.使用 VTK画出点云!用于显示地图和方位演化!  
  117.     Eigen::MatrixXf  m_FeatureMap;  
  118.   
  119.     //画出十字光标  
  120.     int cvDrawCrossCursor(  
  121.         cv::Mat &Canvas,cv::Point &PointS,cv::Point &PointE,cv::Scalar &Color,int Width,int CV_A_Type,int Mark);  
  122.     //画十字光标,中心点、线长度、色彩、线宽  
  123.     int cvDrawCrossCursor(  
  124.         cv::Mat &Canvas,cv::Point &Center,int Length,cv::Scalar &Color,int Width,int CV_A_Type,int Mark);  
  125.   
  126. };  

源码文件代码:

[cpp]  view plain  copy
  1. #include "StdAfx.h"  
  2. #include "Plot.h"  
  3.   
  4.   
  5. #include <iostream>  
  6. #include <iomanip>  
  7. #include <fstream>  
  8. #include <QApplication>  
  9. #include <QFileDialog>  
  10. #include <QString>  
  11. #include <QMenuBar>  
  12. #include <QDesktopWidget>  
  13. #include <opencv/cv.h>  
  14. #include <opencv/highgui.h>  
  15. #include <vector>  
  16.   
  17. class QFileDialog;  
  18. class QString;  
  19. class QMenuBar;  
  20. class QDesktopWidget;  
  21.   
  22. using namespace Qt;  
  23. using namespace OpenGL;  
  24.   
  25.   
  26. 此种用法是错误的,使用mainwindow之前必须构建一个Application!  
  27. CPlot::CPlot(QWidget *parent) : QMainWindow(parent){  
  28.     openglViewer = new OpenGLViewer();  
  29.     this->setCentralWidget(openglViewer);  
  30.     this->setWindowTitle("Wishchin's PCL Window");  
  31.     this->setGeometry((QApplication::desktop()->width()-1.5 *QApplication::desktop()->height())/2,20,640,480);  
  32.   
  33.     this->createActions();  
  34.     this->createMenus();  
  35.     this->createToolBars();  
  36.   
  37.     //初始化画布  
  38.     this->m_Canvas.create(640,480,CV_8UC3);  
  39.     this->m_CanvasSrc.create(640,480,CV_8UC3);  
  40.   
  41.     //this->m_FeatureMap.resize(0);  
  42. }  
  43.   
  44. //CPlot::CPlot(){  
  45. //  //初始化画布  
  46. //  this->m_Canvas.create(640,480,CV_8UC3);  
  47. //  
  48. //  //this->m_FeatureMap.resize(0);  
  49. //}  
  50.   
  51.   
  52. CPlot::~CPlot(void)  
  53. {  
  54. }  
  55.   
  56. void CPlotMark0022(){}  
  57.   
  58. //画出十字光标  
  59. int CPlot::cvDrawCrossCursor(  
  60.     cv::Mat &Canvas,cv::Point &PointS,cv::Point &PointE,cv::Scalar &Color,int Width,int CV_A_Type,int Mark)  
  61. {  
  62.   
  63.   
  64.     return 1;  
  65. }  
  66.   
  67. //画十字光标,中心点、线长度、色彩、线宽  
  68. int CPlot::cvDrawCrossCursor(  
  69.     cv::Mat &Canvas,cv::Point &Center,int Length,cv::Scalar &Color,int Width,int CV_A_Type,int Mark)  
  70. {  
  71.     int H = Length/2;  
  72.     cv::Point PointS;cv::Point PointE;  
  73.     PointS.x =Center.x ;  
  74.     PointS.y =Center.y -H;  
  75.     PointE.x =Center.x ;  
  76.     PointE.y =Center.y +H;  
  77.   
  78.     cv::line(this->m_Canvas,PointS,PointE,Color,Width,CV_A_Type,Mark);  
  79.   
  80.     PointS.x =Center.x -H;  
  81.     PointS.y =Center.y;  
  82.     PointE.x =Center.x +H;  
  83.     PointE.y =Center.y;  
  84.     cv::line(this->m_Canvas,PointS,PointE,Color,Width,CV_A_Type,Mark);  
  85.   
  86.     return 1;  
  87. }  
  88.   
  89. void CPlotMark003(){}  
  90.   
  91. void CPlot::draw3dAxis(int argc, char *argv[])  
  92. {  
  93.     Mat rMat(1,3,CV_32F);  
  94.     rMat.at<float>(0,0) = 1;  
  95.     if (1<0)  
  96.         rMat.at<float>(0,1) = 1;  
  97.     else  
  98.         rMat.at<float>(0,1) = 1;  
  99.     if(1<0)  
  100.         rMat.at<float>(0,2) = 1;  
  101.     else  
  102.         rMat.at<float>(0,2) = 1;  
  103.   
  104.   
  105.     this->updatePos(rMat);  
  106.     openglViewer->updateGL();  
  107.   
  108.     return;  
  109. }  
  110.   
  111. void CPlot::updatePos(const Mat& rMat)  
  112. {  
  113.     Mat rrMat;  
  114.     rMat.convertTo(rrMat , CV_32F);  
  115.     //rMat.convertTo(rMat , CV_32F);  
  116.     rotationX = rrMat.at<float>(0,0);   
  117.     rotationY = rrMat.at<float>(0,1);  
  118.     rotationZ = rrMat.at<float>(0,2);  
  119. }  
  120.   
  121. void CPlot::draw3dAxis()  
  122. {  
  123.   
  124.     GLfloat x = GLfloat(640) / 480;  
  125.     glMatrixMode(GL_MODELVIEW);   
  126.     glLoadIdentity();  
  127.     gluLookAt(-2.0 , 6.0, -4.0, 0.0 , 0.0 , 0.0 , 0.0, 1.0 , 0.0);  
  128.   
  129.     float len = 0.2;  
  130.   
  131.     //The world axis /coordinate system!  
  132.     //1. Line!  
  133.     glColor3f(0.0f,0.0f,1.0f);    
  134.     glBegin(GL_LINES);    
  135.   
  136.     glVertex3f(-2.0f,00.0f,0.0f);//X line!  
  137.     glVertex3f(2.0f,0.0f,0.0f);    
  138.   
  139.     glVertex3f(0.0f,-2.0f,0.0f); //Y line!   
  140.     glVertex3f(0.0f,2.0f,0.0f);    
  141.   
  142.     glVertex3f(0.0f,0.0f,-2.0f); //Z line!   
  143.     glVertex3f(0.0f,0.0f,2.0f);    
  144.     glEnd();    
  145.   
  146.     //The world axis /coordinate system!  
  147.     //2. arrows!  
  148.     glColor3f(1.0f,0.0f,0.0f); // x arrows   
  149.     glPushMatrix();    
  150.     glTranslatef(2.0f,0.0f,0.0f);    
  151.     glRotatef(90.0f,0.0f,1.0f,0.0f);    
  152.     glutSolidCone(0.1,0.3,10,10);    
  153.     glTranslatef(0.0f,0.0f,0.4f);   
  154.     glBegin(GL_LINES);    
  155.     glVertex3f(-len,len,0.0f);    
  156.     glVertex3f(len,-len,0.0f);     
  157.     glEnd();   
  158.     glBegin(GL_LINES);    
  159.     glVertex3f(len,len,0.0f);    
  160.     glVertex3f(-len,-len,0.0f);     
  161.     glEnd();   
  162.     glPopMatrix();    
  163.   
  164.     glColor3f(0.0f,1.0f,0.0f); // y arrows   
  165.     glPushMatrix();    
  166.     glTranslatef(0.0f,2.0f,0.0f);    
  167.     glRotatef(-90.0f,1.0f,0.0f,0.0f);    
  168.     glutSolidCone(0.1,0.3,10,10);    
  169.     glTranslatef(0.0f,0.0f,0.4f);   
  170.     glBegin(GL_LINES);    
  171.     glVertex3f(-len,len,0.0f);    
  172.     glVertex3f(0,0,0.0f);     
  173.     glEnd();   
  174.     glBegin(GL_LINES);    
  175.     glVertex3f(len,len,0.0f);    
  176.     glVertex3f(0,0,0.0f);     
  177.     glEnd();   
  178.     glBegin(GL_LINES);    
  179.     glVertex3f(0,-len,0.0f);    
  180.     glVertex3f(0,0,0.0f);     
  181.     glEnd();   
  182.     glPopMatrix();    
  183.   
  184.     glColor3f(0.0f,0.0f,1.0f); // z  arrows  
  185.     glPushMatrix();    
  186.     glTranslatef(0.0f,0.0f,2.0f);    
  187.     glRotatef(90.0f,0.0f,0.0f,1.0f);    
  188.     glutSolidCone(0.1,0.3,10,10);    
  189.     glTranslatef(0.0f,0.0f,0.4);   
  190.     glTranslatef(0.0f,0.0f,0.4f);   
  191.     glBegin(GL_LINES);    
  192.     glVertex3f(-len,len,0.0f);    
  193.     glVertex3f(len,len,0.0f);     
  194.     glEnd();   
  195.     glBegin(GL_LINES);    
  196.     glVertex3f(len,len,0.0f);    
  197.     glVertex3f(-len,-len,0.0f);     
  198.     glEnd();   
  199.     glBegin(GL_LINES);    
  200.     glVertex3f(-len,-len,0.0f);    
  201.     glVertex3f(len,-len,0.0f);     
  202.     glEnd();   
  203.     glPopMatrix();   
  204.   
  205.     glTranslatef(transX,transY,-transZ);  
  206.     glRotatef(rotationX , 1.0,0.0,0.0);  
  207.     glRotatef(rotationY , 0.0,1.0,0.0);  
  208.     glRotatef(rotationZ , 0.0,0.0,1.0);  
  209.     glScalef(xscale, yscale, zscale);  
  210.   
  211.     //The Cube aixs / coordinate system!  
  212.     //1. The axis line!   
  213.     glColor3f(1.0f,1.0f,1.0f);    
  214.     glBegin(GL_LINES);    
  215.     glVertex3f(-1.2f,00.0f,0.0f);    
  216.     glVertex3f(1.2f,0.0f,0.0f);    
  217.     glVertex3f(0.0f,-1.2f,0.0f);    
  218.     glVertex3f(0.0f,1.2f,0.0f);    
  219.     glVertex3f(0.0f,0.0f,-1.2f);    
  220.     glVertex3f(0.0f,0.0f,1.2f);    
  221.     glEnd();    
  222.   
  223.     //The Cube aixs / coordinate system!  
  224.     //2. The axis arrow!  
  225.     glColor3f(1.0f,0.0f,0.0f); //x arrow  
  226.     glPushMatrix();    
  227.     glTranslatef(1.2f,0.0f,0.0f);    
  228.     glRotatef(90.0f,0.0f,1.0f,0.0f);    
  229.     glutSolidCone(0.05,0.15,10,10);    
  230.     glPopMatrix();    
  231.   
  232.     glColor3f(0.0f,1.0f,0.0f); // y    
  233.     glPushMatrix();    
  234.     glTranslatef(0.0f,1.2f,0.0f);    
  235.     glRotatef(-90.0f,1.0f,0.0f,0.0f);    
  236.     glutSolidCone(0.05,0.15,10,10);    
  237.     glPopMatrix();    
  238.   
  239.     glColor3f(0.0f,0.0f,1.0f); // z    
  240.     glPushMatrix();    
  241.     glTranslatef(0.0f,0.0f,1.2f);    
  242.     glRotatef(90.0f,0.0f,0.0f,1.0f);    
  243.     glutSolidCone(0.05,0.15,10,10);    
  244.     glPopMatrix();   
  245.   
  246.     The Cube Model  
  247.     //for(int i=0;i<mesh->getFCount();i++){  
  248.   
  249.     //  glLoadName(i);  
  250.     //  glBegin(GL_TRIANGLES);  
  251.     //  double r,g,b;  
  252.     //  FaceColorList[1].getRgbF(&r,&g,&b);  
  253.     //  glColor3d(r,g,b);  
  254.     //  for(int j=0;j<3;j++){  
  255.     //      MyPoint_ p = mesh->getPoint(mesh->getFace(i).getRef(j));  
  256.     //      glNormal3d(p.GetNormal()[0],p.GetNormal()[1],p.GetNormal()[2]);  
  257.     //      glVertex3f(p.GetPoint()[0], p.GetPoint()[1], p.GetPoint()[2]);  
  258.     //  }  
  259.     //  glEnd();  
  260.     //}  
  261.     //glFlush();  
  262.   
  263. }  
  264. //测试使用OpenGL画圆!  
  265. void CPlot::drawCircle(int argc, char *argv[])  
  266. {  
  267.     glutInit(&argc, argv);  
  268.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);  
  269.     glutInitWindowSize (400, 300);  
  270.     glutInitWindowPosition (100, 100);  
  271.     glutCreateWindow( "Point examples" );  
  272.     glutDisplayFunc( this->RenderScene );  
  273.     glutReshapeFunc( this->ChangeSize );  
  274.     SetupRC();  
  275.     glutMainLoop();  
  276.   
  277.     return ;  
  278. }  
  279. void CPlot::RenderScene()  
  280. {  
  281.     //清空颜色缓冲区,填充的颜色由 glClearColor( 0, 0.0, 0.0, 1 ); 指定为黑色  
  282.     glClear( GL_COLOR_BUFFER_BIT );  
  283.   
  284.     //绘制一个点  
  285.     {  
  286.         glColor3f( 1.0f, 0.0f, 0.0f );//指定点的颜色,红色  
  287.         glPointSize( 9 );//指定点的大小,9个像素单位  
  288.   
  289.         glBegin( GL_POINTS );//开始画点  
  290.         {  
  291.             glVertex3f(0.0f, 0.0f, 0.0f); // 在坐标为(0,0,0)的地方绘制了一个点  
  292.         }  
  293.         glEnd();//结束画点  
  294.     }  
  295.   
  296.     //绘制一个点圆  
  297.   
  298.     {  
  299.         glColor3f( 0.0f, 1.0f, 0.0f );//指定点的颜色,绿色  
  300.         glPointSize( 3 );//指定点的大小,3个像素单位  
  301.   
  302.         glBegin( GL_POINTS );  
  303.         {  
  304. #define PI 3.14159f  
  305. #define RADIUS 50.f  
  306.             GLfloat x = 0, y = 0, angle = 0.0;  
  307.             for ( angle = 0; angle <= 2.0f * PI; angle += 0.1f )  
  308.             {  
  309.                 x = RADIUS * sin( angle );  
  310.                 y = RADIUS * cos( angle );  
  311.                 glVertex3f( x, y, 0 );  
  312.             }  
  313.         }  
  314.         glEnd();  
  315.     }  
  316.   
  317.     //绘制x、y坐标轴  
  318.     {  
  319.         glColor3f( 0.0f, 0.0f, 1.0f );//指定线的颜色,蓝色  
  320.         glBegin( GL_LINES );  
  321.         {  
  322.             // x-axis  
  323.             glVertex3f( -100.0f, 0.0f, 0.0f);  
  324.             glVertex3f( 100.0f, 0.0f, 0.0f);  
  325.   
  326.             // x-axis arrow  
  327.             glVertex3f( 100.0f, 0.0f, 0.0f);  
  328.             glVertex3f( 93.0f, 3.0f, 0.0f);  
  329.             glVertex3f( 100.0f, 0.0f, 0.0f);  
  330.             glVertex3f( 93.0f,-3.0f, 0.0f);  
  331.   
  332.             // y-axis  
  333.             glVertex3f( 0.0f, -100.0f, 0.0f);  
  334.             glVertex3f( 0.0f, 100.0f, 0.0f);  
  335.             glVertex3f( 0.0f, 100.0f, 0.0f);  
  336.             glVertex3f( 3.0f, 93.0f, 0.0f);  
  337.             glVertex3f( 0.0f, 100.0f, 0.0f);  
  338.             glVertex3f( -3.0f, 93.0f, 0.0f);  
  339.         }  
  340.         glEnd();  
  341.     }  
  342.   
  343.   
  344.     glutSwapBuffers();  
  345. }  
  346.   
  347. void CPlot::SetupRC()  
  348. {  
  349.     glClearColor( 0, 0.0, 0.0, 1 );  
  350.     glColor3f( 1.0f, 0.0f, 0.0f );  
  351. }  
  352.   
  353. void CPlot::ChangeSize( GLsizei w, GLsizei h )  
  354. {  
  355.     GLfloat nRange = 100.0f;  
  356.   
  357.     // Prevent a divide by zero  
  358.     if(h == 0)  
  359.         h = 1;  
  360.   
  361.     // Set Viewport to window dimensions  
  362.     glViewport(0, 0, w, h);  
  363.   
  364.     // Reset projection matrix stack  
  365.     glMatrixMode(GL_PROJECTION);  
  366.     glLoadIdentity();  
  367.   
  368.     // Establish clipping volume (left, right, bottom, top, near, far)  
  369.     if (w <= h)  
  370.         glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange);  
  371.     else  
  372.         glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange);  
  373.   
  374.     // Reset Model view matrix stack  
  375.     glMatrixMode(GL_MODELVIEW);  
  376.     glLoadIdentity();  
  377. }  
  378.   
  379. void CPlotMark004(){}  
  380.   
  381. void CPlot::initializeGL()  
  382. {  
  383.     //loadTexture();  
  384.     loadMeshFile("cub.off");//loadMeshFile("sword.off");//  
  385.     //qglClearColor(QColor(204,204,204)/*Qt::white*/);//暂时注销,wishchin!!!  
  386.   
  387.     glShadeModel(GL_SMOOTH);  
  388.     glClearDepth(1.0f);  
  389.     glEnable(GL_DEPTH_TEST);  
  390.     glEnable(GL_NORMALIZE);  
  391.     glLineWidth(1.5);  
  392.   
  393.     //glColor3d(100,100,100);  
  394.     setMaterial();  
  395.     setLight();  
  396. }  
  397. void CPlot::setLight() {  
  398.     GLfloat light_ambient[] = {0.0f, 0.0f, 0.0f, 1.0f};  
  399.     GLfloat light_diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};  
  400.     GLfloat light_specular[]= {1.0f, 1.0f, 1.0f, 1.0f};  
  401.   
  402.     glLightfv( GL_LIGHT0, GL_AMBIENT, light_ambient);  
  403.     glLightfv( GL_LIGHT0, GL_DIFFUSE, light_diffuse);  
  404.     glLightfv( GL_LIGHT0, GL_SPECULAR, light_specular);  
  405.   
  406.     glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);  
  407.     //glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL  , GL_RGB);  
  408.     //glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL , GL_SEPARATE_SPECULAR_COLOR);  
  409.   
  410.     glEnable(GL_LIGHTING);  
  411.     glEnable(GL_LIGHT0);  
  412.   
  413.     resetGLLightPosition();  
  414. }  
  415.   
  416. void CPlot::setMaterial() {  
  417.     glEnable(GL_COLOR_MATERIAL);  
  418.     GLMaterial(  
  419.         OpenGL::Material::GetMaterial(OpenGL::Material::Default));  
  420. }  
  421. void CPlot::GLMaterial(const OpenGL::Material& material)  
  422. {     
  423.     glMaterialfv(GL_FRONT, GL_DIFFUSE, material.diffuse);  
  424.     glMaterialfv(GL_FRONT, GL_SPECULAR, material.specular);  
  425.     glMaterialfv(GL_FRONT, GL_AMBIENT, material.ambient);  
  426.     glMaterialf(GL_FRONT, GL_SHININESS, material.shininess);  
  427.   
  428.     glMaterialfv(GL_BACK, GL_DIFFUSE, material.diffuse);  
  429.     glMaterialfv(GL_BACK, GL_SPECULAR, material.specular);  
  430.     glMaterialfv(GL_BACK, GL_AMBIENT, material.ambient);  
  431.     glMaterialf(GL_BACK, GL_SHININESS, material.shininess);  
  432. }  
  433. void CPlot::setAntiAliasing() {  
  434.     glEnable ( GL_POLYGON_SMOOTH );  
  435.     glEnable( GL_LINE_SMOOTH );  
  436.     glEnable ( GL_POINT_SMOOTH );  
  437.   
  438.     glEnable(GL_BLEND);  
  439.     glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);  
  440.   
  441.     glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST );  
  442.     glHint(GL_LINE_SMOOTH_HINT, GL_NICEST );  
  443.     glHint(GL_POINT_SMOOTH_HINT, GL_NICEST );  
  444.     glEnable(GL_COLOR_MATERIAL);  
  445. }  
  446.   
  447. void CPlot::setTexture(IplImage* img){  
  448.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);    
  449.     glGenTextures(1, &texName);    
  450.     glBindTexture(GL_TEXTURE_2D, texName);    
  451.   
  452.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);    
  453.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);    
  454.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);    
  455.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);    
  456.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height,     
  457.         0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData);  
  458.     glEnable(GL_TEXTURE_2D);    
  459.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);    
  460.     glBindTexture(GL_TEXTURE_2D, texName);    
  461. }  
  462.   
  463. void CPlot::resetGLLightPosition() {  
  464.     boundingRadius = getBoundingRadius();  
  465.     GLfloat light_position[] = {0.0f, 0.0,   
  466.         (float)(LightDistanceRatio*boundingRadius) , 1.0f};  
  467.     glLightfv(GL_LIGHT0, GL_POSITION, light_position);  
  468. }  
  469. float CPlot::getBoundingRadius() {  
  470.     //if (meshes.size() == 0) return 0;  
  471.   
  472.     Core::Geometry::Vector3D pmin, pmax;  
  473.     //auto it = meshes.begin();  
  474.     //do {  
  475.     //  MyMesh* last = *it;  
  476.     BoundingBox box = mesh->calBoundBox();  
  477.     //      if (it == meshes.begin()) {  
  478.     //          pmin = box.min;  
  479.     //          pmax = box.max;  
  480.     //      } else {  
  481.     //          pmin = min(pmin, box.min);  
  482.     //          pmax = max(pmax, box.max);  
  483.     //      }  
  484.     //  } while ((++it) != meshes.end());  
  485.     pmax = box.max ; pmin = box.min;  
  486.     return sqrt(  
  487.         pow(pmax[0] - pmin[0],2)+  
  488.         pow(pmax[1] - pmin[1],2)+  
  489.         pow(pmax[2] - pmin[2],2)  
  490.         );  
  491. }  
  492. void CPlot::loadMeshFile(char* filename)  
  493. {  
  494.     xscale = 1.0; yscale = 1.0; zscale = 1.0;  
  495.     transX = 0.0; transY = 0.0;  
  496.   
  497.     faceColors.clear();  
  498.     mesh->LoadFromFile(filename);  
  499.     mesh->calFaceNormal();  
  500.     mesh->calVertexNormal();  
  501.     int fCount = mesh->getFCount();  
  502.     char buf[255];  
  503.     sprintf(buf, "%s", filename);  
  504.     int len = strlen(buf);  
  505.     buf[len-1] = 'f'; buf[len-2] = 'd'; buf[len-3] = 's';  
  506.     FILE *fp = NULL;  
  507. }  
  508.   
  509. void CPlotMark005(){}  
  510.   
  511. //初始化坐标系,画出世界坐标系、标志BOX、Box坐标系!  
  512. void CPlot::createActions(){  
  513.   
  514.     loadFileAction    = new QAction(tr("&Load"), this);  
  515.     closeFileAction   = new QAction(tr("&Close"), this);  
  516.     segmentObjAction  = new QAction(tr("&Segment"), this);  
  517.     captureAction     = new QAction(tr("Capture") , this);  
  518.   
  519.     connect(loadFileAction, SIGNAL(triggered()), this, SLOT(openFile()));  
  520.     connect(closeFileAction, SIGNAL(triggered()), this, SLOT(close()));  
  521.     connect(segmentObjAction, SIGNAL(triggered()), this, SLOT(segmentObj()));  
  522.     connect(captureAction, SIGNAL(triggered()), this, SLOT(capture()));  
  523. }  
  524.   
  525. void CPlot::createMenus(){  
  526.     fileMenu = menuBar()->addMenu(tr("&File"));  
  527.     fileMenu->addAction(loadFileAction);  
  528.     fileMenu->addAction(closeFileAction);  
  529.     fileMenu->addAction(captureAction);  
  530.     toolMenu = menuBar()->addMenu(tr("&Tool"));  
  531.     toolMenu->addAction(segmentObjAction);  
  532.     toolMenu->addAction(captureAction);  
  533. }  
  534.   
  535. void CPlot::createToolBars(){  
  536.   
  537.     fileToolBar = addToolBar(tr("&File"));  
  538.     fileToolBar->addAction(loadFileAction);  
  539.     fileToolBar->addAction(closeFileAction);  
  540.     toolsBar = addToolBar(tr("Tool"));  
  541.     toolsBar->addAction(segmentObjAction);  
  542. }  
  543.   
  544. void CPlot::openFile()  
  545. {  
  546.     QString filename = QFileDialog::getOpenFileName(this, tr("Load a Shape"), ".", tr("Object Model (*.obj *.off)"));  
  547.     this->openglViewer->loadMeshFile(filename.toLatin1().data());  
  548.     this->openglViewer->updateGL();  
  549. }  
  550.   
  551. void CPlot::segmentObj(){  
  552.   
  553.   
  554. }  
  555.   
  556.   
  557. //进行数据读入  
  558. void CPlot::capture(){  
  559.   
  560.   
  561.     //  
  562.     // Do what you want  
  563.     // example  
  564.     // you can call openglViewer->updatePos(const Mat& rMat) function to show your Sensorfunsion  
  565.     // result, note that once you call this function, you can just call openglViewer->updateGL()  
  566.     // the re-paint the GL  
  567.     // like:  
  568.     char buffer[255];  
  569.     //ifstream pfile("/home/hll260/aiglass/proj/firefly-3288/1.txt");  
  570.     fstream pfile;  
  571.     pfile.open("D://SensorFusionVector//1030.txt");  
  572.     fstream outfile,outfile2;  
  573.     outfile.open("D://SensorFusionVector//2.txt");  
  574.     outfile2.open("D://SensorFusionVector//1103.txt");  
  575.   
  576.     if(!pfile)  
  577.     {    
  578.         printf("Can not open file!!");   
  579.         _exit(1);  
  580.     }  
  581.     long long sp = pfile.tellg();  
  582.     while(sp <= 207)  
  583.     {  
  584.         pfile.seekg(0,ios::end);  
  585.         sp = pfile.tellg();  
  586.         cout << sp << endl;  
  587.     }  
  588.   
  589.     float ax,ay,az,gx,gy,gz;  
  590.     char testgy[10];  
  591.     char *gyy="Gyro";  
  592.     char testac[10];  
  593.     char *acc="Acce";  
  594.     SensorFusion sf;  
  595.     int bbbb=1;  
  596.     vector <float> gyro(3,0);  
  597.     vector <float> accel(3,0);  
  598.     long long lastsp=0;  
  599.     pfile.seekg(lastsp);  
  600.     for(int i=1;i<=10;i++)  
  601.     {  
  602.         pfile.getline(buffer,100);  
  603.     }  
  604.     lastsp = pfile.tellg();  
  605.   
  606.     char buffer1[50];  
  607.     char buffer2[50];  
  608.     long linep=0;  
  609.     while (1)//!pfile.eof()  
  610.     {  
  611.         pfile.seekg(0,ios::end);  
  612.         pfile.clear();  
  613.         sp = pfile.tellg();  
  614.         if (linep==0)  
  615.             pfile.seekg(lastsp);  
  616.         else  
  617.             pfile.seekg(linep);  
  618.         //cout <<sp<<"  "<<lastsp<<endl;  
  619.         if (sp>lastsp)  
  620.         {  
  621.             lastsp=sp;  
  622.             while(linep+144<sp)  
  623.             {  
  624.   
  625.                 pfile.getline(buffer1,40);  
  626.                 sscanf (buffer1,"%4s,%f,%f,%f",testac,&ax,&ay,&az);  
  627.                 pfile.getline(buffer2,40);  
  628.                 sscanf (buffer2,"%4s,%f,%f,%f",testgy,&gx,&gy,&gz);  
  629.                 linep = pfile.tellg();  
  630.                 if(strcmp(testac, acc) == 0&&strcmp(testac, testgy) == 0)  
  631.                 {  
  632.                     pfile.getline(buffer2,40);  
  633.                     sscanf (buffer2,"%4s,%f,%f,%f",testgy,&gx,&gy,&gz);  
  634.                     linep = pfile.tellg();  
  635.                     if(strcmp(testac, acc) == 0)  
  636.                     {  
  637.                         accel.push_back(ax);  
  638.                         accel.push_back(ay);  
  639.                         accel.push_back(az);  
  640.                         for (int i=0;i<3;i++)  
  641.                         {  
  642.                             accel.erase(accel.begin());  
  643.                         }  
  644.                     }  
  645.                     if (strcmp(testgy, gyy) == 0)  
  646.                     {    
  647.                         gyro.push_back(gx);  
  648.                         gyro.push_back(gy);  
  649.                         gyro.push_back(gz);  
  650.                         for (int i=0;i<3;i++)  
  651.                         {  
  652.                             gyro.erase(gyro.begin());  
  653.                         }  
  654.                     }  
  655.                 }  
  656.                 else  
  657.                 {  
  658.                     if(strcmp(testac, acc) == 0)  
  659.                     {  
  660.                         accel.push_back(ax);  
  661.                         accel.push_back(ay);  
  662.                         accel.push_back(az);  
  663.                         for (int i=0;i<3;i++)  
  664.                         {  
  665.                             accel.erase(accel.begin());  
  666.                         }  
  667.                     }  
  668.                     if (strcmp(testgy, gyy) == 0)  
  669.                     {    
  670.                         gyro.push_back(gx);  
  671.                         gyro.push_back(gy);  
  672.                         gyro.push_back(gz);  
  673.                         for (int i=0;i<3;i++)  
  674.                         {  
  675.                             gyro.erase(gyro.begin());  
  676.                         }  
  677.                     }  
  678.                 }  
  679.                 sf.SensorPretreatment(gyro);  
  680.                 sf.handlemessage(accel,gyro,0.001);  
  681.   
  682.                 Mat rMat(1,3,CV_32F);  
  683.                 //弧度 转角度  
  684.                 rMat.at<float>(0,0) = -sf.jiaodu[1]*57.3;  
  685.                 if (sf.jiaodu[0]<0)  
  686.                     rMat.at<float>(0,1) = 360-sf.jiaodu[0]*57.3;  
  687.                 else  
  688.                     rMat.at<float>(0,1) = sf.jiaodu[0]*57.3;  
  689.                 if(sf.jiaodu[2]<0)  
  690.                     rMat.at<float>(0,2) = sf.jiaodu[2]*57.3;  
  691.                 else  
  692.                     rMat.at<float>(0,2) = sf.jiaodu[2]*57.3;  
  693.   
  694.                 //传入三维坐标参数为绝对位置(相对于原点)  
  695.                 rMat.at<float>(0,3) = sf.LocX;  
  696.                 rMat.at<float>(0,4) = sf.LocY;  
  697.                 rMat.at<float>(0,5) = sf.LocZ;  
  698.   
  699.                 openglViewer->updatePos(rMat);  
  700.                 openglViewer->updateGL();  
  701.                 outfile<<sf.jiaodu[0]*57.3<<" "<<sf.jiaodu[1]*57.3<<" "<<sf.jiaodu[2]*57.3<<endl;  
  702.                 cout<<sf.jiaodu[0]*57.3<<" "<<sf.jiaodu[1]*57.3<<" "<<sf.jiaodu[2]*57.3<<" "<<gyro.size()<<endl;  
  703.   
  704.                 outfile2<<double(accel[0])<<" "<<double(accel[1])<<" "<<double(accel[2])<<endl;  
  705.                 outfile2<<double(gyro[0])<<" "<<double(gyro[1])<<" "<<double(gyro[2])<<endl;  
  706.                 outfile2<<"sp:"<<sp<<"    linep:"<<linep<<endl;  
  707.   
  708.             }  
  709.         }  
  710.         else  
  711.         {     
  712.             continue;  
  713.         }  
  714.     }  
  715.     outfile.close();  
  716.     outfile2.close();  
  717.     pfile.close();  
  718.   
  719. }  
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值