In my game I'm trying to implement an equivalent of gluProject, my problem is that the code only seems to work if the camera has no rotation. Even turning left and right affects the vertical position of the output.
Here is my code so far:
kmVec3 Camera::project_point(ViewportID vid, const kmVec3& point) {
kglt::Viewport& viewport = subscene().scene().window().viewport(vid);
kmVec4 tmp;
kmVec4Fill(&tmp, point.x, point.y, point.z, 1.0);
kmVec3 result;
kmVec4MultiplyMat4(&tmp, &tmp, &view_matrix());
kmVec4MultiplyMat4(&tmp, &tmp, &projection_matrix());
if(tmp.w == 0) {
kmVec3Fill(&result, 0, 0, 0);
return result;
}
tmp.x /= tmp.w;
tmp.y /= tmp.w;
tmp.z /= tmp.w;
float vp_width = viewport.width();
float vp_height = viewport.height();
result.x = vp_width * (tmp.x + 1.0) / 2.0;
result.y = vp_height * (1.0 - ((tmp.y + 1.0) / 2.0));
//result.x = (1 + tmp.x) * vp_width / 2;
//result.y = (1 + tmp.y) * vp_height / 2;
//result.z = (1 + tmp.z) / 2;
return result;
}
In the above example, the view_matrix() is the inverse of the camera's transformation matrix. Is there anything obviously wrong with the above code? I'm having a hard job tracking this down.