对 OpenGL 纹理使用自己的 mipmap 创建算法有意义吗?

发布于 2024-07-18 15:34:56 字数 156 浏览 10 评论 0原文

我想知道如果我使用自己的算法来预先生成纹理 mipmap,而不是内置的自动算法,纹理 mipmap 的质量是否会更好。 我可能会使用一种缓慢但漂亮的算法,例如 Lanczos 重采样。

是否有意义? 我会在现代显卡上获得任何质量提升吗?

I was wondering if the quality of texture mipmaps would be better if I used my own algorithm for pre-generating them, instead of the built-in automatic one. I'd probably use a slow but pretty algorithm, like Lanczos resampling.

Does it make sense? Will I get any quality gain on modern graphics cards?

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

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

发布评论

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

评论(4

冷︶言冷语的世界 2024-07-25 15:34:56

有充分的理由生成您自己的 mipmap。 然而,下采样的质量不是其中之一。

游戏和图形程序员过去已经尝试过各种下采样算法。 最后事实证明,非常简单的“平均四个像素”方法给出了最好的结果。 此外,更先进的方法在理论上数学上更正确,它们往往会从 mipmap 中获取大量清晰度。 这会带来平坦的外观(尝试一下!)。

由于某些(对我来说无法理解)原因,简单平均方法似乎在抗锯齿和保持 mipmap 锐利之间具有最佳权衡。

但是,您可能希望使用伽玛校正来计算 mipmap。 OpenGL 本身并不执行此操作。 这可以产生真正的视觉差异,特别是对于较暗的纹理。

这样做很简单。 不要像这样对四个值进行平均:

float average (float a, float b, float c, float d)
{
  return (a+b+c+d)/4
}

这样做:

float GammaCorrectedAverage (float a, float b, float c, float d)
{
  // assume a gamma of 2.0 In this case we can just square
  // the components. 
  return sqrt ((a*a+b*b+c*c+d*d)/4)
}

此代码假设您的颜色分量已标准化为 0 到 1 的范围内。

There are good reasons to generate your own mipmaps. However, the quality of the downsampling is not one of them.

Game and graphic programmers have experimented with all kinds of downsampling algorithms in the past. In the end it turned out that the very simple "average four pixels"-method gives the best results. Also more advanced methods are in theory mathematical more correct they tend to take a lot of sharpness out of the mipmaps. This gives a flat look (Try it!).

For some (to me not understandable) reason the simple average method seems to have the best tradeoff between antialiasing and keeping the mipmaps sharp.

However, you may want to calculate your mipmaps with gamma-correction. OpenGL does not do this on it's own. This can make a real visual difference, especially for darker textures.

Doing so is simple. Instead of averaging four values together like this:

float average (float a, float b, float c, float d)
{
  return (a+b+c+d)/4
}

Do this:

float GammaCorrectedAverage (float a, float b, float c, float d)
{
  // assume a gamma of 2.0 In this case we can just square
  // the components. 
  return sqrt ((a*a+b*b+c*c+d*d)/4)
}

This code assumes your color components are normalized to be in the range of 0 to 1.

凉栀 2024-07-25 15:34:56

是什么促使你去尝试? 您当前的 mipmap 生成效果是否不佳? (即,您看过吗?)请记住,无论如何,您的结果通常仍会进行(三)线性插值,因此在运动之间,改进重采样的回报通常会急剧递减。

What is motivating you to try? Are the mipmaps you have currently being poorly generated? (i.e. have you looked?) Bear in mind your results will often still be (tri)linearly interpolated anyway, so between that an motion there are often steeply diminishing returns to improved resampling.

写下不归期 2024-07-25 15:34:56

这取决于您显示的资产类型。 Lanczos 滤波器更接近理想的低通滤波器,如果并排比较 mip 贴图,结果会很明显。 大多数人会误以为锯齿是清晰度 - 这又取决于您的资源是否倾向于包含高频 - 我确实见过箱式滤波器不是一个好的选择的情况。 但由于 mip 贴图随后是线性插值的,因此增益可能不会那么明显。 还有一件事要提 - 大多数人使用盒式滤波器并将输出作为输入传递到下一阶段 - 这样你就会失去精度和视觉能量(尽管伽玛会对此有所帮助)。 如果您可以编写使用任意过滤器的代码(请注意,它们中的大多数可分为两个通道),您通常会缩放过滤器内核本身并从基础纹理生成 mip 贴图级别,这是一件好事。

It depends on the kind of assets you display. Lanczos filter gets closer to ideal low-pass filter and the results are noticeable if you compare the mip maps side by side. Most people will mistake aliasing for sharpness - again it depends whether your assets tend to contain high frequencies - I've definitely seen cases where box filter was not a good option. But since the mip map is then linearly interpolated anyway the gain might not be that noticeable. There is another thing to mention - most people use box filter and pass the output as an input into the next stage - in this way you lose both precision and visual energy (although gamma will help this one). If you can come up with code that uses arbitrary filter (mind you that most of them are separable into two passes) you would typically scale the filter kernel itself and produce mip map levels from the base texture, which is a good thing.

浅浅 2024-07-25 15:34:56

作为这个问题的补充,我发现一些完全不同的 mipmapping(而不是那些仅仅试图实现最佳缩小质量的算法,如 Lanczos 过滤)算法对某些纹理具有良好的效果。

例如,在一些应该表示高频信息的纹理上,我尝试使用一种算法,该算法只需从每次迭代所考虑的四个像素中随机选取一个像素。 结果很大程度上取决于纹理和它应该传达的内容,但我发现它对某些人有很大的影响; 尤其是地面纹理。

我尝试过的另一种方法是采用四个像素中偏差最大的像素来保持对比度。 它的用途更少,但它们确实存在。

因此,我实现了为每个纹理选择 mipmap 算法的选项。

编辑:我想我可以提供一些实践中差异的例子。 这是地面上的一块草纹理,最左边的图片使用标准平均 mipmapping,最右边的图片使用随机 mipmapping:

平均 mipmapping,三线性过滤
随机 mipmapping , 三线性过滤

我希望观看者能够体会到平均 mipmap 中丢失了多少“明显细节”,以及这种纹理看起来有多平坦。

另外,作为参考,这里是打开 4× 各向异性过滤的相同示例(以上为三线性):

平均 mipmapping,4× 各向异性过滤
随机 mipmapping , 4× 各向异性过滤

各向异性过滤使差异不那么明显,但它仍然存在。

As an addition to this question, I have found that some completely different mipmapping (rather than those simply trying to achieve best down-scaling quality, like Lanczos filtering) algorithms have good effects on certain textures.

For instance, on some textures that are supposed to represent high-frequency information, I have tried using an algorithm that simply takes one random pixel of the four that are being considered for each iteration. The results depend much on the texture and what it is supposed to convey, but I have found that it gives great effect on some; not least for ground textures.

Another one I've tried is taking the most deviating of the four pixels to preserve contrasts. It has even fewer uses, but they do exist.

As such, I've implemented the option to choose mipmapping algorithm per texture.

EDIT: I thought I might provide some examples of the differences in practice. Here's a piece of grass texture on the ground, the leftmost picture being with standard average mipmapping, and the rightmost being with randomized mipmapping:

Average mipmapping, trilinear filtering
Random mipmapping, trilinear filtering

I hope the viewer can appreciate how much "apparent detail" is lost in the averaged mipmap, and how much flatter it looks for this kind of texture.

Also for reference, here are the same samples with 4× anisotropic filtering turned on (the above being tri-linear):

Average mipmapping, 4× anisotropic filtering
Random mipmapping, 4× anisotropic filtering

Anisotropic filtering makes the difference less pronounced, but it's still there.

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