Writing to depth buffer within fragment program


I have a gray scale texture which is being applied to an object. When the gray scale texture is rendered, I want it to also render into the depth buffer. I wrote the following fragment program to accomplish this:


TEMP tex0;

TEX tex0, fragment.texcoord[0], texture[0], 2D;

MOV result.depth, tex0.r;
MOV result.color, tex0;


I tried this but it seems like any color that isn’t 100% white is being written to the very front of the zbuffer.

I’m assuming that the values written to result.depth are not linearly mapped into the actual zbuffer. Does anybody know how to tranform the texture color so that it fits linearly into the zbuffer?

I’ve set the depth range from 0 to 1. I’ve also set the near and far clip planes to 0.1 and 100.

Thanks in advance,


these chapters from the red book describe opengl transformations:

also, a search cant hurt: