PassThrough Geometry Shader Not Working

眉间皱痕 提交于 2019-12-23 05:45:06

问题


I am using FragmentShader and VertexShader at present, and works absolutely fine. I cannot get my geometry shader working. I am absolutely new to it, below is what I have tried.

I am using VBO, lighting and textures along with some geometry, but it works fine before using GeometryShader. the only thing I have changed is the variable names as I had to get the input in the geometry shader and give the output. So I have appended 1 at the end of those variable names those which will go out from geometry shader to the fragment shader.

Also I have added headers starting with # which were earlier not there. I am using GL_TRIANGLES to draw.

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

This is my 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); 
 } 

This is my 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();
}

I just want that what ever was there earlier is passed from vertex shader to fragment shader via geometry shader, so that I can manipulate the shader later. Currently the screen is just black


回答1:


The core of your problem is that you didn't bother to check for compilation errors when you built your Geometry Shader. I know that because I see several syntax errors for it. In particular:

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.

Notice how you loop over the number of vertices in a primitive and use gl_in[i] to get the input value for each vertex in the primitive. That's how you need to access all of your Geometry Shader inputs. And you need to write each one to its corresponding output variable, then call EmitVertex. All in that loop.



来源:https://stackoverflow.com/questions/34112683/passthrough-geometry-shader-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!