Anyway, I'm trying to convert mouse co-ordinates to world space. At the moment, I'm passing in two normalized device co-ordinates, (x,y,-1.0f) and (x,y,1.0f), then transforming them by the inverse of (proj_matrix*view_matrix). I'm expecting to get two points - one on the near clipping plane and one on the far clipping plane, but I'm not. The near plane is 30, and the far plane is 5000, but I'm getting z values of 0.7 and 7 respectively.
I'm not doing any multiplication by any w value to get to clipping space - could that be the problem? If so, how should I get the w value to multiply all the elements by?
Here's the bits of my code that are relevant:
Ray newray(0.2f,-0.2f,-1.0f,0.2f,-0.2f,1.0f);
newray.SetMatrices(cam_->GetProjectionMatrix(),cam_->GetViewMatrix());
newray.Calculate();
class Ray
{
cml::matrix44f_c inv_mat_;
vector3f start_, end_;
vector3f transformed_start_, transformed_end_;
public:
Ray(float sx, float sy, float sz, float dx, float dy, float dz);
void SetRayEnds(float sx, float sy, float sz, float dx, float dy, float dz);
void SetMatrices(const cml::matrix44f_c & proj, const cml::matrix44f_c & view);
void Calculate();
vector3f GetYIntersection(float y);
};
Ray::Ray(float sx, float sy, float sz, float dx, float dy, float dz) :
inv_mat_(cml::identity_4x4()),
start_(sx,sy,sz),
end_(dx,dy,dz)
{
}
void Ray::SetRayEnds(float sx, float sy, float sz, float dx, float dy, float dz)
{
start_.set(sx,sy,sz);
end_.set(dx,dy,dz);
}
void Ray::SetMatrices(const cml::matrix44f_c & proj, const cml::matrix44f_c & view)
{
inv_mat_ = cml::inverse(proj*view);
}
void Ray::Calculate()
{
transformed_start_ = cml::transform_point(inv_mat_, start_);
transformed_end_ = cml::transform_point(inv_mat_, end_);
}
To all the matrix and graphics wizards, what am I doing wrong? Is this the way that you would approach the problem?
Thanks for your help!
Ben
EDIT: World space, not eye space.