• ### What is your GameDev Story?

#### Archived

This topic is now archived and is closed to further replies.

# Viewport to scene point

This topic is 6848 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I''m working on a level editor of sorts and have run across a problem I''m not doing very well with. I want to be able to select a polygon in the scene with a mouse click. So, I need to convert the viewport point of the mouse click into a point on the viewing plane. Then, create a line from that point into the scene so I can use it to intersect against the polygons in the scene. I realize the width and height of the viewing plane are FOV/2. However, I don''t know how to convert an FOV in radians into world (distance) units. For example, if I have the following (I''m using D3D)... Camera: (0,0,20); Look At: (0,0,-10); Up Vector: (0,1,0); Near Clipping Plane: 3.0 Far Clipping Plane: -10.0 FOV: 30 (degrees) Given some viewport mouse click of (X,Y), how do I go about generating that point in the view frustum? Any pointers will be greatly appreciated.

##### Share on other sites
If the distance from viewpoint to viewing plane is d and the angle is fov you could calculate the distance from the middle of the viewing plane to the point with on the viewing plane with D = d*tan(fov).

GA

##### Share on other sites
I''ve made a TON of progress, but am still having problems with determining the selection point...

Here is what I''m currently doing...

//
// Givens
Viewport mouse click position (Mx,My)
viewport size: ViewX,viewY
Camera Location: vCamera (0,0,20);
Near clipping plane z: fNearClip(3.0);
Field of View: FOV(30)

//
// Compute width/height of near clipping plane
D3DVECTOR vCenter=D3DVECTOR(0,0,0); // Center of view plane
D3DVECTOR vDist=vCamera-vCenter;
float fViewDist=sqrt(vDist.x*vDist.*+vDist.y*vDist.y+vDist.z*vDist.z);
float fSize=2.0*fViewDist*tan(FOV/2.0*3.14159/180.0);

//
// Point on near clipping plane that was hit
D3DVECTOR vPoint;

vPoint.x=fSize/2.0 - fSize*Mx/viewX;
vPoint.y=fSize/2.0 - fSize*My/viewY;
vPoint.z=fNearClip;

IF do do the above, without moving the camera, it works perfectly. However, when I move the camera to any other x or y location, this doesn''t work.

Here are some comments/questions I still have:

1. It turns out that ''D = d*tan(fov)'' gives you the width/height of the near clipping rectangle, not the distance from the middle of the viewing plane to the point selected on the viewing plane.

2. What is the viewing plane? I''ve yet to understand how to determine the viewing (projection?) plane. I know how to tell D3D the projection matrix, but I''m not sure I understand what ends up being the viewing plane. You''ll note above that I say the center of the viewing plane is (0,0,0)...and this works (without the camera being rotated to another location), but I can''t believe that is the actual center of the viewing plane.

3. Is 2.0*tan(1/2.0) equal to tan(1.0)?

4. The reason for doing that funny subtraction stuff on the x and y points is to correctly orient the point about the XYZ orientation (right handed, I believe) that D3D uses.

I''ve made a lot of progress in my understanding of this, but seem to be hitting a wall. Any suggestions or pointers in the right direction will be very much appreciated.

Thanks,

Dean M.

##### Share on other sites
1. Sorry, d*tan(fov) is not what you wanted, it''s d*tan(fov/2) = half the width/height of the near clipping rectangle. If you''ve a point on the viewport, calculate the distance from the middle of the viewport and divide it by half of the width of the viewport. Multiply this result by the width/height of the near clipping rect, then you get the distance from the middle of the near clipping rectangle.

2. The viewing plane:
When a vertex is projected onto the screen, a line between the vertex and the camera position is made. If this line meets the near clipping plane inside the near clipping rectangle, you see the vertex on the screen.

3. No, else the tan calculation wouldn''t cost that much time.

4.

GA

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 9
• 15
• 14
• 46
• ### Forum Statistics

• Total Topics
634062
• Total Posts
3015303
×