7

I wish to generate rays from the camera through the viewing plane. In order to do this, I need my camera position ("eye"), the up, right, and towards vectors (where towards is the vector from the camera in the direction of the object that the camera is looking at) and P, the point on the viewing plane. Once I have these, the ray that's generated is:

ray = camera_eye + t*(P-camera_eye);

where t is the distance along the ray (assume t = 1 for now).

My question is, how do I obtain the 3D coordinates of point P given that it is located at position (i,j) on the viewing plane? Assume that the upper left and lower right corners of the viewing plane are given.

NOTE: The viewing plane is not actually a plane in the sense that it doesn't extend infinitely in all directions. Rather, one may think of this plane as a widthxheight image. In the x direction, the range is 0-->width and in the y direction the range is 0-->height. I wish to find the 3D coordinate of the (i,j)th element, 0

Myx
  • 1,792
  • 5
  • 23
  • 37
  • it could be at any angle. But you know that the viewing plane is perpendicular to the camera and is in the direction of `camera_towards`. Thus, `camera_towards` is the vector perpendicular to the viewing plane and `camera_eye+camera_towards` is the center of the viewing plane of size width*height. – Myx Mar 29 '10 at 15:32
  • Sorry - deleted my comment as I ended up understanding the question better. But (@all) for the sake of clarity since Myx posted a response, I asked if the viewing plane could be at any angle, or if it was always at a certain angle with respect to the camera. – Cam Mar 29 '10 at 15:37

3 Answers3

2

General solution of the itnersection of a line and a plane see http://local.wasp.uwa.edu.au/~pbourke/geometry/planeline/

Your particular graphics lib (OpenGL/DirectcX etc) may have an standard way to do this

edit: You are trying to find the 3d intersection of a screen point (eg a mouse cursor) with a 3d object in you scene?

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
  • I am not trying to find the intersection of a line and a plane. I am actually trying to generate the line. That is, the second end-point of the line is un-known and that's what I'm trying to solve for. The "viewing plane" is just terminology. It's not actually a plane (since a plane extends infinitely in all directions). Rather this "plane" has fixed dimension and I am trying to figure out the 3D coordinates of each of the points on the plane (the points on the plane range from 0 to width in the x direction and 0 to height in the y direction) – Myx Mar 29 '10 at 15:45
  • That's what these algorithms achieve. You can identify the polygons that are represented in a pixel, as well as the barycentric coordinates *within* it. – greyfade Mar 29 '10 at 15:51
0

To work out P, you need the distance from the camera to the near clipping plane (the screen), the size of the window on the near clipping plane (or the view angle, you can work out the window size from the view angle) and the size of the rendered window.

  1. Scale the screen position to the range -1 < x < +1 and -1 < y < +1 where +1 is the top/right and -1 is the bottom/left
  2. Scale normalised x,y by the view window size
  3. Scale by the right and up vectors of the camera and sum the results
  4. Add the look at vector scaled by the clipping plane distance

In effect, you get:

p = at * near_clip_dist + x * right + y * up

where x and y are:

x = (screen_x - screen_centre_x) / (width / 2) * view_width
y = (screen_y - screen_centre_y) / (height / 2) * view_height
Skizz
  • 69,698
  • 10
  • 71
  • 108
  • what's the difference between view_width and width? – Myx Mar 29 '10 at 20:13
  • @Myx: width is the screen width, usually in pixels. view_width is the width, in world co-ordinates, of the window positioned at the near clip plane through which the camera looks. – Skizz Mar 29 '10 at 22:43
0

When I directly plugged in suggested formulas into my program, I didn't obtain correct results (maybe some debugging needed to be done). My initial problem seemed to be in the misunderstanding of the (x,y,z) coordinates of the interpolating corner points. I was treating x,y,z-coordinates separately, where I should not (and this may be specific to the application, since the camera can be oriented in any direction). Instead, the solution turned out to be a simple interpolation of the corner points of the viewing plane:

  • interpolate the bottom corner points in the i direction to get P1
  • interpolate the top corner points in the i direction to get P2
  • interpolate P1 and P2 in the j direction to get the world coordinates of the final point
Myx
  • 1,792
  • 5
  • 23
  • 37