PassThrough几何着色器不起作用

时间:2015-12-06 00:43:31

标签: opengl glsl geometry-shader

我目前正在使用FragmentShader和VertexShader,并且工作得非常好。我不能让我的几何着色器工作。我是新手,下面是我尝试过的。

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

此外,我添加了以#开头的标题,这些标题以前不存在。我正在使用GL_TRIANGLES绘制。

VertexShader

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); 
 } 

这是我的GeometryShader

#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();
}

我只是想通过几何着色器将前面的内容从顶点着色器传递到片段着色器,以便稍后我可以操作着色器。目前屏幕只是黑色

1 个答案:

答案 0 :(得分:3)

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

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 总是聚合到数组中。请记住:几何着色器在 primitives 上运行,它们被定义为一个或多个顶点的集合。因此,每个GS调用都会获得一组每顶点输入值,一个用于primitive type defined by your layout in qualifier中的每个顶点。

注意如何循环基元中的顶点数,并使用gl_in[i]获取基元中每个顶点的输入值。这就是您需要访问几何着色器输入的所有的方法。你需要将每一个写入相应的输出变量,然后调用EmitVertex。一切都在那个循环中。