4

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);
    }
}
tly
  • 1,202
  • 14
  • 17
  • Can you provide the complete code somewhere (just for personal pruposes)? – Vertexwahn Oct 30 '14 at 16:07
  • 1
    do you want all the shaders or all the cpp stuff too? it could be a bit hard to make it work, because i created a small engine with a lot of headers and chaos ;) – tly Oct 31 '14 at 19:20
  • 1
    here you are :) https://www.dropbox.com/s/6so43tbbvdow1uo/tessStage.zip?dl=0 all the visuals are by Alexander Alekseev, you can check out [his original version](https://www.shadertoy.com/view/Ms2SD1) – tly Nov 01 '14 at 11:14
  • @tly: FYI: it's best to post answers as *answers*, not by editing your question. It's OK to answer your own question. – Nicol Bolas Dec 08 '15 at 20:48

0 Answers0