活塞中纹理的转换



活塞具有draw_2d函数,它可以接受函数image的结果,该函数返回由纹理生成的图像。image有一个"transformation"参数,大多数示例都是从texture.transform中获取的。在内部,它是一个列表列表,如下所示:

[[0.0025, 0.0, -1.0], [0.0, -0.0033333333333333335, 1.0]]

呼叫示例为:

window.draw_2d(
&e,
|context, graph_2d, _device| {
pw::image(
&texture,
context.transform, // [[0.001, 0.0, 0.0], [0.0, -0.0033333333333333335, 1.0]]
graph_2d
);

我试图找到这些价值观的定义,但失败了。文件只是说"转型",没有进一步澄清。这些数字在数组中意味着什么?

这可能是从(x,y(坐标到纹理(u,v(坐标的线性变换。类似这样的伪代码正在发生。

with tansform [[a, b, c], [d, e, f]]
For every (x,y) being drawn
u = a*x + b*y + c
v = d*x + e*y + f
color = texture(u,v)
draw color at (x,y)

这是一种变换矩阵形式的线性变换,用于变换所绘制图像的(x,y(坐标。如果需要,可以手动指定它,但典型的用法是使用context.transform上的方法来执行所需的转换;context.transform实际上是一个实现Transformed特性的结构。

image函数通过多个嵌套调用向下传递转换,但它最终在triangulation::rect_tri_list_xy中用于生成用于绘制的三角形顶点:

/// Creates triangle list vertices from rectangle.
#[inline(always)]
pub fn rect_tri_list_xy(m: Matrix2d, rect: Rectangle) -> [[f32; 2]; 6] {
let (x, y, w, h) = (rect[0], rect[1], rect[2], rect[3]);
let (x2, y2) = (x + w, y + h);
[[tx(m, x, y), ty(m, x, y)],
[tx(m, x2, y), ty(m, x2, y)],
[tx(m, x, y2), ty(m, x, y2)],
[tx(m, x2, y), ty(m, x2, y)],
[tx(m, x2, y2), ty(m, x2, y2)],
[tx(m, x, y2), ty(m, x, y2)]]
}

最新更新