1/*
2The MIT License (MIT)
3
4Copyright (c) 2022 Sascha Willems
5
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22SOFTWARE.
23*/
24
25#version 450
26
27layout (set = 0, binding = 0) uniform UBO
28{
29	mat4 projection;
30	mat4 modelview;
31	vec4 lightPos;
32	vec4 frustumPlanes[6];
33	float displacementFactor;
34	float tessellationFactor;
35	vec2 viewportDim;
36	float tessellatedEdgeSize;
37} ubo;
38
39layout (set = 0, binding = 1) uniform sampler2D displacementMap;
40
41layout(quads, equal_spacing, cw) in;
42
43layout (location = 0) in vec3 inNormal[];
44layout (location = 1) in vec2 inUV[];
45
46layout (location = 0) out vec3 outNormal;
47layout (location = 1) out vec2 outUV;
48layout (location = 2) out vec3 outViewVec;
49layout (location = 3) out vec3 outLightVec;
50layout (location = 4) out vec3 outEyePos;
51layout (location = 5) out vec3 outWorldPos;
52
53void main()
54{
55	// Interpolate UV coordinates
56	vec2 uv1 = mix(inUV[0], inUV[1], gl_TessCoord.x);
57	vec2 uv2 = mix(inUV[3], inUV[2], gl_TessCoord.x);
58	outUV = mix(uv1, uv2, gl_TessCoord.y);
59
60	vec3 n1 = mix(inNormal[0], inNormal[1], gl_TessCoord.x);
61	vec3 n2 = mix(inNormal[3], inNormal[2], gl_TessCoord.x);
62	outNormal = mix(n1, n2, gl_TessCoord.y);
63
64	// Interpolate positions
65	vec4 pos1 = mix(gl_in[0].gl_Position, gl_in[1].gl_Position, gl_TessCoord.x);
66	vec4 pos2 = mix(gl_in[3].gl_Position, gl_in[2].gl_Position, gl_TessCoord.x);
67	vec4 pos = mix(pos1, pos2, gl_TessCoord.y);
68	// Displace
69	pos.y -= textureLod(displacementMap, outUV, 0.0).r * ubo.displacementFactor;
70	// Perspective projection
71	gl_Position = ubo.projection * ubo.modelview * pos;
72
73	// Calculate vectors for lighting based on tessellated position
74	outViewVec = -pos.xyz;
75	outLightVec = normalize(ubo.lightPos.xyz + outViewVec);
76	outWorldPos = pos.xyz;
77	outEyePos = vec3(ubo.modelview * pos);
78}
79