zoukankan      html  css  js  c++  java
  • 【转】 《基于MFC的OpenGL编程》Part 6 Keyboard and Mouse Control

    在上一篇的基础上加入对键盘和鼠标的事件处理程序,以便用其来控制3D物体的旋转和移动。

    1,首先在CCY457OpenGLView类中为WM_KEYDOWN, WM_LBUTTONDOWN, WM_LBUTTONUP WM_MOUSEMOVE四个事件加入事件处理函数。

    2,CCY457OpenGLView.h中加入下列用于控制旋转和移动的变量:并在构造函数中初始化:

         GLfloat m_xAngle;
         GLfloat m_yAngle;
         GLfloat m_xPos;
         GLfloat m_yPos;
         CPoint m_MouseDownPoint;
    CCY457OpenGLView::CCY457OpenGLView()
    {
         m_xPos
    = 0.0f
    ;
         m_yPos
    = 0.0f
    ;
         m_xAngle
    = 0.0f
    ;
         m_yAngle
    = 0.0f
    ;
    }

    3,加入绘制代码:

    void COpenGLView::RenderScene ()
    {
         glLoadIdentity();
         glTranslatef(m_xPos, m_yPos,
    -5.0f
    );
         glRotatef(m_xAngle,
    1.0f,0.0f,0.0f
    );
         glRotatef(m_yAngle,
    0.0f,1.0f,0.0f
    );

         glutWireCube(
    1.0f
    );
    }

    4,为四个事件处理函数加入控制代码

    void COpenGLView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
    {
        
    // TODO: Add your message handler code here and/or call default

        switch (nChar)
         {
            
    case VK_UP:         m_yPos = m_yPos + 0.1f
    ;
                            
    break
    ;
            
    case VK_DOWN:     m_yPos = m_yPos - 0.1f
    ;
                            
    break
    ;
            
    case VK_LEFT:     m_xPos = m_xPos - 0.1f
    ;
                            
    break
    ;
            
    case VK_RIGHT:   m_xPos = m_xPos + 0.1f
    ;
                            
    break
    ;
            
    default:         MessageBox("Press the arrow keys only"
    );
                            
    break
    ;
         }        

         InvalidateRect(NULL,FALSE);
        
         CView::OnKeyDown(nChar, nRepCnt, nFlags);
    }

    void
    COpenGLView::OnLButtonDown(UINT nFlags, CPoint point)
    {
        
    // TODO: Add your message handler code here and/or call default

         m_MouseDownPoint=point;
         SetCapture();
        
         CView::OnLButtonDown(nFlags, point);
    }

    void
    COpenGLView::OnLButtonUp(UINT nFlags, CPoint point)
    {
        
    // TODO: Add your message handler code here and/or call default

         m_MouseDownPoint=CPoint(0,0);
         ReleaseCapture();
        
         CView::OnLButtonUp(nFlags, point);
    }

    void
    COpenGLView::OnMouseMove(UINT nFlags, CPoint point)
    {
        
    //
    TODO: Add your message handler code here and/or call default
        
    // Check if we have captured the mouse

        if (GetCapture()==this)
         {
            
    //Increment the object rotation angles

             m_xAngle+=(point.y-m_MouseDownPoint.y)/3.6;
             m_yAngle
    +=(point.x-m_MouseDownPoint.x)/3.6
    ;
            
    //Redraw the view

             InvalidateRect(NULL,FALSE);
            
    //Set the mouse point

             m_MouseDownPoint=point;
         };
        
         CView::OnMouseMove(nFlags, point);
    }

  • 相关阅读:
    数据库连接
    《程序员修炼之道--从小工到专家》读后感(二)
    《程序员修炼之道--从小工到专家》读后感(一)
    《继承与多态》动手动脑
    MyFirstJavaWeb
    静态初始化块的执行顺序
    使用类的静态字段和构造函数,可以跟踪某个类所创建对象的个数。请写一个类,在任何时候都可以向它查询“你已经创建了多少个对象?”。
    FJUT 毒瘤3(二分 + 最大匹配)题解
    HDU 4638 Group(莫队)题解
    HDU 4391 Paint The Wall(分块的区间维护)
  • 原文地址:https://www.cnblogs.com/lcxu2/p/2004062.html
Copyright © 2011-2022 走看看