# Problems projecting a point to screen [closed]

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.

-

## closed as too localized by Tetrad♦Apr 2 at 16:39

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

Nevermind, this code works perfectly:

``````kmVec3 Camera::project_point(ViewportID vid, const kmVec3& point) {
kglt::Viewport& viewport = subscene().scene().window().viewport(vid);

kmVec3 tmp;
kmVec3Fill(&tmp, point.x, point.y, point.z);

kmVec3 result;

kmVec3MultiplyMat4(&tmp, &tmp, &view_matrix());
kmVec3MultiplyMat4(&tmp, &tmp, &projection_matrix());

tmp.x /= tmp.z;
tmp.y /= tmp.z;

float vp_width = viewport.width();
float vp_height = viewport.height();

result.x = (tmp.x + 1) * vp_width / 2.0;
result.y = (tmp.y + 1) * vp_height / 2.0;

return result;
}
``````
-