GLSL Shader Lighting无法正常工作

时间:2013-10-15 17:24:03

标签: opengl glsl lighting

有人可以帮我查看我的glsl着色器程序。我遇到的问题是,尽管光的位置,我的网格的远边是不应该的黑色。

为了帮助说明,我提供了这张照片(注意红色,绿色,蓝色和白点是光的位置): why is the edges black

VertexShader:

#version 330                                                                                                                                                                              

struct Matrix {                                                                                                                                                                              
    mat4 mvp;                                                                                                                                                                              
    mat4 mv;                                                                                                                                                                              
    mat4 view;                                                                                                                                                                              
    mat4 projection;                                                                                                                                                                              
};                                                                                                                                              
struct Light {                                                                                                                                                                              
    vec3 position;          //Supplied in eye space(camera view )                                                                                                                                                                    
    vec3 color;                                                                                                                                                                              
    vec3 direction;                                                                                                                                                                              
    float intensity;                                                                                                                                                                              
    vec3 ambient;                                                                                                                                                                              
};                                                                                                                                         


attribute vec3 inputPosition;
attribute vec3 inputNormal;
attribute vec2 inputTexture;
attribute ivec2 number_influence;
attribute ivec4 boneIDs;
attribute vec4 boneWeights;

//--------------------------------------------                                                                                                                                                                              
// UNIFORM:INPUT Supplied Data from C++ application                                                                                                                                                                              
//--------------------------------------------                                                                                                                                                                              
uniform         Matrix  matrix;                                                                                                                                                                              
uniform         Light   light;                                                                                                                                                                              
uniform         vec3    cameraPosition;        //position of viewer                                                                                                                                 
// Bone deformation matrices                                                                                                                                                                                                                                          
uniform mat4 boneFinalMatrix    [30];                                                                  
uniform mat4 boneInvBindPosition[30];                                   
uniform mat4 boneTranslation    [30];                                   

//Output Variables                                                                       
out vec3        oFragNormal;                                                                                                                                                                                
out vec2        oTexCoord;                                                                                                                                                                              
out vec3 oFragPosition;                                                                                                                                                                                
void main() {                                                                                                                                                                                 

    mat3 Normal_Matrix =  mat3(    transpose(inverse(matrix.view))  );                                                                                                                                                                           

    //PLACE HOLDER                                                                                                                                                                        
    vec4 INPUT= vec4(inputPosition , 1.0);                                                                                                                                 
    vec4 vertex= vec4(0.0,0.0,0.0,0.0);                                                                                                                                                                 
    vec3 normal= vec3(0.0,0.0,0.0);                                                                                                                                                                          
    int id = 0;                                                                                                                                                       

    mat4 im;                                                                       
    vec4 O;                                                                 
    float weight;                                                 

    mat4 locTrans;                                   
    mat4 invMatrix;                                   
    mat4 transform;                                   

    mat4 iBindMatrix;                                                                                     
        mat4 FinalTranform;            
        mat4 Ttrans;                            
    for( int i = 0;  i < number_influence.x; i++ ){                                                                                                                                                                       
        id = boneIDs[i];                                                                                                                                                                     

        weight = boneWeights[id];                                                                                                                
        if(weight <= 0.0) weight = 1.0;                                                

        invMatrix = boneInvBindPosition[id];                                   
        locTrans = boneTranslation[id];   

        transform = boneFinalMatrix[id] ;                                                               


        FinalTranform +=  transform * weight;                        
        iBindMatrix+= (invMatrix)*weight;                    
        Ttrans += locTrans * weight;                


    }

    vertex =    (  FinalTranform  )*  (   iBindMatrix * (INPUT ))  ;                                                                                                    
    normal =   mat3(FinalTranform)  * mat3(iBindMatrix) * inputNormal ; 

    // output the transformed vertex                                                                                                                                                                              
    gl_Position = matrix.projection * matrix.view * vertex;                                                                                                                                                                              
    oFragPosition = vec3(matrix.view * vertex);                                                                                                                                    

    oFragNormal     = Normal_Matrix * normal;                                                                                                                                                                                                         

    //store the texture data                                                                                                                                                                              
    oTexCoord = inputTexture.xy;                                                                                                                                                                                               
}                                                                                                                                                                              

FragmentShader

#version 330                               



