All pastes #344992 Raw Edit

Stuff

public text v1 · immutable
#344992 ·published 2007-02-08 04:30 UTC
rendered paste body
#VERTEX SHADER
varying vec4 diffuse, ambientGlobal, ambient, specular;
varying vec3 normal, lightDir, halfVector;
varying float shininess, attDist;

void main()
{
	vec4 realPos;
	vec3 lightVertVector; 

	//Calculate the "half vector", halfway between the light and eye vector
	halfVector = normalize( gl_LightSource[0].halfVector.xyz );
	//Calculate the vertex' normal
	normal = normalize( gl_NormalMatrix * gl_Normal );
	//Get the position of the vertex
	realPos = gl_ModelViewMatrix * gl_Vertex;
	//Get the vector for the light
	lightVertVector = vec3( gl_LightSource[0].position - realPos );
	//Get the direction for the light
	lightDir = normalize( lightVertVector );
	//The distance used in attenuation
	attDist = length( lightVertVector );

	//Calculate the diffuse
	diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse;

	//Calculate the ambients
	ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient;
	ambientGlobal = gl_FrontMaterial.ambient * gl_LightModel.ambient;

	//Calculate the specular and set shininess
	specular = gl_FrontMaterial.specular * gl_LightSource[0].specular;
	shininess = gl_FrontMaterial.shininess;

	gl_TexCoord[0] = gl_MultiTexCoord0;
	gl_Position = ftransform();
}




#PIXEL SHADER
varying vec4 diffuse, ambient, ambientGlobal, specular;
varying vec3 normal, lightDir, halfVector;
varying float shininess, attDist;

uniform sampler2D texture;

void main()
{
	vec3 normNormal, normHalfVec;
	float normalDotLightDir, normalDotHalfVector;
	vec4 diffuseTerm, specularTerm, color, colorTex, texel;
	float attenuation;

	texel = texture2D(texture, gl_TexCoord[0].st);

	if(texel.a == 0.0)
	{
		discard;
	}

	color = ambientGlobal;

	normNormal = normalize(normal);
	normalDotLightDir = max( dot(normNormal , lightDir) , 0.0 );

	if(normalDotLightDir < 0.0001 && normalDotLightDir > -0.0001 && ambient.rgb == vec3(0.0 , 0.0 , 0.0))
	{
		gl_FragColor = color;
	}
	normHalfVec = normalize(halfVector);

	diffuseTerm = diffuse * normalDotLightDir;

	if(normalDotLightDir > 0.0)
	{
		normalDotHalfVector = max(dot( normNormal , normHalfVec.xyz ) , 0.0);
		specularTerm = specular * pow( normalDotHalfVector , shininess );
		attenuation = 1.0 / ( 	gl_LightSource[0].constantAttenuation + 
					gl_LightSource[0].linearAttenuation * attDist +
					gl_LightSource[0].quadraticAttenuation * attDist * attDist );
		color += (attenuation * (diffuseTerm + ambient) ) + (attenuation * specularTerm);
	}

	gl_FragColor = color * texel;
}