Skip to content

Instantly share code, notes, and snippets.

@cmbruns
Last active May 14, 2020 03:18
Show Gist options
  • Save cmbruns/815fc875afd8fe2755500907325b15f0 to your computer and use it in GitHub Desktop.
Save cmbruns/815fc875afd8fe2755500907325b15f0 to your computer and use it in GitHub Desktop.
#version 450 core
// Simplified vertex shader for rendering an infinite plane.
layout(location = 1) uniform mat4 projection = mat4(1);
layout(location = 2) uniform mat4 view = mat4(1);
uniform mat4 model = mat4(1);
// simple but expensive 1: vertices cover the entire screen
// projected screen quad - this is our hard-coded "vertex buffer"
// Render this from the host side with:
// glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
const vec4 screen_quad[4] = vec4[4](
vec4(-1, -1, 1, 1),
vec4( 1, -1, 1, 1),
vec4( 1, 1, 1, 1),
vec4(-1, 1, 1, 1));
const int triangle_strip[4] = int[4](
0, 1, 3, 2);
// Output coordinates of view/plane intersection in model space
out float intersection_w;
vec3 dehomog(vec4 v)
{
return v.xyz/v.w;
}
void main()
{
vec4 corner_clip = screen_quad[triangle_strip[gl_VertexID]];
// Screen-quad corner coordinates arrive dirctly in clip-space. Pass them through unchanged.
gl_Position = corner_clip;
// Compute camera location in model space
const vec4 cam_pos_view = vec4(0, 0, 0, 1);
mat4 model_from_view = inverse(view * model);
vec3 L = dehomog(model_from_view * cam_pos_view); // viewer location
// Compute screen-quad corner location in model space
mat4 view_from_clip = inverse(projection);
vec3 corner_model = dehomog(model_from_view * view_from_clip * corner_clip);
// view direction (not normalized, normalization not required)
vec3 V = corner_model - L;
// hard-coded plane model faces up at Y == 0
vec3 Pn = vec3(0, 1, 0); // plane normal, Y-up
intersection_w = dot(Pn, V); // w
}
#version 450 core
in float intersection_w; // in model space
out vec4 frag_color;
void main()
{
// simple but expensive 2: discard pixels not on the plane
if (intersection_w > 0)
discard;
frag_color = vec4(101/255.0, 67/255.0, 43/255.0, 1); // brown
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment