GLSL Vertex Shader compiles without display

I am trying to implement phong shading to no avail. I'm building shaders in stages (which might be a problem in itself), but that's where I am now. I expect this to give my result (Utah teapot) as a white image on my black background. It does it correctly when I remove line 18 from the vertex shader, but when the line is not there and there is no error. Any ideas?

Fragment shader:

  1 #version 120                                                                                      
  2                                                                                                   
  3 varying vec3 vertexNormal;                                                                        
  4 varying vec3 cameraSpacePos;                                                                      
  5                                                                                                   
  6 vec3 calcSpecular(vec3 l, vec3 n, vec3 v, vec3 cL, vec3 cS, float p)                              
  7 {                                                                                                 
  8     vec3 r =  2.0 * n * (n * l) - l;                                                              
  9     return cL * cS * pow(max(0.0, dot(r, v)), p);                                                 
 10 }                                                                                                 
 11                                                                                                   
 12 vec3 calcDiffuse(vec3 l, vec3 n, vec3 cL, vec3 cD)                                                
 13 {                                                                                                 
 14     return cL * cD * max(0.0, dot(n, l));                                                         
 15 }                                                                                                 
 16                                                                                                   
 17 void main(void)                                                                                   
 18 {                                                                                                 
 19     vec3 lightDirection = normalize(vec3(-1.0, 0.0, -1.0));                                       
 20     vec3 objectColor    = vec3(0.5, 0.0, 0.0);                                                    
 21     vec3 ambientColor   = vec3(0.1, 0.1, 0.1);                                                    
 22     vec3 lightColor     = vec3(0.3, 0.0, 0.3);                                                    
 23     vec3 specularColor  = vec3(1.0, 1.0, 1.0);                                                    
 24                                                                                                   
 25     gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);                                                      
 26 }                   

      

Vertex Shader:

  1 #version 120
  2 
  3 uniform mat4 modelView;
  4 uniform mat4 normalModelView;
  5 uniform mat4 projection;
  6 
  7 attribute vec3 position;
  8 attribute vec3 normal;
  9 
 10 varying vec3 vertexNormal;
 11 varying vec3 cameraSpacePos;
 12 
 13 void main(void)
 14 {
 15     vec4 tmpCameraSpacePos = modelView * vec4(position, 1.0);
 16     gl_Position    = projection * tmpCameraSpacePos;
 17 
 18     vertexNormal = (normalModelView * vec4(normal, 0.0)).xyz;
 19     cameraSpacePos  = (tmpCameraSpacePos).xyz;
 20 
 21 }
 22

      

It's also worth mentioning that normal and normalModelViews are currently not set. I've tried with these the previously included similar results. I understand that they should be set to their default values.

+3


source to share


1 answer


Since you mentioned that normal

and normalModelView

not installed, I guess they are not set to their default values, and are technically incorrect data. When invalid data is used in a shader, the results are undefined, which is part of the reason shaders are difficult to debug. Make sure you provide the values ​​for these variables before attempting to reference them in your code.



+1


source







All Articles