《基于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;
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;
}
{
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);
}
{
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);
}
{
// 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);
}
作者:洞庭散人
出处:http://phinecos.cnblogs.com/
本博客遵从Creative Commons Attribution 3.0 License,若用于非商业目的,您可以自由转载,但请保留原作者信息和文章链接URL。
分类:
C/C++/VC++
, 计算机图形学
posted on 2008-11-05 16:42 Phinecos(洞庭散人) 阅读(6735) 评论(3) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述