OpenGL实现鼠标绕任意轴旋转/平移/缩放

 刚刚学opengl的童鞋肯定有个苦恼的麻烦,只会绘制一个三角形,但是想像那些三维软件那样用鼠标控制视角还是有点困难的,所以我就封装了一个场景漫游类:RoamingScenceManager,这个类使用非常方便,跟界面没有半毛钱关系,可以在Qt,原生OpenGL,MFC用,下面的内容是简单介绍怎么用,然后就给出上述三个环境的具体例子。

       RoamingScenceManager类

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:14px;">void init(); //初始化  
  2. void render();//开始渲染  
  3. void executeRotateOperation(int x, int y); //执行旋转操作  
  4. void executeScaleOperation(float factor); //执行缩放操作  
  5. void executeTranslateOperation(int x,int y);//执行平移操作  
  6. void getInitPos(int x, int y); //获取鼠标按下时候的坐标</span><span style="font-weight: bold; font-size: 18px;">  
  7. </span>  
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:14px;">void init(); //初始化  
  2. void render();//开始渲染  
  3. void executeRotateOperation(int x, int y); //执行旋转操作  
  4. void executeScaleOperation(float factor); //执行缩放操作  
  5. void executeTranslateOperation(int x,int y);//执行平移操作  
  6. void getInitPos(int x, int y); //获取鼠标按下时候的坐标</span><span style="font-weight: bold; font-size: 18px;">  
  7. </span>  

      就是很简单运用上面六个函数就可以快速构建开放环境,下面说说怎么在三个平台用:


        Qt平台:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include"GL/glew.h"  
  2. #include"GL/glut.h"  
  3.   
  4. #include"GLMainWidget.h"  
  5. #include"RoamingScenceManager.h"  
  6.   
  7. #include<QMouseEvent>  
  8. #include<QWheelEvent>  
  9. #include<QDebug>  
  10.   
  11. GLMainWidget::GLMainWidget(QWidget *parent) :  
  12.     QGLWidget(parent)  
  13. {  
  14.     this->setFocusPolicy(Qt::StrongFocus);  
  15.     manager=new RoamingScenceManager();  
  16. }  
  17.   
  18. GLMainWidget::~GLMainWidget()  
  19. {  
  20.     delete manager;  
  21. }  
  22.   
  23. void GLMainWidget::initializeGL()  
  24. {  
  25.     manager->init();  
  26. }  
  27.   
  28. void GLMainWidget::resizeGL(int w, int h)  
  29. {  
  30.     glViewport(0,-(w-h)/2,w,w);  
  31. }  
  32. void GLMainWidget::paintGL()  
  33. {  
  34.     manager->render();  
  35. }  
  36.   
  37. void GLMainWidget::mousePressEvent(QMouseEvent *e)  
  38. {  
  39.     manager->getInitPos(e->x(),e->y());  
  40. }  
  41.   
  42. void GLMainWidget::mouseMoveEvent(QMouseEvent *e)  
  43. {  
  44.     if(e->buttons()&Qt::MiddleButton)  
  45.     {  
  46.         if(e->modifiers()==Qt::CTRL)  
  47.         {  
  48.             manager->executeTranslateOperation(e->x(),e->y());  
  49.         }  
  50.         else  
  51.         {  
  52.             manager->executeRotateOperation(e->x(),e->y());  
  53.         }  
  54.     }  
  55.     updateGL();  
  56. }  
  57.   
  58. void GLMainWidget::wheelEvent(QWheelEvent *e)  
  59. {  
  60.     if(e->delta()>=0)  
  61.     {  
  62.         manager->executeScaleOperation(-0.1);  
  63.     }else  
  64.     {  
  65.         manager->executeScaleOperation(0.1);  
  66.     }  
  67.     updateGL();  
  68. }  
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include"GL/glew.h"  
  2. #include"GL/glut.h"  
  3.   
  4. #include"GLMainWidget.h"  
  5. #include"RoamingScenceManager.h"  
  6.   
  7. #include<QMouseEvent>  
  8. #include<QWheelEvent>  
  9. #include<QDebug>  
  10.   
  11. GLMainWidget::GLMainWidget(QWidget *parent) :  
  12.     QGLWidget(parent)  
  13. {  
  14.     this->setFocusPolicy(Qt::StrongFocus);  
  15.     manager=new RoamingScenceManager();  
  16. }  
  17.   
  18. GLMainWidget::~GLMainWidget()  
  19. {  
  20.     delete manager;  
  21. }  
  22.   
  23. void GLMainWidget::initializeGL()  
  24. {  
  25.     manager->init();  
  26. }  
  27.   
  28. void GLMainWidget::resizeGL(int w, int h)  
  29. {  
  30.     glViewport(0,-(w-h)/2,w,w);  
  31. }  
  32. void GLMainWidget::paintGL()  
  33. {  
  34.     manager->render();  
  35. }  
  36.   
  37. void GLMainWidget::mousePressEvent(QMouseEvent *e)  
  38. {  
  39.     manager->getInitPos(e->x(),e->y());  
  40. }  
  41.   
  42. void GLMainWidget::mouseMoveEvent(QMouseEvent *e)  
  43. {  
  44.     if(e->buttons()&Qt::MiddleButton)  
  45.     {  
  46.         if(e->modifiers()==Qt::CTRL)  
  47.         {  
  48.             manager->executeTranslateOperation(e->x(),e->y());  
  49.         }  
  50.         else  
  51.         {  
  52.             manager->executeRotateOperation(e->x(),e->y());  
  53.         }  
  54.     }  
  55.     updateGL();  
  56. }  
  57.   
  58. void GLMainWidget::wheelEvent(QWheelEvent *e)  
  59. {  
  60.     if(e->delta()>=0)  
  61.     {  
  62.         manager->executeScaleOperation(-0.1);  
  63.     }else  
  64.     {  
  65.         manager->executeScaleOperation(0.1);  
  66.     }  
  67.     updateGL();  
  68. }  


     Win32平台(原生OpenGL界面)

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:14px;">// OpenGLScence_raw.cpp : 定义控制台应用程序的入口点。  
  2. //  
  3. #include "stdafx.h"  
  4. #include"GL/glut.h"  
  5. #include<memory>   
  6. #include "RoamingScenceManager.h"  
  7.   
  8. void MousePressEvent(int button, int state, int x, int y);  
  9. void MouseMoveEvent(int x,int y);  
  10. void ProcessSpecialKeys(int key, int x, int y);  
  11.   
  12. void reshape(int w, int h);  
  13. void display();  
  14.   
  15. bool leftButtonState;  
  16. bool rightButtonState;  
  17.   
  18. //#define  GLUT_WHEEL_UP 3           //定义滚轮操作    
  19. //#define  GLUT_WHEEL_DOWN 4  
  20.   
  21. std::auto_ptr<RoamingScenceManager> manager(new RoamingScenceManager());  
  22.   
  23.   
  24. int main(int argc, char ** argv)  
  25. {  
  26.     glutInit(&argc, argv);  
  27.     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);  
  28.     glutInitWindowSize(800, 600);  
  29.     glutInitWindowPosition(100, 100);  
  30.     glutCreateWindow("原生OpenGL");  
  31.     manager->init();  
  32.     glutDisplayFunc(display);  
  33.     glutReshapeFunc(reshape);  
  34.     glutMouseFunc(MousePressEvent);  
  35.     glutMotionFunc(MouseMoveEvent);  
  36.     glutSpecialFunc(ProcessSpecialKeys);  
  37.     glutMainLoop();  
  38.     return 0;  
  39.   
  40. }  
  41.   
  42. void display()  
  43. {  
  44.     manager->render();  
  45.     glFlush();  
  46. }  
  47.   
  48. void reshape(int w,int h)  
  49. {  
  50.     glViewport(0, -(w - h) / 2, w, w);  
  51. }  
  52.   
  53. void MousePressEvent(int button, int state, int x, int y)  
  54. {  
  55.     if (button == GLUT_LEFT_BUTTON)  
  56.     {  
  57.         if (state == GLUT_DOWN)  
  58.         {  
  59.             manager->getInitPos(x, y);  
  60.             leftButtonState = true;  
  61.         }  
  62.         else  
  63.         {  
  64.             leftButtonState = false;  
  65.         }  
  66.     }  
  67.   
  68.     if (button == GLUT_RIGHT_BUTTON)  
  69.     {  
  70.         if (state == GLUT_DOWN)  
  71.         {  
  72.             manager->getInitPos(x, y);  
  73.             rightButtonState = true;  
  74.         }  
  75.         else  
  76.         {  
  77.             rightButtonState = false;  
  78.         }  
  79.     }  
  80.   
  81. }  
  82.   
  83. void MouseMoveEvent(int x, int y)  
  84. {  
  85.     if (leftButtonState)  
  86.     {  
  87.         manager->executeRotateOperation(x, y);  
  88.     }  
  89.     if (rightButtonState)  
  90.     {  
  91.         manager->executeTranslateOperation(x, y);  
  92.     }   
  93.        display();  
  94. }  
  95.       
  96. void ProcessSpecialKeys(int key, int x, int y)  
  97. {  
  98.     if (key == GLUT_KEY_UP)  
  99.     {  
  100.         manager->executeScaleOperation(-0.1);  
  101.     }  
  102.     if (key == GLUT_KEY_DOWN)  
  103.     {  
  104.         manager->executeScaleOperation(0.1);  
  105.     }  
  106.     display();  
  107. }</span><span style="font-weight: bold; font-size: 18px;">  
  108. </span>  
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:14px;">// OpenGLScence_raw.cpp : 定义控制台应用程序的入口点。  
  2. //  
  3. #include "stdafx.h"  
  4. #include"GL/glut.h"  
  5. #include<memory>   
  6. #include "RoamingScenceManager.h"  
  7.   
  8. void MousePressEvent(int button, int state, int x, int y);  
  9. void MouseMoveEvent(int x,int y);  
  10. void ProcessSpecialKeys(int key, int x, int y);  
  11.   
  12. void reshape(int w, int h);  
  13. void display();  
  14.   
  15. bool leftButtonState;  
  16. bool rightButtonState;  
  17.   
  18. //#define  GLUT_WHEEL_UP 3           //定义滚轮操作    
  19. //#define  GLUT_WHEEL_DOWN 4  
  20.   
  21. std::auto_ptr<RoamingScenceManager> manager(new RoamingScenceManager());  
  22.   
  23.   
  24. int main(int argc, char ** argv)  
  25. {  
  26.     glutInit(&argc, argv);  
  27.     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);  
  28.     glutInitWindowSize(800, 600);  
  29.     glutInitWindowPosition(100, 100);  
  30.     glutCreateWindow("原生OpenGL");  
  31.     manager->init();  
  32.     glutDisplayFunc(display);  
  33.     glutReshapeFunc(reshape);  
  34.     glutMouseFunc(MousePressEvent);  
  35.     glutMotionFunc(MouseMoveEvent);  
  36.     glutSpecialFunc(ProcessSpecialKeys);  
  37.     glutMainLoop();  
  38.     return 0;  
  39.   
  40. }  
  41.   
  42. void display()  
  43. {  
  44.     manager->render();  
  45.     glFlush();  
  46. }  
  47.   
  48. void reshape(int w,int h)  
  49. {  
  50.     glViewport(0, -(w - h) / 2, w, w);  
  51. }  
  52.   
  53. void MousePressEvent(int button, int state, int x, int y)  
  54. {  
  55.     if (button == GLUT_LEFT_BUTTON)  
  56.     {  
  57.         if (state == GLUT_DOWN)  
  58.         {  
  59.             manager->getInitPos(x, y);  
  60.             leftButtonState = true;  
  61.         }  
  62.         else  
  63.         {  
  64.             leftButtonState = false;  
  65.         }  
  66.     }  
  67.   
  68.     if (button == GLUT_RIGHT_BUTTON)  
  69.     {  
  70.         if (state == GLUT_DOWN)  
  71.         {  
  72.             manager->getInitPos(x, y);  
  73.             rightButtonState = true;  
  74.         }  
  75.         else  
  76.         {  
  77.             rightButtonState = false;  
  78.         }  
  79.     }  
  80.   
  81. }  
  82.   
  83. void MouseMoveEvent(int x, int y)  
  84. {  
  85.     if (leftButtonState)  
  86.     {  
  87.         manager->executeRotateOperation(x, y);  
  88.     }  
  89.     if (rightButtonState)  
  90.     {  
  91.         manager->executeTranslateOperation(x, y);  
  92.     }   
  93.        display();  
  94. }  
  95.       
  96. void ProcessSpecialKeys(int key, int x, int y)  
  97. {  
  98.     if (key == GLUT_KEY_UP)  
  99.     {  
  100.         manager->executeScaleOperation(-0.1);  
  101.     }  
  102.     if (key == GLUT_KEY_DOWN)  
  103.     {  
  104.         manager->executeScaleOperation(0.1);  
  105.     }  
  106.     display();  
  107. }</span><span style="font-weight: bold; font-size: 18px;">  
  108. </span>  



        MFC平台:
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // OpenGLScence_MFCView.cpp : COpenGLScence_MFCView 类的实现  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. // SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的  
  6. // ATL 项目中进行定义,并允许与该项目共享文档代码。  
  7. #ifndef SHARED_HANDLERS  
  8. #include "OpenGLScence_MFC.h"  
  9. #endif  
  10.   
  11. #include"GL/glew.h"  
  12. #include"GL/glut.h"  
  13. #include "OpenGLScence_MFCDoc.h"  
  14. #include "OpenGLScence_MFCView.h"  
  15. #include "RoamingScenceManager.h"  
  16.   
  17. #ifdef _DEBUG  
  18. #define new DEBUG_NEW  
  19. #endif  
  20.   
  21.   
  22. // COpenGLScence_MFCView  
  23.   
  24. IMPLEMENT_DYNCREATE(COpenGLScence_MFCView, CView)  
  25.   
  26. BEGIN_MESSAGE_MAP(COpenGLScence_MFCView, CView)  
  27.     // 标准打印命令  
  28.     ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)  
  29.     ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)  
  30.     ON_COMMAND(ID_FILE_PRINT_PREVIEW, &COpenGLScence_MFCView::OnFilePrintPreview)  
  31.     ON_WM_CONTEXTMENU()  
  32.     ON_WM_RBUTTONUP()  
  33.     ON_WM_CREATE()  
  34.     ON_WM_SIZE()  
  35.     ON_WM_DESTROY()  
  36.     ON_WM_PAINT()  
  37.     ON_WM_MBUTTONDOWN()  
  38.     ON_WM_MBUTTONUP()  
  39.     ON_WM_MOUSEMOVE()  
  40.     ON_WM_MOUSEWHEEL()  
  41. //  ON_WM_KEYDOWN()  
  42. END_MESSAGE_MAP()  
  43.   
  44. // COpenGLScence_MFCView 构造/析构  
  45.   
  46. COpenGLScence_MFCView::COpenGLScence_MFCView()  
  47. {  
  48.     // TODO:  在此处添加构造代码  
  49.     this->m_GLPixelIndex = 0;  
  50.     this->m_hGLContext = NULL;  
  51.     midButtonState = false;  
  52.     ctrlKeyState = false;  
  53.     manager = new RoamingScenceManager();  
  54.   
  55. }  
  56.   
  57. COpenGLScence_MFCView::~COpenGLScence_MFCView()  
  58. {  
  59.   
  60. }  
  61.   
  62. BOOL COpenGLScence_MFCView::PreCreateWindow(CREATESTRUCT& cs)  
  63. {  
  64.     // TODO:  在此处通过修改  
  65.     //  CREATESTRUCT cs 来修改窗口类或样式  
  66.     cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN;// 在客户区绘制  
  67.   
  68.     return CView::PreCreateWindow(cs);  
  69. }  
  70.   
  71. // COpenGLScence_MFCView 绘制  
  72.   
  73. void COpenGLScence_MFCView::OnDraw(CDC* /*pDC*/)  
  74. {  
  75.     COpenGLScence_MFCDoc* pDoc = GetDocument();  
  76.     ASSERT_VALID(pDoc);  
  77.     if (!pDoc)  
  78.         return;  
  79.   
  80.     // TODO:  在此处为本机数据添加绘制代码  
  81. }  
  82.   
  83.   
  84. // COpenGLScence_MFCView 打印  
  85.   
  86.   
  87. void COpenGLScence_MFCView::OnFilePrintPreview()  
  88. {  
  89. #ifndef SHARED_HANDLERS  
  90.     AFXPrintPreview(this);  
  91. #endif  
  92. }  
  93.   
  94. BOOL COpenGLScence_MFCView::OnPreparePrinting(CPrintInfo* pInfo)  
  95. {  
  96.     // 默认准备  
  97.     return DoPreparePrinting(pInfo);  
  98. }  
  99.   
  100. void COpenGLScence_MFCView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)  
  101. {  
  102.     // TODO:  添加额外的打印前进行的初始化过程  
  103. }  
  104.   
  105. void COpenGLScence_MFCView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)  
  106. {  
  107.     // TODO:  添加打印后进行的清理过程  
  108. }  
  109.   
  110. bool COpenGLScence_MFCView::SetWindowPixelFormat(HDC hDC)  
  111. {  
  112.     //定义窗口的像素格式  
  113.     PIXELFORMATDESCRIPTOR pixelDesc =  
  114.     {  
  115.         sizeof(PIXELFORMATDESCRIPTOR),           //nSize结构长度  
  116.         1,                                       //nVersion结构版本  
  117.         PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL |  
  118.         PFD_DOUBLEBUFFER | PFD_SUPPORT_GDI,        //dwFlags告诉OpenGL如何处理像素  
  119.         /* 
  120.         wFlags能接收以下标志: 
  121.         PFD_DRAW_TO_WINDOW 使之能在窗口或者其他设备窗口画图; 
  122.         PFD_DRAW_TO_BITMAP 使之能在内存中的位图画图; 
  123.         PFD_SUPPORT_GDI 使之能调用GDI函数(注:如果指定了PFD_DOUBLEBUFFER,这个选项将无效); 
  124.         PFD_SUPPORT_OpenGL 使之能调用OpenGL函数; 
  125.         PFD_GENERIC_FORMAT 假如这种象素格式由Windows GDI函数库或由第三方硬件设备驱动程序支持,则需指定这一项; 
  126.         PFD_NEED_PALETTE 告诉缓冲区是否需要调色板,本程序假设颜色是使用24或 32位色,并且不会覆盖调色板; 
  127.         PFD_NEED_SYSTEM_PALETTE 这个标志指明缓冲区是否把系统调色板当作它自身调色板的一部分; 
  128.         PFD_DOUBLEBUFFER 指明使用了双缓冲区(注:GDI不能在使用了双缓冲区的窗口中画图); 
  129.         PFD_STEREO 指明左、右缓冲区是否按立体图像来组织。 
  130.         PFD_SWAP_LAYER_BUFFERS 
  131.         */  
  132.         PFD_TYPE_RGBA,  //iPixelType,颜色模式,包括两种PFD_TYPE_RGBA意味着每一位(bit)组代表着rgb各分量的值。PFD_TYPE_COLORINDEX意味着每一位组代表着在彩色查找表中的索引值  
  133.         24,   //cColorBits定义了指定一个颜色的位数。对RGBA来说,位数是在颜色中红、绿、蓝各分量所占的位数。对颜色的索引值来说,指的是表中的颜色数。  
  134.         0, 0, 0, 0, 0, 0,  //cRedBits、cRedShifts、cGreenBits、cGreenShifts、cBlueBits、cBlueShifts用,基本不被采用,一般置0  
  135.         //cRedBits、cGreenBits、cBlueBits用来表明各相应分量所使用的位数。  
  136.         //cRedShift、cGreenShift、cBlue-Shift用来表明各分量从颜色开始的偏移量所占的位数。  
  137.         0,                                       //cAlphaBits,RGB颜色缓存中Alpha的位数                              
  138.         0,                                 //cAlphaShift,已经不被采用,置0                     
  139.         0,                                       //cAcuumBits累计缓存的位数  
  140.         0, 0, 0, 0,                                 //cAcuumRedBits/cAcuumGreenBits/cAcuumBlueBits/cAcuumAlphaBits,基本不被采用,置0  
  141.         32,                                      //cDepthBits深度缓存的位数  
  142.         0,                                       //cStencilBits,模板缓存的位数  
  143.         0,                                       //cAuxBuffers,辅助缓存的位数,一般置0  
  144.         PFD_MAIN_PLANE,                          //iLayerType,说明层面的类型,可忽略置0,是早期的版本,包括  
  145.         //PFD_MAIN_PLANE,PFD_OVER_LAY_PLANE,PFD_UNDERLAY_PLANE  
  146.         0, 0, 0, 0                                  //bReserved,dwLayerMask,dwVisibleMask,dwDamageMask,必须置0  
  147.     };  
  148.   
  149.     this->m_GLPixelIndex = ChoosePixelFormat(hDC, &pixelDesc);//选择最相近的像素格式  
  150.     /* 
  151.     ChoosePixelFormat接受两个参数:一个是hDc,另一个是一个指向PIXELFORMATDESCRIPTOR结构的指针&pixelDesc 
  152.     该函数返回此像素格式的索引值,如果返回0则表示失败。 
  153.     假如函数失败,我们只是把索引值设为1并用 DescribePixelFormat得到像素格式描述。 
  154.     假如你申请一个没得到支持的像素格式,则ChoosePixelFormat将会返回与你要求的像素格式最接近的一个值 
  155.     一旦我们得到一个像素格式的索引值和相应的描述,我们就可以调用SetPixelFormat设置像素格式,并且只需设置一次。 
  156.     */  
  157.     if (this->m_GLPixelIndex == 0)  
  158.     {//选择失败  
  159.         this->m_GLPixelIndex = 1;//默认的像素格式  
  160.         //用默认的像素格式进行设置  
  161.         //  
  162.         if (DescribePixelFormat(hDC, this->m_GLPixelIndex, sizeof(PIXELFORMATDESCRIPTOR), &pixelDesc) == 0)  
  163.         {  
  164.             return FALSE;  
  165.         }  
  166.     }  
  167.   
  168.     if (SetPixelFormat(hDC, this->m_GLPixelIndex, &pixelDesc) == FALSE)  
  169.     {  
  170.         return FALSE;  
  171.     }  
  172.     return TRUE;  
  173.   
  174. }  
  175.   
  176. bool COpenGLScence_MFCView::CreateViewGLContext(HDC hDC)  
  177. {  
  178.   
  179.     //WglCreateContext函数创建一个新的OpenGL渲染描述表(RC)  
  180.     //此描述表必须适用于绘制到由hdc返回的设备  
  181.     //这个渲染描述表将有和设备上下文(dc)一样的像素格式.  
  182.     this->m_hGLContext = wglCreateContext(hDC);//创建RC  
  183.   
  184.     if (this->m_hGLContext == NULL)  
  185.     {//创建失败  
  186.         return FALSE;  
  187.     }  
  188.   
  189.     /* 
  190.     wglMakeCurrent 函数设定OpenGL当前线程(线程相关性)的渲染环境。 
  191.     以后这个线程所有的OpenGL调用都是在这个hdc标识的设备上绘制。 
  192.     你也可以使用wglMakeCurrent 函数来改变调用线程的当前渲染环境 
  193.     使之不再是当前的渲染环境。 
  194.     */  
  195.     if (wglMakeCurrent(hDC, this->m_hGLContext) == FALSE)  
  196.     {//选为当前RC失败  
  197.         return FALSE;  
  198.     }  
  199.     return TRUE;  
  200.   
  201. }  
  202.   
  203. bool COpenGLScence_MFCView::InitGL(void)  
  204. {  
  205.     manager->init();  
  206.     return true;  
  207. }  
  208.   
  209. int COpenGLScence_MFCView::DrawGLScene(void)  
  210. {  
  211.     manager->render();  
  212.     glFlush();  
  213.     return TRUE;                                  
  214. }  
  215.   
  216. void COpenGLScence_MFCView::OnRButtonUp(UINT /* nFlags */, CPoint point)  
  217. {  
  218.     ClientToScreen(&point);  
  219.     OnContextMenu(this, point);  
  220. }  
  221.   
  222. void COpenGLScence_MFCView::OnContextMenu(CWnd* /* pWnd */, CPoint point)  
  223. {  
  224. #ifndef SHARED_HANDLERS  
  225.     theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);  
  226. #endif  
  227. }  
  228.   
  229.   
  230. // COpenGLScence_MFCView 诊断  
  231.   
  232. #ifdef _DEBUG  
  233. void COpenGLScence_MFCView::AssertValid() const  
  234. {  
  235.     CView::AssertValid();  
  236. }  
  237.   
  238. void COpenGLScence_MFCView::Dump(CDumpContext& dc) const  
  239. {  
  240.     CView::Dump(dc);  
  241. }  
  242.   
  243. COpenGLScence_MFCDoc* COpenGLScence_MFCView::GetDocument() const // 非调试版本是内联的  
  244. {  
  245.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(COpenGLScence_MFCDoc)));  
  246.     return (COpenGLScence_MFCDoc*)m_pDocument;  
  247. }  
  248. #endif //_DEBUG  
  249.   
  250.   
  251. // COpenGLScence_MFCView 消息处理程序  
  252.   
  253.   
  254. int COpenGLScence_MFCView::OnCreate(LPCREATESTRUCT lpCreateStruct)  
  255. {  
  256.     if (CView::OnCreate(lpCreateStruct) == -1)  
  257.         return -1;  
  258.   
  259.     // TODO:  在此添加您专用的创建代码  
  260.     //得到一个窗口对象(CWnd的派生对象)指针的句柄(HWND)    
  261.     HWND hWnd = this->GetSafeHwnd();  
  262.     //GetDC该函数检索一指定窗口的客户区域或整个屏幕的显示设备上下文环境的句柄  
  263.     //以后可以在GDI函数中使用该句柄来在设备上下文环境中绘图。  
  264.     HDC hDC = ::GetDC(hWnd);  
  265.   
  266.     if (this->SetWindowPixelFormat(hDC) == FALSE)  
  267.     {//设置像素格式  
  268.         return 0;  
  269.     }  
  270.     if (this->CreateViewGLContext(hDC) == FALSE)  
  271.     {//创建RC并选为所用  
  272.         return 0;  
  273.     }  
  274.     if (!this->InitGL())  
  275.     {//初始化openGL  
  276.         return 0;  
  277.     }  
  278.     return 0;  
  279. }  
  280.   
  281.   
  282. void COpenGLScence_MFCView::OnSize(UINT nType, int cx, int cy)  
  283. {  
  284.     CView::OnSize(nType, cx, cy);  
  285.     glViewport(0, -(cx - cy) / 2, cx, cx);  
  286. }  
  287.   
  288.   
  289. void COpenGLScence_MFCView::OnDestroy()  
  290. {  
  291.     CView::OnDestroy();  
  292.   
  293.     // TODO:  在此处添加消息处理程序代码  
  294.     if (wglGetCurrentContext() != NULL)  
  295.     {  
  296.         wglMakeCurrent(NULL, NULL);  
  297.     }  
  298.     if (this->m_hGLContext != NULL)  
  299.     {  
  300.         wglDeleteContext(this->m_hGLContext);  
  301.         this->m_hGLContext = NULL;  
  302.     }  
  303.   
  304.     delete manager;  
  305.     manager = NULL;  
  306. }  
  307.   
  308.   
  309. void COpenGLScence_MFCView::OnPaint()  
  310. {  
  311.     CPaintDC dc(this); // device context for painting  
  312.     // TODO:  在此处添加消息处理程序代码  
  313.     // 不为绘图消息调用 CView::OnPaint()  
  314.     this->DrawGLScene();  
  315.     SwapBuffers(dc.m_hDC);  
  316. }  
  317.   
  318.   
  319. void COpenGLScence_MFCView::OnMButtonDown(UINT nFlags, CPoint point)  
  320. {  
  321.     // TODO:  在此添加消息处理程序代码和/或调用默认值  
  322.   
  323.     CView::OnMButtonDown(nFlags, point);  
  324.     manager->getInitPos(point.x, point.y);  
  325.     midButtonState = true;  
  326. }  
  327.   
  328.   
  329. void COpenGLScence_MFCView::OnMButtonUp(UINT nFlags, CPoint point)  
  330. {  
  331.     // TODO:  在此添加消息处理程序代码和/或调用默认值  
  332.   
  333.     CView::OnMButtonUp(nFlags, point);  
  334.     midButtonState = false;  
  335. }  
  336.   
  337.   
  338. void COpenGLScence_MFCView::OnMouseMove(UINT nFlags, CPoint point)  
  339. {  
  340.     // TODO:  在此添加消息处理程序代码和/或调用默认值  
  341.   
  342.     CView::OnMouseMove(nFlags, point);  
  343.     if (midButtonState)  
  344.     {  
  345.         if (ctrlKeyState)  
  346.         {  
  347.             manager->executeTranslateOperation(point.x, point.y);  
  348.         }   
  349.         else  
  350.         {  
  351.             manager->executeRotateOperation(point.x, point.y);  
  352.         }  
  353.           
  354.         OnPaint();  
  355.     }  
  356. }  
  357.   
  358.   
  359. BOOL COpenGLScence_MFCView::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)  
  360. {  
  361.     // TODO:  在此添加消息处理程序代码和/或调用默认值  
  362.     if (zDelta>=0)  
  363.     {  
  364.         manager->executeScaleOperation(-0.1);  
  365.     }  
  366.     else  
  367.     {  
  368.         manager->executeScaleOperation(0.1);  
  369.     }  
  370.     OnPaint();  
  371.   
  372.     return CView::OnMouseWheel(nFlags, zDelta, pt);  
  373. }  
  374.   
  375.   
  376.   
  377. BOOL COpenGLScence_MFCView::PreTranslateMessage(MSG* pMsg)  
  378. {  
  379.     // TODO:  在此添加专用代码和/或调用基类  
  380.     if (pMsg->message == WM_KEYDOWN)  
  381.     {  
  382.   
  383.         if (pMsg->wParam == VK_CONTROL)//直接用虚码代替就可以响应所指键  
  384.         {  
  385.             ctrlKeyState = true;  
  386.         }  
  387.   
  388.     }  
  389.   
  390.     if (pMsg->message == WM_KEYUP)  
  391.     {  
  392.   
  393.         if (pMsg->wParam == VK_CONTROL)//直接用虚码代替就可以响应所指键  
  394.         {  
  395.             ctrlKeyState = false;  
  396.         }  
  397.   
  398.     }  
  399.     return CView::PreTranslateMessage(pMsg);  
  400. }  
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // OpenGLScence_MFCView.cpp : COpenGLScence_MFCView 类的实现  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. // SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的  
  6. // ATL 项目中进行定义,并允许与该项目共享文档代码。  
  7. #ifndef SHARED_HANDLERS  
  8. #include "OpenGLScence_MFC.h"  
  9. #endif  
  10.   
  11. #include"GL/glew.h"  
  12. #include"GL/glut.h"  
  13. #include "OpenGLScence_MFCDoc.h"  
  14. #include "OpenGLScence_MFCView.h"  
  15. #include "RoamingScenceManager.h"  
  16.   
  17. #ifdef _DEBUG  
  18. #define new DEBUG_NEW  
  19. #endif  
  20.   
  21.   
  22. // COpenGLScence_MFCView  
  23.   
  24. IMPLEMENT_DYNCREATE(COpenGLScence_MFCView, CView)  
  25.   
  26. BEGIN_MESSAGE_MAP(COpenGLScence_MFCView, CView)  
  27.     // 标准打印命令  
  28.     ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)  
  29.     ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)  
  30.     ON_COMMAND(ID_FILE_PRINT_PREVIEW, &COpenGLScence_MFCView::OnFilePrintPreview)  
  31.     ON_WM_CONTEXTMENU()  
  32.     ON_WM_RBUTTONUP()  
  33.     ON_WM_CREATE()  
  34.     ON_WM_SIZE()  
  35.     ON_WM_DESTROY()  
  36.     ON_WM_PAINT()  
  37.     ON_WM_MBUTTONDOWN()  
  38.     ON_WM_MBUTTONUP()  
  39.     ON_WM_MOUSEMOVE()  
  40.     ON_WM_MOUSEWHEEL()  
  41. //  ON_WM_KEYDOWN()  
  42. END_MESSAGE_MAP()  
  43.   
  44. // COpenGLScence_MFCView 构造/析构  
  45.   
  46. COpenGLScence_MFCView::COpenGLScence_MFCView()  
  47. {  
  48.     // TODO:  在此处添加构造代码  
  49.     this->m_GLPixelIndex = 0;  
  50.     this->m_hGLContext = NULL;  
  51.     midButtonState = false;  
  52.     ctrlKeyState = false;  
  53.     manager = new RoamingScenceManager();  
  54.   
  55. }  
  56.   
  57. COpenGLScence_MFCView::~COpenGLScence_MFCView()  
  58. {  
  59.   
  60. }  
  61.   
  62. BOOL COpenGLScence_MFCView::PreCreateWindow(CREATESTRUCT& cs)  
  63. {  
  64.     // TODO:  在此处通过修改  
  65.     //  CREATESTRUCT cs 来修改窗口类或样式  
  66.     cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN;// 在客户区绘制  
  67.   
  68.     return CView::PreCreateWindow(cs);  
  69. }  
  70.   
  71. // COpenGLScence_MFCView 绘制  
  72.   
  73. void COpenGLScence_MFCView::OnDraw(CDC* /*pDC*/)  
  74. {  
  75.     COpenGLScence_MFCDoc* pDoc = GetDocument();  
  76.     ASSERT_VALID(pDoc);  
  77.     if (!pDoc)  
  78.         return;  
  79.   
  80.     // TODO:  在此处为本机数据添加绘制代码  
  81. }  
  82.   
  83.   
  84. // COpenGLScence_MFCView 打印  
  85.   
  86.   
  87. void COpenGLScence_MFCView::OnFilePrintPreview()  
  88. {  
  89. #ifndef SHARED_HANDLERS  
  90.     AFXPrintPreview(this);  
  91. #endif  
  92. }  
  93.   
  94. BOOL COpenGLScence_MFCView::OnPreparePrinting(CPrintInfo* pInfo)  
  95. {  
  96.     // 默认准备  
  97.     return DoPreparePrinting(pInfo);  
  98. }  
  99.   
  100. void COpenGLScence_MFCView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)  
  101. {  
  102.     // TODO:  添加额外的打印前进行的初始化过程  
  103. }  
  104.   
  105. void COpenGLScence_MFCView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)  
  106. {  
  107.     // TODO:  添加打印后进行的清理过程  
  108. }  
  109.   
  110. bool COpenGLScence_MFCView::SetWindowPixelFormat(HDC hDC)  
  111. {  
  112.     //定义窗口的像素格式  
  113.     PIXELFORMATDESCRIPTOR pixelDesc =  
  114.     {  
  115.         sizeof(PIXELFORMATDESCRIPTOR),           //nSize结构长度  
  116.         1,                                       //nVersion结构版本  
  117.         PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL |  
  118.         PFD_DOUBLEBUFFER | PFD_SUPPORT_GDI,        //dwFlags告诉OpenGL如何处理像素  
  119.         /* 
  120.         wFlags能接收以下标志: 
  121.         PFD_DRAW_TO_WINDOW 使之能在窗口或者其他设备窗口画图; 
  122.         PFD_DRAW_TO_BITMAP 使之能在内存中的位图画图; 
  123.         PFD_SUPPORT_GDI 使之能调用GDI函数(注:如果指定了PFD_DOUBLEBUFFER,这个选项将无效); 
  124.         PFD_SUPPORT_OpenGL 使之能调用OpenGL函数; 
  125.         PFD_GENERIC_FORMAT 假如这种象素格式由Windows GDI函数库或由第三方硬件设备驱动程序支持,则需指定这一项; 
  126.         PFD_NEED_PALETTE 告诉缓冲区是否需要调色板,本程序假设颜色是使用24或 32位色,并且不会覆盖调色板; 
  127.         PFD_NEED_SYSTEM_PALETTE 这个标志指明缓冲区是否把系统调色板当作它自身调色板的一部分; 
  128.         PFD_DOUBLEBUFFER 指明使用了双缓冲区(注:GDI不能在使用了双缓冲区的窗口中画图); 
  129.         PFD_STEREO 指明左、右缓冲区是否按立体图像来组织。 
  130.         PFD_SWAP_LAYER_BUFFERS 
  131.         */  
  132.         PFD_TYPE_RGBA,  //iPixelType,颜色模式,包括两种PFD_TYPE_RGBA意味着每一位(bit)组代表着rgb各分量的值。PFD_TYPE_COLORINDEX意味着每一位组代表着在彩色查找表中的索引值  
  133.         24,   //cColorBits定义了指定一个颜色的位数。对RGBA来说,位数是在颜色中红、绿、蓝各分量所占的位数。对颜色的索引值来说,指的是表中的颜色数。  
  134.         0, 0, 0, 0, 0, 0,  //cRedBits、cRedShifts、cGreenBits、cGreenShifts、cBlueBits、cBlueShifts用,基本不被采用,一般置0  
  135.         //cRedBits、cGreenBits、cBlueBits用来表明各相应分量所使用的位数。  
  136.         //cRedShift、cGreenShift、cBlue-Shift用来表明各分量从颜色开始的偏移量所占的位数。  
  137.         0,                                       //cAlphaBits,RGB颜色缓存中Alpha的位数                              
  138.         0,                                 //cAlphaShift,已经不被采用,置0                     
  139.         0,                                       //cAcuumBits累计缓存的位数  
  140.         0, 0, 0, 0,                                 //cAcuumRedBits/cAcuumGreenBits/cAcuumBlueBits/cAcuumAlphaBits,基本不被采用,置0  
  141.         32,                                      //cDepthBits深度缓存的位数  
  142.         0,                                       //cStencilBits,模板缓存的位数  
  143.         0,                                       //cAuxBuffers,辅助缓存的位数,一般置0  
  144.         PFD_MAIN_PLANE,                          //iLayerType,说明层面的类型,可忽略置0,是早期的版本,包括  
  145.         //PFD_MAIN_PLANE,PFD_OVER_LAY_PLANE,PFD_UNDERLAY_PLANE  
  146.         0, 0, 0, 0                                  //bReserved,dwLayerMask,dwVisibleMask,dwDamageMask,必须置0  
  147.     };  
  148.   
  149.     this->m_GLPixelIndex = ChoosePixelFormat(hDC, &pixelDesc);//选择最相近的像素格式  
  150.     /* 
  151.     ChoosePixelFormat接受两个参数:一个是hDc,另一个是一个指向PIXELFORMATDESCRIPTOR结构的指针&pixelDesc 
  152.     该函数返回此像素格式的索引值,如果返回0则表示失败。 
  153.     假如函数失败,我们只是把索引值设为1并用 DescribePixelFormat得到像素格式描述。 
  154.     假如你申请一个没得到支持的像素格式,则ChoosePixelFormat将会返回与你要求的像素格式最接近的一个值 
  155.     一旦我们得到一个像素格式的索引值和相应的描述,我们就可以调用SetPixelFormat设置像素格式,并且只需设置一次。 
  156.     */  
  157.     if (this->m_GLPixelIndex == 0)  
  158.     {//选择失败  
  159.         this->m_GLPixelIndex = 1;//默认的像素格式  
  160.         //用默认的像素格式进行设置  
  161.         //  
  162.         if (DescribePixelFormat(hDC, this->m_GLPixelIndex, sizeof(PIXELFORMATDESCRIPTOR), &pixelDesc) == 0)  
  163.         {  
  164.             return FALSE;  
  165.         }  
  166.     }  
  167.   
  168.     if (SetPixelFormat(hDC, this->m_GLPixelIndex, &pixelDesc) == FALSE)  
  169.     {  
  170.         return FALSE;  
  171.     }  
  172.     return TRUE;  
  173.   
  174. }  
  175.   
  176. bool COpenGLScence_MFCView::CreateViewGLContext(HDC hDC)  
  177. {  
  178.   
  179.     //WglCreateContext函数创建一个新的OpenGL渲染描述表(RC)  
  180.     //此描述表必须适用于绘制到由hdc返回的设备  
  181.     //这个渲染描述表将有和设备上下文(dc)一样的像素格式.  
  182.     this->m_hGLContext = wglCreateContext(hDC);//创建RC  
  183.   
  184.     if (this->m_hGLContext == NULL)  
  185.     {//创建失败  
  186.         return FALSE;  
  187.     }  
  188.   
  189.     /* 
  190.     wglMakeCurrent 函数设定OpenGL当前线程(线程相关性)的渲染环境。 
  191.     以后这个线程所有的OpenGL调用都是在这个hdc标识的设备上绘制。 
  192.     你也可以使用wglMakeCurrent 函数来改变调用线程的当前渲染环境 
  193.     使之不再是当前的渲染环境。 
  194.     */  
  195.     if (wglMakeCurrent(hDC, this->m_hGLContext) == FALSE)  
  196.     {//选为当前RC失败  
  197.         return FALSE;  
  198.     }  
  199.     return TRUE;  
  200.   
  201. }  
  202.   
  203. bool COpenGLScence_MFCView::InitGL(void)  
  204. {  
  205.     manager->init();  
  206.     return true;  
  207. }  
  208.   
  209. int COpenGLScence_MFCView::DrawGLScene(void)  
  210. {  
  211.     manager->render();  
  212.     glFlush();  
  213.     return TRUE;                                  
  214. }  
  215.   
  216. void COpenGLScence_MFCView::OnRButtonUp(UINT /* nFlags */, CPoint point)  
  217. {  
  218.     ClientToScreen(&point);  
  219.     OnContextMenu(this, point);  
  220. }  
  221.   
  222. void COpenGLScence_MFCView::OnContextMenu(CWnd* /* pWnd */, CPoint point)  
  223. {  
  224. #ifndef SHARED_HANDLERS  
  225.     theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);  
  226. #endif  
  227. }  
  228.   
  229.   
  230. // COpenGLScence_MFCView 诊断  
  231.   
  232. #ifdef _DEBUG  
  233. void COpenGLScence_MFCView::AssertValid() const  
  234. {  
  235.     CView::AssertValid();  
  236. }  
  237.   
  238. void COpenGLScence_MFCView::Dump(CDumpContext& dc) const  
  239. {  
  240.     CView::Dump(dc);  
  241. }  
  242.   
  243. COpenGLScence_MFCDoc* COpenGLScence_MFCView::GetDocument() const // 非调试版本是内联的  
  244. {  
  245.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(COpenGLScence_MFCDoc)));  
  246.     return (COpenGLScence_MFCDoc*)m_pDocument;  
  247. }  
  248. #endif //_DEBUG  
  249.   
  250.   
  251. // COpenGLScence_MFCView 消息处理程序  
  252.   
  253.   
  254. int COpenGLScence_MFCView::OnCreate(LPCREATESTRUCT lpCreateStruct)  
  255. {  
  256.     if (CView::OnCreate(lpCreateStruct) == -1)  
  257.         return -1;  
  258.   
  259.     // TODO:  在此添加您专用的创建代码  
  260.     //得到一个窗口对象(CWnd的派生对象)指针的句柄(HWND)    
  261.     HWND hWnd = this->GetSafeHwnd();  
  262.     //GetDC该函数检索一指定窗口的客户区域或整个屏幕的显示设备上下文环境的句柄  
  263.     //以后可以在GDI函数中使用该句柄来在设备上下文环境中绘图。  
  264.     HDC hDC = ::GetDC(hWnd);  
  265.   
  266.     if (this->SetWindowPixelFormat(hDC) == FALSE)  
  267.     {//设置像素格式  
  268.         return 0;  
  269.     }  
  270.     if (this->CreateViewGLContext(hDC) == FALSE)  
  271.     {//创建RC并选为所用  
  272.         return 0;  
  273.     }  
  274.     if (!this->InitGL())  
  275.     {//初始化openGL  
  276.         return 0;  
  277.     }  
  278.     return 0;  
  279. }  
  280.   
  281.   
  282. void COpenGLScence_MFCView::OnSize(UINT nType, int cx, int cy)  
  283. {  
  284.     CView::OnSize(nType, cx, cy);  
  285.     glViewport(0, -(cx - cy) / 2, cx, cx);  
  286. }  
  287.   
  288.   
  289. void COpenGLScence_MFCView::OnDestroy()  
  290. {  
  291.     CView::OnDestroy();  
  292.   
  293.     // TODO:  在此处添加消息处理程序代码  
  294.     if (wglGetCurrentContext() != NULL)  
  295.     {  
  296.         wglMakeCurrent(NULL, NULL);  
  297.     }  
  298.     if (this->m_hGLContext != NULL)  
  299.     {  
  300.         wglDeleteContext(this->m_hGLContext);  
  301.         this->m_hGLContext = NULL;  
  302.     }  
  303.   
  304.     delete manager;  
  305.     manager = NULL;  
  306. }  
  307.   
  308.   
  309. void COpenGLScence_MFCView::OnPaint()  
  310. {  
  311.     CPaintDC dc(this); // device context for painting  
  312.     // TODO:  在此处添加消息处理程序代码  
  313.     // 不为绘图消息调用 CView::OnPaint()  
  314.     this->DrawGLScene();  
  315.     SwapBuffers(dc.m_hDC);  
  316. }  
  317.   
  318.   
  319. void COpenGLScence_MFCView::OnMButtonDown(UINT nFlags, CPoint point)  
  320. {  
  321.     // TODO:  在此添加消息处理程序代码和/或调用默认值  
  322.   
  323.     CView::OnMButtonDown(nFlags, point);  
  324.     manager->getInitPos(point.x, point.y);  
  325.     midButtonState = true;  
  326. }  
  327.   
  328.   
  329. void COpenGLScence_MFCView::OnMButtonUp(UINT nFlags, CPoint point)  
  330. {  
  331.     // TODO:  在此添加消息处理程序代码和/或调用默认值  
  332.   
  333.     CView::OnMButtonUp(nFlags, point);  
  334.     midButtonState = false;  
  335. }  
  336.   
  337.   
  338. void COpenGLScence_MFCView::OnMouseMove(UINT nFlags, CPoint point)  
  339. {  
  340.     // TODO:  在此添加消息处理程序代码和/或调用默认值  
  341.   
  342.     CView::OnMouseMove(nFlags, point);  
  343.     if (midButtonState)  
  344.     {  
  345.         if (ctrlKeyState)  
  346.         {  
  347.             manager->executeTranslateOperation(point.x, point.y);  
  348.         }   
  349.         else  
  350.         {  
  351.             manager->executeRotateOperation(point.x, point.y);  
  352.         }  
  353.           
  354.         OnPaint();  
  355.     }  
  356. }  
  357.   
  358.   
  359. BOOL COpenGLScence_MFCView::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)  
  360. {  
  361.     // TODO:  在此添加消息处理程序代码和/或调用默认值  
  362.     if (zDelta>=0)  
  363.     {  
  364.         manager->executeScaleOperation(-0.1);  
  365.     }  
  366.     else  
  367.     {  
  368.         manager->executeScaleOperation(0.1);  
  369.     }  
  370.     OnPaint();  
  371.   
  372.     return CView::OnMouseWheel(nFlags, zDelta, pt);  
  373. }  
  374.   
  375.   
  376.   
  377. BOOL COpenGLScence_MFCView::PreTranslateMessage(MSG* pMsg)  
  378. {  
  379.     // TODO:  在此添加专用代码和/或调用基类  
  380.     if (pMsg->message == WM_KEYDOWN)  
  381.     {  
  382.   
  383.         if (pMsg->wParam == VK_CONTROL)//直接用虚码代替就可以响应所指键  
  384.         {  
  385.             ctrlKeyState = true;  
  386.         }  
  387.   
  388.     }  
  389.   
  390.     if (pMsg->message == WM_KEYUP)  
  391.     {  
  392.   
  393.         if (pMsg->wParam == VK_CONTROL)//直接用虚码代替就可以响应所指键  
  394.         {  
  395.             ctrlKeyState = false;  
  396.         }  
  397.   
  398.     }  
  399.     return CView::PreTranslateMessage(pMsg);  
  400. }  

         

下面是看看效果如何:


Qt平台:中键旋转,ctrl+中键平移,滑轮缩放




Win32平台:左键旋转,右键平移, Up/Down缩放(没有滚轮事件)




MFC平台:中键旋转,ctrl+中键平移,滑轮缩放




         其实看那么多也没有用,直接下载代码好了,下载:http://download.csdn.NET/detail/trustguan/9453751  环境分别是Qt5.4.2和VS2013,如果你们不是这些版本,重新编译一次就行了。

  • 2
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值