strange behaviour of ambient color with fragment program

I’ve been recently working on ARB_fragment_program and experimented a phong shading code.
It worked fine but I found a strange thing.
When I turn on ambient color it does not lighten up whole scene.
Just take a look at.

Phong shading + ambient color (0.5, 0.5, 0.5, 1.0)
http://blog.naver.com/main/imageZoom.jsp…6/phongamb1.jpg
http://blog.naver.com/main/imageZoom.jsp…3/phongamb2.jpg

vertex lighting + ambient color (0.5, 0.5, 0.5, 1.0)
http://blog.naver.com/main/imageZoom.jsp…ertexlight1.jpg
http://blog.naver.com/main/imageZoom.jsp…ertexlight2.jpg

What did I do wrong? Here is my code.

!!ARBfp1.0

#hint
#OPTION ARB_precision_hint_nicest;

#declarations
ATTRIB tc0 = fragment.texcoord[0];
ATTRIB tc1 = fragment.texcoord[1];
ATTRIB inrm = fragment.texcoord[2];
ATTRIB ildir = fragment.texcoord[3];
ATTRIB ivdir = fragment.texcoord[4];
ATTRIB col0 = fragment.color;

PARAM exp = state.material.shininess;
#PARAM amb = state.lightprod[0].ambient;
PARAM amb = state.lightmodel.ambient;
PARAM diff = state.lightprod[0].diffuse;
PARAM spec = state.lightprod[0].specular;

OUTPUT out = result.color;

TEMP tex, nrm, ldir, vdir, lit, temp;

#instructions

#normalize normal
DP3 nrm.w, inrm, inrm;
RSQ nrm.w, nrm.w;
MUL nrm, inrm, nrm.w;

#normalize light vector
DP3 ldir.w, ildir, ildir;
RSQ ldir.w, ldir.w;
MUL ldir, ildir, ldir.w;

#normalize view vector
DP3 vdir.w, ivdir, ivdir;
RSQ vdir.w, vdir.w;
MUL vdir, ivdir, vdir.w;

#half angle vector
ADD temp, vdir, ldir;
DP3 temp.w, temp, temp;
RSQ temp.w, temp.w;
MUL temp, temp, temp.w;

#light
DP3 lit.y, nrm, ldir; # diffuse
DP3 lit.z, nrm, temp; # specular
LG2 lit.z, lit.z;
MUL lit.z, lit.z, exp.x;
EX2 lit.z, lit.z;

#sum
MAD temp, lit.y, diff, amb;
MAD temp, lit.z, spec, temp;
MUL temp, temp, col0;
TEX tex, tc0, texture[0], 2D;
MUL temp, temp, tex;
TEX tex, tc1, texture[1], 2D;
MUL out, temp, tex;

END

I also tried

MUL temp, lit.y, diff;
MAD temp, lit.z, spec, temp;
ADD temp, temp, amb;

but still got same result.

A strange thing is when I tried this code

#MAD temp, lit.y, diff, amb;
#MAD temp, lit.z, spec, temp;
MOV temp, amb;

it shows exactly what it should produce.
http://blog.naver.com/main/imageZoom.jsp…3/phongamb3.jpg

So I guess nothing wrong with amb data.
I’m using WinXP + Radeon9700 + cat4.6.

EDIT

I put wrong title on screenshots.

phong shading + no ambient color
http://blog.naver.com/main/imageZoom.jsp…/219/phong1.jpg
when light source is behind
http://blog.naver.com/main/imageZoom.jsp…5/14/phong2.jpg

Phong shading + ambient color (0.5, 0.5, 0.5, 1.0)
http://blog.naver.com/main/imageZoom.jsp…6/phongamb1.jpg
light source is behind
http://blog.naver.com/main/imageZoom.jsp…3/phongamb2.jpg

vertex lighting + no ambient color (light source is behind)
http://blog.naver.com/main/imageZoom.jsp…ertexlight1.jpg
vertex lighting + ambient color (0.5, 0.5, 0.5, 1.0)
http://blog.naver.com/main/imageZoom.jsp…ertexlight2.jpg

This is just a shot in the dark…(not really looking at your images)

Shouldn’t you be clamping the result dot product of the normal against the light vector? Perhaps I misread your code (just a brief skim) but you usually clamp the result between 0…1 otherwise you end up adding negative values to your ambient. (ie -0.5(DIFFUSE) + 0.5(AMBIENT) = 0) (ie it may cancel out the ambient.)

If this is not it I’ll have a closer look.

You’re right!

DP3_SAT solved my problem.

Thanks!