Contact Information

Email - Duylinh.Nguyen@gmail.com
Google Voice - 415-613-2177

Monday, August 18, 2008

GLSL SHADER!! WOO HOO!!

Sooo.. I just finished with this OpenGL Shader tutorial from Digital Tutors. It was pretty neat!!

Here is the shader in the XSI viewport


and here was the code.

Vertex Program:

varying vec4 mat_diffuse; // the material's diffuse color
varying vec4 mat_ambient; // the material's ambientcolor
varying vec4 xsi_ambience; // the XSI scene ambient color (called ambience)
varying vec3 vtx_normal; // output the transformed normal
varying vec3 vtx_light_direction; // output the light direction (from the vertex to the light)
varying vec3 vtx_half_vector; // output the half vector
varying float vtx_dist_from_light; // output the distance from vertex to light (for attenuation)

varying vec4 eyeDir;
vec4 sieyepos;

void main()
{
// transform vertex and normal
gl_Position = ftransform();
vtx_normal = normalize(gl_NormalMatrix * gl_Normal);

// Calculate the light direction from this vertex to the light source
vec4 eyePosition = gl_ModelViewMatrix * gl_Vertex;
vec3 lightDirection = vec3(gl_LightSource[0].position-eyePosition);
eyeDir = normalize(sieyepos - eyePosition);
vtx_light_direction = normalize(lightDirection);
vtx_dist_from_light = length(lightDirection);
vtx_half_vector = normalize(gl_LightSource[0].halfVector.xyz);

// Blend light and material color and output to fragment
mat_diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse;
mat_ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient;
xsi_ambience = gl_LightModel.ambient * gl_FrontMaterial.ambient;
}


Fragment Program:


varying vec4 mat_diffuse; // the material's diffuse color
varying vec4 mat_ambient; // the material's ambientcolor
varying vec4 xsi_ambience; // the XSI scene ambient color (called ambience)

varying vec3 vtx_normal; // the interpolated normal
varying vec3 vtx_light_direction; // the interpolated light direction
varying vec3 vtx_half_vector; // the interpolated half vector

varying float vtx_dist_from_light; // the interpolated distance from the light source
varying vec4 eyeDir;

sampler2D refMap;

void main()
{
vec3 viewV,ldir;

vec4 finalColor = xsi_ambience;
vec3 interpolatedNormal = normalize(vtx_normal);

float NdotL = max(dot(interpolatedNormal,normalize(vtx_light_direction)),0.0);
if (NdotL > 0.0)
{
// calculate diffuse
finalColor += (NdotL * mat_diffuse + mat_ambient);

// calculate attenutation factor.
// NOTE. XSI does not change default OpenGL values for constant, linear and quadratic attenuation.
float att = 1.0 / (gl_LightSource[0].constantAttenuation +
gl_LightSource[0].linearAttenuation * vtx_dist_from_light +
gl_LightSource[0].quadraticAttenuation * vtx_dist_from_light * vtx_dist_from_light);

finalColor *= att;

// calculate (attenuated) specular term
vec3 halfV = normalize(vtx_half_vector);
float NdotHV = max(dot(interpolatedNormal,halfV),0.0);
finalColor += att * gl_FrontMaterial.specular * gl_LightSource[0].specular *
pow(NdotHV,gl_FrontMaterial.shininess);
}
vec4 incidence = 1-abs( dot( interpolatedNormal, eyeDir ) );
vec3 refVec = reflect(interpolatedNormal, eyeDir);
vec4 envColor = texture2D(refMap, refVec.xy);
gl_FragColor = finalColor * (incidence*3) * envColor;
}

1 comment:

Vahan Sosoyan said...

Can you share sample scene to play with it?