-
Notifications
You must be signed in to change notification settings - Fork 0
/
bezier.tese
89 lines (68 loc) · 1.76 KB
/
bezier.tese
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
#version 460 core
#define M_PI 3.1415926535897932384626433832795
#define EPSILON 1e-2
layout (std140, binding = 0) uniform matrices
{
mat4 modelingMatrix;
mat4 viewingMatrix;
mat4 projectionMatrix;
vec3 eyePos;
};
layout (std140, binding = 1) uniform tessLevels
{
float tessInner;
float tessOuter;
float levelOfDetail;
int viewDependantTesselation;
float cameraFov;
};
layout ( quads, equal_spacing, cw) in;
in TESC_TESE_INTERFACE
{
vec3 fragWorldPos;
} tese_in[];
out TESE_FS_INTERFACE
{
vec4 fragWorldPos;
vec3 fragWorldNor;
vec2 texCoord;
} tese_out;
float bern(int i, float u);
void main()
{
float u = gl_TessCoord.x;
float v = gl_TessCoord.y;
tese_out.texCoord = vec2(u,v);
vec3 vert = vec3(0.f, 0.f, 0.f);
for(int i = 0; i <= 3; ++i)
{
for(int j = 0; j <= 3; ++j)
{
vert += bern(i, u) * bern(j, v) * tese_in[i*4 + j].fragWorldPos;
}
}
vec3 du = vec3(0.f, 0.f, 0.f);
vec3 dv = vec3(0.f, 0.f, 0.f);
int dir = 1;
float e1 = EPSILON;
float e2 = EPSILON;
if(u > 0.5){ e1 = -e1; dir = -dir;}
if(v > 0.5){ e2 = -e2; dir = -dir;}
for(int i = 0; i <= 3; ++i)
{
for(int j = 0; j <= 3; ++j)
{
du += bern(i, u+ e1/2.0) * bern(j, v+e2/3.0) * tese_in[i*4 + j].fragWorldPos;
dv += bern(i, u+ e1/3.0) * bern(j, v+e2/2.0) * tese_in[i*4 + j].fragWorldPos;
}
}
tese_out.fragWorldPos = vec4(vert,1);
tese_out.fragWorldNor = cross(dv-vert, du-vert) *dir;
gl_Position = projectionMatrix * viewingMatrix * tese_out.fragWorldPos;
}
float bern(int i, float u)
{
// Binomial lookup table
const float bc[4] = {1.f, 3.f, 3.f, 1.f};
return bc[i] * pow(u, i) * pow(1-u, 3-i);
}