rookie1 Wrote:1. How do we decide the camera position? Ideally, it should not be too near nor too far. And it should not be obstructed by other objects.
Hmm... what about just placing the camera some distance away (the distance could be determined by the object's size), and then providing some mechanism for the user the orbit the camera around the object?
Quote:2. Is there anything wrong in the above code to calculate camera direction?
The lookat function is kind of sketchy. It uses the opengl lookat function to change the modelview matrix, and then copies the model view matrix and tries to back-calculate the quaternion representation of that matrix. I don't think it's used and so probably hasn't ever been tested very well. As an alternative, try attacking the problem using vector math. Warning, sloppy pseudo-code ahead:
By default the quaternion points straight ahead (0,0,1). The direction to the object is (campos - objpos).normalize(). You can find the y-axis rotation between those two vectors by setting the y components of each to zero and using the equation:
angle = arccos (a.normalize().dot(b.normalize()))
You can then do
quaternion.Rotate(angle, 0,1,0);
and your camera should be properly rotated (on the Y axis) so it points at the object. Beware of special cases (what if the camera is above the object?). Also, the quaternion class uses radians as the angle units. By the way, you might as well just rewrite the lookat function and use that, since it is unused and apparently broken at the moment.