public static vec3 project(vec3 modelPosition, mat4 view, mat4 proj, vec4 viewport) { vec4 tmp = new vec4(modelPosition, (1f)); tmp = view * tmp; tmp = proj * tmp;// this is gl_Position tmp /= tmp.w;// after this, tmp is normalized device coordinate. tmp = tmp * 0.5f + new vec4(0.5f, 0.5f, 0.5f, 0.5f); tmp[0] = tmp[0] * viewport[2] + viewport[0]; tmp[1] = tmp[1] * viewport[3] + viewport[1];// after this, tmp is window coordinate. return new vec3(tmp.x, tmp.y, tmp.z); }