tesspot

diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/sdr/bezier.te.glsl	Sun Dec 02 08:23:51 2012 +0200
     1.3 @@ -0,0 +1,59 @@
     1.4 +#version 410 compatibility
     1.5 +
     1.6 +layout(quads) in;
     1.7 +
     1.8 +out vec3 normal;
     1.9 +out vec3 vpos;
    1.10 +
    1.11 +vec3 bezier_patch(float u, float v);
    1.12 +vec3 bezier_patch_norm(float u, float v);
    1.13 +float bernstein(int i, float x);
    1.14 +
    1.15 +void main()
    1.16 +{
    1.17 +	vec3 pos = bezier_patch(gl_TessCoord.x, gl_TessCoord.y);
    1.18 +	normal = gl_NormalMatrix * bezier_patch_norm(gl_TessCoord.x, gl_TessCoord.y);
    1.19 +
    1.20 +	gl_Position = gl_ModelViewProjectionMatrix * vec4(pos, 1.0);
    1.21 +	vpos = (gl_ModelViewMatrix * vec4(pos, 1.0)).xyz;
    1.22 +}
    1.23 +
    1.24 +vec3 bezier_patch(float u, float v)
    1.25 +{
    1.26 +	int i, j;
    1.27 +	vec3 res = vec3(0.0, 0.0, 0.0);
    1.28 +
    1.29 +	for(j=0; j<4; j++) {
    1.30 +		for(i=0; i<4; i++) {
    1.31 +			float bu = bernstein(i, u);
    1.32 +			float bv = bernstein(j, v);
    1.33 +
    1.34 +			res += gl_in[j * 4 + i].gl_Position.xyz * bu * bv;
    1.35 +		}
    1.36 +	}
    1.37 +	return res;
    1.38 +}
    1.39 +
    1.40 +#define DT 0.0001
    1.41 +vec3 bezier_patch_norm(float u, float v)
    1.42 +{
    1.43 +	vec3 tang = bezier_patch(u + DT, v) - bezier_patch(u - DT, v);
    1.44 +	vec3 bitan = bezier_patch(u, v + DT) - bezier_patch(u, v - DT);
    1.45 +	return cross(tang, bitan);
    1.46 +}
    1.47 +
    1.48 +float bernstein(int i, float x)
    1.49 +{
    1.50 +	float invx = 1.0 - x;
    1.51 +
    1.52 +	if(i == 0) {
    1.53 +		return invx * invx * invx;
    1.54 +	}
    1.55 +	if(i == 1) {
    1.56 +		return 3 * x * invx * invx;
    1.57 +	}
    1.58 +	if(i == 2) {
    1.59 +		return 3 * x * x * invx;
    1.60 +	}
    1.61 +	return x * x * x;
    1.62 +}