opengl 尝试将数据传递到几何体着色器时出现GLSL错误

vhmi4jdf  于 2022-11-23  发布在  其他
关注(0)|答案(1)|浏览(151)

我有一个顶点着色器,它可以接受位置、纹理坐标、法线和一些均匀性:

#version 330 core

layout(location = 0) in vec4 position;
layout(location = 1) in vec2 texCoord;
layout(location = 2) in vec3 normal;

//MVP
uniform mat4 u_Model;
uniform mat4 u_View;
uniform mat4 u_Proj;

//Lighting
uniform mat4 u_InvTranspModel;
uniform mat4 u_LightMVP;

out DATA
{
    vec2 v_TexCoord;
    vec3 v_Normal;
    vec3 v_FragPos;
    vec4 v_LightSpacePos;
    mat4 v_Proj;
} data_out[];

void main()
{   
    gl_Position = u_Model * position;
    data_out.v_TexCoord = texCoord;
    data_out.v_Normal = mat3(u_InvTranspModel) * normal;
    data_out.v_Proj = u_Proj * u_View * u_Model;

    //Light
    data_out.v_FragPos = vec3(u_Model*position);
    data_out.v_LightSpacePos = u_LightMVP * position;
}

我正在传递到我的几何着色器中:

#version 330 core

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

in DATA
{
    vec2 v_TexCoord;
    vec3 v_Normal;
    vec3 v_FragPos;
    vec4 v_LightSpacePos;
    mat4 v_Proj;
} data_in[];

out vec2 g_TexCoord;
out vec3 g_Normal;
out vec3 g_FragPos;
out vec4 g_LightSpacePos;

void main()
{ 
    gl_Position = data_in[0].v_Proj * gl_in[0].gl_Position;
    g_Normal = data_in[0].v_Normal;
    g_FragPos = data_in[0].v_FragPos;
    g_TexCoord = data_in[0].v_TexCoord;
    g_LightSpacePos = data_in[0].v_LightSpacePos;
    EmitVertex();

    gl_Position = data_in[1].v_Proj * gl_in[1].gl_Position;
    g_Normal = data_in[1].v_Normal;
    g_FragPos = data_in[1].v_FragPos;
    g_TexCoord = data_in[1].v_TexCoord;
    g_LightSpacePos = data_in[1].v_LightSpacePos;
    EmitVertex();

    gl_Position = data_in[2].v_Proj * gl_in[2].gl_Position;
    g_Normal = data_in[2].v_Normal;
    g_FragPos = data_in[2].v_FragPos;
    g_TexCoord = data_in[2].v_TexCoord;
    g_LightSpacePos = data_in[2].v_LightSpacePos;
    EmitVertex();

    EndPrimitive();
}

然而我得到的错误:

ERROR: 0:28: '.' : dot operator to an array only takes length()
ERROR: 0:28: 'assign' :  cannot convert from 'attribute 2-component vector of highp        float' to 'varying unknown-sized array of highp block'
ERROR: 0:29: '.' : dot operator to an array only takes length()
ERROR: 0:29: 'assign' :  cannot convert from '3-component vector of highp float' to 'varying unknown-sized array of highp block'
................ Etc

我得到这些错误的每个变量在顶点着色器传递到几何着色器。
这个着色器工作时,它的形式没有几何着色器,并应设置目前什么都不做,所以我做错了什么?

7nbnzgx9

7nbnzgx91#

顶点着色器的输出不是数组,即使下一个阶段是Geometry shaderVertex shader处理单个顶点,因此输出总是与该顶点相关的单个值。几何着色器的输入接口是数组,因为几何着色器将图元(多个顶点)作为输入,而不是单个顶点。删除顶点着色器中的[]

out DATA
{
    vec2 v_TexCoord;
    vec3 v_Normal;
    vec3 v_FragPos;
    vec4 v_LightSpacePos;
    mat4 v_Proj;
} data_out;           // <--- remove []

相关问题