Example #1
0
bool ObjectRenderer::moveDrag(int x, int y) 
{
  if(!simObject)
    return false;
  
  if(!dragging)
    return false;

  if(dragSelection == NO_SELECTION) // Camera Control
  {
    if(visParams.cameraMode == VisualizationParameterSet::TARGETCAM)
    {
      if(dragType == DRAG_ROTATE)
      {
        // get the translation vector
        Vector3d viewingvec;
        viewingvec.v[0] = (x - dragCurrentPos.x) * 0.01;
        viewingvec.v[1] = 0;
        viewingvec.v[2] = (y - dragCurrentPos.y) * 0.01;
        viewingvec.rotateZ( -(cameraTarget - cameraPos).getZRotation() );

        // and use it
        cameraPos += viewingvec;
        cameraTarget += viewingvec;
      }
      else // if(dragType == DRAG_NORMAL)
      {
        // shift camera position by camera target (generate viewing vector)
        cameraPos -= cameraTarget;

        // reverse z-rotation
        double zrotation(cameraPos.getZRotation());
        cameraPos.rotateZ( zrotation );
        cameraUpVector.rotateZ( zrotation );

        // rotate around the x-axis
        double xrotation((double)(y - dragCurrentPos.y)/100.0);
        if (cameraUpVector.v[2] < 0.0)
          xrotation = -xrotation;
        cameraPos.rotateX( xrotation );
        cameraUpVector.rotateX( xrotation );

        // rotate to old z-rotation
        cameraPos.rotateZ( -zrotation );
        cameraUpVector.rotateZ( -zrotation );

        // add new z-rotation
        cameraPos.rotateZ( (double)(dragCurrentPos.x - x)/100.0 );
        cameraUpVector.rotateZ( (double)(dragCurrentPos.x - x)/100.0 );

        // shift camera back into position
        cameraPos += cameraTarget;
      }
    }
    else if(visParams.cameraMode == VisualizationParameterSet::FREECAM)
    {
      if(dragType == DRAG_ROTATE)
      {
        // get the viewingvec and get some angles
        Vector3d viewingvec = cameraTarget - cameraPos;
        double zrotation(viewingvec.getZRotation());
        viewingvec.rotateZ( zrotation );
        double xrotation(viewingvec.getXRotation());

        // construct a new translation vector
        viewingvec.v[0] = (x - dragCurrentPos.x) * 0.01;
        viewingvec.v[1] = (y - dragCurrentPos.y) * 0.01;
        viewingvec.v[2] = 0.0;
        viewingvec.rotateX( -xrotation );
        viewingvec.rotateZ( -zrotation );

        // and use it
        cameraPos += viewingvec;
        cameraTarget += viewingvec;
      }
      else
      {
        // shift camera target by camera position (generate viewing vector)
        cameraTarget -= cameraPos;

        // reverse z-rotation
        double zrotation(cameraTarget.getZRotation());
        cameraTarget.rotateZ( zrotation );
        cameraUpVector.rotateZ( zrotation );

        // rotate around the x-axis
        double xrotation((double)(dragCurrentPos.y - y)/100.0);
        if (cameraUpVector.v[2] < 0.0)
          xrotation = -xrotation;
        cameraTarget.rotateX( xrotation );
        cameraUpVector.rotateX( xrotation );

        // rotate to old z-rotation
        cameraTarget.rotateZ( -zrotation );
        cameraUpVector.rotateZ( -zrotation );

        // add new z-rotation
        cameraTarget.rotateZ( (double)(dragCurrentPos.x - x)/200.0 );
        cameraUpVector.rotateZ( (double)(dragCurrentPos.x - x)/200.0 );

        // shift target back into position
        cameraTarget += cameraPos;
      }
    }
    else if(visParams.cameraMode == VisualizationParameterSet::PRESENTATION)
    {
      if(dragType == DRAG_ROTATE)
      {
        // do nothing
      }
      else
      {
        presentationSpeed += (int)((y - dragCurrentPos.y) * 10);
        GLHelper::getGLH()->glPrintText(TextObject::COMMON_HINTS, TextObject::WHITE, 2000,
          "Camera Speed (circles/sec): %1.3f", 1000.0/(double)presentationSpeed);
      }
    }

    dragCurrentPos.x = x;
    dragCurrentPos.y = y;
    return true;
  }
  
  else // Editor Control
  {
    if(dragSelection == OBJECT_SELECTION)
    {
      if(dragMode == APPLY_DYNAMICS)
      {
        if(dragType == DRAG_ROTATE)
          simulation->applyTorqueForSelection(x, y);
        else
          simulation->applyForceForSelection(x, y);
      }
      else
      {
        if(dragType == DRAG_ROTATE)
          simulation->rotateObject(x, y);
        else
          simulation->translateObject(x, y);          
      }
    }
    return true;
  }
}