Hi!

Trying to implement a phong tesselation vertex shader the whole day now. I don’t know a lot about shaders, so I seem to fail. I got something working, but it doesn’t seem to do the right thing and I’d really appreciate any help here! Thank You!!!

Googling will get you informed about the principle, I read it all in a book called GPU pro, which features an article by Tamy Boubekeur about this.

If you try this, be sure to have the model tristripped.

Here is the code(vertex shader), an attempt to merge the code I found in that article with the output of the standard Phong shader in TD.

[code]uniform vec4 uAmbientColor;

uniform vec4 uDiffuseColor;

uniform vec3 uSpecularColor;

uniform float uShininess;

uniform float uShadowStrength;

uniform vec3 uShadowColor;

out Vertex {

vec4 color;

vec3 camSpaceVert;

vec3 camVector;

vec3 norm;

}vVert;

//START TESSELLATION SHADER CODE

//

vec3 p0, p1, p2= P;

vec3 n0, n1, n2 = N;

float alpha = 3/4; //ShapeFactor

vec3 project (vec3 p, vec3 c, vec3 n) {

return p - dot(p - c, n)*n;
}
//
vec3 PhongGeometry (float u, float v, float w) {
vec3 p = w*p0 + u

*p1 + v*p2;

vec3 c0 = project (p, p0, n0);

vec3 c1 = project (p, p1, n1);

vec3 c2 = project (p, p2, n2);

vec3 q = w

*c0 + u*c1 + v*c2;

vec3 r = mix(p, q, alpha);

return r;

}

vec3 PhongNormal (float u, float v, float w) {

vec3 n = normalize (w*n0 + u*n1 + v*n2);

return n;

}

//END TESSELLATION SHADER CODE

void main()

{

// First deform the vertex and normal

// TDDeform always returns values in world space

//vec3 p0, p1, p2 = P;

//vec3 n0, n1, n2 = N;

//float alpha = 3/4;

//vec3 project (vec3 p, vec3 c, vec3 n) {

// return p - dot (p - c, n)*n;

//}

//X = PhongGeometry(p0, p1, p2);

vec4 worldSpaceVert =TDDeform(PhongGeometry(P.x, P.y, P.z));

vec4 camSpaceVert = uTDMat.cam * worldSpaceVert;

gl_Position = TDCamToProj(camSpaceVert);

```
// This is here to ensure we only execute lighting etc. code
// when we need it. If picking is active we don't need this, so
// this entire block of code will be ommited from the compile.
// The TD_PICKING_ACTIVE define will be set automatically when
// picking is active.
```

#ifndef TD_PICKING_ACTIVE

```
vec3 camSpaceNorm = uTDMat.camForNormals * TDDeformNorm(N).xyz;
vVert.norm.stp = camSpaceNorm.stp;
vVert.camSpaceVert.xyz = camSpaceVert.xyz;
vVert.color = TDInstanceColor(Cd);
vec3 camVec = -camSpaceVert.xyz;
vVert.camVector.stp = camVec.stp;
```

#else // TD_PICKING_ACTIVE

```
// This will automatically write out the nessesarily values
// for this shader to work with picking.
// See the documentation if you want to write custom values for picking.
TDWritePickingValues();
```

#endif // TD_PICKING_ACTIVE

}

[/code]