《基于MFC的OpenGL编程》Part 5 Keyboard and Mouse Control
来源:互联网 发布:护肤品成分查询软件 编辑:程序博客网 时间:2024/05/17 00:53
《基于MFC的OpenGL编程》Part 6 Keyboard and Mouse Control
原文链接
在上一篇的基础上加入对 键盘和鼠标 的事件处理程序,以便用其来 控制3D物体的旋转和移动 。
1、首先在CMy2OpenGLView类中为WM_KEYDOWN, WM_LBUTTONDOWN, WM_LBUTTONUP 和 WM_MOUSEMOVE四个事件加入事件处理函数。
2、在CMy2OpenGLView.h中加入下列用于控制旋转和移动的变量:
GLfloat m_xAngle; GLfloat m_yAngle; GLfloat m_xPos; GLfloat m_yPos; CPoint m_MouseDownPoint;
并在构造函数中初始化:
CMy2OpenGLView::CMy2OpenGLView(){ // TODO: add construction code here m_xPos = 0.0f; m_yPos = 0.0f; m_xAngle = 0.0f; m_yAngle = 0.0f; m_bPoint = FALSE; m_bLine = FALSE; m_bPolygon = FALSE; m_bTriangle = FALSE; m_bCube = FALSE; m_bTorus = FALSE; m_bTeapot = FALSE; m_bIcosahedron = FALSE; m_bSimpleCube = FALSE; }
3、加入绘制一个立方体的代码(上一篇的太阳月亮行星暂时注释掉):
void CMy2OpenGLView::RenderSceneSun(){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);// //Draw the sun // glLoadIdentity();// glTranslatef(0.0f,0.0f,-5.0f);// glRotatef(6.0f,1.0f,0.0f,0.0f);// glutWireSphere(1.0f,20,20);// //Draw the Planet// glPushMatrix();// glTranslatef(-1.2f,-0.5f,-2.0f);// glRotatef(6.0f,1.0f,0.0f,0.0f);// glutWireSphere(0.5f,20,20);// //Draw the moon// glPushMatrix();// glTranslatef(-1.2f,-0.5f,-2.0f);// glRotatef(6.0f,1.0f,0.0f,0.0f);// glutWireSphere(0.05f,20,20);// glPopMatrix();// glPopMatrix();}
4、为四个事件处理函数加入控制代码:
void CMy2OpenGLView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) {// TODO: Add your message handler code here and/or call defaultswitch (nChar){case VK_UP: m_yPos+=0.1f;break;case VK_DOWN: m_yPos-=0.1f;break;case VK_LEFT: m_xPos-=0.1f;break;case VK_RIGHT: m_xPos+=0.1f;break;default: MessageBox("Press the arrow keys only!");break;}InvalidateRect(NULL,FALSE);CView::OnKeyDown(nChar, nRepCnt, nFlags);}void CMy2OpenGLView::OnLButtonDown(UINT nFlags, CPoint point) {// TODO: Add your message handler code here and/or call defaultm_MouseDownPoint=point;SetCapture();CView::OnLButtonDown(nFlags, point);}void CMy2OpenGLView::OnLButtonUp(UINT nFlags, CPoint point) {// TODO: Add your message handler code here and/or call defaultm_MouseDownPoint=CPoint(0,0);ReleaseCapture();CView::OnLButtonUp(nFlags, point);}void CMy2OpenGLView::OnMouseMove(UINT nFlags, CPoint point) {// TODO: Add your message handler code here and/or call defaultif (GetCapture()==this){//Increase the Object rotation anglesm_xAngle+=(point.y-m_MouseDownPoint.y)/3.6;m_yAngle+=(point.x-m_MouseDownPoint.x)/3.6;//Redraw the viewInvalidateRect(NULL,FALSE);//Set the mouse point m_MouseDownPoint=point;}CView::OnMouseMove(nFlags, point);}运行效果图:由于下段代码(在OnKeyDown()方法中)截图时候老是弹屏,我就把他注释了。
default: MessageBox("Press the arrow keys only!");break;
附:以上是对一个立方体的操作,对于上篇博文中的太阳行星旋转同样适用,修改RenderSceneSun(),现在贴出代码:
注意:有个细节地方,
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);
标黑处:glRotatef(m_xAngles,1.0f,0.0f,0.0f);
glRotatef(m_yAngles,0.0f,1.0f,0.0f);
<span style="font-size:14px;">void CMy2OpenGLView::RenderSceneSun(){// 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);//Draw the sun 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);glutWireSphere(1.0f,20,20);//Draw the PlanetglPushMatrix();glTranslatef(m_xPos, m_yPos,-2.0f);glRotatef(m_xAngle,1.0f,0.0f,0.0f);glRotatef(m_yAngle,0.0f,1.0f,0.0f);glutWireSphere(0.5f,20,20);//Draw the moonglPushMatrix();glTranslatef(m_xPos, m_yPos,-2.0f);glRotatef(m_xAngle,1.0f,0.0f,0.0f);glRotatef(m_yAngle,0.0f,1.0f,0.0f);glutWireSphere(0.05f,20,20);glPopMatrix();glPopMatrix();}</span>代码运行结果:
0 0
- 《基于MFC的OpenGL编程》Part 5 Keyboard and Mouse Control
- 《基于MFC的OpenGL编程》Part 6 Keyboard and Mouse Control
- 《基于MFC的OpenGL编程》Part 5 Transformations - Rotations, Translations and Scaling
- 《基于MFC的OpenGL编程》Part 13 Creating 2D and 3D Text
- 《基于MFC的OpenGL编程》Part 12 Creating and Using Display Lists
- 《基于MFC的OpenGL编程》Part 11 Blending, Antialiasing and Fog
- 《基于MFC的OpenGL编程》Part 4 Transformations - Rotations, Translations and Scaling
- 《基于MFC的OpenGL编程》Part 10 Blending, Antialiasing and Fog
- 《基于MFC的OpenGL编程》Part 11 Creating and Using Display Lists
- 《基于MFC的OpenGL编程》Part 12 Creating 2D and 3D Text
- 《基于MFC的OpenGL编程》Part 17 Shadows
- 《基于MFC的OpenGL编程》Part 16 Reflection
- 《基于MFC的OpenGL编程》Part 15 Selection
- 《基于MFC的OpenGL编程》Part 14 Quadrics
- 《基于MFC的OpenGL编程》Part 10 Texture Mapping
- 《基于MFC的OpenGL编程》Part 9 Lighting
- 《基于MFC的OpenGL编程》Part 8 Colors
- 《基于MFC的OpenGL编程》Part 7 Animation
- poj1006
- 关联式容器map/multimap之初始化、删除元素、交换元素及清空操作
- LeetCode题解:Palindrome Number
- 【bzoj3238】[Ahoi2013]差异 后缀数组+单调栈
- 【leetcode】Array——Remove Duplicates from Sorted Array(26)
- 《基于MFC的OpenGL编程》Part 5 Keyboard and Mouse Control
- 将1234567890转换成1,234,567,890每3位用逗号隔开的形式(百度)
- leetcode之H-index &&H-index II
- 手动操作导航控制器的子视图控制器的数组
- 【数据结构与算法】五 兔子数列 斐波那契数列
- 《leetCode》:Triangle
- UVA 10474 Where is the Marble? (STL)
- 文件IO
- HDU 5620 KK's Steel 斐波那契