Source

gltut / Tut 12 Dynamic Range / data / DiffuseOnly.frag

Full commit
Jason McKesson f781309 







Jason McKesson 53eb28f 
Jason McKesson f781309 
Jason McKesson 53eb28f 





Jason McKesson f781309 
Jason McKesson 53eb28f 

Jason McKesson d2472b1 
Jason McKesson 53eb28f 

Jason McKesson f781309 
Jason McKesson d2472b1 

Jason McKesson 53eb28f 



Jason McKesson d2472b1 
Jason McKesson 53eb28f 
Jason McKesson f781309 

Jason McKesson 53eb28f 


Jason McKesson f781309 




Jason McKesson 5ed67a0 
Jason McKesson f781309 

Jason McKesson d2472b1 
Jason McKesson f781309 
Jason McKesson d2472b1 












Jason McKesson f781309 


Jason McKesson 2aa4061 
Jason McKesson f781309 
Jason McKesson d2472b1 


Jason McKesson f781309 
Jason McKesson d2472b1 









#version 330

in vec4 diffuseColor;
in vec3 vertexNormal;
in vec3 cameraSpacePosition;

out vec4 outputColor;

layout(std140) uniform;

uniform Material
{
	vec4 diffuseColor;
	vec4 specularColor;
	float specularShininess;
} Mtl;

struct PerLight
{
	vec4 cameraSpaceLightPos;
	vec4 lightIntensity;
};

const int numberOfLights = 4;

uniform Light
{
	vec4 ambientIntensity;
	float lightAttenuation;
	PerLight lights[numberOfLights];
} Lgt;


float CalcAttenuation(in vec3 cameraSpacePosition,
	in vec3 cameraSpaceLightPos,
	out vec3 lightDirection)
{
	vec3 lightDifference =  cameraSpaceLightPos - cameraSpacePosition;
	float lightDistanceSqr = dot(lightDifference, lightDifference);
	lightDirection = lightDifference * inversesqrt(lightDistanceSqr);
	
	return (1 / ( 1.0 + Lgt.lightAttenuation * lightDistanceSqr));
}

vec4 ComputeLighting(in PerLight lightData)
{
	vec3 lightDir;
	vec4 lightIntensity;
	if(lightData.cameraSpaceLightPos.w == 0.0)
	{
		lightDir = vec3(lightData.cameraSpaceLightPos);
		lightIntensity = lightData.lightIntensity;
	}
	else
	{
		float atten = CalcAttenuation(cameraSpacePosition,
			lightData.cameraSpaceLightPos.xyz, lightDir);
		lightIntensity = atten * lightData.lightIntensity;
	}
	
	vec3 surfaceNormal = normalize(vertexNormal);
	float cosAngIncidence = dot(surfaceNormal, lightDir);
	cosAngIncidence = cosAngIncidence < 0.0001 ? 0.0 : cosAngIncidence;
	
	vec4 lighting = diffuseColor * lightIntensity * cosAngIncidence;
	
	return lighting;
}

void main()
{
	vec4 accumLighting = diffuseColor * Lgt.ambientIntensity;
	for(int light = 0; light < numberOfLights; light++)
	{
		accumLighting += ComputeLighting(Lgt.lights[light]);
	}
	
	outputColor = accumLighting;
}