《基于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
原创粉丝点击