Sign in to follow this  
superpig

Viewport coords - Texture coords

Recommended Posts

What kind of transform do I need to perform to map a vertex shader oPos to texture coordinates? I've got a texture the same size as the backbuffer and I want to write a shader that will allow me to render quads from it by extracting the source texture coordinates from the destination position, i.e. so that a vertex in the top left corner of the screen transforms to a texcoord of 0,0 and a vertex in the bottom right corner transforms to 1,1.
m4x4 r0, v0, c0
mov oPos, r0

mad oT0.xy, r0.xy, c4.xy, c4.zw
I thought that would work (given constant 4 == [0.5, -0.5, 0.5, 0.5]) but it doesn't - it maps a small rectangle in the center of the screen to the full texture instead. And yet I thought that the m4x4 instruction would produce output in viewport coords, [-1..1] in each axis? What am I missing here?

Share this post


Link to post
Share on other sites
I fixed it. For anyone encountering the same problem:

The oPos of the vertex shader *almost* comes out in viewport space, except that everything is W times too large. You just divide xyz by w to get things in the expected [-1..1] range.

So post-shader you actually get two transforms going on: this divide-by-w ('perspective divide') and then the map-to-screen-pixels viewport transform.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this