How to set depth value by fragment program

const char *readPBuffer_text =
"!!ARBfp1.0
"
"TEMP pt, dproduct;
"
"TEX pt, fragment.texcoord[0], texture[0], RECT;
"
"DP4 dproduct, color.primary, pt;
"
"ADD result.depth, dproduct, color.secondary ;
"
"END
";

glColor4f(rf,gf,bf,af);//rf,gf,bf,af are float variants value in [0…1]
glSecondaryColor3f(sf,sf,sf);//sf is a float variant value in [0…1]

glDrawPixels(0,0,WIDTH,HEIGHT,GL_DEPTH_COMPONENT,GL_FLOAT,pData);//the values in pData are 1;

glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, readPBuffer);
glEnable(GL_FRAGMENT_PROGRAM_ARB);

glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS,0,0);//Stencil test always passes
glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);//when stencil test fall, keep;
//when stencil test pass, depth test fail, keep;
//when all pass, increase;

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);

glBindTexture(GL_TEXTURE_RECTANGLE_NV, _tID);
glEnable(GL_TEXTURE_RECTANGLE_NV);

// draw quad with texture coordinates in the [0…texturewidth, 0…textureheight] range
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex2f(-1,-1);
glTexCoord2f(WIDTH, 0); glVertex2f( 1,-1);
glTexCoord2f(WIDTH, HEIGHT); glVertex2f( 1, 1);
glTexCoord2f(0, HEIGHT); glVertex2f(-1, 1);
glEnd();

I write this fragment program, to dot poduct every texel in texture with the primary color, then add the secondary color, and copy the result into the depth buffer. But I alway got 1 in depth buffer, Is there any problem of my code?

another question is if the depth test is pass, the value in depth buffer will be replaced?

I have resolve the problem of set depth.

But there are another question come out.

it is the problem of color precise.

for example I call glcolor4f(0.03,0.03,0.03,0.03);
then in the fragment program I get the fragment.color.primary I alway got the color like this:
0.02984,0.02984,0.02984,0.02984

There are some drift between the color I set and get.

How to get the precise color of I set?

Thanks!

That is a limitation of floating point values, floats and doubles are just approximations, not exact values.

Then, if it could not be resolve by color, Is there any other method to input pricise parameter into fragment program?

No, it doesnt matter how you pass them since a IEEE floating point value will be used on the cpu and even on the gpu in your fragment program. But you can try to minizize the error. Instead of .03f you pass 3.0f (this one is precise) and divide the result by 100.0f which is also precise. This should work if there is no clamping between 0.0f and 1.0f going on.

Thanks for your advise, I tried it.
but the fragment.primarycolor is 1.0 now, how to indicate no clamp?

I will start a new topic on this question. Expect to see your discussing in the new topic.