DX9纹理半像素偏移-Directly Mapping Texels to Pixels

为了便于理解, 我们通常会把像素抽象成下面的样子
1
但是更正确的理解应该是这样, 像素始终是一个点, 它位于格子的中心, 这个格子用来表示像素照亮的区域.
2
这时, 如果我们画一个从 ( 0, 0 ) 到 ( 4, 4 ) 的矩形, 光栅化之后的结果实际上会偏移 0.5 个像素. 因为一个像素的颜色只能有一个值, 如果直接显示意味着边缘的像素需要两个颜色.
3
现在我们画的是一个全蓝色的方块, 这个时候还没有严重的问题, 接下来我们看一下给他添加纹理之后的情况.
4
这个纹理是 4 x 4 的, 也许你希望看到的是一个和 4 x 4 矩形完美匹配的样子, 但实际上添加纹理之后的结果是这样的 ( linear filtering mode + clamp addressing mode ).
5
假设我们的 4 x 4 纹理存储在 MyTexture 里, 使用 MySampler 对其进行采样. 每一个光栅化后得到的像素都会执行一次 Pixel Shader, 并且返回一个在 vTexCoord 坐标处采样到的颜色. 但是纹理坐标系是不受光栅化影响的, 如下图所示, 纹理实际上还处在原来的位置, 但是光栅化得到的像素实际上是偏移了 0.5 个像素的( 参考黑点和蓝点的差异 )
6
光栅化后的像素 ( 0, 0 )直接拿到了 UV ( 0.0, 0.0 ). 但是, 光栅化后的像素 ( 3, 1 ) 经过插值之后拿到的是 UV ( 0.75, 0.25 ), 而这个 UV 值采样的颜色经过 Bilinear Texture Filtering 之后是绿色占 1/4, 灰色占 3/4. 所有像素都经过这个采样之后就会变成之前看到的那种奇怪的样子.


如何调整这个问题? 将顶点的坐标偏移 ( -0.5, -0.5 )


//define FVF with vertex values in transformed screen space
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_TEX1)

struct CUSTOMVERTEX
{
    FLOAT x, y, z, rhw; // position
    FLOAT tu, tv;       // texture coordinates
};

//unadjusted vertex values
float left = 0.0f;
float right = 255.0f;
float top = 0.0f;
float bottom = 255.0f;

//256 by 256 rectangle matching 256 by 256 texture
CUSTOMVERTEX vertices[] =
{
    { left,  top,    0.5f, 1.0f, 0.0f, 0.0f}, // x, y, z, rhw, u, v
    { right, top,    0.5f, 1.0f, 1.0f, 0.0f},
    { right, bottom, 0.5f, 1.0f, 1.0f, 1.0f},
    { left,  top,    0.5f, 1.0f, 0.0f, 0.0f},
    { right, bottom, 0.5f, 1.0f, 1.0f, 1.0f},
    { left,  bottom, 0.5f, 1.0f, 0.0f, 1.0f},
};

//adjust all the vertices to correctly line up texels with pixels 
for (int i=0; i<6; i++)
{
    vertices[i].x -= 0.5f;
    vertices[i].y -= 0.5f;
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Quasi-polynomial mapping-based root-finder is a numerical method used to find the roots of a given polynomial equation. It is an iterative method that uses a quasi-polynomial mapping function to transform the polynomial equation into a linear equation. The method works by iteratively solving the linear equation until the root of the original polynomial equation is found. The quasi-polynomial mapping function is a polynomial function of two variables that maps the complex plane to itself. The function is defined as follows: f(z) = z^d + a_1(z)z^(d-1) + a_2(z)z^(d-2) + ... + a_d-1(z)z + a_d(z) where d is the degree of the polynomial equation, a_i(z) are polynomial functions of z of degree at most d-i, and z is a complex variable. To use the quasi-polynomial mapping-based root-finder, the following steps are performed: 1. Choose an initial guess for the root of the polynomial equation. 2. Apply the quasi-polynomial mapping function to the initial guess. 3. Solve the resulting linear equation for the new value of the guess. 4. Repeat steps 2 and 3 until the difference between successive guesses is less than a specified tolerance. The quasi-polynomial mapping-based root-finder has the advantage of being able to find multiple roots of a polynomial equation, including roots with multiplicity greater than one. It is also relatively fast and efficient compared to other root-finding methods. However, it may not always converge to the correct root, particularly for polynomial equations with multiple roots that are close together.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值