Jump to content
  • Advertisement
Sign in to follow this  
superpig

Viewport coords - Texture coords

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

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!