2013-10-15 5 views
2

Quelqu'un peut m'aider s'il vous plaît à revoir mon programme glsl shader. Le problème que je suis est que, malgré l'emplacement de la lumière, les bords lointains de ma maille est noir quand il ne devrait pas être.GLSL Shader L'éclairage ne fonctionne pas

Pour illustrer, je fournir cette image (Notez le rouge, vert, bleu avec un point blanc est la position de la lumière): 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;       
}        
+2

vous n'ajoutez pas d'ambiant partout, ce qui est nécessaire car votre couleur diffuse provoque la couleur noire –

+0

Dois-je ajouter de l'ambiance en ajoutant simplement: finalOutput = texColor * vec4 (mDiffuse, 1.0) + vec4 (0.3,0.3,0.3, 1,0); – ukaku

+0

Si la sphère blanche est une lumière et qu'elle se trouve derrière l'objet, ma première question est la suivante: pourquoi la surface exposée à l'utilisateur est-elle éclairée? Quelque chose est terriblement faux =) – Drop

Répondre

2

J'ai trouvé la solution à mon problème: dans le vertex shader je transformais la position du vertex et normal en utilisant mes caméras vi Nouvelle matrice. Je l'ai enlevé et maintenant tout fonctionne bien.

enter image description here

Vertex Shader:

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

Merci à tous pour votre aide.