-
Notifications
You must be signed in to change notification settings - Fork 22
/
bezier.geom
executable file
·253 lines (216 loc) · 8.16 KB
/
bezier.geom
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#version 330
/* \brief Geometry GLSL shader that demonstrates how to draw basic thick and smooth bezier curves in 3D.
* This file is a part of shader-3dcurve example (https://github.com/vicrucann/shader-3dcurve).
*
* \author Victoria Rudakova
* \date January 2017
* \copyright MIT license
*/
uniform float Thickness;
uniform vec2 Viewport;
uniform float MiterLimit;
uniform int Segments;
const int SegmentsMax = 30; // max_vertices = (SegmentsMax+1)*4;
const int SegmentsMin = 3; // min number of segments per curve
layout(lines_adjacency) in;
layout(triangle_strip, max_vertices = 124) out;
in VertexData{
vec4 mColor;
vec4 mVertex;
} VertexIn[4];
out VertexData{
vec4 mColor;
vec2 mTexCoord; // fix before usage
vec4 mVertex; // to pass in case if we use fog effect
} VertexOut;
vec2 toScreenSpace(vec4 vertex)
{
return vec2( vertex.xy / vertex.w ) * Viewport;
}
float toZValue(vec4 vertex)
{
return (vertex.z/vertex.w);
}
vec4 toBezier(float delta, int i, vec4 P0, vec4 P1, vec4 P2, vec4 P3)
{
float t = delta * float(i);
float t2 = t * t;
float one_minus_t = 1.0 - t;
float one_minus_t2 = one_minus_t * one_minus_t;
return (P0 * one_minus_t2 * one_minus_t + P1 * 3.0 * t * one_minus_t2 + P2 * 3.0 * t2 * one_minus_t + P3 * t2 * t);
}
void drawSegment(vec2 points[4], vec4 colors[4], float zValues[4], vec4 V[4])
{
vec2 p0 = points[0];
vec2 p1 = points[1];
vec2 p2 = points[2];
vec2 p3 = points[3];
/* perform naive culling */
vec2 area = Viewport * 4;
if( p1.x < -area.x || p1.x > area.x ) return;
if( p1.y < -area.y || p1.y > area.y ) return;
if( p2.x < -area.x || p2.x > area.x ) return;
if( p2.y < -area.y || p2.y > area.y ) return;
/* determine the direction of each of the 3 segments (previous, current, next) */
vec2 v0 = normalize( p1 - p0 );
vec2 v1 = normalize( p2 - p1 );
vec2 v2 = normalize( p3 - p2 );
/* determine the normal of each of the 3 segments (previous, current, next) */
vec2 n0 = vec2( -v0.y, v0.x );
vec2 n1 = vec2( -v1.y, v1.x );
vec2 n2 = vec2( -v2.y, v2.x );
/* determine miter lines by averaging the normals of the 2 segments */
vec2 miter_a = normalize( n0 + n1 ); // miter at start of current segment
vec2 miter_b = normalize( n1 + n2 ); // miter at end of current segment
/* determine the length of the miter by projecting it onto normal and then inverse it */
float an1 = dot(miter_a, n1);
float bn1 = dot(miter_b, n2);
if (an1==0) an1 = 1;
if (bn1==0) bn1 = 1;
float length_a = Thickness / an1;
float length_b = Thickness / bn1;
/* prevent excessively long miters at sharp corners */
if( dot( v0, v1 ) < -MiterLimit ) {
miter_a = n1;
length_a = Thickness;
/* close the gap */
if( dot( v0, n1 ) > 0 ) {
VertexOut.mTexCoord = vec2( 0, 0 );
VertexOut.mColor = colors[1];
VertexOut.mVertex = V[1];
gl_Position = vec4( ( p1 + Thickness * n0 ) / Viewport, zValues[1], 1.0 );
EmitVertex();
VertexOut.mTexCoord = vec2( 0, 0 );
VertexOut.mColor = colors[1];
VertexOut.mVertex = V[1];
gl_Position = vec4( ( p1 + Thickness * n1 ) / Viewport, zValues[1], 1.0 );
EmitVertex();
VertexOut.mTexCoord = vec2( 0, 0.5 );
VertexOut.mColor = colors[1];
VertexOut.mVertex = V[1];
gl_Position = vec4( p1 / Viewport, 0.0, 1.0 );
EmitVertex();
EndPrimitive();
}
else {
VertexOut.mTexCoord = vec2( 0, 1 );
VertexOut.mColor = colors[1];
VertexOut.mVertex = V[1];
gl_Position = vec4( ( p1 - Thickness * n1 ) / Viewport, zValues[1], 1.0 );
EmitVertex();
VertexOut.mTexCoord = vec2( 0, 1 );
VertexOut.mColor = colors[1];
VertexOut.mVertex = V[1];
gl_Position = vec4( ( p1 - Thickness * n0 ) / Viewport, zValues[1], 1.0 );
EmitVertex();
VertexOut.mTexCoord = vec2( 0, 0.5 );
VertexOut.mColor = colors[1];
VertexOut.mVertex = V[1];
gl_Position = vec4( p1 / Viewport, zValues[1], 1.0 );
EmitVertex();
EndPrimitive();
}
}
if( dot( v1, v2 ) < -MiterLimit ) {
miter_b = n1;
length_b = Thickness;
}
// generate the triangle strip
VertexOut.mTexCoord = vec2( 0, 0 );
VertexOut.mColor = colors[1];
VertexOut.mVertex = V[1];
gl_Position = vec4( ( p1 + length_a * miter_a ) / Viewport, zValues[1], 1.0 );
EmitVertex();
VertexOut.mTexCoord = vec2( 0, 1 );
VertexOut.mColor = colors[1];
VertexOut.mVertex = V[1];
gl_Position = vec4( ( p1 - length_a * miter_a ) / Viewport, zValues[1], 1.0 );
EmitVertex();
VertexOut.mTexCoord = vec2( 0, 0 );
VertexOut.mColor = colors[2];
VertexOut.mVertex = V[2];
gl_Position = vec4( ( p2 + length_b * miter_b ) / Viewport, zValues[2], 1.0 );
EmitVertex();
VertexOut.mTexCoord = vec2( 0, 1 );
VertexOut.mColor = colors[2];
VertexOut.mVertex = V[2];
gl_Position = vec4( ( p2 - length_b * miter_b ) / Viewport, zValues[2], 1.0 );
EmitVertex();
EndPrimitive();
}
void main(void)
{
/* cut segments number if larger or smaller than allowed */
int nSegments = (Segments > SegmentsMax)? SegmentsMax : Segments;
nSegments = (nSegments < SegmentsMin)? SegmentsMin: nSegments;
// 4 control points
vec4 B[4];
B[0] = gl_in[0].gl_Position;
B[1] = gl_in[1].gl_Position;
B[2] = gl_in[2].gl_Position;
B[3] = gl_in[3].gl_Position;
// vertex format (will be passed to fragment shader for fogging effect)
vec4 V[4];
V[0] = VertexIn[0].mVertex;
V[1] = VertexIn[1].mVertex;
V[2] = VertexIn[2].mVertex;
V[3] = VertexIn[3].mVertex;
// 4 attached colors
vec4 C[4];
C[0] = VertexIn[0].mColor;
C[1] = VertexIn[1].mColor;
C[2] = VertexIn[2].mColor;
C[3] = VertexIn[3].mColor;
/* use the points to build a bezier line */
float delta = 1.0 / float(nSegments);
vec4 Points[4]; // segments of curve in 3d
vec4 colors[4]; // interpolated colors
float zValues[4];
int j = 0; // bezier segment index for color interpolation
for (int i=0; i<=nSegments; ++i){
/* first point */
if (i==0){
Points[1] = toBezier(delta, i, B[0], B[1], B[2], B[3]);
Points[2] = toBezier(delta, i+1, B[0], B[1], B[2], B[3]);
Points[3] = toBezier(delta, i+2, B[0], B[1], B[2], B[3]);
vec4 dir = normalize(Points[1] - Points[2]);
Points[0] = Points[1] + dir * 0.01;
}
else if (i < nSegments-1){
Points[0] = Points[1];
Points[1] = Points[2];
Points[2] = Points[3];
Points[3] = toBezier(delta, i+2, B[0], B[1], B[2], B[3]);
}
/* last point */
else {
Points[0] = Points[1];
Points[1] = Points[2];
Points[2] = Points[3];
vec4 dir = normalize(Points[2] - Points[1]);
Points[3] = Points[2] + dir * 0.01;
}
/* color interpolation: define which bezier segment the point belongs to and then interpolate
between the two colors of that segment */
if (i==0) colors[1] = C[0];
else colors[1] = colors[2];
/* fraction p{i} is located between fraction p{j} and p{j+1} */
float pi = float(i+1) / float(nSegments);
if (pi >= float(j+1)/3.f) j++;
float pj = float(j)/3.f; // 4 bezier points means 3 segments between which points are plotted
float pj1 = float(j+1)/3.f;
float a = (pi-pj) / (pj1-pj);
colors[2] = mix(C[j], C[j+1], a);
// segments of curve in 2d
vec2 points[4];
points[0] = toScreenSpace(Points[0]);
points[1] = toScreenSpace(Points[1]);
points[2] = toScreenSpace(Points[2]);
points[3] = toScreenSpace(Points[3]);
zValues[0] = toZValue(Points[0]);
zValues[1] = toZValue(Points[1]);
zValues[2] = toZValue(Points[2]);
zValues[3] = toZValue(Points[3]);
drawSegment(points, colors, zValues, V);
}
}