Vertex program, placing z in color.

Shouldn’t it be fairly simple to take the [-1,1] z value after the modelview and projection transforms, divide by 2, and then add 1/2?

For some reason I’m getting either 0 or 1, nothing in between.

This isn’t verbatim, but pretty close to what I want to do.

DP4 UnitSpace.x, mvp[0], Input.x;
DP4 UnitSpace.y, mvp[1], Input.y;
DP4 UnitSpace.z, mvp[2], Input.z;
DP4 UnitSpace.w, mvp[3], Input.w;

MUL r0.z, half.z, UnitSpace.z;
ADD r1.z, half.z, r0.z;

MOV oColor.x, r1.z;

I just want to color the vertices based on their distance from the near plane, to compare to my rough front-to-back bounding volume sort.

Maybe I’m missing some fundamental understanding here.


Edit: if I try to take the x or y coordinates, the same thing happens. One half of the model is red, the other black. There is nothing in between. Yes, there’s enough tesselation

[This message has been edited by CatAtWork (edited 08-28-2003).]

The depth is computed as zc/wc where (xc,yc,zc,wc) is the clip coordinates, after transformation through the modelview and projection matrices.
In other words, you have to divide z by w.

Note that if you want the real value of the computed depth that will be used with the z-buffer, you may take into account depth range, polygon offset, etc.

And by the way, you can join the MUL and ADD calls into one single call thanks to the MAD operation.

[This message has been edited by vincoof (edited 08-29-2003).]

Ah, clipping space. Thanks vincoof.