void glutMotion(int x, int y) { if (gIsRotatingCamera) { static const double kTrackBallRadius = 0.8; Vec3d lastPos; lastPos[0] = gLastMouseX * 2.0 / gWindowWidth - 1.0; lastPos[1] = (gWindowHeight - gLastMouseY) * 2.0 / gWindowHeight - 1.0; lastPos[2] = projectToTrackball(kTrackBallRadius, lastPos[0], lastPos[1]); Vec3d currPos; currPos[0] = x * 2.0 / gWindowWidth - 1.0; currPos[1] = (gWindowHeight - y) * 2.0 / gWindowHeight - 1.0; currPos[2] = projectToTrackball(kTrackBallRadius, currPos[0], currPos[1]); currPos.normalize(); lastPos.normalize(); Vec3d rotateVec = lastPos.cross(currPos); double rotateAngle = asin(rotateVec.norm()); if (fabs(rotateAngle) > 1e-6) { double deltaRotation[16]; generateRotationMatrix(deltaRotation, rotateAngle, rotateVec[0], rotateVec[1], rotateVec[2]); multRight(gCameraRotation, deltaRotation); updateCamera(); } } else if (gIsScalingCamera) { float y1 = gWindowHeight - gLastMouseY; float y2 = gWindowHeight - y; gCameraScale *= 1 + (y1 - y2) / gWindowHeight; updateCamera(); } gLastMouseX = x; gLastMouseY = y; }
/******************************************************************************* GLUT callback for mouse motion, which is used for controlling the view rotation and scaling. *******************************************************************************/ void glutMotion(int x, int y) { if (gIsRotatingCamera) { static const double kTrackBallRadius = 0.8; hduVector3Dd lastPos; lastPos[0] = gLastMouseX * 2.0 / gWindowWidth - 1.0; lastPos[1] = (gWindowHeight - gLastMouseY) * 2.0 / gWindowHeight - 1.0; lastPos[2] = projectToTrackball(kTrackBallRadius, lastPos[0], lastPos[1]); hduVector3Dd currPos; currPos[0] = x * 2.0 / gWindowWidth - 1.0; currPos[1] = (gWindowHeight - y) * 2.0 / gWindowHeight - 1.0; currPos[2] = projectToTrackball(kTrackBallRadius, currPos[0], currPos[1]); currPos.normalize(); lastPos.normalize(); hduVector3Dd rotateVec = lastPos.crossProduct(currPos); double rotateAngle = asin(rotateVec.magnitude()); if (!hduIsEqual(rotateAngle, 0.0, DBL_EPSILON)) { hduMatrix deltaRotation = hduMatrix::createRotation( rotateVec, rotateAngle); gCameraRotation.multRight(deltaRotation); updateCamera(); } } else if (gIsScalingCamera) { float y1 = gWindowHeight - gLastMouseY; float y2 = gWindowHeight - y; gCameraScale *= 1 + (y1 - y2) / gWindowHeight; updateCamera(); } gLastMouseX = x; gLastMouseY = y; }
void CHapticViewerView::OnMouseMove(UINT nFlags, CPoint point) { CHapticViewerDoc* pDoc = GetDocument(); Mesh* pObj = pDoc->getObj(); if (!pObj) return; int x = point.x; int y = point.y; if (m_isRotatingCamera) { static const double kTrackBallRadius = 0.8; hduVector3Dd lastPos; lastPos[0] = m_lastMouseX * 2.0 / m_windowWidth - 1.0; lastPos[1] = (m_windowHeight - m_lastMouseY) * 2.0 / m_windowHeight - 1.0; lastPos[2] = projectToTrackball(kTrackBallRadius, lastPos[0], lastPos[1]); hduVector3Dd currPos; currPos[0] = x * 2.0 / m_windowWidth - 1.0; currPos[1] = (m_windowHeight - y) * 2.0 / m_windowHeight - 1.0; currPos[2] = projectToTrackball(kTrackBallRadius, currPos[0], currPos[1]); currPos.normalize(); lastPos.normalize(); hduVector3Dd rotateVec = lastPos.crossProduct(currPos); double rotateAngle = asin(rotateVec.magnitude()); if (!hduIsEqual(rotateAngle, 0.0, DBL_EPSILON)) { CHapticViewerDoc* pDoc = GetDocument(); Mesh* pObj = pDoc->getObj(); hduMatrix deltaRotation = hduMatrix::createRotation( rotateVec, rotateAngle); m_cameraRotation.multRight(deltaRotation); updateGLCamera(); } } if (m_isTranslatingCamera) { m_cameraTranslationX += 10 * double(x - m_lastMouseX)/m_windowWidth; m_cameraTranslationY -= 10 * double(y - m_lastMouseY)/m_windowWidth; updateGLCamera(); } else if (m_isScalingCamera) { float y1 = m_windowHeight - m_lastMouseY; float y2 = m_windowHeight - y; m_cameraScale *= 1 + (y1 - y2) / m_windowHeight; updateGLCamera(); } m_lastMouseX = x; m_lastMouseY = y; }