Sign in to follow this  
Funkymunky

how does a pixel shader calculate tex coordinates

Recommended Posts

Funkymunky    1413
How does a pixel shader calculate the interpolated texture coordinates? You assign texture coordinates to the vertices, so I assume the pixel shader has to calculate an interpolated value; how does the pixel shader know which vertex(s) it is attached to? I ask because I'm programming a pixel shader that needs to know which vertex it is most associated with. Such that I could know in the pixel shader that I am on vertex 5, or 27, or whatever. Or even all 3 vertices of the face the pixel shader is being used on, and then of course the weighting.

Share this post


Link to post
Share on other sites
xycsoscyx    1149
Through rasterization; the pixel doesn't really "know" which vertex it's attached to. Instead, the rasterizer takes a triangle at a time, scans it, then fills it in. As it's filling it in, it's interpolating all the vertex data to that pixel, then passing it to the pixel shader.

To handle your case, you'd need to somehow store that information in the vertex itself, then pass it down to the pixel shader. The problem there is still interpolation, the vertex value from each vertex will be interpolated into that pixel, so you wouldn't end up with the right values. Offhand, I'm really not sure if you CAN do what you want to do (without some really complex chickanery).

Share this post


Link to post
Share on other sites
Fingers_    410
If you can store and interpolate an extra RGB color per vertex, you could set the color of one of the vertices of a triangle to red, another to green and the third to blue.. Then check which channel has the highest value in the interpolated color. This will be the vertex you're closest to. You could perhaps then use this to look up a "real" vertex index.

Not sure if this is at all practical though. Setting it up could be difficult and you couldn't share vertices between triangles.

Share this post


Link to post
Share on other sites
Todo    451
Quote:
Original post by Funkymunky
How does a pixel shader calculate the interpolated texture coordinates? You assign texture coordinates to the vertices, so I assume the pixel shader has to calculate an interpolated value; how does the pixel shader know which vertex(s) it is attached to?


As said before, but simply put : you can't, the rasterizer interpolates and feeds these values to the pixel shader, where you can take matters into your own hands. However, Fingers_ tip sounds like a good solution for triangle primitives.

Share this post


Link to post
Share on other sites
Funkymunky    1413
Ah! Or how about this. If I make sure the mesh I'm using is uv mapped, and I also assure that each face is uniquely mapped, then I can make a new texture map of indexes to use in the pixel shader. The map will basically set each pixel as an index to the nearest vertex based on proximity to the texture coordinates of the regular uv map. Then I can use those regular texture coordinates on the map of indexes to find the nearest vertex to the fragment, even after interpolation.

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