# why do we need to divide w in texture projection

This topic is 1239 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

oh my poor English. Please, bear with me.

I'm trying to figure out the math in texture projection. and such shader code comes to me:

half4 col = tex2Dproj( _GrabTexture, i.uvgrab);

//half4 col = tex2D( _GrabTexture, i.uvgrab);

I found  the difference between tex2Dproj and tex2D is

half4 col = tex2Dproj( _GrabTexture, i.uvgrab);

equals to

half4 col = tex2D( _GrabTexture, i.uvgrab / i.uvgrab.w);


what I don't get is the reason for this.

the concept of texture projection is finding the texcoord of each vertex in projector's draw call, and convert screen coords([-1,1],  [-1, 1], [ -1,1]) to clamped texture coords ([0,1],  [0, 1], [ -1,1]) . and then the modified screen coords can be used to sample frame buffer(rt) and draw those vertices again. But it seems to me at this point it's sufficed. I don't see the reason to divide the depth field.

can anyone give me a clue.

##### Share on other sites
Dividing by 'w' gives perspective correct texture mapping. Failing to do so will give affine texture mapping, which has issues with distortion and texture crawl.

Image from:
https://en.wikipedia.org/wiki/Texture_mapping Edited by MarkS

##### Share on other sites

http://web.cs.ucdavis.edu/~amenta/s12/perspectiveCorrect.pdf

so you mean: the implementation of tex2D actually divides w from vertex automatically?

like:

notTex2D(_DispMap, i.uv.xy / i.vertex.w);

and

notTex2DProj(_DispMap, i.uv.xy / i.vertex.w / i.uv.w);

right?

##### Share on other sites

I have no idea what tex2D and tex2DProj do internally.

##### Share on other sites

guys, frankly, I thank you all for reply, but I still don't get it.

Why is this divide called perspective divide? and it's a  (x or y / depth) => tan( fov / 2) right?

I'm gonna go through some basic knowledge again, I must be missing sth, seriously!

##### Share on other sites

It's called perspective correct texture mapping because dividing the u and v coordinates by the depth (z) coordinate causes the texture look up to be compressed by distance.

In the early days of graphics programming, you divided by the depth (z) component. Today, with the use of matrices, the z component somehow (I'm a little unclear on this) gets merged into the 'w' component during projection. When you divide by 'w', you are dividing by depth. Either way, you are forcing the texture to be mapped to the polygon in a way that shows perspective foreshortening.

##### Share on other sites

guys, frankly, I thank you all for reply, but I still don't get it.

Why is this divide called perspective divide? and it's a  (x or y / depth) => tan( fov / 2) right?

I'm gonna go through some basic knowledge again, I must be missing sth, seriously!

The "perspective divide" comes from the use of homogeneous coordinates. If you're not familiar with that term, I would suggest reading some background material. This link that I found explains some of the basics, and how it applies to graphics. Edited by MJP

##### Share on other sites

thanks to you all.

"Don't build your house on the quicksand!" that means me, I'm gonna get this clear!

##### Share on other sites

I mean I built my house on the quicksand

thanks again

1. 1
2. 2
Rutin
20
3. 3
khawk
18
4. 4
A4L
14
5. 5

• 12
• 16
• 26
• 10
• 44
• ### Forum Statistics

• Total Topics
633762
• Total Posts
3013727
×