diff --git a/src/rendering/twn_draw.c b/src/rendering/twn_draw.c index ed68e72..0705155 100644 --- a/src/rendering/twn_draw.c +++ b/src/rendering/twn_draw.c @@ -521,19 +521,31 @@ DrawCameraUnprojectResult draw_camera_unproject(Vec2 point, .far_z = draw_distance }; - Matrix4 const projection_matrix = orthographic ? camera_orthographic(&camera) : camera_perspective(&camera); - Matrix4 const look_at_matrix = camera_look_at(&camera); - /* TODO: cache matrices for repeated camera inputs, those are expensive */ - Matrix4 const inverse_view_proj = matrix_inverse(matrix_multiply(projection_matrix, look_at_matrix)); - point.y = (float)ctx.base_render_height - point.y; Vec4 v; v.x = 2.0f * point.x / (float)ctx.base_render_width - 1.0f; v.y = 2.0f * point.y / (float)ctx.base_render_height - 1.0f; - v.z = 2.0f * 1.0f - 1.0f; + v.z = 2.0f * 1.0f - 1.0f; v.w = 1.0f; + /* simpler case, just shoot a point from viewbox face along the supplied direction */ + if (orthographic) { + Vec3 right = vec3_cross(direction, up); + Vec3 aup = vec3_cross(direction, right); + return (DrawCameraUnprojectResult){ + .direction = direction, + .position = vec3_add(vec3_add(position, vec3_scale(right, v.x * aspect/zoom)), vec3_scale(aup, -v.y * aspect/zoom)), + }; + } + + /* TODO: sanity check for point being in proper box? it would produce bogus results otherwise */ + + Matrix4 const projection_matrix = orthographic ? camera_orthographic(&camera) : camera_perspective(&camera); + Matrix4 const look_at_matrix = camera_look_at(&camera); + /* TODO: cache matrices for repeated camera inputs, those are expensive */ + Matrix4 const inverse_view_proj = matrix_inverse(matrix_multiply(projection_matrix, look_at_matrix)); + v = matrix_vector_multiply(inverse_view_proj, v); /* TODO: is it even ever not equal to 1 in our case ? */