tesspot

view sdr/bezier.te.glsl @ 0:72b7f9f2eead

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 02 Dec 2012 08:23:51 +0200
parents
children befe01bbd27f
line source
1 #version 410 compatibility
3 layout(quads) in;
5 out vec3 normal;
6 out vec3 vpos;
8 vec3 bezier_patch(float u, float v);
9 vec3 bezier_patch_norm(float u, float v);
10 float bernstein(int i, float x);
12 void main()
13 {
14 vec3 pos = bezier_patch(gl_TessCoord.x, gl_TessCoord.y);
15 normal = gl_NormalMatrix * bezier_patch_norm(gl_TessCoord.x, gl_TessCoord.y);
17 gl_Position = gl_ModelViewProjectionMatrix * vec4(pos, 1.0);
18 vpos = (gl_ModelViewMatrix * vec4(pos, 1.0)).xyz;
19 }
21 vec3 bezier_patch(float u, float v)
22 {
23 int i, j;
24 vec3 res = vec3(0.0, 0.0, 0.0);
26 for(j=0; j<4; j++) {
27 for(i=0; i<4; i++) {
28 float bu = bernstein(i, u);
29 float bv = bernstein(j, v);
31 res += gl_in[j * 4 + i].gl_Position.xyz * bu * bv;
32 }
33 }
34 return res;
35 }
37 #define DT 0.0001
38 vec3 bezier_patch_norm(float u, float v)
39 {
40 vec3 tang = bezier_patch(u + DT, v) - bezier_patch(u - DT, v);
41 vec3 bitan = bezier_patch(u, v + DT) - bezier_patch(u, v - DT);
42 return cross(tang, bitan);
43 }
45 float bernstein(int i, float x)
46 {
47 float invx = 1.0 - x;
49 if(i == 0) {
50 return invx * invx * invx;
51 }
52 if(i == 1) {
53 return 3 * x * invx * invx;
54 }
55 if(i == 2) {
56 return 3 * x * x * invx;
57 }
58 return x * x * x;
59 }