# Near Plane to Far Plane Line Question

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

## Recommended Posts

I want to be able to generate the point on the near plane and the far plane for a given mouse point. What I want is the line / ray generated by a mouse point as it travels from the near clipping plane to the far clipping pane. Does anybody have an example that does this?

##### Share on other sites
Here is my solution. It starts with a 2D screen point, asking the CameraView for the corresponding 3D point on the view plane, uses that point as the origin of a ray, where the ray's track is set to a projector as the camera defines.
// ray from screen pointModelSpace::Ray_tCameraView::rayFromPixel(const Screen::Point& location) const {   Point3_t origin(pointFromPixel(location));   Vector3_t track(_camera->getProjector(origin));   return Ray3_t(origin,track);}// point on view plane from a screen pointModelSpace::Point_tCameraView::pointFromPixel(const Screen::Point& location) const {   // computing point on view plane in camera's frame   register ModelSpace::scalar_t x = ((ModelSpace::scalar_t(location.s0)+0.5f)/widthInPixels()-0.5f)*_width;   register ModelSpace::scalar_t y = ((ModelSpace::scalar_t(location.s1)+0.5f)/heightInPixels()-0.5f)*_height;   register ModelSpace::scalar_t z = _camera->viewDistance();   return Point3_t(x,y,z);}// projector from cameraModelSpace::Vector_tCamera::getProjector(const Point3_t& onViewPlane) const {   Vector3_t result;   if(_modeOfProjection==Projection::PERSPECTIVE) {      result.setFromTo(Point3_t::ZERO,onViewPlane).normalize();   } else /* PARALLEL */ {      result.set(Vector3_t::UNIT_2);   }   // considers any LOS aberration if necessary ...   if(_horizontalLOSAberration || _verticalLOSAberration) {      result.s0 -= _horizontalLOSAberration*(result.s2-_viewDistance);      result.s1 -= _verticalLOSAberration*(result.s2-_viewDistance);      result.normalize();   }   return result;}

Some more explanations: The .s0, .s1, and .s2 components correspond with x, y, and z, resp., for spatial space. The CameraView::widthInPixels() and CameraView::heightInPixels() is the size in _pixels_, while CameraView::_width and CameraView::_height is the corresponding size in _world's length units_ (e.g. meters).

The xxxLOSAberration is good for non-orthogonal parallel projections (e.g. Cavalier and Cabinet) and probably of no interest for you.

Please notice that I've used here the view plane as input plane (i.e. the mouse click is located on the view plane), and the view plane is just Camera::viewDistance() many world's length units (e.g. meters) away.

From the ray any point could be computed by ray/plane intersection.

The rest should be self explanatory, but don't hesitate to ask if something is unclear.

• ### 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
634058
• Total Posts
3015288
×