博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
《基于MFC的OpenGL编程》Part 6 Keyboard and Mouse Control
阅读量:7020 次
发布时间:2019-06-28

本文共 2052 字,大约阅读时间需要 6 分钟。

在上一篇的基础上加入对键盘和鼠标的事件处理程序,以便用其来控制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);
}
复制代码
本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2008/11/05/1327443.html,如需转载请自行联系原作者
你可能感兴趣的文章
API接口项目框架搭建 - 目录
查看>>
插入排序--代码详解
查看>>
Analyse Case 01
查看>>
echo
查看>>
开源 免费 java CMS - FreeCMS1.5 标签 infoRelate
查看>>
第16课:Spark Streaming源码解读之数据清理内幕彻底解密
查看>>
GlobalSign 团队与世界领先的认证机构(CAs)合作 共同加强网络安全
查看>>
关于网址前面小图标显示问题(解决360浏览器不显示)
查看>>
adb命令、adb shell与Linux各种命令(busybox)
查看>>
TCP/IP详解学习笔记(7)-广播和多播,IGMP协议
查看>>
4.Object有哪些公用方法?
查看>>
基于帧的分时调度框架设计
查看>>
Java NIO系列教程(一) Java NIO 概述
查看>>
JQuery判断元素是否存在
查看>>
周鸿祎:很多程序员一看就知道不会创业
查看>>
Web前端测试
查看>>
Parcelable encountered IOException writing seriali
查看>>
可靠消息服务实现(分布式事务)
查看>>
“什么编程课?不重要!等以后再学吧!”
查看>>
Dao层系列-5-Hibernate JPA
查看>>