Ouch. It's tier 1.
Then, you could try and rip off GLM's unproject function, which looks like this:
template <typename T, typename U>
GLM_FUNC_QUALIFIER detail::tvec3<T> unProject
(
detail::tvec3<T> const & win,
detail::tmat4x4<T> const & model,
detail::tmat4x4<T> const & proj,
detail::tvec4<U> const & viewport
)
{
detail::tmat4x4<T> inverse = glm::inverse(proj * model);
detail::tvec4<T> tmp = detail::tvec4<T>(win, T(1));
tmp.x = (tmp.x - T(viewport[0])) / T(viewport[2]);
tmp.y = (tmp.y - T(viewport[1])) / T(viewport[3]);
tmp = tmp * T(2) - T(1);
detail::tvec4<T> obj = inverse * tmp;
obj /= obj.w;
return detail::tvec3<T>(obj);
}
That's some work to convert it to BASIC.
Not sure if it can work for you either.
Best option is if Paul could speak out here.