#version 330 core out vec3 nearPoint; out vec3 farPoint; uniform mat4 projection; uniform mat4 view; // Grid position are in xy clipped space vec3 gridPlane[6] = vec3[]( vec3(1, 1, 0), vec3(-1, -1, 0), vec3(-1, 1, 0), vec3(-1, -1, 0), vec3(1, 1, 0), vec3(1, -1, 0) ); vec3 UnprojectPoint(float x, float y, float z, mat4 v, mat4 proj) { mat4 viewInv = inverse(v); mat4 projInv = inverse(proj); vec4 unprojectedPoint = viewInv * projInv * vec4(x, y, z, 1.0); return unprojectedPoint.xyz / unprojectedPoint.w; } void main() { vec3 p = gridPlane[gl_VertexID].xyz; nearPoint = UnprojectPoint(p.x, p.y, 0.0, view, projection).xyz; // unprojecting on the near plane farPoint = UnprojectPoint(p.x, p.y, 1.0, view, projection).xyz; // unprojecting on the far plane gl_Position = vec4(p, 1.0); // using directly the clipped coordinates }