PDA

View Full Version : How to set depth value by fragment program



jeffreyopengl
02-01-2005, 04:57 PM
const char *readPBuffer_text =
"!!ARBfp1.0\n"
"TEMP pt, dproduct;\n"
"TEX pt, fragment.texcoord[0], texture[0], RECT;\n"
"DP4 dproduct, color.primary, pt;\n"
"ADD result.depth, dproduct, color.secondary ;\n"
"END\n";
...

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,G L_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?

jeffreyopengl
02-01-2005, 09:15 PM
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!

02-02-2005, 12:00 AM
That is a limitation of floating point values, floats and doubles are just approximations, not exact values.

jeffreyopengl
02-02-2005, 01:00 AM
Then, if it could not be resolve by color, Is there any other method to input pricise parameter into fragment program?

02-02-2005, 01:22 AM
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.

jeffreyopengl
02-02-2005, 06:15 PM
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.