I found a screen to world coordinates function, but it's not entirely working out.
Does anything look suspicious with this code?
vertex_3 Get_Screen_Ray(vertex_3 eye, vertex_3 look_at, vertex_3 up, const float fov_degrees, const int x, const int y, const int screen_width, const int screen_height)
{
vertex_3 E(eye.x, eye.y, eye.z);
vertex_3 T(look_at.x, look_at.y, look_at.z);
vertex_3 w(up.x, up.y, up.z);
w.normalize();
vertex_3 t = T - E;
vertex_3 b = w.cross(t);
vertex_3 t_n = t;
t_n.normalize();
vertex_3 b_n = b;
b_n.normalize();
vertex_3 v_n = t_n.cross(b_n);
static const float pi = 4.0f * atanf(1.0f);
float theta = fov_degrees * pi / 180.0f;
float aspect = static_cast<float>(screen_width) / static_cast<float>(screen_height);
float g_y = -tanf(theta / 2.0f);
float g_x = g_y * aspect;
vertex_3 q_x = b_n * (2.0f * g_x) / (static_cast<float>(screen_width) - 1.0f);
vertex_3 q_y = v_n * (2.0f * g_y) / (static_cast<float>(screen_height) - 1.0f);
vertex_3 p_1m = t_n - b_n * g_x - v_n * g_y;
vertex_3 p_ij = p_1m + q_x * static_cast<float>(x) + q_y * static_cast<float>(y);
return p_ij;
}
The whole code is at: https://github.com/sjhalayka/opengl4_stlview