HLSL Lit Function

Hi guys,

I know someone has already answered this one, but I can’t seem to find the post now, unfortunately.

Soooo… I’d be really grateful if whoever answered my question last time could let me know how to emulate the HLSL lit builtin function in GLSL?

It’s defined in the MS DirectX/HLSL documentation as

``````
This function returns a lighting coefficient vector (ambient, diffuse, specular, 1) where:

ambient = 1.
diffuse = ((n • l) < 0) ? 0 : n • l.
specular = ((n • l) < 0) || ((n • h) < 0) ? 0 : ((n • h) * m).

``````

Sorry to be a pain,

a|x

``````
float fNDotL           = dot( fvNormal, fvLightDirection );
vec3  fvReflection     = normalize( ( ( 2.0 * fvNormal ) * fNDotL ) - fvLightDirection );
float fRDotV           = max( 0.0, dot( fvReflection, fvViewDirection ) );
vec4  fvTotalAmbient   = fvAmbient * fvBaseColor;
vec4  fvTotalDiffuse   = fvDiffuse * fNDotL * fvBaseColor;
vec4  fvTotalSpecular  = fvSpecular * ( pow( fRDotV, fSpecularPower ) );

gl_FragColor = ( fvTotalAmbient + fvTotalDiffuse + fvTotalSpecular );

``````

For developing GLSL oder HLSL Shaders you should look into ATI’s Rendermonkey. They have very good examples there. The code above is taken out of one of ATI’s samples.

GLSL doesn’t have lit but on nVidia, you can use Cg functions under certain circumstances :
Do not put a #version and I forgot the other requirement.

When you compile, it will give you warnings that lit is not a standard in GLSL.

vec4 result;
result = lit(diffuseDOTProduct, specularDOTProduct, Shininess);

The direct conversion would be something like

``````
vec4 lit(float NdotL, float NdotH, float m) {

float ambient = 1.0;
float diffuse = max(NdotL, 0.0);
float specular = step(0.0, NdotL) * max(NdotH * m, 0.0);

return vec4(ambient, diffuse, specular, 1.0);
}

``````

I haven’t tried this, but should be close to what you want. It’s also probably not optimal.

I have no knowledge of the lit() function in HLSL, but I find it odd that they multiply by m, instead of raising NdotH by m, which would be the correct way for Blinn shading. I guess it’s a cheap fake thing?

Thanks very much for those replies, guys. Sorry for not getting back to you earlier. I will try your various suggestions.

Thanks again,

alx

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.