const vec4 AMBIENT = vec4(0.452, 0.452, 0.479, 1.0);    //0.2 for all component is a good dark value                               

struct Light {                               
    vec3 position;                               
    vec3 diffuse;                               
    vec3 direction;                               
    float specularExponent;                               
    vec3 ambient; 
    vec3 specular;                              
};                               

//the image                               
uniform sampler2D textureSampler;                               
uniform sampler2D normalSampler;             
uniform vec3    cameraPosition;

uniform vec3 materialDiffuse;
uniform vec3 materialSpecular;



uniform Light light[10];            
uniform int numberLights;

out vec4 finalOutput;                               


in vec2 oTexCoord;                                            
in vec3 oFragNormal;     
in vec3 oFragPosition;                               

void main() {                 
    int i=0;
    vec3 N              = normalize( oFragNormal );
    //V = frag to viewier vector
    vec3 V = normalize( cameraPosition - oFragPosition    );


    vec4 texColor = texture2D(textureSampler, oTexCoord);     



    vec3 mDiffuse = vec3(0.0);
    vec3 mSpecular = vec3(0.0);
    float N_dot_L = 0.0;
    float N_dot_H = 0.0;
    vec3 L;
    vec3 H;
    for( i= 0; i < numberLights; i++){
        L               = normalize( oFragPosition - light[i].position     );

        //Diffuse: CmaterialDiffuseColor = max( Normal * LightDir, 0) * CmaterialColor * ClightColor
        N_dot_L = max ( dot ( N, L),  0.0  ) ;
        mDiffuse += materialDiffuse * light[i].diffuse * N_dot_L ;

        //Specular: CmaterialSpecularColor = max(Normal * HalfAngleLightandViewVector, 0) ^ exp * CmaterialColor * ClightColor

        H               = normalize( (L + V)/2.0 );
        N_dot_H = max( dot ( N , H ),  0.0 ) ;
        mSpecular += materialSpecular * light[i].specular * vec3(pow(N_dot_H, light[i].specularExponent) );
    }                     


    finalOutput =  texColor * vec4(mDiffuse,1.0 )   ;    
    finalOutput.a=1.0;                           
}                               

1 个答案:

答案 0 :(得分:2)

我找到了解决问题的方法:在顶点着色器中,我使用相机视图矩阵转换顶点位置和法线。我删除它,现在一切正常。

enter image description here

顶点着色器:

#version 330                                                                                                                                                                              

struct Matrix {                                                                                                                                                                              
    mat4 mvp;                                                                                                                                                                              
    mat4 mv;                                                                                                                                                                              
    mat4 view;                                                                                                                                                                              
    mat4 projection;                                                                                                                                                                              
};                                                                                                                                              
struct Light {                                                                                                                                                                              
    vec3 position;          //Supplied in eye space(camera view )                                                                                                                                                                    
    vec3 color;                                                                                                                                                                              
    vec3 direction;                                                                                                                                                                              
    float intensity;                                                                                                                                                                              
    vec3 ambient;                                                                                                                                                                              
};                                                                                                                                         


attribute vec3 inputPosition;
attribute vec3 inputNormal;
attribute vec2 inputTexture;
attribute ivec2 number_influence;
attribute ivec4 boneIDs;
attribute vec4 boneWeights;

//--------------------------------------------                                                                                                                                                                              
// UNIFORM:INPUT Supplied Data from C++ application                                                                                                                                                                              
//--------------------------------------------                                                                                                                                                                              
uniform         Matrix  matrix;                                                                                                                                                                              
uniform         Light   light;                                                                                                                                                                              
uniform         vec3    cameraPosition;        //position of viewer                                                                                                                                 
// Bone deformation matrices                                                                                                                                                                                                                                          
uniform mat4 boneFinalMatrix    [30];                                                                  
uniform mat4 boneInvBindPosition[30];                                   
uniform mat4 boneTranslation    [30];                                   

