interpolated texture coordinates

*爱你&永不变心* 提交于 2019-12-06 06:40:52

I always struggle with this, so let's see if I can explain it.

Imagine one row of your grid. In the following picture, I've numbered each of the fragments (0-127) in that row. I've put some texture coordinates below the pixels, too. Notice that the leftmost edge is 0.0 and the rightmost edge is 1.0.

 +-----------+-----------+-----------+-----------+---    ---+-----------+
 |           |           |           |           |          |           |
 |           |           |           |           |          |           |
 |     0     |     1     |     2     |     3     |   . . .  |    127    |
 |           |           |           |           |          |           |
 |           |           |           |           |          |           |
 +-----------+-----------+-----------+-----------+---    ---+-----------+
 ^           ^           ^           ^                      ^           ^
 |           |           |           |                      |           |
 |           |           |           |                      |           |
0/128       1/128       2/128       3/128                127/128     128/128

When the render wants to texture a fragment, it uses the texture coordinates of the center of the fragment -- where the number is. Notice that the center of fragment 0 is half-way between 0/128 (aka 0) and 1/128 (aka .0078125). That's 1/256 (aka .00390625).

So, i think the formula would be better expressed as:

coordinate = (pixelId + 0.5) / 128

Here's some python that gets answers similar to yours:

for i in range(128):
   print (0.5 + i) / 128


0.00390625
0.01171875
0.01953125
0.02734375
0.03515625
0.04296875
...
0.97265625
0.98046875
0.98828125
0.99609375

I suspect that the difference between my results and your results has something to do with the fact that your values were squeezed into one [8-bit] color channel to return them from the shader.

I hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!