0 Replies - 11602 Views - Last Post: 12 May 2012 - 06:24 PM

#1 jmd_  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 8
  • Joined: 12-May 12

Per Fragment Lighting Shader (GLSL 420)

Posted 12 May 2012 - 06:24 PM

Description: The vertex shader receives a vertex and a normal vector as an attribute. Pass the modelview matrix, the projection matrix, the normal matrix, and the light position vector from the application to the vertex shader as uniform variables. The fragment shader receives two linearly interpolated vectors from the vertex shader: the normal and the light direction vectors. Pass the diffuse color, the ambient color, and the exponent for the specular light from the application as uniform variables. Computes OpenGL 4.2 3D lighting in the fragment shader using the Phong lighting model.
 
//////////// VERTEX SHADER /////////////////
                                             
#version 420 core                                                                  
                                                                              
layout (location = 0) in vec3 position;                                                                                 
layout (location = 1) in vec3 normal;                                         
uniform mat4 projection;													  
uniform mat4 modelview;														  
uniform mat3 normalMatrix;
uniform vec3 lightPosition;                                                                                                      

out vec3 vNormal;                                                             
out vec3 vLightDir; 
                                                                              
void main()                                                                   
{   
// transform geometry                                                                         
    gl_Position = projection * modelview * vec4(position, 1.0);    

// transform normal                                           
    vNormal = normalMatrix * normal;

// get position in eye coordinates                               
    vec3 vPosition = vec3(modelview * vec4(position,1.0));

// calculate light direction vector                               
    vLightDir = normalize(lightPosition - vPosition); 
}

/////////// FRAGMENT SHADER //////////////////

#version 420 core                                                                
                       
in vec3 vNormal; 													          
in vec3 vLightDir;  

uniform vec3 diffColor;
uniform vec3 ambColor;
uniform float shiny;
                                                                                
out vec4 FragColor;                                                           
                                                                         
void main()                                                                   
{     
// normalize normal and light direction vector                                                                        	                           
	vec3 fragNormal = normalize(vNormal);
        vec3 fragLightDir = normalize(vLightDir);	
 
// compute diffuse light                                   
	float diff = max(0.0, dot (fragLightDir,fragNormal));
	FragColor = vec4(ambColor, 1.0) + vec4(diffColor, 1.0)*diff;

// compute specular light only when required, i.e. scene is not black
        if(diff != 0) 
        {
	    vec3 vReflection = normalize(reflect(-fragLightDir, fragNormal));
            float spec = max(0.0, dot(fragNormal,vReflection));
            float fSpec = pow(spec, shiny);
            FragColor.rgb += vec3(fSpec, fSpec, fSpec);	
	} 
}


Is This A Good Question/Topic? 0
  • +

Page 1 of 1