CPU 上的 XNA 插补?

发布于 2024-11-28 20:18:17 字数 948 浏览 1 评论 0原文

CPU 上的插值

我希望能够在 CPU 上进行插值。

考虑以下示例:

X1 | X2

-------

X3 | X4

X1、X2、X3和X4都是像素(Vector4格式)。它们都有以下坐标。

  • X1: {X: 0, Y: 0}
  • X2: {X: 1, Y: 0}
  • X3: {X: 0, Y: 1}
  • X4: {X: 1, Y: 1}

现在,我想成为能够在给定 Vector2 的像素之间进行插值。

假设我想要坐标 {X: 0.348, Y: 0.129} 的颜色。

我如何在像素之间正确插值?

为什么我想这样做

我知道这听起来很疯狂,但这就是我想做的。我正在尝试在 CPU 上模拟 GPU 着色器算法。它适用于柏林噪声发生器。我已经在游戏中通过像素着色器生成了 2D 地形,并且我希望能够(在给定的 X、Y 和 Z 坐标)检查那里是否存在墙壁,使用准确的CPU 上的算法相同。

如果您想更多地了解我正在尝试做什么,请参阅此问题:https://gamedev.stackexchange.com/questions/15667/perlin-noise-copying-the-algorithm-on-the-cpu

编辑

我发布这是错误的地方。它应该位于“gamedev.stackexchange.com”中。无论如何我希望你能得到答案。

Interpolation on the CPU

I would like to be able to do interpolation on the CPU.

Consider the following example:

X1 | X2

-------

X3 | X4

X1, X2, X3 and X4 are all pixels (in Vector4 format). They all have the following coordinates.

  • X1: {X: 0, Y: 0}
  • X2: {X: 1, Y: 0}
  • X3: {X: 0, Y: 1}
  • X4: {X: 1, Y: 1}

Now, I want to be able to do interpolation between the pixels given a Vector2.

So let's say I want the color of the coordinates {X: 0.348, Y: 0.129}.

How would I interpolate properly between the pixels?

Why I want to do this

I know this sounds crazy, but it's what I want to do. I am trying to simulate a GPU shader algorithm on the CPU. It's for a Perlin Noise generator. I've already got the 2D terrain generated through a pixel shader that way in the game, and I want to be able to (at a given X, Y and Z coordinate) check if there's a wall present there or not, using the exact same algorithm on the CPU.

If you want to know more about what I am trying to do, see this question: https://gamedev.stackexchange.com/questions/15667/perlin-noise-copying-the-algorithm-on-the-cpu

Edit

I posted this the wrong place. It should have been in "gamedev.stackexchange.com". I hope you have the answer anyway.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌路黄昏 2024-12-05 20:18:17

您正在寻找的是双线性插值维基百科)。

XNA Lerp 函数执行线性插值 - 即:一个轴。双线性是在两个轴上。幸运的是,这只是在一个轴上进行线性插值(两次:该轴上的每对输入一次),然后对两个结果进行线性插值的问题。

所以你会做这样的事情:

Vector2 position = new Vector2(0.348f, 0.129f); // <- example data from question

Vector4 result = Vector4.Lerp(Vector4.Lerp(X1, X2, position.X),
                              Vector4.Lerp(X3, X4, position.X),
                              position.Y);

What you are looking for is bilinear interpolation (Wikipedia).

The XNA Lerp functions do linear interpolation - that is: one axis. Bi-linear is on two axes. Fortunately this is simply a matter of linearly interpolating on one axis (twice: once for each pair of inputs on that axis), and then linearly interpolating the two results.

So you'd do something like this:

Vector2 position = new Vector2(0.348f, 0.129f); // <- example data from question

Vector4 result = Vector4.Lerp(Vector4.Lerp(X1, X2, position.X),
                              Vector4.Lerp(X3, X4, position.X),
                              position.Y);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文