直通几何着色器不工作

PassThrough Geometry Shader Not Working

我目前正在使用 FragmentShader 和 VertexShader,并且工作得很好。我无法让我的几何着色器工作。我对它绝对陌生,下面是我尝试过的。

我正在使用 VBO、光照和纹理以及一些几何体,但在使用 GeometryShader 之前它工作正常。我唯一改变的是变量名,因为我必须在几何着色器中获取输入并给出输出。所以我在那些将从几何着色器传到片段着色器的变量名的末尾附加了 1。

我还添加了以 # 开头的 headers,之前不存在。我正在使用 GL_TRIANGLES 绘图。

顶点着色器

in vec4 position; 
in vec4 color1;
in vec4 normal; 
in vec2 texCoord; 

uniform sampler2D Tex1; 

uniform int use_texture;

out vec4 pcolor;

out vec3 N;
out vec3 L;
out vec3 R;
out vec3 V;


uniform mat4 local2clip;
uniform mat4 local2eye;
uniform mat4 normal_matrix;
uniform mat4 world2eye; 

uniform vec4 light_ambient;
uniform vec4 light_diffuse;
uniform vec4 light_specular;
uniform vec4 light_pos;
#version 330 compatibility

uniform vec4 mat_ambient;
uniform vec4 mat_diffuse;
uniform vec4 mat_specular;
uniform float mat_shine; 

//varying vec3 v_normal;  // vertex normal 
out vec4 v_color;   // vertex color 
out vec4 pos_in_eye;  //vertex position in eye space 
out vec2 FtexCoord; 

void main(){

       gl_Position = local2clip * position;

       N =     normalize(vec3(normal_matrix * normal));    //v_normal
       vec4 Lpos =  world2eye * light_pos;                  //light pos. in eye
       vec4 Vpos =  local2eye * position;                   //pos_in_eye
       L = normalize(vec3(Lpos - Vpos));                    //light_vector

       R = normalize(reflect(-L, N)); 
       V = normalize(vec3(-Vpos));                          //eye vector

       vec3 halfv = normalize(L+V);
       FtexCoord = texCoord; 

       //pcolor = color1;
}

这是我的 FragemntShader

#version 330 compatibility

uniform int use_texture; 

in vec4 pcolor; 

in vec3 N1;
in vec3 L1;
in vec3 R1;
in vec3 V1;


uniform mat4 local2clip;
uniform mat4 local2eye;
uniform mat4 normal_matrix;
uniform mat4 world2eye; 

uniform vec4 light_ambient;
uniform vec4 light_diffuse;
uniform vec4 light_specular;
uniform vec4 light_pos;

uniform vec4 mat_ambient;
uniform vec4 mat_diffuse;
uniform vec4 mat_specular;
uniform float mat_shine;

uniform sampler2D Tex1; 
in vec2 FtexCoord1; 

void main() { 

       vec4 ambient = light_ambient * mat_ambient;
       float NdotL; 
       if (dot(N1,L1) <0.0) NdotL = 0.0; 
       else NdotL = dot(N1, L1); 

       vec4 diffuse = light_diffuse * mat_diffuse * NdotL;

       float RdotV; 
       RdotV = dot(R1, V1); 

       if (NdotL == 0.0) RdotV = 0.0; 
       if (RdotV <0.0) RdotV = 0.0; 

       vec4 specular = light_specular * mat_specular * pow(RdotV,mat_shine);   

       vec4 texcolor;


       if( use_texture == 1 ) {
          texcolor = texture2D(Tex1, FtexCoord1); 
          gl_FragColor = texcolor; 
       }
       else
          gl_FragColor = (diffuse + ambient + specular); 
 } 

这是我的几何着色器

#version 330

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

out vec3 N1;
out vec3 L1;
out vec3 R1;
out vec3 V1;

in vec3 N;
in vec3 L;
in vec3 R;
in vec3 V;

uniform mat4 local2clip;
uniform mat4 local2eye;
uniform mat4 normal_matrix;
uniform mat4 world2eye; 

uniform vec4 light_ambient;
uniform vec4 light_diffuse;
uniform vec4 light_specular;
uniform vec4 light_pos;

uniform vec4 mat_ambient;
uniform vec4 mat_diffuse;
uniform vec4 mat_specular;
uniform float mat_shine; 

//varying vec3 v_normal;  // vertex normal 
out vec4 v_color1;  // vertex color 
out vec4 pos_in_eye1;  //vertex position in eye space 
out vec2 FtexCoord1; 

in vec4 v_color;    // vertex color 
in vec4 pos_in_eye;  //vertex position in eye space 
in vec2 FtexCoord; 

void main(void)
{
    int i;
        N1=N;
        L1=L;
        R1=R;
        V1=R;

        FtexCoord1=FtexCoord;
        v_color1=v_color;
        pos_in_eye1=pos_in_eye;



    for (i = 0; i < gl_in.length(); i++)
    {


        gl_Position = gl_in[i].gl_Position;
        EmitVertex();
    }
    EndPrimitive();
}

我只是希望之前存在的东西通过几何着色器从顶点着色器传递到片段着色器,这样我以后就可以操作着色器了。目前只是黑屏

你的问题的核心是你在构建几何着色器时没有费心去检查编译错误。我知道是因为我看到它有几个语法错误。特别是:

in vec3 N;
in vec3 L;
in vec3 R;
in vec3 V;

in vec4 v_color;    // vertex color 
in vec4 pos_in_eye;  //vertex position in eye space 
in vec2 FtexCoord; 

Geometry Shader inputs are always aggregated into arrays. Remember: a geometry shader operates on primitives, which are defined as a collection of one or more vertices. Each GS invocation therefore gets a set of per-vertex input values, one for each vertex in the primitive type defined by your layout in qualifier.

请注意您是如何遍历图元中的顶点数并使用 gl_in[i] 获取图元中每个顶点的输入值的。这就是您需要访问 all 几何着色器输入的方式。并且你需要将每一个都写到它对应的输出变量中,然后调用EmitVertex。全部在那个循环中。