bool Viewer::on_motion_notify_event(GdkEventMotion* event) { DEBUG_MSG("Stub: Motion at " << event->x << ", " << event->y ); Point2D dMouse(event->x - m_lastMouse[0], event->y - m_lastMouse[1]); bool b1,b2,b3; b1 = event->state & GDK_BUTTON1_MASK; b2 = event->state & GDK_BUTTON2_MASK; b3 = event->state & GDK_BUTTON3_MASK; //track ball mode if (m_mode == POSITION) { // do x,y translation if (b1) { double translationScale = 50.0; Vector3D translate(dMouse[0] / translationScale, -dMouse[1] / translationScale , 0); Matrix4x4 t = translation(translate); m_trackballTranslation = m_trackballTranslation * t; } if (b2) { Vector3D translate(0.0, 0.0, dMouse[1]/ 35.0); Matrix4x4 t = translation(translate); m_trackballTranslation = m_trackballTranslation * t; } if (b3) { /* * Track ball rotations are being used. */ float fDiameter; int iCenterX, iCenterY; float fNewModX, fNewModY, fOldModX, fOldModY; /* vCalcRotVec expects new and old positions in relation to the center of the * trackball circle which is centered in the middle of the window and * half the smaller of nWinWidth or nWinHeight. */ fDiameter = (get_width() < get_height()) ? (float)get_width() * 0.5f : (float)get_height() * 0.5f; iCenterX = get_width() / 2; iCenterY = get_height() / 2; fOldModX = m_lastMouse[0] - iCenterX; fOldModY = m_lastMouse[1] - iCenterY; fNewModX = event->x - iCenterX; fNewModY = event->y - iCenterY; float fRotVecX, fRotVecY, fRotVecZ; vCalcRotVec(fNewModX, fNewModY, fOldModX, fOldModY, fDiameter, &fRotVecX, &fRotVecY, &fRotVecZ); /* Negate Y component since Y axis increases downwards * in screen space and upwards in OpenGL. */ Matrix4x4 mNewMat = vAxisRotMatrix(fRotVecX, -fRotVecY, fRotVecZ); // Since all these matrices are meant to be loaded // into the OpenGL matrix stack, we need to transpose the // rotation matrix (since OpenGL wants rows stored // in columns) //vTransposeMatrix(mNewMat); //vRightMultiply(mRotations, mNewMat); m_trackballRotation = m_trackballRotation * mNewMat; } DEBUG_MSG("trackballMatrix: Translation\n" << m_trackballTranslation << "Rotation\n" << m_trackballRotation); } else { // JOINT manipulation mode // rotate head up down relative to y when b2 // rotate head left right with x motion when b3 // grab all selected nodes // grab their parents // rotate their joint left/right, up/down for (std::list<SceneNode*>::const_iterator it = m_selected.begin(), end = m_selected.end(); it != end; ++it) { assert((*it)->is_selected()); SceneNode *parent = (*it)->jointParent(); // check if parent is joint node DEBUG_MSG("Rotating: " << (*it)->name()); assert(parent); DEBUG_MSG("Parent: name " << parent->name()); assert(parent->is_joint()); JointNode *joint = dynamic_cast<JointNode*>(parent); assert(joint); double rotationScale = -3.0; if (b2) { // rotate in degrees joint->rotate( dMouse[1] / rotationScale, 0.0); } if (b3) { joint->rotate(0.0, dMouse[0] / rotationScale); } } } m_lastMouse = Point2D(event->x, event->y); invalidate(); return true; }
//----------------------------------------------------------------------------- //! Gets called whenever the mouse is moved. SLbool SLCamera::onMouseMove(const SLMouseButton button, const SLint x, const SLint y, const SLKey mod) { if (button == ButtonLeft) //================================================ { // new vars needed SLVec3f position = this->translation(); SLVec3f forward = this->forward(); SLVec3f right = this->right(); SLVec3f up = this->up(); // The lookAt point SLVec3f laP = position + _focalDist * forward; // Determine rotation point as the center of the AABB of the hitNode SLVec3f rtP; if (_lookAtRay.length < FLT_MAX && _lookAtRay.hitNode) rtP = _lookAtRay.hitNode->aabb()->centerWS(); else rtP = laP; // Determine rot angles around x- & y-axis SLfloat dY = (y-_oldTouchPos1.y) * _rotFactor; SLfloat dX = (x-_oldTouchPos1.x) * _rotFactor; if (_camAnim==turntableYUp) //....................................... { SLMat4f rot; rot.translate(rtP); rot.rotate(-dX, SLVec3f(0,1,0)); rot.rotate(-dY, right); rot.translate(-rtP); _om.setMatrix(rot * _om); needWMUpdate(); } else if (_camAnim==turntableZUp) //.................................. { SLMat4f rot; rot.translate(rtP); rot.rotate(dX, SLVec3f(0,0,1)); rot.rotate(dY, right); rot.translate(-rtP); _om.setMatrix(rot * _om); needWMUpdate(); } else if (_camAnim==walkingYUp) //.................................... { dY *= 0.5f; dX *= 0.5f; SLMat4f rot; rot.rotate(-dX, SLVec3f(0, 1, 0)); rot.rotate(-dY, right); forward.set(rot.multVec(forward)); lookAt(position + forward); } else if (_camAnim==walkingZUp) //.................................... { dY *= 0.5f; dX *= 0.5f; SLMat4f rot; rot.rotate(-dX, SLVec3f(0, 0, 1)); rot.rotate(-dY, right); forward.set(rot.multVec(forward)); lookAt(position + forward, SLVec3f(0, 0, 1)); } _oldTouchPos1.set((SLfloat)x,(SLfloat)y); } else if (button == ButtonMiddle) //============================================== { if (_camAnim==turntableYUp || _camAnim==turntableZUp) { // Calculate the fraction delta of the mouse movement SLVec2f dMouse(x-_oldTouchPos1.x, _oldTouchPos1.y-y); dMouse.x /= (SLfloat)_scrW; dMouse.y /= (SLfloat)_scrH; // Scale the mouse delta by the lookAt distance SLfloat lookAtDist; if (_lookAtRay.length < FLT_MAX) lookAtDist = _lookAtRay.length; else lookAtDist = _focalDist; // scale factor depending on the space sice at focal dist SLfloat spaceH = tan(SL_DEG2RAD*_fov/2) * lookAtDist * 2.0f; SLfloat spaceW = spaceH * _aspect; dMouse.x *= spaceW; dMouse.y *= spaceH; if (mod==KeyCtrl) { translate(SLVec3f(-dMouse.x, 0, dMouse.y), TS_Object); } else { translate(SLVec3f(-dMouse.x, -dMouse.y, 0), TS_Object); } _oldTouchPos1.set((SLfloat)x,(SLfloat)y); } } //======================================================================== return true; }
//----------------------------------------------------------------------------- //! Gets called whenever the mouse is moved. SLbool SLCamera::onMouseMove(const SLMouseButton button, const SLint x, const SLint y, const SLKey mod) { SLScene* s = SLScene::current; SLSceneView* sv = s->activeSV(); if (button == ButtonLeft) //================================================ { // Get camera vectors: eye pos., lookAt, lookUp, lookRight SLVec3f eye, LA, LU, LR; _vm.lookAt(&eye, &LA, &LU, &LR); // The lookAt and lookUp point in VS SLVec3f laP = eye + _focalDist * LA; // Determine rotation point as the center of the AABB of the hitShape SLVec3f rtP; if (_lookAtRay.length < FLT_MAX && _lookAtRay.hitShape) rtP = _lookAtRay.hitShape->aabb()->centerWS(); else rtP = laP; // Determine rot angles around x- & y-axis SLfloat dY = (_oldTouchPos1.y-y) * _rotFactor; SLfloat dX = (_oldTouchPos1.x-x) * _rotFactor; if (_camAnim==turntableYUp) //....................................... { // Apply rotation around the lookAt point SLMat4f rot; rot.translate(rtP); rot.rotate(-dY, LR); rot.rotate(-dX, SLVec3f(0,1,0)); rot.translate(-rtP); _vm *= rot; } else if (_camAnim==turntableZUp) //.................................. { // Apply rotation around the lookAt point SLMat4f rot; rot.translate(rtP); rot.rotate(-dY, LR); rot.rotate(-dX, SLVec3f(0,0,1)); rot.translate(-rtP); _vm *= rot; } else if (_camAnim==walkingYUp) //.................................... { dY *= 0.5f; dX *= 0.5f; // Apply rotation around the lookRight and the Y-axis SLMat4f rot; rot.rotate(-dY, LR); rot.rotate(-dX, SLVec3f(0,1,0)); // rotate eye position LA.set(rot*LA); _vm.lookAt(eye, eye+LA*_focalDist, SLVec3f(0,1,0)); } else if (_camAnim==walkingZUp) //.................................... { dY *= 0.5f; dX *= 0.5f; // Apply rotation around the lookRight and the Z-axis SLMat4f rot; rot.rotate(-dY, LR); rot.rotate(-dX, SLVec3f(0,0,1)); // rotate eye position LA.set(rot*LA); _vm.lookAt(eye, eye+LA*_focalDist, SLVec3f(0,0,1)); } setWMandState(); _oldTouchPos1.set((SLfloat)x,(SLfloat)y); return true; } else if (button == ButtonMiddle) //============================================== { if (_camAnim==turntableYUp || _camAnim==turntableZUp) { // Calculate the fraction delta of the mouse movement SLVec2f dMouse(x-_oldTouchPos1.x, _oldTouchPos1.y-y); dMouse.x /= (SLfloat)sv->scrW(); dMouse.y /= (SLfloat)sv->scrH(); // Scale the mouse delta by the lookAt distance SLfloat lookAtDist; if (_lookAtRay.length < FLT_MAX) lookAtDist = _lookAtRay.length; else lookAtDist = _focalDist; // scale factor depending on the space sice at focal dist SLfloat spaceH = tan(SL_DEG2RAD*_fov/2) * lookAtDist * 2.0f; SLfloat spaceW = spaceH * sv->scrWdivH(); dMouse.x *= spaceW; dMouse.y *= spaceH; if (mod==KeyCtrl) { _vm.translation(_vm.m(12) + dMouse.x, _vm.m(13), _vm.m(14) + dMouse.y); } else { _vm.translation(_vm.m(12) + dMouse.x, _vm.m(13) + dMouse.y, _vm.m(14)); } setWMandState(); _oldTouchPos1.set((SLfloat)x,(SLfloat)y); return true; } } //======================================================================== return false; }