No Description

terrain.tese 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #version 450
  2. #extension GL_ARB_separate_shader_objects : enable
  3. #extension GL_ARB_shading_language_420pack : enable
  4. layout (set = 0, binding = 0) uniform UBO
  5. {
  6. mat4 projection;
  7. mat4 modelview;
  8. vec4 lightPos;
  9. vec4 frustumPlanes[6];
  10. float displacementFactor;
  11. float tessellationFactor;
  12. vec2 viewportDim;
  13. float tessellatedEdgeSize;
  14. } ubo;
  15. layout (set = 0, binding = 1) uniform sampler2D displacementMap;
  16. layout(quads, equal_spacing, cw) in;
  17. layout (location = 0) in vec3 inNormal[];
  18. layout (location = 1) in vec2 inUV[];
  19. layout (location = 0) out vec3 outNormal;
  20. layout (location = 1) out vec2 outUV;
  21. layout (location = 2) out vec3 outViewVec;
  22. layout (location = 3) out vec3 outLightVec;
  23. layout (location = 4) out vec3 outEyePos;
  24. layout (location = 5) out vec3 outWorldPos;
  25. void main()
  26. {
  27. // Interpolate UV coordinates
  28. vec2 uv1 = mix(inUV[0], inUV[1], gl_TessCoord.x);
  29. vec2 uv2 = mix(inUV[3], inUV[2], gl_TessCoord.x);
  30. outUV = mix(uv1, uv2, gl_TessCoord.y);
  31. vec3 n1 = mix(inNormal[0], inNormal[1], gl_TessCoord.x);
  32. vec3 n2 = mix(inNormal[3], inNormal[2], gl_TessCoord.x);
  33. outNormal = mix(n1, n2, gl_TessCoord.y);
  34. // Interpolate positions
  35. vec4 pos1 = mix(gl_in[0].gl_Position, gl_in[1].gl_Position, gl_TessCoord.x);
  36. vec4 pos2 = mix(gl_in[3].gl_Position, gl_in[2].gl_Position, gl_TessCoord.x);
  37. vec4 pos = mix(pos1, pos2, gl_TessCoord.y);
  38. // Displace
  39. pos.y -= textureLod(displacementMap, outUV, 0.0).r * ubo.displacementFactor;
  40. // Perspective projection
  41. gl_Position = ubo.projection * ubo.modelview * pos;
  42. // Calculate vectors for lighting based on tessellated position
  43. outViewVec = -pos.xyz;
  44. outLightVec = normalize(ubo.lightPos.xyz + outViewVec);
  45. outWorldPos = pos.xyz;
  46. outEyePos = vec3(ubo.modelview * pos);
  47. }