screenspace.vp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #version 140
  2. // The model's vertex position and texture coordinates.
  3. in vec4 position;
  4. in vec2 texcoord0;
  5. // The projection, view and world matrices.
  6. uniform general_vp
  7. {
  8. mat4 mtx_world;
  9. mat4 mtx_view;
  10. mat4 mtx_proj;
  11. };
  12. // The output of a vertex shader are passed to the fragment shader.
  13. // The texture coordinates of the vertex.
  14. out vec2 var_texcoord0;
  15. // The screen texture coordinates of the vertex.
  16. out vec4 var_screen_texcoord;
  17. // Converts the clip space position to the screen position.
  18. vec4 clip_to_screen(vec4 pos)
  19. {
  20. // Position is [-w,w], convert to [-0.5w,0.5w]
  21. vec4 o = pos * 0.5;
  22. // Convert from [-0.5w + 0.5w,0.5w + 0.5w] to [0,w]
  23. o.xy = vec2(o.x, o.y) + o.w;
  24. // Keep "zw" as it is
  25. o.zw = pos.zw;
  26. return o;
  27. }
  28. void main()
  29. {
  30. // Pass the texture coordinates to the fragment shader.
  31. var_texcoord0 = texcoord0;
  32. // Transform the vertex position to clip space.
  33. vec4 vertex_pos = mtx_proj * mtx_view * mtx_world * vec4(position.xyz, 1.0);
  34. gl_Position = vertex_pos;
  35. // Convert the clip space position to the screen position and pass the value to the fragment shader.
  36. var_screen_texcoord = clip_to_screen(vertex_pos);
  37. }