Skip to content
Prev 77449 / 398502 Next

distance to eye in persp()

(Ted Harding) wrote:
I think you forgot the rescaling to [0,1], but it's probably even easier 
than that.  persp returns the 4x4 transformation matrix used to take 
user coordinates into screen coordinates.  trans3d uses that matrix to 
extract the screen x and screen y coordinates (extend the 3-vector to 
homogeneous coordinates by appending a 1, multiply by the projection 
matrix, convert back to Euclidean coordinates by dividing by the last 
coordinate).  You can probably just do what trans3d does, but keep the 
z-coordinate, to get a reasonable measure of depth.  i.e.

depth3d <- function(x,y,z, pmat) {
   tr <- cbind(x, y, z, 1) %*% pmat
   return(tr[,3]/tr[,4])
}

This is sufficient for doing fog calculations.  For perspective 
shrinkage, you'll need to say where the user's eye is in these 
coordinates (or just use depths as distances).

Duncan Murdoch