+
+ // Given a projection matrix, return a new one with the same frustum
+ // sides and new near / far values.
+
+ void makeNewProjMat(Matrixd& oldProj, double znear,
+ double zfar, Matrixd& projection)
+ {
+ projection = oldProj;
+ // Slightly inflate the near & far planes to avoid objects at the
+ // extremes being clipped out.
+ znear *= 0.999;
+ zfar *= 1.001;
+
+ // Clamp the projection matrix z values to the range (near, far)
+ double epsilon = 1.0e-6;
+ if (fabs(projection(0,3)) < epsilon &&
+ fabs(projection(1,3)) < epsilon &&
+ fabs(projection(2,3)) < epsilon) {
+ // Projection is Orthographic
+ epsilon = -1.0/(zfar - znear); // Used as a temp variable
+ projection(2,2) = 2.0*epsilon;
+ projection(3,2) = (zfar + znear)*epsilon;
+ } else {
+ // Projection is Perspective
+ double trans_near = (-znear*projection(2,2) + projection(3,2)) /
+ (-znear*projection(2,3) + projection(3,3));
+ double trans_far = (-zfar*projection(2,2) + projection(3,2)) /
+ (-zfar*projection(2,3) + projection(3,3));
+ double ratio = fabs(2.0/(trans_near - trans_far));
+ double center = -0.5*(trans_near + trans_far);
+
+ projection.postMult(osg::Matrixd(1.0, 0.0, 0.0, 0.0,
+ 0.0, 1.0, 0.0, 0.0,
+ 0.0, 0.0, ratio, 0.0,
+ 0.0, 0.0, center*ratio, 1.0));
+ }
+ }
+
+ osg::Matrix
+ invert(const osg::Matrix& matrix)
+ {
+ return osg::Matrix::inverse(matrix);
+ }
+
+ /// Returns the zoom factor of the master camera.
+ /// The reference fov is the historic 55 deg
+ double
+ zoomFactor()
+ {
+ double fov = fgGetDouble("/sim/current-view/field-of-view", 55);
+ if (fov < 1)
+ fov = 1;
+ return tan(55*0.5*SG_DEGREES_TO_RADIANS)/tan(fov*0.5*SG_DEGREES_TO_RADIANS);
+ }
+
+ osg::Vec2d
+ preMult(const osg::Vec2d& v, const osg::Matrix& m)
+ {
+ osg::Vec3d tmp = m.preMult(osg::Vec3(v, 0));
+ return osg::Vec2d(tmp[0], tmp[1]);
+ }
+
+ osg::Matrix
+ relativeProjection(const osg::Matrix& P0, const osg::Matrix& R, const osg::Vec2d ref[2],
+ const osg::Matrix& pP, const osg::Matrix& pR, const osg::Vec2d pRef[2])
+ {
+ // Track the way from one projection space to the other:
+ // We want
+ // P = T*S*P0
+ // where P0 is the projection template sensible for the given window size,
+ // T is a translation matrix and S a scale matrix.
+ // We need to determine T and S so that the reference points in the parents
+ // projection space match the two reference points in this cameras projection space.
+
+ // Starting from the parents camera projection space, we get into this cameras
+ // projection space by the transform matrix:
+ // P*R*inv(pP*pR) = T*S*P0*R*inv(pP*pR)
+ // So, at first compute that matrix without T*S and determine S and T from that
+
+ // Ok, now osg uses the inverse matrix multiplication order, thus:
+ osg::Matrix PtoPwithoutTS = invert(pR*pP)*R*P0;
+ // Compute the parents reference points in the current projection space
+ // without the yet unknown T and S
+ osg::Vec2d pRefInThis[2] = {
+ preMult(pRef[0], PtoPwithoutTS),
+ preMult(pRef[1], PtoPwithoutTS)
+ };
+
+ // To get the same zoom, rescale to match the parents size
+ double s = (ref[0] - ref[1]).length()/(pRefInThis[0] - pRefInThis[1]).length();
+ osg::Matrix S = osg::Matrix::scale(s, s, 1);
+
+ // For the translation offset, incorporate the now known scale
+ // and recompute the position ot the first reference point in the
+ // currents projection space without the yet unknown T.
+ pRefInThis[0] = preMult(pRef[0], PtoPwithoutTS*S);
+ // The translation is then the difference of the reference points
+ osg::Matrix T = osg::Matrix::translate(osg::Vec3d(ref[0] - pRefInThis[0], 0));
+
+ // Compose and return the desired final projection matrix
+ return P0*S*T;
+ }