如果有三角形顶点,如何通过三角形内的任意 x,y 坐标找到 z
给定顶点 V1 (x1,y1,z1)
、V2 (x2,y2,z2)
、V3 (x3,y3,z3)
一个三角形 T,如果我知道 (x,y)
位于三角形 Tp (x1,y1) 的投影内,我必须通过它的 x,y 坐标找到该点的 z 坐标, (x2,y2),(x3,y3)。
实际上,3D 中的三角形平面是由等式定义的:Ax+By+Cz+D=0
,我可以找到 z = (D-Ax-By)/C
问题是 A、B、C、D 在运行时计算成本太高:
A = y1(z2-z3) + y2(z3-z1) + y3(z1-z2)
B = z1(x2-x3) + z2(x3-x1) + z3(x1-x2)
C = x1(y2-y3) + x2(y3-y1) + x3(y1-y2)
D = -x1(y2*z3 – y3*z2) – x2(y3*z1 – y1*z3) – x3 (y1*z2 – y2*z1)
是否可以使用 opengl 着色器来计算 A、B、C、D?是否有寻找平面系数的优化算法?
Given vertices V1 (x1,y1,z1)
, V2 (x2,y2,z2)
, V3 (x3,y3,z3)
of a triangle T, I have to find z coordinate of a point by it's x,y coordinate if I know that (x,y)
lies within projection of triangle Tp (x1,y1), (x2,y2), (x3,y3)
.
Actually, triangle plane in 3D is defined by equation: Ax+By+Cz+D=0
, and I can find z = (D-Ax-By)/C
The problem is that A, B, C, D are too expensive to calculate in run-time:
A = y1(z2-z3) + y2(z3-z1) + y3(z1-z2)
B = z1(x2-x3) + z2(x3-x1) + z3(x1-x2)
C = x1(y2-y3) + x2(y3-y1) + x3(y1-y2)
D = -x1(y2*z3 – y3*z2) – x2(y3*z1 – y1*z3) – x3 (y1*z2 – y2*z1)
Is it possible to calculate A, B, C, D using, say, opengl shaders? Are there optimized algorithms to find plane coefficients?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该技术称为重心坐标,但维基页面很难理解 -
请参阅 http://www.alecjacobson.com/weblog/?p=1596
代码来自 http://www.gamedev.net/topic/597393-getting-the-height-of-a-point-on-a-triangle/ - 仔细考虑计算机图形与数学的使用YZ
ps。我不知道有任何使用着色器的更快版本。一种快速的肮脏+解决方案是使用基于顶点高度的颜色渲染三角形,并在 X,Y 处选择像素颜色 - 实际上,这在台式机上永远不会更快,不知道 opengl -es
The technique is called Barycentric coordinates but the wiki page is pretty hard to follow -
See http://www.alecjacobson.com/weblog/?p=1596
Code from http://www.gamedev.net/topic/597393-getting-the-height-of-a-point-on-a-triangle/ - carefull about computer graphics vs maths use of Y Z
ps. I Don't know of any faster version using shaders. One quick dirty+solution is to render the triangle using colors based on the height of the vertices and pick the pixel color at your X,Y - in practice this never ends up being much faster on a desktop machine, don't know about opengl-es