Problem solved, see below for the correction
Im trying to implement TDM's "Seascape" (see here https://www.shadertoy.com/view/Ms2SD1)
with rectangle patches ,tesselation and custom level of detail. Unfortunately i cant get the gl_TessLevelOuter
right. There are some small holes between the patches.
I made the skybox red to emphasize the error.
I use glDrawArraysInstanced
to draw 64² 10*10 patches and rearrange them around the camera position in the vertex shader:
#version 330 core
in vec3 in_Position;
out vec3 ex_Position;
layout (std140) uniform globalUniforms{
layout(row_major) mat4 mvp;
layout(row_major) mat4 vp;
vec3 cameraPos;
float time;
vec3 sun;
};
const float patchSize=10;
const int patchCount=64;
void main(){
ex_Position=in_Position;
ex_Position.xz+=patchSize*vec2(gl_InstanceID%patchCount-patchCount/2,int(gl_InstanceID/patchCount)-patchCount/2);
ex_Position.xz+=patchSize*floor(cameraPos.xz/patchSize);
}
then I calculate the level of detail in the tesselation control shader:
#version 410 core
layout(vertices = 4) out;
in vec3 ex_Position[];
out vec3 tc_Position[];
layout (std140) uniform globalUniforms{
layout(row_major) mat4 mvp;
layout(row_major) mat4 vp;
vec3 cameraPos;
float time;
vec3 sun;
};
const int maxLevel=64;
const float maxDistance=100;
int getTessLevel(vec3 p){
float l=distance(p,cameraPos);
return int((maxLevel-1)*(1.0-clamp(l,0,maxDistance)/maxDistance))+1;
}
void main()
{
tc_Position[gl_InvocationID] = ex_Position[gl_InvocationID];
if (gl_InvocationID == 0) {
vec3 d0=ex_Position[0]+(ex_Position[1]-ex_Position[0])/2;
vec3 d1=ex_Position[1]+(ex_Position[2]-ex_Position[1])/2;
vec3 d2=ex_Position[2]+(ex_Position[3]-ex_Position[2])/2;
vec3 d3=ex_Position[3]+(ex_Position[0]-ex_Position[3])/2;
gl_TessLevelOuter[0] = getTessLevel(d0);
gl_TessLevelOuter[1] = getTessLevel(d1);
gl_TessLevelOuter[2] = getTessLevel(d2);
gl_TessLevelOuter[3] = getTessLevel(d3);
gl_TessLevelInner[0] = gl_TessLevelOuter[0];
gl_TessLevelInner[1] = gl_TessLevelOuter[1];
}
}
I think the error is somewhere in the TCS shader but I cant find a right way to calculate the tesselation levels.
Correction
void main()
{
tc_Position[gl_InvocationID] = ex_Position[gl_InvocationID];
if (gl_InvocationID == 0) {
vec3 d0=ex_Position[2]+(ex_Position[2]-ex_Position[1])/2;
vec3 d1=ex_Position[0]+(ex_Position[1]-ex_Position[0])/2;
vec3 d2=ex_Position[3]+(ex_Position[3]-ex_Position[0])/2;
vec3 d3=ex_Position[2]+(ex_Position[3]-ex_Position[2])/2;
gl_TessLevelOuter[0] = getTessLevel(d0);
gl_TessLevelOuter[1] = getTessLevel(d1);
gl_TessLevelOuter[2] = getTessLevel(d2);
gl_TessLevelOuter[3] = getTessLevel(d3);
gl_TessLevelInner[0] = mix(gl_TessLevelOuter[1],gl_TessLevelOuter[2],0.5);
gl_TessLevelInner[1] = mix(gl_TessLevelOuter[0],gl_TessLevelOuter[3],0.5);
}
}