1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
#version 450
// PN patch data
struct PnPatch
{
float b210;
float b120;
float b021;
float b012;
float b102;
float b201;
float b111;
float n110;
float n011;
float n101;
};
layout (set = 0, binding = 0) uniform UBO {
mat4 projection;
mat4 model;
mat4 view;
} ubo;
const float tessAlpha = 1.0;
layout(triangles, fractional_odd_spacing, ccw) in;
layout(location = 0) in vec3 inNormal[];
layout(location = 3) in vec2 inUV0[];
layout(location = 6) in vec2 inUV1[];
layout(location = 9) patch in vec4 inColor;
layout(location = 10) in PnPatch inPnPatch[];
layout (location = 0) out vec3 outWorldPos;
layout (location = 1) out vec3 outNormal;
layout (location = 2) out vec2 outUV0;
layout (location = 3) out vec2 outUV1;
layout (location = 4) out vec4 outColor;
#define uvw gl_TessCoord
void main()
{
vec3 uvwSquared = uvw * uvw;
vec3 uvwCubed = uvwSquared * uvw;
// extract control points
vec3 b210 = vec3(inPnPatch[0].b210, inPnPatch[1].b210, inPnPatch[2].b210);
vec3 b120 = vec3(inPnPatch[0].b120, inPnPatch[1].b120, inPnPatch[2].b120);
vec3 b021 = vec3(inPnPatch[0].b021, inPnPatch[1].b021, inPnPatch[2].b021);
vec3 b012 = vec3(inPnPatch[0].b012, inPnPatch[1].b012, inPnPatch[2].b012);
vec3 b102 = vec3(inPnPatch[0].b102, inPnPatch[1].b102, inPnPatch[2].b102);
vec3 b201 = vec3(inPnPatch[0].b201, inPnPatch[1].b201, inPnPatch[2].b201);
vec3 b111 = vec3(inPnPatch[0].b111, inPnPatch[1].b111, inPnPatch[2].b111);
// extract control normals
vec3 n110 = normalize(vec3(inPnPatch[0].n110, inPnPatch[1].n110, inPnPatch[2].n110));
vec3 n011 = normalize(vec3(inPnPatch[0].n011, inPnPatch[1].n011, inPnPatch[2].n011));
vec3 n101 = normalize(vec3(inPnPatch[0].n101, inPnPatch[1].n101, inPnPatch[2].n101));
// compute texcoords
outUV0 = gl_TessCoord[2]*inUV0[0] + gl_TessCoord[0]*inUV0[1] + gl_TessCoord[1]*inUV0[2];
outUV1 = gl_TessCoord[2]*inUV1[0] + gl_TessCoord[0]*inUV1[1] + gl_TessCoord[1]*inUV1[2];
// normal
// Barycentric normal
vec3 barNormal = gl_TessCoord[2]*inNormal[0] + gl_TessCoord[0]*inNormal[1] + gl_TessCoord[1]*inNormal[2];
vec3 pnNormal = inNormal[0]*uvwSquared[2] + inNormal[1]*uvwSquared[0] + inNormal[2]*uvwSquared[1]
+ n110*uvw[2]*uvw[0] + n011*uvw[0]*uvw[1]+ n101*uvw[2]*uvw[1];
outNormal = tessAlpha * pnNormal + (1.0 - tessAlpha) * barNormal;
// compute interpolated pos
vec3 barPos = gl_TessCoord[2]*gl_in[0].gl_Position.xyz
+ gl_TessCoord[0]*gl_in[1].gl_Position.xyz
+ gl_TessCoord[1]*gl_in[2].gl_Position.xyz;
// save some computations
uvwSquared *= 3.0;
// compute PN position
vec3 pnPos = gl_in[0].gl_Position.xyz*uvwCubed[2]
+ gl_in[1].gl_Position.xyz*uvwCubed[0]
+ gl_in[2].gl_Position.xyz*uvwCubed[1]
+ b210*uvwSquared[2]*uvw[0]
+ b120*uvwSquared[0]*uvw[2]
+ b201*uvwSquared[2]*uvw[1]
+ b021*uvwSquared[0]*uvw[1]
+ b102*uvwSquared[1]*uvw[2]
+ b012*uvwSquared[1]*uvw[0]
+ b111*6.0*uvw[0]*uvw[1]*uvw[2];
outColor = inColor;
// final position and normal
outWorldPos = (1.0 - tessAlpha) * barPos + tessAlpha * pnPos;
gl_Position = ubo.projection * ubo.view * vec4 (outWorldPos, 1.0);
}
|