projective textures


I´m currently trying out projective textures. The main thing, projecting the texture itself worked out to be easy. But I (inspite of most papers/tutorials) I also want to use the (projected) z-coordinate for another texture, in this case in order to achieve falloff-effects. The falloff should be done by looking up the distance in a 1D-texture. For this to work I need a linear mapping from [nearz,farz] to [0,1]. First I tried a standard projection matrix. After some hours of trying I finally convinced myself that the x,y projection is done ok (as expected), but the falloff is MUCH too much. So I placed some test z-values into the matrix. The projected z-values are not equally distributed from 0…1. Which is AFAIK a desired effect for the depthvalues in the depthbuffer.
So my question is: is there a projection matrix that projects x and y perspectivly as usual, but maps z linearly from nearz…farz to 0…1 ?
I know, this post is probably off-topic, sorry for that. But I think here are the most competent users able to answer this question.

I was messing around with some numbers and this seemed to work:

Assuming nearz = 0
Let i : number from [0,farz] you want to convert
Let m : mapped i to [0,1]

m = ( i / (farz/2) ) * 0.5

Now if you can’t have nearz at 0 then it will be different.


Thanks for your reply, but this is not what I need. I know how to map ranges from one to another. The Problem is, putting this into a projection matrix, where the result is a homogenous vector that undergoes a divide-by-w before being used for sampling the texture.

Could you alter your 1D texture so that it scaled non-linearly to remove the effect of a non-linear z range?

Why don’t use just use a texgen?

rgpc: this is a good idea, but I´d rather leave the texture unchanged. On the hand side you could model the falloff much more detailed for near objects…

gorg: texgen is not an option for me, I use vertex programs. Up to now everything was very convenient, the whole transformation from objectspace into the texturespace of the projected texture could be done in one matrix:

For orthogonal projection this works for all 3 coordinates… It would be sad if I´d had to do extra code for each type of projection.

There actually is another option. I can make the fragment program in a way that it doesn´t do the divide-by-w for the falloff texture coordinate. I could then setup a linear mapping in the z-row of the projection matrix. But by abandon the division I make the “bias*scale” part not working correctly on the z-axis. Doh!..

anyway, thanks for your suggestions