3d 中纹理坐标的透视校正 [英] perspective correction of texture coordinates in 3d

查看:28
本文介绍了3d 中纹理坐标的透视校正的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个目前运行良好的软件渲染器,但我正在尝试对纹理坐标进行透视校正,但这似乎不正确.我在渲染器中使用与 opengl 相同的矩阵数学.要栅格化三角形,我执行以下操作:

  1. 使用模型视图和投影矩阵转换顶点,并转换为剪辑坐标.

  2. 对于每个三角形中的每个像素,计算重心坐标以插入属性(颜色、纹理坐标、法线等)

  3. 为了校正透视,我使用透视校正插值:(w为顶点深度坐标,c为顶点纹理坐标,b为顶点重心权重)

<块引用>

1/w = b0*(1/w0) + b1*(1/w1) + b2*(1/w2)c/w = b0*(c0/w0) + b1*(c1/w1) + b2*(c2/w2)c = (c/w)/(1/w)

这应该可以修正透视图,它有一点帮助,但仍然存在明显的透视问题.我在这里遗漏了什么,也许是一些舍入问题(我在所有数学中都使用浮点数)?

在这张图中看到纹理坐标沿对角线的明显误差,这是除以深度坐标的结果.

此外,这通常用于纹理坐标......其他属性(例如法线等)是否也需要这样做?

解决方案

我最近破解了这个问题的代码.如果您计划在将纹理分配给表面之前修改内存中的纹理,则可以使用单应性.这在计算上很昂贵,并且会为您的程序添加额外的依赖项.有一个很好的技巧可以为您解决问题.

OpenGL 会自动对您渲染的纹理应用透视校正.您需要做的就是将您的纹理坐标 (UV - 0.0f-1.0f) 乘以平面每个角的 Z 分量(XYZ 位置向量的世界空间深度),它会甩掉"OpenGL 的透视校正.

我最近询问并解决了这个问题.试试这个链接:

纹理映射具有方形纹理的梯形OpenGL

我读到的解决这个问题的论文叫做,Nofollow noreferating-S 页面使用Morphing 和Morphing-Nofollow noreferrings>"">Nofollow noreferring-S.

希望这有帮助!

ct

I'm writing a software renderer which is currently working well, but I'm trying to get perspective correction of texture coordinates and that doesn't seem to be correct. I am using all the same matrix math as opengl for my renderer. To rasterise a triangle I do the following:

  1. transform the vertices using the modelview and projection matrixes, and transform into clip coordinates.

  2. for each pixel in each triangle, calculate barycentric coordinates to interpolate properties (color, texture coordinates, normals etc.)

  3. to correct for perspective I use perspective correct interpolation: (w is depth coordinate of vertex, c is texture coordinate of vertex, b is the barycentric weight of a vertex)

1/w = b0*(1/w0) + b1*(1/w1) + b2*(1/w2)
c/w = b0*(c0/w0) + b1*(c1/w1) + b2*(c2/w2)
c = (c/w)/(1/w)

This should correct for perspective, and it helps a little, but there is still an obvious perspective problem. Am I missing something here, perhaps some rounding issues (I'm using floats for all math)?

See in this image the error in the texture coordinates evident along the diagonal, this is the result having done the division by depth coordinates.

Also, this is usually done for texture coordinates... is it necessary for other properties (e.g. normals etc.) as well?

解决方案

I cracked the code on this issue recently. You can use a homography if you plan on modifying the texture in memory prior to assigning it to the surface. That's computationally expensive and adds an additional dependency to your program. There's a nice hack that'll fix the problem for you.

OpenGL automatically applies perspective correction to the texture you are rendering. All you need to do is multiply your texture coordinates (UV - 0.0f-1.0f) by the Z component (world space depth of an XYZ position vector) of each corner of the plane and it'll "throw off" OpenGL's perspective correction.

I asked and solved this problem recently. Give this link a shot:

texture mapping a trapezoid with a square texture in OpenGL

The paper I read that fixed this issue is called, "Navigating Static Environments Using Image-Space Simplification and Morphing" - page 9 appendix A.

Hope this helps!

ct

这篇关于3d 中纹理坐标的透视校正的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