Jump to content
  • Advertisement
Sign in to follow this  
superahoy

Near Plane to Far Plane Line Question

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 point
ModelSpace::Ray_t
CameraView::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 point
ModelSpace::Point_t
CameraView::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 camera
ModelSpace::Vector_t
Camera::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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!