tesspot

view sdr/bezier.te.glsl @ 1:befe01bbd27f

tessellated the teapot
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 02 Dec 2012 17:16:32 +0200
parents 72b7f9f2eead
children 178a9e3c3c8c
line source
1 #version 410 compatibility
3 layout(quads, ccw) in;
5 out vec3 normal;
6 out vec3 vpos;
8 uniform vec3 norm_scale;
10 vec3 bezier_patch(float u, float v);
11 vec3 bezier_patch_norm(float u, float v);
12 float bernstein(int i, float x);
14 void main()
15 {
16 vec3 pos = bezier_patch(gl_TessCoord.x, gl_TessCoord.y);
17 normal = gl_NormalMatrix * bezier_patch_norm(gl_TessCoord.x, gl_TessCoord.y);
19 gl_Position = gl_ModelViewProjectionMatrix * vec4(pos, 1.0);
20 vpos = (gl_ModelViewMatrix * vec4(pos, 1.0)).xyz;
21 }
23 vec3 bezier_patch(float u, float v)
24 {
25 int i, j;
26 vec3 res = vec3(0.0, 0.0, 0.0);
28 for(j=0; j<4; j++) {
29 for(i=0; i<4; i++) {
30 float bu = bernstein(i, u);
31 float bv = bernstein(j, v);
33 res += gl_in[j * 4 + i].gl_Position.xyz * bu * bv;
34 }
35 }
36 return res;
37 }
39 #define DT 0.0001
40 vec3 bezier_patch_norm(float u, float v)
41 {
42 vec3 tang = bezier_patch(u + DT, v) - bezier_patch(u - DT, v);
43 vec3 bitan = bezier_patch(u, v + DT) - bezier_patch(u, v - DT);
44 return cross(tang, bitan) * norm_scale;
45 }
47 float bernstein(int i, float x)
48 {
49 float invx = 1.0 - x;
51 if(i == 0) {
52 return invx * invx * invx;
53 }
54 if(i == 1) {
55 return 3 * x * invx * invx;
56 }
57 if(i == 2) {
58 return 3 * x * x * invx;
59 }
60 return x * x * x;
61 }