0

I have a perfectly working fine Arcball implementation. Usually in opengl the standard camera is facing -Z, meaning XY plane is parallel to the viewing plane.

glm::vec3 Arcball::_plane2sphere(glm::vec2 &v)
{
glm::vec2         f = v;
glm::vec3         r;
double            lenSquared;
double            len;

  f         /= _radius;
  lenSquared = f.x * f.x + f.y * f.y;
  len        = sqrt(lenSquared);

  if (lenSquared > 1.0)
  {
    r = glm::vec3(f.x / len,f.y / len, 0.0f );
  }

  else
  { 
    double fz = sqrt(1 - lenSquared);

      r                 = glm::vec3(f.x, f.y, fz);
  }
  return r;
}

Now if I change my camera to face -Y, meaning XZ is parallel to the viewing plane, the same arcball implementation doesnt hold good. I had to change my arcball implementation as follows. The 'Y's taking place of 'Z's and vice versa.

glm::vec3 Arcball::_plane2sphere(glm::vec2 &v)
{
glm::vec2         f = v;
glm::vec3         r;
double            lenSquared;
double            len;

  f         /= _radius;
  lenSquared = f.x * f.x + f.y * f.y;
  len        = sqrt(lenSquared);

  if (lenSquared > 1.0)
  {
    r = glm::vec3(f.x / len, 0.0f, f.y / len);
  }

  else
  { 
    double fz = sqrt(1 - lenSquared);

      r                 = glm::vec3(f.x, fz, f.y);
  }
  return r;
}

While this does make sense to me, I was wondering what if I had a very arbitrary camera. When I have an arbitrary camera, the rotations to my object are not intuitive.

Any help is appreciated! Thanks!

Rathnavel
  • 85
  • 1
  • 15

1 Answers1

0

Your arcball code computes on NDC space, whose axis are parallel to Camera space axis. The computed rotation can be applied directly on Camera space, before projecting into NDC.

If you want to rotate the camera, then you need to convert the axis of rotation to World space.

Notice that all of above applies whatever camera position and orientation are. So, your second version is a way to get rotation in World space for that particular camera orientation. For an arbitrary camera, use matrix transformation from Camera to World.

Ripi2
  • 7,031
  • 1
  • 17
  • 33
  • here's wat I wanna do. I have a camera and an object in myscene. On mouse click drag I want the camera to move around the scene which it does now. On Shift + mouse drag I want the object to rotate. I'm calculating a quaternion from the arcball and multiply it with the camera matrix. When I don't change the camera and just shift drag and rotate the object it works fine. But when I drag&move the cam into an arbitrary position, on shift drag, the object still rotates but it doesnt follow my mouse and it is not intuitive. I'm trying to get intuitive rotation on my object with an arbitrary camera. – Rathnavel Mar 31 '17 at 16:29
  • but I apply the same rotation matrix that I get from the quaternion(but I populate to different variables based on Shift/No Shift) to both the object and the camera and I'm guessing thats not the way to do it. so how do I correct the rotation on the object, when the camera space axis is not parallel to NDC space. I mean what transformation I need to add on the object transformation so it follows my mouse as it does now when cam space is parallel to NDC. – Rathnavel Mar 31 '17 at 16:33
  • The way to go is use BOTH the previous quaternium and the new rotation. Combine them in the right order. – Ripi2 Mar 31 '17 at 16:55