HLSL Pixel position/direction relative to triangle center

Started by
1 comment, last by ET3D 16 years, 6 months ago
Does anyone have a smooth trick for determining a pixels position/direction relative to the current triangle center in the pixel shader? I was thinking of baking this info into a texture, but it'd be preferable to derive this in the shader if possible. Oh yes, I should add that the triangles are part of a mesh, and the mesh is rendered using an index buffer.
Advertisement
I would add a texture coordinate set to the vertices (perhaps computed by the vertex shader to save bandwidth) that encoded barycentric coordinates at the vertices. As the barycentric coordinates are interpolated across the triangle, you'll know where you are w.r.t. the triangle vertices at any given pixel. If you need to know the *actual* location, as opposed to the relative location, of a pixel w.r.t. the triangles then you'll also have to pass in the vertex position(s) as texture coordinate set(s) to compute the position from the barycentric coordinates. At which point, you might as well just skip the whole barycentric coordinate business and use the interpolated position directly.

My free book on Direct3D: "The Direct3D Graphics Pipeline"
My blog on programming, vintage computing, music, politics, etc.: Legalize Adulthood!

I think that using an indexed triangle list will make any per-vertex solution difficult. When a vertex is shared by several triangles, it's impossible to code into it any information which is supposed to be different for different triangles. Baking such information into a texture sounds to me like the only solution in this case.

In DX10 you can use the geometry shader to calculate such information.

This topic is closed to new replies.

Advertisement