通过几何着色器的光线变化

时间:2015-12-06 15:44:59

标签: opengl glsl geometry-shader

enter image description here

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

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

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

VertexShader

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

这是我的FragmentShader

#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 (triangle_strip, max_vertices = 3) out;

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

in vec3 N[3];
in vec3 L[3];
in vec3 R[3];
in vec3 V[3];

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 vec2 FtexCoord1; 

in vec2 FtexCoord[3]; 

void main(void)
{
    int i;



    for (i = 0; i < gl_in.length(); i++)
    {
        N1=N[i];
        L1=L[i];
        R1=R[i];
        V1=R[i];

        FtexCoord1=FtexCoord[i]; 

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

我只是想通过几何着色器将前面的内容从顶点着色器传递到片段着色器,以便稍后我可以操作着色器。但是光线并没有表现出同样的效果。如图所示。

1 个答案:

答案 0 :(得分:0)

代码中有一个小错误。它应该是V1=V[i];而不是V1=R[i];