OpenGL,世界到对象坐标映射? (逆矩阵)

发布于 2024-10-06 01:32:12 字数 875 浏览 0 评论 0原文

大家好,

如图所示,

alt text

我有一个名为 O 的对象(线条纹集)。它的对象-坐标系是(x',y',z')。 我使用以下代码片段在 OpenGL 场景中平移、旋转该对象

glPushMatrix();
 glTranslatef(Oz, Oy,Oz);
 glRotatef(rotationX , 1.0, 0.0, 0.0);  
 glRotatef(rotationY, 0.0, 1.0, 0.0);
 glRotatef(rotationZ, 0.0, 0.0, 1.0); 
contour->render();
glPopMatrix()

我有一个名为 H 的点,它被转换为 (hx,hy,hz) 世界坐标

glPushMatrix();
 glTranslatef(hx,hy,hz);
glPopMatrix();

如果我是正确的,(Oz,Oy,Oz) 和 (hx,hy,hz) 是世界坐标。

现在,我要做的是计算 H (hx,hy,hz) 相对于 O 的对象坐标系的位置。(x',y',z'); 据我了解,我可以通过计算对象 O 的逆变换并将其应用于点 H 来做到这一点。

对此有什么建议吗? OpenGL有提供逆矩阵计算的函数吗?如果我以某种方式找到逆矩阵,它们相乘的顺序是什么?

注意:我想实现类似“锤子”的工具,在 H 点,我画一个半径为 R 的球体。用户可以使用这个球体像锤子一样砍倒对象 O。我已经在 2D 中实现了这个,所以我可以使用如果我可以计算锤子位置,则使用相同的算法 相对于 (x',y',z')

提前致谢。

Greetings all,

As seen in the image,

alt text

I have an object named O (set of linestripes).Its object-coordinate system is (x',y',z').
I translate,rotate this object in my OpenGL scene using following code snippet:

glPushMatrix();
 glTranslatef(Oz, Oy,Oz);
 glRotatef(rotationX , 1.0, 0.0, 0.0);  
 glRotatef(rotationY, 0.0, 1.0, 0.0);
 glRotatef(rotationZ, 0.0, 0.0, 1.0); 
contour->render();
glPopMatrix()

;

I have a point called H ,which is translated to (hx,hy,hz) world coordinates using

glPushMatrix();
 glTranslatef(hx,hy,hz);
glPopMatrix();

If I am correct, (Oz,Oy,Oz) and (hx,hy,hz) are world coordinates.

Now,what I want todo is calculate the position of H (hx,hy,hz) relative to O's object-coordinate system.(x',y',z');
As I understood,I can do this by calculating inverse transformations of object O and apply them to point H.

Any tips on this? Does OpenGL gives any functions for inverse-matrix calculation ? If I somehow found inverse-matrices what the order of multiplying them ?

Note : I want to implement "hammer" like tool where at point H ,I draw a sphere with radius R.User can use this sphere to chop the object O like a hammer.I have implemented this in 2D ,so I can use the same algorithm if I can calculate the hammer position
relative to (x',y',z')

Thanks in advance.

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

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

发布评论

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

评论(2

倾听心声的旋律 2024-10-13 01:32:12

反转矩阵将是通用解决方案,但据我所知,这实际上并不是一个“通用”问题。您不是撤消任意变换,而是尝试执行已知变换序列的相反操作,每个变换都可以非常简单地反转。如果您的对象到世界的变换是:

glTranslatef(Ox, Oy, Oz);
glRotatef(rotationX , 1.0, 0.0, 0.0);
glRotatef(rotationY, 0.0, 1.0, 0.0);
glRotatef(rotationZ, 0.0, 0.0, 1.0);

那么世界到对象的逆就是:

glRotatef(-rotationZ, 0.0, 0.0, 1.0);
glRotatef(-rotationY, 0.0, 1.0, 0.0);
glRotatef(-rotationX , 1.0, 0.0, 0.0);
glTranslatef(-Ox, -Oy, -Oz);

基本上,只需按照最初应用的相反顺序取消每个应用的变换即可。

Inverting the matrix would be the general solution, but as far as I can see this isn't actually a "general" problem. Rather than undoing an arbitrary transformation, you are trying to do the reverse of a known sequence of transformations, each of which can be inverted very simply. If your object-to-world transformation is:

glTranslatef(Ox, Oy, Oz);
glRotatef(rotationX , 1.0, 0.0, 0.0);
glRotatef(rotationY, 0.0, 1.0, 0.0);
glRotatef(rotationZ, 0.0, 0.0, 1.0);

Then the world-to-object inverse is just:

glRotatef(-rotationZ, 0.0, 0.0, 1.0);
glRotatef(-rotationY, 0.0, 1.0, 0.0);
glRotatef(-rotationX , 1.0, 0.0, 0.0);
glTranslatef(-Ox, -Oy, -Oz);

Basically, just back out each applied transformation in the opposite order originally applied.

咆哮 2024-10-13 01:32:12

是的,基本上你是对的,你可以通过平移矩阵

M = O^-1 * H

来执行这个操作,就像你已经猜到你需要 O 的逆矩阵一样。 OpenGL 不是一个数学库,它只处理渲染的东西。所以你必须自己实现反转。谷歌搜索“Gauss Jordan”以找到一种可能的算法。如果您可以绝对确定 O 仅由旋转和平移组成,即没有剪切或缩放,那么您可以通过转置左上角的 3x3 子矩阵并否定最右列的最上面 3 个元素(这利用了正交的性质)来捷径矩阵,如旋转矩阵,转置也是逆矩阵,左上角的 3x3 是旋转部分,平移的逆矩阵对其向量的元素求反,即最右边的上 3 个元素)。

Yes, basically you're right that you can perform this operation by the translation matrix

M = O^-1 * H

any like you already guessed you need the inverse of O for this. OpenGL is not a math library though, it only deals with rendering stuff. So you'll have to implement the inversion yourself. Google for "Gauss Jordan" to find one possible algorithm. If you can be absolutely sure, that O consists only of rotation and translation, i.e. no shearing or scaling, then you can shortcut by transposing the upper left 3x3 submatrix and negating the uppermost 3 elements of the rightmost column (this exploits the nature of orthogonal matrices, like rotation matrices, that the transpose is also the inverse, the upper left 3x3 is the rotational part, the inverse of a translation is negating the elements of it's vector which is the rightmost upper 3 elements).

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