Arcball Implementation Dependency on View Plane

295 Views Asked by At

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!

1

There are 1 best solutions below

3
Ripi2 On

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.