//Output Variables                                                                       
out vec3        oFragNormal;                                                                                                                                                                                
out vec2        oTexCoord;                                                                                                                                                                              
out vec3 oFragPosition;                                                                                                                                                                                
void main() {                                                                                                                                                                                 



    //PLACE HOLDER                                                                                                                                
    vec4 vertex= vec4(0.0,0.0,0.0,0.0);                                                                                                                                                                 
    vec3 normal= vec3(0.0,0.0,0.0);                                                                                                                                                                          
    int id = 0;                                                                                                                                                       

    mat4 im;                                                                       
    vec4 O;                                                                 
    float weight;                                                 

    mat4 locTrans;                                   
    mat4 invMatrix;                                   
    mat4 transform;                                   

    mat4 iBindMatrix;                                                                                     
        mat4 FinalTranform;            
        mat4 Ttrans;                            
    for( int i = 0;  i < number_influence.x; i++ ){                                                                                                                                                                       
        id = boneIDs[i];                                                                                                                                                                     

        weight = boneWeights[id];                                                                                                                
        if(weight <= 0.0) weight = 1.0;                                                

        invMatrix = boneInvBindPosition[id];                                   
        locTrans = boneTranslation[id];   

        transform = boneFinalMatrix[id] ;                                                               


        FinalTranform +=  transform * weight;                        
        iBindMatrix+= (invMatrix)*weight;                    
        Ttrans += locTrans * weight;                


    }

    mat4 BoneFinal = (  FinalTranform  )*  (   iBindMatrix );
    vertex =    BoneFinal * vec4(inputPosition , 1.0)   ;      

    mat3 transInvView = mat3(    transpose(inverse(matrix.view ))  );
    mat3 transInvBone = mat3( transpose ( inverse (BoneFinal)) );
    normal =  vec3( normalize (   BoneFinal * vec4(inputNormal,0.0)    ) ); 


    mat3 Normal_Matrix =  mat3(    transpose(inverse(matrix.view ))  );                                                                                                   
    // output the transformed vertex                                                                                                                                                                              
    gl_Position = matrix.projection * matrix.view * vertex;        

    oFragPosition = vec3( vertex);                                                                                                                                    

    oFragNormal     =  normal;                                                                                                                                                                                                         

    //store the texture data                                                                                                                                                                              
    oTexCoord = inputTexture.xy;                                                                                                                                                                                               
}                                                                                                                                                                              

Fragment Shader:

#version 330                               



const vec4 AMBIENT = vec4(0.452, 0.452, 0.479, 1.0);    //0.2 for all component is a good dark value                               

struct Light {                               
    vec3 position;                               
    vec3 diffuse;                               
    vec3 direction;                               
    float specularExponent;                               
    vec3 ambient; 
    vec3 specular;                              
};                               

//the image                               
uniform sampler2D textureSampler;                               
uniform sampler2D normalSampler;             
uniform vec3    cameraPosition;

uniform vec3 materialDiffuse;
uniform vec3 materialSpecular;
uniform vec3 materialAmbient;


uniform Light light[10];            
uniform int numberLights;

out vec4 finalOutput;                               


in vec2 oTexCoord;                                            
in vec3 oFragNormal;     
in vec3 oFragPosition;                               

void main() {                 
    int i=0;
    vec3 N              = normalize( oFragNormal );
    //V = frag to viewier vector
    vec3 V = normalize( vec3(0.0) - oFragPosition    );


    vec4 texColor = texture2D(textureSampler, oTexCoord);     


    vec3 mAmbient = vec3(0.0);
    vec3 mDiffuse = vec3(0.0);
    vec3 mSpecular = vec3(0.0);
    float N_dot_L = 0.0;
    float N_dot_H = 0.0;
    vec3 L;
    vec3 H;
    for( i= 0; i < numberLights; i++){
        L               = normalize( oFragPosition - light[i].position     );

        mAmbient = materialAmbient * light[i].ambient;
        //Diffuse: CmaterialDiffuseColor = max( Normal * LightDir, 0) * CmaterialColor * ClightColor
        N_dot_L = max ( dot ( N, L),  0.0  ) ;
        mDiffuse += materialDiffuse * light[i].diffuse * N_dot_L ;

        //Specular: CmaterialSpecularColor = max(Normal * HalfAngleLightandViewVector, 0) ^ exp * CmaterialColor * ClightColor

        H               = normalize( (L + V));
        N_dot_H = max( dot ( N , H ),  0.0 ) ;
        mSpecular += materialSpecular * light[i].specular * vec3(pow(N_dot_H, light[i].specularExponent) );
    }                     


    finalOutput =  texColor * (vec4(mDiffuse,1.0 )  +  vec4(mAmbient, 1.0)  );    
    finalOutput.a=1.0;                           
}                               

谢谢大家的帮助。