I've succeeded in getting tesselation control and evaluation shaders to work correctly, but the lighting for my scene is still blocky because I've been calculating per (triangle) face normals, instead of per vertex. Now that I'm using tesselation, it seems necessary to calculate new normal vertors for the newly tesselated vertexes in either the tess eval or geometry shader.
In order to get smooth per vertex normals I need to calculate per face normals for every triangle sharing the vertex in question, then compute a weighted average of these face normals. But I'm not sure how to get all those triangle faces in the tess eval or geometry shader. The layout (triangles_adjacency) in
option for the geometry shader looks promising, but theres not much information about how to use it.
Is it possible to compute smooth per vertex normals on the GPU without the use of a normal/bump map in this way? The end goal is to have smooth per vertex lighting that benefits from the increased level of tesselation detail.
Here are my tessellation control and evaluation shaders:
// control
#version 400
layout (vertices = 3) out;
in vec3 vPosition[];
out vec3 tcPosition[];
const float tessLevelInner = 1.0;
const float tessLevelOuter = 1.0;
void main()
{
tcPosition[gl_InvocationID] = vPosition[gl_InvocationID];
if (gl_InvocationID == 0) {
gl_TessLevelInner[0] = tessLevelInner;
gl_TessLevelOuter[0] = tessLevelOuter;
gl_TessLevelOuter[1] = tessLevelOuter;
gl_TessLevelOuter[2] = tessLevelOuter;
}
}
// eval
#version 400
layout (triangles, equal_spacing, cw) in;
uniform mat4 uProj;
uniform mat4 uModelView;
in vec3 tcPosition[];
out vec3 tePosition;
void main()
{
vec3 p0 = gl_TessCoord.x * tcPosition[0];
vec3 p1 = gl_TessCoord.y * tcPosition[1];
vec3 p2 = gl_TessCoord.z * tcPosition[2];
tePosition = p0 + p1 + p2;
gl_Position = uProj * uModelView * vec4(tePosition, 1);
}