Control of Orthographic camera position and orient

2019-09-06 05:41发布

问题:

Attempting to control orthorgraphic camera position (x,y,z) and up vector (top of camera) from euler angles.

I have a real world device with an IMU that outputs rotation along each axis (x,y,z). I'm trying to translate these angles into camera eye position and up vector direction. If the physical device is altered, the change in orientation should be reflected in the change in position of the camera.

I'm having a hard time with the math behind this mapping, and have not had a ton of luck in what I've attempted. I can get single axis rotations to work but multiple axes has been a problem. Any help in mapping yaw,pitch and roll from physical device onto the position and up vector of camera would be amazing. The math is proving to be quite a problem for me.

The distance from camera to object and target point should remain constant.

radius = camera.target.distanceTo(camera.eye)
pos = get_data()
#convert to radians and cast to float
roll = float(pos['x'])*(math.pi/180.0)
pitch = float(pos['y'])*(math.pi/180.0)
yaw = float(pos['z'])*(math.pi/180.0)

x = camera.eye.x
y = camera.eye.y
z = camera.eye.z

x = x*math.cos(pitch)*math.sin(yaw)
y = y*math.cos(yaw)*math.cos(pitch)
z = z*math.sin(pitch)

eye = Point3D.create(x,y,z)

camera.eye = eye
camera.target = target      
camera.upVector = up

回答1:

If your Euler angles represent an absolute rotation, you can calculate a rotation matrix R. How you do this depends on the definition of the Euler angles, which should be described in the documentation. It might be:

R = rotateY(yaw) * rotateX(pitch) * rotateZ(roll)

Again, this is just an example. The correct formula depends on the order of Euler angles and their interpretation.

The camera's model matrix can then be calculated as:

cam = translation(target) * R * translation(0, 0, radius)

(or -radius for left-handed coordinate systems). translation() calculates a translation matrix.

The intuition behind this is that you move your camera to the target, then rotate it according to R, and then move it back by radius.

If you have this matrix, the camera's up-vector will be its second column, the eye-vector will be its fourth column. If you just need a view-matrix, use cam's inverse.

Note: The explanations above assume that vectors are treated as column vectors. If they are treated as row vectors (e.g. common in DirectX), the calculations have to be adapted accordingly.