PDA

View Full Version : cg phong shader and opengl



giuseppe500
07-28-2010, 02:04 AM
Hy , i'm still implement a phong shader in opengl, but i'm not understand at all the attenuation component that is multiplyed for the results color (diffuse , specular and ambient):
.
.
.
float fAttn = GetAttenuation((float3x3)WMatrix,kModelPosition,
Light0ModelPosition,Light0Attenuation);

float3 kColor = MaterialAmbient*Light0Ambient
+ kLighting.y*MaterialDiffuse.xyz*Light0Diffuse
+ kLighting.z*MaterialSpecular.xyz*Light0Specular;

kVertexColor.rgb = MaterialEmissive + fAttn*kColor;

float GetAttenuation
(
float3x3 kWMatrix,
float3 kModelPos,
float3 kLightPos,
float4 kAttenuation)
{
// Attenuate the color (x=constant, y=linear, z=quadratic, w=intensity).
// Attenuation is not active when the x component is zero. The distance
// must be computed in *world* coordinates. The distance in camera
// coordinates is not correct when the MVP matrix has nonunit scaling
// factors.

float3 kVertexModelDir = kModelPos - kLightPos;
float3 kVertexWorldDir = mul(kVertexModelDir,kWMatrix);
float fDistance = sqrt(
kVertexWorldDir.x*kVertexWorldDir.x +
kVertexWorldDir.y*kVertexWorldDir.y +
kVertexWorldDir.z*kVertexWorldDir.z);

float fAttn = kAttenuation.w/(kAttenuation.x + fDistance*(kAttenuation.y
+ fDistance*kAttenuation.z));

return fAttn;
}

i see that the attenuation go from 0 to 1 or that is seems.
But what kind of parameter i must put in the attenuation function , in particular the for the Light0Attenuation parameter?What mean:
x=constant, y=linear, z=quadratic, w=intensity?

Thanks.

DmitryM
07-28-2010, 07:22 AM
The meaning of the attenuation vector can be clearly seen in the 'float fAttn = ...' equation.

W = basic light intensity
Y = linear distance attenuation constant, determines how fast light goes off linearly with distance to the source
Z = the same, but with distance^2
X = constant factor to soften linear & quadratic factors

Blender allows to setup these parameters explicitly for a light source. So you can play with them and get the required visual effect.