问题已解决,请参阅下面的更正
我试图实施TDM的“海景”(见https://www.shadertoy.com/view/Ms2SD1)
具有矩形补丁,细分和自定义细节级别。不幸的是我无法获得gl_TessLevelOuter
权利。补丁之间有一些小洞。
我把天空盒变成红色以强调错误。
我使用glDrawArraysInstanced
绘制64²10* 10个色块,并在顶点着色器中的相机位置重新排列它们:
#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);
}
然后我计算了tesselation控件着色器中的细节级别:
#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];
}
}
我认为错误在TCS着色器的某处,但我找不到合适的方法来计算细分水平。
校正
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);
}
}