c++ 着色器出现问题“着色器使用变化的---但上一个着色器未对其进行写入”

ac1kyiln  于 2022-12-01  发布在  其他
关注(0)|答案(1)|浏览(183)

有人知道为什么我总是收到这样的错误消息吗:◇着色器使用变化的_I; DATA;g_mapCoord,但是之前的着色器没有写入到它。DATA;worldPosition,但上一个着色器未写入该位置。
看看我的着色器。
顶点

#version 430

layout (location = 0) in vec2 position0;

out DATA {
    vec2 v_mapCoord;
    vec3 worldPosition;
} Out;

uniform vec3 u_cameraPosition;
uniform mat4 u_localMatrix;
uniform mat4 u_worldMatrix;
uniform float u_scaleY;
uniform int u_lod;
uniform vec2 u_index;
uniform float u_gap;
uniform vec2 u_location;
uniform sampler2D s_heightmap;
uniform int u_lodMorphArea[8];

float morphLatitude(vec2 position)
{
    //not important code
    return 0;
}

float morphLongitude(vec2 position)
{
    //not important code
    return 0;
}

vec2 morph(vec2 localPosition, int morph_area){
    //not important code
return vec2(0);
}

void main()
{
    vec2 localPosition = (u_localMatrix * vec4(position0.x,0,position0.y,1)).xz;
    
    if (u_lod > 0) { 
        localPosition += morph(localPosition, u_lodMorphArea[u_lod-1]); // Translate position by morphing vector 
    }
            
    float height = texture(s_heightmap, localPosition).r;
    Out.v_mapCoord = localPosition;
    vec4 _worldPosition = u_worldMatrix * vec4(localPosition.x, height, localPosition.y,1);
    Out.worldPosition = _worldPosition.xyz;

    gl_Position = u_worldMatrix * vec4(localPosition.x, height, localPosition.y,1);
}

片段

#version 430

layout (location = 0) out vec4 outputColor;

in DATA {
    vec2 g_mapCoord;
    vec3 worldPosition;
} In;

const vec3 lightDirection = vec3(-0.2, -1.0, -0.2);
const float intensity = 1.2;

uniform sampler2D s_textureNormal;
uniform sampler2D s_textureWater;
uniform sampler2D s_textureLand;

float diffuse(vec3 direction, vec3 normal, float intensity) 
{
    return max(0.01, dot(normal, -direction) * intensity);
}

void main() 
{
    vec3 normal = texture(s_textureNormal, In.g_mapCoord).rgb;
    float diff = diffuse(lightDirection, normal, intensity);
    
    outputColor = vec4(1,0,0,1);
}

几何

#version 430

layout(triangles) in;
layout(triangle_strip, max_vertices = 3) out;

in vec2 te_mapCoord[];
out vec2 g_mapCoord;

uniform mat4 u_viewProjection;

void main() {   

    for (int i = 0; i < gl_in.length(); ++i)
    {
        vec4 position = gl_in[i].gl_Position;
        gl_Position = u_viewProjection * position;
        g_mapCoord = te_mapCoord[i];
        EmitVertex();
    }
    
    EndPrimitive();
}

牵引控制系统

#version 430

layout(vertices = 16) out;

in DATA {
    vec2 v_mapCoord;
    vec3 worldPosition;
} In[];

out vec2 tc_mapCoord[];

const int AB = 2;
const int BC = 3;
const int CD = 0;
const int DA = 1;

uniform int u_tessellationFactor;
uniform float u_tessellationSlope;
uniform float u_tessellationShift;
uniform vec3 u_cameraPosition;

// Calculate tessellation levels
float lodFactor(float dist) 
{
    float tessellationLevel = max(0.0, u_tessellationFactor/pow(dist, u_tessellationSlope) + u_tessellationShift);
    return tessellationLevel;
}

void main()
{

    if (gl_InvocationID == 0){
    
        // Calculate mid points of the edges of the quad
        vec3 abMid = vec3(gl_in[0].gl_Position + gl_in[3].gl_Position)/2.0; //Bottom left, Bottom right
        vec3 bcMid = vec3(gl_in[3].gl_Position + gl_in[15].gl_Position)/2.0; //Bottom right Top right
        vec3 cdMid = vec3(gl_in[15].gl_Position + gl_in[12].gl_Position)/2.0; //Top right, Top left
        vec3 daMid = vec3(gl_in[12].gl_Position + gl_in[0].gl_Position)/2.0; //Top left, Bottom left
        
        // Calculate distance between camera and mid points of the edges of the quad
        float distanceAB = distance(abMid, u_cameraPosition);
        float distanceBC = distance(bcMid, u_cameraPosition);
        float distanceCD = distance(cdMid, u_cameraPosition);
        float distanceDA = distance(daMid, u_cameraPosition);
        
        // Tesselation levels used by tessellation primitive generator (define how much tessellation to apply to the patch). Value between 1 and gl_MaxTessGenLevel, depending on lodFactor.
        gl_TessLevelOuter[AB] = mix(1, gl_MaxTessGenLevel, lodFactor(distanceAB));
        gl_TessLevelOuter[BC] = mix(1, gl_MaxTessGenLevel, lodFactor(distanceBC));
        gl_TessLevelOuter[CD] = mix(1, gl_MaxTessGenLevel, lodFactor(distanceCD));
        gl_TessLevelOuter[DA] = mix(1, gl_MaxTessGenLevel, lodFactor(distanceDA));
        
        gl_TessLevelInner[0] = (gl_TessLevelOuter[BC] + gl_TessLevelOuter[DA])/4;
        gl_TessLevelInner[1] = (gl_TessLevelOuter[AB] + gl_TessLevelOuter[CD])/4;
    }
    
    tc_mapCoord[gl_InvocationID] = In[gl_InvocationID].v_mapCoord; // Just pass to the next stage
    
    gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position;
}

TES系统

#version 430

layout(quads, fractional_odd_spacing, cw) in;

in vec2 tc_mapCoord[];
out vec2 te_mapCoord;

uniform sampler2D s_heightmap;
uniform float u_scaleY;

void main(){

    float u = gl_TessCoord.x;
    float v = gl_TessCoord.y;
    
    // Compute new position for each tessellated vertex within the patch. gl_in with index 12, 0, 3, 15 are corners of the patch.
    vec4 position = ((1 - u) * (1 - v) * gl_in[12].gl_Position + u * (1 - v) * gl_in[0].gl_Position + u * v * gl_in[3].gl_Position +(1 - u) * v * gl_in[15].gl_Position);

    vec2 mapCoord = ((1 - u) * (1 - v) * tc_mapCoord[12] + u * (1 - v) * tc_mapCoord[0] + u * v * tc_mapCoord[3] +(1 - u) * v * tc_mapCoord[15]);
    
    float height = texture(s_heightmap, mapCoord).r;
    height *= u_scaleY;
    
    position.y = height;
    te_mapCoord = mapCoord;
    
    gl_Position = position;
}

有人能帮我找到这里的错误,这就是为什么我得到的错误消息?

z9smfwbn

z9smfwbn1#

引入几何着色器时,需要从几何着色器(而不是顶点着色器)传递片段着色器的变量。
您可以看到几何体着色器是如何执行此操作的:

out vec2 g_mapCoord;

与片段着色器不兼容,预期为:

in DATA {
    vec2 g_mapCoord;
    vec3 worldPosition;
} In;

相关问题和后续答案here

相关问题