Hi,
I am using an ortho camera and trying to convert from screen to world coordinates using this calculation. But it does not seem to work.
Can anyone explain why?
Previously i was, getting the ratio of screen pos to screen size, multiplying that ratio by the ortho camera frustum size, then adding that to the left and top of the frustum.
I thought i would change to the way below so it would work for all camera types, not just ortho.
ci::mat4 matProjection = m_pCameraOrtho->getProjectionMatrix() * m_pCameraOrtho->getViewMatrix();
ci::mat4 matInverse = ci::inverse(matProjection);
float x = vPos.x;
float y = vPos.y;
float in[4];
float winZ = 1.0;
// convert to NDC normalized device coordinates
in[0] = (2.0f * ((float)(x - 0) / (ci::app::getWindowWidth() - 0))) - 1.0f,
in[1] = 1.0f - (2.0f * ((float)(y - 0) / (ci::app::getWindowHeight() - 0)));
in[2] = 2.0 * winZ - 1.0;
in[3] = 1.0;
ci::vec4 vIn = ci::vec4(in[0], in[1], in[2], in[3]);
// convert to world coordiantes
ci::vec4 _vPos = vIn * matInverse;
_vPos.w = 1.0 / _vPos.w;
// scale up to pixel coordinates
_vPos.x *= _vPos.w;
_vPos.y *= _vPos.w;
_vPos.z *= _vPos.w;
return ci::vec2(_vPos);